From 8f7f6aa92084cec5b2453e26185547dc82b8ebbc Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Fri, 3 Jul 2015 15:11:49 +0200 Subject: [PATCH] [Major] Major refactoring of toDom and toSax implementations - adding Activity and Action - remove methods on model itself and move to visitors --- ch.eitchnet.utils | 2 +- .../strolch/model/AbstractStrolchElement.java | 28 -- .../model/GroupedParameterizedElement.java | 30 -- .../li/strolch/model/ModelStatistics.java | 11 + .../src/main/java/li/strolch/model/Order.java | 43 +- .../java/li/strolch/model/ParameterBag.java | 22 -- .../strolch/model/ParameterizedElement.java | 85 ---- .../main/java/li/strolch/model/Resource.java | 65 +-- .../java/li/strolch/model/StrolchElement.java | 14 - .../li/strolch/model/StrolchValueType.java | 374 ++++++++++++++++++ .../src/main/java/li/strolch/model/Tags.java | 6 +- .../li/strolch/model/activity/Action.java | 84 ++-- .../li/strolch/model/activity/Activity.java | 71 ++-- .../model/activity/IActivityElement.java | 12 +- .../model/parameter/AbstractParameter.java | 73 ---- .../model/parameter/BooleanParameter.java | 33 +- .../model/parameter/DateParameter.java | 33 +- .../model/parameter/DurationParameter.java | 34 +- .../model/parameter/FloatListParameter.java | 32 +- .../model/parameter/FloatParameter.java | 34 +- .../model/parameter/IntegerListParameter.java | 32 +- .../model/parameter/IntegerParameter.java | 34 +- .../model/parameter/LongListParameter.java | 32 +- .../model/parameter/LongParameter.java | 34 +- .../li/strolch/model/parameter/Parameter.java | 12 +- .../model/parameter/StringListParameter.java | 32 +- .../model/parameter/StringParameter.java | 34 +- .../timedstate/AbstractStrolchTimedState.java | 74 ---- .../model/timedstate/BooleanTimedState.java | 51 +-- .../model/timedstate/FloatTimedState.java | 51 +-- .../model/timedstate/IntegerTimedState.java | 51 +-- .../model/timedstate/StringSetTimedState.java | 73 +--- .../model/timedstate/StrolchTimedState.java | 10 + .../li/strolch/model/timevalue/IValue.java | 5 + .../strolch/model/timevalue/IValueChange.java | 35 +- .../model/timevalue/impl/BooleanValue.java | 6 + .../model/timevalue/impl/FloatValue.java | 6 + .../model/timevalue/impl/IntegerValue.java | 6 + .../model/timevalue/impl/StringSetValue.java | 17 +- .../model/timevalue/impl/ValueChange.java | 29 +- .../model/xml/AbstractToSaxWriterVisitor.java | 100 +++-- .../model/xml/ActivityToDomVisitor.java | 51 +++ .../model/xml/ActivityToSaxWriterVisitor.java | 49 +++ .../model/xml/OrderFromDomVisitor.java | 58 +++ .../strolch/model/xml/OrderToDomVisitor.java | 18 +- .../model/xml/OrderToSaxWriterVisitor.java | 3 +- .../model/xml/ResourceFromDomVisitor.java | 105 +++++ .../model/xml/ResourceToDomVisitor.java | 18 +- .../model/xml/ResourceToSaxWriterVisitor.java | 3 +- .../xml/StrolchElementFromDomVisitor.java | 122 ++++++ .../model/xml/StrolchElementToDomVisitor.java | 222 +++++++++++ .../model/xml/XmlModelSaxFileReader.java | 3 +- .../strolch/model/xml/XmlModelSaxReader.java | 224 +++++------ .../test/java/li/strolch/model/ModelTest.java | 4 + .../java/li/strolch/model/XmlToDomTest.java | 9 +- .../li/strolch/model/activity/ActionTest.java | 3 +- .../strolch/model/activity/ActivityTest.java | 3 +- .../resources/data/activities/Activities.xml | 100 ++++- .../resources/data/resources/Resources.xml | 8 +- .../li/strolch/command/ClearModelCommand.java | 15 + .../command/XmlExportModelCommand.java | 14 + 61 files changed, 1605 insertions(+), 1237 deletions(-) create mode 100644 li.strolch.model/src/main/java/li/strolch/model/StrolchValueType.java create mode 100644 li.strolch.model/src/main/java/li/strolch/model/xml/ActivityToDomVisitor.java create mode 100644 li.strolch.model/src/main/java/li/strolch/model/xml/ActivityToSaxWriterVisitor.java create mode 100644 li.strolch.model/src/main/java/li/strolch/model/xml/OrderFromDomVisitor.java create mode 100644 li.strolch.model/src/main/java/li/strolch/model/xml/ResourceFromDomVisitor.java create mode 100644 li.strolch.model/src/main/java/li/strolch/model/xml/StrolchElementFromDomVisitor.java create mode 100644 li.strolch.model/src/main/java/li/strolch/model/xml/StrolchElementToDomVisitor.java diff --git a/ch.eitchnet.utils b/ch.eitchnet.utils index 1689ff69a..9e449e56e 160000 --- a/ch.eitchnet.utils +++ b/ch.eitchnet.utils @@ -1 +1 @@ -Subproject commit 1689ff69a93f7fcf491400e36b941c905d368018 +Subproject commit 9e449e56eedc01ef24b9d4a8a5cbf5c52b013a48 diff --git a/li.strolch.model/src/main/java/li/strolch/model/AbstractStrolchElement.java b/li.strolch.model/src/main/java/li/strolch/model/AbstractStrolchElement.java index 44783903d..123572a80 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/AbstractStrolchElement.java +++ b/li.strolch.model/src/main/java/li/strolch/model/AbstractStrolchElement.java @@ -19,9 +19,6 @@ import java.text.MessageFormat; import li.strolch.exception.StrolchException; import li.strolch.model.Locator.LocatorBuilder; - -import org.w3c.dom.Element; - import ch.eitchnet.utils.helper.StringHelper; /** @@ -114,31 +111,6 @@ public abstract class AbstractStrolchElement implements StrolchElement { clone.setName(getName()); } - protected void fillElement(Element element) { - element.setAttribute(Tags.ID, getId()); - element.setAttribute(Tags.NAME, getName()); - element.setAttribute(Tags.TYPE, getType()); - } - - /** - * Builds the fields of this {@link StrolchElement} from a {@link Element} - * - * @param element - */ - protected void fromDom(Element element) { - String id = element.getAttribute(Tags.ID); - String name = element.getAttribute(Tags.NAME); - - if (id != null && name != null) { - setId(id); - setName(name); - } else { - String msg = "Check the values of the element: {0} either id or name attribute is null!"; //$NON-NLS-1$ - msg = MessageFormat.format(msg, element.getNodeName()); - throw new StrolchException(msg); - } - } - @Override public int hashCode() { final int prime = 31; diff --git a/li.strolch.model/src/main/java/li/strolch/model/GroupedParameterizedElement.java b/li.strolch.model/src/main/java/li/strolch/model/GroupedParameterizedElement.java index cbb073571..a211bba1e 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/GroupedParameterizedElement.java +++ b/li.strolch.model/src/main/java/li/strolch/model/GroupedParameterizedElement.java @@ -25,10 +25,6 @@ import java.util.Set; import li.strolch.exception.StrolchException; import li.strolch.exception.StrolchModelException; import li.strolch.model.parameter.Parameter; - -import org.w3c.dom.Element; -import org.w3c.dom.NodeList; - import ch.eitchnet.utils.helper.StringHelper; /** @@ -291,32 +287,6 @@ public abstract class GroupedParameterizedElement extends AbstractStrolchElement return new HashSet(this.parameterBagMap.keySet()); } - @Override - public void fromDom(Element element) { - super.fromDom(element); - - String type = element.getAttribute(Tags.TYPE); - setType(type); - - NodeList bags = element.getElementsByTagName(Tags.PARAMETER_BAG); - for (int i = 0; i < bags.getLength(); i++) { - Element bagElement = (Element) bags.item(i); - ParameterBag bag = new ParameterBag(bagElement); - addParameterBag(bag); - } - } - - @Override - protected void fillElement(Element element) { - super.fillElement(element); - - if (this.parameterBagMap != null) { - for (ParameterBag bag : this.parameterBagMap.values()) { - element.appendChild(bag.toDom(element.getOwnerDocument())); - } - } - } - /** * Fills {@link GroupedParameterizedElement} properties of this clone * diff --git a/li.strolch.model/src/main/java/li/strolch/model/ModelStatistics.java b/li.strolch.model/src/main/java/li/strolch/model/ModelStatistics.java index 31a70b0d4..30c21f1fa 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/ModelStatistics.java +++ b/li.strolch.model/src/main/java/li/strolch/model/ModelStatistics.java @@ -80,6 +80,17 @@ public class ModelStatistics { return this.nrOfActivities; } + /** + * Adds the statistics of the other statistics to this statistics instance + * + * @param statistics + */ + public void add(ModelStatistics statistics) { + this.nrOfOrders += statistics.nrOfOrders; + this.nrOfResources += statistics.nrOfResources; + this.nrOfActivities += statistics.nrOfActivities; + } + @Override public String toString() { StringBuilder builder = new StringBuilder(); diff --git a/li.strolch.model/src/main/java/li/strolch/model/Order.java b/li.strolch.model/src/main/java/li/strolch/model/Order.java index fe08fcb9a..b0d4ea9ff 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/Order.java +++ b/li.strolch.model/src/main/java/li/strolch/model/Order.java @@ -19,11 +19,6 @@ import java.util.Date; import li.strolch.model.Locator.LocatorBuilder; import li.strolch.model.visitor.StrolchRootElementVisitor; - -import org.w3c.dom.Document; -import org.w3c.dom.Element; - -import ch.eitchnet.utils.helper.StringHelper; import ch.eitchnet.utils.iso8601.ISO8601FormatFactory; /** @@ -80,30 +75,6 @@ public class Order extends GroupedParameterizedElement implements StrolchRootEle setDate(date); } - /** - * DOM Constructor - * - * @param element - */ - public Order(Element element) { - super.fromDom(element); - - String date = element.getAttribute(Tags.DATE); - String state = element.getAttribute(Tags.STATE); - - if (StringHelper.isEmpty(date)) { - setDate(ISO8601FormatFactory.getInstance().getDateFormat().parse("-")); //$NON-NLS-1$ - } else { - setDate(ISO8601FormatFactory.getInstance().getDateFormat().parse(date)); - } - - if (state == null || state.isEmpty()) { - setState(State.CREATED); - } else { - setState(State.valueOf(state)); - } - } - /** * @return the date */ @@ -134,18 +105,6 @@ public class Order extends GroupedParameterizedElement implements StrolchRootEle this.state = state; } - @Override - public Element toDom(Document doc) { - - Element orderElement = doc.createElement(Tags.ORDER); - fillElement(orderElement); - - orderElement.setAttribute(Tags.DATE, ISO8601FormatFactory.getInstance().formatDate(this.date)); - orderElement.setAttribute(Tags.STATE, this.state.toString()); - - return orderElement; - } - @Override public Order getClone() { Order clone = new Order(); @@ -179,7 +138,7 @@ public class Order extends GroupedParameterizedElement implements StrolchRootEle public Order getRootElement() { return this; } - + @Override public boolean isRootElement() { return true; diff --git a/li.strolch.model/src/main/java/li/strolch/model/ParameterBag.java b/li.strolch.model/src/main/java/li/strolch/model/ParameterBag.java index 3d59f23e4..8bc9541a4 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/ParameterBag.java +++ b/li.strolch.model/src/main/java/li/strolch/model/ParameterBag.java @@ -17,9 +17,6 @@ package li.strolch.model; import li.strolch.model.Locator.LocatorBuilder; -import org.w3c.dom.Document; -import org.w3c.dom.Element; - /** * @author Robert von Burg */ @@ -45,15 +42,6 @@ public class ParameterBag extends ParameterizedElement { super(id, name, type); } - /** - * DOM Constructor - * - * @param bagElement - */ - public ParameterBag(Element bagElement) { - super.fromDom(bagElement); - } - @Override public ParameterBag getClone() { ParameterBag clone = new ParameterBag(); @@ -68,16 +56,6 @@ public class ParameterBag extends ParameterizedElement { lb.append(this.id); } - @Override - public Element toDom(Document doc) { - - Element element = doc.createElement(Tags.PARAMETER_BAG); - - fillElement(element); - - return element; - } - @Override public boolean isRootElement() { return false; diff --git a/li.strolch.model/src/main/java/li/strolch/model/ParameterizedElement.java b/li.strolch.model/src/main/java/li/strolch/model/ParameterizedElement.java index 95242e77d..6810dbbcd 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/ParameterizedElement.java +++ b/li.strolch.model/src/main/java/li/strolch/model/ParameterizedElement.java @@ -26,23 +26,7 @@ import java.util.Set; import li.strolch.exception.StrolchException; import li.strolch.model.Locator.LocatorBuilder; -import li.strolch.model.parameter.BooleanParameter; -import li.strolch.model.parameter.DateParameter; -import li.strolch.model.parameter.DurationParameter; -import li.strolch.model.parameter.FloatListParameter; -import li.strolch.model.parameter.FloatParameter; -import li.strolch.model.parameter.IntegerListParameter; -import li.strolch.model.parameter.IntegerParameter; -import li.strolch.model.parameter.LongListParameter; -import li.strolch.model.parameter.LongParameter; import li.strolch.model.parameter.Parameter; -import li.strolch.model.parameter.StringListParameter; -import li.strolch.model.parameter.StringParameter; - -import org.w3c.dom.Element; -import org.w3c.dom.NodeList; - -import ch.eitchnet.utils.dbc.DBC; import ch.eitchnet.utils.helper.StringHelper; /** @@ -206,75 +190,6 @@ public abstract class ParameterizedElement extends AbstractStrolchElement { return lb.build(); } - // TODO remove the whole fromDom methods from strolch model - we want to use the visitor pattern only! - - @Override - protected void fromDom(Element element) { - super.fromDom(element); - - String type = element.getAttribute(Tags.TYPE); - setType(type); - - // add all the parameters - NodeList parameterElements = element.getElementsByTagName(Tags.PARAMETER); - for (int i = 0; i < parameterElements.getLength(); i++) { - Element paramElement = (Element) parameterElements.item(i); - String paramtype = paramElement.getAttribute(Tags.TYPE); - - DBC.PRE.assertNotEmpty("Type must be set on Parameter for bag with id " + this.id, paramtype); - - if (paramtype.equals(StringParameter.TYPE)) { - StringParameter param = new StringParameter(paramElement); - addParameter(param); - } else if (paramtype.equals(IntegerParameter.TYPE)) { - IntegerParameter param = new IntegerParameter(paramElement); - addParameter(param); - } else if (paramtype.equals(FloatParameter.TYPE)) { - FloatParameter param = new FloatParameter(paramElement); - addParameter(param); - } else if (paramtype.equals(LongParameter.TYPE)) { - LongParameter param = new LongParameter(paramElement); - addParameter(param); - } else if (paramtype.equals(DateParameter.TYPE)) { - DateParameter param = new DateParameter(paramElement); - addParameter(param); - } else if (paramtype.equals(DurationParameter.TYPE)) { - DurationParameter param = new DurationParameter(paramElement); - addParameter(param); - } else if (paramtype.equals(BooleanParameter.TYPE)) { - BooleanParameter param = new BooleanParameter(paramElement); - addParameter(param); - } else if (paramtype.equals(StringListParameter.TYPE)) { - StringListParameter param = new StringListParameter(paramElement); - addParameter(param); - } else if (paramtype.equals(IntegerListParameter.TYPE)) { - IntegerListParameter param = new IntegerListParameter(paramElement); - addParameter(param); - } else if (paramtype.equals(FloatListParameter.TYPE)) { - FloatListParameter param = new FloatListParameter(paramElement); - addParameter(param); - } else if (paramtype.equals(LongListParameter.TYPE)) { - LongListParameter param = new LongListParameter(paramElement); - addParameter(param); - } else { - String msg = "What kind of parameter is this: {0}"; //$NON-NLS-1$ - msg = MessageFormat.format(msg, paramtype); - throw new StrolchException(msg); - } - } - } - - @Override - protected void fillElement(Element element) { - super.fillElement(element); - - if (this.parameterMap != null) { - for (Parameter parameter : this.parameterMap.values()) { - element.appendChild(parameter.toDom(element.getOwnerDocument())); - } - } - } - @Override protected void fillClone(StrolchElement clone) { super.fillClone(clone); diff --git a/li.strolch.model/src/main/java/li/strolch/model/Resource.java b/li.strolch.model/src/main/java/li/strolch/model/Resource.java index d028e72c4..210d16a40 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/Resource.java +++ b/li.strolch.model/src/main/java/li/strolch/model/Resource.java @@ -15,7 +15,6 @@ */ package li.strolch.model; -import java.text.MessageFormat; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; @@ -24,22 +23,11 @@ import java.util.List; import java.util.Map; import java.util.Set; -import li.strolch.exception.StrolchException; import li.strolch.model.Locator.LocatorBuilder; -import li.strolch.model.timedstate.BooleanTimedState; -import li.strolch.model.timedstate.FloatTimedState; -import li.strolch.model.timedstate.IntegerTimedState; -import li.strolch.model.timedstate.StringSetTimedState; import li.strolch.model.timedstate.StrolchTimedState; import li.strolch.model.timevalue.IValue; import li.strolch.model.visitor.StrolchRootElementVisitor; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.NodeList; - -import ch.eitchnet.utils.dbc.DBC; - /** * @author Robert von Burg */ @@ -67,41 +55,6 @@ public class Resource extends GroupedParameterizedElement implements StrolchRoot super(id, name, type); } - /** - * DOM Constructor - * - * @param element - */ - public Resource(Element element) { - super.fromDom(element); - - NodeList timedStateElems = element.getElementsByTagName(Tags.TIMED_STATE); - for (int i = 0; i < timedStateElems.getLength(); i++) { - Element timedStateElem = (Element) timedStateElems.item(i); - String typeS = timedStateElem.getAttribute(Tags.TYPE); - - DBC.PRE.assertNotEmpty("Type must be set on TimedState for resource with id " + this.id, typeS); - - if (typeS.equals(FloatTimedState.TYPE)) { - FloatTimedState timedState = new FloatTimedState(timedStateElem); - addTimedState(timedState); - } else if (typeS.equals(IntegerTimedState.TYPE)) { - IntegerTimedState timedState = new IntegerTimedState(timedStateElem); - addTimedState(timedState); - } else if (typeS.equals(BooleanTimedState.TYPE)) { - BooleanTimedState timedState = new BooleanTimedState(timedStateElem); - addTimedState(timedState); - } else if (typeS.equals(StringSetTimedState.TYPE)) { - StringSetTimedState timedState = new StringSetTimedState(timedStateElem); - addTimedState(timedState); - } else { - String msg = "What kind of TimedState is this: {0}"; //$NON-NLS-1$ - msg = MessageFormat.format(msg, typeS); - throw new StrolchException(msg); - } - } - } - @SuppressWarnings("unchecked") public void addTimedState(StrolchTimedState strolchTimedState) { if (this.timedStateMap == null) { @@ -150,22 +103,6 @@ public class Resource extends GroupedParameterizedElement implements StrolchRoot return this.timedStateMap != null && this.timedStateMap.containsKey(id); } - @Override - public Element toDom(Document doc) { - - Element element = doc.createElement(Tags.RESOURCE); - fillElement(element); - - if (this.timedStateMap != null) { - for (StrolchTimedState state : this.timedStateMap.values()) { - Element timedStateElem = state.toDom(element.getOwnerDocument()); - element.appendChild(timedStateElem); - } - } - - return element; - } - @Override public Resource getClone() { Resource clone = new Resource(); @@ -202,7 +139,7 @@ public class Resource extends GroupedParameterizedElement implements StrolchRoot public Resource getRootElement() { return this; } - + @Override public boolean isRootElement() { return true; diff --git a/li.strolch.model/src/main/java/li/strolch/model/StrolchElement.java b/li.strolch.model/src/main/java/li/strolch/model/StrolchElement.java index 7328bc4a8..1f3ad4734 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/StrolchElement.java +++ b/li.strolch.model/src/main/java/li/strolch/model/StrolchElement.java @@ -17,9 +17,6 @@ package li.strolch.model; import java.io.Serializable; -import org.w3c.dom.Document; -import org.w3c.dom.Element; - /** * @author Robert von Burg */ @@ -76,17 +73,6 @@ public interface StrolchElement extends Serializable, Comparable */ public long getDbid(); - /** - * Returns an {@link Element} object which is an XML representation of this object - * - * @param doc - * the document to which this element is being written. The client must not append to the document, the - * caller will perform this as needed - * - * @return - */ - public Element toDom(Document doc); - /** * Returns the type of this {@link StrolchElement} * diff --git a/li.strolch.model/src/main/java/li/strolch/model/StrolchValueType.java b/li.strolch.model/src/main/java/li/strolch/model/StrolchValueType.java new file mode 100644 index 000000000..160731a7d --- /dev/null +++ b/li.strolch.model/src/main/java/li/strolch/model/StrolchValueType.java @@ -0,0 +1,374 @@ +/* + * Copyright 2015 Robert von Burg + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package li.strolch.model; + +import java.text.MessageFormat; + +import li.strolch.model.parameter.BooleanParameter; +import li.strolch.model.parameter.DateParameter; +import li.strolch.model.parameter.DurationParameter; +import li.strolch.model.parameter.FloatListParameter; +import li.strolch.model.parameter.FloatParameter; +import li.strolch.model.parameter.IntegerListParameter; +import li.strolch.model.parameter.IntegerParameter; +import li.strolch.model.parameter.LongListParameter; +import li.strolch.model.parameter.LongParameter; +import li.strolch.model.parameter.Parameter; +import li.strolch.model.parameter.StringListParameter; +import li.strolch.model.parameter.StringParameter; +import li.strolch.model.timedstate.BooleanTimedState; +import li.strolch.model.timedstate.FloatTimedState; +import li.strolch.model.timedstate.IntegerTimedState; +import li.strolch.model.timedstate.StringSetTimedState; +import li.strolch.model.timedstate.StrolchTimedState; +import li.strolch.model.timedstate.TimedState; +import li.strolch.model.timevalue.IValue; +import li.strolch.model.timevalue.IValueChange; +import li.strolch.model.timevalue.impl.BooleanValue; +import li.strolch.model.timevalue.impl.FloatValue; +import li.strolch.model.timevalue.impl.IntegerValue; +import li.strolch.model.timevalue.impl.StringSetValue; + +public enum StrolchValueType { + + /** + * Can be used for:
+ *
    + *
  • {@link Parameter}
  • + *
  • {@link TimedState}
  • + *
  • {@link IValue}
  • + *
  • {@link IValueChange}
  • + *
+ */ + BOOLEAN("Boolean") { + @Override + public Parameter parameterInstance() { + return new BooleanParameter(); + } + + @Override + public StrolchTimedState> timedStateInstance() { + return new BooleanTimedState(); + } + + @Override + public IValue valueInstance(String valueAsString) { + return new BooleanValue(valueAsString); + } + }, + + /** + * Can be used for:
+ *
    + *
  • {@link Parameter}
  • + *
  • {@link TimedState}
  • + *
  • {@link IValue}
  • + *
  • {@link IValueChange}
  • + *
+ */ + INTEGER("Integer") { + @Override + public Parameter parameterInstance() { + return new IntegerParameter(); + } + + @Override + public StrolchTimedState> timedStateInstance() { + return new IntegerTimedState(); + } + + @Override + public IValue valueInstance(String valueAsString) { + return new IntegerValue(valueAsString); + } + }, + + /** + * Can be used for:
+ *
    + *
  • {@link Parameter}
  • + *
  • {@link TimedState}
  • + *
  • {@link IValue}
  • + *
  • {@link IValueChange}
  • + *
+ */ + FLOAT("Float") { + @Override + public Parameter parameterInstance() { + return new FloatParameter(); + } + + @Override + public StrolchTimedState> timedStateInstance() { + return new FloatTimedState(); + } + + @Override + public IValue valueInstance(String valueAsString) { + return new FloatValue(valueAsString); + } + }, + + /** + * Can be used for:
+ *
    + *
  • {@link Parameter}
  • + *
+ */ + LONG("Long") { + @Override + public Parameter parameterInstance() { + return new LongParameter(); + } + + @Override + public StrolchTimedState> timedStateInstance() { + throw new UnsupportedOperationException(MessageFormat.format( + "TimeStates of type {0} are not supported!", getType())); //$NON-NLS-1$ + } + + @Override + public IValue valueInstance(String valueAsString) { + throw new UnsupportedOperationException(MessageFormat.format( + "Parameters of type {0} are not supported!", getType())); //$NON-NLS-1$ + } + }, + + /** + * Can be used for:
+ *
    + *
  • {@link Parameter}
  • + *
+ */ + STRING("String") { + @Override + public Parameter parameterInstance() { + return new StringParameter(); + } + + @Override + public StrolchTimedState> timedStateInstance() { + throw new UnsupportedOperationException(MessageFormat.format( + "TimeStates of type {0} are not supported!", getType())); //$NON-NLS-1$ + } + + @Override + public IValue valueInstance(String valueAsString) { + throw new UnsupportedOperationException(MessageFormat.format( + "Parameters of type {0} are not supported!", getType())); //$NON-NLS-1$ + } + }, + + /** + * Can be used for:
+ *
    + *
  • {@link Parameter}
  • + *
+ */ + DATE("Date") { + @Override + public Parameter parameterInstance() { + return new DateParameter(); + } + + @Override + public StrolchTimedState> timedStateInstance() { + throw new UnsupportedOperationException(MessageFormat.format( + "TimeStates of type {0} are not supported!", getType())); //$NON-NLS-1$ + } + + @Override + public IValue valueInstance(String valueAsString) { + throw new UnsupportedOperationException(MessageFormat.format( + "Parameters of type {0} are not supported!", getType())); //$NON-NLS-1$ + } + }, + + /** + * Can be used for:
+ *
    + *
  • {@link Parameter}
  • + *
+ */ + DURATION("Duration") { + @Override + public Parameter parameterInstance() { + return new DurationParameter(); + } + + @Override + public StrolchTimedState> timedStateInstance() { + throw new UnsupportedOperationException(MessageFormat.format( + "TimeStates of type {0} are not supported!", getType())); //$NON-NLS-1$ + } + + @Override + public IValue valueInstance(String valueAsString) { + throw new UnsupportedOperationException(MessageFormat.format( + "Parameters of type {0} are not supported!", getType())); //$NON-NLS-1$ + } + }, + + /** + * Can be used for:
+ *
    + *
  • {@link Parameter}
  • + *
+ */ + FLOAT_LIST("FloatList") { + @Override + public Parameter parameterInstance() { + return new FloatListParameter(); + } + + @Override + public StrolchTimedState> timedStateInstance() { + throw new UnsupportedOperationException(MessageFormat.format( + "TimeStates of type {0} are not supported!", getType())); //$NON-NLS-1$ + } + + @Override + public IValue valueInstance(String valueAsString) { + throw new UnsupportedOperationException(MessageFormat.format( + "Parameters of type {0} are not supported!", getType())); //$NON-NLS-1$ + } + }, + + /** + * Can be used for:
+ *
    + *
  • {@link Parameter}
  • + *
+ */ + INTEGER_LIST("IntegerList") { + @Override + public Parameter parameterInstance() { + return new IntegerListParameter(); + } + + @Override + public StrolchTimedState> timedStateInstance() { + throw new UnsupportedOperationException(MessageFormat.format( + "TimeStates of type {0} are not supported!", getType())); //$NON-NLS-1$ + } + + @Override + public IValue valueInstance(String valueAsString) { + throw new UnsupportedOperationException(MessageFormat.format( + "Parameters of type {0} are not supported!", getType())); //$NON-NLS-1$ + } + }, + + /** + * Can be used for:
+ *
    + *
  • {@link Parameter}
  • + *
+ */ + LONG_LIST("LongList") { + @Override + public Parameter parameterInstance() { + return new LongListParameter(); + } + + @Override + public StrolchTimedState> timedStateInstance() { + throw new UnsupportedOperationException(MessageFormat.format( + "TimeStates of type {0} are not supported!", getType())); //$NON-NLS-1$ + } + + @Override + public IValue valueInstance(String valueAsString) { + throw new UnsupportedOperationException(MessageFormat.format( + "Parameters of type {0} are not supported!", getType())); //$NON-NLS-1$ + } + }, + + /** + * Can be used for:
+ *
    + *
  • {@link Parameter}
  • + *
+ */ + STRING_LIST("StringList") { + @Override + public Parameter parameterInstance() { + return new StringListParameter(); + } + + @Override + public StrolchTimedState> timedStateInstance() { + throw new UnsupportedOperationException(MessageFormat.format( + "TimeStates of type {0} are not supported!", getType())); //$NON-NLS-1$ + } + + @Override + public IValue valueInstance(String valueAsString) { + throw new UnsupportedOperationException(MessageFormat.format( + "Values of type {0} are not supported!", getType())); //$NON-NLS-1$ + } + }, + + /** + * Can be used for:
+ *
    + *
  • {@link TimedState}
  • + *
  • {@link IValue}
  • + *
  • {@link IValueChange}
  • + *
+ */ + STRING_SET("StringSet") { + @Override + public Parameter parameterInstance() { + throw new UnsupportedOperationException(MessageFormat.format( + "Parameters of type {0} are not supported!", getType())); //$NON-NLS-1$ + } + + @Override + public StrolchTimedState> timedStateInstance() { + return new StringSetTimedState(); + } + + @Override + public IValue valueInstance(String valueAsString) { + return new StringSetValue(valueAsString); + } + }; + + private String type; + + private StrolchValueType(String type) { + this.type = type; + } + + public String getType() { + return this.type; + } + + public static StrolchValueType parse(String value) { + for (StrolchValueType type : StrolchValueType.values()) { + if (type.type.equals(value)) + return type; + } + throw new IllegalArgumentException("Type " + value + " does not exist!"); + } + + public abstract Parameter parameterInstance(); + + public abstract StrolchTimedState> timedStateInstance(); + + public abstract IValue valueInstance(String valueAsString); +} \ No newline at end of file diff --git a/li.strolch.model/src/main/java/li/strolch/model/Tags.java b/li.strolch.model/src/main/java/li/strolch/model/Tags.java index 92974fa20..d14fd3408 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/Tags.java +++ b/li.strolch.model/src/main/java/li/strolch/model/Tags.java @@ -41,19 +41,17 @@ public class Tags { public static final String FILE = "file"; public static final String BAG = "Bag"; public static final String AUDIT = "Audit"; - + public static final String ACTIVITY = "Activity"; public static final String ACTION = "Action"; public static final String START = "Start"; public static final String END = "End"; public static final String VALUE_CHANGE = "ValueChange"; - public static final String VALUE_CLASS = "Class"; public static final String RESOURCE_ID = "ResourceId"; public static final String RESOURCE_TYPE = "ResourceType"; public static final String STATE_ID = "StateId"; - - public class Audit { + public static class Audit { public static final String ID = Tags.ID; public static final String USERNAME = "Username"; diff --git a/li.strolch.model/src/main/java/li/strolch/model/activity/Action.java b/li.strolch.model/src/main/java/li/strolch/model/activity/Action.java index 4da30b4a6..412fa1c5a 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/activity/Action.java +++ b/li.strolch.model/src/main/java/li/strolch/model/activity/Action.java @@ -18,6 +18,8 @@ package li.strolch.model.activity; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; +import java.util.Iterator; import java.util.List; import li.strolch.model.GroupedParameterizedElement; @@ -27,17 +29,13 @@ import li.strolch.model.Resource; import li.strolch.model.State; import li.strolch.model.StrolchElement; import li.strolch.model.StrolchRootElement; -import li.strolch.model.Tags; +import li.strolch.model.timevalue.IValue; import li.strolch.model.timevalue.IValueChange; -import org.w3c.dom.Document; -import org.w3c.dom.Element; - /** - * An {@link Action} represents a single step within an {@link Activity}, that - * is, one that is not further decomposed within the {@link Activity}. A - * {@link Activity} applies {@link IValueChange} objects at the start and end - * time of the {@link Activity}. + * An {@link Action} represents a single step within an {@link Activity}, that is, one that is not further decomposed + * within the {@link Activity}. A {@link Activity} applies {@link IValueChange} objects at the start and end time of the + * {@link Activity}. * * @author Martin Smock */ @@ -46,13 +44,20 @@ public class Action extends GroupedParameterizedElement implements IActivityElem protected static final long serialVersionUID = 1L; protected Activity parent; - protected String resourceId, resourceType; - protected State state = State.CREATED; + protected String resourceId; + protected String resourceType; + protected State state; - protected final List> changes = new ArrayList<>(); + protected List>> changes; public Action(String id, String name, String type) { super(id, name, type); + this.state = State.CREATED; + } + + private void initChanges() { + if (this.changes == null) + this.changes = new ArrayList<>(); } /** @@ -86,8 +91,7 @@ public class Action extends GroupedParameterizedElement implements IActivityElem } /** - * @return the type of the Resource this Action - * acts on + * @return the type of the Resource this Action acts on */ public String getResourceType() { return this.resourceType; @@ -100,25 +104,41 @@ public class Action extends GroupedParameterizedElement implements IActivityElem this.resourceType = resourceType; } + /** + * Returns true if this {@link Action} contains any {@link IValueChange changes}, false if not + * + * @return true if this {@link Action} contains any {@link IValueChange changes}, false if not + */ + public boolean hasChanges() { + return this.changes != null && !this.changes.isEmpty(); + } + /** * @param add - * IValueChange to be applied to the - * Resource + * IValueChange to be applied to the Resource * * @return true (as specified by {@link Collection#add}) */ - public boolean addChange(IValueChange change) { + public boolean addChange(IValueChange> change) { + initChanges(); return changes.add(change); } /** - * @return the list of IValueChange attached to the - * Action start + * @return the list of IValueChange attached to the Action start */ - public List> getChanges() { + public List>> getChanges() { + if (this.changes == null) + return Collections.emptyList(); return changes; } + public Iterator>> changesIterator() { + if (this.changes == null) + return Collections.>> emptyList().iterator(); + return this.changes.iterator(); + } + @Override public StrolchElement getParent() { return parent; @@ -135,14 +155,16 @@ public class Action extends GroupedParameterizedElement implements IActivityElem } @Override - public StrolchElement getClone() { + public Action getClone() { Action clone = new Action(getId(), getName(), getType()); clone.setDbid(getDbid()); clone.setResourceId(resourceId); clone.setResourceType(resourceType); clone.setState(state); - for (IValueChange change : getChanges()) { - clone.changes.add(change.getClone()); + if (this.changes != null) { + for (IValueChange> change : getChanges()) { + clone.addChange(change.getClone()); + } } return clone; } @@ -185,6 +207,8 @@ public class Action extends GroupedParameterizedElement implements IActivityElem @Override public Long getStart() { Long start = Long.MAX_VALUE; + if (this.changes == null) + return start; for (IValueChange change : changes) { start = Math.min(start, change.getTime()); } @@ -194,23 +218,11 @@ public class Action extends GroupedParameterizedElement implements IActivityElem @Override public Long getEnd() { Long end = 0L; + if (this.changes == null) + return end; for (IValueChange change : changes) { end = Math.max(end, change.getTime()); } return end; } - - @Override - public Element toDom(Document doc) { - Element element = doc.createElement(Tags.ACTION); - fillElement(element); - element.setAttribute(Tags.STATE, this.state.toString()); - element.setAttribute(Tags.RESOURCE_ID, this.resourceId); - element.setAttribute(Tags.RESOURCE_TYPE, this.resourceType); - for (IValueChange change : changes) { - element.appendChild(change.toDom(doc)); - } - return element; - } - } diff --git a/li.strolch.model/src/main/java/li/strolch/model/activity/Activity.java b/li.strolch.model/src/main/java/li/strolch/model/activity/Activity.java index f550814d8..7b24948d3 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/activity/Activity.java +++ b/li.strolch.model/src/main/java/li/strolch/model/activity/Activity.java @@ -15,6 +15,7 @@ */ package li.strolch.model.activity; +import java.util.Collections; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; @@ -30,12 +31,9 @@ import li.strolch.model.StrolchRootElement; import li.strolch.model.Tags; import li.strolch.model.visitor.StrolchRootElementVisitor; -import org.w3c.dom.Document; -import org.w3c.dom.Element; - /** - * Parameterized object grouping a collection of {@link Activity} and - * {@link Action} objects defining the process to be scheduled + * Parameterized object grouping a collection of {@link Activity} and {@link Action} objects defining the process to be + * scheduled * * @author Martin Smock */ @@ -45,27 +43,37 @@ public class Activity extends GroupedParameterizedElement implements IActivityEl protected Activity parent; + protected Map elements; + public Activity(String id, String name, String type) { super(id, name, type); } - // use a LinkedHashMap since we will iterate elements in the order added and - // lookup elements by ID - protected Map elements = new LinkedHashMap(); + private void initElements() { + if (this.elements == null) { + // use a LinkedHashMap since we will iterate elements in the order added and lookup elements by ID + elements = new LinkedHashMap(); + } + } + + public boolean hasElements() { + return this.elements != null && !this.elements.isEmpty(); + } /** - * add an activity element to the LinkedHashMap of - * IActivityElements + * add an activity element to the LinkedHashMap of IActivityElements * * @param activityElement * @return the element added */ public IActivityElement addElement(IActivityElement activityElement) { + initElements(); String id = activityElement.getId(); if (id == null) throw new StrolchException("Cannot add IActivityElement without id."); else if (elements.containsKey(id)) - throw new StrolchException("Activiy " + getLocator() + " already contains an activity element with id = " + id); + throw new StrolchException("Activiy " + getLocator() + " already contains an activity element with id = " + + id); else { activityElement.setParent(this); return elements.put(activityElement.getId(), activityElement); @@ -80,27 +88,33 @@ public class Activity extends GroupedParameterizedElement implements IActivityEl * @return IActivityElement */ public IActivityElement getElement(String id) { + if (this.elements == null) + return null; return elements.get(id); } /** - * @return get the LinkedHashMap of - * IActivityElements + * @return get the LinkedHashMap of IActivityElements */ public Map getElements() { + if (this.elements == null) + return Collections.emptyMap(); return elements; } /** - * @return the iterator for entries, which include the id as key and the - * {@link IActivityElement} as value + * @return the iterator for entries, which include the id as key and the {@link IActivityElement} as value */ public Iterator> elementIterator() { + if (this.elements == null) + return Collections. emptyMap().entrySet().iterator(); return elements.entrySet().iterator(); } public Long getStart() { Long start = Long.MAX_VALUE; + if (this.elements == null) + return start; Iterator> elementIterator = elementIterator(); while (elementIterator.hasNext()) { IActivityElement action = elementIterator.next().getValue(); @@ -111,6 +125,8 @@ public class Activity extends GroupedParameterizedElement implements IActivityEl public Long getEnd() { Long end = 0L; + if (this.elements == null) + return end; Iterator> elementIterator = elementIterator(); while (elementIterator.hasNext()) { IActivityElement action = elementIterator.next().getValue(); @@ -121,6 +137,8 @@ public class Activity extends GroupedParameterizedElement implements IActivityEl public State getState() { State state = State.PLANNED; + if (this.elements == null) + return state; Iterator> elementIterator = elementIterator(); while (elementIterator.hasNext()) { IActivityElement child = elementIterator.next().getValue(); @@ -144,18 +162,6 @@ public class Activity extends GroupedParameterizedElement implements IActivityEl locatorBuilder.append(Tags.ACTIVITY).append(getType()).append(getId()); } - @Override - public Element toDom(Document doc) { - Element element = doc.createElement(Tags.ACTIVITY); - fillElement(element); - Iterator> elementIterator = elementIterator(); - while (elementIterator.hasNext()) { - IActivityElement activityElement = elementIterator.next().getValue(); - element.appendChild(activityElement.toDom(doc)); - } - return element; - } - @Override public StrolchElement getParent() { return parent; @@ -172,12 +178,12 @@ public class Activity extends GroupedParameterizedElement implements IActivityEl } @Override - public StrolchElement getClone() { + public Activity getClone() { Activity clone = new Activity(id, name, type); - Iterator> elementIterator = elementIterator(); - while (elementIterator.hasNext()) { - Entry next = elementIterator.next(); - clone.elements.put(next.getKey(), (IActivityElement) next.getValue().getClone()); + if (this.elements == null) + return clone; + for (IActivityElement element : this.elements.values()) { + clone.addElement(element.getClone()); } return clone; } @@ -210,5 +216,4 @@ public class Activity extends GroupedParameterizedElement implements IActivityEl public void setParent(Activity activity) { this.parent = activity; } - } diff --git a/li.strolch.model/src/main/java/li/strolch/model/activity/IActivityElement.java b/li.strolch.model/src/main/java/li/strolch/model/activity/IActivityElement.java index 226d52763..0bee8af39 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/activity/IActivityElement.java +++ b/li.strolch.model/src/main/java/li/strolch/model/activity/IActivityElement.java @@ -22,16 +22,16 @@ import li.strolch.model.StrolchElement; * Marker for all child elements of {@link Activity} objects * * @author Martin Smock - * */ public interface IActivityElement extends StrolchElement { - Long getStart(); + public Long getStart(); - Long getEnd(); + public Long getEnd(); - State getState(); - - void setParent(Activity activity); + public State getState(); + public void setParent(Activity activity); + + public IActivityElement getClone(); } diff --git a/li.strolch.model/src/main/java/li/strolch/model/parameter/AbstractParameter.java b/li.strolch.model/src/main/java/li/strolch/model/parameter/AbstractParameter.java index 04110f754..b5e2c2506 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/parameter/AbstractParameter.java +++ b/li.strolch.model/src/main/java/li/strolch/model/parameter/AbstractParameter.java @@ -26,12 +26,7 @@ import li.strolch.model.Locator; import li.strolch.model.Locator.LocatorBuilder; import li.strolch.model.ParameterizedElement; import li.strolch.model.StrolchRootElement; -import li.strolch.model.Tags; import li.strolch.model.visitor.ParameterVisitor; - -import org.w3c.dom.Document; -import org.w3c.dom.Element; - import ch.eitchnet.utils.helper.StringHelper; /** @@ -135,74 +130,6 @@ public abstract class AbstractParameter extends AbstractStrolchElement implem return false; } - @Override - public Element toDom(Document doc) { - Element element = doc.createElement(Tags.PARAMETER); - fillElement(element); - - element.setAttribute(Tags.VALUE, getValueAsString()); - - if (!this.interpretation.equals(INTERPRETATION_NONE)) { - element.setAttribute(Tags.INTERPRETATION, this.interpretation); - } - if (!this.uom.equals(UOM_NONE)) { - element.setAttribute(Tags.UOM, this.uom); - } - if (this.hidden) { - element.setAttribute(Tags.HIDDEN, Boolean.toString(this.hidden)); - } - if (this.index != 0) { - element.setAttribute(Tags.INDEX, Integer.toString(this.index)); - } - - return element; - } - - @Override - public void fromDom(Element element) { - - super.fromDom(element); - - String typeS = element.getAttribute(Tags.TYPE); - if (StringHelper.isEmpty(typeS)) { - String msg = "Type must be set on element with id {0}"; //$NON-NLS-1$ - msg = MessageFormat.format(msg, this.id); - throw new StrolchException(msg); - } else if (!typeS.equals(getType())) { - String msg = "{0} must have type {1}, not: {2}"; //$NON-NLS-1$ - msg = MessageFormat.format(msg, getClass().getSimpleName(), getType(), typeS); - throw new StrolchException(msg); - } - - String interpretation = element.getAttribute(Tags.INTERPRETATION); - String hidden = element.getAttribute(Tags.HIDDEN); - String uom = element.getAttribute(Tags.UOM); - String index = element.getAttribute(Tags.INDEX); - - setInterpretation(interpretation); - setUom(uom); - - if (StringHelper.isEmpty(index)) { - this.index = 0; - } else { - this.index = Integer.valueOf(index); - } - - if (StringHelper.isEmpty(hidden)) { - setHidden(false); - } else { - if (hidden.equalsIgnoreCase(Boolean.TRUE.toString())) { - setHidden(true); - } else if (hidden.equalsIgnoreCase(Boolean.FALSE.toString())) { - setHidden(false); - } else { - String msg = "Boolean string must be either {0} or {1}"; //$NON-NLS-1$ - msg = MessageFormat.format(msg, Boolean.TRUE.toString(), Boolean.FALSE.toString()); - throw new StrolchException(msg); - } - } - } - @Override protected void fillLocator(LocatorBuilder lb) { lb.append(this.id); diff --git a/li.strolch.model/src/main/java/li/strolch/model/parameter/BooleanParameter.java b/li.strolch.model/src/main/java/li/strolch/model/parameter/BooleanParameter.java index 56c1aba28..8f1ada4ab 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/parameter/BooleanParameter.java +++ b/li.strolch.model/src/main/java/li/strolch/model/parameter/BooleanParameter.java @@ -15,14 +15,8 @@ */ package li.strolch.model.parameter; -import java.text.MessageFormat; - -import li.strolch.exception.StrolchException; -import li.strolch.model.Tags; +import li.strolch.model.StrolchValueType; import li.strolch.model.visitor.ParameterVisitor; - -import org.w3c.dom.Element; - import ch.eitchnet.utils.helper.StringHelper; /** @@ -30,7 +24,6 @@ import ch.eitchnet.utils.helper.StringHelper; */ public class BooleanParameter extends AbstractParameter { - public static final String TYPE = "Boolean"; //$NON-NLS-1$ private static final long serialVersionUID = 0L; private Boolean value = Boolean.FALSE; @@ -54,23 +47,6 @@ public class BooleanParameter extends AbstractParameter { setValue(value); } - /** - * DOM Constructor - * - * @param element - */ - public BooleanParameter(Element element) { - super.fromDom(element); - - String valueS = element.getAttribute(Tags.VALUE); - if (StringHelper.isEmpty(valueS)) { - String msg = MessageFormat.format("No value defined for {0}", this.id); //$NON-NLS-1$ - throw new StrolchException(msg); - } - - setValue(parseFromString(valueS)); - } - @Override public String getValueAsString() { return this.value.toString(); @@ -87,9 +63,14 @@ public class BooleanParameter extends AbstractParameter { this.value = value; } + @Override + public void setValueFromString(String valueAsString) { + setValue(parseFromString(valueAsString)); + } + @Override public String getType() { - return BooleanParameter.TYPE; + return StrolchValueType.BOOLEAN.getType(); } @Override diff --git a/li.strolch.model/src/main/java/li/strolch/model/parameter/DateParameter.java b/li.strolch.model/src/main/java/li/strolch/model/parameter/DateParameter.java index ffffb8462..461319b6f 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/parameter/DateParameter.java +++ b/li.strolch.model/src/main/java/li/strolch/model/parameter/DateParameter.java @@ -15,16 +15,10 @@ */ package li.strolch.model.parameter; -import java.text.MessageFormat; import java.util.Date; -import li.strolch.exception.StrolchException; -import li.strolch.model.Tags; +import li.strolch.model.StrolchValueType; import li.strolch.model.visitor.ParameterVisitor; - -import org.w3c.dom.Element; - -import ch.eitchnet.utils.helper.StringHelper; import ch.eitchnet.utils.iso8601.ISO8601FormatFactory; /** @@ -32,7 +26,6 @@ import ch.eitchnet.utils.iso8601.ISO8601FormatFactory; */ public class DateParameter extends AbstractParameter { - public static final String TYPE = "Date"; //$NON-NLS-1$ private static final long serialVersionUID = 0L; private Date value; @@ -56,23 +49,6 @@ public class DateParameter extends AbstractParameter { setValue(value); } - /** - * DOM Constructor - * - * @param element - */ - public DateParameter(Element element) { - super.fromDom(element); - - String valueS = element.getAttribute(Tags.VALUE); - if (StringHelper.isEmpty(valueS)) { - String msg = MessageFormat.format("No value defined for {0}", this.id); //$NON-NLS-1$ - throw new StrolchException(msg); - } - - setValue(parseFromString(valueS)); - } - @Override public String getValueAsString() { return ISO8601FormatFactory.getInstance().formatDate(this.value); @@ -89,9 +65,14 @@ public class DateParameter extends AbstractParameter { this.value = value; } + @Override + public void setValueFromString(String valueAsString) { + setValue(parseFromString(valueAsString)); + } + @Override public String getType() { - return DateParameter.TYPE; + return StrolchValueType.DATE.getType(); } @Override diff --git a/li.strolch.model/src/main/java/li/strolch/model/parameter/DurationParameter.java b/li.strolch.model/src/main/java/li/strolch/model/parameter/DurationParameter.java index 626ce8635..2916d7aff 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/parameter/DurationParameter.java +++ b/li.strolch.model/src/main/java/li/strolch/model/parameter/DurationParameter.java @@ -15,15 +15,8 @@ */ package li.strolch.model.parameter; -import java.text.MessageFormat; - -import li.strolch.exception.StrolchException; -import li.strolch.model.Tags; +import li.strolch.model.StrolchValueType; import li.strolch.model.visitor.ParameterVisitor; - -import org.w3c.dom.Element; - -import ch.eitchnet.utils.helper.StringHelper; import ch.eitchnet.utils.iso8601.ISO8601FormatFactory; /** @@ -31,7 +24,6 @@ import ch.eitchnet.utils.iso8601.ISO8601FormatFactory; */ public class DurationParameter extends AbstractParameter { - public static final String TYPE = "Duration"; //$NON-NLS-1$ private static final long serialVersionUID = 0L; private Long value; @@ -55,23 +47,6 @@ public class DurationParameter extends AbstractParameter { setValue(value); } - /** - * DOM Constructor - * - * @param element - */ - public DurationParameter(Element element) { - super.fromDom(element); - - String valueS = element.getAttribute(Tags.VALUE); - if (StringHelper.isEmpty(valueS)) { - String msg = MessageFormat.format("No value defined for {0}", this.id); //$NON-NLS-1$ - throw new StrolchException(msg); - } - - setValue(parseFromString(valueS)); - } - @Override public String getValueAsString() { return ISO8601FormatFactory.getInstance().formatDuration(this.value); @@ -88,9 +63,14 @@ public class DurationParameter extends AbstractParameter { this.value = value; } + @Override + public void setValueFromString(String valueAsString) { + setValue(parseFromString(valueAsString)); + } + @Override public String getType() { - return DurationParameter.TYPE; + return StrolchValueType.DURATION.getType(); } @Override diff --git a/li.strolch.model/src/main/java/li/strolch/model/parameter/FloatListParameter.java b/li.strolch.model/src/main/java/li/strolch/model/parameter/FloatListParameter.java index 83240a518..5bee81921 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/parameter/FloatListParameter.java +++ b/li.strolch.model/src/main/java/li/strolch/model/parameter/FloatListParameter.java @@ -15,18 +15,13 @@ */ package li.strolch.model.parameter; -import java.text.MessageFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; -import li.strolch.exception.StrolchException; -import li.strolch.model.Tags; +import li.strolch.model.StrolchValueType; import li.strolch.model.visitor.ParameterVisitor; - -import org.w3c.dom.Element; - import ch.eitchnet.utils.helper.StringHelper; /** @@ -34,7 +29,6 @@ import ch.eitchnet.utils.helper.StringHelper; */ public class FloatListParameter extends AbstractParameter> implements ListParameter { - public static final String TYPE = "FloatList"; //$NON-NLS-1$ private static final long serialVersionUID = 1L; protected List value; @@ -59,23 +53,6 @@ public class FloatListParameter extends AbstractParameter> implemen setValue(value); } - /** - * DOM Constructor - * - * @param element - */ - public FloatListParameter(Element element) { - super.fromDom(element); - - String valueS = element.getAttribute(Tags.VALUE); - if (StringHelper.isEmpty(valueS)) { - String msg = MessageFormat.format("No value defined for {0}", this.id); //$NON-NLS-1$ - throw new StrolchException(msg); - } - - setValue(parseFromString(valueS)); - } - @Override public String getValueAsString() { if (this.value.isEmpty()) { @@ -111,6 +88,11 @@ public class FloatListParameter extends AbstractParameter> implemen this.value.addAll(value); } + @Override + public void setValueFromString(String valueAsString) { + setValue(parseFromString(valueAsString)); + } + @Override public void addValue(Double value) { this.value.add(value); @@ -133,7 +115,7 @@ public class FloatListParameter extends AbstractParameter> implemen @Override public String getType() { - return TYPE; + return StrolchValueType.FLOAT_LIST.getType(); } @Override diff --git a/li.strolch.model/src/main/java/li/strolch/model/parameter/FloatParameter.java b/li.strolch.model/src/main/java/li/strolch/model/parameter/FloatParameter.java index 565ce2738..f9c408ea1 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/parameter/FloatParameter.java +++ b/li.strolch.model/src/main/java/li/strolch/model/parameter/FloatParameter.java @@ -15,23 +15,15 @@ */ package li.strolch.model.parameter; -import java.text.MessageFormat; - -import li.strolch.exception.StrolchException; -import li.strolch.model.Tags; +import li.strolch.model.StrolchValueType; import li.strolch.model.visitor.ParameterVisitor; -import org.w3c.dom.Element; - -import ch.eitchnet.utils.helper.StringHelper; - /** * @author Robert von Burg * */ public class FloatParameter extends AbstractParameter { - public static final String TYPE = "Float"; //$NON-NLS-1$ private static final long serialVersionUID = 0L; private Double value = Double.MAX_VALUE; @@ -56,23 +48,6 @@ public class FloatParameter extends AbstractParameter { setValue(value); } - /** - * DOM Constructor - * - * @param element - */ - public FloatParameter(Element element) { - super.fromDom(element); - - String valueS = element.getAttribute(Tags.VALUE); - if (StringHelper.isEmpty(valueS)) { - String msg = MessageFormat.format("No value defined for {0}", this.id); //$NON-NLS-1$ - throw new StrolchException(msg); - } - - setValue(parseFromString(valueS)); - } - @Override public String getValueAsString() { return Double.toString(this.value); @@ -89,9 +64,14 @@ public class FloatParameter extends AbstractParameter { this.value = value; } + @Override + public void setValueFromString(String valueAsString) { + setValue(parseFromString(valueAsString)); + } + @Override public String getType() { - return FloatParameter.TYPE; + return StrolchValueType.FLOAT.getType(); } @Override diff --git a/li.strolch.model/src/main/java/li/strolch/model/parameter/IntegerListParameter.java b/li.strolch.model/src/main/java/li/strolch/model/parameter/IntegerListParameter.java index 7ee9dfb78..47fc8f55b 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/parameter/IntegerListParameter.java +++ b/li.strolch.model/src/main/java/li/strolch/model/parameter/IntegerListParameter.java @@ -15,18 +15,13 @@ */ package li.strolch.model.parameter; -import java.text.MessageFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; -import li.strolch.exception.StrolchException; -import li.strolch.model.Tags; +import li.strolch.model.StrolchValueType; import li.strolch.model.visitor.ParameterVisitor; - -import org.w3c.dom.Element; - import ch.eitchnet.utils.helper.StringHelper; /** @@ -34,7 +29,6 @@ import ch.eitchnet.utils.helper.StringHelper; */ public class IntegerListParameter extends AbstractParameter> implements ListParameter { - public static final String TYPE = "IntegerList"; //$NON-NLS-1$ private static final long serialVersionUID = 1L; protected List value; @@ -59,23 +53,6 @@ public class IntegerListParameter extends AbstractParameter> imple setValue(value); } - /** - * DOM Constructor - * - * @param element - */ - public IntegerListParameter(Element element) { - super.fromDom(element); - - String valueS = element.getAttribute(Tags.VALUE); - if (StringHelper.isEmpty(valueS)) { - String msg = MessageFormat.format("No value defined for {0}", this.id); //$NON-NLS-1$ - throw new StrolchException(msg); - } - - setValue(parseFromString(valueS)); - } - @Override public String getValueAsString() { if (this.value.isEmpty()) { @@ -111,6 +88,11 @@ public class IntegerListParameter extends AbstractParameter> imple this.value.addAll(value); } + @Override + public void setValueFromString(String valueAsString) { + setValue(parseFromString(valueAsString)); + } + @Override public void addValue(Integer value) { this.value.add(value); @@ -133,7 +115,7 @@ public class IntegerListParameter extends AbstractParameter> imple @Override public String getType() { - return TYPE; + return StrolchValueType.INTEGER_LIST.getType(); } @Override diff --git a/li.strolch.model/src/main/java/li/strolch/model/parameter/IntegerParameter.java b/li.strolch.model/src/main/java/li/strolch/model/parameter/IntegerParameter.java index cf29997d4..333a2aae4 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/parameter/IntegerParameter.java +++ b/li.strolch.model/src/main/java/li/strolch/model/parameter/IntegerParameter.java @@ -15,23 +15,15 @@ */ package li.strolch.model.parameter; -import java.text.MessageFormat; - -import li.strolch.exception.StrolchException; -import li.strolch.model.Tags; +import li.strolch.model.StrolchValueType; import li.strolch.model.visitor.ParameterVisitor; -import org.w3c.dom.Element; - -import ch.eitchnet.utils.helper.StringHelper; - /** * @author Robert von Burg * */ public class IntegerParameter extends AbstractParameter { - public static final String TYPE = "Integer"; //$NON-NLS-1$ private static final long serialVersionUID = 0L; private Integer value = Integer.MAX_VALUE; @@ -55,26 +47,9 @@ public class IntegerParameter extends AbstractParameter { setValue(value); } - /** - * DOM Constructor - * - * @param element - */ - public IntegerParameter(Element element) { - super.fromDom(element); - - String valueS = element.getAttribute(Tags.VALUE); - if (StringHelper.isEmpty(valueS)) { - String msg = MessageFormat.format("No value defined for {0}", this.id); //$NON-NLS-1$ - throw new StrolchException(msg); - } - - setValue(parseFromString(valueS)); - } - @Override public String getType() { - return IntegerParameter.TYPE; + return StrolchValueType.INTEGER.getType(); } @Override @@ -93,6 +68,11 @@ public class IntegerParameter extends AbstractParameter { this.value = value; } + @Override + public void setValueFromString(String valueAsString) { + setValue(parseFromString(valueAsString)); + } + @Override public IntegerParameter getClone() { IntegerParameter clone = new IntegerParameter(); diff --git a/li.strolch.model/src/main/java/li/strolch/model/parameter/LongListParameter.java b/li.strolch.model/src/main/java/li/strolch/model/parameter/LongListParameter.java index 941b7ccf6..a5be57b2c 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/parameter/LongListParameter.java +++ b/li.strolch.model/src/main/java/li/strolch/model/parameter/LongListParameter.java @@ -15,18 +15,13 @@ */ package li.strolch.model.parameter; -import java.text.MessageFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; -import li.strolch.exception.StrolchException; -import li.strolch.model.Tags; +import li.strolch.model.StrolchValueType; import li.strolch.model.visitor.ParameterVisitor; - -import org.w3c.dom.Element; - import ch.eitchnet.utils.helper.StringHelper; /** @@ -34,7 +29,6 @@ import ch.eitchnet.utils.helper.StringHelper; */ public class LongListParameter extends AbstractParameter> implements ListParameter { - public static final String TYPE = "LongList"; //$NON-NLS-1$ private static final long serialVersionUID = 1L; protected List value; @@ -59,23 +53,6 @@ public class LongListParameter extends AbstractParameter> implements setValue(value); } - /** - * DOM Constructor - * - * @param element - */ - public LongListParameter(Element element) { - super.fromDom(element); - - String valueS = element.getAttribute(Tags.VALUE); - if (StringHelper.isEmpty(valueS)) { - String msg = MessageFormat.format("No value defined for {0}", this.id); //$NON-NLS-1$ - throw new StrolchException(msg); - } - - setValue(parseFromString(valueS)); - } - @Override public String getValueAsString() { if (this.value.isEmpty()) { @@ -111,6 +88,11 @@ public class LongListParameter extends AbstractParameter> implements this.value.addAll(value); } + @Override + public void setValueFromString(String valueAsString) { + setValue(parseFromString(valueAsString)); + } + @Override public void addValue(Long value) { this.value.add(value); @@ -133,7 +115,7 @@ public class LongListParameter extends AbstractParameter> implements @Override public String getType() { - return TYPE; + return StrolchValueType.LONG_LIST.getType(); } @Override diff --git a/li.strolch.model/src/main/java/li/strolch/model/parameter/LongParameter.java b/li.strolch.model/src/main/java/li/strolch/model/parameter/LongParameter.java index 0e47f51a9..443daf743 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/parameter/LongParameter.java +++ b/li.strolch.model/src/main/java/li/strolch/model/parameter/LongParameter.java @@ -15,23 +15,15 @@ */ package li.strolch.model.parameter; -import java.text.MessageFormat; - -import li.strolch.exception.StrolchException; -import li.strolch.model.Tags; +import li.strolch.model.StrolchValueType; import li.strolch.model.visitor.ParameterVisitor; -import org.w3c.dom.Element; - -import ch.eitchnet.utils.helper.StringHelper; - /** * @author Robert von Burg * */ public class LongParameter extends AbstractParameter { - public static final String TYPE = "Long"; //$NON-NLS-1$ private static final long serialVersionUID = 0L; protected Long value; @@ -55,23 +47,6 @@ public class LongParameter extends AbstractParameter { setValue(value); } - /** - * DOM Constructor - * - * @param element - */ - public LongParameter(Element element) { - super.fromDom(element); - - String valueS = element.getAttribute(Tags.VALUE); - if (StringHelper.isEmpty(valueS)) { - String msg = MessageFormat.format("No value defined for {0}", this.id); //$NON-NLS-1$ - throw new StrolchException(msg); - } - - setValue(parseFromString(valueS)); - } - @Override public String getValueAsString() { return this.value.toString(); @@ -88,9 +63,14 @@ public class LongParameter extends AbstractParameter { this.value = value; } + @Override + public void setValueFromString(String valueAsString) { + setValue(parseFromString(valueAsString)); + } + @Override public String getType() { - return LongParameter.TYPE; + return StrolchValueType.LONG.getType(); } @Override diff --git a/li.strolch.model/src/main/java/li/strolch/model/parameter/Parameter.java b/li.strolch.model/src/main/java/li/strolch/model/parameter/Parameter.java index f55b99594..c6ecde5dd 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/parameter/Parameter.java +++ b/li.strolch.model/src/main/java/li/strolch/model/parameter/Parameter.java @@ -26,12 +26,20 @@ import li.strolch.model.visitor.ParameterVisitor; public interface Parameter extends StrolchElement { /** - * the value of the parameter as string + * Returns the value of the parameter as string * - * @return String + * @return the value as string */ public String getValueAsString(); + /** + * Set the value of the parameter from a string + * + * @param valueAsString + * the string from which to set the value + */ + public void setValueFromString(String valueAsString); + /** * the value of the parameter * diff --git a/li.strolch.model/src/main/java/li/strolch/model/parameter/StringListParameter.java b/li.strolch.model/src/main/java/li/strolch/model/parameter/StringListParameter.java index b19c008cb..323506efb 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/parameter/StringListParameter.java +++ b/li.strolch.model/src/main/java/li/strolch/model/parameter/StringListParameter.java @@ -15,18 +15,13 @@ */ package li.strolch.model.parameter; -import java.text.MessageFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; -import li.strolch.exception.StrolchException; -import li.strolch.model.Tags; +import li.strolch.model.StrolchValueType; import li.strolch.model.visitor.ParameterVisitor; - -import org.w3c.dom.Element; - import ch.eitchnet.utils.helper.StringHelper; /** @@ -34,7 +29,6 @@ import ch.eitchnet.utils.helper.StringHelper; */ public class StringListParameter extends AbstractParameter> implements ListParameter { - public static final String TYPE = "StringList"; //$NON-NLS-1$ private static final long serialVersionUID = 1L; protected List value; @@ -59,23 +53,6 @@ public class StringListParameter extends AbstractParameter> impleme setValue(value); } - /** - * DOM Constructor - * - * @param element - */ - public StringListParameter(Element element) { - super.fromDom(element); - - String valueS = element.getAttribute(Tags.VALUE); - if (StringHelper.isEmpty(valueS)) { - String msg = MessageFormat.format("No value defined for {0}", this.id); //$NON-NLS-1$ - throw new StrolchException(msg); - } - - setValue(parseFromString(valueS)); - } - @Override public String getValueAsString() { if (this.value.isEmpty()) { @@ -111,6 +88,11 @@ public class StringListParameter extends AbstractParameter> impleme this.value.addAll(value); } + @Override + public void setValueFromString(String valueAsString) { + setValue(parseFromString(valueAsString)); + } + @Override public void addValue(String value) { this.value.add(value); @@ -133,7 +115,7 @@ public class StringListParameter extends AbstractParameter> impleme @Override public String getType() { - return TYPE; + return StrolchValueType.STRING_LIST.getType(); } @Override diff --git a/li.strolch.model/src/main/java/li/strolch/model/parameter/StringParameter.java b/li.strolch.model/src/main/java/li/strolch/model/parameter/StringParameter.java index b7c65b897..81c3e7882 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/parameter/StringParameter.java +++ b/li.strolch.model/src/main/java/li/strolch/model/parameter/StringParameter.java @@ -15,16 +15,9 @@ */ package li.strolch.model.parameter; -import java.text.MessageFormat; - -import li.strolch.exception.StrolchException; -import li.strolch.model.Tags; +import li.strolch.model.StrolchValueType; import li.strolch.model.visitor.ParameterVisitor; -import org.w3c.dom.Element; - -import ch.eitchnet.utils.helper.StringHelper; - /** * @author Robert von Burg * @@ -32,7 +25,6 @@ import ch.eitchnet.utils.helper.StringHelper; public class StringParameter extends AbstractParameter { public static final String UNDEFINED_VALUE = "-"; //$NON-NLS-1$ - public static final String TYPE = "String"; //$NON-NLS-1$ private static final long serialVersionUID = 0L; private String value = UNDEFINED_VALUE; @@ -57,26 +49,9 @@ public class StringParameter extends AbstractParameter { setValue(value); } - /** - * DOM Constructor - * - * @param element - */ - public StringParameter(Element element) { - super.fromDom(element); - - String valueS = element.getAttribute(Tags.VALUE); - if (StringHelper.isEmpty(valueS)) { - String msg = MessageFormat.format("No value defined for {0}", this.id); //$NON-NLS-1$ - throw new StrolchException(msg); - } - - setValue(valueS); - } - @Override public String getType() { - return StringParameter.TYPE; + return StrolchValueType.STRING.getType(); } @Override @@ -95,6 +70,11 @@ public class StringParameter extends AbstractParameter { this.value = value; } + @Override + public void setValueFromString(String valueAsString) { + setValue(valueAsString); + } + @Override public StringParameter getClone() { StringParameter clone = new StringParameter(); diff --git a/li.strolch.model/src/main/java/li/strolch/model/timedstate/AbstractStrolchTimedState.java b/li.strolch.model/src/main/java/li/strolch/model/timedstate/AbstractStrolchTimedState.java index 4dbd1f1eb..ca27e5365 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/timedstate/AbstractStrolchTimedState.java +++ b/li.strolch.model/src/main/java/li/strolch/model/timedstate/AbstractStrolchTimedState.java @@ -17,10 +17,6 @@ package li.strolch.model.timedstate; import static li.strolch.model.StrolchModelConstants.INTERPRETATION_NONE; import static li.strolch.model.StrolchModelConstants.UOM_NONE; - -import java.text.MessageFormat; - -import li.strolch.exception.StrolchException; import li.strolch.model.AbstractStrolchElement; import li.strolch.model.Locator; import li.strolch.model.Locator.LocatorBuilder; @@ -33,10 +29,6 @@ import li.strolch.model.timevalue.ITimeVariable; import li.strolch.model.timevalue.IValue; import li.strolch.model.timevalue.IValueChange; import li.strolch.model.visitor.TimedStateVisitor; - -import org.w3c.dom.Document; -import org.w3c.dom.Element; - import ch.eitchnet.utils.helper.StringHelper; /** @@ -160,72 +152,6 @@ public abstract class AbstractStrolchTimedState extends Abstra return false; } - @Override - public Element toDom(Document doc) { - Element element = doc.createElement(Tags.PARAMETER); - fillElement(element); - - if (!this.interpretation.equals(INTERPRETATION_NONE)) { - element.setAttribute(Tags.INTERPRETATION, this.interpretation); - } - if (!this.uom.equals(UOM_NONE)) { - element.setAttribute(Tags.UOM, this.uom); - } - if (this.hidden) { - element.setAttribute(Tags.HIDDEN, Boolean.toString(this.hidden)); - } - if (this.index != 0) { - element.setAttribute(Tags.INDEX, Integer.toString(this.index)); - } - - return element; - } - - @Override - public void fromDom(Element element) { - - super.fromDom(element); - - String typeS = element.getAttribute(Tags.TYPE); - if (StringHelper.isEmpty(typeS)) { - String msg = "Type must be set on element with id {0}"; //$NON-NLS-1$ - msg = MessageFormat.format(msg, this.id); - throw new StrolchException(msg); - } else if (!typeS.equals(getType())) { - String msg = "{0} must have type {1}, not: {2}"; //$NON-NLS-1$ - msg = MessageFormat.format(msg, getClass().getSimpleName(), getType(), typeS); - throw new StrolchException(msg); - } - - String interpretation = element.getAttribute(Tags.INTERPRETATION); - String hidden = element.getAttribute(Tags.HIDDEN); - String uom = element.getAttribute(Tags.UOM); - String index = element.getAttribute(Tags.INDEX); - - setInterpretation(interpretation); - setUom(uom); - - if (StringHelper.isEmpty(index)) { - this.index = 0; - } else { - this.index = Integer.valueOf(index); - } - - if (StringHelper.isEmpty(hidden)) { - setHidden(false); - } else { - if (hidden.equalsIgnoreCase(Boolean.TRUE.toString())) { - setHidden(true); - } else if (hidden.equalsIgnoreCase(Boolean.FALSE.toString())) { - setHidden(false); - } else { - String msg = "Boolean string must be either {0} or {1}"; //$NON-NLS-1$ - msg = MessageFormat.format(msg, Boolean.TRUE.toString(), Boolean.FALSE.toString()); - throw new StrolchException(msg); - } - } - } - @Override protected void fillLocator(LocatorBuilder lb) { lb.append(Tags.STATE); diff --git a/li.strolch.model/src/main/java/li/strolch/model/timedstate/BooleanTimedState.java b/li.strolch.model/src/main/java/li/strolch/model/timedstate/BooleanTimedState.java index 773421ff8..14c5aede4 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/timedstate/BooleanTimedState.java +++ b/li.strolch.model/src/main/java/li/strolch/model/timedstate/BooleanTimedState.java @@ -15,19 +15,9 @@ */ package li.strolch.model.timedstate; -import java.util.Date; -import java.util.SortedSet; - -import li.strolch.model.Tags; -import li.strolch.model.timevalue.ITimeValue; +import li.strolch.model.StrolchValueType; import li.strolch.model.timevalue.impl.BooleanValue; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.NodeList; - -import ch.eitchnet.utils.iso8601.ISO8601FormatFactory; - /** * @author Robert von Burg */ @@ -35,8 +25,6 @@ public class BooleanTimedState extends AbstractStrolchTimedState { private static final long serialVersionUID = 1L; - public static final String TYPE = "BooleanState"; - public BooleanTimedState() { super(); } @@ -45,45 +33,14 @@ public class BooleanTimedState extends AbstractStrolchTimedState { super(id, name); } - public BooleanTimedState(Element element) { - super.fromDom(element); - - this.state = new TimedState<>(); - - NodeList timeValueElems = element.getElementsByTagName(Tags.VALUE); - for (int i = 0; i < timeValueElems.getLength(); i++) { - Element timeValueElem = (Element) timeValueElems.item(i); - String timeS = timeValueElem.getAttribute(Tags.TIME); - Date date = ISO8601FormatFactory.getInstance().parseDate(timeS); - long time = date.getTime(); - - Boolean value = Boolean.valueOf(timeValueElem.getAttribute(Tags.VALUE)); - BooleanValue booleanValue = new BooleanValue(value); - this.state.getTimeEvolution().setValueAt(time, booleanValue); - } - } - @Override - public Element toDom(Document doc) { - - Element stateElement = doc.createElement(Tags.TIMED_STATE); - super.fillElement(stateElement); - SortedSet> values = this.state.getTimeEvolution().getValues(); - for (ITimeValue timeValue : values) { - Long time = timeValue.getTime(); - BooleanValue value = timeValue.getValue(); - Element valueElem = doc.createElement(Tags.VALUE); - valueElem.setAttribute(Tags.TIME, ISO8601FormatFactory.getInstance().formatDate(time)); - valueElem.setAttribute(Tags.VALUE, value.getValue().toString()); - stateElement.appendChild(valueElem); - } - - return stateElement; + public void setStateFromStringAt(Long time, String value) { + getTimeEvolution().setValueAt(time, new BooleanValue(value)); } @Override public String getType() { - return TYPE; + return StrolchValueType.BOOLEAN.getType(); } @Override diff --git a/li.strolch.model/src/main/java/li/strolch/model/timedstate/FloatTimedState.java b/li.strolch.model/src/main/java/li/strolch/model/timedstate/FloatTimedState.java index bf1748cb8..a91918b91 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/timedstate/FloatTimedState.java +++ b/li.strolch.model/src/main/java/li/strolch/model/timedstate/FloatTimedState.java @@ -15,19 +15,9 @@ */ package li.strolch.model.timedstate; -import java.util.Date; -import java.util.SortedSet; - -import li.strolch.model.Tags; -import li.strolch.model.timevalue.ITimeValue; +import li.strolch.model.StrolchValueType; import li.strolch.model.timevalue.impl.FloatValue; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.NodeList; - -import ch.eitchnet.utils.iso8601.ISO8601FormatFactory; - /** * @author Robert von Burg */ @@ -35,8 +25,6 @@ public class FloatTimedState extends AbstractStrolchTimedState { private static final long serialVersionUID = 1L; - public static final String TYPE = "FloatState"; - public FloatTimedState() { super(); } @@ -45,45 +33,14 @@ public class FloatTimedState extends AbstractStrolchTimedState { super(id, name); } - public FloatTimedState(Element element) { - super.fromDom(element); - - this.state = new TimedState<>(); - - NodeList timeValueElems = element.getElementsByTagName(Tags.VALUE); - for (int i = 0; i < timeValueElems.getLength(); i++) { - Element timeValueElem = (Element) timeValueElems.item(i); - String timeS = timeValueElem.getAttribute(Tags.TIME); - Date date = ISO8601FormatFactory.getInstance().parseDate(timeS); - long time = date.getTime(); - - Double value = Double.valueOf(timeValueElem.getAttribute(Tags.VALUE)); - FloatValue floatValue = new FloatValue(value); - this.state.getTimeEvolution().setValueAt(time, floatValue); - } - } - @Override - public Element toDom(Document doc) { - - Element stateElement = doc.createElement(Tags.TIMED_STATE); - super.fillElement(stateElement); - SortedSet> values = this.state.getTimeEvolution().getValues(); - for (ITimeValue timeValue : values) { - Long time = timeValue.getTime(); - FloatValue value = timeValue.getValue(); - Element valueElem = doc.createElement(Tags.VALUE); - valueElem.setAttribute(Tags.TIME, ISO8601FormatFactory.getInstance().formatDate(time)); - valueElem.setAttribute(Tags.VALUE, value.getValue().toString()); - stateElement.appendChild(valueElem); - } - - return stateElement; + public void setStateFromStringAt(Long time, String value) { + getTimeEvolution().setValueAt(time, new FloatValue(value)); } @Override public String getType() { - return TYPE; + return StrolchValueType.FLOAT.getType(); } @Override diff --git a/li.strolch.model/src/main/java/li/strolch/model/timedstate/IntegerTimedState.java b/li.strolch.model/src/main/java/li/strolch/model/timedstate/IntegerTimedState.java index dd239adad..63c39e46b 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/timedstate/IntegerTimedState.java +++ b/li.strolch.model/src/main/java/li/strolch/model/timedstate/IntegerTimedState.java @@ -15,19 +15,9 @@ */ package li.strolch.model.timedstate; -import java.util.Date; -import java.util.SortedSet; - -import li.strolch.model.Tags; -import li.strolch.model.timevalue.ITimeValue; +import li.strolch.model.StrolchValueType; import li.strolch.model.timevalue.impl.IntegerValue; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.NodeList; - -import ch.eitchnet.utils.iso8601.ISO8601FormatFactory; - /** * @author Robert von Burg */ @@ -35,8 +25,6 @@ public class IntegerTimedState extends AbstractStrolchTimedState { private static final long serialVersionUID = 1L; - public static final String TYPE = "IntegerState"; - public IntegerTimedState() { super(); } @@ -45,45 +33,14 @@ public class IntegerTimedState extends AbstractStrolchTimedState { super(id, name); } - public IntegerTimedState(Element element) { - super.fromDom(element); - - this.state = new TimedState<>(); - - NodeList timeValueElems = element.getElementsByTagName(Tags.VALUE); - for (int i = 0; i < timeValueElems.getLength(); i++) { - Element timeValueElem = (Element) timeValueElems.item(i); - String timeS = timeValueElem.getAttribute(Tags.TIME); - Date date = ISO8601FormatFactory.getInstance().parseDate(timeS); - long time = date.getTime(); - - Integer value = Integer.valueOf(timeValueElem.getAttribute(Tags.VALUE)); - IntegerValue integerValue = new IntegerValue(value); - this.state.getTimeEvolution().setValueAt(time, integerValue); - } - } - @Override - public Element toDom(Document doc) { - - Element stateElement = doc.createElement(Tags.TIMED_STATE); - super.fillElement(stateElement); - SortedSet> values = this.state.getTimeEvolution().getValues(); - for (ITimeValue timeValue : values) { - Long time = timeValue.getTime(); - IntegerValue value = timeValue.getValue(); - Element valueElem = doc.createElement(Tags.VALUE); - valueElem.setAttribute(Tags.TIME, ISO8601FormatFactory.getInstance().formatDate(time)); - valueElem.setAttribute(Tags.VALUE, value.getValue().toString()); - stateElement.appendChild(valueElem); - } - - return stateElement; + public void setStateFromStringAt(Long time, String value) { + getTimeEvolution().setValueAt(time, new IntegerValue(value)); } @Override public String getType() { - return TYPE; + return StrolchValueType.INTEGER.getType(); } @Override diff --git a/li.strolch.model/src/main/java/li/strolch/model/timedstate/StringSetTimedState.java b/li.strolch.model/src/main/java/li/strolch/model/timedstate/StringSetTimedState.java index 72d7e6f10..9ebcfc3ca 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/timedstate/StringSetTimedState.java +++ b/li.strolch.model/src/main/java/li/strolch/model/timedstate/StringSetTimedState.java @@ -15,23 +15,9 @@ */ package li.strolch.model.timedstate; -import java.util.Date; -import java.util.HashSet; -import java.util.Iterator; -import java.util.Set; -import java.util.SortedSet; - -import li.strolch.model.Tags; -import li.strolch.model.timevalue.ITimeValue; -import li.strolch.model.timevalue.impl.AString; +import li.strolch.model.StrolchValueType; import li.strolch.model.timevalue.impl.StringSetValue; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.NodeList; - -import ch.eitchnet.utils.iso8601.ISO8601FormatFactory; - /** * @author Robert von Burg */ @@ -39,8 +25,6 @@ public class StringSetTimedState extends AbstractStrolchTimedState(); - - NodeList timeValueElems = element.getElementsByTagName(Tags.VALUE); - for (int i = 0; i < timeValueElems.getLength(); i++) { - Element timeValueElem = (Element) timeValueElems.item(i); - String timeS = timeValueElem.getAttribute(Tags.TIME); - Date date = ISO8601FormatFactory.getInstance().parseDate(timeS); - long time = date.getTime(); - - String valueAsString = timeValueElem.getAttribute(Tags.VALUE); - Set value = new HashSet<>(); - String[] values = valueAsString.split(","); - for (String s : values) { - value.add(new AString(s.trim())); - } - - StringSetValue integerValue = new StringSetValue(value); - this.state.getTimeEvolution().setValueAt(time, integerValue); - } - } - @Override - public Element toDom(Document doc) { - - Element stateElement = doc.createElement(Tags.TIMED_STATE); - super.fillElement(stateElement); - SortedSet> values = this.state.getTimeEvolution().getValues(); - for (ITimeValue timeValue : values) { - Long time = timeValue.getTime(); - StringSetValue stringSetValue = timeValue.getValue(); - - Set value = stringSetValue.getValue(); - StringBuilder sb = new StringBuilder(); - Iterator iter = value.iterator(); - while (iter.hasNext()) { - sb.append(iter.next().getString()); - if (iter.hasNext()) { - sb.append(", "); - } - } - String valueAsString = sb.toString(); - - Element valueElem = doc.createElement(Tags.VALUE); - valueElem.setAttribute(Tags.TIME, ISO8601FormatFactory.getInstance().formatDate(time)); - valueElem.setAttribute(Tags.VALUE, valueAsString); - stateElement.appendChild(valueElem); - } - - return stateElement; + public void setStateFromStringAt(Long time, String value) { + getTimeEvolution().setValueAt(time, new StringSetValue(value)); } @Override public String getType() { - return TYPE; + return StrolchValueType.STRING_SET.getType(); } @Override diff --git a/li.strolch.model/src/main/java/li/strolch/model/timedstate/StrolchTimedState.java b/li.strolch.model/src/main/java/li/strolch/model/timedstate/StrolchTimedState.java index 89ad1548a..76f8281e8 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/timedstate/StrolchTimedState.java +++ b/li.strolch.model/src/main/java/li/strolch/model/timedstate/StrolchTimedState.java @@ -108,6 +108,16 @@ public interface StrolchTimedState extends StrolchElement { public ITimeValue getStateAt(Long time); + /** + * set the value at a point in time to a given time value object from a string value + * + * @param time + * the time to set the {@link IValue} + * @param value + * the string to parse to an {@link IValue} + */ + void setStateFromStringAt(final Long time, final String value); + public ITimeVariable getTimeEvolution(); public void setParent(Resource aThis); diff --git a/li.strolch.model/src/main/java/li/strolch/model/timevalue/IValue.java b/li.strolch.model/src/main/java/li/strolch/model/timevalue/IValue.java index 2c33a262d..53f8d02fe 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/timevalue/IValue.java +++ b/li.strolch.model/src/main/java/li/strolch/model/timevalue/IValue.java @@ -26,6 +26,11 @@ package li.strolch.model.timevalue; */ public interface IValue { + /** + * @return the type of this {@link IValue} + */ + String getType(); + /** * @return the backing value */ diff --git a/li.strolch.model/src/main/java/li/strolch/model/timevalue/IValueChange.java b/li.strolch.model/src/main/java/li/strolch/model/timevalue/IValueChange.java index f9b880239..3dc923ac7 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/timevalue/IValueChange.java +++ b/li.strolch.model/src/main/java/li/strolch/model/timevalue/IValueChange.java @@ -18,12 +18,9 @@ package li.strolch.model.timevalue; import li.strolch.model.timedstate.AbstractStrolchTimedState; import li.strolch.model.timevalue.impl.TimeVariable; -import org.w3c.dom.Document; -import org.w3c.dom.Element; - /** - * Interface for operators to be used to change the values of {@link ITimeValue} - * in a {@link ITimeVariable} or {@link AbstractStrolchTimedState}. + * Interface for operators to be used to change the values of {@link ITimeValue} in a {@link ITimeVariable} or + * {@link AbstractStrolchTimedState}. * * @author Martin Smock */ @@ -31,15 +28,13 @@ import org.w3c.dom.Element; public interface IValueChange { /** - * @return the id of the {@link AbstractStrolchTimedState} the change - * applies to + * @return the id of the {@link AbstractStrolchTimedState} the change applies to */ String getStateId(); /** * @param id - * the id of the {@link AbstractStrolchTimedState} the change - * applies to + * the id of the {@link AbstractStrolchTimedState} the change applies to */ void setStateId(String id); @@ -47,31 +42,23 @@ public interface IValueChange { * @return the time this change has to be applied */ Long getTime(); - - void setTime(Long time); + + void setTime(Long time); /** * @return the value of the change */ T getValue(); - - void setValue(T value); + + void setValue(T value); /** - * @return the inverse neutralizing a change. Very useful to undo changes - * made to a {@link TimeVariable}. + * @return the inverse neutralizing a change. Very useful to undo changes made to a {@link TimeVariable}. */ IValueChange getInverse(); - + /** * @return a copy of this */ - IValueChange getClone(); - - /** - * @param doc - * @return a xml serialisation of this - */ - Element toDom(Document doc); - + IValueChange getClone(); } diff --git a/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/BooleanValue.java b/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/BooleanValue.java index 4e00f603c..e66cdc998 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/BooleanValue.java +++ b/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/BooleanValue.java @@ -17,6 +17,7 @@ package li.strolch.model.timevalue.impl; import java.io.Serializable; +import li.strolch.model.StrolchValueType; import li.strolch.model.timevalue.ITimeValue; import li.strolch.model.timevalue.IValue; @@ -45,6 +46,11 @@ public class BooleanValue implements IValue, Serializable { this.value = Boolean.parseBoolean(valueAsString); } + @Override + public String getType() { + return StrolchValueType.BOOLEAN.getType(); + } + @Override public BooleanValue add(Boolean o) { this.value = o; diff --git a/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/FloatValue.java b/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/FloatValue.java index deaddc02d..e1c059d4e 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/FloatValue.java +++ b/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/FloatValue.java @@ -17,6 +17,7 @@ package li.strolch.model.timevalue.impl; import java.io.Serializable; +import li.strolch.model.StrolchValueType; import li.strolch.model.timevalue.ITimeValue; import li.strolch.model.timevalue.IValue; @@ -53,6 +54,11 @@ public class FloatValue implements IValue, Serializable { this.value = Double.parseDouble(valueAsString); } + @Override + public String getType() { + return StrolchValueType.FLOAT.getType(); + } + @Override public FloatValue add(Double o) { this.value += o; diff --git a/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/IntegerValue.java b/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/IntegerValue.java index c1476273f..2cf6f5b96 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/IntegerValue.java +++ b/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/IntegerValue.java @@ -17,6 +17,7 @@ package li.strolch.model.timevalue.impl; import java.io.Serializable; +import li.strolch.model.StrolchValueType; import li.strolch.model.timevalue.ITimeValue; import li.strolch.model.timevalue.IValue; @@ -45,6 +46,11 @@ public class IntegerValue implements IValue, Serializable { this.value = Integer.parseInt(valueAsString); } + @Override + public String getType() { + return StrolchValueType.INTEGER.getType(); + } + @Override public IntegerValue add(Integer o) { this.value += o; diff --git a/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/StringSetValue.java b/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/StringSetValue.java index e17c590c7..002298022 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/StringSetValue.java +++ b/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/StringSetValue.java @@ -22,6 +22,7 @@ import java.util.Iterator; import java.util.Set; import li.strolch.exception.StrolchException; +import li.strolch.model.StrolchValueType; import li.strolch.model.timevalue.ITimeValue; import li.strolch.model.timevalue.IValue; import ch.eitchnet.utils.dbc.DBC; @@ -42,7 +43,7 @@ public class StringSetValue implements IValue>, Serializable { private Set aStrings = new HashSet<>(); - public StringSetValue() { + private StringSetValue() { } public StringSetValue(final Set aStrings) { @@ -50,6 +51,20 @@ public class StringSetValue implements IValue>, Serializable { this.aStrings = aStrings; } + public StringSetValue(String valueAsString) { + Set value = new HashSet<>(); + String[] values = valueAsString.split(","); + for (String s : values) { + value.add(new AString(s.trim())); + } + this.aStrings = value; + } + + @Override + public String getType() { + return StrolchValueType.STRING_SET.getType(); + } + @Override public Set getValue() { return this.aStrings; diff --git a/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/ValueChange.java b/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/ValueChange.java index db56b0936..ce8808558 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/ValueChange.java +++ b/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/ValueChange.java @@ -17,15 +17,9 @@ package li.strolch.model.timevalue.impl; import java.io.Serializable; -import li.strolch.model.Tags; import li.strolch.model.timevalue.IValue; import li.strolch.model.timevalue.IValueChange; -import org.w3c.dom.Document; -import org.w3c.dom.Element; - -import ch.eitchnet.utils.iso8601.ISO8601FormatFactory; - /** * @author Martin Smock */ @@ -67,7 +61,8 @@ public class ValueChange implements IValueChange, Serializa public Long getTime() { return this.time; } - + + @Override public void setTime(Long time) { this.time = time; } @@ -77,7 +72,8 @@ public class ValueChange implements IValueChange, Serializa public T getValue() { return (T) this.value.getCopy(); } - + + @Override public void setValue(T value) { this.value = value; } @@ -134,13 +130,15 @@ public class ValueChange implements IValueChange, Serializa sb.append(this.time); sb.append(", value="); sb.append(this.value); + sb.append(", stateId="); + sb.append(this.stateId); sb.append("]"); return sb.toString(); } @Override public String getStateId() { - return stateId; + return this.stateId; } @Override @@ -151,17 +149,6 @@ public class ValueChange implements IValueChange, Serializa @SuppressWarnings("unchecked") @Override public IValueChange getClone() { - return new ValueChange(time, value); + return new ValueChange(this.time, this.value, this.stateId); } - - @Override - public Element toDom(Document doc) { - Element element = doc.createElement(Tags.VALUE_CHANGE); - element.setAttribute(Tags.STATE_ID, this.stateId); - element.setAttribute(Tags.TIME, ISO8601FormatFactory.getInstance().formatDate(time)); - element.setAttribute(Tags.VALUE, this.value.getValueAsString()); - element.setAttribute(Tags.VALUE_CLASS, this.value.getClass().getName()); - return element; - } - } diff --git a/li.strolch.model/src/main/java/li/strolch/model/xml/AbstractToSaxWriterVisitor.java b/li.strolch.model/src/main/java/li/strolch/model/xml/AbstractToSaxWriterVisitor.java index b71671ec8..c7423b850 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/xml/AbstractToSaxWriterVisitor.java +++ b/li.strolch.model/src/main/java/li/strolch/model/xml/AbstractToSaxWriterVisitor.java @@ -20,8 +20,9 @@ import static li.strolch.model.StrolchModelConstants.UOM_NONE; import java.util.ArrayList; import java.util.Collections; -import java.util.Comparator; +import java.util.Iterator; import java.util.List; +import java.util.Map.Entry; import java.util.Set; import java.util.SortedSet; import java.util.TreeSet; @@ -36,12 +37,15 @@ import li.strolch.model.ParameterizedElement; import li.strolch.model.Resource; import li.strolch.model.StrolchElement; import li.strolch.model.Tags; +import li.strolch.model.activity.Action; +import li.strolch.model.activity.Activity; +import li.strolch.model.activity.IActivityElement; import li.strolch.model.parameter.Parameter; import li.strolch.model.timedstate.StrolchTimedState; import li.strolch.model.timevalue.ITimeValue; import li.strolch.model.timevalue.ITimeVariable; import li.strolch.model.timevalue.IValue; -import ch.eitchnet.utils.helper.StringHelper; +import li.strolch.model.timevalue.IValueChange; import ch.eitchnet.utils.iso8601.ISO8601FormatFactory; /** @@ -55,16 +59,81 @@ public abstract class AbstractToSaxWriterVisitor { this.writer = writer; } - protected void writeElement(String tag, Order order) throws XMLStreamException { + protected void writeElement(Order order) throws XMLStreamException { boolean empty = !order.hasParameterBags(); - writeElement(tag, empty, order); + + writeStartStrolchElement(Tags.ORDER, empty, order); + this.writer.writeAttribute(Tags.DATE, ISO8601FormatFactory.getInstance().formatDate(order.getDate())); + this.writer.writeAttribute(Tags.STATE, order.getState().name()); + + if (order.hasParameterBags()) { + writeParameterBags(order); + } + if (!empty) this.writer.writeEndElement(); } - protected void writeElement(String tag, Resource resource) throws XMLStreamException { + protected void writeElement(Activity activity) throws XMLStreamException { + boolean empty = !activity.hasParameterBags() && !activity.hasElements(); + + writeStartStrolchElement(Tags.ACTIVITY, empty, activity); + + if (activity.hasParameterBags()) { + writeParameterBags(activity); + } + + if (activity.hasElements()) { + Iterator> iter = activity.elementIterator(); + while (iter.hasNext()) { + IActivityElement element = iter.next().getValue(); + if (element instanceof Activity) + writeElement((Activity) element); + else if (element instanceof Action) + writeElement((Action) element); + else + throw new IllegalArgumentException("Unhandled Element class " + element.getClass()); + } + } + + if (!empty) + this.writer.writeEndElement(); + } + + private void writeElement(Action action) throws XMLStreamException { + boolean empty = !action.hasParameterBags() && !action.hasChanges(); + + writeStartStrolchElement(Tags.ACTION, empty, action); + this.writer.writeAttribute(Tags.STATE, action.getState().name()); + this.writer.writeAttribute(Tags.RESOURCE_ID, action.getResourceId()); + this.writer.writeAttribute(Tags.RESOURCE_TYPE, action.getResourceType()); + + if (action.hasParameterBags()) { + writeParameterBags(action); + } + + if (action.hasChanges()) { + for (IValueChange> change : action.getChanges()) { + this.writer.writeEmptyElement(Tags.VALUE_CHANGE); + this.writer.writeAttribute(Tags.STATE_ID, change.getStateId()); + this.writer.writeAttribute(Tags.TIME, ISO8601FormatFactory.getInstance().formatDate(change.getTime())); + this.writer.writeAttribute(Tags.VALUE, change.getValue().getValueAsString()); + this.writer.writeAttribute(Tags.TYPE, change.getValue().getClass().getName()); + } + } + + if (!empty) + this.writer.writeEndElement(); + } + + protected void writeElement(Resource resource) throws XMLStreamException { boolean empty = !resource.hasParameterBags() && !resource.hasTimedStates(); - writeElement(tag, empty, resource); + + writeStartStrolchElement(Tags.RESOURCE, empty, resource); + + if (resource.hasParameterBags()) { + writeParameterBags(resource); + } if (resource.hasTimedStates()) writeTimedStates(resource); @@ -97,14 +166,6 @@ public abstract class AbstractToSaxWriterVisitor { } } - protected void writeElement(String tag, boolean empty, GroupedParameterizedElement element) - throws XMLStreamException { - writeStartStrolchElement(tag, empty, element); - if (!empty) { - writeParameterBags(element); - } - } - protected void writeStartStrolchElement(String tag, boolean empty, StrolchElement element) throws XMLStreamException { if (empty) { @@ -114,9 +175,7 @@ public abstract class AbstractToSaxWriterVisitor { } this.writer.writeAttribute(Tags.ID, element.getId()); - if (StringHelper.isNotEmpty(element.getName())) { - this.writer.writeAttribute(Tags.NAME, element.getName()); - } + this.writer.writeAttribute(Tags.NAME, element.getName()); this.writer.writeAttribute(Tags.TYPE, element.getType()); } @@ -136,12 +195,7 @@ public abstract class AbstractToSaxWriterVisitor { protected void writeParameters(ParameterizedElement element) throws XMLStreamException { List> parameters = new ArrayList<>(element.getParameters()); - Collections.sort(parameters, new Comparator>() { - @Override - public int compare(Parameter o1, Parameter o2) { - return Integer.valueOf(o1.getIndex()).compareTo(o2.getIndex()); - } - }); + Collections.sort(parameters, (o1, o2) -> Integer.valueOf(o1.getIndex()).compareTo(o2.getIndex())); for (Parameter parameter : parameters) { writeStartStrolchElement(Tags.PARAMETER, true, parameter); diff --git a/li.strolch.model/src/main/java/li/strolch/model/xml/ActivityToDomVisitor.java b/li.strolch.model/src/main/java/li/strolch/model/xml/ActivityToDomVisitor.java new file mode 100644 index 000000000..9cbf8c231 --- /dev/null +++ b/li.strolch.model/src/main/java/li/strolch/model/xml/ActivityToDomVisitor.java @@ -0,0 +1,51 @@ +/* + * Copyright 2013 Robert von Burg + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package li.strolch.model.xml; + +import javax.xml.parsers.DocumentBuilder; + +import li.strolch.model.ActivityVisitor; +import li.strolch.model.activity.Action; +import li.strolch.model.activity.Activity; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; + +import ch.eitchnet.utils.helper.DomUtil; + +/** + * @author Robert von Burg + */ +public class ActivityToDomVisitor extends StrolchElementToDomVisitor implements ActivityVisitor { + + @Override + public Document visit(Activity activity) { + DocumentBuilder documentBuilder = DomUtil.createDocumentBuilder(); + this.document = documentBuilder.getDOMImplementation().createDocument(null, null, null); + + Element asDom = toDom(activity); + document.appendChild(asDom); + return this.document; + } + + public Element toDom(Action action) { + return super.toDom(action); + } + + public Element toDom(Activity activity) { + return super.toDom(activity); + } +} diff --git a/li.strolch.model/src/main/java/li/strolch/model/xml/ActivityToSaxWriterVisitor.java b/li.strolch.model/src/main/java/li/strolch/model/xml/ActivityToSaxWriterVisitor.java new file mode 100644 index 000000000..cc0a11204 --- /dev/null +++ b/li.strolch.model/src/main/java/li/strolch/model/xml/ActivityToSaxWriterVisitor.java @@ -0,0 +1,49 @@ +/* + * Copyright 2015 Robert von Burg + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package li.strolch.model.xml; + +import java.text.MessageFormat; + +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamWriter; + +import li.strolch.exception.StrolchException; +import li.strolch.model.ActivityVisitor; +import li.strolch.model.activity.Activity; + +/** + * @author Robert von Burg + */ +public class ActivityToSaxWriterVisitor extends AbstractToSaxWriterVisitor implements ActivityVisitor { + + public ActivityToSaxWriterVisitor(XMLStreamWriter writer) { + super(writer); + } + + @Override + public Void visit(Activity activity) { + try { + writeElement(activity); + this.writer.flush(); + } catch (XMLStreamException e) { + String msg = "Failed to write Activity {0} due to {1}"; //$NON-NLS-1$ + msg = MessageFormat.format(msg, activity.getLocator(), e.getMessage()); + throw new StrolchException(msg, e); + } + + return null; + } +} diff --git a/li.strolch.model/src/main/java/li/strolch/model/xml/OrderFromDomVisitor.java b/li.strolch.model/src/main/java/li/strolch/model/xml/OrderFromDomVisitor.java new file mode 100644 index 000000000..a3b664fa4 --- /dev/null +++ b/li.strolch.model/src/main/java/li/strolch/model/xml/OrderFromDomVisitor.java @@ -0,0 +1,58 @@ +/* + * Copyright 2015 Robert von Burg + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package li.strolch.model.xml; + +import li.strolch.model.Order; +import li.strolch.model.State; +import li.strolch.model.Tags; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; + +import ch.eitchnet.utils.helper.StringHelper; +import ch.eitchnet.utils.iso8601.ISO8601FormatFactory; + +/** + * @author Robert von Burg + */ +public class OrderFromDomVisitor extends StrolchElementFromDomVisitor { + + public Order visit(Document doc) { + return fromDom(doc.getDocumentElement()); + } + + public Order fromDom(Element element) { + Order order = new Order(); + fromDom(element, order); + + String date = element.getAttribute(Tags.DATE); + String state = element.getAttribute(Tags.STATE); + + if (StringHelper.isEmpty(date)) { + order.setDate(ISO8601FormatFactory.getInstance().getDateFormat().parse("-")); //$NON-NLS-1$ + } else { + order.setDate(ISO8601FormatFactory.getInstance().getDateFormat().parse(date)); + } + + if (state == null || state.isEmpty()) { + order.setState(State.CREATED); + } else { + order.setState(State.valueOf(state)); + } + + return order; + } +} diff --git a/li.strolch.model/src/main/java/li/strolch/model/xml/OrderToDomVisitor.java b/li.strolch.model/src/main/java/li/strolch/model/xml/OrderToDomVisitor.java index 77c9995aa..07dc7829b 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/xml/OrderToDomVisitor.java +++ b/li.strolch.model/src/main/java/li/strolch/model/xml/OrderToDomVisitor.java @@ -28,25 +28,15 @@ import ch.eitchnet.utils.helper.DomUtil; /** * @author Robert von Burg */ -public class OrderToDomVisitor implements OrderVisitor { - - private Document document; - - /** - * @return the document - */ - public Document getDocument() { - return this.document; - } +public class OrderToDomVisitor extends StrolchElementToDomVisitor implements OrderVisitor { @Override public Document visit(Order order) { DocumentBuilder documentBuilder = DomUtil.createDocumentBuilder(); - Document document = documentBuilder.getDOMImplementation().createDocument(null, null, null); + this.document = documentBuilder.getDOMImplementation().createDocument(null, null, null); - Element orderDom = order.toDom(document); - document.appendChild(orderDom); - this.document = document; + Element asDom = toDom(order); + document.appendChild(asDom); return this.document; } } diff --git a/li.strolch.model/src/main/java/li/strolch/model/xml/OrderToSaxWriterVisitor.java b/li.strolch.model/src/main/java/li/strolch/model/xml/OrderToSaxWriterVisitor.java index dc3b16d52..ece6c6984 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/xml/OrderToSaxWriterVisitor.java +++ b/li.strolch.model/src/main/java/li/strolch/model/xml/OrderToSaxWriterVisitor.java @@ -23,7 +23,6 @@ import javax.xml.stream.XMLStreamWriter; import li.strolch.exception.StrolchException; import li.strolch.model.Order; import li.strolch.model.OrderVisitor; -import li.strolch.model.Tags; /** * @author Robert von Burg @@ -37,7 +36,7 @@ public class OrderToSaxWriterVisitor extends AbstractToSaxWriterVisitor implemen @Override public Void visit(Order order) { try { - writeElement(Tags.ORDER, order); + writeElement(order); this.writer.flush(); } catch (XMLStreamException e) { String msg = "Failed to write Order {0} due to {1}"; //$NON-NLS-1$ diff --git a/li.strolch.model/src/main/java/li/strolch/model/xml/ResourceFromDomVisitor.java b/li.strolch.model/src/main/java/li/strolch/model/xml/ResourceFromDomVisitor.java new file mode 100644 index 000000000..d23773784 --- /dev/null +++ b/li.strolch.model/src/main/java/li/strolch/model/xml/ResourceFromDomVisitor.java @@ -0,0 +1,105 @@ +/* + * Copyright 2015 Robert von Burg + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package li.strolch.model.xml; + +import java.text.MessageFormat; +import java.util.Date; + +import li.strolch.exception.StrolchException; +import li.strolch.model.AbstractStrolchElement; +import li.strolch.model.Resource; +import li.strolch.model.StrolchValueType; +import li.strolch.model.Tags; +import li.strolch.model.timedstate.StrolchTimedState; +import li.strolch.model.timevalue.IValue; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; + +import ch.eitchnet.utils.dbc.DBC; +import ch.eitchnet.utils.helper.StringHelper; +import ch.eitchnet.utils.iso8601.ISO8601FormatFactory; + +/** + * @author Robert von Burg + */ +public class ResourceFromDomVisitor extends StrolchElementFromDomVisitor { + + public Resource visit(Document doc) { + return fromDom(doc.getDocumentElement()); + } + + public Resource fromDom(Element resourceElement) { + Resource resource = new Resource(); + fromDom(resourceElement, resource); + + NodeList timedStateElems = resourceElement.getElementsByTagName(Tags.TIMED_STATE); + for (int i = 0; i < timedStateElems.getLength(); i++) { + Element timedStateElem = (Element) timedStateElems.item(i); + String typeS = timedStateElem.getAttribute(Tags.TYPE); + + DBC.PRE.assertNotEmpty("Type must be set on TimedState for resource with id " + resource.getId(), typeS); + StrolchValueType valueType = StrolchValueType.parse(typeS); + StrolchTimedState> timedState = valueType.timedStateInstance(); + + fromDom(timedStateElem, (AbstractStrolchElement) timedState); + + String interpretation = timedStateElem.getAttribute(Tags.INTERPRETATION); + String hidden = timedStateElem.getAttribute(Tags.HIDDEN); + String uom = timedStateElem.getAttribute(Tags.UOM); + String index = timedStateElem.getAttribute(Tags.INDEX); + + timedState.setInterpretation(interpretation); + timedState.setUom(uom); + + if (StringHelper.isEmpty(index)) { + timedState.setIndex(0); + } else { + timedState.setIndex(Integer.valueOf(index)); + } + + if (StringHelper.isEmpty(hidden)) { + timedState.setHidden(false); + } else { + if (hidden.equalsIgnoreCase(Boolean.TRUE.toString())) { + timedState.setHidden(true); + } else if (hidden.equalsIgnoreCase(Boolean.FALSE.toString())) { + timedState.setHidden(false); + } else { + String msg = "Boolean string must be either {0} or {1}"; //$NON-NLS-1$ + msg = MessageFormat.format(msg, Boolean.TRUE.toString(), Boolean.FALSE.toString()); + throw new StrolchException(msg); + } + } + + NodeList timeValueElems = timedStateElem.getElementsByTagName(Tags.VALUE); + for (int j = 0; j < timeValueElems.getLength(); j++) { + Element timeValueElem = (Element) timeValueElems.item(j); + String timeS = timeValueElem.getAttribute(Tags.TIME); + Date date = ISO8601FormatFactory.getInstance().parseDate(timeS); + long time = date.getTime(); + + String valueS = timeValueElem.getAttribute(Tags.VALUE); + timedState.setStateFromStringAt(time, valueS); + } + + resource.addTimedState(timedState); + } + + return resource; + } +} diff --git a/li.strolch.model/src/main/java/li/strolch/model/xml/ResourceToDomVisitor.java b/li.strolch.model/src/main/java/li/strolch/model/xml/ResourceToDomVisitor.java index 2942d547c..e783bcfc1 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/xml/ResourceToDomVisitor.java +++ b/li.strolch.model/src/main/java/li/strolch/model/xml/ResourceToDomVisitor.java @@ -28,25 +28,15 @@ import ch.eitchnet.utils.helper.DomUtil; /** * @author Robert von Burg */ -public class ResourceToDomVisitor implements ResourceVisitor { - - private Document document; - - /** - * @return the document - */ - public Document getDocument() { - return this.document; - } +public class ResourceToDomVisitor extends StrolchElementToDomVisitor implements ResourceVisitor { @Override public Document visit(Resource resource) { DocumentBuilder documentBuilder = DomUtil.createDocumentBuilder(); - Document document = documentBuilder.getDOMImplementation().createDocument(null, null, null); + this.document = documentBuilder.getDOMImplementation().createDocument(null, null, null); - Element resourceDom = resource.toDom(document); - document.appendChild(resourceDom); - this.document = document; + Element asDom = toDom(resource); + document.appendChild(asDom); return this.document; } } diff --git a/li.strolch.model/src/main/java/li/strolch/model/xml/ResourceToSaxWriterVisitor.java b/li.strolch.model/src/main/java/li/strolch/model/xml/ResourceToSaxWriterVisitor.java index 764984a14..3b64c4b04 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/xml/ResourceToSaxWriterVisitor.java +++ b/li.strolch.model/src/main/java/li/strolch/model/xml/ResourceToSaxWriterVisitor.java @@ -23,7 +23,6 @@ import javax.xml.stream.XMLStreamWriter; import li.strolch.exception.StrolchException; import li.strolch.model.Resource; import li.strolch.model.ResourceVisitor; -import li.strolch.model.Tags; /** * @author Robert von Burg @@ -37,7 +36,7 @@ public class ResourceToSaxWriterVisitor extends AbstractToSaxWriterVisitor imple @Override public Void visit(Resource resource) { try { - writeElement(Tags.RESOURCE, resource); + writeElement(resource); this.writer.flush(); } catch (XMLStreamException e) { String msg = "Failed to write Resource {0} due to {1}"; //$NON-NLS-1$ diff --git a/li.strolch.model/src/main/java/li/strolch/model/xml/StrolchElementFromDomVisitor.java b/li.strolch.model/src/main/java/li/strolch/model/xml/StrolchElementFromDomVisitor.java new file mode 100644 index 000000000..986748cf5 --- /dev/null +++ b/li.strolch.model/src/main/java/li/strolch/model/xml/StrolchElementFromDomVisitor.java @@ -0,0 +1,122 @@ +/* + * Copyright 2015 Robert von Burg + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package li.strolch.model.xml; + +import java.text.MessageFormat; + +import li.strolch.exception.StrolchException; +import li.strolch.model.AbstractStrolchElement; +import li.strolch.model.GroupedParameterizedElement; +import li.strolch.model.ParameterBag; +import li.strolch.model.ParameterizedElement; +import li.strolch.model.StrolchValueType; +import li.strolch.model.Tags; +import li.strolch.model.parameter.Parameter; + +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; + +import ch.eitchnet.utils.helper.StringHelper; + +/** + * @author Robert von Burg + */ +public class StrolchElementFromDomVisitor { + + protected void fromDom(Element element, AbstractStrolchElement strolchElement) { + String id = element.getAttribute(Tags.ID); + String name = element.getAttribute(Tags.NAME); + + if (id != null && name != null) { + strolchElement.setId(id); + strolchElement.setName(name); + } else { + String msg = "Check the values of the element: {0} either id or name attribute is null!"; //$NON-NLS-1$ + msg = MessageFormat.format(msg, element.getNodeName()); + throw new StrolchException(msg); + } + } + + protected void fromDom(Element element, GroupedParameterizedElement order) { + fromDom(element, (AbstractStrolchElement) order); + + String type = element.getAttribute(Tags.TYPE); + order.setType(type); + + NodeList bags = element.getElementsByTagName(Tags.PARAMETER_BAG); + for (int i = 0; i < bags.getLength(); i++) { + Element bagElement = (Element) bags.item(i); + ParameterBag bag = new ParameterBag(); + fromDom(bagElement, bag); + order.addParameterBag(bag); + } + } + + protected void fromDom(Element element, ParameterizedElement parameterizedElement) { + fromDom(element, (AbstractStrolchElement) parameterizedElement); + + String type = element.getAttribute(Tags.TYPE); + parameterizedElement.setType(type); + + // add all the parameters + NodeList parameterElements = element.getElementsByTagName(Tags.PARAMETER); + for (int i = 0; i < parameterElements.getLength(); i++) { + Element paramElement = (Element) parameterElements.item(i); + String paramtype = paramElement.getAttribute(Tags.TYPE); + + StrolchValueType paramValueType = StrolchValueType.parse(paramtype); + Parameter parameter = paramValueType.parameterInstance(); + fromDom(paramElement, parameter); + parameterizedElement.addParameter(parameter); + } + } + + protected void fromDom(Element element, Parameter param) { + + fromDom(element, (AbstractStrolchElement) param); + + String value = element.getAttribute(Tags.VALUE); + param.setValueFromString(value); + + String interpretation = element.getAttribute(Tags.INTERPRETATION); + String hidden = element.getAttribute(Tags.HIDDEN); + String uom = element.getAttribute(Tags.UOM); + String index = element.getAttribute(Tags.INDEX); + + param.setInterpretation(interpretation); + param.setUom(uom); + + if (StringHelper.isEmpty(index)) { + param.setIndex(0); + } else { + param.setIndex(Integer.valueOf(index)); + } + + if (StringHelper.isEmpty(hidden)) { + param.setHidden(false); + } else { + if (hidden.equalsIgnoreCase(Boolean.TRUE.toString())) { + param.setHidden(true); + } else if (hidden.equalsIgnoreCase(Boolean.FALSE.toString())) { + param.setHidden(false); + } else { + String msg = "Boolean string must be either {0} or {1}"; //$NON-NLS-1$ + msg = MessageFormat.format(msg, Boolean.TRUE.toString(), Boolean.FALSE.toString()); + throw new StrolchException(msg); + } + } + } +} diff --git a/li.strolch.model/src/main/java/li/strolch/model/xml/StrolchElementToDomVisitor.java b/li.strolch.model/src/main/java/li/strolch/model/xml/StrolchElementToDomVisitor.java new file mode 100644 index 000000000..5a00f1a3b --- /dev/null +++ b/li.strolch.model/src/main/java/li/strolch/model/xml/StrolchElementToDomVisitor.java @@ -0,0 +1,222 @@ +/* + * Copyright 2015 Robert von Burg + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package li.strolch.model.xml; + +import java.util.Iterator; +import java.util.Map.Entry; +import java.util.SortedSet; + +import li.strolch.model.AbstractStrolchElement; +import li.strolch.model.GroupedParameterizedElement; +import li.strolch.model.Order; +import li.strolch.model.ParameterBag; +import li.strolch.model.ParameterizedElement; +import li.strolch.model.Resource; +import li.strolch.model.StrolchModelConstants; +import li.strolch.model.Tags; +import li.strolch.model.activity.Action; +import li.strolch.model.activity.Activity; +import li.strolch.model.activity.IActivityElement; +import li.strolch.model.parameter.Parameter; +import li.strolch.model.timedstate.StrolchTimedState; +import li.strolch.model.timevalue.ITimeValue; +import li.strolch.model.timevalue.IValue; +import li.strolch.model.timevalue.IValueChange; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; + +import ch.eitchnet.utils.iso8601.ISO8601FormatFactory; + +/** + * @author Robert von Burg + */ +public class StrolchElementToDomVisitor { + + protected Document document; + + public Document getDocument() { + return this.document; + } + + protected Element toDom(Order order) { + + Element asDom = document.createElement(Tags.ORDER); + fillElement(asDom, order); + + asDom.setAttribute(Tags.DATE, ISO8601FormatFactory.getInstance().formatDate(order.getDate())); + asDom.setAttribute(Tags.STATE, order.getState().name()); + + return asDom; + } + + protected Element toDom(Resource resource) { + + Element asDom = document.createElement(Tags.RESOURCE); + fillElement(asDom, resource); + + if (resource.hasTimedStates()) { + for (String stateKey : resource.getTimedStateKeySet()) { + StrolchTimedState> timedState = resource.getTimedState(stateKey); + Element stateElement = toDom(timedState); + asDom.appendChild(stateElement); + } + } + + return asDom; + } + + protected Element toDom(Activity activity) { + Element element = document.createElement(Tags.ACTIVITY); + fillElement(element, activity); + + if (activity.hasElements()) { + Iterator> iter = activity.elementIterator(); + while (iter.hasNext()) { + IActivityElement activityElement = iter.next().getValue(); + if (activityElement instanceof Activity) { + element.appendChild(toDom((Activity) activityElement)); + } else if (activityElement instanceof Action) { + element.appendChild(toDom((Action) activityElement)); + } + } + } + + return element; + } + + protected Element toDom(Action action) { + Element element = document.createElement(Tags.ACTION); + fillElement(element, action); + + element.setAttribute(Tags.RESOURCE_ID, action.getResourceId()); + element.setAttribute(Tags.RESOURCE_TYPE, action.getResourceType()); + element.setAttribute(Tags.STATE, action.getState().name()); + + if (action.hasChanges()) { + Iterator>> iter = action.changesIterator(); + while (iter.hasNext()) { + IValueChange> value = iter.next(); + Element valueChangeElement = toDom(value); + element.appendChild(valueChangeElement); + } + } + + return element; + } + + protected Element toDom(IValueChange> value) { + Element element = document.createElement(Tags.VALUE_CHANGE); + element.setAttribute(Tags.STATE_ID, value.getStateId()); + element.setAttribute(Tags.TIME, ISO8601FormatFactory.getInstance().formatDate(value.getTime())); + element.setAttribute(Tags.VALUE, value.getValue().getValueAsString()); + element.setAttribute(Tags.TYPE, value.getValue().getType()); + return element; + } + + protected Element toDom(StrolchTimedState> timedState) { + + Element element = document.createElement(Tags.TIMED_STATE); + fillElement(element, (AbstractStrolchElement) timedState); + + if (!timedState.getInterpretation().equals(StrolchModelConstants.INTERPRETATION_NONE)) { + element.setAttribute(Tags.INTERPRETATION, timedState.getInterpretation()); + } + if (!timedState.getUom().equals(StrolchModelConstants.UOM_NONE)) { + element.setAttribute(Tags.UOM, timedState.getUom()); + } + if (timedState.isHidden()) { + element.setAttribute(Tags.HIDDEN, Boolean.toString(timedState.isHidden())); + } + if (timedState.getIndex() != 0) { + element.setAttribute(Tags.INDEX, Integer.toString(timedState.getIndex())); + } + + SortedSet>> values = timedState.getTimeEvolution().getValues(); + for (ITimeValue> iTimeValue : values) { + + Long time = iTimeValue.getTime(); + String valueS = iTimeValue.getValue().getValueAsString(); + + Element valueElement = document.createElement(Tags.VALUE); + valueElement.setAttribute(Tags.TIME, ISO8601FormatFactory.getInstance().formatDate(time)); + valueElement.setAttribute(Tags.VALUE, valueS); + + element.appendChild(valueElement); + } + + return element; + } + + protected Element toDom(ParameterBag bag) { + Element bagElement = document.createElement(Tags.PARAMETER_BAG); + fillElement(bagElement, (ParameterizedElement) bag); + return bagElement; + } + + protected Element toDom(Parameter param) { + Element element = document.createElement(Tags.PARAMETER); + fillElement(element, (AbstractStrolchElement) param); + + element.setAttribute(Tags.VALUE, param.getValueAsString()); + + if (!param.getInterpretation().equals(StrolchModelConstants.INTERPRETATION_NONE)) { + element.setAttribute(Tags.INTERPRETATION, param.getInterpretation()); + } + if (!param.getUom().equals(StrolchModelConstants.UOM_NONE)) { + element.setAttribute(Tags.UOM, param.getUom()); + } + if (param.isHidden()) { + element.setAttribute(Tags.HIDDEN, Boolean.toString(param.isHidden())); + } + if (param.getIndex() != 0) { + element.setAttribute(Tags.INDEX, Integer.toString(param.getIndex())); + } + + return element; + } + + protected void fillElement(Element element, AbstractStrolchElement strolchElement) { + element.setAttribute(Tags.ID, strolchElement.getId()); + element.setAttribute(Tags.NAME, strolchElement.getName()); + element.setAttribute(Tags.TYPE, strolchElement.getType()); + } + + protected void fillElement(Element element, GroupedParameterizedElement groupedParameterizedElement) { + fillElement(element, (AbstractStrolchElement) groupedParameterizedElement); + + if (groupedParameterizedElement.hasParameterBags()) { + for (String bagKey : groupedParameterizedElement.getParameterBagKeySet()) { + ParameterBag bag = groupedParameterizedElement.getParameterBag(bagKey); + Element bagElement = toDom(bag); + element.appendChild(bagElement); + } + } + } + + protected void fillElement(Element element, ParameterizedElement parameterizedElement) { + fillElement(element, (AbstractStrolchElement) parameterizedElement); + + if (parameterizedElement.hasParameters()) { + for (String paramKey : parameterizedElement.getParameterKeySet()) { + Parameter parameter = parameterizedElement.getParameter(paramKey); + Element paramElement = toDom(parameter); + element.appendChild(paramElement); + } + } + } + +} diff --git a/li.strolch.model/src/main/java/li/strolch/model/xml/XmlModelSaxFileReader.java b/li.strolch.model/src/main/java/li/strolch/model/xml/XmlModelSaxFileReader.java index d805274f8..1c2d9be32 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/xml/XmlModelSaxFileReader.java +++ b/li.strolch.model/src/main/java/li/strolch/model/xml/XmlModelSaxFileReader.java @@ -76,8 +76,7 @@ public class XmlModelSaxFileReader extends XmlModelSaxReader { XmlModelSaxFileReader handler = new XmlModelSaxFileReader(this.listener, includeFile, this.allowInclude); handler.parseFile(); - this.statistics.nrOfOrders += handler.statistics.nrOfOrders; - this.statistics.nrOfResources += handler.statistics.nrOfResources; + this.statistics.add(handler.statistics); break; default: diff --git a/li.strolch.model/src/main/java/li/strolch/model/xml/XmlModelSaxReader.java b/li.strolch.model/src/main/java/li/strolch/model/xml/XmlModelSaxReader.java index e97751b15..4c29a79f6 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/xml/XmlModelSaxReader.java +++ b/li.strolch.model/src/main/java/li/strolch/model/xml/XmlModelSaxReader.java @@ -16,9 +16,9 @@ package li.strolch.model.xml; import java.text.MessageFormat; +import java.util.ArrayDeque; import java.util.Date; -import java.util.HashSet; -import java.util.Set; +import java.util.Deque; import li.strolch.exception.StrolchException; import li.strolch.model.GroupedParameterizedElement; @@ -27,30 +27,14 @@ import li.strolch.model.Order; import li.strolch.model.ParameterBag; import li.strolch.model.Resource; import li.strolch.model.State; +import li.strolch.model.StrolchValueType; import li.strolch.model.Tags; -import li.strolch.model.parameter.BooleanParameter; -import li.strolch.model.parameter.DateParameter; -import li.strolch.model.parameter.DurationParameter; -import li.strolch.model.parameter.FloatListParameter; -import li.strolch.model.parameter.FloatParameter; -import li.strolch.model.parameter.IntegerListParameter; -import li.strolch.model.parameter.IntegerParameter; -import li.strolch.model.parameter.LongListParameter; -import li.strolch.model.parameter.LongParameter; +import li.strolch.model.activity.Action; +import li.strolch.model.activity.Activity; import li.strolch.model.parameter.Parameter; -import li.strolch.model.parameter.StringListParameter; -import li.strolch.model.parameter.StringParameter; -import li.strolch.model.timedstate.BooleanTimedState; -import li.strolch.model.timedstate.FloatTimedState; -import li.strolch.model.timedstate.IntegerTimedState; -import li.strolch.model.timedstate.StringSetTimedState; import li.strolch.model.timedstate.StrolchTimedState; import li.strolch.model.timevalue.IValue; -import li.strolch.model.timevalue.impl.AString; -import li.strolch.model.timevalue.impl.BooleanValue; -import li.strolch.model.timevalue.impl.FloatValue; -import li.strolch.model.timevalue.impl.IntegerValue; -import li.strolch.model.timevalue.impl.StringSetValue; +import li.strolch.model.timevalue.impl.ValueChange; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -72,13 +56,15 @@ public class XmlModelSaxReader extends DefaultHandler { protected ModelStatistics statistics; private GroupedParameterizedElement parameterizedElement; + private Deque activityStack; private ParameterBag pBag; private StrolchTimedState> state; - private String stateType; + private StrolchValueType stateType; public XmlModelSaxReader(StrolchElementListener listener) { this.listener = listener; this.statistics = new ModelStatistics(); + this.activityStack = new ArrayDeque<>(); } /** @@ -104,6 +90,56 @@ public class XmlModelSaxReader extends DefaultHandler { String resType = attributes.getValue(Tags.TYPE); Resource resource = new Resource(resId, resName, resType); this.parameterizedElement = resource; + + break; + + case Tags.ACTIVITY: + + String activityId = attributes.getValue(Tags.ID); + String activityName = attributes.getValue(Tags.NAME); + String activityType = attributes.getValue(Tags.TYPE); + Activity activity = new Activity(activityId, activityName, activityType); + this.parameterizedElement = activity; + this.activityStack.push(activity); + + break; + + case Tags.ACTION: + + // Action Id="action_1" Name="Action 1" ResourceId="dummyId" ResourceType="dummyType" State="CREATED" Type="Use" + + String actionId = attributes.getValue(Tags.ID); + String actionName = attributes.getValue(Tags.NAME); + String actionType = attributes.getValue(Tags.TYPE); + String actionResourceId = attributes.getValue(Tags.RESOURCE_ID); + String actionResourceType = attributes.getValue(Tags.RESOURCE_TYPE); + String actionState = attributes.getValue(Tags.STATE); + Action action = new Action(actionId, actionName, actionType); + action.setResourceId(actionResourceId); + action.setResourceType(actionResourceType); + action.setState(State.valueOf(actionState)); + + this.activityStack.peek().addElement(action); + this.parameterizedElement = action; + + break; + + case Tags.VALUE_CHANGE: + + // ValueChange StateId="dummyId" Time="2012-11-30T18:12:05.628+01:00" Value="5" ValueClass="Integer" + + String valueChangeStateId = attributes.getValue(Tags.STATE_ID); + String valueChangeTimeS = attributes.getValue(Tags.TIME); + String valueChangeValue = attributes.getValue(Tags.VALUE); + String valueChangeType = attributes.getValue(Tags.TYPE); + + IValue value = StrolchValueType.parse(valueChangeType).valueInstance(valueChangeValue); + long valueChangeTime = ISO8601FormatFactory.getInstance().getDateFormat().parse(valueChangeTimeS).getTime(); + ValueChange> valueChange = new ValueChange>(valueChangeTime, value); + valueChange.setStateId(valueChangeStateId); + + ((Action) this.parameterizedElement).addChange(valueChange); + break; case Tags.ORDER: @@ -122,6 +158,7 @@ public class XmlModelSaxReader extends DefaultHandler { order.setState(orderState); } this.parameterizedElement = order; + break; case Tags.PARAMETER_BAG: @@ -131,12 +168,11 @@ public class XmlModelSaxReader extends DefaultHandler { ParameterBag pBag = new ParameterBag(pBagId, pBagName, pBagType); this.pBag = pBag; this.parameterizedElement.addParameterBag(pBag); + break; case Tags.PARAMETER: - // TODO refactor this code into using visitors - String paramId = attributes.getValue(Tags.ID); String paramName = attributes.getValue(Tags.NAME); String paramType = attributes.getValue(Tags.TYPE); @@ -149,110 +185,50 @@ public class XmlModelSaxReader extends DefaultHandler { .parseBoolean(paramHiddenS); String paramUom = attributes.getValue(Tags.UOM); String paramInterpretation = attributes.getValue(Tags.INTERPRETATION); - Parameter param; - switch (paramType) { - case StringParameter.TYPE: - param = new StringParameter(paramId, paramName, paramValue); - break; - case IntegerParameter.TYPE: - param = new IntegerParameter(paramId, paramName, IntegerParameter.parseFromString(paramValue)); - break; - case BooleanParameter.TYPE: - param = new BooleanParameter(paramId, paramName, BooleanParameter.parseFromString(paramValue)); - break; - case LongParameter.TYPE: - param = new LongParameter(paramId, paramName, LongParameter.parseFromString(paramValue)); - break; - case DateParameter.TYPE: - param = new DateParameter(paramId, paramName, DateParameter.parseFromString(paramValue)); - break; - case DurationParameter.TYPE: - param = new DurationParameter(paramId, paramName, DurationParameter.parseFromString(paramValue)); - break; - case StringListParameter.TYPE: - param = new StringListParameter(paramId, paramName, StringListParameter.parseFromString(paramValue)); - break; - case IntegerListParameter.TYPE: - param = new IntegerListParameter(paramId, paramName, - IntegerListParameter.parseFromString(paramValue)); - break; - case FloatListParameter.TYPE: - param = new FloatListParameter(paramId, paramName, FloatListParameter.parseFromString(paramValue)); - break; - case LongListParameter.TYPE: - param = new LongListParameter(paramId, paramName, LongListParameter.parseFromString(paramValue)); - break; - case FloatParameter.TYPE: - param = new FloatParameter(paramId, paramName, FloatParameter.parseFromString(paramValue)); - break; - default: - throw new UnsupportedOperationException(MessageFormat.format( - "Parameters of type {0} are not supported!", paramType)); //$NON-NLS-1$ - } + + StrolchValueType type = StrolchValueType.parse(paramType); + + Parameter param = type.parameterInstance(); + param.setId(paramId); + param.setName(paramName); + param.setValueFromString(paramValue); + param.setHidden(paramHidden); param.setUom(paramUom); param.setInterpretation(paramInterpretation); param.setIndex(index); + this.pBag.addParameter(param); + } catch (Exception e) { throw new StrolchException("Failed to instantiate parameter " + paramId + " for bag " + this.pBag.getLocator() + " due to " + e.getMessage(), e); } + break; case Tags.TIMED_STATE: + String stateId = attributes.getValue(Tags.ID); String stateName = attributes.getValue(Tags.NAME); - this.stateType = attributes.getValue(Tags.TYPE); + String stateType = attributes.getValue(Tags.TYPE); - switch (this.stateType) { - case FloatTimedState.TYPE: - this.state = new FloatTimedState(stateId, stateName); - break; - case IntegerTimedState.TYPE: - this.state = new IntegerTimedState(stateId, stateName); - break; - case BooleanTimedState.TYPE: - this.state = new BooleanTimedState(stateId, stateName); - break; - case StringSetTimedState.TYPE: - this.state = new StringSetTimedState(stateId, stateName); - break; - default: - break; - } + this.stateType = StrolchValueType.parse(stateType); + this.state = this.stateType.timedStateInstance(); + this.state.setId(stateId); + this.state.setName(stateName); break; case Tags.VALUE: + String valueTime = attributes.getValue(Tags.TIME); Date date = ISO8601FormatFactory.getInstance().parseDate(valueTime); long time = date.getTime(); String valueValue = attributes.getValue(Tags.VALUE); - switch (this.stateType) { - case FloatTimedState.TYPE: - ((FloatTimedState) this.state).getTimeEvolution().setValueAt(time, new FloatValue(valueValue)); - break; - case IntegerTimedState.TYPE: - ((IntegerTimedState) this.state).getTimeEvolution().setValueAt(time, new IntegerValue(valueValue)); - break; - case BooleanTimedState.TYPE: - ((BooleanTimedState) this.state).getTimeEvolution().setValueAt(time, new BooleanValue(valueValue)); - break; - case StringSetTimedState.TYPE: - Set value = new HashSet<>(); - String[] values = valueValue.split(","); - for (String s : values) { - value.add(new AString(s.trim())); - } + this.state.setStateFromStringAt(time, valueValue); - StringSetValue stringSetValue = new StringSetValue(value); - ((StringSetTimedState) this.state).getTimeEvolution().setValueAt(time, stringSetValue); - break; - default: - break; - } break; default: @@ -264,30 +240,58 @@ public class XmlModelSaxReader extends DefaultHandler { public void endElement(String uri, String localName, String qName) throws SAXException { switch (qName) { - case Tags.STROLCH_MODEL: - break; case Tags.RESOURCE: this.listener.notifyResource((Resource) this.parameterizedElement); this.statistics.nrOfResources++; this.parameterizedElement = null; + break; + + case Tags.ACTIVITY: + + Activity activity = this.activityStack.pop(); + if (this.activityStack.isEmpty()) { + this.listener.notifyActivity(activity); + this.statistics.nrOfActivities++; + } + this.parameterizedElement = null; + + break; + case Tags.ORDER: + this.listener.notifyOrder((Order) this.parameterizedElement); this.statistics.nrOfOrders++; this.parameterizedElement = null; + break; + + case Tags.ACTION: + + this.parameterizedElement = null; + + break; + case Tags.PARAMETER_BAG: + this.pBag = null; + break; - case Tags.PARAMETER: - break; - case Tags.INCLUDE_FILE: - break; + case Tags.TIMED_STATE: + ((Resource) this.parameterizedElement).addTimedState(this.state); + break; + + case Tags.PARAMETER: + case Tags.INCLUDE_FILE: case Tags.VALUE: + case Tags.VALUE_CHANGE: + case Tags.STROLCH_MODEL: + break; + default: throw new IllegalArgumentException(MessageFormat.format("The element ''{0}'' is unhandled!", qName)); //$NON-NLS-1$ } diff --git a/li.strolch.model/src/test/java/li/strolch/model/ModelTest.java b/li.strolch.model/src/test/java/li/strolch/model/ModelTest.java index 777a8a6dd..98acd84d8 100644 --- a/li.strolch.model/src/test/java/li/strolch/model/ModelTest.java +++ b/li.strolch.model/src/test/java/li/strolch/model/ModelTest.java @@ -86,10 +86,14 @@ import li.strolch.model.visitor.OrderDeepEqualsVisitor; import li.strolch.model.visitor.ResourceDeepEqualsVisitor; import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; @SuppressWarnings("nls") public class ModelTest { + protected static final Logger logger = LoggerFactory.getLogger(ModelTest.class); + @Test public void shouldCreateResource() { diff --git a/li.strolch.model/src/test/java/li/strolch/model/XmlToDomTest.java b/li.strolch.model/src/test/java/li/strolch/model/XmlToDomTest.java index 84f3438f0..8d66c911b 100644 --- a/li.strolch.model/src/test/java/li/strolch/model/XmlToDomTest.java +++ b/li.strolch.model/src/test/java/li/strolch/model/XmlToDomTest.java @@ -18,12 +18,13 @@ package li.strolch.model; import static org.junit.Assert.assertTrue; import li.strolch.model.visitor.OrderDeepEqualsVisitor; import li.strolch.model.visitor.ResourceDeepEqualsVisitor; +import li.strolch.model.xml.OrderFromDomVisitor; import li.strolch.model.xml.OrderToDomVisitor; +import li.strolch.model.xml.ResourceFromDomVisitor; import li.strolch.model.xml.ResourceToDomVisitor; import org.junit.Test; import org.w3c.dom.Document; -import org.w3c.dom.Element; /** * @author Robert von Burg @@ -40,8 +41,7 @@ public class XmlToDomTest extends ModelTest { domVisitor.visit(order); Document document = domVisitor.getDocument(); - Element rootElement = document.getDocumentElement(); - Order parsedOrder = new Order(rootElement); + Order parsedOrder = new OrderFromDomVisitor().visit(document); OrderDeepEqualsVisitor visitor = new OrderDeepEqualsVisitor(order); visitor.visit(parsedOrder); @@ -57,8 +57,7 @@ public class XmlToDomTest extends ModelTest { domVisitor.visit(resource); Document document = domVisitor.getDocument(); - Element rootElement = document.getDocumentElement(); - Resource parsedResource = new Resource(rootElement); + Resource parsedResource = new ResourceFromDomVisitor().visit(document); ResourceDeepEqualsVisitor visitor = new ResourceDeepEqualsVisitor(resource); visitor.visit(parsedResource); diff --git a/li.strolch.model/src/test/java/li/strolch/model/activity/ActionTest.java b/li.strolch.model/src/test/java/li/strolch/model/activity/ActionTest.java index 57633d2ce..a70751bf9 100644 --- a/li.strolch.model/src/test/java/li/strolch/model/activity/ActionTest.java +++ b/li.strolch.model/src/test/java/li/strolch/model/activity/ActionTest.java @@ -18,6 +18,7 @@ import javax.xml.transform.stream.StreamResult; import li.strolch.model.timevalue.IValueChange; import li.strolch.model.timevalue.impl.IntegerValue; import li.strolch.model.timevalue.impl.ValueChange; +import li.strolch.model.xml.ActivityToDomVisitor; import org.junit.Assert; import org.junit.Before; @@ -76,7 +77,7 @@ public class ActionTest { DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document document = db.newDocument(); - Element dom = this.action.toDom(document); + Element dom = new ActivityToDomVisitor().toDom(this.action); document.appendChild(dom); Transformer transformer = TransformerFactory.newInstance().newTransformer(); diff --git a/li.strolch.model/src/test/java/li/strolch/model/activity/ActivityTest.java b/li.strolch.model/src/test/java/li/strolch/model/activity/ActivityTest.java index 16047919b..95bf8847d 100644 --- a/li.strolch.model/src/test/java/li/strolch/model/activity/ActivityTest.java +++ b/li.strolch.model/src/test/java/li/strolch/model/activity/ActivityTest.java @@ -13,6 +13,7 @@ import javax.xml.transform.stream.StreamResult; import li.strolch.exception.StrolchException; import li.strolch.model.State; +import li.strolch.model.xml.ActivityToDomVisitor; import org.junit.Assert; import org.junit.Before; @@ -128,7 +129,7 @@ public class ActivityTest { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document document = db.newDocument(); - Element dom = this.activity.toDom(document); + Element dom = new ActivityToDomVisitor().toDom(this.activity); document.appendChild(dom); Transformer transformer = TransformerFactory.newInstance().newTransformer(); diff --git a/li.strolch.model/src/test/resources/data/activities/Activities.xml b/li.strolch.model/src/test/resources/data/activities/Activities.xml index be58063a8..3efad9741 100644 --- a/li.strolch.model/src/test/resources/data/activities/Activities.xml +++ b/li.strolch.model/src/test/resources/data/activities/Activities.xml @@ -1,12 +1,108 @@ - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/li.strolch.model/src/test/resources/data/resources/Resources.xml b/li.strolch.model/src/test/resources/data/resources/Resources.xml index fcbb0f7d2..a9efa557b 100644 --- a/li.strolch.model/src/test/resources/data/resources/Resources.xml +++ b/li.strolch.model/src/test/resources/data/resources/Resources.xml @@ -14,12 +14,12 @@ - + - + @@ -27,7 +27,7 @@ - + @@ -35,7 +35,7 @@ - + diff --git a/li.strolch.service/src/main/java/li/strolch/command/ClearModelCommand.java b/li.strolch.service/src/main/java/li/strolch/command/ClearModelCommand.java index f2d935c9b..1bbbfb2ea 100644 --- a/li.strolch.service/src/main/java/li/strolch/command/ClearModelCommand.java +++ b/li.strolch.service/src/main/java/li/strolch/command/ClearModelCommand.java @@ -15,6 +15,7 @@ */ package li.strolch.command; +import li.strolch.agent.api.ActivityMap; import li.strolch.agent.api.ComponentContainer; import li.strolch.agent.api.OrderMap; import li.strolch.agent.api.ResourceMap; @@ -30,6 +31,7 @@ public class ClearModelCommand extends Command { // input private boolean clearOrders; private boolean clearResources; + private boolean clearActivities; // output private ModelStatistics statistics; @@ -62,6 +64,11 @@ public class ClearModelCommand extends Command { statistics.nrOfResources = resourceMap.removeAll(tx()); } + if (this.clearActivities) { + ActivityMap activitiesMap = tx().getActivityMap(); + statistics.nrOfActivities = activitiesMap.removeAll(tx()); + } + this.statistics = statistics; } @@ -86,6 +93,14 @@ public class ClearModelCommand extends Command { this.clearResources = clearResources; } + /** + * @param clearActivities + * the clearActivities to set + */ + public void setClearActivities(boolean clearActivities) { + this.clearActivities = clearActivities; + } + /** * @return the statistics */ diff --git a/li.strolch.service/src/main/java/li/strolch/command/XmlExportModelCommand.java b/li.strolch.service/src/main/java/li/strolch/command/XmlExportModelCommand.java index 7f2408b0c..ca67462ea 100644 --- a/li.strolch.service/src/main/java/li/strolch/command/XmlExportModelCommand.java +++ b/li.strolch.service/src/main/java/li/strolch/command/XmlExportModelCommand.java @@ -34,16 +34,19 @@ import javax.xml.stream.XMLOutputFactory; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; +import li.strolch.agent.api.ActivityMap; import li.strolch.agent.api.ComponentContainer; import li.strolch.agent.api.OrderMap; import li.strolch.agent.api.ResourceMap; import li.strolch.exception.StrolchException; +import li.strolch.model.ActivityVisitor; import li.strolch.model.ModelStatistics; import li.strolch.model.Order; import li.strolch.model.OrderVisitor; import li.strolch.model.Resource; import li.strolch.model.ResourceVisitor; import li.strolch.model.Tags; +import li.strolch.model.activity.Activity; import li.strolch.model.xml.OrderToSaxWriterVisitor; import li.strolch.model.xml.ResourceToSaxWriterVisitor; import li.strolch.persistence.api.StrolchTransaction; @@ -262,6 +265,17 @@ public class XmlExportModelCommand extends Command { } } + private void writeActivitiesByType(XMLStreamWriter writer, ActivityMap activityMap, String type) { + ActivityVisitor visitor = new ActivityToSaxWriterVisitor(writer); + Set keysByType = new TreeSet<>(activityMap.getKeysBy(tx(), type)); + for (String id : keysByType) { + Activity activity = activityMap.getBy(tx(), type, id); + visitor.visit(activity); + this.statistics.nrOfActivities++; + logElementsWritten(); + } + } + private XMLStreamWriter openXmlStreamWriter(FileOutputStream out) throws FactoryConfigurationError, XMLStreamException { XMLOutputFactory factory = XMLOutputFactory.newInstance();