UserTaskActivityBehavior.java 10.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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.activiti.engine.impl.bpmn.behavior;

J
Joram Barrez 已提交
15 16 17
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
T
Tijs Rademakers 已提交
18
import java.util.Iterator;
J
Joram Barrez 已提交
19
import java.util.List;
T
Tijs Rademakers 已提交
20 21
import java.util.Map;
import java.util.Set;
J
Joram Barrez 已提交
22

23
import org.activiti.engine.ActivitiException;
24
import org.activiti.engine.ActivitiIllegalArgumentException;
25
import org.activiti.engine.delegate.Expression;
26
import org.activiti.engine.delegate.TaskListener;
27 28
import org.activiti.engine.delegate.event.ActivitiEventType;
import org.activiti.engine.delegate.event.impl.ActivitiEventBuilder;
29
import org.activiti.engine.impl.bpmn.helper.SkipExpressionUtil;
30
import org.activiti.engine.impl.calendar.BusinessCalendar;
31
import org.activiti.engine.impl.calendar.DueDateBusinessCalendar;
32
import org.activiti.engine.impl.context.Context;
33
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
T
tombaeyens 已提交
34
import org.activiti.engine.impl.persistence.entity.TaskEntity;
35 36 37 38 39 40 41 42 43 44 45 46
import org.activiti.engine.impl.pvm.delegate.ActivityExecution;
import org.activiti.engine.impl.task.TaskDefinition;

/**
 * activity implementation for the user task.
 * 
 * @author Joram Barrez
 */
public class UserTaskActivityBehavior extends TaskActivityBehavior {

  protected TaskDefinition taskDefinition;

47
  public UserTaskActivityBehavior(TaskDefinition taskDefinition) {
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    this.taskDefinition = taskDefinition;
  }

  public void execute(ActivityExecution execution) throws Exception {
    TaskEntity task = TaskEntity.createAndInsert(execution);
    task.setExecution(execution);
    task.setTaskDefinition(taskDefinition);

    if (taskDefinition.getNameExpression() != null) {
      String name = (String) taskDefinition.getNameExpression().getValue(execution);
      task.setName(name);
    }

    if (taskDefinition.getDescriptionExpression() != null) {
      String description = (String) taskDefinition.getDescriptionExpression().getValue(execution);
      task.setDescription(description);
    }
    
66 67 68
    if(taskDefinition.getDueDateExpression() != null) {
      Object dueDate = taskDefinition.getDueDateExpression().getValue(execution);
      if(dueDate != null) {
69 70 71
        if (dueDate instanceof Date) {
          task.setDueDate((Date) dueDate);
        } else if (dueDate instanceof String) {
72 73 74 75 76
          BusinessCalendar businessCalendar = Context
            .getProcessEngineConfiguration()
            .getBusinessCalendarManager()
            .getBusinessCalendar(DueDateBusinessCalendar.NAME);
          task.setDueDate(businessCalendar.resolveDuedate((String) dueDate));
77
        } else {
78
          throw new ActivitiIllegalArgumentException("Due date expression does not resolve to a Date or Date string: " + 
79
              taskDefinition.getDueDateExpression().getExpressionText());
80 81 82
        }
      }
    }
T
trademak 已提交
83 84 85 86 87 88 89 90

    if (taskDefinition.getPriorityExpression() != null) {
      final Object priority = taskDefinition.getPriorityExpression().getValue(execution);
      if (priority != null) {
        if (priority instanceof String) {
          try {
            task.setPriority(Integer.valueOf((String) priority));
          } catch (NumberFormatException e) {
91
            throw new ActivitiIllegalArgumentException("Priority does not resolve to a number: " + priority, e);
T
trademak 已提交
92 93 94 95
          }
        } else if (priority instanceof Number) {
          task.setPriority(((Number) priority).intValue());
        } else {
96
          throw new ActivitiIllegalArgumentException("Priority expression does not resolve to a number: " + 
T
trademak 已提交
97 98 99 100
                  taskDefinition.getPriorityExpression().getExpressionText());
        }
      }
    }
101
    
102 103 104 105 106 107 108 109 110 111 112 113
    if (taskDefinition.getCategoryExpression() != null) {
    	final Object category = taskDefinition.getCategoryExpression().getValue(execution);
    	if (category != null) {
    		if (category instanceof String) {
    			task.setCategory((String) category);
    		} else {
    			 throw new ActivitiIllegalArgumentException("Category expression does not resolve to a string: " + 
               taskDefinition.getCategoryExpression().getExpressionText());
    		}
    	}
    }
    
J
Joram Barrez 已提交
114
    if (taskDefinition.getFormKeyExpression() != null) {
J
Joram Barrez 已提交
115
    	final Object formKey = taskDefinition.getFormKeyExpression().getValue(execution);
J
Joram Barrez 已提交
116 117 118 119 120 121 122 123 124 125
    	if (formKey != null) {
    		if (formKey instanceof String) {
    			task.setFormKey((String) formKey);
    		} else {
    			 throw new ActivitiIllegalArgumentException("FormKey expression does not resolve to a string: " + 
               taskDefinition.getFormKeyExpression().getExpressionText());
    		}
    	}
    }
    
126 127
    handleAssignments(task, execution);
   
128 129 130 131 132 133
    // All properties set, now firing 'create' events
    if (Context.getProcessEngineConfiguration().getEventDispatcher().isEnabled()) {
      Context.getProcessEngineConfiguration().getEventDispatcher().dispatchEvent(
        ActivitiEventBuilder.createEntityEvent(ActivitiEventType.TASK_CREATED, task));
    }

134
    task.fireEvent(TaskListener.EVENTNAME_CREATE);
135 136 137

    Expression skipExpression = taskDefinition.getSkipExpression();
    if (SkipExpressionUtil.isSkipExpressionEnabled(execution, skipExpression) &&
138 139
        SkipExpressionUtil.shouldSkipFlowElement(execution, skipExpression)) {
      
140 141
      leave(execution);
    }
142 143 144
  }

  public void signal(ActivityExecution execution, String signalName, Object signalData) throws Exception {
145
    if (!((ExecutionEntity)execution).getTasks().isEmpty())
146
      throw new ActivitiException("UserTask should not be signalled before complete");
147 148 149
    leave(execution);
  }

150
  @SuppressWarnings({ "unchecked", "rawtypes" })
151 152
  protected void handleAssignments(TaskEntity task, ActivityExecution execution) {
    if (taskDefinition.getAssigneeExpression() != null) {
F
fheremans 已提交
153
      task.setAssignee((String) taskDefinition.getAssigneeExpression().getValue(execution), true, false);
154
    }
155 156 157 158
    
    if (taskDefinition.getOwnerExpression() != null) {
      task.setOwner((String) taskDefinition.getOwnerExpression().getValue(execution));
    }
159 160 161 162 163

    if (!taskDefinition.getCandidateGroupIdExpressions().isEmpty()) {
      for (Expression groupIdExpr : taskDefinition.getCandidateGroupIdExpressions()) {
        Object value = groupIdExpr.getValue(execution);
        if (value instanceof String) {
164 165
          List<String> candiates = extractCandidates((String) value);
          task.addCandidateGroups(candiates);
166 167 168
        } else if (value instanceof Collection) {
          task.addCandidateGroups((Collection) value);
        } else {
169
          throw new ActivitiIllegalArgumentException("Expression did not resolve to a string or collection of strings");
170 171 172 173 174 175 176 177
        }
      }
    }

    if (!taskDefinition.getCandidateUserIdExpressions().isEmpty()) {
      for (Expression userIdExpr : taskDefinition.getCandidateUserIdExpressions()) {
        Object value = userIdExpr.getValue(execution);
        if (value instanceof String) {
178 179
          List<String> candiates = extractCandidates((String) value);
          task.addCandidateUsers(candiates);
180 181 182 183 184 185 186
        } else if (value instanceof Collection) {
          task.addCandidateUsers((Collection) value);
        } else {
          throw new ActivitiException("Expression did not resolve to a string or collection of strings");
        }
      }
    }
T
Tijs Rademakers 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231

    if (!taskDefinition.getCustomUserIdentityLinkExpressions().isEmpty()) {
      Map<String, Set<Expression>> identityLinks = taskDefinition.getCustomUserIdentityLinkExpressions();
      for (String identityLinkType : identityLinks.keySet()) {
        for (Expression idExpression : identityLinks.get(identityLinkType) ) {
          Object value = idExpression.getValue(execution);
          if (value instanceof String) {
            List<String> userIds = extractCandidates((String) value);
            for (String userId : userIds) {
              task.addUserIdentityLink(userId, identityLinkType);
            }
          } else if (value instanceof Collection) {
            Iterator userIdSet = ((Collection) value).iterator();
            while (userIdSet.hasNext()) {
              task.addUserIdentityLink((String)userIdSet.next(), identityLinkType);
            }
          } else {
            throw new ActivitiException("Expression did not resolve to a string or collection of strings");
          }
        }
      }
    }

    if (!taskDefinition.getCustomGroupIdentityLinkExpressions().isEmpty()) {
      Map<String, Set<Expression>> identityLinks = taskDefinition.getCustomGroupIdentityLinkExpressions();
      for (String identityLinkType : identityLinks.keySet()) {
        for (Expression idExpression : identityLinks.get(identityLinkType) ) {
          Object value = idExpression.getValue(execution);
          if (value instanceof String) {
            List<String> groupIds = extractCandidates((String) value);
            for (String groupId : groupIds) {
              task.addGroupIdentityLink(groupId, identityLinkType);
            }
          } else if (value instanceof Collection) {
            Iterator groupIdSet = ((Collection) value).iterator();
            while (groupIdSet.hasNext()) {
              task.addGroupIdentityLink((String)groupIdSet.next(), identityLinkType);
            }
          } else {
            throw new ActivitiException("Expression did not resolve to a string or collection of strings");
          }
        }
      }
    }
}
232

233 234 235 236 237 238 239 240 241 242
  /**
   * Extract a candidate list from a string. 
   * 
   * @param str
   * @return 
   */
  protected List<String> extractCandidates(String str) {
    return Arrays.asList(str.split("[\\s]*,[\\s]*"));
  }
  
243 244 245 246 247 248 249
  // getters and setters //////////////////////////////////////////////////////
  
  public TaskDefinition getTaskDefinition() {
    return taskDefinition;
  }
  
}