changeset 6976:c47f1a1d0f0f

D4EArtifactCollection: Do not push CallContext and reference to db around. Instead, store a reference initially.
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Fri, 06 Sep 2013 15:44:02 +0200
parents 8bcc120e64ae
children e1010a0f0b05
files artifacts/src/main/java/org/dive4elements/river/collections/D4EArtifactCollection.java
diffstat 1 files changed, 79 insertions(+), 49 deletions(-) [+]
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/collections/D4EArtifactCollection.java	Fri Sep 06 15:40:38 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/collections/D4EArtifactCollection.java	Fri Sep 06 15:44:02 2013 +0200
@@ -74,6 +74,22 @@
     public static final String XPATH_LOADED_RECOMMENDATIONS =
         "/art:attribute/art:loaded-recommendations";
 
+    private CallContext context;
+
+    private ArtifactDatabase db;
+
+    protected CallContext getContext() {
+        return this.context;
+    }
+
+    protected ArtifactDatabase getArtifactDB() {
+        return this.db;
+    }
+
+    protected void setContext(CallContext context) {
+        this.context = context;
+        this.db = context.getDatabase();
+    }
 
     /**
      * Create and return description Document for this collection.
@@ -82,26 +98,26 @@
     public Document describe(CallContext context) {
         log.debug("D4EArtifactCollection.describe: " + identifier);
 
+        setContext(context);
+
         CollectionDescriptionHelper helper = new CollectionDescriptionHelper(
             getName(), identifier(), getCreationTime(), getTTL(),
             context);
 
-        ArtifactDatabase db = context.getDatabase();
 
         Document        oldAttrs = getAttribute();
         AttributeParser parser   = new AttributeParser(oldAttrs);
 
         try {
-            String[] aUUIDs  = getArtifactUUIDs(context);
+            String[] aUUIDs  = getArtifactUUIDs();
 
-            oldAttrs = removeAttributes(oldAttrs, context);
+            oldAttrs = removeAttributes(oldAttrs);
             parser   = new AttributeParser(oldAttrs);
 
-            CollectionAttribute newAttr = mergeAttributes(
-                db, context, parser, aUUIDs);
+            CollectionAttribute newAttr = mergeAttributes(parser, aUUIDs);
 
-            if (checkOutputSettings(newAttr, context)) {
-                saveCollectionAttribute(db, context, newAttr);
+            if (checkOutputSettings(newAttr)) {
+                saveCollectionAttribute(newAttr);
             }
 
             helper.setAttribute(newAttr);
@@ -129,13 +145,11 @@
      * @param uuids Artifact uuids.
      */
     protected CollectionAttribute mergeAttributes(
-        ArtifactDatabase db,
-        CallContext      context,
         AttributeParser  oldParser,
         String[]         uuids
     ) {
         CollectionAttribute cAttribute =
-            buildOutAttributes(db, context, oldParser, uuids);
+            buildOutAttributes(oldParser, uuids);
 
         if (cAttribute == null) {
             log.warn("mergeAttributes: cAttribute == null");
@@ -145,13 +159,19 @@
         cAttribute.setLoadedRecommendations(
             getLoadedRecommendations(oldParser.getAttributeDocument()));
 
-        saveCollectionAttribute(db, context, cAttribute);
+        saveCollectionAttribute(cAttribute);
 
         return cAttribute;
     }
 
 
-    protected Document removeAttributes(Document attrs, CallContext context) {
+    /**
+     * Remove those output-elements which have a name that does
+     * not appear in master artifacts out-list.
+     * @param attr[in,out] Document to clean and return.
+     * @return param attr.
+     */
+    protected Document removeAttributes(Document attrs) {
         Node outs = (Node) XMLUtils.xpath(
             attrs,
             "/art:attribute/art:outputs",
@@ -167,7 +187,7 @@
         if (nodes != null) {
             for (int i = 0; i < nodes.getLength(); i++) {
                 Element e = (Element)nodes.item(i);
-                if(!outputExists(e.getAttribute("name"), context)) {
+                if(!outputExists(e.getAttribute("name"))) {
                     outs.removeChild(e);
                 }
             }
@@ -182,9 +202,9 @@
      * @param context current context
      * @return true if current master artifact has given output.
      */
-    protected boolean outputExists(String name, CallContext context) {
-        D4EArtifact master = getMasterArtifact(context);
-        List<Output> outList = master.getOutputs(context);
+    protected boolean outputExists(String name) {
+        D4EArtifact master = getMasterArtifact();
+        List<Output> outList = master.getOutputs(getContext());
 
         for (Output o : outList) {
             if (name.equals(o.getName())) {
@@ -203,8 +223,6 @@
      * @return true, if the transaction was successful, otherwise false.
      */
     protected boolean saveCollectionAttribute(
-        ArtifactDatabase    db,
-        CallContext         context,
         CollectionAttribute attribute
     ) {
         log.info("Save new CollectionAttribute into database.");
@@ -213,7 +231,7 @@
 
         try {
             // Save the merged document into database.
-            db.setCollectionAttribute(identifier(), context.getMeta(), doc);
+            getArtifactDB().setCollectionAttribute(identifier(), getContext().getMeta(), doc);
 
             log.info("Saving CollectionAttribute was successful.");
 
@@ -254,8 +272,7 @@
      * @return true, if the CollectionAttribute was modified, otherwise false.
      */
     protected boolean checkOutputSettings(
-        CollectionAttribute attribute,
-        CallContext         cc
+        CollectionAttribute attribute
     ) {
         boolean modified = false;
 
@@ -281,7 +298,7 @@
             if (settings == null) {
                 log.debug("No Settings set for Output '" + outName + "'.");
                 output.setSettings(
-                    createInitialOutputSettings(cc, attribute, outName));
+                    createInitialOutputSettings(attribute, outName));
 
                 modified = true;
             }
@@ -302,11 +319,10 @@
      * @return a default Settings object for the specified Output.
      */
     protected Settings createInitialOutputSettings(
-        CallContext         cc,
         CollectionAttribute attr,
         String              out
     ) {
-        OutGenerator outGen = RiverContext.getOutGenerator(cc, out, null);
+        OutGenerator outGen = RiverContext.getOutGenerator(getContext(), out, null);
 
         if (outGen == null) {
             return null;
@@ -314,13 +330,13 @@
 
         // XXX NOTE: the outGen is not able to process its generate() operation,
         // because it has no OutputStream set!
-        outGen.init(XMLUtils.newDocument(), null, cc);
-        prepareMasterArtifact(outGen, cc);
+        outGen.init(XMLUtils.newDocument(), null, getContext());
+        prepareMasterArtifact(outGen);
 
         try {
-            Document outAttr = getAttribute(cc, attr, out);
+            Document outAttr = getAttribute(attr, out);
             OutputHelper helper = new OutputHelper(identifier());
-            helper.doOut(outGen, out, out, outAttr, cc);
+            helper.doOut(outGen, out, out, outAttr, getContext());
         }
         catch (ArtifactDatabaseException adbe) {
             log.error(adbe, adbe);
@@ -343,6 +359,8 @@
     {
         boolean debug = log.isDebugEnabled();
 
+        setContext(context);
+
         long reqBegin = System.currentTimeMillis();
 
         if (debug) {
@@ -402,10 +420,10 @@
         generator.init(format, out, context);
         generator.setSettings(settings);
         generator.setCollection(this);
-        prepareMasterArtifact(generator, context);
+        prepareMasterArtifact(generator);
 
         try {
-            Document attr = getAttribute(context, cAttr, name);
+            Document attr = getAttribute(cAttr, name);
             OutputHelper helper = new OutputHelper(identifier());
             if (name.equals("sq_overview")) {
                 helper.doOut(generator, name, subtype, format, context);
@@ -428,12 +446,11 @@
      * Sets the master Artifact at the given <i>generator</i>.
      *
      * @param generator The generator that gets a master Artifact.
-     * @param cc The CallContext.
      */
-    protected void prepareMasterArtifact(OutGenerator generator, CallContext cc
+    protected void prepareMasterArtifact(OutGenerator generator
     ) {
         // Get master artifact.
-        D4EArtifact master = getMasterArtifact(cc);
+        D4EArtifact master = getMasterArtifact();
         if (master != null) {
             log.debug("Set master Artifact to uuid: " + master.identifier());
             generator.setMasterArtifact(master);
@@ -447,18 +464,18 @@
     /**
      * @return masterartifact or null if exception/not found.
      */
-    protected D4EArtifact getMasterArtifact(CallContext context)
+    protected D4EArtifact getMasterArtifact()
     {
         try {
-            ArtifactDatabase db = context.getDatabase();
-            CallMeta callMeta   = context.getMeta();
+            ArtifactDatabase db = getArtifactDB();
+            CallMeta callMeta   = getContext().getMeta();
             Document document   = db.getCollectionsMasterArtifact(
                 identifier(), callMeta);
 
             String masterUUID   = XMLUtils.xpathString(
                 document, XPATH_MASTER_UUID, ArtifactNamespaceContext.INSTANCE);
             D4EArtifact masterArtifact =
-                (D4EArtifact) getArtifact(masterUUID, context);
+                (D4EArtifact) getArtifact(masterUUID);
             return masterArtifact;
         }
         catch (ArtifactDatabaseException ade) {
@@ -473,8 +490,6 @@
      * @param uuids List of artifact uuids.
      */
     protected CollectionAttribute buildOutAttributes(
-        ArtifactDatabase db,
-        CallContext      context,
         AttributeParser  aParser,
         String[]         uuids)
     {
@@ -487,14 +502,16 @@
             return null;
         }
 
-        D4EArtifact masterArtifact = getMasterArtifact(context);
+        D4EArtifact masterArtifact = getMasterArtifact();
 
         if (masterArtifact == null) {
             log.debug("buildOutAttributes: masterArtifact == null");
             return null;
         }
 
-        OutputParser oParser = new OutputParser(db, context);
+        OutputParser oParser = new OutputParser(
+            getArtifactDB(),
+            getContext());
 
         if (uuids != null) {
             for (String uuid: uuids) {
@@ -510,7 +527,7 @@
         aParser.parse();
 
         AttributeWriter aWriter = new AttributeWriter(
-            db,
+            getArtifactDB(),
             aParser.getCollectionAttribute(),
             aParser.getOuts(),
             aParser.getFacets(),
@@ -533,7 +550,6 @@
      * @return the attribute for the desired output type.
      */
     protected Document getAttribute(
-        CallContext         context,
         CollectionAttribute cAttr,
         String              output)
     throws    ArtifactDatabaseException
@@ -572,13 +588,13 @@
      *
      * @return a list of uuids.
      */
-    protected String[] getArtifactUUIDs(CallContext context)
+    protected String[] getArtifactUUIDs()
     throws    ArtifactDatabaseException
     {
         log.debug("D4EArtifactCollection.getArtifactUUIDs");
 
-        ArtifactDatabase db = context.getDatabase();
-        CallMeta meta       = context.getMeta();
+        ArtifactDatabase db = getArtifactDB();
+        CallMeta meta       = getContext().getMeta();
 
         Document itemList = db.listCollectionArtifacts(identifier(), meta);
         NodeList items    = (NodeList) XMLUtils.xpath(
@@ -619,7 +635,7 @@
      *
      * @return an Artifact.
      */
-    protected Artifact getArtifact(String uuid, CallContext context)
+    protected Artifact getArtifact(String uuid)
     throws    ArtifactDatabaseException
     {
         log.debug("D4EArtifactCollection.getArtifact");
@@ -640,11 +656,25 @@
      */
     public List<Artifact> getArtifactsByName(String name, CallContext context)
     {
+        setContext(context);
+        return getArtifactsByName(name);
+    }
+
+
+    /**
+     * Returns artifacts with name name.
+     *
+     * @param name The Artifact name to search
+     *
+     * @return a list of artifacts matching this name.
+     */
+    protected List<Artifact> getArtifactsByName(String name)
+    {
         log.debug("Searching for Artifacts: " + name);
         List<Artifact> ret =  new ArrayList<Artifact>();
         try {
-            for (String uuid: getArtifactUUIDs(context)) {
-                D4EArtifact subArt = (D4EArtifact)getArtifact(uuid, context);
+            for (String uuid: getArtifactUUIDs()) {
+                D4EArtifact subArt = (D4EArtifact) getArtifact(uuid);
                 if (subArt.getName() != null && subArt.getName().equals(name)) {
                     ret.add(subArt);
                 }

http://dive4elements.wald.intevation.org