# HG changeset patch # User Ingo Weinzierl # Date 1314196538 0 # Node ID feaf778d510e2f51f8159982ea475579f8a540c0 # Parent 21b9eb24bd477e371d88829c097d98a2abd47443 Changed the way of collecting the data for the RiverAxisArtifact - use the database instead of a Master-Artifact. flys-artifacts/trunk@2568 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 21b9eb24bd47 -r feaf778d510e flys-artifacts/ChangeLog --- a/flys-artifacts/ChangeLog Wed Aug 24 14:30:27 2011 +0000 +++ b/flys-artifacts/ChangeLog Wed Aug 24 14:35:38 2011 +0000 @@ -1,3 +1,13 @@ +2011-08-24 Ingo Weinzierl + + * src/main/java/de/intevation/flys/artifacts/model/RiverFactory.java: + Added a method that returns a River object based on its database id. + + * src/main/java/de/intevation/flys/artifacts/RiverAxisArtifact.java: The + data that is required for this artifact is fetched from database instead + from a Master-Artifact. The creation of static artifacts should use + database ids instead of cloning a Master-Artifact. + 2011-08-24 Ingo Weinzierl * src/main/java/de/intevation/flys/artifacts/MainValuesArtifact.java: diff -r 21b9eb24bd47 -r feaf778d510e flys-artifacts/src/main/java/de/intevation/flys/artifacts/RiverAxisArtifact.java --- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/RiverAxisArtifact.java Wed Aug 24 14:30:27 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/RiverAxisArtifact.java Wed Aug 24 14:35:38 2011 +0000 @@ -3,13 +3,23 @@ import java.util.ArrayList; import java.util.List; +import org.w3c.dom.Document; + import org.apache.log4j.Logger; import de.intevation.artifacts.Artifact; +import de.intevation.artifacts.ArtifactFactory; import de.intevation.artifacts.CallMeta; +import de.intevation.artifacts.common.ArtifactNamespaceContext; +import de.intevation.artifacts.common.utils.XMLUtils; + +import de.intevation.artifactdatabase.data.DefaultStateData; import de.intevation.artifactdatabase.state.Facet; +import de.intevation.flys.model.River; + +import de.intevation.flys.artifacts.model.RiverFactory; import de.intevation.flys.artifacts.states.DefaultState; @@ -17,6 +27,8 @@ public static final String NAME = "riveraxis"; + public static final String XPATH_IDS = "/art:action/art:ids/@value"; + private static final Logger logger = Logger.getLogger(RiverAxisArtifact.class); @@ -29,21 +41,60 @@ @Override - protected void initialize(Artifact artifact, Object context, CallMeta meta) { - logger.debug("Initialize internal state with: "+ artifact.identifier()); - - FLYSArtifact flys = (FLYSArtifact) artifact; - addData("river", flys.getData("river")); + public void setup( + String identifier, + ArtifactFactory factory, + Object context, + CallMeta callMeta, + Document data) + { + logger.debug("RiverAxisArtifact.setup"); - List fs = new ArrayList(); + super.setup(identifier, factory, context, callMeta, data); - // TODO Add CallMeta - DefaultState state = (DefaultState) getCurrentState(context); - state.computeInit(this, hash(), context, meta, fs); + String ids = XMLUtils.xpathString( + data, XPATH_IDS, ArtifactNamespaceContext.INSTANCE); - if (!fs.isEmpty()) { - facets.put(getCurrentStateId(), fs); + String[] splitted = ids != null ? ids.split(" ") : null; + + if (splitted != null && splitted.length > 0) { + try { + int river_id = Integer.parseInt(splitted[0]); + River river = RiverFactory.getRiver(river_id); + + if (river == null) { + throw new IllegalArgumentException( + "No river found for id: " + river_id); + } + + String name = river.getName(); + + addData("river", new DefaultStateData("river",null,null,name)); + + List fs = new ArrayList(); + + DefaultState state = (DefaultState) getCurrentState(context); + state.computeInit(this, hash(), context, callMeta, fs); + + if (!fs.isEmpty()) { + facets.put(getCurrentStateId(), fs); + } + } + catch (NumberFormatException nfe) { + logger.error("Could not create Artifact: " + nfe.getMessage()); + throw new IllegalArgumentException("No river id given."); + } } } + + + @Override + protected void initialize( + Artifact artifact, + Object context, + CallMeta callMeta) + { + // do nothing + } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : diff -r 21b9eb24bd47 -r feaf778d510e flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/RiverFactory.java --- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/RiverFactory.java Wed Aug 24 14:30:27 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/RiverFactory.java Wed Aug 24 14:35:38 2011 +0000 @@ -31,6 +31,25 @@ /** + * Returns a River object fetched from database based on its id. + * + * @param river_id The id of the desired river. + * + * @return the river. + */ + public static River getRiver(int river_id) { + Session session = SessionHolder.HOLDER.get(); + + Query query = session.createQuery("from River where id=:river_id"); + query.setParameter("river_id", river_id); + + List rivers = query.list(); + + return (rivers != null && rivers.size() > 0) ? rivers.get(0) : null; + } + + + /** * Returns a River object fetched from database based on its name. * * @param river The name of a river.