package net.fp.rp.workflow.flow;
import net.fp.rp.workflow.db.MessageDao;
import net.fp.rp.workflow.flow.state.FlowStateBean;
import java.util.List;
import net.fp.rp.common.exception.RpException;
import net.fp.rp.spring.FullWorkflowStrategy;
import net.fp.rp.spring.IStarterStrategy;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jbpm.graph.exe.ProcessInstance;
/**
* An implementation of a strategy object used when Spring starts a workflow.
*
* This one
* <ul>
* <li>Reads a list of messages_logs from the database</li>
* <li>Starts a process flow for each instance of message_log</li>
* <li>Execute the given process from end to end</li>
* *
*
* This project uses Apache, Spring, JBoss and other GPL Licenced Code
*
* @author paul.browne
*/
public class DBWorkflowStrategy extends FullWorkflowStrategy implements
IStarterStrategy {
// Logger
private static Log log = LogFactory.getLog(DBWorkflowStrategy.class);
// Internal Handle for Dao
private MessageDao dao = null;
//the max number of messages ; default to 100, but allow it to be set by Spring
private int maxNumberOfMessages=100;
//if this is present , only execute this message id
private Integer singleMessageId=null;
/**
* Execute the workflow in accordence with the strategy employed
*
* @param processInstance
* to implement
*/
public void executeWorkflow(ProcessInstance inProcessInstance) {
// Local Variables
ProcessInstance newInstance = null;
FlowStateBean flowBean = new FlowStateBean();
List result = null;
// Get the Messages Delta from the Dao
try {
if(getSingleMessageId()==null){
//Get the full set of next messages
log.debug("Getting the next "+maxNumberOfMessages+" messages");
result = dao.getObjectsToProcess(null,maxNumberOfMessages);
} else {
log.debug("Getting Message Log id:"+getSingleMessageId());
result = dao.getObjectsToProcess(getSingleMessageId(),1);
}
log.info("About to process "+result.size()+" messages");
// Iterate over them
for (int i = 0; i < result.size(); i++){
// thisMsg = (MessageLogs) result.get(i);
log.debug("");
log.debug("");
log.debug("Start Message Cycle:" + i );
// Create a new instance of the workflow
log.debug("creating new WorkflowInstance");
newInstance = inProcessInstance.getProcessDefinition()
.createProcessInstance();
// Make our message log object available to the flow
newInstance.getContextInstance()
.createVariable(
FlowStateBean.FLOW_STATE_BEAN_IN_CONTEXT_NAME,
flowBean);
// Execute it to the full (using the super class)
log.debug("Executing WorkflowInstance");
super.executeFullWorkflow(newInstance);
newInstance.end();
//inProcessInstance.removeInstance();
log.debug("");
log.debug("");
log.debug("Complete Message Cycle:" + i);
}
} catch (RpException rp) {
log.warn("Attempting to recover from Exception");
log.warn(rp);
}
}
/**
* @param dao
* The dao to set.
*/
public void setDao(MessageDao dao) {
this.dao = dao;
}
/**
* @return Returns the dao.
*/
public MessageDao getDao() {
return dao;
}
/**
* @param maxNumberOfMessages The maxNumberOfMessages to set.
*/
public void setMaxNumberOfMessages(int maxNumberOfMessages) {
this.maxNumberOfMessages = maxNumberOfMessages;
}
/**
* @return Returns the maxNumberOfMessages.
*/
public int getMaxNumberOfMessages() {
return maxNumberOfMessages;
}
/**
* @param singleMessageId The singleMessageId to set.
*/
public void setSingleMessageId(Integer singleMessageId) {
this.singleMessageId = singleMessageId;
}
/**
* @return Returns the singleMessageId.
*/
public Integer getSingleMessageId() {
return singleMessageId;
}
}