ingo@14: package de.intevation.flys.client.server;
ingo@14:
ingo@870: import java.util.ArrayList;
ingo@870: import java.util.List;
ingo@870:
ingo@870: import javax.xml.xpath.XPathConstants;
ingo@870:
ingo@14: import org.w3c.dom.Document;
ingo@870: import org.w3c.dom.Element;
ingo@870: import org.w3c.dom.NodeList;
ingo@14:
ingo@1367: import org.apache.log4j.Logger;
ingo@1367:
ingo@16: import de.intevation.artifacts.common.utils.XMLUtils;
ingo@16: import de.intevation.artifacts.common.ArtifactNamespaceContext;
ingo@16:
ingo@14: import de.intevation.artifacts.httpclient.utils.ArtifactCreator;
ingo@14:
ingo@14: import de.intevation.flys.client.shared.model.Artifact;
ingo@870: import de.intevation.flys.client.shared.model.CalculationMessage;
bjoern@3865: import de.intevation.flys.client.shared.model.ChartArtifact;
ingo@14: import de.intevation.flys.client.shared.model.DefaultArtifact;
bjoern@3865: import de.intevation.flys.client.shared.model.FixAnalysisArtifact;
bjoern@3865: import de.intevation.flys.client.shared.model.GaugeDischargeCurveArtifact;
bjoern@3865: import de.intevation.flys.client.shared.model.MapArtifact;
ingo@2520: import de.intevation.flys.client.shared.model.MINFOArtifact;
rrenkert@5503: import de.intevation.flys.client.shared.model.StaticSQRelationArtifact;
ingo@227: import de.intevation.flys.client.shared.model.WINFOArtifact;
ingo@14:
ingo@14:
ingo@14: /**
ingo@14: * An implementation of an {@link ArtifactCreator}. This class uses the document
ingo@14: * that is returned by the artifact server to parse important information (like
ingo@14: * uuid, hash) and returns a new {@link Artifact} instance.
ingo@14: *
ingo@14: * @author Ingo Weinzierl
ingo@14: */
ingo@14: public class FLYSArtifactCreator implements ArtifactCreator {
ingo@14:
ingo@1367: private static final Logger logger =
ingo@1367: Logger.getLogger(FLYSArtifactCreator.class);
ingo@1367:
ingo@1367:
ingo@16: /** The XPath to the artifact's uuid.*/
ingo@16: public static final String XPATH_UUID = "/art:result/art:uuid/@art:value";
ingo@16:
ingo@16: /** The XPath to the artifact's hash value.*/
ingo@16: public static final String XPATH_HASH = "/art:result/art:hash/@art:value";
ingo@16:
ingo@227: /** The XPath to the artifact's name value.*/
ingo@227: public static final String XPATH_NAME = "/art:result/art:name/@art:value";
ingo@227:
ingo@862: /** The XPath to the value that determines if the artifact is processing in
ingo@862: * background.*/
ingo@870: public static final String XPATH_BACKGROUND_VALUE =
ingo@870: "/art:result/art:background-processing/@art:value";
ingo@870:
ingo@870: /** The XPath that points to the (if existing) background messages.*/
ingo@862: public static final String XPATH_BACKGROUND =
ingo@870: "/art:result/art:background-processing";
ingo@862:
ingo@16:
ingo@14: /**
ingo@14: * Creates a new instance of an {@link ArtifactCreator}.
ingo@14: */
ingo@14: public FLYSArtifactCreator() {
ingo@14: }
ingo@14:
ingo@14:
ingo@14: /**
ingo@14: * This concreate implementation returns an instance of {@link Artifact}
ingo@14: * that is used in the FLYS GWT Client code.
ingo@14: *
ingo@14: * @param doc A document that describes the artifact that has been created
ingo@14: * in the artifact server.
ingo@14: *
ingo@14: * @return an instance if {@link Artifact}.
ingo@14: */
ingo@14: public Object create(Document doc) {
ingo@16: Artifact artifact = extractArtifact(doc);
ingo@16: artifact.setArtifactDescription(
ingo@16: ArtifactDescriptionFactory.createArtifactDescription(doc));
ingo@16:
ingo@16: return artifact;
ingo@16: }
ingo@16:
ingo@16:
ingo@16: /**
ingo@16: * This method extracts the UUID und HASH information of the returned
ingo@16: * artifact document.
ingo@16: *
ingo@16: * @param doc The result of the CREATE operation.
ingo@16: *
ingo@16: * @return an instance of an {@link Artifact}.
ingo@16: */
ingo@16: protected Artifact extractArtifact(Document doc) {
ingo@1367: logger.debug("FLYSArtifactCreator - extractArtifact()");
ingo@16:
ingo@16: String uuid = XMLUtils.xpathString(
ingo@16: doc, XPATH_UUID, ArtifactNamespaceContext.INSTANCE);
ingo@16:
ingo@16: String hash = XMLUtils.xpathString(
ingo@16: doc, XPATH_HASH, ArtifactNamespaceContext.INSTANCE);
ingo@16:
ingo@227: String name = XMLUtils.xpathString(
ingo@227: doc, XPATH_NAME, ArtifactNamespaceContext.INSTANCE);
ingo@227:
ingo@862: String backgroundStr = XMLUtils.xpathString(
ingo@870: doc, XPATH_BACKGROUND_VALUE, ArtifactNamespaceContext.INSTANCE);
ingo@862:
ingo@862: boolean background = false;
ingo@862: if (backgroundStr != null && backgroundStr.length() > 0) {
ingo@862: background = Boolean.valueOf(backgroundStr);
ingo@862: }
ingo@862:
ingo@870: List msg = parseBackgroundMessages(doc);
ingo@870:
ingo@1367: logger.debug("NEW Artifact UUID: " + uuid);
ingo@1367: logger.debug("NEW Artifact HASH: " + hash);
ingo@1367: logger.debug("NEW Artifact NAME: " + name);
ingo@1367: logger.debug("NEW Artifact IN BACKGROUND: " + background);
ingo@227:
ingo@227: if (name == null) {
ingo@870: return new DefaultArtifact(uuid, hash, background, msg);
ingo@227: }
ingo@227:
ingo@227: name = name.trim();
ingo@227:
ingo@227: if (name.length() > 0 && name.equals("winfo")) {
ingo@1367: logger.debug("+++++ NEW WINFO ARTIFACT.");
ingo@870: return new WINFOArtifact(uuid, hash, background, msg);
ingo@227: }
ingo@1521: else if (name.length() > 0 && name.equals("new_map")) {
raimund@1513: logger.debug("+++++ NEW MAP ARTIFACT.");
raimund@1513: return new MapArtifact(uuid, hash, background, msg);
raimund@1513: }
raimund@1530: else if (name.length() > 0 && name.equals("new_chart")) {
raimund@1530: logger.debug("+++++ NEW CHART ARTIFACT.");
raimund@1530: return new ChartArtifact(uuid, hash, background, msg);
raimund@1530: }
ingo@2520: else if (name.length() > 0 && name.equals("minfo")) {
ingo@2520: logger.debug("+++++ NEW MINFO ARTIFACT.");
ingo@2520: return new MINFOArtifact(uuid, hash, background, msg);
ingo@2520: }
raimund@2541: else if (name.length() > 0 && name.equals("fixanalysis")) {
raimund@2541: logger.debug("+++++ NEW FIXANALYSIS ARTIFACT.");
raimund@2541: return new FixAnalysisArtifact(uuid, hash, background, msg);
raimund@2541: }
bjoern@3865: else if (name.length() > 0 && name.equals("gaugedischargecurve")) {
bjoern@3998: logger.debug("+++++ NEW GAUGEDISCHARGECURVE ARTIFACT.");
bjoern@3865: return new GaugeDischargeCurveArtifact(uuid, hash, background, msg);
bjoern@3865: }
rrenkert@5503: else if (name.length() > 0 && name.equals("staticsqrelation")) {
rrenkert@5503: logger.debug("+++++ STATICSQRELATION ARTIFACT.");
rrenkert@5503: return new StaticSQRelationArtifact(uuid, hash, background, msg);
rrenkert@5503: }
ingo@16:
ingo@870: return new DefaultArtifact(uuid, hash, background, msg);
ingo@870: }
ingo@870:
ingo@870:
ingo@870: public static List parseBackgroundMessages(Document d) {
ingo@870: NodeList list = (NodeList) XMLUtils.xpath(
ingo@870: d, XPATH_BACKGROUND, XPathConstants.NODESET,
ingo@870: ArtifactNamespaceContext.INSTANCE);
ingo@870:
ingo@870: int len = list != null ? list.getLength() : 0;
ingo@870:
ingo@1367: logger.debug("Found " + len + " background messages.");
ingo@870:
ingo@870: List res = new ArrayList(len);
ingo@870:
ingo@870: for (int i = 0; i < len; i++) {
ingo@870: CalculationMessage msg = parseBackgroundMessage(
ingo@870: (Element) list.item(i));
ingo@870:
ingo@870: if (msg != null) {
ingo@870: res.add(msg);
ingo@870: }
ingo@870: }
ingo@870:
ingo@870: return res;
ingo@870: }
ingo@870:
ingo@870:
ingo@870: public static CalculationMessage parseBackgroundMessage(Element e) {
ingo@870: String steps = e.getAttribute("art:steps");
ingo@870: String currentStep = e.getAttribute("art:currentStep");
ingo@870: String message = e.getTextContent();
ingo@870:
ingo@870: int lenCurStep = currentStep != null ? currentStep.length() : 0;
ingo@870: int lenSteps = steps != null ? steps.length() : 0;
ingo@870: int lenMessage = message != null ? message.length() : 0;
ingo@870:
ingo@870: if (lenSteps > 0 && lenMessage > 0 && lenCurStep > 0) {
ingo@870: try {
ingo@870: return new CalculationMessage(
ingo@870: Integer.parseInt(steps),
ingo@870: Integer.parseInt(currentStep),
ingo@870: message);
ingo@870:
ingo@870: }
ingo@870: catch (NumberFormatException nfe) {
ingo@870: nfe.printStackTrace();
ingo@870: }
ingo@870: }
ingo@870:
ingo@870: return null;
ingo@14: }
ingo@14: }
ingo@14: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :