implemented xml serialization for activity, action and value changes.

This commit is contained in:
msmock 2015-05-23 12:25:26 +02:00
parent 188a8d9461
commit 19c6d0827f
9 changed files with 132 additions and 32 deletions

View File

@ -46,6 +46,12 @@ public class Tags {
public static final String ACTION = "Action"; public static final String ACTION = "Action";
public static final String START = "Start"; public static final String START = "Start";
public static final String END = "End"; 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 class Audit {
public static final String ID = Tags.ID; public static final String ID = Tags.ID;

View File

@ -30,8 +30,7 @@ public class Action extends GroupedParameterizedElement implements IActivityElem
protected static final long serialVersionUID = 1L; protected static final long serialVersionUID = 1L;
protected Activity parent; protected Activity parent;
protected String resourceId; protected String resourceId, resourceType;
private String resourceType;
protected State state = State.CREATED; protected State state = State.CREATED;
protected final List<IValueChange<?>> changes = new ArrayList<>(); protected final List<IValueChange<?>> changes = new ArrayList<>();
@ -104,14 +103,6 @@ public class Action extends GroupedParameterizedElement implements IActivityElem
return changes; return changes;
} }
@Override
public Element toDom(Document doc) {
Element element = doc.createElement(Tags.ACTION);
fillElement(element);
element.setAttribute(Tags.STATE, this.state.toString());
return element;
}
@Override @Override
public StrolchElement getParent() { public StrolchElement getParent() {
return parent; return parent;
@ -177,20 +168,33 @@ public class Action extends GroupedParameterizedElement implements IActivityElem
@Override @Override
public Long getStart() { public Long getStart() {
Long start = Long.MAX_VALUE; Long start = Long.MAX_VALUE;
for (IValueChange<?> change : changes){ for (IValueChange<?> change : changes) {
start = Math.min(start, change.getTime()); start = Math.min(start, change.getTime());
} }
return start; return start;
} }
@Override @Override
public Long getEnd() { public Long getEnd() {
Long end = 0L; Long end = 0L;
for (IValueChange<?> change : changes){ for (IValueChange<?> change : changes) {
end = Math.max(end, change.getTime()); end = Math.max(end, change.getTime());
} }
return end; 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;
} }
} }

View File

@ -146,7 +146,14 @@ public class Activity extends GroupedParameterizedElement implements IActivityEl
@Override @Override
public Element toDom(Document doc) { public Element toDom(Document doc) {
throw new StrolchException("not implemented yet"); Element element = doc.createElement(Tags.ACTIVITY);
fillElement(element);
Iterator<Entry<String, IActivityElement>> elementIterator = elementIterator();
while (elementIterator.hasNext()) {
IActivityElement activityElement = elementIterator.next().getValue();
element.appendChild(activityElement.toDom(doc));
}
return element;
} }
@Override @Override
@ -156,7 +163,7 @@ public class Activity extends GroupedParameterizedElement implements IActivityEl
@Override @Override
public StrolchRootElement getRootElement() { public StrolchRootElement getRootElement() {
return (parent == null) ? null : parent.getRootElement(); return (parent == null) ? this : parent.getRootElement();
} }
@Override @Override

View File

@ -18,6 +18,9 @@ package li.strolch.model.timevalue;
import li.strolch.model.timedstate.AbstractStrolchTimedState; import li.strolch.model.timedstate.AbstractStrolchTimedState;
import li.strolch.model.timevalue.impl.TimeVariable; 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} * Interface for operators to be used to change the values of {@link ITimeValue}
* in a {@link ITimeVariable} or {@link AbstractStrolchTimedState}. * in a {@link ITimeVariable} or {@link AbstractStrolchTimedState}.
@ -64,5 +67,11 @@ public interface IValueChange<T extends IValue> {
* @return a copy of this * @return a copy of this
*/ */
IValueChange<T> getClone(); IValueChange<T> getClone();
/**
* @param doc
* @return a xml serialisation of this
*/
Element toDom(Document doc);
} }

View File

@ -17,9 +17,15 @@ package li.strolch.model.timevalue.impl;
import java.io.Serializable; import java.io.Serializable;
import li.strolch.model.Tags;
import li.strolch.model.timevalue.IValue; import li.strolch.model.timevalue.IValue;
import li.strolch.model.timevalue.IValueChange; 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 <smock.martin@gmail.com> * @author Martin Smock <smock.martin@gmail.com>
*/ */
@ -148,4 +154,14 @@ public class ValueChange<T extends IValue> implements IValueChange<T>, Serializa
return new ValueChange(time, value); return new ValueChange(time, value);
} }
@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;
}
} }

View File

@ -9,7 +9,6 @@ import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer; import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerFactory;
@ -37,6 +36,8 @@ public class ActionTest {
public void init() { public void init() {
// create action // create action
action = new Action("action_1", "Action 1", "Use"); action = new Action("action_1", "Action 1", "Use");
action.setResourceId("dummyRe");
action.setResourceType("dummyReType");
IValueChange<IntegerValue> startChange = new ValueChange<>(STATE_TIME_10, new IntegerValue(1)); IValueChange<IntegerValue> startChange = new ValueChange<>(STATE_TIME_10, new IntegerValue(1));
startChange.setStateId(STATE_INTEGER_ID); startChange.setStateId(STATE_INTEGER_ID);
@ -71,18 +72,15 @@ public class ActionTest {
@Test @Test
public void testToDOM() throws ParserConfigurationException, TransformerException { public void testToDOM() throws ParserConfigurationException, TransformerException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.newDocument(); Document document = db.newDocument();
Element dom = action.toDom(document); Element dom = action.toDom(document);
document.appendChild(dom); document.appendChild(dom);
TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = TransformerFactory.newInstance().newTransformer();
Transformer transformer = tf.newTransformer(); StringWriter stringWriter = new StringWriter();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.transform(new DOMSource(document), new StreamResult(stringWriter));
StringWriter writer = new StringWriter(); String content = stringWriter.getBuffer().toString();
transformer.transform(new DOMSource(document), new StreamResult(writer));
String content = writer.getBuffer().toString();
System.out.println(content); System.out.println(content);
} }

View File

@ -1,11 +1,24 @@
package li.strolch.model.activity; package li.strolch.model.activity;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import li.strolch.exception.StrolchException; import li.strolch.exception.StrolchException;
import li.strolch.model.State; import li.strolch.model.State;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class ActivityTest { public class ActivityTest {
@ -19,11 +32,13 @@ public class ActivityTest {
public void init() { public void init() {
// create activity element // create activity element
activity = new Activity("activity", "Activity", "mayorType"); activity = new Activity("activity", "Activity", "parentType");
// create action 1 // create action 1
action_1 = new Action("action_1", "Action 1", "Use"); action_1 = new Action("action_1", "Action 1", "Use");
action_1.setState(State.CREATED); action_1.setState(State.CREATED);
action_1.setResourceType("dummyType");
action_1.setResourceId("dummyId");
activity.addElement(action_1); activity.addElement(action_1);
@ -32,12 +47,16 @@ public class ActivityTest {
// create action 2 // create action 2
action_2 = new Action("action_2", "Action 2", "Use"); action_2 = new Action("action_2", "Action 2", "Use");
action_2.setState(State.PLANNED); action_2.setState(State.PLANNED);
action_2.setResourceType("dummyType");
action_2.setResourceId("dummyId");
childActivity.addElement(action_2); childActivity.addElement(action_2);
// create action 3 // create action 3
action_3 = new Action("action_3", "Action 3", "Use"); action_3 = new Action("action_3", "Action 3", "Use");
action_3.setState(State.CREATED); action_3.setState(State.CREATED);
action_3.setResourceType("dummyType");
action_3.setResourceId("dummyId");
childActivity.addElement(action_3); childActivity.addElement(action_3);
@ -86,10 +105,38 @@ public class ActivityTest {
} }
@Test @Test
public void getParentTest(){ public void parentTests(){
Assert.assertNull(activity.getParent()); Assert.assertNull(activity.getParent());
Assert.assertNull(activity.getRootElement()); Assert.assertEquals(activity, activity.getRootElement());
Assert.assertTrue(activity.isRootElement()); Assert.assertTrue(activity.isRootElement());
Assert.assertEquals(activity, childActivity.getParent());
Assert.assertEquals(activity, childActivity.getRootElement());
Assert.assertFalse(childActivity.isRootElement());
Assert.assertEquals(childActivity, action_2.getParent());
Assert.assertEquals(activity, action_2.getRootElement());
Assert.assertFalse(action_2.isRootElement());
}
/**
* no test. Just to see the XML serialization in the console
*/
// @Test
public void testToDOM() throws ParserConfigurationException, TransformerException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.newDocument();
Element dom = activity.toDom(document);
document.appendChild(dom);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
StringWriter writer = new StringWriter();
transformer.transform(new DOMSource(document), new StreamResult(writer));
String content = writer.getBuffer().toString();
System.out.println(content);
} }
} }

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Action Id="action_1" Name="Action 1" ResourceId="dummyRe" ResourceType="dummyReType" State="CREATED" Type="Use">
<ValueChange Class="li.strolch.model.timevalue.impl.IntegerValue" StateId="@state2" Time="1970-01-01T01:00:00.010+01:00" Value="1" />
<ValueChange Class="li.strolch.model.timevalue.impl.IntegerValue" StateId="@state2" Time="1970-01-01T01:00:00.030+01:00" Value="-1" />
</Action>

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Activity Id="activity" Name="Activity" Type="parentType">
<Action Id="action_1" Name="Action 1" ResourceId="dummyId" ResourceType="dummyType" State="CREATED" Type="Use" />
<Activity Id="child_activity" Name="Child Activity" Type="childType">
<Action Id="action_2" Name="Action 2" ResourceId="dummyId" ResourceType="dummyType" State="PLANNED" Type="Use" />
<Action Id="action_3" Name="Action 3" ResourceId="dummyId" ResourceType="dummyType" State="CREATED" Type="Use" />
</Activity>
</Activity>