VariableMap.java 5.8 KB
Newer Older
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.runtime;
14 15

import java.io.Serializable;
16
import java.util.Collection;
17
import java.util.HashMap;
18 19
import java.util.HashSet;
import java.util.List;
20 21 22
import java.util.Map;
import java.util.Set;

23
import org.activiti.engine.ActivitiException;
T
tombaeyens 已提交
24
import org.activiti.engine.impl.cfg.ProcessEngineConfiguration;
T
tombaeyens 已提交
25
import org.activiti.engine.impl.db.DbSqlSession;
T
tombaeyens 已提交
26
import org.activiti.engine.impl.history.HistoricVariableUpdateEntity;
27 28 29 30
import org.activiti.engine.impl.interceptor.CommandContext;
import org.activiti.engine.impl.variable.Type;
import org.activiti.engine.impl.variable.VariableTypes;

31 32 33 34 35


/**
 * @author Tom Baeyens
 */
36
public class VariableMap implements Map<String, Object> , Serializable {
37 38 39
  
  private static final long serialVersionUID = 1L;
  
40 41
  protected String executionId;
  protected String processInstanceId;
42
  protected Map<String, VariableInstanceEntity> variableInstances = null;
T
tombaeyens 已提交
43
  protected static ThreadLocal<Boolean> isExternalUpdateThreadLocal = new ThreadLocal<Boolean>();
44

45 46 47
  public VariableMap(String executionId, String processInstanceId) {
    this.executionId = executionId;
    this.processInstanceId = processInstanceId;
48
  }
49
  
T
tombaeyens 已提交
50 51 52 53
  public static void setExternalUpdate(Boolean isExternalUpdate) {
    isExternalUpdateThreadLocal.set(isExternalUpdate);
  }
  
54 55 56 57 58 59
  /** returns an initialized empty variable map */
  public static VariableMap createNewInitialized(String executionId, String processInstanceId) {
    VariableMap variableMap = new VariableMap(executionId, processInstanceId);
    variableMap.variableInstances = new HashMap<String, VariableInstanceEntity>();
    return variableMap;
  }
60

61
  protected void ensureInitialized() {
62 63 64 65 66 67 68 69 70 71
    if (variableInstances==null) {
      variableInstances = new HashMap<String, VariableInstanceEntity>();
      CommandContext commandContext = CommandContext.getCurrent();
      if (commandContext == null) {
        throw new ActivitiException("lazy loading outside command context");
      }
      List<VariableInstanceEntity> variableInstancesList = commandContext.getRuntimeSession().findVariableInstancesByExecutionId(executionId);
      for (VariableInstanceEntity variableInstance : variableInstancesList) {
        variableInstances.put(variableInstance.getName(), variableInstance);
      }
72
    }
73 74
  }

75 76 77 78 79 80 81 82 83 84 85 86 87
  public Object get(Object key) {
    ensureInitialized();
    VariableInstanceEntity variableInstance = variableInstances.get(key);
    if (variableInstance==null) {
      return null;
    }
    return variableInstance.getValue();
  }
  

  public boolean isEmpty() {
    ensureInitialized();
    return variableInstances.isEmpty();
88 89
  }

90 91 92
  public boolean containsKey(Object key) {
    ensureInitialized();
    return variableInstances.containsKey(key);
93 94
  }

95 96 97
  public Set<String> keySet() {
    ensureInitialized();
    return variableInstances.keySet();
98 99
  }

100 101 102 103 104 105 106 107
  public Object put(String key, Object value) {
    ensureInitialized();
    VariableInstanceEntity variableInstance = variableInstances.get(key);
    if ((variableInstance != null) && (!variableInstance.getType().isAbleToStore(value))) {
      // delete variable
      remove(key);
      variableInstance = null;
    }
T
tombaeyens 已提交
108
    CommandContext commandContext = CommandContext.getCurrent();
109
    if (variableInstance == null) {
T
tombaeyens 已提交
110
      VariableTypes variableTypes = commandContext
111 112 113 114 115 116 117 118 119 120 121
        .getProcessEngineConfiguration()
        .getVariableTypes();
      
      Type type = variableTypes.findVariableType(value);
  
      variableInstance = VariableInstanceEntity.createAndInsert(key, type, value);
      variableInstance.setExecutionId(executionId);
      variableInstance.setProcessInstanceId(processInstanceId);
    }
    
    variableInstance.setValue(value);
T
tombaeyens 已提交
122 123 124 125 126 127 128 129 130 131
    
    int historyLevel = commandContext.getProcessEngineConfiguration().getHistoryLevel();
    if ( (historyLevel>=ProcessEngineConfiguration.HISTORYLEVEL_AUDIT)
         && (Boolean.TRUE.equals(isExternalUpdateThreadLocal.get()))
       ) {
      DbSqlSession dbSqlSession = commandContext.getDbSqlSession();
      HistoricVariableUpdateEntity historicVariableUpdate = new HistoricVariableUpdateEntity(variableInstance, dbSqlSession);
      dbSqlSession.insert(historicVariableUpdate);
    }
    
132
    variableInstances.put(key, variableInstance);
133 134 135 136 137 138 139
    return null;
  }

  public Object remove(Object key) {
    ensureInitialized();
    VariableInstanceEntity variableInstance = variableInstances.remove(key);
    if (variableInstance != null) {
T
tombaeyens 已提交
140
      variableInstance.delete();
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
    }
    return null;
  }

  public void putAll(Map< ? extends String, ? extends Object> m) {
    for (String key: m.keySet()) {
      put(key, m.get(key));
    }
  }

  public int size() {
    ensureInitialized();
    return variableInstances.size();
  }

  public void clear() {
    ensureInitialized();
    Set<String> keys = new HashSet<String>(variableInstances.keySet());
    for (String key: keys) {
      remove(key);
    }
  }

  // unsupported map operations ///////////////////////////////////////////////
  
  public boolean containsValue(Object value) {
    throw new UnsupportedOperationException("please implement me");
168 169
  }

170 171
  public Set<java.util.Map.Entry<String, Object>> entrySet() {
    throw new UnsupportedOperationException("please implement me");
172 173
  }

174 175
  public Collection<Object> values() {
    throw new UnsupportedOperationException("please implement me");
176 177
  }
}