/*
* Copyright 2004 The Apache Software Foundation.
*
* 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 org.apache.agila.services;
import org.apache.agila.engine.InstanceID;
import org.apache.agila.engine.Instance;
import org.apache.agila.engine.Token;
import org.apache.agila.engine.EngineMessage;
import org.apache.agila.model.BusinessProcessID;
import org.apache.agila.model.BusinessProcess;
import org.apache.agila.model.NodeID;
import org.apache.agila.model.Node;
import org.apache.agila.impl.EngineMessageImpl;
import org.apache.agila.services.log.LogService;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
/**
*
* @author <a href="mailto:geir@gluecode.com">Geir Magnusson Jr.</a>
* @version $Id: AbstractInstanceService.java 38 2005-06-01 19:39:54Z chirino $
*/
public abstract class AbstractInstanceService implements InstanceService {
private TokenService tokenService;
private BusinessProcessService businessProcessService;
private QueueService queueService;
private LogService logService;
public void setTokenService(TokenService ts) {
tokenService = ts;
}
public void setBusinessProcessService(BusinessProcessService bps) {
this.businessProcessService = bps;
}
public void setQueueService(QueueService qs) {
this.queueService = qs;
}
public void setLogService(LogService logger) {
this.logService = logger;
}
public InstanceID newInstance(BusinessProcessID processID, Map params) {
Instance ei = internalCreate(processID, params);
BusinessProcess graph = businessProcessService.getGraphByID(processID);
if (graph == null) {
logService.error("Unable to get process for ID = " + processID);
return null;
}
ei.setGraphName(graph.getName());
save(ei);
return ei.getInstanceID();
}
public void start(InstanceID id) {
Instance instance = getInstanceByID(id);
/*
* now make a token
*/
BusinessProcess bp = businessProcessService.getGraphByID(instance.getBusinessProcessID());
InstanceID instanceID = instance.getInstanceID();
Node root = bp.getRoot();
NodeID nodeId = root.getNodeId();
Token token = tokenService.newToken(instanceID, nodeId, Token.PRE);
/*
* and a message
*/
EngineMessage em = new EngineMessageImpl();
em.setCurrentTokenID(token.getTokenID());
queueService.enqueue(em);
}
public void save(Instance instance) {
internalSave(instance);
}
public Instance getInstanceByID(InstanceID id) {
return internalGetByID(id);
}
public InstanceInfo getInstanceInfoByID(InstanceID id) {
Instance instance = getInstanceByID(id);
InstanceInfo info = new InstanceInfo(instance);
populateNodeTrack(info);
return info;
}
/**
* TODO - scrap this. Imagine 1,000,000 instances....
* @return
*/
public List listInstanceInfo() {
List l= internalListInstanceInfo();
if( l != null ) {
/*
* now add the tokenlists
*/
List data = new ArrayList();
Iterator it = l.iterator();
while(it.hasNext()) {
InstanceInfo info = (InstanceInfo) it.next();
populateNodeTrack(info);
data.add(info);
}
return data;
}
return( null );
}
private void populateNodeTrack(InstanceInfo info) {
BusinessProcess businessProcess =
businessProcessService.getGraphByID(info.getBusinessProcessID());
List activeTokens = tokenService.getActiveTokensForInstance(info.getInstanceID());
List nodes = new ArrayList();
for( Iterator iterator = activeTokens.iterator(); iterator.hasNext(); ) {
Token token = (Token) iterator.next();
NodeID nodeID = token.getCurrentNodeID();
String displayName = businessProcess.getNode(nodeID).getDisplayName();
nodes.add( displayName );
}
info.setNodeTrack((String[]) nodes.toArray(new String[nodes.size()]));
}
public InstanceServiceInfo getInstanceServiceInfo() {
return internalInstanceServiceInfo();
}
public void registerListener(InstanceLifecycleListener listener) {
}
public void deRegisterListener(InstanceLifecycleListener listener) {
}
/**
* Method to create a new instance. Implementations can just override to
* use a different kind of persistent store
* @param processID
* @param params
* @return new instances
*/
protected abstract Instance internalCreate(BusinessProcessID processID, Map params);
/**
* Method to save an instance (update/persiste). Implementations can just
* override.
*
* @param instance
*/
protected abstract void internalSave(Instance instance);
/**
* returns an instance by ID from the persistence store
*
* @param id
* @return Instance if found, or null
*/
protected abstract Instance internalGetByID(InstanceID id);
/**
* returns all instances from the persistence store
*
* @return List with 0 or more instances
*/
protected abstract List internalListInstanceInfo();
protected abstract InstanceServiceInfo internalInstanceServiceInfo();
}