[Major] added ClearModelCommand and Service
This commit is contained in:
parent
40de765eca
commit
14d9c66a78
|
@ -0,0 +1,95 @@
|
|||
/*
|
||||
* 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.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 <eitch@eitchnet.ch>
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -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<String> 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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<String> 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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
/*
|
||||
* 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.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;
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
/*
|
||||
* 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.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 <eitch@eitchnet.ch>
|
||||
*/
|
||||
public class ClearModelService extends AbstractService<ClearModelArgument, ServiceResult> {
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
|
@ -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<XmlExportModelArgumen
|
|||
tx.addCommand(command);
|
||||
}
|
||||
|
||||
XmlModelStatistics statistics = command.getStatistics();
|
||||
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()));
|
||||
return ServiceResult.success();
|
||||
|
|
|
@ -20,7 +20,7 @@ import java.text.MessageFormat;
|
|||
|
||||
import li.strolch.command.XmlImportModelCommand;
|
||||
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;
|
||||
|
@ -63,7 +63,7 @@ public class XmlImportModelService extends AbstractService<XmlImportModelArgumen
|
|||
tx.addCommand(command);
|
||||
}
|
||||
|
||||
XmlModelStatistics statistics = command.getStatistics();
|
||||
ModelStatistics statistics = command.getStatistics();
|
||||
String durationS = StringHelper.formatNanoDuration(statistics.durationNanos);
|
||||
logger.info(MessageFormat.format(
|
||||
"Loading XML Model file {0} for realm {1} took {2}.", modelFile.getName(), arg.realm, durationS)); //$NON-NLS-1$
|
||||
|
|
Loading…
Reference in New Issue