Mercurial > dive4elements > gnv-client
view gnv-artifacts/src/main/java/de/intevation/gnv/artifacts/fis/SelectProductArtifact.java @ 626:61f688a69a55
Split up export modes from output modes.
gnv-artifacts/trunk@700 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Tue, 23 Feb 2010 11:47:08 +0000 |
parents | 9681ac6b6527 |
children | 9a828e5a2390 |
line wrap: on
line source
package de.intevation.gnv.artifacts.fis; import java.io.IOException; import java.io.OutputStream; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import javax.xml.xpath.XPathConstants; import org.apache.log4j.Logger; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import de.intevation.artifactdatabase.ProxyArtifact; import de.intevation.artifactdatabase.XMLUtils; import de.intevation.artifacts.Artifact; import de.intevation.artifacts.ArtifactFactory; import de.intevation.artifacts.ArtifactNamespaceContext; import de.intevation.artifacts.CallContext; import de.intevation.artifacts.CallMeta; import de.intevation.gnv.artifacts.GNVArtifactBase; import de.intevation.gnv.artifacts.GNVDefaultArtifact; import de.intevation.gnv.artifacts.GNVProductArtifactFactory; import de.intevation.gnv.artifacts.PreSettingArtifact; import de.intevation.gnv.artifacts.fis.product.Product; import de.intevation.gnv.artifacts.ressource.RessourceFactory; import de.intevation.gnv.state.DefaultInputData; import de.intevation.gnv.state.InputData; /** * @author Ingo Weinzierl <ingo.weinzierl@intevation.de> */ public class SelectProductArtifact extends GNVDefaultArtifact { /** * THE UID of this Class. */ private static final long serialVersionUID = -7952357683127758677L; public static final String XPATH_UUID = "art:action/art:uuid/@value"; public static final String XPATH_HASH = "art:action/art:hash/@value"; public static final String XPATH_INPUT_DATA_VALUE = "art:action/art:data/art:input[@name='product']/@value"; public static final String XPATH_SETUP_PARAMETER_NODES = "/art:action/art:factory/art:parameter"; public static final String XFORM_URL = "http://www.w3.org/2002/xforms"; public static final String XFORM_PREFIX = "xform"; private static Logger log = Logger.getLogger(SelectProductArtifact.class); private Map products; private Product current; private Artifact artifact; private String name; private Map<String, InputData> preSettings = null; public SelectProductArtifact() { super(); } @Override public void setup( String identifier, ArtifactFactory factory, Object context, Document data) { log.debug("SelectProductArtifact.setup()"); super.setup(identifier, factory, context,data); this.name = factory.getName(); // Read the Parameters that should be used for the setup from the // Data-XML.Document. NodeList parameterNodes = (NodeList) XMLUtils.xpath(data, XPATH_SETUP_PARAMETER_NODES, XPathConstants.NODESET, ArtifactNamespaceContext.INSTANCE); if (parameterNodes != null && parameterNodes.getLength() > 0){ this.preSettings = new HashMap<String, InputData>(); for (int i = 0; i < parameterNodes.getLength(); i++){ Element parameterNode = (Element)parameterNodes.item(i); String name = parameterNode.getAttribute("name"); String value = parameterNode.getAttribute("value"); log.debug("Name: "+name); log.debug("Value: "+value); InputData inputData = this.preSettings.get(name); if (inputData == null){ inputData = new DefaultInputData(name, value); this.preSettings.put(name, inputData); }else{ inputData.concartValue(value); } } } } public void setProducts(Map products) { this.products = products; } @Override public Document feed(Document target, CallContext context) { log.debug("SelectProductArtifact.feed()"); if (artifact == null) { Document document = XMLUtils.newDocument(); String productName = XMLUtils.xpathString( target, XPATH_INPUT_DATA_VALUE, ArtifactNamespaceContext.INSTANCE ); current = (Product) products.get(productName); String reportNode = null; String resultNode = null; String msg = null; if (current != null) { reportNode = "result"; resultNode = "success"; msg = "Feed was successfully. New Artifact created."; } else { reportNode = "exceptionreport"; resultNode = "exception"; msg = "Product does not exist."; } XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( document, ArtifactNamespaceContext.NAMESPACE_URI, ArtifactNamespaceContext.NAMESPACE_PREFIX ); Element report = creator.create(reportNode); Element success = creator.create(resultNode); success.setTextContent(msg); report.appendChild(success); document.appendChild(report); return document; } else { return artifact.feed(target, context); } } @Override public Document advance(Document target, CallContext context) { log.debug("SelectProductArtifact.advance()"); if (artifact != null) { Document result = artifact.advance(target, context); context.putContextValue(ProxyArtifact.REPLACE_PROXY, artifact); return result; } Document result = XMLUtils.newDocument(); if (current == null) { // artifact needs to be feeded first String msg = "Artifact is not configured properly. Call 'feed' fist."; log.error(msg); XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( result, ArtifactNamespaceContext.NAMESPACE_URI, ArtifactNamespaceContext.NAMESPACE_PREFIX ); Element report = creator.create("exceptionreport"); Element exception = creator.create("exception"); exception.setTextContent(msg); report.appendChild(exception); result.appendChild(report); return result; } String uuid = XMLUtils.xpathString( target, XPATH_UUID, ArtifactNamespaceContext.INSTANCE ); String hash = XMLUtils.xpathString( target, XPATH_HASH, ArtifactNamespaceContext.INSTANCE ); // fetch ArtifactFactory from context and create a new Artifact ArtifactFactory factory = context.getDatabase() .getInternalArtifactFactory(this.name); factory = ((GNVProductArtifactFactory)factory) .getArtifactFactoryByName(current.getName()); artifact = factory.createArtifact(uuid, context, null); if (this.preSettings != null && artifact instanceof PreSettingArtifact){ ((PreSettingArtifact)artifact).setPreSettings(this.preSettings); } artifact.feed(feedDocument(uuid, hash), context); result = ((GNVArtifactBase) artifact).initialize(context); if (artifact instanceof GNVArtifactBase) { ((GNVArtifactBase) artifact).setProduct(current); } context.putContextValue(ProxyArtifact.REPLACE_PROXY, artifact); return result; } @Override public Document describe(Document data, CallContext context) { log.debug("SelectProductArtifact.describe()"); // create root node Document document = XMLUtils.newDocument(); XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( document, ArtifactNamespaceContext.NAMESPACE_URI, ArtifactNamespaceContext.NAMESPACE_PREFIX ); Element rootNode = creator.create("result"); Element typeNode = creator.create("type"); creator.addAttr(typeNode, "name", "describe"); rootNode.appendChild(typeNode); Element uuidNode = creator.create("uuid"); creator.addAttr(uuidNode, "value", super.identifier); rootNode.appendChild(uuidNode); Element hashNode = creator.create("hash"); creator.addAttr(hashNode, "value", hash()); rootNode.appendChild(hashNode); // create output node Element out = creator.create("outputs"); rootNode.appendChild(out); // create current state Element state = creator.create("state"); creator.addAttr(state, "name", "choose-product"); creator.addAttr(state, "description", "Auswahl des Produktes."); rootNode.appendChild(state); // create reachable states Element rStates = creator.create("reachable-states"); appendProducts(document, rStates, context); rootNode.appendChild(rStates); // create model Element model = creator.create("model"); Element input = creator.create("input"); creator.addAttr(input, "name", "product"); creator.addAttr(input, "type", "String"); model.appendChild(input); rootNode.appendChild(model); // create ui Element ui = creator.create("ui"); Element staticNode = creator.create("static"); Element dynamic = creator.create("dynamic"); appendFis(document, staticNode, context, this.name); appendSelectProducts(document, dynamic, context.getMeta()); ui.appendChild(staticNode); ui.appendChild(dynamic); rootNode.appendChild(ui); document.appendChild(rootNode); return document; } @Override public void out(Document document, OutputStream out, CallContext context) throws IOException { log.debug("SelectProductArtifact.out()"); if (artifact != null) { artifact.out(document, out, context); } } protected void appendProducts( Document document, Node parent, Object context ) { Iterator iter = products.values().iterator(); XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( document, ArtifactNamespaceContext.NAMESPACE_URI, ArtifactNamespaceContext.NAMESPACE_PREFIX ); while(iter.hasNext()) { Product prod = (Product) iter.next(); String name = prod.getName(); Element current = creator.create("state"); creator.addAttr(current, "name", name); creator.addAttr(current, "description", name); parent.appendChild(current); } } protected void appendSelectProducts( Document document, Node node, CallMeta callMeta ) { RessourceFactory ressource = RessourceFactory.getInstance(); XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( document, XFORM_URL, XFORM_PREFIX ); String selectboxName = "product"; Element selectNode = creator.create("select1"); creator.addAttr(selectNode, "ref", selectboxName); Element lableNode = creator.create("label"); lableNode.setTextContent(ressource.getRessource(callMeta.getLanguages(), selectboxName, selectboxName ) ); Element choiceNode = creator.create("choices"); selectNode.appendChild(lableNode); selectNode.appendChild(choiceNode); Iterator it = products.values().iterator(); while (it.hasNext()) { Product p = (Product) it.next(); Element itemNode = creator.create("item"); Element choiceLableNode = creator.create("label"); choiceLableNode.setTextContent(ressource.getRessource( callMeta.getLanguages(), p.getName(), p.getName() )); itemNode.appendChild(choiceLableNode); Element choiceValueNode = creator.create("value"); choiceValueNode.setTextContent(p.getName()); itemNode.appendChild(choiceValueNode); choiceNode.appendChild(itemNode); } node.appendChild(selectNode); } protected Document feedDocument(String uuid, String hash) { Document document = XMLUtils.newDocument(); XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( document, ArtifactNamespaceContext.NAMESPACE_URI, ArtifactNamespaceContext.NAMESPACE_PREFIX ); Element rootNode = creator.create("action"); Element typeNode = creator.create("type"); creator.addAttr(typeNode, "name", "feed"); rootNode.appendChild(typeNode); Element uuidNode = creator.create("uuid"); creator.addAttr(uuidNode, "value", uuid); rootNode.appendChild(uuidNode); Element hashNode = creator.create("hash"); creator.addAttr(hashNode, "value", hash); rootNode.appendChild(hashNode); Element dataNode = creator.create("data"); rootNode.appendChild(dataNode); Collection<InputData> parameter = this.current.getParameter(); if (parameter != null) { Iterator<InputData> parameterIt = parameter.iterator(); while (parameterIt.hasNext()) { InputData inputData = parameterIt.next(); Element inputNode = creator.create("input"); creator.addAttr(inputNode, "name", inputData.getName()); creator.addAttr(inputNode, "value", inputData.getValue()); dataNode.appendChild(inputNode); } } document.appendChild(rootNode); return document; } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8: