[Major] Extended XmlExportModel XmlImportModel for Activities

This commit is contained in:
Robert von Burg 2015-07-08 07:42:53 +02:00
parent 588952b526
commit 5fe84b1d02
8 changed files with 148 additions and 20 deletions

View File

@ -47,6 +47,7 @@ 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.ActivityToSaxWriterVisitor;
import li.strolch.model.xml.OrderToSaxWriterVisitor;
import li.strolch.model.xml.ResourceToSaxWriterVisitor;
import li.strolch.persistence.api.StrolchTransaction;
@ -68,8 +69,10 @@ public class XmlExportModelCommand extends Command {
private boolean overwrite;
private boolean doOrders;
private boolean doResources;
private boolean doActivities;
private Set<String> orderTypes;
private Set<String> resourceTypes;
private Set<String> activityTypes;
// output
private ModelStatistics statistics;
@ -77,6 +80,7 @@ public class XmlExportModelCommand extends Command {
private int elementsToWrite;
private int nrOfResourcesToExport;
private int nrOfOrdersToExport;
private int nrOfActivitiesToExport;
private long nextLogTime;
@ -140,10 +144,22 @@ public class XmlExportModelCommand extends Command {
}
}
this.elementsToWrite = this.nrOfResourcesToExport + this.nrOfOrdersToExport;
if (this.doActivities) {
ActivityMap activityMap = tx().getActivityMap();
Set<String> activityTypesToExport = activityMap.getTypes(tx());
if (!this.activityTypes.isEmpty())
activityTypesToExport.retainAll(this.activityTypes);
for (String type : activityTypesToExport) {
this.nrOfActivitiesToExport += activityMap.querySize(tx(), type);
}
}
this.elementsToWrite = this.nrOfResourcesToExport + this.nrOfOrdersToExport + this.nrOfActivitiesToExport;
logger.info("Exporting " + this.elementsToWrite + " Elements...");
logger.info("Exporting " + this.nrOfResourcesToExport + " Resources...");
logger.info("Exporting " + this.nrOfOrdersToExport + " Orders...");
logger.info("Exporting " + this.nrOfActivitiesToExport + " Activities...");
try (FileOutputStream out = new FileOutputStream(this.modelFile)) {
createdFiles.add(this.modelFile);
@ -152,11 +168,10 @@ public class XmlExportModelCommand extends Command {
if (this.doResources) {
ResourceMap resourceMap = tx().getResourceMap();
Set<String> resourceTypesToExport = resourceMap.getTypes(tx());
Set<String> resourceTypesToExport = new TreeSet<>(resourceMap.getTypes(tx()));
if (!this.resourceTypes.isEmpty())
resourceTypesToExport.retainAll(this.resourceTypes);
resourceTypesToExport = new TreeSet<>(resourceTypesToExport);
for (String type : resourceTypesToExport) {
if (!this.multiFile) {
@ -183,11 +198,10 @@ public class XmlExportModelCommand extends Command {
if (this.doOrders) {
OrderMap orderMap = tx().getOrderMap();
Set<String> orderTypesToExport = orderMap.getTypes(tx());
Set<String> orderTypesToExport = new TreeSet<>(orderMap.getTypes(tx()));
if (!this.orderTypes.isEmpty())
orderTypesToExport.retainAll(this.orderTypes);
orderTypesToExport = new TreeSet<>(orderTypesToExport);
for (String type : orderTypesToExport) {
if (!this.multiFile) {
@ -211,6 +225,36 @@ public class XmlExportModelCommand extends Command {
}
}
if (this.doActivities) {
ActivityMap activityMap = tx().getActivityMap();
Set<String> activityTypesToExport = new TreeSet<>(activityMap.getTypes(tx()));
if (!this.activityTypes.isEmpty())
activityTypesToExport.retainAll(this.activityTypes);
for (String type : activityTypesToExport) {
if (!this.multiFile) {
writeActivitiesByType(writer, activityMap, type);
} else {
String typeXmlFile = exportName + UNDERLINE + Tags.ACTIVITY + UNDERLINE + type
+ XML_FILE_SUFFIX;
writer.writeEmptyElement(Tags.INCLUDE_FILE);
writer.writeAttribute(Tags.FILE, typeXmlFile);
File typeXmlFileF = new File(this.modelFile.getParentFile(), typeXmlFile);
DBC.INTERIM.assertNotExists("The type file should not exist with name.", typeXmlFileF);
logger.info("Writing " + activityMap.querySize(tx(), type) + " " + type
+ " Activities to path: " + typeXmlFileF.getAbsolutePath() + "...");
try (FileOutputStream typeOut = new FileOutputStream(typeXmlFileF)) {
createdFiles.add(typeXmlFileF);
XMLStreamWriter typeWriter = openXmlStreamWriter(typeOut);
writeActivitiesByType(typeWriter, activityMap, type);
typeWriter.writeEndDocument();
}
}
}
}
// and now end
writer.writeEndElement();
writer.writeEndDocument();
@ -319,6 +363,14 @@ public class XmlExportModelCommand extends Command {
this.doResources = doResources;
}
/**
* @param doActivities
* the doActivities to set
*/
public void setDoActivities(boolean doActivities) {
this.doActivities = doActivities;
}
/**
* @param orderTypes
* the orderTypes to set
@ -335,6 +387,14 @@ public class XmlExportModelCommand extends Command {
this.resourceTypes = resourceTypes;
}
/**
* @param activityTypes
* the activityTypes to set
*/
public void setActivityTypes(Set<String> activityTypes) {
this.activityTypes = activityTypes;
}
/**
* @return the statistics
*/

View File

@ -35,10 +35,13 @@ public class XmlImportModelCommand extends Command {
private File modelFile;
private boolean addOrders;
private boolean addResources;
private boolean addActivities;
private boolean updateOrders;
private boolean updateResources;
private boolean updateActivities;
private Set<String> orderTypes;
private Set<String> resourceTypes;
private Set<String> activityTypes;
// output
private ModelStatistics statistics;
@ -64,10 +67,13 @@ public class XmlImportModelCommand extends Command {
elementListener.setAddOrders(this.addOrders);
elementListener.setAddResources(this.addResources);
elementListener.setAddActivities(this.addActivities);
elementListener.setUpdateOrders(this.updateOrders);
elementListener.setUpdateResources(this.updateResources);
elementListener.setUpdateActivities(this.updateActivities);
elementListener.setOrderTypes(this.orderTypes);
elementListener.setResourceTypes(this.resourceTypes);
elementListener.setActivityTypes(this.activityTypes);
XmlModelSaxFileReader handler = new XmlModelSaxFileReader(elementListener, this.modelFile, this.allowInclude);
handler.parseFile();
@ -110,6 +116,14 @@ public class XmlImportModelCommand extends Command {
this.addResources = addResources;
}
/**
* @param addActivities
* the addActivities to set
*/
public void setAddActivities(boolean addActivities) {
this.addActivities = addActivities;
}
/**
* @param updateOrders
* the updateOrders to set
@ -126,6 +140,14 @@ public class XmlImportModelCommand extends Command {
this.updateResources = updateResources;
}
/**
* @param updateActivities
* the updateActivities to set
*/
public void setUpdateActivities(boolean updateActivities) {
this.updateActivities = updateActivities;
}
/**
* @param orderTypes
* the orderTypes to set
@ -142,6 +164,14 @@ public class XmlImportModelCommand extends Command {
this.resourceTypes = resourceTypes;
}
/**
* @param activityTypes
* the activityTypes to set
*/
public void setActivityTypes(Set<String> activityTypes) {
this.activityTypes = activityTypes;
}
/**
* @return the statistics
*/

View File

@ -29,6 +29,7 @@ import li.strolch.model.activity.Action;
import li.strolch.model.activity.Activity;
import li.strolch.model.activity.IActivityElement;
import li.strolch.model.timedstate.StrolchTimedState;
import li.strolch.model.timevalue.IValue;
import li.strolch.model.timevalue.IValueChange;
import li.strolch.persistence.api.StrolchTransaction;
import li.strolch.service.api.Command;
@ -47,16 +48,16 @@ public abstract class AbstractPlanCommand extends Command {
}
/**
* plan an {@link Action}.It iterates the {@link IValueChange} operators and
* registers the changes on the {@link StrolchTimedState} objects assigned
* to the {@link Resource} referenced by type and id.
* plan an {@link Action}.It iterates the {@link IValueChange} operators and registers the changes on the
* {@link StrolchTimedState} objects assigned to the {@link Resource} referenced by type and id.
*
* @param action
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
protected void plan(final Action action) {
final Locator locator = Locator.newBuilder(Tags.RESOURCE, action.getResourceType(), action.getResourceId()).build();
final Locator locator = Locator.newBuilder(Tags.RESOURCE, action.getResourceType(), action.getResourceId())
.build();
final Resource resource = tx().findElement(locator);
if (resource == null)
@ -65,7 +66,7 @@ public abstract class AbstractPlanCommand extends Command {
tx().lock(resource);
final List<IValueChange<?>> changes = action.getChanges();
final List<IValueChange<? extends IValue<?>>> changes = action.getChanges();
for (final IValueChange<?> change : changes) {
final StrolchTimedState timedState = resource.getTimedState(change.getStateId());
timedState.applyChange(change);
@ -75,10 +76,12 @@ public abstract class AbstractPlanCommand extends Command {
}
/**
* plan an {@link Activity} by navigating to the {#link Action} and
* delegating the planning depending on the {@link IActivityElement} class.
* plan an {@link Activity} by navigating to the {#link Action} and delegating the planning depending on the
* {@link IActivityElement} class.
*/
protected void plan(final Activity activity) {
// TODO Martin: Use a visitor pattern so we don't start with instanceof again...
final Iterator<Entry<String, IActivityElement>> elementIterator = activity.elementIterator();
@ -94,10 +97,11 @@ public abstract class AbstractPlanCommand extends Command {
@SuppressWarnings({ "unchecked", "rawtypes" })
protected void unplan(final Action action) {
final Locator locator = Locator.newBuilder(Tags.RESOURCE, action.getResourceType(), action.getResourceId()).build();
final Locator locator = Locator.newBuilder(Tags.RESOURCE, action.getResourceType(), action.getResourceId())
.build();
final Resource resource = tx().findElement(locator);
final List<IValueChange<?>> changes = action.getChanges();
final List<IValueChange<? extends IValue<?>>> changes = action.getChanges();
for (final IValueChange<?> change : changes) {
final StrolchTimedState timedState = resource.getTimedState(change.getStateId());
timedState.applyChange(change.getInverse());
@ -117,8 +121,6 @@ public abstract class AbstractPlanCommand extends Command {
unplan((Activity) activityElement);
else if (activityElement instanceof Action)
unplan((Action) activityElement);
}
}
}

View File

@ -19,6 +19,7 @@ import java.util.List;
import li.strolch.agent.api.ComponentContainer;
import li.strolch.model.State;
import li.strolch.model.timevalue.IValue;
import li.strolch.model.timevalue.IValueChange;
import li.strolch.persistence.api.StrolchTransaction;
import ch.eitchnet.utils.dbc.DBC;
@ -56,7 +57,7 @@ public class ShiftActionCommand extends PlanActionCommand {
unplan(action);
// iterate all changes and shift
final List<IValueChange<?>> changes = action.getChanges();
final List<IValueChange<? extends IValue<?>>> changes = action.getChanges();
for (final IValueChange<?> change : changes) {
change.setTime(change.getTime() + shift);
}
@ -74,7 +75,7 @@ public class ShiftActionCommand extends PlanActionCommand {
unplan(action);
// iterate all changes and shift
final List<IValueChange<?>> changes = action.getChanges();
final List<IValueChange<? extends IValue<?>>> changes = action.getChanges();
for (final IValueChange<?> change : changes) {
change.setTime(change.getTime() - shift);
}
@ -86,5 +87,4 @@ public class ShiftActionCommand extends PlanActionCommand {
public void setShift(Long shift) {
this.shift = shift;
}
}

View File

@ -29,8 +29,10 @@ public class XmlExportModelArgument extends ServiceArgument {
public boolean multiFile;
public boolean doOrders = true;
public boolean doResources = true;
public boolean doActivities = true;
public Set<String> orderTypes = new HashSet<>();
public Set<String> resourceTypes = new HashSet<>();
public Set<String> activityTypes = new HashSet<>();
@Override
public String toString() {
@ -52,6 +54,8 @@ public class XmlExportModelArgument extends ServiceArgument {
builder.append(", resources");
if (this.doOrders)
builder.append(", orders");
if (this.doActivities)
builder.append(", activities");
if (this.resourceTypes != null && !this.resourceTypes.isEmpty()) {
builder.append(", resourceTypes=");
@ -59,6 +63,7 @@ public class XmlExportModelArgument extends ServiceArgument {
} else {
builder.append(", resourceTypes=*");
}
if (this.orderTypes != null && !this.orderTypes.isEmpty()) {
builder.append(", orderTypes=");
builder.append(this.orderTypes);
@ -66,6 +71,13 @@ public class XmlExportModelArgument extends ServiceArgument {
builder.append(", orderTypes=*");
}
if (this.activityTypes != null && !this.activityTypes.isEmpty()) {
builder.append(", activityTypes=");
builder.append(this.activityTypes);
} else {
builder.append(", activityTypes=*");
}
builder.append("]");
return builder.toString();
}

View File

@ -83,8 +83,10 @@ public class XmlExportModelService extends AbstractService<XmlExportModelArgumen
command.setMultiFile(arg.multiFile);
command.setDoOrders(arg.doOrders);
command.setDoResources(arg.doResources);
command.setDoActivities(arg.doActivities);
command.setOrderTypes(arg.orderTypes);
command.setResourceTypes(arg.resourceTypes);
command.setActivityTypes(arg.activityTypes);
tx.addCommand(command);
tx.commitOnClose();
@ -93,6 +95,9 @@ public class XmlExportModelService extends AbstractService<XmlExportModelArgumen
ModelStatistics statistics = command.getStatistics();
String msg = "Wrote XML Model file {0} for realm {1}: {2} at path: {3}";
logger.info(MessageFormat.format(msg, modelFile.getName(), arg.realm, statistics, modelFile.getAbsolutePath()));
logger.info(MessageFormat.format("Wrote {0} Orders", statistics.nrOfOrders)); //$NON-NLS-1$
logger.info(MessageFormat.format("Wrote {0} Resources", statistics.nrOfResources)); //$NON-NLS-1$
logger.info(MessageFormat.format("Wrote {0} Activities", statistics.nrOfActivities)); //$NON-NLS-1$
return ServiceResult.success();
}
}

View File

@ -28,10 +28,13 @@ public class XmlImportModelArgument extends ServiceArgument {
public boolean allowInclude = true;
public boolean addOrders = true;
public boolean addResources = true;
public boolean addActivities = true;
public boolean updateOrders = true;
public boolean updateResources = true;
public boolean updateActivities = true;
public Set<String> orderTypes = new HashSet<>();
public Set<String> resourceTypes = new HashSet<>();
public Set<String> activityTypes = new HashSet<>();
@Override
public String toString() {
@ -40,7 +43,7 @@ public class XmlImportModelArgument extends ServiceArgument {
builder.append("external=");
builder.append(this.external);
builder.append(", allowInclude=");
builder.append(this.allowInclude);
@ -51,11 +54,15 @@ public class XmlImportModelArgument extends ServiceArgument {
builder.append(", addOrders");
if (this.addResources)
builder.append(", addResources");
if (this.addActivities)
builder.append(", addActivities");
if (this.updateOrders)
builder.append(", updateOrders");
if (this.updateResources)
builder.append(", updateResources");
if (this.updateActivities)
builder.append(", updateActivities");
if (this.resourceTypes != null && !this.resourceTypes.isEmpty()) {
builder.append(", resourceTypes=");
@ -63,6 +70,7 @@ public class XmlImportModelArgument extends ServiceArgument {
} else {
builder.append(", resourceTypes=*");
}
if (this.orderTypes != null && !this.orderTypes.isEmpty()) {
builder.append(", orderTypes=");
builder.append(this.orderTypes);
@ -70,6 +78,13 @@ public class XmlImportModelArgument extends ServiceArgument {
builder.append(", orderTypes=*");
}
if (this.activityTypes != null && !this.activityTypes.isEmpty()) {
builder.append(", activityTypes=");
builder.append(this.activityTypes);
} else {
builder.append(", activityTypes=*");
}
builder.append("]");
return builder.toString();
}

View File

@ -76,10 +76,13 @@ public class XmlImportModelService extends AbstractService<XmlImportModelArgumen
command.setAllowInclude(arg.allowInclude);
command.setAddOrders(arg.addOrders);
command.setAddResources(arg.addResources);
command.setAddActivities(arg.addActivities);
command.setUpdateOrders(arg.updateOrders);
command.setUpdateResources(arg.updateResources);
command.setUpdateActivities(arg.addActivities);
command.setOrderTypes(arg.orderTypes);
command.setResourceTypes(arg.resourceTypes);
command.setActivityTypes(arg.activityTypes);
tx.addCommand(command);
tx.commitOnClose();
@ -91,6 +94,7 @@ public class XmlImportModelService extends AbstractService<XmlImportModelArgumen
"Loading XML Model file {0} for realm {1} took {2}.", modelFile.getName(), arg.realm, durationS)); //$NON-NLS-1$
logger.info(MessageFormat.format("Loaded {0} Orders", statistics.nrOfOrders)); //$NON-NLS-1$
logger.info(MessageFormat.format("Loaded {0} Resources", statistics.nrOfResources)); //$NON-NLS-1$
logger.info(MessageFormat.format("Loaded {0} Activities", statistics.nrOfActivities)); //$NON-NLS-1$
return new XmlImportModelResult(statistics);
}