ProcessDefinitionEntity.java 11.7 KB
Newer Older
T
tombaeyens 已提交
1 2 3 4 5 6 7 8 9 10 11 12
/* 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.
 */
T
tombaeyens 已提交
13
package org.activiti.engine.impl.persistence.entity;
T
tombaeyens 已提交
14

15 16
import java.io.IOException;
import java.io.ObjectInputStream;
T
tombaeyens 已提交
17
import java.util.ArrayList;
18
import java.util.HashMap;
19
import java.util.HashSet;
20
import java.util.List;
21
import java.util.Map;
22
import java.util.Set;
T
tombaeyens 已提交
23

T
Merge  
Tijs Rademakers 已提交
24
import org.activiti.bpmn.model.FlowElement;
25
import org.activiti.engine.ProcessEngineConfiguration;
26
import org.activiti.engine.delegate.Expression;
27 28
import org.activiti.engine.delegate.event.ActivitiEventType;
import org.activiti.engine.delegate.event.impl.ActivitiEventBuilder;
29
import org.activiti.engine.delegate.event.impl.ActivitiEventSupport;
30
import org.activiti.engine.impl.bpmn.parser.BpmnParse;
31
import org.activiti.engine.impl.context.Context;
32
import org.activiti.engine.impl.db.HasRevision;
T
tombaeyens 已提交
33
import org.activiti.engine.impl.db.PersistentObject;
34
import org.activiti.engine.impl.form.StartFormHandler;
T
tombaeyens 已提交
35
import org.activiti.engine.impl.identity.Authentication;
T
tombaeyens 已提交
36
import org.activiti.engine.impl.pvm.process.ProcessDefinitionImpl;
T
tombaeyens 已提交
37
import org.activiti.engine.impl.pvm.runtime.InterpretableExecution;
38
import org.activiti.engine.impl.task.TaskDefinition;
39
import org.activiti.engine.repository.ProcessDefinition;
40
import org.activiti.engine.task.IdentityLinkType;
T
tombaeyens 已提交
41 42

/**
T
Merge  
Tijs Rademakers 已提交
43 44
 * @author Joram Barrez
 * @author Tijs Rademakers
T
tombaeyens 已提交
45
 */
46
public class ProcessDefinitionEntity extends ProcessDefinitionImpl implements ProcessDefinition, PersistentObject, HasRevision {
T
tombaeyens 已提交
47

T
Merge  
Tijs Rademakers 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 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 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
    private static final long serialVersionUID = 1L;

    protected String key;
    protected int revision = 1;
    protected int version;
    protected String category;
    protected String deploymentId;
    protected String resourceName;
    protected String tenantId = ProcessEngineConfiguration.NO_TENANT_ID;
    protected Integer historyLevel;
    protected StartFormHandler startFormHandler;
    protected String diagramResourceName;
    protected boolean isGraphicalNotationDefined;
    protected Map<String, TaskDefinition> taskDefinitions;
    protected Map<String, Object> variables;
    protected boolean hasStartFormKey;
    protected int suspensionState = SuspensionState.ACTIVE.getStateCode();
    protected boolean isIdentityLinksInitialized = false;
    protected List<IdentityLinkEntity> definitionIdentityLinkEntities = new ArrayList<IdentityLinkEntity>();
    protected Set<Expression> candidateStarterUserIdExpressions = new HashSet<Expression>();
    protected Set<Expression> candidateStarterGroupIdExpressions = new HashSet<Expression>();
    protected transient ActivitiEventSupport eventSupport;

    public ProcessDefinitionEntity() {
        super(null);
        eventSupport = new ActivitiEventSupport();
    }

    private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
        in.defaultReadObject();
        eventSupport = new ActivitiEventSupport();

    }

    public ExecutionEntity createProcessInstance(String businessKey, FlowElement initialFlowElement) {
        ExecutionEntity processInstance = null;

        if (initial == null) {
            processInstance = (ExecutionEntity) super.createProcessInstance();
        } else {
            processInstance = (ExecutionEntity) super.createProcessInstanceForInitial(initial);
        }

        processInstance.setExecutions(new ArrayList<ExecutionEntity>());
        processInstance.setProcessDefinition(processDefinition);
        // Do not initialize variable map (let it happen lazily)

        // Set business key (if any)
        if (businessKey != null) {
            processInstance.setBusinessKey(businessKey);
        }

        // Inherit tenant id (if any)
        if (getTenantId() != null) {
            processInstance.setTenantId(getTenantId());
        }

        // Reset the process instance in order to have the db-generated process
        // instance id available
        processInstance.setProcessInstance(processInstance);

        // initialize the template-defined data objects as variables first
        Map<String, Object> dataObjectVars = getVariables();
        if (dataObjectVars != null) {
            processInstance.setVariables(dataObjectVars);
        }

        String authenticatedUserId = Authentication.getAuthenticatedUserId();
        String initiatorVariableName = (String) getProperty(BpmnParse.PROPERTYNAME_INITIATOR_VARIABLE_NAME);
        if (initiatorVariableName != null) {
            processInstance.setVariable(initiatorVariableName, authenticatedUserId);
        }
        if (authenticatedUserId != null) {
            processInstance.addIdentityLink(authenticatedUserId, null, IdentityLinkType.STARTER);
        }

        Context.getCommandContext().getHistoryManager().recordProcessInstanceStart(processInstance, initialFlowElement);

        if (Context.getProcessEngineConfiguration().getEventDispatcher().isEnabled()) {
            Context.getProcessEngineConfiguration().getEventDispatcher()
                    .dispatchEvent(ActivitiEventBuilder.createEntityEvent(ActivitiEventType.ENTITY_CREATED, processInstance));
        }

        return processInstance;
    }

    public ExecutionEntity createProcessInstance(String businessKey) {
        return createProcessInstance(businessKey, null);
    }

    public ExecutionEntity createProcessInstance() {
        return createProcessInstance(null);
    }

    @Override
    protected InterpretableExecution newProcessInstance() {
        ExecutionEntity processInstance = new ExecutionEntity();
        processInstance.insert();
        return processInstance;
    }

    public IdentityLinkEntity addIdentityLink(String userId, String groupId) {
        IdentityLinkEntity identityLinkEntity = new IdentityLinkEntity();
        getIdentityLinks().add(identityLinkEntity);
        identityLinkEntity.setProcessDef(this);
        identityLinkEntity.setUserId(userId);
        identityLinkEntity.setGroupId(groupId);
        identityLinkEntity.setType(IdentityLinkType.CANDIDATE);
        identityLinkEntity.insert();
        return identityLinkEntity;
    }

    public void deleteIdentityLink(String userId, String groupId) {
        List<IdentityLinkEntity> identityLinks = Context.getCommandContext().getIdentityLinkEntityManager()
                .findIdentityLinkByProcessDefinitionUserAndGroup(id, userId, groupId);

        for (IdentityLinkEntity identityLink : identityLinks) {
            Context.getCommandContext().getIdentityLinkEntityManager().deleteIdentityLink(identityLink, false);
        }
    }

    public List<IdentityLinkEntity> getIdentityLinks() {
        if (!isIdentityLinksInitialized) {
            definitionIdentityLinkEntities = Context.getCommandContext().getIdentityLinkEntityManager().findIdentityLinksByProcessDefinitionId(id);
            isIdentityLinksInitialized = true;
        }

        return definitionIdentityLinkEntities;
    }

    public String toString() {
        return "ProcessDefinitionEntity[" + id + "]";
    }

    // getters and setters
    // //////////////////////////////////////////////////////

    public Object getPersistentState() {
        Map<String, Object> persistentState = new HashMap<String, Object>();
        persistentState.put("suspensionState", this.suspensionState);
        persistentState.put("category", this.category);
        return persistentState;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getDescription() {
        return description;
    }

    public String getDeploymentId() {
        return deploymentId;
    }

    public void setDeploymentId(String deploymentId) {
        this.deploymentId = deploymentId;
    }

    public int getVersion() {
        return version;
    }

    public void setVersion(int version) {
        this.version = version;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getResourceName() {
        return resourceName;
    }

    public void setResourceName(String resourceName) {
        this.resourceName = resourceName;
    }

    public String getTenantId() {
        return tenantId;
    }

    public void setTenantId(String tenantId) {
        this.tenantId = tenantId;
    }

    public Integer getHistoryLevel() {
        return historyLevel;
    }

    public void setHistoryLevel(Integer historyLevel) {
        this.historyLevel = historyLevel;
    }

    public StartFormHandler getStartFormHandler() {
        return startFormHandler;
    }

    public void setStartFormHandler(StartFormHandler startFormHandler) {
        this.startFormHandler = startFormHandler;
    }

    public Map<String, TaskDefinition> getTaskDefinitions() {
        return taskDefinitions;
    }

    public void setTaskDefinitions(Map<String, TaskDefinition> taskDefinitions) {
        this.taskDefinitions = taskDefinitions;
    }

    public Map<String, Object> getVariables() {
        return variables;
    }

    public void setVariables(Map<String, Object> variables) {
        this.variables = variables;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getDiagramResourceName() {
        return diagramResourceName;
    }

    public void setDiagramResourceName(String diagramResourceName) {
        this.diagramResourceName = diagramResourceName;
    }

    public boolean hasStartFormKey() {
        return hasStartFormKey;
    }

    public boolean getHasStartFormKey() {
        return hasStartFormKey;
    }

    public void setStartFormKey(boolean hasStartFormKey) {
        this.hasStartFormKey = hasStartFormKey;
    }

    public void setHasStartFormKey(boolean hasStartFormKey) {
        this.hasStartFormKey = hasStartFormKey;
    }

    public boolean isGraphicalNotationDefined() {
        return isGraphicalNotationDefined;
    }

    public boolean hasGraphicalNotation() {
        return isGraphicalNotationDefined;
    }

    public void setGraphicalNotationDefined(boolean isGraphicalNotationDefined) {
        this.isGraphicalNotationDefined = isGraphicalNotationDefined;
    }

    public int getRevision() {
        return revision;
    }

    public void setRevision(int revision) {
        this.revision = revision;
    }

    public int getRevisionNext() {
        return revision + 1;
    }

    public int getSuspensionState() {
        return suspensionState;
    }

    public void setSuspensionState(int suspensionState) {
        this.suspensionState = suspensionState;
    }

    public boolean isSuspended() {
        return suspensionState == SuspensionState.SUSPENDED.getStateCode();
    }

    public Set<Expression> getCandidateStarterUserIdExpressions() {
        return candidateStarterUserIdExpressions;
    }

    public void addCandidateStarterUserIdExpression(Expression userId) {
        candidateStarterUserIdExpressions.add(userId);
    }

    public Set<Expression> getCandidateStarterGroupIdExpressions() {
        return candidateStarterGroupIdExpressions;
    }

    public void addCandidateStarterGroupIdExpression(Expression groupId) {
        candidateStarterGroupIdExpressions.add(groupId);
    }

    public ActivitiEventSupport getEventSupport() {
        return eventSupport;
    }
T
tombaeyens 已提交
363
}