# HG changeset patch # User Sascha L. Teichmann # Date 1311199430 0 # Node ID f48cef242e7f85bac04ab9b98888381810e073b9 # Parent 2de1808503be64b4fde4087a43b9ef50d13255e9 Datacage: recommendation mechanism now accepts nodes to append results. flys-artifacts/trunk@2382 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 2de1808503be -r f48cef242e7f flys-artifacts/ChangeLog --- a/flys-artifacts/ChangeLog Wed Jul 20 20:51:13 2011 +0000 +++ b/flys-artifacts/ChangeLog Wed Jul 20 22:03:50 2011 +0000 @@ -1,3 +1,15 @@ +2011-07-21 Sascha L. Teichmann + + * src/main/java/de/intevation/flys/artifacts/services/meta/Builder.java: + Now it is possible to directly pass a Node as a root to the builder. + The owning document if fetch by Node.getOwnerDocument(). This is + useful if you want to generate the recommendation directly into + an already existing document under a given node. + + * src/main/java/de/intevation/flys/artifacts/services/meta/DataCage.java: + Changed the signature of recommend() to accept a node where to + append the recommendations. + 2011-07-20 Sascha L. Teichmann * src/main/java/de/intevation/flys/artifacts/services/meta/Builder.java: diff -r 2de1808503be -r f48cef242e7f flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/Builder.java --- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/Builder.java Wed Jul 20 20:51:13 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/Builder.java Wed Jul 20 22:03:50 2011 +0000 @@ -39,15 +39,22 @@ protected Document template; + protected static Document getOwnerDocument(Node node) { + Document document = node.getOwnerDocument(); + return document != null ? document : (Document)node; + } + + public class BuildHelper { - protected Document output; + protected Node output; + protected Document owner; protected StackFrames frames; protected Connection connection; protected Map statements; public BuildHelper( - Document output, + Node output, Connection connection, Map parameters ) { @@ -55,6 +62,7 @@ this.connection = connection; frames = new StackFrames(parameters); statements = new HashMap(); + owner = getOwnerDocument(output); } public void build(List elements) throws SQLException { @@ -146,7 +154,7 @@ return; } - Element element = output.createElement(attr); + Element element = owner.createElement(attr); NodeList children = current.getChildNodes(); for (int i = 0, N = children.getLength(); i < N; ++i) { @@ -161,7 +169,7 @@ { log.debug("dc:text"); String value = expand(current.getTextContent()); - parent.appendChild(output.createTextNode(value)); + parent.appendChild(owner.createTextNode(value)); } @@ -368,7 +376,7 @@ } } - Node copy = output.importNode(current, false); + Node copy = owner.importNode(current, false); NodeList children = current.getChildNodes(); for (int i = 0, N = children.getLength(); i < N; ++i) { @@ -386,23 +394,7 @@ this.template = template; } - public Document build(Connection connection) - throws SQLException - { - return build(connection, XMLUtils.newDocument(), null); - } - - public Document build( - Connection connection, - Document output, - Map parameters - ) - throws SQLException - { - NodeList roots = template.getElementsByTagNameNS( - DC_NAMESPACE_URI, "template"); - - BuildHelper helper = new BuildHelper(output, connection, parameters); + protected static List rootsToList(NodeList roots) { List elements = new ArrayList(); @@ -415,9 +407,23 @@ } } } - helper.build(elements); - return output; + return elements; + } + + public void build( + Connection connection, + Node output, + Map parameters + ) + throws SQLException + { + BuildHelper helper = new BuildHelper(output, connection, parameters); + + NodeList roots = template.getElementsByTagNameNS( + DC_NAMESPACE_URI, "template"); + + helper.build(rootsToList(roots)); } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : diff -r 2de1808503be -r f48cef242e7f flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/DataCage.java --- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/DataCage.java Wed Jul 20 20:51:13 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/DataCage.java Wed Jul 20 22:03:50 2011 +0000 @@ -15,6 +15,7 @@ import org.apache.log4j.Logger; import org.w3c.dom.Document; +import org.w3c.dom.Node; import org.hibernate.Session; @@ -52,23 +53,11 @@ return builder; } - public Document recommend(FLYSArtifact artifact, String [] outs) { - return recommend(artifact, outs, null); - } - - public Document recommend( - FLYSArtifact artifact, - String [] outs, - Map extraParameters + protected static void artifactToParameters( + FLYSArtifact artifact, + Map parameters ) { - Map parameters = new HashMap(); - - if (extraParameters != null) { - parameters.putAll(extraParameters); - } - parameters.put("current-state-id", artifact.getCurrentStateId()); - parameters.put("artifact-outs", outs); for (StateData sd: artifact.getAllData()) { Object value = sd.getValue(); @@ -78,16 +67,34 @@ String key = sd.getName().replace('.', '-'); parameters.put(key, value); } - - return process(parameters); } - public Document process(final Map parameters) { - final Document result = XMLUtils.newDocument(); + public void recommend( + FLYSArtifact artifact, + String [] outs, + Map extraParameters, + Node result + ) { + Map parameters = new HashMap(); + if (extraParameters != null) { + parameters.putAll(extraParameters); + } + + artifactToParameters(artifact, parameters); + + parameters.put("artifact-outs", outs); + + recommend(parameters, result); + } + + public void recommend( + final Map parameters, + final Node result + ) { if (builder != null) { log.error("builder not configured properly."); - return result; + return; } Session session = SessionHolder.HOLDER.get(); @@ -100,8 +107,6 @@ builder.build(connection, result, parameters); } }); - - return result; } public static synchronized DataCage getInstance() {