[New] Added Activity.getActionsWithState(State)

This commit is contained in:
Robert von Burg 2017-02-22 17:46:27 +01:00
parent 89739717f2
commit 967b54f279
2 changed files with 41 additions and 19 deletions

View File

@ -16,9 +16,11 @@
package li.strolch.model.activity; package li.strolch.model.activity;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
@ -39,8 +41,8 @@ import li.strolch.model.visitor.StrolchRootElementVisitor;
import li.strolch.utils.dbc.DBC; import li.strolch.utils.dbc.DBC;
/** /**
* Parameterized object grouping a collection of {@link Activity} and * Parameterized object grouping a collection of {@link Activity} and {@link Action} objects defining the process to be
* {@link Action} objects defining the process to be scheduled * scheduled
* *
* @author Martin Smock <martin.smock@bluewin.ch> * @author Martin Smock <martin.smock@bluewin.ch>
*/ */
@ -118,35 +120,30 @@ public class Activity extends GroupedParameterizedElement
} }
/** /**
* Returns true if this {@link Activity} contains any children i.e. any of * Returns true if this {@link Activity} contains any children i.e. any of {@link Action} or {@link Activity}
* {@link Action} or {@link Activity}
* *
* @return true if this {@link Activity} contains any children i.e. any of * @return true if this {@link Activity} contains any children i.e. any of {@link Action} or {@link Activity}
* {@link Action} or {@link Activity}
*/ */
public boolean hasElements() { public boolean hasElements() {
return this.elements != null && !this.elements.isEmpty(); return this.elements != null && !this.elements.isEmpty();
} }
/** /**
* Returns true if this {@link Activity} contains a child with the given id. * Returns true if this {@link Activity} contains a child with the given id. The element instance type is ignored,
* The element instance type is ignored, i.e. {@link Action} or * i.e. {@link Action} or {@link Activity}
* {@link Activity}
* *
* @param id * @param id
* the id of the element to check for * the id of the element to check for
* *
* @return true if this {@link Activity} contains a child with the given id. * @return true if this {@link Activity} contains a child with the given id. The element instance type is ignored,
* The element instance type is ignored, i.e. {@link Action} or * i.e. {@link Action} or {@link Activity}
* {@link Activity}
*/ */
public boolean hasElement(String id) { public boolean hasElement(String id) {
return this.elements != null && this.elements.containsKey(id); return this.elements != null && this.elements.containsKey(id);
} }
/** /**
* add an activity element to the <code>LinkedHashMap</code> of * add an activity element to the <code>LinkedHashMap</code> of <code>IActivityElements</code>
* <code>IActivityElements</code>
* *
* @param activityElement * @param activityElement
* @return the element added * @return the element added
@ -200,8 +197,7 @@ public class Activity extends GroupedParameterizedElement
} }
/** /**
* @return get the <code>LinkedHashMap</code> of * @return get the <code>LinkedHashMap</code> of <code>IActivityElements</code>
* <code>IActivityElements</code>
*/ */
public Map<String, IActivityElement> getElements() { public Map<String, IActivityElement> getElements() {
if (this.elements == null) if (this.elements == null)
@ -209,13 +205,27 @@ public class Activity extends GroupedParameterizedElement
return this.elements; return this.elements;
} }
public List<Action> getActionsWithState(State state) {
List<Action> actions = new ArrayList<>();
getActionsWithState(actions, state);
return actions;
}
private void getActionsWithState(List<Action> actions, State state) {
for (IActivityElement element : this.elements.values()) {
if (element instanceof Activity)
((Activity) element).getActionsWithState(actions, state);
else if (element.getState() == state)
actions.add((Action) element);
}
}
/** /**
* @return the iterator for entries, which include the id as key and the * @return the iterator for entries, which include the id as key and the {@link IActivityElement} as value
* {@link IActivityElement} as value
*/ */
public Iterator<Entry<String, IActivityElement>> elementIterator() { public Iterator<Entry<String, IActivityElement>> elementIterator() {
if (this.elements == null) if (this.elements == null)
return Collections.<String, IActivityElement>emptyMap().entrySet().iterator(); return Collections.<String, IActivityElement> emptyMap().entrySet().iterator();
return this.elements.entrySet().iterator(); return this.elements.entrySet().iterator();
} }

View File

@ -159,6 +159,7 @@ public class ModelTest {
validateBag(bag); validateBag(bag);
Action action = activity.getElement("action_" + actId); Action action = activity.getElement("action_" + actId);
action.setState(State.ERROR);
assertEquals("action_" + actId, action.getId()); assertEquals("action_" + actId, action.getId());
assertEquals("Action " + actName, action.getName()); assertEquals("Action " + actName, action.getName());
assertEquals("Use", action.getType()); assertEquals("Use", action.getType());
@ -174,6 +175,7 @@ public class ModelTest {
validateBag(bag); validateBag(bag);
action = activity.getElement("action_" + actId); action = activity.getElement("action_" + actId);
action.setState(State.ERROR);
assertEquals("action_" + actId, action.getId()); assertEquals("action_" + actId, action.getId());
assertEquals("Action " + actName, action.getName()); assertEquals("Action " + actName, action.getName());
assertEquals("Use", action.getType()); assertEquals("Use", action.getType());
@ -189,12 +191,22 @@ public class ModelTest {
validateBag(bag); validateBag(bag);
action = activity.getElement("action1_" + actId); action = activity.getElement("action1_" + actId);
action.setState(State.EXECUTION);
assertEquals("action1_" + actId, action.getId()); assertEquals("action1_" + actId, action.getId());
assertEquals("Action " + actName, action.getName()); assertEquals("Action " + actName, action.getName());
assertEquals("Use", action.getType()); assertEquals("Use", action.getType());
assertEquals(ACTION_RES_ID, action.getResourceId()); assertEquals(ACTION_RES_ID, action.getResourceId());
assertEquals(ACTION_RES_TYPE, action.getResourceType()); assertEquals(ACTION_RES_TYPE, action.getResourceType());
assertEquals(changes, action.getChanges()); assertEquals(changes, action.getChanges());
List<Action> actions = activity.getRootElement().getActionsWithState(State.ERROR);
assertEquals(2, actions.size());
actions = activity.getRootElement().getActionsWithState(State.EXECUTION);
assertEquals(1, actions.size());
actions = activity.getRootElement().getActionsWithState(State.CREATED);
assertEquals(1, actions.size());
} }
@Test @Test