/*
* 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.impl.memory;
import org.apache.agila.services.user.UserID;
import org.apache.agila.services.user.GroupID;
import org.apache.agila.services.task.TaskID;
import org.apache.agila.services.task.Task;
import org.apache.agila.services.task.AbstractTaskService;
import org.apache.agila.services.task.TaskImpl;
import org.apache.agila.services.user.GroupID;
import org.apache.agila.engine.InstanceID;
import java.util.Map;
import java.util.HashMap;
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: TaskServiceImpl.java 288 2005-07-26 10:37:38Z gregw $
*/
public class TaskServiceImpl extends AbstractTaskService {
private Map bucket = new HashMap();
private Map taskIDMap = new HashMap();
private static int id = 0;
public List getTasksForGroups(GroupID[] teamIDs, int status) {
return null; /** TODO */
}
public boolean lockTaskForUser(TaskID taskID, UserID userID) {
Task task = getTaskByID(taskID);
if (task != null && task.getUserID() == null) {
task.setUserID(userID);
return true;
}
return false;
}
public void unlockTaskForUser(TaskID taskID, UserID userID) {
Task task = getTaskByID(taskID);
if (task != null && task.getUserID() == null) {
task.setUserID(null);
}
}
public List getTasksForInstance(InstanceID instanceID, int type) {
List answer = new ArrayList();
for (Iterator iter = taskIDMap.values().iterator(); iter.hasNext();) {
Task task = (Task) iter.next();
if (instanceID.equals(task.getInstanceID()) && (type == Task.TASK_ALL || task.getTaskStatus() == type)) {
answer.add(task);
}
}
return answer;
}
public int setTaskStatusForInstance(InstanceID instanceID, int oldType, int newType) {
int answer = 0;
for (Iterator iter = taskIDMap.values().iterator(); iter.hasNext();) {
Task task = (Task) iter.next();
if (instanceID.equals(task.getInstanceID()) && (oldType == Task.TASK_ALL || task.getTaskStatus() == oldType)) {
task.setTaskStatus(newType);
answer++;
}
}
return answer;
}
/**
* Add or update a task.
*/
protected void internalSave(Task ti) {
UserID userID = ti.getUserID();
List taskList = (List) bucket.get( userID );
if( taskList == null ) {
taskList = new ArrayList();
}
if( ti.getTaskID() != null ) {
// Update mode
int index = -1;
for( int i = 0; i < taskList.size(); i++ ) {
Task task = (Task)taskList.get( i );
if( task.getTaskID().equals( ti.getTaskID() ) ) {
index = i;
break;
}
}
// Task should exist in the list in order for the update to work.
if (index == -1)
throw new Error("Task should exist");
taskList.set( index, ti );
} else { // Add mode
TaskID tid = new TaskID( ++id );
ti.setTaskID( tid );
taskList.add( ti );
}
bucket.put( userID, taskList );
taskIDMap.put(ti.getTaskID(), ti);
}
protected List internalGetTasksByUserID(UserID userID, int type) {
List l = (List) bucket.get(userID);
List ret = new ArrayList();
/*
* make a copy to prevent caller for dorking the list
*/
if (l != null) {
l = new ArrayList(l);
Iterator it = l.iterator();
while(it.hasNext()) {
TaskImpl ti = (TaskImpl) it.next();
if ( (ti.getStatus() & type) > 0) {
ret.add(ti);
}
}
}
return ret;
}
protected Task internalGetTask(TaskID taskID) {
return (Task) taskIDMap.get(taskID);
}
}