teichmann@5863: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5863: * Software engineering by Intevation GmbH teichmann@5863: * teichmann@5863: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5863: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5863: * documentation coming with Dive4Elements River for details. teichmann@5863: */ teichmann@5863: teichmann@5831: package org.dive4elements.river.collections; ingo@346: ingo@944: import java.util.ArrayList; ingo@346: import java.util.HashMap; ingo@944: import java.util.List; ingo@346: import java.util.Map; ingo@346: ingo@346: import org.apache.log4j.Logger; ingo@346: teichmann@5831: import org.dive4elements.artifacts.ArtifactDatabase; teichmann@5831: import org.dive4elements.artifacts.ArtifactDatabaseException; teichmann@5831: import org.dive4elements.artifacts.CallContext; teichmann@5831: import org.dive4elements.artifacts.CallMeta; ingo@346: teichmann@5831: import org.dive4elements.artifactdatabase.state.DefaultOutput; teichmann@5831: import org.dive4elements.artifactdatabase.state.Facet; teichmann@5831: import org.dive4elements.artifactdatabase.state.Output; ingo@346: teichmann@5831: import org.dive4elements.river.artifacts.FLYSArtifact; teichmann@5831: import org.dive4elements.river.artifacts.model.ManagedFacetAdapter; ingo@346: ingo@346: felix@3149: /** felix@3149: * The OutputParsers task is to pull Artifacts from database and put felix@3149: * its outputs and facets into some structures. felix@3149: */ ingo@346: public class OutputParser { ingo@346: felix@1770: /** Constant XPath that points to the outputmodes of an artifact. */ ingo@346: public static final String XPATH_ARTIFACT_OUTPUTMODES = ingo@346: "/art:result/art:outputmodes/art:output"; ingo@346: ingo@346: private static Logger logger = Logger.getLogger(OutputParser.class); ingo@346: ingo@346: protected ArtifactDatabase db; ingo@346: protected CallMeta meta; ingo@944: protected CallContext context; ingo@346: felix@1779: /** Map outputs name to Output. */ ingo@346: protected Map outs; ingo@346: felix@1779: /** Map facets name to list of Facets. */ felix@1779: protected List facets; felix@1779: ingo@346: felix@1770: /** felix@1770: * @param db Database used to fetch artifacts, outputs and facets. felix@1770: */ ingo@944: public OutputParser(ArtifactDatabase db, CallContext context) { ingo@944: this.db = db; ingo@944: this.meta = context.getMeta(); ingo@944: this.context = context; ingo@944: this.outs = new HashMap(); felix@1779: this.facets = new ArrayList(); ingo@346: } ingo@346: ingo@346: felix@1770: /** felix@1779: * Gets raw artifact with given id and sorts outputs in mapping. felix@1770: * Converts Facets to ManagedFacets on the way. felix@1770: * @param uuid uuid of artifact to load from database. felix@1770: */ ingo@346: public void parse(String uuid) ingo@346: throws ArtifactDatabaseException ingo@346: { ingo@346: logger.debug("OutputParser.parse: " + uuid); ingo@346: ingo@944: FLYSArtifact flys = (FLYSArtifact) db.getRawArtifact(uuid); ingo@346: ingo@944: List outList = flys.getOutputs(context); ingo@346: ingo@2094: logger.debug(" has " + outList.size() + " Outputs."); ingo@2094: ingo@944: for (Output out: outList) { ingo@944: String name = out.getName(); ingo@2094: logger.debug("Process Output '" + name + "'"); ingo@346: ingo@944: Output o = outs.get(name); ingo@944: int pos = 1; ingo@346: ingo@944: if (o == null) { ingo@944: o = new DefaultOutput( ingo@944: out.getName(), ingo@944: out.getDescription(), ingo@944: out.getMimeType(), ingo@944: new ArrayList(), ingo@944: out.getType()); ingo@944: outs.put(name, o); ingo@944: } ingo@944: else { felix@1770: logger.debug("OutputParser.parse: Use 'old' Output"); ingo@944: pos = o.getFacets().size() + 1; ingo@944: } ingo@944: felix@1779: List mfacets = facet2ManagedFacet(uuid, out.getFacets(), pos); felix@1779: o.addFacets(mfacets); felix@1779: this.facets.addAll(mfacets); ingo@346: } ingo@346: } ingo@346: ingo@346: felix@1770: /** felix@1770: * Access mapping of Outputname to Output. felix@1770: */ ingo@346: public Map getOuts() { ingo@346: return outs; ingo@346: } ingo@346: ingo@346: felix@1779: /** felix@1779: * Access all facets. felix@1779: */ felix@1779: public List getFacets() { felix@1779: return this.facets; felix@1779: } felix@1779: felix@1779: felix@1779: /** felix@1779: * Creates a list of ManagedFacets from list of Facets. felix@1779: * @param pos Position of first facet (for each other the positions felix@1779: * will be increased). felix@1779: */ ingo@944: protected List facet2ManagedFacet( ingo@944: String uuid, ingo@944: List old, ingo@944: int pos) ingo@944: { ingo@944: List newFacets = new ArrayList(old.size()); ingo@346: ingo@2094: logger.debug("There are " + old.size() + " Facets for this Output."); ingo@2094: ingo@944: for (Facet f: old) { ingo@1715: newFacets.add(new ManagedFacetAdapter(f, uuid, pos++, 1, 1)); ingo@346: } ingo@346: ingo@944: return newFacets; ingo@346: } ingo@346: } ingo@346: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :