[New] Implemented Export and Import model services and commands

This commit is contained in:
Robert von Burg 2014-01-30 00:21:13 +01:00
parent 9fda376697
commit 743e1c039b
8 changed files with 220 additions and 13 deletions

View File

@ -186,6 +186,15 @@ public abstract class GroupedParameterizedElement extends AbstractStrolchElement
return this.parameterBagMap.remove(key);
}
/**
* Returns true if this {@link GroupedParameterizedElement} has any {@link ParameterBag ParameterBag}
*
* @return true if this {@link GroupedParameterizedElement} has any {@link ParameterBag ParameterBag}
*/
public boolean hasParameterBags() {
return this.parameterBagMap != null && !this.parameterBagMap.isEmpty();
}
/**
* Returns true if the {@link ParameterBag} with the given key exists on this {@link GroupedParameterizedElement}.
*
@ -194,7 +203,7 @@ public abstract class GroupedParameterizedElement extends AbstractStrolchElement
* @return true if the {@link ParameterBag} with the given key exists on this {@link GroupedParameterizedElement}.
*/
public boolean hasParameterBag(String bagKey) {
return this.parameterBagMap != null && this.parameterBagMap.containsKey(bagKey);
return this.parameterBagMap != null && this.parameterBagMap.containsKey(bagKey);
}
/**

View File

@ -165,6 +165,15 @@ public abstract class ParameterizedElement extends AbstractStrolchElement {
return new ArrayList<Parameter<?>>(this.parameterMap.values());
}
/**
* Returns true, if the this {@link ParameterizedElement} has any {@link Parameter Parameters}, false otherwise
*
* @return true, if the this {@link ParameterizedElement} has any {@link Parameter Parameters}, false otherwise
*/
public boolean hasParameters() {
return this.parameterMap != null && !this.parameterMap.isEmpty();
}
/**
* Returns true, if the {@link Parameter} exists with the given key, false otherwise
*
@ -261,8 +270,8 @@ public abstract class ParameterizedElement extends AbstractStrolchElement {
super.fillClone(clone);
ParameterizedElement peClone = (ParameterizedElement) clone;
peClone.setType(this.type);
if(this.parameterMap != null) {
for(Parameter<?> param : this.parameterMap.values()) {
if (this.parameterMap != null) {
for (Parameter<?> param : this.parameterMap.values()) {
peClone.addParameter(param.getClone());
}
}

View File

@ -0,0 +1,96 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* 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.Set;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import li.strolch.model.GroupedParameterizedElement;
import li.strolch.model.ParameterBag;
import li.strolch.model.ParameterizedElement;
import li.strolch.model.StrolchElement;
import li.strolch.model.Tags;
import li.strolch.model.parameter.Parameter;
import ch.eitchnet.utils.helper.StringHelper;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public abstract class AbstractToSaxWriterVisitor {
protected XMLStreamWriter writer;
public AbstractToSaxWriterVisitor(XMLStreamWriter writer) {
this.writer = writer;
}
protected void writeElement(String tag, GroupedParameterizedElement element) throws XMLStreamException {
boolean isEmpty = !element.hasParameterBags();
writeStartStrolchElement(tag, isEmpty, element);
if (!isEmpty) {
writeParameterBags(element);
writer.writeEndElement();
}
}
protected void writeStartStrolchElement(String tag, boolean empty, StrolchElement element)
throws XMLStreamException {
if (empty)
writer.writeEmptyElement(tag);
else
writer.writeStartElement(tag);
writer.writeAttribute(Tags.ID, element.getId());
if (!StringHelper.isEmpty(element.getName()))
writer.writeAttribute(Tags.NAME, element.getName());
writer.writeAttribute(Tags.TYPE, element.getType());
}
protected void writeParameters(ParameterizedElement element) throws XMLStreamException {
Set<String> parameterKeySet = element.getParameterKeySet();
for (String paramKey : parameterKeySet) {
Parameter<?> parameter = element.getParameter(paramKey);
writeStartStrolchElement(Tags.PARAMETER, true, parameter);
if (!Parameter.INTERPRETATION_NONE.equals(parameter.getInterpretation()))
writer.writeAttribute(Tags.INTERPRETATION, parameter.getInterpretation());
if (!Parameter.UOM_NONE.equals(parameter.getUom()))
writer.writeAttribute(Tags.UOM, parameter.getUom());
if (parameter.isHidden())
writer.writeAttribute(Tags.HIDDEN, Boolean.toString(parameter.isHidden()));
writer.writeAttribute(Tags.VALUE, parameter.getValueAsString());
}
}
protected void writeParameterBags(GroupedParameterizedElement element) throws XMLStreamException {
Set<String> bagKeySet = element.getParameterBagKeySet();
for (String bagKey : bagKeySet) {
ParameterBag parameterBag = element.getParameterBag(bagKey);
boolean isEmpty = !parameterBag.hasParameters();
writeStartStrolchElement(Tags.PARAMETER_BAG, isEmpty, parameterBag);
if (!isEmpty) {
writeParameters(parameterBag);
writer.writeEndElement();
}
}
}
}

View File

@ -0,0 +1,47 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* 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.Order;
import li.strolch.model.OrderVisitor;
import li.strolch.model.Tags;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class OrderToSaxWriterVisitor extends AbstractToSaxWriterVisitor implements OrderVisitor {
public OrderToSaxWriterVisitor(XMLStreamWriter writer) {
super(writer);
}
@Override
public void visit(Order order) {
try {
writeElement(Tags.ORDER, order);
} catch (XMLStreamException e) {
String msg = "Failed to write Order {0} due to {1}";
msg = MessageFormat.format(msg, order.getLocator(), e.getMessage());
throw new StrolchException(msg, e);
}
}
}

View File

@ -0,0 +1,47 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* 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.Resource;
import li.strolch.model.ResourceVisitor;
import li.strolch.model.Tags;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class ResourceToSaxWriterVisitor extends AbstractToSaxWriterVisitor implements ResourceVisitor {
public ResourceToSaxWriterVisitor(XMLStreamWriter writer) {
super(writer);
}
@Override
public void visit(Resource resource) {
try {
writeElement(Tags.RESOURCE, resource);
} catch (XMLStreamException e) {
String msg = "Failed to write Resource {0} due to {1}";
msg = MessageFormat.format(msg, resource.getLocator(), e.getMessage());
throw new StrolchException(msg, e);
}
}
}

View File

@ -36,7 +36,7 @@ import ch.eitchnet.utils.helper.StringHelper;
* @author Robert von Burg <eitch@eitchnet.ch>
*
*/
public class XmlModelFileHandler extends XmlModelDefaultHandler {
public class XmlModelSaxFileReader extends XmlModelSaxReader {
private File modelFile;
@ -44,7 +44,7 @@ public class XmlModelFileHandler extends XmlModelDefaultHandler {
* @param listener
* @param modelFile
*/
public XmlModelFileHandler(StrolchElementListener listener, File modelFile) {
public XmlModelSaxFileReader(StrolchElementListener listener, File modelFile) {
super(listener);
this.modelFile = modelFile;
}
@ -67,7 +67,7 @@ public class XmlModelFileHandler extends XmlModelDefaultHandler {
throw new IllegalArgumentException(msg);
}
XmlModelFileHandler handler = new XmlModelFileHandler(this.listener, includeFile);
XmlModelSaxFileReader handler = new XmlModelSaxFileReader(this.listener, includeFile);
handler.parseFile();
this.statistics.nrOfOrders += handler.statistics.nrOfOrders;
this.statistics.nrOfResources += handler.statistics.nrOfResources;

View File

@ -44,11 +44,10 @@ import ch.eitchnet.utils.iso8601.ISO8601FormatFactory;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*
*/
public class XmlModelDefaultHandler extends DefaultHandler {
public class XmlModelSaxReader extends DefaultHandler {
protected static final Logger logger = LoggerFactory.getLogger(XmlModelDefaultHandler.class);
protected static final Logger logger = LoggerFactory.getLogger(XmlModelSaxReader.class);
protected StrolchElementListener listener;
protected XmlModelStatistics statistics;
@ -56,7 +55,7 @@ public class XmlModelDefaultHandler extends DefaultHandler {
private GroupedParameterizedElement parameterizedElement;
private ParameterBag pBag;
public XmlModelDefaultHandler(StrolchElementListener listener) {
public XmlModelSaxReader(StrolchElementListener listener) {
this.listener = listener;
this.statistics = new XmlModelStatistics();
}

View File

@ -22,8 +22,8 @@ import java.util.HashMap;
import java.util.Map;
import li.strolch.model.xml.StrolchElementListener;
import li.strolch.model.xml.XmlModelDefaultHandler.XmlModelStatistics;
import li.strolch.model.xml.XmlModelFileHandler;
import li.strolch.model.xml.XmlModelSaxReader.XmlModelStatistics;
import li.strolch.model.xml.XmlModelSaxFileReader;
import org.junit.Test;
import org.slf4j.Logger;
@ -58,7 +58,7 @@ public class XmlModelDefaultHandlerTest {
orderMap.put(order.getId(), order);
}
};
XmlModelFileHandler handler = new XmlModelFileHandler(listener, file);
XmlModelSaxFileReader handler = new XmlModelSaxFileReader(listener, file);
handler.parseFile();
assertEquals(3, resourceMap.size());