diff --git a/li.strolch.agent/src/main/java/li/strolch/agent/api/ActivityMap.java b/li.strolch.agent/src/main/java/li/strolch/agent/api/ActivityMap.java new file mode 100644 index 000000000..9fe310a1f --- /dev/null +++ b/li.strolch.agent/src/main/java/li/strolch/agent/api/ActivityMap.java @@ -0,0 +1,31 @@ +/* + * Copyright 2015 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.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 + */ +public interface ActivityMap extends ElementMap { + + public List doQuery(StrolchTransaction tx, ActivityQuery query, ActivityVisitor activityVisitor); +} diff --git a/li.strolch.agent/src/main/java/li/strolch/agent/impl/AuditingActivityMap.java b/li.strolch.agent/src/main/java/li/strolch/agent/impl/AuditingActivityMap.java new file mode 100644 index 000000000..56bdd9c6e --- /dev/null +++ b/li.strolch.agent/src/main/java/li/strolch/agent/impl/AuditingActivityMap.java @@ -0,0 +1,57 @@ +/* + * Copyright 2015 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.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 + * + * @see AuditingElementMapFacade + */ +public class AuditingActivityMap extends AuditingElementMapFacade implements ActivityMap { + + /** + * @param elementMap + */ + public AuditingActivityMap(ElementMap elementMap, boolean observeAccessReads) { + super(elementMap, observeAccessReads); + } + + @Override + protected ActivityMap getElementMap() { + return (ActivityMap) super.getElementMap(); + } + + @Override + public List doQuery(StrolchTransaction tx, ActivityQuery query, ActivityVisitor activityVisitor) { + List result = getElementMap().doQuery(tx, query, activity -> { + this.read.add(activity); + return activityVisitor.visit(activity); + }); + return result; + } +} diff --git a/li.strolch.agent/src/main/java/li/strolch/agent/impl/CachedActivityMap.java b/li.strolch.agent/src/main/java/li/strolch/agent/impl/CachedActivityMap.java new file mode 100644 index 000000000..5bf7dc7a9 --- /dev/null +++ b/li.strolch.agent/src/main/java/li/strolch/agent/impl/CachedActivityMap.java @@ -0,0 +1,59 @@ +/* + * Copyright 2015 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.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 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 List doQuery(StrolchTransaction tx, ActivityQuery query, ActivityVisitor activityVisitor) { + return getCachedDao().doQuery(query, activityVisitor); + } +} diff --git a/li.strolch.agent/src/main/java/li/strolch/agent/impl/CachedOrderMap.java b/li.strolch.agent/src/main/java/li/strolch/agent/impl/CachedOrderMap.java index cde783d74..76ee49584 100644 --- a/li.strolch.agent/src/main/java/li/strolch/agent/impl/CachedOrderMap.java +++ b/li.strolch.agent/src/main/java/li/strolch/agent/impl/CachedOrderMap.java @@ -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 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 diff --git a/li.strolch.agent/src/main/java/li/strolch/agent/impl/CachedRealm.java b/li.strolch.agent/src/main/java/li/strolch/agent/impl/CachedRealm.java index 6291b6718..b3384bbf8 100644 --- a/li.strolch.agent/src/main/java/li/strolch/agent/impl/CachedRealm.java +++ b/li.strolch.agent/src/main/java/li/strolch/agent/impl/CachedRealm.java @@ -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 activityTypes = activityDao.queryTypes(); + for (String type : activityTypes) { + List 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 diff --git a/li.strolch.agent/src/main/java/li/strolch/agent/impl/CachedResourceMap.java b/li.strolch.agent/src/main/java/li/strolch/agent/impl/CachedResourceMap.java index c719d0c31..f67a9eaa2 100644 --- a/li.strolch.agent/src/main/java/li/strolch/agent/impl/CachedResourceMap.java +++ b/li.strolch.agent/src/main/java/li/strolch/agent/impl/CachedResourceMap.java @@ -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 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 diff --git a/li.strolch.agent/src/main/java/li/strolch/agent/impl/ElementMapHelpers.java b/li.strolch.agent/src/main/java/li/strolch/agent/impl/ElementMapHelpers.java new file mode 100644 index 000000000..42d8204b0 --- /dev/null +++ b/li.strolch.agent/src/main/java/li/strolch/agent/impl/ElementMapHelpers.java @@ -0,0 +1,44 @@ +/* + * Copyright 2015 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.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 + */ +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)); + } + } +} diff --git a/li.strolch.agent/src/main/java/li/strolch/agent/impl/EmptyRealm.java b/li.strolch.agent/src/main/java/li/strolch/agent/impl/EmptyRealm.java index 33d2e0bbd..9801b2368 100644 --- a/li.strolch.agent/src/main/java/li/strolch/agent/impl/EmptyRealm.java +++ b/li.strolch.agent/src/main/java/li/strolch/agent/impl/EmptyRealm.java @@ -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; diff --git a/li.strolch.agent/src/main/java/li/strolch/agent/impl/InMemoryElementListener.java b/li.strolch.agent/src/main/java/li/strolch/agent/impl/InMemoryElementListener.java index d3c92ee74..8a818f23d 100644 --- a/li.strolch.agent/src/main/java/li/strolch/agent/impl/InMemoryElementListener.java +++ b/li.strolch.agent/src/main/java/li/strolch/agent/impl/InMemoryElementListener.java @@ -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 orderTypes; private Set resourceTypes; + private Set 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 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); + } + } } diff --git a/li.strolch.agent/src/main/java/li/strolch/agent/impl/InternalStrolchRealm.java b/li.strolch.agent/src/main/java/li/strolch/agent/impl/InternalStrolchRealm.java index 1fc60dfb6..bba2e338a 100644 --- a/li.strolch.agent/src/main/java/li/strolch/agent/impl/InternalStrolchRealm.java +++ b/li.strolch.agent/src/main/java/li/strolch/agent/impl/InternalStrolchRealm.java @@ -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(); } diff --git a/li.strolch.agent/src/main/java/li/strolch/agent/impl/StoreToDaoElementListener.java b/li.strolch.agent/src/main/java/li/strolch/agent/impl/StoreToDaoElementListener.java index a151f8b43..b48cbc000 100644 --- a/li.strolch.agent/src/main/java/li/strolch/agent/impl/StoreToDaoElementListener.java +++ b/li.strolch.agent/src/main/java/li/strolch/agent/impl/StoreToDaoElementListener.java @@ -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); + } } \ No newline at end of file diff --git a/li.strolch.agent/src/main/java/li/strolch/agent/impl/TransactionalActivityMap.java b/li.strolch.agent/src/main/java/li/strolch/agent/impl/TransactionalActivityMap.java new file mode 100644 index 000000000..3b06a72ec --- /dev/null +++ b/li.strolch.agent/src/main/java/li/strolch/agent/impl/TransactionalActivityMap.java @@ -0,0 +1,46 @@ +/* + * Copyright 2015 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.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 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 List doQuery(StrolchTransaction tx, ActivityQuery query, ActivityVisitor activityVisitor) { + return getDao(tx).doQuery(query, activityVisitor); + } +} diff --git a/li.strolch.agent/src/main/java/li/strolch/agent/impl/TransactionalOrderMap.java b/li.strolch.agent/src/main/java/li/strolch/agent/impl/TransactionalOrderMap.java index 76ddb40fd..ccb2bcc36 100644 --- a/li.strolch.agent/src/main/java/li/strolch/agent/impl/TransactionalOrderMap.java +++ b/li.strolch.agent/src/main/java/li/strolch/agent/impl/TransactionalOrderMap.java @@ -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 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 diff --git a/li.strolch.agent/src/main/java/li/strolch/agent/impl/TransactionalRealm.java b/li.strolch.agent/src/main/java/li/strolch/agent/impl/TransactionalRealm.java index 56a1b8a4e..bd7cfd173 100644 --- a/li.strolch.agent/src/main/java/li/strolch/agent/impl/TransactionalRealm.java +++ b/li.strolch.agent/src/main/java/li/strolch/agent/impl/TransactionalRealm.java @@ -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 diff --git a/li.strolch.agent/src/main/java/li/strolch/agent/impl/TransactionalResourceMap.java b/li.strolch.agent/src/main/java/li/strolch/agent/impl/TransactionalResourceMap.java index 6b93b56a6..6f6eeebfa 100644 --- a/li.strolch.agent/src/main/java/li/strolch/agent/impl/TransactionalResourceMap.java +++ b/li.strolch.agent/src/main/java/li/strolch/agent/impl/TransactionalResourceMap.java @@ -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 @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 diff --git a/li.strolch.agent/src/main/java/li/strolch/agent/impl/TransientRealm.java b/li.strolch.agent/src/main/java/li/strolch/agent/impl/TransientRealm.java index e470c2769..755aa30b6 100644 --- a/li.strolch.agent/src/main/java/li/strolch/agent/impl/TransientRealm.java +++ b/li.strolch.agent/src/main/java/li/strolch/agent/impl/TransientRealm.java @@ -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 diff --git a/li.strolch.agent/src/main/java/li/strolch/persistence/api/AbstractTransaction.java b/li.strolch.agent/src/main/java/li/strolch/persistence/api/AbstractTransaction.java index 3cb1a9cd1..a5dced69a 100644 --- a/li.strolch.agent/src/main/java/li/strolch/persistence/api/AbstractTransaction.java +++ b/li.strolch.agent/src/main/java/li/strolch/persistence/api/AbstractTransaction.java @@ -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 doQuery(ActivityQuery query) { + assertQueryAllowed(query); + return getActivityMap().doQuery(this, query, new NoStrategyActivityVisitor()); + } + + @Override + public List doQuery(ActivityQuery query, ActivityVisitor activityVisitor) { + assertQueryAllowed(query); + return getActivityMap().doQuery(this, query, activityVisitor); + } @Override public List 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 getActivitiesBy(StringListParameter refP) throws StrolchException { + DBC.PRE.assertNotNull("refP", refP); + return getActivityMap().getBy(this, refP, false); + } + + @Override + public List getActivitiesBy(StringListParameter refP, boolean assertExists) throws StrolchException { + DBC.PRE.assertNotNull("refP", refP); + return getActivityMap().getBy(this, refP, assertExists); + } + + + + + + + + + + + + + + + + + + + + + @Override public void flush() { try { diff --git a/li.strolch.agent/src/main/java/li/strolch/persistence/api/ActivityDao.java b/li.strolch.agent/src/main/java/li/strolch/persistence/api/ActivityDao.java new file mode 100644 index 000000000..79e2ea9ad --- /dev/null +++ b/li.strolch.agent/src/main/java/li/strolch/persistence/api/ActivityDao.java @@ -0,0 +1,30 @@ +/* + * Copyright 2015 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.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 + */ +public interface ActivityDao extends StrolchDao { + + public List doQuery(ActivityQuery query, ActivityVisitor activityVisitor); +} diff --git a/li.strolch.agent/src/main/java/li/strolch/persistence/api/PersistenceHandler.java b/li.strolch.agent/src/main/java/li/strolch/persistence/api/PersistenceHandler.java index 94d224fd5..eb52f4716 100644 --- a/li.strolch.agent/src/main/java/li/strolch/persistence/api/PersistenceHandler.java +++ b/li.strolch.agent/src/main/java/li/strolch/persistence/api/PersistenceHandler.java @@ -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 diff --git a/li.strolch.agent/src/main/java/li/strolch/persistence/api/StrolchTransaction.java b/li.strolch.agent/src/main/java/li/strolch/persistence/api/StrolchTransaction.java index dace3d9ed..081e50bfc 100644 --- a/li.strolch.agent/src/main/java/li/strolch/persistence/api/StrolchTransaction.java +++ b/li.strolch.agent/src/main/java/li/strolch/persistence/api/StrolchTransaction.java @@ -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 List doQuery(ResourceQuery query, ResourceVisitor resourceVisitor); + /** + *

+ * Performs the given {@link ActivityQuery} returning the resulting list of {@link Activity Activities}. + *

+ * + *

+ * Note: Should the result be mapped to different objects, then use + * {@link #doQuery(ActivityQuery, ActivityVisitor)} + *

+ * + * @param query + * the query to perform + * + * @return the result list, never null + */ + public List doQuery(ActivityQuery query); + + /** + *

+ * 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 + *

+ * + *

+ * 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 + *

+ * + * @param query + * the query to perform + * + * @return the result list of elements as returned by the {@link ActivityVisitor}, never null + */ + public List doQuery(ActivityQuery query, ActivityVisitor activityVisitor); + /** *

* 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. Note: Any - * missing resources are not returned! + * missing resources are not returned unless assertExists is true * * @throws StrolchException * if the {@link StringListParameter} is not a properly configured as a reference parameter */ public List 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. Note: Any + * missing activities are not returned! + * + * @throws StrolchException + * if the {@link StringListParameter} is not a properly configured as a reference parameter + */ + public List 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. Note: Any + * missing activities are not returned unless assertExists is true + * + * @throws StrolchException + * if the {@link StringListParameter} is not a properly configured as a reference parameter + */ + public List 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. Note: Any missing - * orders are not returned! + * orders are not returned unless assertExists is true * * @throws StrolchException * if the {@link StringListParameter} is not a properly configured as a reference parameter diff --git a/li.strolch.agent/src/main/java/li/strolch/persistence/inmemory/InMemoryActivityDao.java b/li.strolch.agent/src/main/java/li/strolch/persistence/inmemory/InMemoryActivityDao.java new file mode 100644 index 000000000..665a6b86a --- /dev/null +++ b/li.strolch.agent/src/main/java/li/strolch/persistence/inmemory/InMemoryActivityDao.java @@ -0,0 +1,35 @@ +/* + * Copyright 2015 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.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 implements ActivityDao { + + @Override + public List doQuery(ActivityQuery activityQuery, ActivityVisitor activityVisitor) { + InMemoryActivityQueryVisitor visitor = new InMemoryActivityQueryVisitor(); + InMemoryQuery query = visitor.visit(activityQuery, activityVisitor); + return query.doQuery(this); + } +} diff --git a/li.strolch.agent/src/main/java/li/strolch/persistence/inmemory/InMemoryPersistence.java b/li.strolch.agent/src/main/java/li/strolch/persistence/inmemory/InMemoryPersistence.java index 328d7baff..251da8939 100644 --- a/li.strolch.agent/src/main/java/li/strolch/persistence/inmemory/InMemoryPersistence.java +++ b/li.strolch.agent/src/main/java/li/strolch/persistence/inmemory/InMemoryPersistence.java @@ -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; + } } } diff --git a/li.strolch.agent/src/main/java/li/strolch/persistence/inmemory/InMemoryPersistenceHandler.java b/li.strolch.agent/src/main/java/li/strolch/persistence/inmemory/InMemoryPersistenceHandler.java index 7b6165d41..b702dfa43 100644 --- a/li.strolch.agent/src/main/java/li/strolch/persistence/inmemory/InMemoryPersistenceHandler.java +++ b/li.strolch.agent/src/main/java/li/strolch/persistence/inmemory/InMemoryPersistenceHandler.java @@ -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); diff --git a/li.strolch.agent/src/main/java/li/strolch/runtime/StrolchConstants.java b/li.strolch.agent/src/main/java/li/strolch/runtime/StrolchConstants.java index d6ad8a2be..514dbd8df 100644 --- a/li.strolch.agent/src/main/java/li/strolch/runtime/StrolchConstants.java +++ b/li.strolch.agent/src/main/java/li/strolch/runtime/StrolchConstants.java @@ -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)) diff --git a/li.strolch.agent/src/main/java/li/strolch/runtime/query/inmemory/ActivityTypeNavigator.java b/li.strolch.agent/src/main/java/li/strolch/runtime/query/inmemory/ActivityTypeNavigator.java new file mode 100644 index 000000000..6aed9ba5b --- /dev/null +++ b/li.strolch.agent/src/main/java/li/strolch/runtime/query/inmemory/ActivityTypeNavigator.java @@ -0,0 +1,31 @@ +/* + * Copyright 2015 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.runtime.query.inmemory; + +import li.strolch.model.activity.Activity; + +/** + * @author Robert von Burg + */ +public class ActivityTypeNavigator extends StrolchTypeNavigator { + + /** + * @param type + */ + public ActivityTypeNavigator(String type) { + super(type); + } +} diff --git a/li.strolch.agent/src/main/java/li/strolch/runtime/query/inmemory/InMemoryActivityQueryVisitor.java b/li.strolch.agent/src/main/java/li/strolch/runtime/query/inmemory/InMemoryActivityQueryVisitor.java new file mode 100644 index 000000000..482ef37a7 --- /dev/null +++ b/li.strolch.agent/src/main/java/li/strolch/runtime/query/inmemory/InMemoryActivityQueryVisitor.java @@ -0,0 +1,65 @@ +/* + * Copyright 2015 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.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 + */ +public class InMemoryActivityQueryVisitor extends InMemoryQueryVisitor implements + ActivityQueryVisitor { + + public InMemoryActivityQueryVisitor() { + super(); + } + + @Override + protected InMemoryQueryVisitor newInstance() { + return new InMemoryActivityQueryVisitor(); + } + + public InMemoryQuery visit(ActivityQuery activityQuery, ActivityVisitor activityVisitor) { + DBC.PRE.assertNotNull("ActivityVisitor may not be null!", activityVisitor); //$NON-NLS-1$ + activityQuery.accept(this); + + Navigator navigator = getNavigator(); + if (navigator == null) { + String msg = "Query is missing a navigation!"; //$NON-NLS-1$ + throw new QueryException(msg); + } + + List> 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())); + } +} diff --git a/li.strolch.model/src/main/java/li/strolch/model/ActivityVisitor.java b/li.strolch.model/src/main/java/li/strolch/model/ActivityVisitor.java new file mode 100644 index 000000000..29bc2f3b4 --- /dev/null +++ b/li.strolch.model/src/main/java/li/strolch/model/ActivityVisitor.java @@ -0,0 +1,29 @@ +/* + * Copyright 2015 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.model; + +import li.strolch.model.activity.Activity; +import li.strolch.model.visitor.StrolchElementVisitor; + +/** + * @author Robert von Burg + * @param + */ +public interface ActivityVisitor extends StrolchElementVisitor { + + @Override + public U visit(Activity element); +} diff --git a/li.strolch.model/src/main/java/li/strolch/model/ModelStatistics.java b/li.strolch.model/src/main/java/li/strolch/model/ModelStatistics.java index b5cdc70b7..31a70b0d4 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/ModelStatistics.java +++ b/li.strolch.model/src/main/java/li/strolch/model/ModelStatistics.java @@ -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(); } diff --git a/li.strolch.model/src/main/java/li/strolch/model/StrolchModelConstants.java b/li.strolch.model/src/main/java/li/strolch/model/StrolchModelConstants.java index 88493f81a..de8cf59a2 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/StrolchModelConstants.java +++ b/li.strolch.model/src/main/java/li/strolch/model/StrolchModelConstants.java @@ -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 */ diff --git a/li.strolch.model/src/main/java/li/strolch/model/query/ActivityQuery.java b/li.strolch.model/src/main/java/li/strolch/model/query/ActivityQuery.java new file mode 100644 index 000000000..8ec8c16dd --- /dev/null +++ b/li.strolch.model/src/main/java/li/strolch/model/query/ActivityQuery.java @@ -0,0 +1,66 @@ +/* + * Copyright 2015 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.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 + */ +public class ActivityQuery extends StrolchElementQuery { + + /** + * @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)); + } +} diff --git a/li.strolch.model/src/main/java/li/strolch/model/query/ActivityQueryVisitor.java b/li.strolch.model/src/main/java/li/strolch/model/query/ActivityQueryVisitor.java new file mode 100644 index 000000000..56540f8a4 --- /dev/null +++ b/li.strolch.model/src/main/java/li/strolch/model/query/ActivityQueryVisitor.java @@ -0,0 +1,24 @@ +/* + * Copyright 2015 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.model.query; + +/** + * @author Robert von Burg + */ +public interface ActivityQueryVisitor extends StrolchRootElementSelectionVisitor, ParameterSelectionVisitor { + + // marker interface +} diff --git a/li.strolch.model/src/main/java/li/strolch/model/visitor/NoStrategyActivityVisitor.java b/li.strolch.model/src/main/java/li/strolch/model/visitor/NoStrategyActivityVisitor.java new file mode 100644 index 000000000..702ef986d --- /dev/null +++ b/li.strolch.model/src/main/java/li/strolch/model/visitor/NoStrategyActivityVisitor.java @@ -0,0 +1,30 @@ +/* + * Copyright 2015 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.model.visitor; + +import li.strolch.model.ActivityVisitor; +import li.strolch.model.activity.Activity; + +/** + * @author Robert von Burg + */ +public class NoStrategyActivityVisitor implements ActivityVisitor { + + @Override + public Activity visit(Activity element) { + return element; + } +} diff --git a/li.strolch.model/src/main/java/li/strolch/model/xml/SimpleStrolchElementListener.java b/li.strolch.model/src/main/java/li/strolch/model/xml/SimpleStrolchElementListener.java index 2806d5a41..c339f2725 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/xml/SimpleStrolchElementListener.java +++ b/li.strolch.model/src/main/java/li/strolch/model/xml/SimpleStrolchElementListener.java @@ -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 @@ -29,6 +30,7 @@ public class SimpleStrolchElementListener implements StrolchElementListener { private List resources; private List orders; + private List 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 getActivities() { + if (this.activities == null) + return Collections.emptyList(); + return this.activities; + } } diff --git a/li.strolch.model/src/main/java/li/strolch/model/xml/StrolchElementListener.java b/li.strolch.model/src/main/java/li/strolch/model/xml/StrolchElementListener.java index 58f1d419c..617694624 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/xml/StrolchElementListener.java +++ b/li.strolch.model/src/main/java/li/strolch/model/xml/StrolchElementListener.java @@ -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 @@ -27,4 +28,6 @@ public interface StrolchElementListener { public void notifyResource(Resource resource); public void notifyOrder(Order order); + + public void notifyActivity(Activity activity); } diff --git a/li.strolch.model/src/test/java/li/strolch/model/XmlModelDefaultHandlerTest.java b/li.strolch.model/src/test/java/li/strolch/model/XmlModelDefaultHandlerTest.java index 24c845d66..2d698523f 100644 --- a/li.strolch.model/src/test/java/li/strolch/model/XmlModelDefaultHandlerTest.java +++ b/li.strolch.model/src/test/java/li/strolch/model/XmlModelDefaultHandlerTest.java @@ -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 - * */ @SuppressWarnings("nls") public class XmlModelDefaultHandlerTest { @@ -42,8 +42,9 @@ public class XmlModelDefaultHandlerTest { @Test public void shouldParseXmlModelFile() { - final Map resourceMap = new HashMap<>(); - final Map orderMap = new HashMap<>(); + Map resourceMap = new HashMap<>(); + Map orderMap = new HashMap<>(); + Map 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); } } diff --git a/li.strolch.model/src/test/java/li/strolch/model/activity/ActionTest.java b/li.strolch.model/src/test/java/li/strolch/model/activity/ActionTest.java index d13369e62..57633d2ce 100644 --- a/li.strolch.model/src/test/java/li/strolch/model/activity/ActionTest.java +++ b/li.strolch.model/src/test/java/li/strolch/model/activity/ActionTest.java @@ -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 startChange = new ValueChange<>(STATE_TIME_10, new IntegerValue(1)); startChange.setStateId(STATE_INTEGER_ID); - action.addChange(startChange); + this.action.addChange(startChange); IValueChange 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); - } - } diff --git a/li.strolch.model/src/test/java/li/strolch/model/activity/ActivityTest.java b/li.strolch.model/src/test/java/li/strolch/model/activity/ActivityTest.java index c0570c0d6..16047919b 100644 --- a/li.strolch.model/src/test/java/li/strolch/model/activity/ActivityTest.java +++ b/li.strolch.model/src/test/java/li/strolch/model/activity/ActivityTest.java @@ -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); - } - } diff --git a/li.strolch.model/src/test/resources/data/StrolchModel.xml b/li.strolch.model/src/test/resources/data/StrolchModel.xml index 818cccb2d..6016993e0 100644 --- a/li.strolch.model/src/test/resources/data/StrolchModel.xml +++ b/li.strolch.model/src/test/resources/data/StrolchModel.xml @@ -14,5 +14,6 @@ + \ No newline at end of file diff --git a/li.strolch.model/src/test/resources/data/activities/Activities.xml b/li.strolch.model/src/test/resources/data/activities/Activities.xml new file mode 100644 index 000000000..be58063a8 --- /dev/null +++ b/li.strolch.model/src/test/resources/data/activities/Activities.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/li.strolch.model/src/test/java/li/strolch/model/activity/action.xml b/li.strolch.model/src/test/resources/data/activities/action.xml similarity index 100% rename from li.strolch.model/src/test/java/li/strolch/model/activity/action.xml rename to li.strolch.model/src/test/resources/data/activities/action.xml diff --git a/li.strolch.model/src/test/java/li/strolch/model/activity/activity.xml b/li.strolch.model/src/test/resources/data/activities/activity.xml similarity index 100% rename from li.strolch.model/src/test/java/li/strolch/model/activity/activity.xml rename to li.strolch.model/src/test/resources/data/activities/activity.xml