package nl.enovation.commons.command;
import java.util.Collection;
import org.apache.commons.chain.Command;
import org.apache.commons.chain.Context;
public class ForeachCommand implements Command {
private String collectionProperty;
private String instanceProperty;
private Command command;
@SuppressWarnings("unchecked")
public boolean execute(Context context) throws Exception {
Collection<? extends Object> collection = (Collection<? extends Object>) context.get(collectionProperty);
if (collection != null) {
for(Object object : collection) {
context.put(instanceProperty, object);
if (PROCESSING_COMPLETE == command.execute(context)) {
return PROCESSING_COMPLETE;
} // else continue
}
}
return CONTINUE_PROCESSING;
}
public String getCollectionProperty() {
return collectionProperty;
}
public void setCollectionProperty(String collectionProperty) {
this.collectionProperty = collectionProperty;
}
public String getInstanceProperty() {
return instanceProperty;
}
public void setInstanceProperty(String instanceProperty) {
this.instanceProperty = instanceProperty;
}
public Command getCommand() {
return command;
}
public void setCommand(Command command) {
this.command = command;
}
}