[New][Devel] Work in progress of implementing ActivityMap

- Creating new maps for transient, transactional and cached mode
- adding to Realms; including initialization
- adding new methods to transactions e.g. getActivityBy(), etc.
- Adding new ActivityDao with InMemory implementation
- extending PersistenceHandler
- fixed compile errors in the rest of the project

Still missing is the implementation in persistence handler and fixing
the tests. Currently no tests were run, just trying to fix compile
errors
This commit is contained in:
Robert von Burg 2015-06-28 20:42:42 +02:00
parent 3a197d6629
commit 82ce5a7261
41 changed files with 1106 additions and 158 deletions

View File

@ -0,0 +1,31 @@
/*
* Copyright 2015 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.agent.api;
import java.util.List;
import li.strolch.model.ActivityVisitor;
import li.strolch.model.activity.Activity;
import li.strolch.model.query.ActivityQuery;
import li.strolch.persistence.api.StrolchTransaction;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public interface ActivityMap extends ElementMap<Activity> {
public <U> List<U> doQuery(StrolchTransaction tx, ActivityQuery query, ActivityVisitor<U> activityVisitor);
}

View File

@ -0,0 +1,57 @@
/*
* Copyright 2015 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.agent.impl;
import java.util.List;
import li.strolch.agent.api.ActivityMap;
import li.strolch.agent.api.AuditTrail;
import li.strolch.agent.api.ElementMap;
import li.strolch.model.ActivityVisitor;
import li.strolch.model.activity.Activity;
import li.strolch.model.query.ActivityQuery;
import li.strolch.persistence.api.StrolchTransaction;
/**
* This is the {@link AuditTrail} for {@link Activity Activities}
*
* @author Robert von Burg <eitch@eitchnet.ch>
*
* @see AuditingElementMapFacade
*/
public class AuditingActivityMap extends AuditingElementMapFacade<Activity> implements ActivityMap {
/**
* @param elementMap
*/
public AuditingActivityMap(ElementMap<Activity> elementMap, boolean observeAccessReads) {
super(elementMap, observeAccessReads);
}
@Override
protected ActivityMap getElementMap() {
return (ActivityMap) super.getElementMap();
}
@Override
public <U> List<U> doQuery(StrolchTransaction tx, ActivityQuery query, ActivityVisitor<U> activityVisitor) {
List<U> result = getElementMap().doQuery(tx, query, activity -> {
this.read.add(activity);
return activityVisitor.visit(activity);
});
return result;
}
}

View File

@ -0,0 +1,59 @@
/*
* Copyright 2015 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.agent.impl;
import static li.strolch.model.StrolchModelConstants.INTERPRETATION_ACTIVITY_REF;
import java.util.List;
import li.strolch.agent.api.ActivityMap;
import li.strolch.model.ActivityVisitor;
import li.strolch.model.activity.Activity;
import li.strolch.model.parameter.Parameter;
import li.strolch.model.query.ActivityQuery;
import li.strolch.persistence.api.ActivityDao;
import li.strolch.persistence.api.StrolchTransaction;
import li.strolch.persistence.inmemory.InMemoryActivityDao;
public class CachedActivityMap extends CachedElementMap<Activity> implements ActivityMap {
private ActivityDao cachedDao;
public CachedActivityMap() {
super();
this.cachedDao = new InMemoryActivityDao();
}
@Override
protected void assertIsRefParam(Parameter<?> refP) {
ElementMapHelpers.assertIsRefParam(INTERPRETATION_ACTIVITY_REF, refP);
}
@Override
protected ActivityDao getDbDao(StrolchTransaction tx) {
return tx.getPersistenceHandler().getActivityDao(tx);
}
@Override
public ActivityDao getCachedDao() {
return this.cachedDao;
}
@Override
public <U> List<U> doQuery(StrolchTransaction tx, ActivityQuery query, ActivityVisitor<U> activityVisitor) {
return getCachedDao().doQuery(query, activityVisitor);
}
}

View File

@ -16,13 +16,10 @@
package li.strolch.agent.impl;
import static li.strolch.model.StrolchModelConstants.INTERPRETATION_ORDER_REF;
import static li.strolch.model.StrolchModelConstants.UOM_NONE;
import java.text.MessageFormat;
import java.util.List;
import li.strolch.agent.api.OrderMap;
import li.strolch.exception.StrolchException;
import li.strolch.model.Order;
import li.strolch.model.OrderVisitor;
import li.strolch.model.parameter.Parameter;
@ -42,18 +39,7 @@ public class CachedOrderMap extends CachedElementMap<Order> implements OrderMap
@Override
protected void assertIsRefParam(Parameter<?> refP) {
String interpretation = refP.getInterpretation();
if (!interpretation.equals(INTERPRETATION_ORDER_REF)) {
String msg = "{0} is not an Order reference as its interpretation is not {1} it is {2}"; //$NON-NLS-1$
throw new StrolchException(MessageFormat.format(msg, refP.getLocator(), INTERPRETATION_ORDER_REF,
interpretation));
}
if (refP.getUom().equals(UOM_NONE)) {
String msg = "{0} is not an Order reference as its UOM is not set to a type!"; //$NON-NLS-1$
throw new StrolchException(MessageFormat.format(msg, refP.getLocator()));
}
ElementMapHelpers.assertIsRefParam(INTERPRETATION_ORDER_REF, refP);
}
@Override

View File

@ -19,12 +19,15 @@ import java.text.MessageFormat;
import java.util.List;
import java.util.Set;
import li.strolch.agent.api.ActivityMap;
import li.strolch.agent.api.AuditTrail;
import li.strolch.agent.api.ComponentContainer;
import li.strolch.agent.api.OrderMap;
import li.strolch.agent.api.ResourceMap;
import li.strolch.model.Order;
import li.strolch.model.Resource;
import li.strolch.model.activity.Activity;
import li.strolch.persistence.api.ActivityDao;
import li.strolch.persistence.api.OrderDao;
import li.strolch.persistence.api.PersistenceHandler;
import li.strolch.persistence.api.ResourceDao;
@ -43,6 +46,7 @@ public class CachedRealm extends InternalStrolchRealm {
private PersistenceHandler persistenceHandler;
private CachedResourceMap resourceMap;
private CachedOrderMap orderMap;
private CachedActivityMap activityMap;
private AuditTrail auditTrail;
public CachedRealm(String realm) {
@ -76,6 +80,11 @@ public class CachedRealm extends InternalStrolchRealm {
return this.orderMap;
}
@Override
public ActivityMap getActivityMap() {
return this.activityMap;
}
@Override
public AuditTrail getAuditTrail() {
return this.auditTrail;
@ -88,6 +97,7 @@ public class CachedRealm extends InternalStrolchRealm {
this.persistenceHandler = container.getComponent(PersistenceHandler.class);
this.resourceMap = new CachedResourceMap();
this.orderMap = new CachedOrderMap();
this.activityMap = new CachedActivityMap();
if (isAuditTrailEnabled()) {
this.auditTrail = new CachedAuditTrail();
@ -104,6 +114,7 @@ public class CachedRealm extends InternalStrolchRealm {
long start = System.nanoTime();
int nrOfOrders = 0;
int nrOfResources = 0;
int nrOfActivities = 0;
try (StrolchTransaction tx = openTx(privilegeContext.getCertificate(), DefaultRealmHandler.AGENT_BOOT)) {
ResourceDao resourceDao = tx.getPersistenceHandler().getResourceDao(tx);
@ -133,11 +144,26 @@ public class CachedRealm extends InternalStrolchRealm {
tx.commitOnClose();
}
try (StrolchTransaction tx = openTx(privilegeContext.getCertificate(), DefaultRealmHandler.AGENT_BOOT)) {
ActivityDao activityDao = tx.getPersistenceHandler().getActivityDao(tx);
Set<String> activityTypes = activityDao.queryTypes();
for (String type : activityTypes) {
List<Activity> activities = activityDao.queryAll(type);
for (Activity activity : activities) {
this.activityMap.insert(activity);
nrOfActivities++;
}
}
tx.commitOnClose();
}
long duration = System.nanoTime() - start;
String durationS = StringHelper.formatNanoDuration(duration);
logger.info(MessageFormat.format("Loading Model from Database for realm {0} took {1}.", getRealm(), durationS)); //$NON-NLS-1$
logger.info(MessageFormat.format("Loaded {0} Orders", nrOfOrders)); //$NON-NLS-1$
logger.info(MessageFormat.format("Loaded {0} Resources", nrOfResources)); //$NON-NLS-1$
logger.info(MessageFormat.format("Loaded {0} Activities", nrOfActivities)); //$NON-NLS-1$
}
@Override

View File

@ -16,13 +16,10 @@
package li.strolch.agent.impl;
import static li.strolch.model.StrolchModelConstants.INTERPRETATION_RESOURCE_REF;
import static li.strolch.model.StrolchModelConstants.UOM_NONE;
import java.text.MessageFormat;
import java.util.List;
import li.strolch.agent.api.ResourceMap;
import li.strolch.exception.StrolchException;
import li.strolch.model.Resource;
import li.strolch.model.ResourceVisitor;
import li.strolch.model.parameter.Parameter;
@ -42,20 +39,7 @@ public class CachedResourceMap extends CachedElementMap<Resource> implements Res
@Override
protected void assertIsRefParam(Parameter<?> refP) {
String interpretation = refP.getInterpretation();
if (!interpretation.equals(INTERPRETATION_RESOURCE_REF)) {
String msg = MessageFormat.format(
"{0} is not an Resource reference as its interpretation is not {1} it is {2}", //$NON-NLS-1$
refP.getLocator(), INTERPRETATION_RESOURCE_REF, interpretation);
throw new StrolchException(msg);
}
if (refP.getUom().equals(UOM_NONE)) {
String msg = MessageFormat.format("{0} is not an Resource reference as its UOM is not set to a type!", //$NON-NLS-1$
refP.getLocator());
throw new StrolchException(msg);
}
ElementMapHelpers.assertIsRefParam(INTERPRETATION_RESOURCE_REF, refP);
}
@Override

View File

@ -0,0 +1,44 @@
/*
* Copyright 2015 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.agent.impl;
import static li.strolch.model.StrolchModelConstants.UOM_NONE;
import java.text.MessageFormat;
import li.strolch.exception.StrolchException;
import li.strolch.model.parameter.Parameter;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
class ElementMapHelpers {
public static void assertIsRefParam(String expectedInterpretation, Parameter<?> refP) {
String interpretation = refP.getInterpretation();
if (!interpretation.equals(expectedInterpretation)) {
String msg = "{0} is not an expected element reference as its interpretation is {1} instead of {2}"; //$NON-NLS-1$
throw new StrolchException(MessageFormat.format(msg, refP.getLocator(), expectedInterpretation,
interpretation));
}
if (refP.getUom().equals(UOM_NONE)) {
String msg = "{0} is not an expected element reference as its UOM is not set to a type it is set to {1}!"; //$NON-NLS-1$
throw new StrolchException(MessageFormat.format(msg, refP.getLocator(), UOM_NONE));
}
}
}

View File

@ -17,6 +17,7 @@ package li.strolch.agent.impl;
import java.text.MessageFormat;
import li.strolch.agent.api.ActivityMap;
import li.strolch.agent.api.AuditTrail;
import li.strolch.agent.api.ComponentContainer;
import li.strolch.agent.api.OrderMap;
@ -37,6 +38,7 @@ public class EmptyRealm extends InternalStrolchRealm {
private ResourceMap resourceMap;
private OrderMap orderMap;
private ActivityMap activityMap;
private AuditTrail auditTrail;
private PersistenceHandler persistenceHandler;
@ -71,6 +73,11 @@ public class EmptyRealm extends InternalStrolchRealm {
return this.orderMap;
}
@Override
public ActivityMap getActivityMap() {
return this.activityMap;
}
@Override
public AuditTrail getAuditTrail() {
return this.auditTrail;

View File

@ -18,10 +18,12 @@ package li.strolch.agent.impl;
import java.util.Collections;
import java.util.Set;
import li.strolch.agent.api.ActivityMap;
import li.strolch.agent.api.OrderMap;
import li.strolch.agent.api.ResourceMap;
import li.strolch.model.Order;
import li.strolch.model.Resource;
import li.strolch.model.activity.Activity;
import li.strolch.model.xml.StrolchElementListener;
import li.strolch.persistence.api.StrolchTransaction;
@ -32,26 +34,34 @@ public class InMemoryElementListener implements StrolchElementListener {
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;
private StrolchTransaction tx;
private ResourceMap resourceMap;
private OrderMap orderMap;
private ActivityMap activityMap;
public InMemoryElementListener(StrolchTransaction tx) {
this.tx = tx;
this.resourceMap = tx.getResourceMap();
this.orderMap = tx.getOrderMap();
this.activityMap = tx.getActivityMap();
this.addResources = true;
this.addOrders = true;
this.addActivities = true;
this.updateResources = true;
this.updateOrders = true;
this.updateActivities = true;
this.orderTypes = Collections.emptySet();
this.resourceTypes = Collections.emptySet();
this.activityTypes = Collections.emptySet();
}
/**
@ -70,6 +80,14 @@ public class InMemoryElementListener implements StrolchElementListener {
this.addOrders = addOrders;
}
/**
* @param addActivities
* the addActivities to set
*/
public void setAddActivities(boolean addActivities) {
this.addActivities = addActivities;
}
/**
* @param updateResources
* the updateResources to set
@ -86,6 +104,14 @@ public class InMemoryElementListener implements StrolchElementListener {
this.updateOrders = updateOrders;
}
/**
* @param updateActivities
* the updateActivities to set
*/
public void setUpdateActivities(boolean updateActivities) {
this.updateActivities = updateActivities;
}
/**
* @param orderTypes
* the orderTypes to set
@ -102,6 +128,14 @@ public class InMemoryElementListener implements StrolchElementListener {
this.resourceTypes = resourceTypes;
}
/**
* @param activityTypes
* the activityTypes to set
*/
public void setActivityTypes(Set<String> activityTypes) {
this.activityTypes = activityTypes;
}
@Override
public void notifyResource(Resource resource) {
if (!this.resourceTypes.isEmpty() && !this.resourceTypes.contains(resource.getType()))
@ -129,4 +163,18 @@ public class InMemoryElementListener implements StrolchElementListener {
this.orderMap.add(this.tx, order);
}
}
@Override
public void notifyActivity(Activity activity) {
if (!this.activityTypes.isEmpty() && !this.activityTypes.contains(activity.getType()))
return;
if (this.activityMap.hasElement(this.tx, activity.getType(), activity.getId())) {
if (this.updateActivities) {
this.activityMap.update(this.tx, activity);
}
} else if (this.addActivities) {
this.activityMap.add(this.tx, activity);
}
}
}

View File

@ -18,6 +18,7 @@ package li.strolch.agent.impl;
import java.text.MessageFormat;
import java.util.concurrent.TimeUnit;
import li.strolch.agent.api.ActivityMap;
import li.strolch.agent.api.AuditTrail;
import li.strolch.agent.api.ComponentContainer;
import li.strolch.agent.api.LockHandler;
@ -132,6 +133,8 @@ public abstract class InternalStrolchRealm implements StrolchRealm {
public abstract OrderMap getOrderMap();
public abstract ActivityMap getActivityMap();
public abstract AuditTrail getAuditTrail();
}

View File

@ -17,7 +17,9 @@ package li.strolch.agent.impl;
import li.strolch.model.Order;
import li.strolch.model.Resource;
import li.strolch.model.activity.Activity;
import li.strolch.model.xml.StrolchElementListener;
import li.strolch.persistence.api.ActivityDao;
import li.strolch.persistence.api.OrderDao;
import li.strolch.persistence.api.ResourceDao;
import li.strolch.persistence.api.StrolchTransaction;
@ -27,11 +29,13 @@ public class StoreToDaoElementListener implements StrolchElementListener {
private StrolchTransaction tx;
private ResourceDao resourceDao;
private OrderDao orderDao;
private ActivityDao activityDao;
public StoreToDaoElementListener(StrolchTransaction tx) {
this.tx = tx;
this.resourceDao = tx.getPersistenceHandler().getResourceDao(this.tx);
this.orderDao = tx.getPersistenceHandler().getOrderDao(this.tx);
this.activityDao = tx.getPersistenceHandler().getActivityDao(this.tx);
}
@Override
@ -43,4 +47,9 @@ public class StoreToDaoElementListener implements StrolchElementListener {
public void notifyOrder(Order order) {
this.orderDao.save(order);
}
@Override
public void notifyActivity(Activity activity) {
this.activityDao.save(activity);
}
}

View File

@ -0,0 +1,46 @@
/*
* Copyright 2015 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.agent.impl;
import static li.strolch.model.StrolchModelConstants.INTERPRETATION_ACTIVITY_REF;
import java.util.List;
import li.strolch.agent.api.ActivityMap;
import li.strolch.model.ActivityVisitor;
import li.strolch.model.activity.Activity;
import li.strolch.model.parameter.Parameter;
import li.strolch.model.query.ActivityQuery;
import li.strolch.persistence.api.ActivityDao;
import li.strolch.persistence.api.StrolchTransaction;
public class TransactionalActivityMap extends TransactionalElementMap<Activity> implements ActivityMap {
@Override
protected void assertIsRefParam(Parameter<?> refP) {
ElementMapHelpers.assertIsRefParam(INTERPRETATION_ACTIVITY_REF, refP);
}
@Override
protected ActivityDao getDao(StrolchTransaction tx) {
return tx.getPersistenceHandler().getActivityDao(tx);
}
@Override
public <U> List<U> doQuery(StrolchTransaction tx, ActivityQuery query, ActivityVisitor<U> activityVisitor) {
return getDao(tx).doQuery(query, activityVisitor);
}
}

View File

@ -16,13 +16,10 @@
package li.strolch.agent.impl;
import static li.strolch.model.StrolchModelConstants.INTERPRETATION_ORDER_REF;
import static li.strolch.model.StrolchModelConstants.UOM_NONE;
import java.text.MessageFormat;
import java.util.List;
import li.strolch.agent.api.OrderMap;
import li.strolch.exception.StrolchException;
import li.strolch.model.Order;
import li.strolch.model.OrderVisitor;
import li.strolch.model.parameter.Parameter;
@ -34,18 +31,7 @@ public class TransactionalOrderMap extends TransactionalElementMap<Order> implem
@Override
protected void assertIsRefParam(Parameter<?> refP) {
String interpretation = refP.getInterpretation();
if (!interpretation.equals(INTERPRETATION_ORDER_REF)) {
String msg = "{0} is not an Order reference as its interpretation is not {1} it is {2}"; //$NON-NLS-1$
throw new StrolchException(MessageFormat.format(msg, refP.getLocator(), INTERPRETATION_ORDER_REF,
interpretation));
}
if (refP.getUom().equals(UOM_NONE)) {
String msg = "{0} is not an Order reference as its UOM is not set to a type!"; //$NON-NLS-1$
throw new StrolchException(MessageFormat.format(msg, refP.getLocator()));
}
ElementMapHelpers.assertIsRefParam(INTERPRETATION_ORDER_REF, refP);
}
@Override

View File

@ -17,6 +17,7 @@ package li.strolch.agent.impl;
import java.text.MessageFormat;
import li.strolch.agent.api.ActivityMap;
import li.strolch.agent.api.AuditTrail;
import li.strolch.agent.api.ComponentContainer;
import li.strolch.agent.api.OrderMap;
@ -36,6 +37,7 @@ public class TransactionalRealm extends InternalStrolchRealm {
private ResourceMap resourceMap;
private OrderMap orderMap;
private ActivityMap activityMap;
private AuditTrail auditTrail;
private PersistenceHandler persistenceHandler;
@ -70,6 +72,11 @@ public class TransactionalRealm extends InternalStrolchRealm {
return this.orderMap;
}
@Override
public ActivityMap getActivityMap() {
return this.activityMap;
}
@Override
public AuditTrail getAuditTrail() {
return this.auditTrail;
@ -80,6 +87,7 @@ public class TransactionalRealm extends InternalStrolchRealm {
super.initialize(container, configuration);
this.resourceMap = new TransactionalResourceMap();
this.orderMap = new TransactionalOrderMap();
this.activityMap = new TransactionalActivityMap();
if (isAuditTrailEnabled()) {
this.auditTrail = new TransactionalAuditTrail();
@ -98,6 +106,7 @@ public class TransactionalRealm extends InternalStrolchRealm {
long start = System.nanoTime();
int nrOfOrders = 0;
int nrOfResources = 0;
int nrOfActivities = 0;
try (StrolchTransaction tx = openTx(privilegeContext.getCertificate(), DefaultRealmHandler.AGENT_BOOT)) {
nrOfOrders = this.orderMap.getAllKeys(tx).size();
@ -107,12 +116,17 @@ public class TransactionalRealm extends InternalStrolchRealm {
nrOfResources = this.resourceMap.getAllKeys(tx).size();
}
try (StrolchTransaction tx = openTx(privilegeContext.getCertificate(), DefaultRealmHandler.AGENT_BOOT)) {
nrOfActivities = this.activityMap.getAllKeys(tx).size();
}
long duration = System.nanoTime() - start;
String durationS = StringHelper.formatNanoDuration(duration);
logger.info(MessageFormat.format(
"Initialized Transactional Maps for realm {0} took {1}.", getRealm(), durationS)); //$NON-NLS-1$
logger.info(MessageFormat.format("There are {0} Orders", nrOfOrders)); //$NON-NLS-1$
logger.info(MessageFormat.format("There are {0} Resources", nrOfResources)); //$NON-NLS-1$
logger.info(MessageFormat.format("There are {0} Activities", nrOfActivities)); //$NON-NLS-1$
}
@Override

View File

@ -16,13 +16,10 @@
package li.strolch.agent.impl;
import static li.strolch.model.StrolchModelConstants.INTERPRETATION_RESOURCE_REF;
import static li.strolch.model.StrolchModelConstants.UOM_NONE;
import java.text.MessageFormat;
import java.util.List;
import li.strolch.agent.api.ResourceMap;
import li.strolch.exception.StrolchException;
import li.strolch.model.Resource;
import li.strolch.model.ResourceVisitor;
import li.strolch.model.parameter.Parameter;
@ -34,18 +31,7 @@ public class TransactionalResourceMap extends TransactionalElementMap<Resource>
@Override
protected void assertIsRefParam(Parameter<?> refP) {
String interpretation = refP.getInterpretation();
if (!interpretation.equals(INTERPRETATION_RESOURCE_REF)) {
String msg = "{0} is not an Resource reference as its interpretation is not {1} it is {2}"; //$NON-NLS-1$
throw new StrolchException(MessageFormat.format(msg, refP.getLocator(), INTERPRETATION_RESOURCE_REF,
interpretation));
}
if (refP.getUom().equals(UOM_NONE)) {
String msg = "{0} is not an Resource reference as its UOM is not set to a type!"; //$NON-NLS-1$
throw new StrolchException(MessageFormat.format(msg, refP.getLocator()));
}
ElementMapHelpers.assertIsRefParam(INTERPRETATION_RESOURCE_REF, refP);
}
@Override

View File

@ -18,6 +18,7 @@ package li.strolch.agent.impl;
import java.io.File;
import java.text.MessageFormat;
import li.strolch.agent.api.ActivityMap;
import li.strolch.agent.api.AuditTrail;
import li.strolch.agent.api.ComponentContainer;
import li.strolch.agent.api.OrderMap;
@ -42,6 +43,7 @@ public class TransientRealm extends InternalStrolchRealm {
private ResourceMap resourceMap;
private OrderMap orderMap;
private ActivityMap activityMap;
private AuditTrail auditTrail;
private PersistenceHandler persistenceHandler;
@ -78,6 +80,11 @@ public class TransientRealm extends InternalStrolchRealm {
return this.orderMap;
}
@Override
public ActivityMap getActivityMap() {
return this.activityMap;
}
@Override
public AuditTrail getAuditTrail() {
return this.auditTrail;
@ -99,6 +106,7 @@ public class TransientRealm extends InternalStrolchRealm {
this.persistenceHandler = new InMemoryPersistence(container.getPrivilegeHandler());
this.resourceMap = new TransactionalResourceMap();
this.orderMap = new TransactionalOrderMap();
this.activityMap = new TransactionalActivityMap();
if (isAuditTrailEnabled()) {
this.auditTrail = new TransactionalAuditTrail();
@ -126,6 +134,7 @@ public class TransientRealm extends InternalStrolchRealm {
"Loading XML Model file {0} for realm {1} took {2}.", this.modelFile.getName(), getRealm(), 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$
}
@Override

View File

@ -23,6 +23,7 @@ import java.util.List;
import java.util.ListIterator;
import java.util.Set;
import li.strolch.agent.api.ActivityMap;
import li.strolch.agent.api.AuditTrail;
import li.strolch.agent.api.ObserverHandler;
import li.strolch.agent.api.OrderMap;
@ -30,12 +31,14 @@ import li.strolch.agent.api.ResourceMap;
import li.strolch.agent.api.StrolchAgent;
import li.strolch.agent.api.StrolchLockException;
import li.strolch.agent.api.StrolchRealm;
import li.strolch.agent.impl.AuditingActivityMap;
import li.strolch.agent.impl.AuditingAuditMapFacade;
import li.strolch.agent.impl.AuditingOrderMap;
import li.strolch.agent.impl.AuditingResourceMap;
import li.strolch.agent.impl.InternalStrolchRealm;
import li.strolch.exception.StrolchAccessDeniedException;
import li.strolch.exception.StrolchException;
import li.strolch.model.ActivityVisitor;
import li.strolch.model.GroupedParameterizedElement;
import li.strolch.model.Locator;
import li.strolch.model.Order;
@ -46,6 +49,7 @@ import li.strolch.model.ResourceVisitor;
import li.strolch.model.StrolchElement;
import li.strolch.model.StrolchRootElement;
import li.strolch.model.Tags;
import li.strolch.model.activity.Activity;
import li.strolch.model.audit.AccessType;
import li.strolch.model.audit.Audit;
import li.strolch.model.audit.AuditQuery;
@ -54,12 +58,14 @@ import li.strolch.model.audit.NoStrategyAuditVisitor;
import li.strolch.model.parameter.Parameter;
import li.strolch.model.parameter.StringListParameter;
import li.strolch.model.parameter.StringParameter;
import li.strolch.model.query.ActivityQuery;
import li.strolch.model.query.OrderQuery;
import li.strolch.model.query.ResourceQuery;
import li.strolch.model.query.StrolchQuery;
import li.strolch.model.timedstate.StrolchTimedState;
import li.strolch.model.timevalue.IValue;
import li.strolch.model.visitor.ElementTypeVisitor;
import li.strolch.model.visitor.NoStrategyActivityVisitor;
import li.strolch.model.visitor.NoStrategyOrderVisitor;
import li.strolch.model.visitor.NoStrategyResourceVisitor;
import li.strolch.runtime.StrolchConstants;
@ -95,6 +101,7 @@ public abstract class AbstractTransaction implements StrolchTransaction {
private AuditingOrderMap orderMap;
private AuditingResourceMap resourceMap;
private AuditingActivityMap activityMap;
private AuditingAuditMapFacade auditTrail;
private String action;
@ -257,6 +264,15 @@ public abstract class AbstractTransaction implements StrolchTransaction {
return this.orderMap;
}
@Override
public ActivityMap getActivityMap() {
if (this.activityMap == null) {
this.activityMap = new AuditingActivityMap(this.realm.getActivityMap(),
this.realm.isAuditTrailEnabledForRead());
}
return this.activityMap;
}
@Override
public AuditTrail getAuditTrail() {
if (this.auditTrail == null) {
@ -298,6 +314,18 @@ public abstract class AbstractTransaction implements StrolchTransaction {
assertQueryAllowed(query);
return getResourceMap().doQuery(this, query, resourceVisitor);
}
@Override
public List<Activity> doQuery(ActivityQuery query) {
assertQueryAllowed(query);
return getActivityMap().doQuery(this, query, new NoStrategyActivityVisitor());
}
@Override
public <U> List<U> doQuery(ActivityQuery query, ActivityVisitor<U> activityVisitor) {
assertQueryAllowed(query);
return getActivityMap().doQuery(this, query, activityVisitor);
}
@Override
public List<Audit> doQuery(AuditQuery query) {
@ -486,7 +514,78 @@ public abstract class AbstractTransaction implements StrolchTransaction {
DBC.PRE.assertNotNull("refP", refP);
return getResourceMap().getBy(this, refP, assertExists);
}
@Override
public Activity getActivityBy(String type, String id) {
return getActivityBy(type, id, false);
}
@Override
public Activity getActivityBy(String type, String id, boolean assertExists) throws StrolchException {
Activity activity = getActivityMap().getBy(this, type, id);
if (assertExists && activity == null) {
String msg = "No Activity exists with the id {0} with type {1}";
throw new StrolchException(MessageFormat.format(msg, id, type));
}
return activity;
}
@Override
public Activity getActivityBy(StringParameter refP) throws StrolchException {
DBC.PRE.assertNotNull("refP", refP);
return getActivityBy(refP, false);
}
@Override
public Activity getActivityBy(StringParameter refP, boolean assertExists) throws StrolchException {
DBC.PRE.assertNotNull("refP", refP);
return getActivityMap().getBy(this, refP, assertExists);
}
@Override
public List<Activity> getActivitiesBy(StringListParameter refP) throws StrolchException {
DBC.PRE.assertNotNull("refP", refP);
return getActivityMap().getBy(this, refP, false);
}
@Override
public List<Activity> getActivitiesBy(StringListParameter refP, boolean assertExists) throws StrolchException {
DBC.PRE.assertNotNull("refP", refP);
return getActivityMap().getBy(this, refP, assertExists);
}
@Override
public void flush() {
try {

View File

@ -0,0 +1,30 @@
/*
* Copyright 2015 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.persistence.api;
import java.util.List;
import li.strolch.model.ActivityVisitor;
import li.strolch.model.activity.Activity;
import li.strolch.model.query.ActivityQuery;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public interface ActivityDao extends StrolchDao<Activity> {
public <U> List<U> doQuery(ActivityQuery query, ActivityVisitor<U> activityVisitor);
}

View File

@ -15,12 +15,14 @@
*/
package li.strolch.persistence.api;
import li.strolch.agent.api.ActivityMap;
import li.strolch.agent.api.AuditTrail;
import li.strolch.agent.api.OrderMap;
import li.strolch.agent.api.ResourceMap;
import li.strolch.agent.api.StrolchRealm;
import li.strolch.model.Order;
import li.strolch.model.Resource;
import li.strolch.model.activity.Activity;
import li.strolch.model.audit.Audit;
import ch.eitchnet.privilege.model.Certificate;
@ -68,6 +70,18 @@ public interface PersistenceHandler {
*/
public ResourceDao getResourceDao(StrolchTransaction tx);
/**
* Returns the {@link ActivityDao} for the given transaction. Use this only if you want to bypass certain
* transaction features. Accessing {@link Activity Activities} should be done through the {@link ActivityMap}
* accessed from the transaction
*
* @param tx
* the transaction for which the {@link ActivityDao} is to be returned
*
* @return the {@link ActivityDao}
*/
public ActivityDao getActivityDao(StrolchTransaction tx);
/**
* Returns the {@link AuditDao} for the given transaction. Use this only if you want to bypass certain transaction
* features. Accessing {@link Audit Audits} should be done through the {@link AuditTrail} accessed from the

View File

@ -17,6 +17,7 @@ package li.strolch.persistence.api;
import java.util.List;
import li.strolch.agent.api.ActivityMap;
import li.strolch.agent.api.AuditTrail;
import li.strolch.agent.api.OrderMap;
import li.strolch.agent.api.ResourceMap;
@ -25,6 +26,7 @@ import li.strolch.agent.api.StrolchLockException;
import li.strolch.agent.api.StrolchRealm;
import li.strolch.agent.impl.DataStoreMode;
import li.strolch.exception.StrolchException;
import li.strolch.model.ActivityVisitor;
import li.strolch.model.Locator;
import li.strolch.model.Order;
import li.strolch.model.OrderVisitor;
@ -34,6 +36,7 @@ import li.strolch.model.ResourceVisitor;
import li.strolch.model.StrolchElement;
import li.strolch.model.StrolchRootElement;
import li.strolch.model.Tags;
import li.strolch.model.activity.Activity;
import li.strolch.model.audit.AccessType;
import li.strolch.model.audit.Audit;
import li.strolch.model.audit.AuditQuery;
@ -41,6 +44,7 @@ import li.strolch.model.audit.AuditVisitor;
import li.strolch.model.parameter.Parameter;
import li.strolch.model.parameter.StringListParameter;
import li.strolch.model.parameter.StringParameter;
import li.strolch.model.query.ActivityQuery;
import li.strolch.model.query.OrderQuery;
import li.strolch.model.query.ResourceQuery;
import li.strolch.runtime.StrolchConstants;
@ -123,6 +127,13 @@ public interface StrolchTransaction extends AutoCloseable {
*/
public OrderMap getOrderMap();
/**
* Returns a reference to the {@link ActivityMap} for the {@link StrolchRealm} for which this transaction was opened
*
* @return the {@link ActivityMap}
*/
public ActivityMap getActivityMap();
/**
* Returns the {@link PersistenceHandler}. If the {@link StrolchRealm} is not running in
* {@link DataStoreMode#TRANSIENT} mode, then the {@link PersistenceHandler} will be a {@link StrolchComponent},
@ -424,6 +435,42 @@ public interface StrolchTransaction extends AutoCloseable {
*/
public <U> List<U> doQuery(ResourceQuery query, ResourceVisitor<U> resourceVisitor);
/**
* <p>
* Performs the given {@link ActivityQuery} returning the resulting list of {@link Activity Activities}.
* </p>
*
* <p>
* <b>Note:</b> Should the result be mapped to different objects, then use
* {@link #doQuery(ActivityQuery, ActivityVisitor)}
* </p>
*
* @param query
* the query to perform
*
* @return the result list, never null
*/
public List<Activity> doQuery(ActivityQuery query);
/**
* <p>
* Performs the given {@link ActivityQuery} and each returned {@link Activity} is passed through the
* {@link ActivityVisitor} and the return value of the visitor is added to the return list
* </p>
*
* <p>
* This method is intended for situations where the query result should not be {@link Activity} but some other
* object type. For instance in a restful API, the result might have to be mapped to a POJO, thus using this method
* can perform the mapping step for you
* </p>
*
* @param query
* the query to perform
*
* @return the result list of elements as returned by the {@link ActivityVisitor}, never null
*/
public <U> List<U> doQuery(ActivityQuery query, ActivityVisitor<U> activityVisitor);
/**
* <p>
* Performs the given {@link AuditQuery} returning the resulting list of {@link Audit Audits}.
@ -670,13 +717,111 @@ public interface StrolchTransaction extends AutoCloseable {
* if true, and resource does not exist, then a {@link StrolchException} is thrown
*
* @return the resources referenced by the parameter, or the empty list if they do not exist. <b>Note:</b> Any
* missing resources are not returned!
* missing resources are not returned unless <code>assertExists</code> is true
*
* @throws StrolchException
* if the {@link StringListParameter} is not a properly configured as a reference parameter
*/
public List<Resource> getResourcesBy(StringListParameter refP, boolean assertExists) throws StrolchException;
/**
* Returns the {@link Activity} with the given type and id, or null if it does not exist
*
* @param type
* the type of the {@link Activity}
* @param id
* the id of the {@link Activity}
*
* @return the {@link Activity} with the given type and id, or null if it does not exist
*/
public Activity getActivityBy(String type, String id);
/**
* Returns the {@link Activity} with the given type and id, or null if it does not exist
*
* @param type
* the type of the {@link Activity}
* @param id
* the id of the {@link Activity}
* @param assertExists
* if true, and activity does not exist, then a {@link StrolchException} is thrown
*
* @return the {@link Activity} with the given type and id, or null if it does not exist
*
* @throws StrolchException
* if the activity does not exist, and assertExists is true
*/
public Activity getActivityBy(String type, String id, boolean assertExists) throws StrolchException;
/**
* Returns the {@link Activity} which is referenced by the given {@link StringParameter}. A reference
* {@link Parameter} must have its interpretation set to {@link StrolchConstants#INTERPRETATION_ACTIVITY_REF} and
* the UOM must be set to the activity's type and the value is the id of the activity
*
* @param refP
* the {@link StringParameter} which references an {@link Activity}
*
* @return the activity referenced by the parameter, or null if it does not exist
*
* @throws StrolchException
* if the {@link StringParameter} is not a properly configured as a reference parameter
*/
public Activity getActivityBy(StringParameter refP) throws StrolchException;
/**
* Returns the {@link Activity} which is referenced by the given {@link StringParameter}. A reference
* {@link Parameter} must have its interpretation set to {@link StrolchConstants#INTERPRETATION_ACTIVITY_REF} and
* the UOM must be set to the activity's type and the value is the id of the activity
*
* @param refP
* the {@link StringParameter} which references an {@link Activity}
* @param assertExists
* if true, and activity does not exist, then a {@link StrolchException} is thrown
*
* @return the activity referenced by the parameter, or null if it does not exist
*
* @throws StrolchException
* if the {@link StringParameter} is not a properly configured as a reference parameter, or if the
* activity does not exist, and assertExists is true
*/
public Activity getActivityBy(StringParameter refP, boolean assertExists) throws StrolchException;
/**
* Returns all {@link Activity Activities} which are referenced by the given {@link StringListParameter}. A
* reference {@link Parameter} must have its interpretation set to
* {@link StrolchConstants#INTERPRETATION_ACTIVITY_REF} and the UOM must be set to the activity's type and the value
* is the id of the activity
*
* @param refP
* the {@link StringListParameter} which references a list of {@link Activity Activities}
*
* @return the activities referenced by the parameter, or the empty list if they do not exist. <b>Note:</b> Any
* missing activities are not returned!
*
* @throws StrolchException
* if the {@link StringListParameter} is not a properly configured as a reference parameter
*/
public List<Activity> getActivitiesBy(StringListParameter refP) throws StrolchException;
/**
* Returns all {@link Activity Activities} which are referenced by the given {@link StringListParameter}. A
* reference {@link Parameter} must have its interpretation set to
* {@link StrolchConstants#INTERPRETATION_ACTIVITY_REF} and the UOM must be set to the activity's type and the value
* is the id of the activity
*
* @param refP
* the {@link StringListParameter} which references a list of {@link Activity Activities}
* @param assertExists
* if true, and activity does not exist, then a {@link StrolchException} is thrown
*
* @return the activities referenced by the parameter, or the empty list if they do not exist. <b>Note:</b> Any
* missing activities are not returned unless <code>assertExists</code> is true
*
* @throws StrolchException
* if the {@link StringListParameter} is not a properly configured as a reference parameter
*/
public List<Activity> getActivitiesBy(StringListParameter refP, boolean assertExists) throws StrolchException;
/**
* Returns the {@link Order} with the given type and id, or null if it does not exist
*
@ -766,7 +911,7 @@ public interface StrolchTransaction extends AutoCloseable {
* if true, and order does not exist, then a {@link StrolchException} is thrown
*
* @return the orders referenced by the parameter, or the empty list if they do not exist. <b>Note:</b> Any missing
* orders are not returned!
* orders are not returned unless <code>assertExists</code> is true
*
* @throws StrolchException
* if the {@link StringListParameter} is not a properly configured as a reference parameter

View File

@ -0,0 +1,35 @@
/*
* Copyright 2015 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.persistence.inmemory;
import java.util.List;
import li.strolch.model.ActivityVisitor;
import li.strolch.model.activity.Activity;
import li.strolch.model.query.ActivityQuery;
import li.strolch.persistence.api.ActivityDao;
import li.strolch.runtime.query.inmemory.InMemoryActivityQueryVisitor;
import li.strolch.runtime.query.inmemory.InMemoryQuery;
public class InMemoryActivityDao extends InMemoryDao<Activity> implements ActivityDao {
@Override
public <U> List<U> doQuery(ActivityQuery activityQuery, ActivityVisitor<U> activityVisitor) {
InMemoryActivityQueryVisitor visitor = new InMemoryActivityQueryVisitor();
InMemoryQuery<Activity, U> query = visitor.visit(activityQuery, activityVisitor);
return query.doQuery(this);
}
}

View File

@ -19,6 +19,7 @@ import java.util.HashMap;
import java.util.Map;
import li.strolch.agent.api.StrolchRealm;
import li.strolch.persistence.api.ActivityDao;
import li.strolch.persistence.api.AuditDao;
import li.strolch.persistence.api.OrderDao;
import li.strolch.persistence.api.PersistenceHandler;
@ -54,6 +55,12 @@ public class InMemoryPersistence implements PersistenceHandler {
return daoCache.getResourceDao();
}
@Override
public ActivityDao getActivityDao(StrolchTransaction tx) {
DaoCache daoCache = getDaoCache(tx);
return daoCache.getActivityDao();
}
@Override
public AuditDao getAuditDao(StrolchTransaction tx) {
DaoCache daoCache = getDaoCache(tx);
@ -68,7 +75,8 @@ public class InMemoryPersistence implements PersistenceHandler {
private synchronized DaoCache getDaoCache(StrolchTransaction tx) {
DaoCache daoCache = this.daoCache.get(tx.getRealmName());
if (daoCache == null) {
daoCache = new DaoCache(new InMemoryOrderDao(), new InMemoryResourceDao(), new InMemoryAuditDao());
daoCache = new DaoCache(new InMemoryOrderDao(), new InMemoryResourceDao(), new InMemoryActivityDao(),
new InMemoryAuditDao());
this.daoCache.put(tx.getRealmName(), daoCache);
}
return daoCache;
@ -77,11 +85,13 @@ public class InMemoryPersistence implements PersistenceHandler {
private class DaoCache {
private OrderDao orderDao;
private ResourceDao resourceDao;
private ActivityDao activityDao;
private AuditDao auditDao;
public DaoCache(OrderDao orderDao, ResourceDao resourceDao, AuditDao auditDao) {
public DaoCache(OrderDao orderDao, ResourceDao resourceDao, ActivityDao activityDao, AuditDao auditDao) {
this.orderDao = orderDao;
this.resourceDao = resourceDao;
this.activityDao = activityDao;
this.auditDao = auditDao;
}
@ -96,5 +106,9 @@ public class InMemoryPersistence implements PersistenceHandler {
public AuditDao getAuditDao() {
return this.auditDao;
}
public ActivityDao getActivityDao() {
return this.activityDao;
}
}
}

View File

@ -18,6 +18,7 @@ package li.strolch.persistence.inmemory;
import li.strolch.agent.api.ComponentContainer;
import li.strolch.agent.api.StrolchComponent;
import li.strolch.agent.api.StrolchRealm;
import li.strolch.persistence.api.ActivityDao;
import li.strolch.persistence.api.AuditDao;
import li.strolch.persistence.api.OrderDao;
import li.strolch.persistence.api.PersistenceHandler;
@ -62,6 +63,11 @@ public class InMemoryPersistenceHandler extends StrolchComponent implements Pers
return this.persistence.getResourceDao(tx);
}
@Override
public ActivityDao getActivityDao(StrolchTransaction tx) {
return this.persistence.getActivityDao(tx);
}
@Override
public AuditDao getAuditDao(StrolchTransaction tx) {
return this.persistence.getAuditDao(tx);

View File

@ -53,6 +53,11 @@ public class StrolchConstants {
*/
public static final String INTERPRETATION_ORDER_REF = StrolchModelConstants.INTERPRETATION_ORDER_REF;
/**
* @see StrolchModelConstants#INTERPRETATION_ACTIVITY_REF
*/
public static final String INTERPRETATION_ACTIVITY_REF = StrolchModelConstants.INTERPRETATION_ACTIVITY_REF;
public static String makeRealmKey(String realmName, String key) {
String realmKey = key;
if (!realmName.equals(DEFAULT_REALM))

View File

@ -0,0 +1,31 @@
/*
* Copyright 2015 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.runtime.query.inmemory;
import li.strolch.model.activity.Activity;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class ActivityTypeNavigator extends StrolchTypeNavigator<Activity> {
/**
* @param type
*/
public ActivityTypeNavigator(String type) {
super(type);
}
}

View File

@ -0,0 +1,65 @@
/*
* Copyright 2015 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.runtime.query.inmemory;
import java.util.List;
import li.strolch.model.ActivityVisitor;
import li.strolch.model.activity.Activity;
import li.strolch.model.query.ActivityQuery;
import li.strolch.model.query.ActivityQueryVisitor;
import li.strolch.model.query.StrolchTypeNavigation;
import li.strolch.persistence.api.ActivityDao;
import ch.eitchnet.utils.dbc.DBC;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class InMemoryActivityQueryVisitor extends InMemoryQueryVisitor<Activity, ActivityDao> implements
ActivityQueryVisitor {
public InMemoryActivityQueryVisitor() {
super();
}
@Override
protected InMemoryQueryVisitor<Activity, ActivityDao> newInstance() {
return new InMemoryActivityQueryVisitor();
}
public <U> InMemoryQuery<Activity, U> visit(ActivityQuery activityQuery, ActivityVisitor<U> activityVisitor) {
DBC.PRE.assertNotNull("ActivityVisitor may not be null!", activityVisitor); //$NON-NLS-1$
activityQuery.accept(this);
Navigator<Activity> navigator = getNavigator();
if (navigator == null) {
String msg = "Query is missing a navigation!"; //$NON-NLS-1$
throw new QueryException(msg);
}
List<Selector<Activity>> selectors = getSelectors();
if (selectors.isEmpty())
return new InMemoryQuery<>(navigator, null, activityVisitor);
DBC.PRE.assertTrue("Invalid query as it may only contain one selector!", selectors.size() == 1); //$NON-NLS-1$
return new InMemoryQuery<>(navigator, selectors.get(0), activityVisitor);
}
@Override
public void visit(StrolchTypeNavigation navigation) {
setNavigator(new ActivityTypeNavigator(navigation.getType()));
}
}

View File

@ -0,0 +1,29 @@
/*
* Copyright 2015 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;
import li.strolch.model.activity.Activity;
import li.strolch.model.visitor.StrolchElementVisitor;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
* @param <U>
*/
public interface ActivityVisitor<U> extends StrolchElementVisitor<Activity, U> {
@Override
public U visit(Activity element);
}

View File

@ -49,6 +49,9 @@ public class ModelStatistics {
@XmlAttribute(name = "nrOfOrders")
public long nrOfOrders;
@XmlAttribute(name = "nrOfActivities")
public long nrOfActivities;
/**
* @return the nrOfOrders
*/
@ -70,6 +73,13 @@ public class ModelStatistics {
return this.nrOfOrders + this.nrOfResources;
}
/**
* @return the nrOfActivities
*/
public long getNrOfActivities() {
return this.nrOfActivities;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
@ -81,6 +91,8 @@ public class ModelStatistics {
builder.append(this.nrOfResources);
builder.append(", nrOfOrders=");
builder.append(this.nrOfOrders);
builder.append(", nrOfActivities=");
builder.append(this.nrOfActivities);
builder.append("]");
return builder.toString();
}

View File

@ -40,6 +40,12 @@ public class StrolchModelConstants {
*/
public static final String INTERPRETATION_ORDER_REF = "Order-Ref"; //$NON-NLS-1$
/**
* This interpretation value indicates that the value of the {@link Parameter} should be understood as a reference
* to an {@link Activity}
*/
public static final String INTERPRETATION_ACTIVITY_REF = "Activity-Ref"; //$NON-NLS-1$
/**
* This interpretation value indicates that the {@link Parameter} has no defined interpretation
*/

View File

@ -0,0 +1,66 @@
/*
* Copyright 2015 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.query;
import li.strolch.model.ActivityVisitor;
import li.strolch.model.activity.Action;
import li.strolch.model.activity.Activity;
/**
* {@link ActivityQuery} is the user API to query {@link Activity Activities} in Strolch. The {@link Navigation} is used
* to navigate to a type of activity on which any further {@link Selection Selections} will be performed. The
* {@link ActivityVisitor} is used to transform the returned object into a domain specific object (if required). This
* mechanism allows you to query e.g. a specific {@link Action} instead of having to return all the elements and then
* performing this transformation.
*
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class ActivityQuery extends StrolchElementQuery<ActivityQueryVisitor> {
/**
* @param navigation
* @param elementVisitor
*/
public ActivityQuery(Navigation navigation) {
super(navigation);
}
@Override
public ActivityQuery with(Selection selection) {
super.with(selection);
return this;
}
@Override
public ActivityQuery not(Selection selection) {
super.not(selection);
return this;
}
@Override
public ActivityQuery withAny() {
super.withAny();
return this;
}
public static ActivityQuery query(Navigation navigation) {
return new ActivityQuery(navigation);
}
public static ActivityQuery query(String type) {
return new ActivityQuery(new StrolchTypeNavigation(type));
}
}

View File

@ -0,0 +1,24 @@
/*
* Copyright 2015 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.query;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public interface ActivityQueryVisitor extends StrolchRootElementSelectionVisitor, ParameterSelectionVisitor {
// marker interface
}

View File

@ -0,0 +1,30 @@
/*
* Copyright 2015 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.visitor;
import li.strolch.model.ActivityVisitor;
import li.strolch.model.activity.Activity;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class NoStrategyActivityVisitor implements ActivityVisitor<Activity> {
@Override
public Activity visit(Activity element) {
return element;
}
}

View File

@ -21,6 +21,7 @@ import java.util.List;
import li.strolch.model.Order;
import li.strolch.model.Resource;
import li.strolch.model.activity.Activity;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
@ -29,6 +30,7 @@ public class SimpleStrolchElementListener implements StrolchElementListener {
private List<Resource> resources;
private List<Order> orders;
private List<Activity> activities;
@Override
public void notifyResource(Resource resource) {
@ -46,6 +48,14 @@ public class SimpleStrolchElementListener implements StrolchElementListener {
this.orders.add(order);
}
@Override
public void notifyActivity(Activity activity) {
if (this.activities == null) {
this.activities = new ArrayList<>();
}
this.activities.add(activity);
}
/**
* @return the resources
*/
@ -63,4 +73,13 @@ public class SimpleStrolchElementListener implements StrolchElementListener {
return Collections.emptyList();
return this.orders;
}
/**
* @return the activities
*/
public List<Activity> getActivities() {
if (this.activities == null)
return Collections.emptyList();
return this.activities;
}
}

View File

@ -17,6 +17,7 @@ package li.strolch.model.xml;
import li.strolch.model.Order;
import li.strolch.model.Resource;
import li.strolch.model.activity.Activity;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
@ -27,4 +28,6 @@ public interface StrolchElementListener {
public void notifyResource(Resource resource);
public void notifyOrder(Order order);
public void notifyActivity(Activity activity);
}

View File

@ -21,6 +21,7 @@ import java.io.File;
import java.util.HashMap;
import java.util.Map;
import li.strolch.model.activity.Activity;
import li.strolch.model.xml.StrolchElementListener;
import li.strolch.model.xml.XmlModelSaxFileReader;
@ -32,7 +33,6 @@ import ch.eitchnet.utils.helper.StringHelper;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*
*/
@SuppressWarnings("nls")
public class XmlModelDefaultHandlerTest {
@ -42,8 +42,9 @@ public class XmlModelDefaultHandlerTest {
@Test
public void shouldParseXmlModelFile() {
final Map<String, Resource> resourceMap = new HashMap<>();
final Map<String, Order> orderMap = new HashMap<>();
Map<String, Resource> resourceMap = new HashMap<>();
Map<String, Order> orderMap = new HashMap<>();
Map<String, Activity> activityMap = new HashMap<>();
File file = new File("src/test/resources/data/StrolchModel.xml");
StrolchElementListener listener = new StrolchElementListener() {
@ -56,16 +57,24 @@ public class XmlModelDefaultHandlerTest {
public void notifyOrder(Order order) {
orderMap.put(order.getId(), order);
}
@Override
public void notifyActivity(Activity activity) {
activityMap.put(activity.getId(), activity);
}
};
XmlModelSaxFileReader handler = new XmlModelSaxFileReader(listener, file, true);
handler.parseFile();
assertEquals(3, resourceMap.size());
assertEquals(3, orderMap.size());
assertEquals(3, activityMap.size());
ModelStatistics statistics = handler.getStatistics();
logger.info("Parsing took " + StringHelper.formatNanoDuration(statistics.durationNanos));
assertEquals(3, statistics.nrOfOrders);
assertEquals(3, statistics.nrOfResources);
assertEquals(3, statistics.nrOfActivities);
}
}

View File

@ -35,54 +35,54 @@ public class ActionTest {
@Before
public void init() {
// create action
action = new Action("action_1", "Action 1", "Use");
action.setResourceId("dummyRe");
action.setResourceType("dummyReType");
this.action = new Action("action_1", "Action 1", "Use");
this.action.setResourceId("dummyRe");
this.action.setResourceType("dummyReType");
IValueChange<IntegerValue> startChange = new ValueChange<>(STATE_TIME_10, new IntegerValue(1));
startChange.setStateId(STATE_INTEGER_ID);
action.addChange(startChange);
this.action.addChange(startChange);
IValueChange<IntegerValue> endChange = new ValueChange<>(STATE_TIME_30, new IntegerValue(-1));
endChange.setStateId(STATE_INTEGER_ID);
action.addChange(endChange);
this.action.addChange(endChange);
}
@Test
public void testGetStart() {
Assert.assertTrue(STATE_TIME_10 == action.getStart());
Assert.assertTrue(STATE_TIME_10 == this.action.getStart());
}
@Test
public void testGetEnd() {
Assert.assertTrue(STATE_TIME_30 == action.getEnd());
Assert.assertTrue(STATE_TIME_30 == this.action.getEnd());
}
@Test
public void testClone() {
Action clone = (Action) action.getClone();
Assert.assertEquals(action.toString(), clone.toString());
Assert.assertEquals(action.changes.size(), clone.changes.size());
for (int i = 0; i < action.changes.size(); i++) {
Assert.assertEquals(action.changes.get(i).getTime(), clone.changes.get(i).getTime());
Action clone = (Action) this.action.getClone();
Assert.assertEquals(this.action.toString(), clone.toString());
Assert.assertEquals(this.action.changes.size(), clone.changes.size());
for (int i = 0; i < this.action.changes.size(); i++) {
Assert.assertEquals(this.action.changes.get(i).getTime(), clone.changes.get(i).getTime());
}
}
@Test
/**
* no test. Just to see the XML serialization in the console
*/
// @Test
public void testToDOM() throws ParserConfigurationException, TransformerException {
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = db.newDocument();
Element dom = action.toDom(document);
document.appendChild(dom);
Document document = db.newDocument();
Element dom = this.action.toDom(document);
document.appendChild(dom);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
StringWriter stringWriter = new StringWriter();
transformer.transform(new DOMSource(document), new StreamResult(stringWriter));
String content = stringWriter.getBuffer().toString();
System.out.println(content);
}
}

View File

@ -32,111 +32,109 @@ public class ActivityTest {
public void init() {
// create activity element
activity = new Activity("activity", "Activity", "parentType");
this.activity = new Activity("activity", "Activity", "parentType");
// create action 1
action_1 = new Action("action_1", "Action 1", "Use");
action_1.setState(State.CREATED);
action_1.setResourceType("dummyType");
action_1.setResourceId("dummyId");
this.action_1 = new Action("action_1", "Action 1", "Use");
this.action_1.setState(State.CREATED);
this.action_1.setResourceType("dummyType");
this.action_1.setResourceId("dummyId");
activity.addElement(action_1);
childActivity = new Activity("child_activity", "Child Activity", "childType");
this.activity.addElement(this.action_1);
this.childActivity = new Activity("child_activity", "Child Activity", "childType");
// create action 2
action_2 = new Action("action_2", "Action 2", "Use");
action_2.setState(State.PLANNED);
action_2.setResourceType("dummyType");
action_2.setResourceId("dummyId");
this.action_2 = new Action("action_2", "Action 2", "Use");
this.action_2.setState(State.PLANNED);
this.action_2.setResourceType("dummyType");
this.action_2.setResourceId("dummyId");
this.childActivity.addElement(this.action_2);
childActivity.addElement(action_2);
// create action 3
action_3 = new Action("action_3", "Action 3", "Use");
action_3.setState(State.CREATED);
action_3.setResourceType("dummyType");
action_3.setResourceId("dummyId");
childActivity.addElement(action_3);
activity.addElement(childActivity);
this.action_3 = new Action("action_3", "Action 3", "Use");
this.action_3.setState(State.CREATED);
this.action_3.setResourceType("dummyType");
this.action_3.setResourceId("dummyId");
Assert.assertEquals(2, activity.getElements().size());
Assert.assertEquals(2, childActivity.getElements().size());
this.childActivity.addElement(this.action_3);
this.activity.addElement(this.childActivity);
Assert.assertEquals(2, this.activity.getElements().size());
Assert.assertEquals(2, this.childActivity.getElements().size());
}
@Test
public void testStart() {
Assert.assertEquals(action_1.getStart(), activity.getStart());
Assert.assertEquals(this.action_1.getStart(), this.activity.getStart());
}
@Test
public void testEnd() {
Assert.assertEquals(action_3.getEnd(), activity.getEnd());
Assert.assertEquals(this.action_3.getEnd(), this.activity.getEnd());
}
@Test
public void testState() {
Assert.assertEquals(State.CREATED, activity.getState());
Assert.assertEquals(State.CREATED, this.activity.getState());
}
@Test (expected = StrolchException.class)
@Test(expected = StrolchException.class)
public void testIdNull() {
activity.addElement(new Action(null, null, null));
this.activity.addElement(new Action(null, null, null));
}
@Test (expected = StrolchException.class)
@Test(expected = StrolchException.class)
public void testIdAlreadyExists() {
activity.addElement(new Action("action_1", "Action 1", "Use"));
}
@Test
public void getElementTest(){
Assert.assertNull(activity.getElement("not contained"));
Assert.assertEquals(action_1, activity.getElement(action_1.getId()));
this.activity.addElement(new Action("action_1", "Action 1", "Use"));
}
@Test
public void cloneTest(){
Activity clone = (Activity) activity.getClone();
Assert.assertEquals(activity.toString(), clone.toString());
Assert.assertEquals(activity.getElements().size(), clone.getElements().size());
public void getElementTest() {
Assert.assertNull(this.activity.getElement("not contained"));
Assert.assertEquals(this.action_1, this.activity.getElement(this.action_1.getId()));
}
@Test
public void parentTests(){
Assert.assertNull(activity.getParent());
Assert.assertEquals(activity, activity.getRootElement());
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());
public void cloneTest() {
Activity clone = (Activity) this.activity.getClone();
Assert.assertEquals(this.activity.toString(), clone.toString());
Assert.assertEquals(this.activity.getElements().size(), clone.getElements().size());
}
@Test
public void parentTests() {
Assert.assertNull(this.activity.getParent());
Assert.assertEquals(this.activity, this.activity.getRootElement());
Assert.assertTrue(this.activity.isRootElement());
Assert.assertEquals(this.activity, this.childActivity.getParent());
Assert.assertEquals(this.activity, this.childActivity.getRootElement());
Assert.assertFalse(this.childActivity.isRootElement());
Assert.assertEquals(this.childActivity, this.action_2.getParent());
Assert.assertEquals(this.activity, this.action_2.getRootElement());
Assert.assertFalse(this.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);
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.newDocument();
Element dom = this.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

@ -14,5 +14,6 @@
<IncludeFile file="resources/Resources.xml" />
<IncludeFile file="orders/Orders.xml" />
<IncludeFile file="activities/Activities.xml" />
</StrolchModel>

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<StrolchModel>
<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>
</StrolchModel>