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@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@71: import de.intevation.gnv.transition.DefaultInputData; tim@71: import de.intevation.gnv.transition.InputData; tim@71: import de.intevation.gnv.transition.InputValue; tim@71: import de.intevation.gnv.transition.OutputMode; tim@71: import de.intevation.gnv.transition.OutputTransition; 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@71: * tim@71: */ tim@71: public class FISArtifact extends DefaultArtifact { tim@71: 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@71: 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@71: tim@71: /** tim@71: * The XPATH to the XML-Fragment that should be used for the Configuration tim@71: */ tim@71: public static final String XPATH_ARTIFACT_CONFIGURATION= "/artifact-database/artifacts/artifact[@name='"+XPATH_IDENTIFIER_REPLACE+"']"; tim@71: tim@71: /** tim@71: * The Name of the Artifact tim@71: */ tim@71: private String name = null; tim@71: tim@71: private Map products = null; tim@71: tim@71: private Artifact productArtifact = null; tim@71: tim@71: private Product current = null; tim@71: tim@71: private ArtifactXMLUtilities xmlUtilities = new ArtifactXMLUtilities(); tim@71: tim@71: tim@71: /** tim@73: * @see de.intevation.artifactdatabase.DefaultArtifact#advance(org.w3c.dom.Document, de.intevation.artifacts.CallContext) tim@71: */ tim@71: @Override tim@73: public Document advance(Document target, CallContext context) { tim@71: if (this.productArtifact == null){ tim@71: if (this.current != null){ tim@79: try { tim@79: String uuid = Config.getStringXPath(target, "action/uuid/@value"); // TODO: müssen wir für das subartifact eine veränderte uuid führen? tim@79: String hash = Config.getStringXPath(target, "action/hash/@value"); tim@79: this.productArtifact = this.current.getArtifactFactory().createArtifact(uuid, context); tim@79: Document feedDocument = xmlUtilities.reInitDocument(this.createFeedProductArtifactDocument(uuid, hash)); tim@79: log.debug("Feed ==> "+this.xmlUtilities.writeDocument2String(feedDocument)); tim@79: Document descibeDocument = xmlUtilities.reInitDocument(this.productArtifact.describe(context)); tim@79: log.debug("Descibe ==> "+this.xmlUtilities.writeDocument2String(descibeDocument)); tim@79: this.productArtifact.feed(feedDocument, context); tim@79: String targetName = Config.getStringXPath(descibeDocument, "result/reachable-states/state/@name"); tim@79: Document advanceDocument = xmlUtilities.reInitDocument(this.createAdvanceProductArtifactDocument(uuid, hash, targetName)); tim@79: log.debug("Advance ==> "+this.xmlUtilities.writeDocument2String(advanceDocument)); tim@79: return this.productArtifact.advance(advanceDocument, context); tim@79: } catch (RuntimeException e) { tim@79: log.error(e,e); tim@79: } tim@71: }else{ tim@71: log.warn("Artifact is not configured properly. Call feed first."); tim@71: // TODO Fehlerdokument erzeugen. tim@71: } tim@71: return null; tim@71: }else{ tim@71: return this.productArtifact.advance(target, context); tim@71: } tim@71: } tim@71: private Document createAdvanceProductArtifactDocument(String uuid, String hash, String targetName){ tim@71: Document document = XMLUtils.newDocument(); tim@71: Element rootNode = xmlUtilities.createArtifactElement(document, "action"); tim@71: tim@71: Element typeNode = xmlUtilities.createArtifactElement(document, "type"); tim@71: typeNode.setAttribute("name", "advanve"); tim@71: rootNode.appendChild(typeNode); tim@71: tim@71: Element uuidNode = xmlUtilities.createArtifactElement(document, "uuid"); tim@71: uuidNode.setAttribute("value", uuid); tim@71: rootNode.appendChild(uuidNode); tim@71: tim@71: Element hashNode = xmlUtilities.createArtifactElement(document, "hash"); tim@71: hashNode.setAttribute("value", hash); tim@71: rootNode.appendChild(hashNode); tim@71: Element targetNode = xmlUtilities.createArtifactElement(document, "target"); tim@71: targetNode.setAttribute("name", targetName); tim@71: rootNode.appendChild(targetNode); tim@71: tim@71: document.appendChild(rootNode); tim@71: return document; tim@71: } tim@71: tim@71: private Document createFeedProductArtifactDocument(String uuid, String hash){ tim@71: Document document = XMLUtils.newDocument(); tim@71: Element rootNode = xmlUtilities.createArtifactElement(document, "action"); tim@71: tim@71: Element typeNode = xmlUtilities.createArtifactElement(document, "type"); tim@71: typeNode.setAttribute("name", "feed"); tim@71: rootNode.appendChild(typeNode); tim@71: tim@71: Element uuidNode = xmlUtilities.createArtifactElement(document, "uuid"); tim@71: uuidNode.setAttribute("value", uuid); tim@71: rootNode.appendChild(uuidNode); tim@71: tim@71: Element hashNode = xmlUtilities.createArtifactElement(document, "hash"); tim@71: hashNode.setAttribute("value", hash); tim@71: rootNode.appendChild(hashNode); tim@71: tim@71: Element dataNode = xmlUtilities.createArtifactElement(document, "data"); tim@71: rootNode.appendChild(dataNode); tim@71: tim@71: tim@71: Collection parameter = this.current.getParameter(); tim@71: if (parameter != null){ tim@71: Iterator parameterIt = parameter.iterator(); tim@71: while(parameterIt.hasNext()){ tim@71: InputData inputData = parameterIt.next(); tim@71: Element inputNode = xmlUtilities.createArtifactElement(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@71: tim@71: } tim@71: tim@71: /** tim@71: * @see de.intevation.artifactdatabase.DefaultArtifact#describe(java.lang.Object) tim@71: */ tim@71: @Override tim@73: public Document describe(CallContext context) { tim@71: if (this.productArtifact == null){ tim@71: return this.createDescibeOutput(); tim@71: }else{ tim@79: Document document = this.productArtifact.describe(context); tim@79: document = new ArtifactXMLUtilities().reInitDocument(document); tim@79: Node staticNode = Config.getNodeXPath(document, "/result/ui/static"); tim@79: if (staticNode != null){ tim@79: Node staticUI = this.createSelectBox(document); tim@79: staticNode.insertBefore(staticUI, staticNode.getFirstChild()); tim@79: } tim@79: return document; tim@71: } tim@71: } tim@71: tim@71: /** tim@71: * @see de.intevation.artifactdatabase.DefaultArtifact#feed(org.w3c.dom.Document, java.lang.Object) tim@71: */ tim@71: @Override tim@73: public Document feed(Document target, CallContext context) { tim@71: if (this.productArtifact == null){ tim@71: String productName = Config.getStringXPath(target, "action/data/input[@name='product']/@value"); tim@71: if (this.products.containsKey(productName)) { tim@71: this.current = this.products.get(productName); tim@71: }else{ tim@71: log.error("Product does not exists for "+productName); tim@71: // TODO: Fehlerdokument erzeugen. tim@71: } tim@71: return null; tim@71: }else{ tim@71: return this.productArtifact.feed(target, context); tim@71: } tim@71: } tim@71: tim@71: /** tim@71: * @see de.intevation.artifactdatabase.DefaultArtifact#out(org.w3c.dom.Document, java.lang.Object) tim@71: */ tim@71: @Override tim@73: public void out(Document format, OutputStream outputStream, CallContext context) throws IOException { tim@71: if (this.productArtifact != null){ tim@73: 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@71: /** tim@71: * @see de.intevation.artifactdatabase.DefaultArtifact#setup(java.lang.String, de.intevation.artifacts.ArtifactFactory, java.lang.Object) tim@71: */ 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@71: super.setup(identifier,factory, context); tim@71: if (context instanceof GNVArtifactContext){ tim@71: GNVArtifactContext gnvContext = (GNVArtifactContext)context; tim@71: Document doc = gnvContext.getConfig(); tim@71: Node artifactNode = this.getConfigurationFragment(doc); tim@71: tim@71: NodeList products = Config.getNodeSetXPath(artifactNode,"products/product"); tim@71: if (products != null){ tim@71: this.products = new HashMap(products.getLength()); tim@71: for (int i = 0; i < products.getLength(); i++){ tim@71: Node productNode = products.item(i); tim@71: String productName = Config.getStringXPath(productNode, "@name"); tim@71: NodeList parameterNodes = Config.getNodeSetXPath(productNode, "parameters/parameter"); tim@71: Collection parameter = null; tim@71: if (parameterNodes != null){ tim@71: parameter = new ArrayList(parameterNodes.getLength()); tim@71: for (int j = 0; j < parameterNodes.getLength(); j++){ tim@71: Node parameterNode = parameterNodes.item(j); tim@71: String name = Config.getStringXPath(parameterNode, "@name"); tim@71: String value = Config.getStringXPath(parameterNode, "@value"); tim@71: parameter.add(new DefaultInputData(name, value)); tim@71: } tim@71: } tim@71: Node artifactFactoryNode = Config.getNodeXPath(productNode, "artifact-factory"); tim@71: ArtifactFactory artifactFactory =new ArtifactFactoryUtilities().createArtitfactFactor(doc, artifactFactoryNode); tim@71: this.products.put(productName, new DefaultProduct(productName, parameter,artifactFactory)); tim@71: } tim@71: } tim@71: tim@71: tim@71: } tim@71: } tim@71: tim@71: protected Node getConfigurationFragment(Document document){ tim@71: log.debug("GNVArtifactBase.getConfigurationFragment"); tim@71: String xpathQuery = XPATH_ARTIFACT_CONFIGURATION.replaceAll(XPATH_IDENTIFIER_REPLACE, this.name); tim@71: log.debug(xpathQuery); tim@71: return Config.getNodeXPath(document,xpathQuery); tim@71: } tim@71: tim@71: protected Document createDescibeOutput(){ 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@71: this.createUserInterface(rootNode, document); tim@71: tim@71: return document; tim@71: } tim@71: tim@71: protected Element createRootNode(Document document){ tim@71: Element rootNode = xmlUtilities.createArtifactElement(document,"result"); tim@71: document.appendChild(rootNode); tim@71: return rootNode; tim@71: } tim@71: tim@71: protected void createHeader(Element parent, Document document, String documentType){ tim@71: Element typeNode = xmlUtilities.createArtifactElement(document,"type"); tim@71: typeNode.setAttribute("name", documentType); tim@71: parent.appendChild(typeNode); tim@71: tim@71: Element uuidNode = xmlUtilities.createArtifactElement(document,"uuid"); tim@71: uuidNode.setAttribute("value", super.identifier); tim@71: parent.appendChild(uuidNode); tim@71: tim@71: Element hashNode = xmlUtilities.createArtifactElement(document,"hash"); tim@71: hashNode.setAttribute("value", this.hash()); tim@71: parent.appendChild(hashNode); tim@71: tim@71: tim@71: } tim@71: protected void createReachableStates(Element parent,Document document){ tim@71: Element stateNode = xmlUtilities.createArtifactElement(document,"reachable-states"); tim@71: if (this.products != null){ tim@71: Iterator products = this.products.values().iterator(); tim@71: while(products.hasNext()){ tim@71: Product product = products.next(); tim@71: Element currentNode = xmlUtilities.createArtifactElement(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@71: tim@71: protected void createCurrentState(Element parent, Document document){ tim@71: Element stateNode = xmlUtilities.createArtifactElement(document,"state"); tim@71: stateNode.setAttribute("name", "choose-product"); tim@71: stateNode.setAttribute("description", "Initialer Stand Auswahl des products"); tim@71: parent.appendChild(stateNode); tim@71: } tim@71: tim@71: tim@71: protected void createModel(Element parent, Document document){ tim@71: Element modelNode = xmlUtilities.createArtifactElement(document,"model"); tim@71: tim@71: Element inputNode = xmlUtilities.createArtifactElement(document,"input"); tim@71: inputNode.setAttribute("name", "product"); tim@71: inputNode.setAttribute("type", "String"); tim@71: modelNode.appendChild(inputNode); tim@71: tim@71: parent.appendChild(modelNode); tim@71: } tim@71: tim@71: protected void createUserInterface(Element parent, Document document){ tim@71: Element uiNode = xmlUtilities.createArtifactElement(document,"ui"); tim@76: ArtifactXMLUtilities xmlUtilities = new ArtifactXMLUtilities(); tim@79: Node dynamic = xmlUtilities.createArtifactElement(document, "dynamic"); tim@79: uiNode.appendChild(dynamic); tim@76: if (this.products != null && !this.products.isEmpty()){ tim@79: Element selectNode = createSelectBox(document); tim@79: tim@79: dynamic.appendChild(selectNode); tim@76: } tim@71: tim@71: parent.appendChild(uiNode); tim@71: } tim@79: /** tim@79: * @param document tim@79: * @param xmlUtilities tim@79: * @return tim@79: */ tim@79: private Element createSelectBox(Document document) { tim@79: ArtifactXMLUtilities xmlUtilities = new ArtifactXMLUtilities(); tim@79: String selectboxName = "product"; tim@79: Iterator it = this.products.values().iterator(); tim@79: Element selectNode = xmlUtilities.createXFormElement(document,"select1"); tim@79: selectNode.setAttribute("ref", selectboxName); tim@79: tim@79: tim@79: Element lableNode = xmlUtilities.createXFormElement(document, "label"); tim@79: lableNode.setTextContent(selectboxName); tim@79: selectNode.appendChild(lableNode); tim@79: Element choiceNode = xmlUtilities.createXFormElement(document, "choices"); tim@79: selectNode.appendChild(choiceNode); tim@79: while (it.hasNext()){ tim@79: Product p = it.next(); tim@79: Element itemNode = xmlUtilities.createXFormElement(document, "item"); tim@79: tim@79: tim@79: Element choiceLableNode = xmlUtilities.createXFormElement(document, "label"); tim@79: choiceLableNode.setTextContent(p.getName()); tim@79: itemNode.appendChild(choiceLableNode); tim@79: tim@79: Element choicValueNode = xmlUtilities.createXFormElement(document, "value"); tim@79: choicValueNode.setTextContent(p.getName()); tim@79: itemNode.appendChild(choicValueNode); tim@79: tim@79: choiceNode.appendChild(itemNode); tim@79: tim@79: } tim@79: return selectNode; tim@79: } tim@71: tim@71: protected void createOutputs(Element parent, Document document){ tim@71: log.debug("GNVArtifactBase.createOutputs"); tim@71: Element outputsNode = xmlUtilities.createArtifactElement(document,"outputs"); tim@71: parent.appendChild(outputsNode); tim@71: } tim@71: tim@71: }