tim@71: /** tim@71: * tim@71: */ tim@71: package de.intevation.gnv.artifacts.fis; tim@71: tim@73: import java.io.IOException; tim@73: import java.io.OutputStream; tim@71: import java.util.ArrayList; tim@71: import java.util.Collection; tim@71: import java.util.HashMap; tim@71: import java.util.Iterator; tim@71: import java.util.Map; tim@71: tim@71: import org.apache.log4j.Logger; tim@71: import org.w3c.dom.Document; tim@71: import org.w3c.dom.Element; tim@71: import org.w3c.dom.Node; tim@71: import org.w3c.dom.NodeList; tim@71: tim@71: import de.intevation.artifactdatabase.Config; tim@71: import de.intevation.artifactdatabase.DefaultArtifact; tim@71: import de.intevation.artifactdatabase.XMLUtils; tim@71: import de.intevation.artifacts.Artifact; tim@71: import de.intevation.artifacts.ArtifactFactory; tim@73: import de.intevation.artifacts.CallContext; tim@117: import de.intevation.artifacts.CallMeta; tim@71: import de.intevation.gnv.artifacts.GNVArtifactBase; tim@71: import de.intevation.gnv.artifacts.context.GNVArtifactContext; tim@71: import de.intevation.gnv.artifacts.fis.product.DefaultProduct; tim@71: import de.intevation.gnv.artifacts.fis.product.Product; tim@117: import de.intevation.gnv.artifacts.ressource.RessourceFactory; tim@335: import de.intevation.gnv.state.DefaultInputData; tim@335: import de.intevation.gnv.state.InputData; tim@71: import de.intevation.gnv.utils.ArtifactFactoryUtilities; tim@71: import de.intevation.gnv.utils.ArtifactXMLUtilities; tim@71: tim@71: /** tim@71: * @author Tim Englich tim@171: * tim@71: */ tim@71: public class FISArtifact extends DefaultArtifact { tim@171: tim@71: /** tim@71: * the logger, used to log exceptions and additonaly information tim@71: */ tim@71: private static Logger log = Logger.getLogger(GNVArtifactBase.class); tim@71: /** tim@71: * The UID of this Class tim@71: */ tim@71: private static final long serialVersionUID = 2874044542701727083L; tim@171: tim@71: /** tim@71: * The Identifier for the Replacement of the Artifactname tim@71: */ tim@71: public static final String XPATH_IDENTIFIER_REPLACE = "IDENTIFIER"; tim@171: tim@71: /** tim@71: * The XPATH to the XML-Fragment that should be used for the Configuration tim@71: */ tim@171: public static final String XPATH_ARTIFACT_CONFIGURATION = "/artifact-database/artifacts/artifact[@name='" tim@171: + XPATH_IDENTIFIER_REPLACE tim@171: + "']"; tim@171: tim@71: /** tim@71: * The Name of the Artifact tim@71: */ tim@71: private String name = null; tim@171: tim@71: private Map products = null; tim@171: tim@71: private Artifact productArtifact = null; tim@171: tim@71: private Product current = null; tim@171: tim@71: private ArtifactXMLUtilities xmlUtilities = new ArtifactXMLUtilities(); tim@171: tim@71: /** tim@171: * @see de.intevation.artifactdatabase.DefaultArtifact#advance(org.w3c.dom.Document, tim@171: * de.intevation.artifacts.CallContext) tim@71: */ tim@71: @Override tim@73: public Document advance(Document target, CallContext context) { tim@171: tim@88: Document result = null; tim@171: if (this.productArtifact == null) { tim@171: if (this.current != null) { tim@171: tim@171: String uuid = Config.getStringXPath(target, tim@171: "action/uuid/@value"); tim@171: String hash = Config.getStringXPath(target, tim@171: "action/hash/@value"); tim@171: this.productArtifact = this.current.getArtifactFactory() tim@171: .createArtifact(uuid, context); tim@325: tim@171: Document feedDocument = xmlUtilities.reInitDocument(this tim@171: .createFeedProductArtifactDocument(uuid, hash)); tim@171: log.debug("Feed ==> " tim@171: + this.xmlUtilities tim@171: .writeDocument2String(feedDocument)); tim@171: this.productArtifact.feed(feedDocument, context); tim@325: result = ((GNVArtifactBase)this.productArtifact).initialize(context); tim@171: } else { tim@88: String msg = "Artifact is not configured properly. Call feed first."; tim@88: log.error(msg); tim@171: result = new ArtifactXMLUtilities().createExceptionReport(msg, tim@171: XMLUtils.newDocument()); tim@71: } tim@171: } else { tim@88: result = this.productArtifact.advance(target, context); tim@71: } tim@88: return result; tim@71: } tim@222: tim@222: private Document createDescribeRequestBody(String uuid, tim@222: String hash, tim@222: boolean includeUI){ tim@222: Document document = XMLUtils.newDocument(); tim@222: tim@222: Element rootNode = xmlUtilities.createArtifactElement(document,"action"); tim@222: document.appendChild(rootNode); tim@222: tim@222: Element typeNode = this.xmlUtilities.createArtifactElement(document, "type"); tim@222: typeNode.setAttribute("name", "describe"); tim@222: rootNode.appendChild(typeNode); tim@222: tim@222: Element uuidNode = this.xmlUtilities.createArtifactElement(document, "uuid"); tim@222: uuidNode.setAttribute("value",uuid); tim@222: rootNode.appendChild(uuidNode); tim@222: tim@222: Element hashNode = this.xmlUtilities.createArtifactElement(document, "hash"); tim@222: hashNode.setAttribute("value", hash); tim@222: rootNode.appendChild(hashNode); tim@222: tim@222: Element includeUINode = this.xmlUtilities.createArtifactElement(document, "include-ui"); tim@222: includeUINode.setTextContent(""+includeUI); tim@222: rootNode.appendChild(includeUINode); tim@222: tim@222: return document; tim@222: } tim@171: tim@171: private Document createAdvanceProductArtifactDocument(String uuid, tim@171: String hash, tim@171: String targetName) { tim@71: Document document = XMLUtils.newDocument(); tim@171: Element rootNode = xmlUtilities.createArtifactElement(document, tim@171: "action"); tim@171: tim@71: Element typeNode = xmlUtilities.createArtifactElement(document, "type"); tim@71: typeNode.setAttribute("name", "advanve"); tim@71: rootNode.appendChild(typeNode); tim@171: tim@71: Element uuidNode = xmlUtilities.createArtifactElement(document, "uuid"); tim@71: uuidNode.setAttribute("value", uuid); tim@71: rootNode.appendChild(uuidNode); tim@171: tim@71: Element hashNode = xmlUtilities.createArtifactElement(document, "hash"); tim@71: hashNode.setAttribute("value", hash); tim@71: rootNode.appendChild(hashNode); tim@171: Element targetNode = xmlUtilities.createArtifactElement(document, tim@171: "target"); tim@71: targetNode.setAttribute("name", targetName); tim@71: rootNode.appendChild(targetNode); tim@171: tim@71: document.appendChild(rootNode); tim@71: return document; tim@71: } tim@171: tim@171: private Document createFeedProductArtifactDocument(String uuid, String hash) { tim@71: Document document = XMLUtils.newDocument(); tim@171: Element rootNode = xmlUtilities.createArtifactElement(document, tim@171: "action"); tim@171: tim@71: Element typeNode = xmlUtilities.createArtifactElement(document, "type"); tim@71: typeNode.setAttribute("name", "feed"); tim@71: rootNode.appendChild(typeNode); tim@171: tim@71: Element uuidNode = xmlUtilities.createArtifactElement(document, "uuid"); tim@71: uuidNode.setAttribute("value", uuid); tim@71: rootNode.appendChild(uuidNode); tim@171: tim@71: Element hashNode = xmlUtilities.createArtifactElement(document, "hash"); tim@71: hashNode.setAttribute("value", hash); tim@71: rootNode.appendChild(hashNode); tim@171: tim@71: Element dataNode = xmlUtilities.createArtifactElement(document, "data"); tim@71: rootNode.appendChild(dataNode); tim@171: tim@71: Collection parameter = this.current.getParameter(); tim@171: if (parameter != null) { tim@71: Iterator parameterIt = parameter.iterator(); tim@171: while (parameterIt.hasNext()) { tim@71: InputData inputData = parameterIt.next(); tim@171: Element inputNode = xmlUtilities.createArtifactElement( tim@171: document, "input"); tim@71: inputNode.setAttribute("name", inputData.getName()); tim@71: inputNode.setAttribute("value", inputData.getValue()); tim@71: dataNode.appendChild(inputNode); tim@71: } tim@71: } tim@71: document.appendChild(rootNode); tim@71: return document; tim@171: tim@71: } tim@71: tim@71: /** tim@222: * @see de.intevation.artifactdatabase.DefaultArtifact#describe(org.w3c.dom.Document, de.intevation.artifacts.CallContext) tim@71: */ tim@71: @Override tim@222: public Document describe(Document data, CallContext context) { tim@171: if (this.productArtifact == null) { tim@117: return this.createDescibeOutput(context.getMeta()); tim@171: } else { tim@222: Document document = this.productArtifact.describe(data,context); tim@79: document = new ArtifactXMLUtilities().reInitDocument(document); tim@171: Node staticNode = Config tim@171: .getNodeXPath(document, "/result/ui/static"); tim@171: if (staticNode != null) { tim@171: Node staticUI = this.createSelectBox(document, context tim@171: .getMeta()); tim@79: staticNode.insertBefore(staticUI, staticNode.getFirstChild()); tim@79: } tim@79: return document; tim@71: } tim@71: } tim@71: tim@222: tim@71: /** tim@171: * @see de.intevation.artifactdatabase.DefaultArtifact#feed(org.w3c.dom.Document, tim@171: * java.lang.Object) tim@71: */ tim@71: @Override tim@73: public Document feed(Document target, CallContext context) { tim@89: log.debug("FISArtifact.feed"); tim@89: Document result = null; tim@171: if (this.productArtifact == null) { tim@171: String productName = Config.getStringXPath(target, tim@171: "action/data/input[@name='product']/@value"); tim@171: log.debug("Looking for ProductArtifact " + productName); tim@71: if (this.products.containsKey(productName)) { tim@71: this.current = this.products.get(productName); tim@171: result = new ArtifactXMLUtilities().createSuccessReport( tim@171: "Feed success New ProductArtifact created", XMLUtils tim@171: .newDocument()); tim@171: } else { tim@171: String msg = "Product does not exists for " + productName; tim@88: log.error(msg); tim@171: result = new ArtifactXMLUtilities().createExceptionReport(msg, tim@171: XMLUtils.newDocument()); tim@71: } tim@171: } else { tim@89: log.debug("Feed a Productartifact"); tim@89: result = this.productArtifact.feed(target, context); tim@71: } tim@89: return result; tim@71: } tim@71: tim@71: /** tim@171: * @see de.intevation.artifactdatabase.DefaultArtifact#out(org.w3c.dom.Document, tim@171: * java.lang.Object) tim@71: */ tim@71: @Override tim@171: public void out(Document format, OutputStream outputStream, tim@171: CallContext context) throws IOException { tim@171: if (this.productArtifact != null) { tim@171: this.productArtifact.out(format, outputStream, context); tim@71: } tim@71: } tim@71: tim@71: /** tim@71: * Constructor tim@71: */ tim@71: public FISArtifact() { tim@71: super(); tim@71: } tim@71: tim@171: /** tim@171: * @see de.intevation.artifactdatabase.DefaultArtifact#setup(java.lang.String, tim@171: * de.intevation.artifacts.ArtifactFactory, java.lang.Object) tim@171: */ tim@71: @Override tim@71: public void setup(String identifier, ArtifactFactory factory, Object context) { tim@71: log.debug("FISArtifact.setup"); tim@71: this.name = factory.getName(); tim@171: super.setup(identifier, factory, context); tim@171: if (context instanceof GNVArtifactContext) { tim@171: GNVArtifactContext gnvContext = (GNVArtifactContext) context; tim@71: Document doc = gnvContext.getConfig(); tim@71: Node artifactNode = this.getConfigurationFragment(doc); tim@171: tim@171: NodeList products = Config.getNodeSetXPath(artifactNode, tim@171: "products/product"); tim@171: if (products != null) { tim@171: this.products = new HashMap(products tim@171: .getLength()); tim@204: tim@171: for (int i = 0; i < products.getLength(); i++) { tim@204: Element productNode = (Element)products.item(i); tim@204: String productName = productNode.getAttribute("name"); tim@171: NodeList parameterNodes = Config.getNodeSetXPath( tim@171: productNode, "parameters/parameter"); tim@171: Collection parameter = null; tim@171: if (parameterNodes != null) { tim@171: parameter = new ArrayList(parameterNodes tim@171: .getLength()); tim@171: for (int j = 0; j < parameterNodes.getLength(); j++) { tim@204: Element parameterNode = (Element)parameterNodes.item(j); tim@204: String name = parameterNode.getAttribute("name"); tim@204: String value = parameterNode.getAttribute("value"); tim@171: parameter.add(new DefaultInputData(name, value)); tim@171: } tim@171: } tim@171: Node artifactFactoryNode = Config.getNodeXPath(productNode, tim@171: "artifact-factory"); tim@171: ArtifactFactory artifactFactory = new ArtifactFactoryUtilities() tim@171: .createArtitfactFactor(doc, artifactFactoryNode); tim@171: this.products.put(productName, new DefaultProduct( tim@171: productName, parameter, artifactFactory)); tim@171: } tim@171: } tim@71: } tim@71: } tim@171: tim@171: protected Node getConfigurationFragment(Document document) { tim@71: log.debug("GNVArtifactBase.getConfigurationFragment"); tim@171: String xpathQuery = XPATH_ARTIFACT_CONFIGURATION.replaceAll( tim@171: XPATH_IDENTIFIER_REPLACE, this.name); tim@71: log.debug(xpathQuery); tim@171: return Config.getNodeXPath(document, xpathQuery); tim@71: } tim@171: tim@171: protected Document createDescibeOutput(CallMeta callMeta) { tim@71: log.debug("GNVArtifactBase.createDescibeOutput"); tim@71: Document document = XMLUtils.newDocument(); tim@71: Element rootNode = this.createRootNode(document); tim@71: this.createHeader(rootNode, document, "describe"); tim@71: this.createOutputs(rootNode, document); tim@71: this.createCurrentState(rootNode, document); tim@71: this.createReachableStates(rootNode, document); tim@71: this.createModel(rootNode, document); tim@117: this.createUserInterface(rootNode, document, callMeta); tim@171: tim@71: return document; tim@71: } tim@171: tim@171: protected Element createRootNode(Document document) { tim@171: Element rootNode = xmlUtilities.createArtifactElement(document, tim@171: "result"); tim@71: document.appendChild(rootNode); tim@71: return rootNode; tim@71: } tim@171: tim@171: protected void createHeader(Element parent, Document document, tim@171: String documentType) { tim@171: Element typeNode = xmlUtilities.createArtifactElement(document, "type"); tim@71: typeNode.setAttribute("name", documentType); tim@71: parent.appendChild(typeNode); tim@171: tim@171: Element uuidNode = xmlUtilities.createArtifactElement(document, "uuid"); tim@71: uuidNode.setAttribute("value", super.identifier); tim@71: parent.appendChild(uuidNode); tim@171: tim@171: Element hashNode = xmlUtilities.createArtifactElement(document, "hash"); tim@71: hashNode.setAttribute("value", this.hash()); tim@71: parent.appendChild(hashNode); tim@71: tim@71: } tim@171: tim@171: protected void createReachableStates(Element parent, Document document) { tim@171: Element stateNode = xmlUtilities.createArtifactElement(document, tim@171: "reachable-states"); tim@171: if (this.products != null) { tim@71: Iterator products = this.products.values().iterator(); tim@171: while (products.hasNext()) { tim@71: Product product = products.next(); tim@171: Element currentNode = xmlUtilities.createArtifactElement( tim@171: document, "state"); tim@71: currentNode.setAttribute("name", product.getName()); tim@71: currentNode.setAttribute("description", product.getName()); tim@71: stateNode.appendChild(currentNode); tim@71: } tim@71: } tim@71: parent.appendChild(stateNode); tim@71: } tim@171: tim@171: protected void createCurrentState(Element parent, Document document) { tim@171: Element stateNode = xmlUtilities.createArtifactElement(document, tim@171: "state"); tim@71: stateNode.setAttribute("name", "choose-product"); tim@171: stateNode.setAttribute("description", tim@171: "Initialer Stand Auswahl des products"); tim@71: parent.appendChild(stateNode); tim@71: } tim@171: tim@171: protected void createModel(Element parent, Document document) { tim@171: Element modelNode = xmlUtilities.createArtifactElement(document, tim@171: "model"); tim@171: tim@171: Element inputNode = xmlUtilities.createArtifactElement(document, tim@171: "input"); tim@71: inputNode.setAttribute("name", "product"); tim@71: inputNode.setAttribute("type", "String"); tim@71: modelNode.appendChild(inputNode); tim@171: tim@71: parent.appendChild(modelNode); tim@71: } tim@171: tim@171: protected void createUserInterface(Element parent, Document document, tim@171: CallMeta callMeta) { tim@171: Element uiNode = xmlUtilities.createArtifactElement(document, "ui"); tim@76: ArtifactXMLUtilities xmlUtilities = new ArtifactXMLUtilities(); tim@171: Node dynamic = xmlUtilities.createArtifactElement(document, "dynamic"); tim@79: uiNode.appendChild(dynamic); tim@171: if (this.products != null && !this.products.isEmpty()) { tim@171: Element selectNode = createSelectBox(document, callMeta); tim@171: tim@79: dynamic.appendChild(selectNode); tim@76: } tim@171: tim@71: parent.appendChild(uiNode); tim@71: } tim@171: tim@79: /** tim@79: * @param document tim@79: * @param xmlUtilities tim@79: * @return tim@79: */ tim@117: private Element createSelectBox(Document document, CallMeta callMeta) { tim@171: tim@79: ArtifactXMLUtilities xmlUtilities = new ArtifactXMLUtilities(); tim@79: String selectboxName = "product"; tim@79: Iterator it = this.products.values().iterator(); tim@171: Element selectNode = xmlUtilities.createXFormElement(document, tim@171: "select1"); tim@79: selectNode.setAttribute("ref", selectboxName); tim@171: tim@79: Element lableNode = xmlUtilities.createXFormElement(document, "label"); tim@171: lableNode.setTextContent(RessourceFactory.getInstance().getRessource( tim@171: callMeta.getLanguages(), selectboxName, selectboxName)); tim@79: selectNode.appendChild(lableNode); tim@171: Element choiceNode = xmlUtilities.createXFormElement(document, tim@171: "choices"); tim@79: selectNode.appendChild(choiceNode); tim@171: while (it.hasNext()) { tim@79: Product p = it.next(); tim@171: Element itemNode = xmlUtilities tim@171: .createXFormElement(document, "item"); tim@171: tim@171: if (this.current != null tim@171: && this.current.getName().equals(p.getName())) { tim@82: itemNode.setAttribute("selected", "true"); tim@82: } tim@171: tim@171: Element choiceLableNode = xmlUtilities.createXFormElement(document, tim@171: "label"); tim@171: choiceLableNode.setTextContent(RessourceFactory.getInstance() tim@171: .getRessource(callMeta.getLanguages(), p.getName(), tim@171: p.getName())); tim@79: itemNode.appendChild(choiceLableNode); tim@171: tim@171: Element choicValueNode = xmlUtilities.createXFormElement(document, tim@171: "value"); tim@79: choicValueNode.setTextContent(p.getName()); tim@79: itemNode.appendChild(choicValueNode); tim@171: tim@79: choiceNode.appendChild(itemNode); tim@171: tim@79: } tim@79: return selectNode; tim@79: } tim@171: tim@171: protected void createOutputs(Element parent, Document document) { tim@71: log.debug("GNVArtifactBase.createOutputs"); tim@171: Element outputsNode = xmlUtilities.createArtifactElement(document, tim@171: "outputs"); tim@71: parent.appendChild(outputsNode); tim@71: } tim@71: tim@71: }