tim@52: /** tim@52: * tim@52: */ tim@52: package de.intevation.gnv.transition; tim@52: tim@52: import java.util.ArrayList; tim@52: import java.util.Collection; tim@61: import java.util.Date; tim@61: import java.util.GregorianCalendar; tim@57: import java.util.HashMap; tim@79: import java.util.HashSet; tim@57: import java.util.Iterator; tim@81: import java.util.List; tim@57: import java.util.Map; tim@79: import java.util.Set; tim@52: tim@52: import org.apache.log4j.Logger; tim@52: import org.w3c.dom.Document; tim@59: import org.w3c.dom.Element; tim@52: import org.w3c.dom.Node; tim@52: import org.w3c.dom.NodeList; tim@52: tim@52: import de.intevation.artifactdatabase.Config; tim@52: import de.intevation.gnv.artifacts.GNVArtifactBase; tim@58: import de.intevation.gnv.geobackend.base.Result; tim@58: import de.intevation.gnv.geobackend.base.query.QueryExecutor; tim@58: import de.intevation.gnv.geobackend.base.query.QueryExecutorFactory; tim@58: import de.intevation.gnv.geobackend.base.query.exception.QueryException; tim@61: import de.intevation.gnv.geobackend.util.DateUtils; tim@68: import de.intevation.gnv.transition.describedata.DefaultKeyValueDescribeData; tim@68: import de.intevation.gnv.transition.describedata.KeyValueDescibeData; tim@61: import de.intevation.gnv.transition.describedata.MinMaxDescribeData; tim@79: import de.intevation.gnv.transition.describedata.NamedArrayList; tim@79: import de.intevation.gnv.transition.describedata.NamedCollection; tim@81: import de.intevation.gnv.transition.describedata.SingleValueDescribeData; tim@57: import de.intevation.gnv.transition.exception.TransitionException; tim@76: import de.intevation.gnv.utils.ArtifactXMLUtilities; tim@52: tim@52: /** tim@52: * @author Tim Englich tim@52: * tim@52: */ tim@52: public abstract class TransitionBase implements Transition { tim@77: tim@77: /** tim@77: * The UID of this Class tim@77: */ tim@77: private static final long serialVersionUID = 2411169179001645426L; tim@77: tim@52: /** tim@52: * the logger, used to log exceptions and additonaly information tim@52: */ tim@52: private static Logger log = Logger.getLogger(GNVArtifactBase.class); tim@52: tim@52: private String id = null; tim@52: tim@53: private String description = null; tim@53: tim@81: protected String dataName = null; tim@79: tim@79: private boolean dataMultiSelect = false; tim@53: tim@52: protected String queryID = null; tim@52: tim@52: private Collection reachableTransitions = null; tim@52: tim@79: protected Collection inputValueNames = null; tim@57: tim@57: private Map inputValues = null; tim@55: tim@52: private Transition parent = null; tim@57: tim@99: protected Map inputData = null; tim@58: tim@61: protected Collection descibeData = null; tim@61: tim@52: /** tim@52: * Constructor tim@52: */ tim@52: public TransitionBase() { tim@52: super(); tim@52: } tim@52: tim@52: /** tim@52: * @see de.intevation.gnv.transition.Transition#getID() tim@52: */ tim@52: public String getID() { tim@52: return this.id; tim@52: } tim@53: tim@53: /** tim@53: * @see de.intevation.gnv.transition.Transition#getDescription() tim@53: */ tim@53: public String getDescription() { tim@53: return this.description; tim@53: } tim@53: tim@52: /** tim@52: * @see de.intevation.gnv.transition.Transition#reachableTransitions() tim@52: */ tim@52: public Collection reachableTransitions() { tim@52: return this.reachableTransitions; tim@52: } tim@52: tim@52: /** tim@55: * @see de.intevation.gnv.transition.Transition#getRequiredInputValues() tim@55: */ tim@55: public Collection getRequiredInputValues() { tim@57: return this.inputValues.values(); tim@55: } tim@55: tim@55: /** tim@52: * @see de.intevation.gnv.transition.Transition#setup(org.w3c.dom.Node) tim@52: */ tim@52: public void setup(Node configuration) { tim@52: tim@52: this.id = Config.getStringXPath(configuration,"@id"); tim@54: this.description = Config.getStringXPath(configuration,"@description"); tim@53: tim@52: log.info("Transition-ID = "+ this.id); tim@52: NodeList nodes = Config.getNodeSetXPath(configuration,"reachableTransitions/transition"); tim@52: this.reachableTransitions = new ArrayList(nodes.getLength()); tim@52: for (int i = 0 ; i < nodes.getLength(); i++){ tim@52: String reachableTransition = nodes.item(i).getTextContent(); tim@52: log.info("ReachableTransition ==> "+ reachableTransition); tim@52: this.reachableTransitions.add(reachableTransition); tim@52: tim@52: } tim@55: tim@55: NodeList inputValuesNodes = Config.getNodeSetXPath(configuration,"inputvalues/inputvalue"); tim@57: this.inputValues = new HashMap(inputValuesNodes.getLength()); tim@57: this.inputValueNames = new ArrayList(inputValuesNodes.getLength()); tim@55: for (int i = 0 ; i < inputValuesNodes.getLength(); i++){ tim@55: Node inputValueNode = inputValuesNodes.item(i); tim@81: String usedinQueryValue = Config.getStringXPath(inputValueNode,"@usedinquery"); tim@81: int usedinQuery = 1; tim@81: if (usedinQueryValue != null){ tim@81: try { tim@81: usedinQuery = Integer.parseInt(usedinQueryValue); tim@81: } catch (NumberFormatException e) { tim@81: log.warn("Used in Query Value cannot be transformed into a Number"); tim@81: } tim@81: } tim@81: InputValue inputValue = new DefaultInputValue( tim@81: Config.getStringXPath(inputValueNode,"@name"), tim@81: Config.getStringXPath(inputValueNode,"@type"), tim@81: Boolean.parseBoolean(Config.getStringXPath(inputValueNode,"@multiselect")), tim@81: usedinQuery); tim@56: log.debug(inputValue.toString()); tim@57: this.inputValues.put(inputValue.getName(),inputValue); tim@57: this.inputValueNames.add(inputValue.getName()); tim@55: } tim@55: tim@52: this.queryID = Config.getStringXPath(configuration,"queryID"); tim@57: log.info("QueryID ==> "+ this.queryID); tim@52: tim@79: this.dataName = Config.getStringXPath(configuration,"dataname"); tim@79: tim@79: String dataMultiSelectValue = Config.getStringXPath(configuration,"data-multiselect"); tim@79: if (dataMultiSelectValue != null){ tim@79: this.dataMultiSelect = Boolean.parseBoolean(dataMultiSelectValue); tim@79: } tim@79: tim@52: } tim@52: tim@52: /** tim@52: * @see de.intevation.gnv.transition.Transition#getParent() tim@52: */ tim@52: public Transition getParent() { tim@52: return this.parent; tim@52: } tim@52: tim@52: /** tim@52: * @see de.intevation.gnv.transition.Transition#setParent(de.intevation.gnv.transition.Transition) tim@52: */ tim@52: public void setParent(Transition transition) { tim@52: this.parent = transition; tim@52: } tim@57: tim@57: /** tim@57: * @see de.intevation.gnv.transition.Transition#putInputData(java.util.Collection) tim@57: */ tim@99: public void putInputData(Collection inputData, String uuid) throws TransitionException { tim@57: log.debug("TransitionBase.putInputData"); tim@57: if (inputData != null){ tim@57: Iterator it = inputData.iterator(); tim@57: while(it.hasNext()){ tim@57: InputData tmpItem = it.next(); tim@57: InputValue inputValue = this.inputValues.get(tmpItem.getName()); tim@57: if (inputValue != null){ tim@57: if (this.inputData == null){ tim@58: this.inputData = new HashMap(inputData.size()); tim@57: } tim@79: this.setSelection(tmpItem); tim@59: this.inputData.put(tmpItem.getName(),tmpItem); tim@57: tim@57: }else{ tim@57: String errMsg = "No Inputvalue given for Inputdata "+ tmpItem.getName(); tim@57: log.warn(errMsg+ "Value will be ignored"); tim@57: tim@57: } tim@57: } tim@57: }else{ tim@57: log.warn("No Inputdata given"); tim@57: } tim@79: } tim@57: tim@79: private void setSelection(InputData inputData){ tim@79: log.debug("TransitionBase.setSelection"); tim@79: tim@79: Object o = this.getDescribeData(inputData.getName()); tim@79: if(o != null){ tim@79: if (o instanceof Collection){ tim@79: Collection values = (Collection)o; tim@79: tim@79: String value = inputData.getValue(); tim@79: String[] selectedValues = value.split(","); tim@79: Set selectedItems = new HashSet(selectedValues.length); tim@79: for (int i = 0; i < selectedValues.length; i++){ tim@79: selectedItems.add(selectedValues[i].trim()); tim@79: } tim@79: // Selektion umsetzen tim@79: Iterator it = values.iterator(); tim@79: while (it.hasNext()){ tim@79: KeyValueDescibeData data = it.next(); tim@79: String key = ""+data.getKey(); tim@79: boolean selected = selectedItems.contains(key); tim@79: data.setSelected(selected); tim@79: } tim@79: }else if (o instanceof MinMaxDescribeData){ tim@79: MinMaxDescribeData data = (MinMaxDescribeData)o; tim@79: if (inputData.getName().equals(data.getMinName())){ tim@79: // TODO: müssen die werte geparst werden? tim@79: data.setMinValue(inputData.getValue()); tim@79: } tim@79: if (inputData.getName().equals(data.getMaxName())){ tim@79: // TODO: müssen die werte geparst werden? tim@79: data.setMaxValue(inputData.getValue()); tim@79: } tim@79: } tim@79: } tim@79: } tim@79: tim@79: private Object getDescribeData(String name){ tim@79: if (this.descibeData != null){ tim@79: Iterator it = this.descibeData.iterator(); tim@79: while (it.hasNext()){ tim@79: Object o = it.next(); tim@79: if (o instanceof NamedCollection){ tim@79: if (name.equals(((NamedCollection)o).getName())){ tim@79: return o; tim@79: } tim@79: }else if (o instanceof MinMaxDescribeData){ tim@79: if (name.equals(((MinMaxDescribeData)o).getMinName())){ tim@79: return o; tim@79: } tim@79: if (name.equals(((MinMaxDescribeData)o).getMaxName())){ tim@79: return o; tim@79: } tim@79: } tim@79: } tim@79: } tim@79: return null; tim@79: tim@57: } tim@58: tim@58: /** tim@58: * @see de.intevation.gnv.transition.Transition#isTransitionReachable(java.lang.String) tim@58: */ tim@58: public boolean isTransitionReachable(String transitionID) { tim@58: log.debug("TransitionBase.isTransitionReachable"); tim@58: boolean returnValue = false; tim@58: Iterator transitions = reachableTransitions.iterator(); tim@58: while (transitions.hasNext()){ tim@58: if(transitions.next().equals(transitionID)){ tim@58: log.debug("Transition "+transitionID+" wird unterstützt."); tim@58: returnValue = true; tim@58: break; tim@58: } tim@58: } tim@58: return returnValue; tim@58: } tim@58: tim@58: /** tim@58: * @see de.intevation.gnv.transition.Transition#advance() tim@58: */ tim@99: public void advance(String uuid) throws TransitionException { tim@58: log.debug("TransitionBase.advance"); tim@58: try { tim@81: List list = new ArrayList(); tim@81: tim@81: tim@58: Iterator it = this.inputValueNames.iterator(); tim@58: int i = 0; tim@58: while (it.hasNext()){ tim@59: String value = it.next(); tim@59: InputData data = this.inputData.get(value); tim@83: if (data != null && this.inputValues.containsKey(data.getName())){ tim@83: int size = this.inputValues.get(data.getName()).usedInQueries(); tim@83: String type = this.inputValues.get(data.getName()).getType(); tim@83: String requestValue = data.getValue(); tim@83: if (type.equalsIgnoreCase("string")){ tim@83: requestValue = this.prepareInputData4DBQuery(requestValue); tim@83: }else if (type.equalsIgnoreCase("date")){ tim@83: requestValue = this.prepareInputData4DateDBQuery(requestValue); tim@83: } tim@83: for (int j = 0; j < size; j++){ tim@83: list.add(requestValue); tim@83: } tim@81: } tim@58: } tim@81: String[] filterValues = list.toArray(new String[0]); tim@81: Collection result = null; tim@81: try { tim@81: if (this.queryID != null){ tim@81: QueryExecutor queryExecutor = QueryExecutorFactory.getInstance().getQueryExecutor(); tim@81: result = queryExecutor.executeQuery(this.queryID, filterValues); tim@81: } tim@99: this.purifyResult(result, uuid); tim@81: } catch (RuntimeException e) { tim@81: log.error(e,e); tim@81: } tim@58: } catch (QueryException e) { tim@58: log.error(e,e); tim@58: throw new TransitionException(e); tim@58: } tim@58: } tim@83: tim@83: private String prepareInputData4DateDBQuery(String value){ tim@83: if (value != null){ tim@83: String[] values = value.split(","); tim@83: String newValue = ""; tim@83: for (int i = 0; i < values.length; i++){ tim@83: if (newValue.length() > 0){ tim@83: newValue= newValue + " , "; tim@83: } tim@88: // TODO JUST HACK FIND A BETTER RESOLUTION tim@88: newValue = newValue + "to_date('"+values[i].trim()+"', 'YYYY.MM.DD HH24:MI:SS')"; tim@83: } tim@83: return newValue; tim@83: } tim@83: tim@83: return value; tim@83: } tim@83: private String prepareInputData4DBQuery(String value){ tim@83: if (value != null){ tim@83: String[] values = value.split(","); tim@83: String newValue = ""; tim@83: for (int i = 0; i < values.length; i++){ tim@83: if (newValue.length() > 0){ tim@83: newValue= newValue + " , "; tim@83: } tim@84: newValue = newValue + "'"+values[i].trim()+"'"; tim@83: } tim@83: return newValue; tim@83: } tim@83: tim@83: return value; tim@83: tim@83: } tim@61: tim@61: /** tim@61: * @param result tim@61: */ tim@99: protected void purifyResult(Collection result, String uuid) { tim@61: if (this.descibeData == null){ tim@61: this.descibeData = new ArrayList(); tim@61: } tim@79: Iterator rit = result.iterator(); tim@79: NamedCollection keyValueDescibeData = new NamedArrayList(this.dataName,result.size()); tim@79: keyValueDescibeData.setMultiSelect(this.dataMultiSelect); tim@79: while(rit.hasNext()){ tim@79: Result resultValue = rit.next(); tim@82: keyValueDescibeData.add(new DefaultKeyValueDescribeData(resultValue.getString("KEY"), resultValue.getString("VALUE"))); tim@68: } tim@68: this.descibeData.add(keyValueDescibeData); tim@61: } tim@59: tim@59: /** tim@59: * @see de.intevation.gnv.transition.Transition#describe(org.w3c.dom.Document, org.w3c.dom.Node) tim@59: */ tim@59: public void describe(Document document, Node rootNode) { tim@59: tim@59: if(this.descibeData != null){ tim@76: ArtifactXMLUtilities xmlutilities = new ArtifactXMLUtilities(); tim@59: Iterator it = this.descibeData.iterator(); tim@79: Node staticNode = xmlutilities.createArtifactElement(document, "static"); tim@79: Node dynamic = xmlutilities.createArtifactElement(document, "dynamic"); tim@79: rootNode.appendChild(staticNode); tim@79: rootNode.appendChild(dynamic); tim@59: while (it.hasNext()){ tim@77: tim@59: Object o = it.next(); tim@79: if (o instanceof Collection){ tim@79: String name = null; tim@79: boolean multiselect = false; tim@79: if (o instanceof NamedCollection){ tim@79: NamedCollection nc = ((NamedCollection)o); tim@79: name = nc.getName(); tim@79: multiselect = nc.isMultiSelect(); tim@79: }else{ tim@79: Object[] names = this.inputValueNames.toArray(); tim@79: name = names[names.length-1].toString(); tim@79: } tim@79: tim@61: tim@78: Element selectNode = xmlutilities.createXFormElement(document,multiselect ? "select" : "select1"); tim@77: selectNode.setAttribute("ref", name); tim@59: tim@77: Element lableNode = xmlutilities.createXFormElement(document, "label"); tim@77: lableNode.setTextContent(name); tim@77: Element choiceNode = xmlutilities.createXFormElement(document, "choices"); tim@59: tim@79: Collection values = (Collection)o; tim@77: Iterator resultIt = values.iterator(); tim@77: while (resultIt.hasNext()){ tim@77: KeyValueDescibeData result = resultIt.next(); tim@77: Element itemNode = xmlutilities.createXFormElement(document, "item"); tim@77: tim@79: if (result.isSelected()){ tim@79: itemNode.setAttribute("selected", "true"); tim@79: } tim@79: tim@77: tim@77: Element choiceLableNode = xmlutilities.createXFormElement(document, "label"); tim@77: choiceLableNode.setTextContent(result.getValue()); tim@77: itemNode.appendChild(choiceLableNode); tim@77: tim@77: Element choicValueNode = xmlutilities.createXFormElement(document, "value"); tim@77: choicValueNode.setTextContent(""+result.getKey()); tim@77: itemNode.appendChild(choicValueNode); tim@77: choiceNode.appendChild(itemNode); tim@77: } tim@77: selectNode.appendChild(lableNode); tim@77: selectNode.appendChild(choiceNode); tim@79: tim@79: if (!it.hasNext()){ tim@79: dynamic.appendChild(selectNode); tim@79: }else{ tim@79: staticNode.appendChild(selectNode); tim@79: } tim@79: tim@77: }else if (o instanceof MinMaxDescribeData){ tim@77: MinMaxDescribeData descibeData = (MinMaxDescribeData)o; tim@77: Object min = descibeData.getMinValue(); tim@77: Object max = descibeData.getMaxValue(); tim@77: if (min instanceof GregorianCalendar){ tim@77: Date d = ((GregorianCalendar)min).getTime(); tim@77: min = DateUtils.getPatternedDateAmer(d); tim@77: } tim@77: tim@77: if (max instanceof GregorianCalendar){ tim@77: Date d = ((GregorianCalendar)max).getTime(); tim@77: max = DateUtils.getPatternedDateAmer(d); tim@77: } tim@77: tim@77: Element inputMinNode = xmlutilities.createXFormElement(document, "input"); tim@77: inputMinNode.setAttribute("ref", "minvalue"); tim@77: Element inputMinLableNode = xmlutilities.createXFormElement(document, "label"); tim@77: inputMinLableNode.setTextContent("minvalue"); tim@77: inputMinNode.appendChild(inputMinLableNode); tim@77: tim@77: Element inputMinValueNode = xmlutilities.createXFormElement(document, "value"); tim@77: inputMinValueNode.setTextContent(min.toString()); tim@77: inputMinNode.appendChild(inputMinValueNode); tim@77: tim@77: Element inputMaxNode = xmlutilities.createXFormElement(document, "input"); tim@77: inputMaxNode.setAttribute("ref", "maxvalue"); tim@77: Element inputMaxLableNode = xmlutilities.createXFormElement(document, "label"); tim@77: inputMaxLableNode.setTextContent("maxvalue"); tim@77: inputMaxNode.appendChild(inputMaxLableNode); tim@77: tim@77: Element inputMaxValueNode = xmlutilities.createXFormElement(document, "value"); tim@77: inputMaxValueNode.setTextContent(max.toString()); tim@77: inputMaxNode.appendChild(inputMaxValueNode); tim@77: tim@77: tim@79: if (!it.hasNext()){ tim@79: dynamic.appendChild(inputMinNode); tim@79: dynamic.appendChild(inputMaxNode); tim@79: }else{ tim@79: staticNode.appendChild(inputMinNode); tim@79: staticNode.appendChild(inputMaxNode); tim@79: } tim@81: }else if (o instanceof SingleValueDescribeData){ tim@81: tim@81: SingleValueDescribeData svdb = (SingleValueDescribeData)o; tim@81: tim@81: Element inputNode = xmlutilities.createXFormElement(document, "input"); tim@81: inputNode.setAttribute("ref", svdb.getName()); tim@81: tim@81: Element inputLableNode = xmlutilities.createXFormElement(document, "label"); tim@81: inputLableNode.setTextContent(svdb.getName()); tim@81: inputNode.appendChild(inputLableNode); tim@81: tim@81: Element inputValueNode = xmlutilities.createXFormElement(document, "value"); tim@81: inputValueNode.setTextContent(svdb.getValue()); tim@81: inputNode.appendChild(inputValueNode); tim@81: tim@81: tim@81: if (!it.hasNext()){ tim@81: dynamic.appendChild(inputNode); tim@81: }else{ tim@81: staticNode.appendChild(inputNode); tim@81: } tim@59: } tim@81: tim@59: } tim@59: } tim@59: } tim@59: tim@58: /** tim@58: * @see de.intevation.gnv.transition.Transition#getDescibeData() tim@58: */ tim@58: public Collection getDescibeData() { tim@58: return this.descibeData; tim@58: } tim@58: tim@58: /** tim@58: * @see de.intevation.gnv.transition.Transition#setDescibeData(java.util.Collection) tim@58: */ tim@58: public void setDescibeData(Collection descibeData) { tim@58: this.descibeData = descibeData; tim@58: tim@58: } tim@59: tim@59: /** tim@59: * @see de.intevation.gnv.transition.Transition#getInputData() tim@59: */ tim@59: public Collection getInputData() throws TransitionException { tim@81: return this.inputData != null ? this.inputData.values() : null; tim@59: } tim@52: }