diff --git a/src/main/java/li/strolch/command/ClearModelCommand.java b/src/main/java/li/strolch/command/ClearModelCommand.java new file mode 100644 index 000000000..f2d935c9b --- /dev/null +++ b/src/main/java/li/strolch/command/ClearModelCommand.java @@ -0,0 +1,95 @@ +/* + * 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.command; + +import li.strolch.agent.api.ComponentContainer; +import li.strolch.agent.api.OrderMap; +import li.strolch.agent.api.ResourceMap; +import li.strolch.model.ModelStatistics; +import li.strolch.persistence.api.StrolchTransaction; +import li.strolch.service.api.Command; + +/** + * @author Robert von Burg + */ +public class ClearModelCommand extends Command { + + // input + private boolean clearOrders; + private boolean clearResources; + + // output + private ModelStatistics statistics; + + /** + * @param container + * @param tx + */ + public ClearModelCommand(ComponentContainer container, StrolchTransaction tx) { + super(container, tx); + } + + @Override + public void validate() { + // nothing to do + } + + @Override + public void doCommand() { + + ModelStatistics statistics = new ModelStatistics(); + + if (this.clearOrders) { + OrderMap orderMap = tx().getOrderMap(); + statistics.nrOfOrders = orderMap.removeAll(tx()); + } + + if (this.clearResources) { + ResourceMap resourceMap = tx().getResourceMap(); + statistics.nrOfResources = resourceMap.removeAll(tx()); + } + + this.statistics = statistics; + } + + @Override + public void undo() { + logger.warn("Can not undo clearing of model!"); + } + + /** + * @param clearOrders + * the clearOrders to set + */ + public void setClearOrders(boolean clearOrders) { + this.clearOrders = clearOrders; + } + + /** + * @param clearResources + * the clearResources to set + */ + public void setClearResources(boolean clearResources) { + this.clearResources = clearResources; + } + + /** + * @return the statistics + */ + public ModelStatistics getStatistics() { + return this.statistics; + } +} diff --git a/src/main/java/li/strolch/command/XmlExportModelCommand.java b/src/main/java/li/strolch/command/XmlExportModelCommand.java index b5e2c300f..ff8edaaf7 100644 --- a/src/main/java/li/strolch/command/XmlExportModelCommand.java +++ b/src/main/java/li/strolch/command/XmlExportModelCommand.java @@ -37,6 +37,7 @@ 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.ModelStatistics; import li.strolch.model.Order; import li.strolch.model.OrderVisitor; import li.strolch.model.Resource; @@ -44,7 +45,6 @@ import li.strolch.model.ResourceVisitor; import li.strolch.model.Tags; import li.strolch.model.xml.OrderToSaxWriterVisitor; import li.strolch.model.xml.ResourceToSaxWriterVisitor; -import li.strolch.model.xml.XmlModelSaxReader.XmlModelStatistics; import li.strolch.persistence.api.StrolchTransaction; import li.strolch.runtime.StrolchConstants; import li.strolch.service.api.Command; @@ -66,7 +66,7 @@ public class XmlExportModelCommand extends Command { private Set resourceTypes; // output - private XmlModelStatistics statistics; + private ModelStatistics statistics; private boolean multiFile; private int elementsToWrite; @@ -91,7 +91,7 @@ public class XmlExportModelCommand extends Command { String fileName = this.modelFile.getName(); long start = System.nanoTime(); - this.statistics = new XmlModelStatistics(); + this.statistics = new ModelStatistics(); this.statistics.startTime = new Date(); String exportName = fileName.substring(0, fileName.indexOf(XML_FILE_SUFFIX)); @@ -303,7 +303,7 @@ public class XmlExportModelCommand extends Command { /** * @return the statistics */ - public XmlModelStatistics getStatistics() { + public ModelStatistics getStatistics() { return this.statistics; } } diff --git a/src/main/java/li/strolch/command/XmlImportModelCommand.java b/src/main/java/li/strolch/command/XmlImportModelCommand.java index b89798b75..cba03a295 100644 --- a/src/main/java/li/strolch/command/XmlImportModelCommand.java +++ b/src/main/java/li/strolch/command/XmlImportModelCommand.java @@ -20,8 +20,8 @@ import java.util.Set; import li.strolch.agent.api.ComponentContainer; import li.strolch.agent.impl.InMemoryElementListener; +import li.strolch.model.ModelStatistics; import li.strolch.model.xml.XmlModelSaxFileReader; -import li.strolch.model.xml.XmlModelSaxReader.XmlModelStatistics; import li.strolch.persistence.api.StrolchTransaction; import li.strolch.service.api.Command; import ch.eitchnet.utils.dbc.DBC; @@ -41,7 +41,7 @@ public class XmlImportModelCommand extends Command { private Set resourceTypes; // output - private XmlModelStatistics statistics; + private ModelStatistics statistics; /** * @param container @@ -137,7 +137,7 @@ public class XmlImportModelCommand extends Command { /** * @return the statistics */ - public XmlModelStatistics getStatistics() { + public ModelStatistics getStatistics() { return this.statistics; } } diff --git a/src/main/java/li/strolch/service/ClearModelArgument.java b/src/main/java/li/strolch/service/ClearModelArgument.java new file mode 100644 index 000000000..6dd57b921 --- /dev/null +++ b/src/main/java/li/strolch/service/ClearModelArgument.java @@ -0,0 +1,25 @@ +/* + * 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.service; + +import li.strolch.service.api.ServiceArgument; + +public class ClearModelArgument extends ServiceArgument { + private static final long serialVersionUID = 1L; + + public boolean clearOrders = true; + public boolean clearResources = true; +} \ No newline at end of file diff --git a/src/main/java/li/strolch/service/ClearModelService.java b/src/main/java/li/strolch/service/ClearModelService.java new file mode 100644 index 000000000..3f73a93ef --- /dev/null +++ b/src/main/java/li/strolch/service/ClearModelService.java @@ -0,0 +1,60 @@ +/* + * 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.service; + +import java.text.MessageFormat; + +import li.strolch.command.ClearModelCommand; +import li.strolch.model.ModelStatistics; +import li.strolch.persistence.api.StrolchTransaction; +import li.strolch.service.api.AbstractService; +import li.strolch.service.api.ServiceResult; +import ch.eitchnet.utils.helper.StringHelper; + +/** + * @author Robert von Burg + */ +public class ClearModelService extends AbstractService { + + private static final long serialVersionUID = 1L; + + @Override + protected ServiceResult getResultInstance() { + return new ServiceResult(); + } + + @Override + protected ServiceResult internalDoService(ClearModelArgument arg) { + + ClearModelCommand command; + try (StrolchTransaction tx = openTx(arg.realm)) { + + command = new ClearModelCommand(getContainer(), tx); + command.setClearOrders(arg.clearOrders); + command.setClearResources(arg.clearResources); + tx.addCommand(command); + } + + ModelStatistics statistics = command.getStatistics(); + String durationS = StringHelper.formatNanoDuration(statistics.durationNanos); + logger.info(MessageFormat.format( + "Clearing Model for realm {1} took {2}.", arg.realm, durationS)); //$NON-NLS-1$ + logger.info(MessageFormat.format("Cleared {0} Orders", statistics.nrOfOrders)); //$NON-NLS-1$ + logger.info(MessageFormat.format("Cleared {0} Resources", statistics.nrOfResources)); //$NON-NLS-1$ + + return ServiceResult.success(); + } +} diff --git a/src/main/java/li/strolch/service/XmlExportModelService.java b/src/main/java/li/strolch/service/XmlExportModelService.java index bc501925f..6d8ea7418 100644 --- a/src/main/java/li/strolch/service/XmlExportModelService.java +++ b/src/main/java/li/strolch/service/XmlExportModelService.java @@ -20,7 +20,7 @@ import java.text.MessageFormat; import li.strolch.command.XmlExportModelCommand; import li.strolch.exception.StrolchException; -import li.strolch.model.xml.XmlModelSaxReader.XmlModelStatistics; +import li.strolch.model.ModelStatistics; import li.strolch.persistence.api.StrolchTransaction; import li.strolch.service.api.AbstractService; import li.strolch.service.api.ServiceResult; @@ -67,7 +67,7 @@ public class XmlExportModelService extends AbstractService