diff gnv-artifacts/src/main/java/de/intevation/gnv/state/StateBase.java @ 607:292fbcd5e9ac

Parted the cache blob into pieces and changed the way how user input is stored on each state. gnv-artifacts/trunk@673 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 10 Feb 2010 11:43:35 +0000
parents e8ebdbc7f1e3
children 74dfb9346574
line wrap: on
line diff
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/state/StateBase.java	Tue Feb 09 15:23:07 2010 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/state/StateBase.java	Wed Feb 10 11:43:35 2010 +0000
@@ -14,6 +14,8 @@
 
 import javax.xml.xpath.XPathConstants;
 
+import net.sf.ehcache.Cache;
+
 import org.apache.log4j.Logger;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
@@ -59,8 +61,8 @@
      */
     private static Logger log = Logger.getLogger(GNVArtifactBase.class);
 
-    private final static String MINVALUEFIELDNAME = "minvalue";
-    private final static String MAXVALUEFIELDNAME = "maxvalue";
+    protected final static String MINVALUEFIELDNAME = "minvalue";
+    protected final static String MAXVALUEFIELDNAME = "maxvalue";
 
     private final static String NODATASELECTIONKEY = "n/n";
 
@@ -88,13 +90,13 @@
 
     protected Collection<String> inputValueNames = null;
 
-    private Map<String, InputValue> inputValues = null;
+    protected Map<String, InputValue> inputValues = null;
 
-    private State parent = null;
+    protected State parent = null;
 
     protected Map<String, InputData> inputData = null;
 
-    private Map<String, InputData> preSettings = null;
+    protected Map<String, InputData> preSettings = null;
 
     /**
      * Constructor
@@ -128,7 +130,7 @@
 
 
     public void reset(String uuid) {
-        
+
         if (parent != null) {
             inputData = parent.inputData();
         }
@@ -139,7 +141,6 @@
      * @see de.intevation.gnv.state.State#setup(org.w3c.dom.Node)
      */
     public void setup(Node configuration) {
-        log.debug("StateBase.setup");
         this.id = ((Element)configuration).getAttribute("id");
         this.description = ((Element)configuration).getAttribute("description");
 
@@ -167,7 +168,6 @@
                                                           inputValueNode.getAttribute("type"), 
                                                           Boolean.parseBoolean(inputValueNode.
                                                           getAttribute("multiselect")), usedinQuery);
-            log.debug(inputValue.toString());
             this.inputValues.put(inputValue.getName(), inputValue);
             this.inputValueNames.add(inputValue.getName());
         }
@@ -207,12 +207,10 @@
         this.parent = state;
     }
 
-    /**
-     * @see de.intevation.gnv.state.State#putInputData(java.util.Collection)
-     */
-    public void putInputData(Collection<InputData> inputData, String uuid)
-                                                                          throws StateException {
-        log.debug("StateBase.putInputData");
+
+    public void feed(Collection<InputData> inputData, String uuid)
+    throws StateException
+    {
         if (inputData != null) {
             Iterator<InputData> it = inputData.iterator();
             InputValidator iv = new InputValidator();
@@ -224,7 +222,7 @@
                         this.inputData = new HashMap<String, InputData>(
                                 inputData.size());
                     }
-                    
+
                     boolean valid = iv.isInputValid(tmpItem.getValue(),
                             inputValue.getType());
                     if (valid) {
@@ -239,8 +237,8 @@
                                 log.warn(errMsg);
                                 throw new StateException(errMsg);
                             }
-                            
-                            valid = iv.isInputValid(minValue, 
+
+                            valid = iv.isInputValid(minValue,
                                     maxValue,
                                     inputValue.getType());
                             if (!valid){
@@ -259,8 +257,8 @@
                                 log.warn(errMsg);
                                 throw new StateException(errMsg);
                             }
-                            
-                            valid = iv.isInputValid(minValue, 
+
+                            valid = iv.isInputValid(minValue,
                                                     maxValue,
                                                     inputValue.getType());
                             if (!valid){
@@ -269,7 +267,6 @@
                                 throw new StateException(errMsg);
                             }
                         }
-                        this.setSelection(tmpItem, uuid);
                         this.inputData.put(tmpItem.getName(), tmpItem);
                     } else {
                         String errMsg = "Wrong input for " + tmpItem.getValue()
@@ -292,6 +289,99 @@
     }
 
     /**
+     * @see de.intevation.gnv.state.State#putInputData(java.util.Collection)
+     */
+    public void putInputData(Collection<InputData> inputData, String uuid)
+    throws StateException {
+        if (inputData != null) {
+            Iterator<InputData> it = inputData.iterator();
+            InputValidator iv = new InputValidator();
+            while (it.hasNext()) {
+                InputData tmpItem = it.next();
+                InputValue inputValue = this.inputValues.get(tmpItem.getName());
+                if (inputValue != null) {
+                    if (this.inputData == null) {
+                        this.inputData = new HashMap<String, InputData>(
+                                inputData.size());
+                    }
+
+                    boolean valid = iv.isInputValid(tmpItem.getValue(),
+                            inputValue.getType());
+                    if (valid) {
+                        if (tmpItem.getName().equals(MINVALUEFIELDNAME)){
+                            String minValue = tmpItem.getValue();
+                            String maxValue = this.getInputValue4ID(inputData, MAXVALUEFIELDNAME);
+                            valid = iv.isInputValid(maxValue,inputValue.getType());
+                            if (!valid){
+                                String errMsg = "Wrong input for " + tmpItem.getValue()
+                                                + " is not an " + inputValue.getType()
+                                                + " Value.";
+                                log.warn(errMsg);
+                                throw new StateException(errMsg);
+                            }
+
+                            valid = iv.isInputValid(minValue,
+                                    maxValue,
+                                    inputValue.getType());
+                            if (!valid){
+                                String errMsg = "MaxValue-Input is less than MinValue-Input ";
+                                log.warn(errMsg);
+                                throw new StateException(errMsg);
+                            }
+                        }else if (tmpItem.getName().equals(MAXVALUEFIELDNAME)){
+                            String minValue = this.getInputValue4ID(inputData, MINVALUEFIELDNAME);
+                            String maxValue = tmpItem.getValue();
+                            valid = iv.isInputValid(minValue,inputValue.getType());
+                            if (!valid){
+                                String errMsg = "Wrong input for " + tmpItem.getValue()
+                                                + " is not an " + inputValue.getType()
+                                                + " Value.";
+                                log.warn(errMsg);
+                                throw new StateException(errMsg);
+                            }
+
+                            valid = iv.isInputValid(minValue,
+                                                    maxValue,
+                                                    inputValue.getType());
+                            if (!valid){
+                                String errMsg = "MaxValue-Input is less than MinValue-Input ";
+                                log.warn(errMsg);
+                                throw new StateException(errMsg);
+                            }
+                        }
+                        this.inputData.put(tmpItem.getName(), tmpItem);
+                    } else {
+                        String errMsg = "Wrong input for " + tmpItem.getValue()
+                                        + " is not an " + inputValue.getType()
+                                        + " Value.";
+                        log.warn(errMsg);
+                        throw new StateException(errMsg);
+                    }
+
+                } else {
+                    String errMsg = "No Inputvalue given for Inputdata "
+                                    + tmpItem.getName();
+                    log.warn(errMsg + "Value will be ignored");
+
+                }
+            }
+        } else {
+            log.warn("No Inputdata given");
+        }
+
+        debugInputData();
+    }
+
+    private void debugInputData() {
+        if (log.isDebugEnabled()) {
+            Iterator iter = inputData.keySet().iterator();
+            while (iter.hasNext()) {
+                String key = (String) iter.next();
+            }
+        }
+    }
+
+    /**
      * 
      * @see de.intevation.gnv.state.State#setPreSettings(java.util.Map)
      */
@@ -311,7 +401,6 @@
     }
 
     private void setSelection(InputData inputData, String uuid) {
-        log.debug("StateBase.setSelection");
 
         Object o = this.getDescribeData(inputData.getName(),uuid);
         if (o != null) {
@@ -348,7 +437,6 @@
     }
 
     private Object getDescribeData(String name, String uuid) {
-        log.debug("StateBase.getDescribeData");
         Collection<Object> descibeData = this.getDescibeData(uuid);
         if (descibeData != null) {
             Iterator<Object> it = descibeData.iterator();
@@ -383,27 +471,14 @@
     throws StateException
     {
     }
-    
+
     public void initialize(String uuid, CallContext context)
     throws StateException
     {
-        log.debug("StateBase.initialize");
         try {
-            String[] filterValues = this
-                    .generateFilterValuesFromInputData();
-            Collection<Result> result = null;
-            try {
-                if (this.queryID != null) {
-                    QueryExecutor queryExecutor = QueryExecutorFactory
-                            .getInstance().getQueryExecutor();
-                    result = queryExecutor.executeQuery(this.queryID,
-                            filterValues);
-                }
-                this.purifyResult(result, uuid);
-            } catch (RuntimeException e) {
-                log.error(e, e);
-            }
-        } catch (QueryException e) {
+            getDescibeData(uuid);
+        }
+        catch (RuntimeException e) {
             log.error(e, e);
             throw new StateException(e);
         }
@@ -449,7 +524,6 @@
     }
 
     private String prepareInputData4DateDBQuery(String value) {
-        log.debug("StateBase.prepareInputData4DateDBQuery");
         if (value != null) {
             String[] values = value.split(",");
             String newValue = "";
@@ -468,7 +542,6 @@
     }
 
     private String prepareInputData4DBQuery(String value) {
-        log.debug("StateBase.prepareInputData4DBQuery");
         if (value != null) {
             String[] values = value.split(",");
             String newValue = "";
@@ -488,15 +561,15 @@
     /**
      * @param result
      */
-    protected void purifyResult(Collection<Result> result, String uuid) {
-        log.debug("StateBase.purifyResult");
-        List<Object> describeData = this.getDescibeData(uuid);
-        if (describeData == null) {
-            describeData = new ArrayList<Object>();
-        }
-        NamedCollection<KeyValueDescibeData> keyValueDescibeData = extractKVP(result, "KEY", "VALUE");
-        describeData.add(keyValueDescibeData); 
-        this.setDescibeData(uuid, describeData);
+    protected List<Object> purifyResult(Collection<Result> result, String uuid) {
+        List<Object> describeData = new ArrayList<Object>();
+
+        NamedCollection<KeyValueDescibeData> keyValueDescibeData =
+            extractKVP(result, "KEY", "VALUE");
+
+        describeData.add(keyValueDescibeData);
+
+        return describeData;
     }
 
     /**
@@ -508,11 +581,11 @@
                                                               String valueid) {
         Iterator<Result> rit = result.iterator();
         int dataSize = (this.dataNoSelect ? result.size()+1 : result.size());
-        
+
         NamedCollection<KeyValueDescibeData> keyValueDescibeData = new NamedArrayList<KeyValueDescibeData>(
                 this.dataName, dataSize);
         keyValueDescibeData.setMultiSelect(this.dataMultiSelect);
-        
+
         if (this.dataNoSelect){
             keyValueDescibeData.add(new DefaultKeyValueDescribeData(
                 NODATASELECTIONKEY,
@@ -525,12 +598,12 @@
         int     keyPos      = 0;
         int     valuePos    = 1;
         String  previousKey = null;
-        InputData preSettingsData = (this.preSettings != null && 
-                                    this.preSettingsName != null) ? 
-                                    this.preSettings.get(this.preSettingsName) :
-                                    null;
+        InputData preSettingsData =
+            (this.preSettings != null && this.preSettingsName != null)
+                ? this.preSettings.get(this.preSettingsName)
+                : null;
         boolean filterWithPresettings = preSettingsData != null;
-        
+
         List<String> preSettingValues = null;
         if(filterWithPresettings){
             preSettingValues = Arrays.asList(preSettingsData.splitValue());
@@ -546,7 +619,7 @@
                 initialized = true;
             }
             String tmpKey = resultValue.getString(keyPos);
-           
+
             // TODO: FIXME: We have to do that because the arcsde does not 
             //       support a distinct Query on Layers.
             if (previousKey == null || !tmpKey.equals(previousKey)){
@@ -612,70 +685,12 @@
      *      org.w3c.dom.Node, de.intevation.artifacts.CallMeta,
      *      java.lang.String)
      */
-    /*
-    public void describe(
-        Document    document,
-        Node        rootNode,
-        CallContext context,
-        String      uuid
-    ) {
-        log.debug("StateBase.describe");
-        CallMeta callMeta = context.getMeta();
-
-        List<Object> descibeData = this.getDescibeData(uuid);
-        if (descibeData != null) {
-            Iterator<Object> it = descibeData.iterator();
-
-            XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator(
-                document,
-                ArtifactNamespaceContext.NAMESPACE_URI,
-                ArtifactNamespaceContext.NAMESPACE_PREFIX
-            );
-
-            Node staticNode = (Node) XMLUtils.xpath(
-                rootNode,
-                XPATH_STATIC_UI,
-                XPathConstants.NODE,
-                ArtifactNamespaceContext.INSTANCE
-            );
-
-            Node dynamic = (Node) XMLUtils.xpath(
-                rootNode,
-                XPATH_DYNAMIC_UI,
-                XPathConstants.NODE,
-                ArtifactNamespaceContext.INSTANCE
-            );
-
-            XMLUtils.ElementCreator xCreator = new XMLUtils.ElementCreator(
-                document,
-                XMLUtils.XFORM_URL,
-                XMLUtils.XFORM_PREFIX
-            );
-
-            while (it.hasNext()) {
-                Object o = it.next();
-                if ((!it.hasNext() && dataName != null)) {
-                    appendToDynamicNode(
-                        creator, xCreator, document, dynamic, callMeta, o);
-                }
-                else {
-                    appendToStaticNode(
-                        creator, xCreator, document, staticNode, callMeta, o);
-                }
-            }
-        }
-    }
-    */
-
-
     public void describe(
         Document    document,
         Node        rootNode,
         CallContext context,
         String      uuid)
     {
-        log.debug("StateBase.describe");
-
         XMLUtils.ElementCreator xCreator = new XMLUtils.ElementCreator(
             document,
             XMLUtils.XFORM_URL,
@@ -720,9 +735,12 @@
         CallContext             context,
         String                  uuid)
     {
-        log.debug("StateBase.describeDynamic");
         CallMeta callMeta = context.getMeta();
 
+        String newInput = findNewValue();
+        if (newInput == null && parent != null)
+            return;
+
         List<Object> descibeData = getDescibeData(uuid);
         if (descibeData != null) {
             Iterator<Object> it = descibeData.iterator();
@@ -835,7 +853,7 @@
             } else {
                 Object[] names = this.inputValueNames.toArray();
                 name = names[names.length - 1].toString();
-            } 
+            }
 
             Element selectNode = creator.create(multiselect?"select":"select1");
             creator.addAttr(selectNode, "ref", name);
@@ -994,33 +1012,77 @@
         node.appendChild(groupNode);
     }
 
-    /**
-     * @see de.intevation.gnv.state.State#getDescibeData()
-     */
-    protected List<Object> getDescibeData(String uuid) {
-        if (CacheFactory.getInstance().isInitialized()) {
-            String key = uuid + DESCRIBEDATAKEY;
-            log.debug("Hash for Queryelements: " + key);
-            net.sf.ehcache.Element value = CacheFactory.getInstance().getCache().get(key);
-            if (value != null) {
-                return (List<Object>) (value.getObjectValue());
-            }
-        }
-        return null;
+
+    protected String getHash(String uuid) {
+        return uuid + id + inputData.hashCode();
     }
 
+
     /**
      * @see de.intevation.gnv.state.State#getDescibeData()
      */
-    protected void setDescibeData(String uuid, List<Object> describeData) {
-        
-        if (CacheFactory.getInstance().isInitialized()) {
-            String key = uuid + DESCRIBEDATAKEY;
-            log.debug("Hash for Queryelements: " + key);
-            CacheFactory.getInstance().getCache().put(new net.sf.ehcache.Element(key, describeData));
+    public List<Object> getDescibeData(String uuid) {
+        CacheFactory factory = CacheFactory.getInstance();
+        if (factory.isInitialized()) {
+            // we use a cache
+            Cache cache = factory.getCache();
+
+            String key = getHash(uuid);
+
+            net.sf.ehcache.Element value = cache.get(key);
+            if (value != null) {
+                // element already in cache, so return it.
+                return (List<Object>) (value.getObjectValue());
+            }
+            else {
+                // element is not in cache yet, so we need to fetch data from
+                // database and put it into cache right now
+                try {
+                    String[] filterValues = generateFilterValuesFromInputData();
+                    List<Object> data     = queryDatabase(filterValues, uuid);
+
+                    cache.put(new net.sf.ehcache.Element(key, data));
+
+                    return data;
+                }
+                catch (QueryException qe) {
+                    log.error(qe, qe);
+                }
+            }
         }
+        else {
+            // we don't use a cache, so we have to query the database every
+            // single time
+            String[] filterValues     = generateFilterValuesFromInputData();
+            Collection<Result> result = null;
+            try {
+                return queryDatabase(filterValues, uuid);
+            }
+            catch (RuntimeException e) {
+                log.error(e, e);
+            }
+            catch (QueryException e) {
+                log.error(e, e);
+            }
+        }
+
+        return null;
     }
 
+    protected List<Object> queryDatabase(String[] filterValues, String uuid)
+    throws QueryException {
+        Collection<Result> result = null;
+
+        if (queryID != null) {
+            QueryExecutor queryExecutor =
+                QueryExecutorFactory.getInstance().getQueryExecutor();
+
+            result = queryExecutor.executeQuery(queryID, filterValues);
+        }
+        return purifyResult(result, uuid);
+    }
+
+
     public Map<String, InputData> inputData() {
         return inputData;
     }

http://dive4elements.wald.intevation.org