001: /*
002: * JBoss, Home of Professional Open Source
003: * Copyright 2005, JBoss Inc., and individual contributors as indicated
004: * by the @authors tag. See the copyright.txt in the distribution for a
005: * full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jbpm.db;
023:
024: import org.hibernate.Session;
025:
026: /**
027: * contains queries to search the database for process instances and tokens
028: * based on process variableInstances.
029: * <p><b>NOTE: TODO</b></p>
030: */
031: public class ContextSession {
032:
033: JbpmSession jbpmSession = null;
034: Session session = null;
035:
036: public ContextSession(JbpmSession jbpmSession) {
037: this .jbpmSession = jbpmSession;
038: this .session = jbpmSession.getSession();
039: }
040:
041: public ContextSession(Session session) {
042: this .session = session;
043: this .jbpmSession = new JbpmSession(session);
044: }
045:
046: /**
047: * converts all newly created variable values that are inside the
048: * variableUpdates into {@link VariableInstance}s
049: * as a preparation for storage in the database.
050: public void mergeVariableUpdates(Map variableUpdates, Map variableInstances, TokenVariableMap tokenVariableMap, TaskInstance taskInstance, Token token) {
051: if (variableUpdates!=null) {
052: Iterator iter = variableUpdates.entrySet().iterator();
053: while (iter.hasNext()) {
054: Map.Entry entry = (Map.Entry) iter.next();
055: String name = (String) entry.getKey();
056: Object value = entry.getValue();
057:
058: VariableInstance variableInstance = (variableInstances!=null ? (VariableInstance) variableInstances.get(name) : null);
059: if ( (variableInstance!=null)
060: && (!variableInstance.supports(value))
061: ) {
062: // delete the old variable instance
063: if (taskInstance!=null) {
064: log.debug("task variable type change. deleting '"+name+"' from task instance '"+taskInstance+"'");
065: taskInstance.deleteVariable(name);
066: } else if (tokenVariableMap!=null) {
067: log.debug("process variable type change. deleting '"+name+"' from token '"+token+"'");
068: tokenVariableMap.deleteVariable(name);
069: }
070: // make sure the variable instance gets recreated below
071: variableInstances.remove(name);
072: }
073:
074: if ( (variableInstances==null)
075: || (! variableInstances.containsKey(name))
076: ) {
077: // it's a creation
078: variableInstance = VariableInstance.create(token, name, value);
079:
080: // set the apropriate backpointers
081: if (tokenVariableMap!=null) {
082: log.debug("created variable instance '"+name+"' for token '"+token+"': "+value);
083: tokenVariableMap.addVariableInstance(variableInstance);
084: }
085: if (taskInstance!=null) {
086: log.debug("created variable instance '"+name+"' for task instance '"+taskInstance+"': "+value);
087: taskInstance.addVariableInstance(variableInstance);
088: }
089:
090: } else if (variableInstance!=null) {
091: // it's an update
092: variableInstance = (VariableInstance) variableInstances.get(name);
093: if (taskInstance!=null) {
094: log.debug("updated task variable instance '"+name+"' to '"+value+"' for '"+taskInstance+"'");
095: } else {
096: log.debug("updated process variable instance '"+name+"' to '"+value+"' for '"+token+"'");
097: }
098: variableInstance.setValue(value);
099: }
100: }
101: }
102: }
103:
104: public void updateProcessContextVariables(ContextInstance contextInstance) {
105: if (contextInstance!=null) {
106: Map tokenVariableMaps = contextInstance.getTokenVariableMaps();
107: if (tokenVariableMaps!=null) {
108: Iterator iter = tokenVariableMaps.values().iterator();
109: while (iter.hasNext()) {
110: TokenVariableMap tokenVariableMap = (TokenVariableMap) iter.next();
111: mergeVariableUpdates(tokenVariableMap.getVariableUpdates(), tokenVariableMap.getVariableInstances(), tokenVariableMap, null, tokenVariableMap.getToken());
112: }
113: }
114: }
115: }
116:
117: public void updateTaskInstanceVariables(TaskMgmtInstance taskMgmtInstance) {
118: if (taskMgmtInstance!=null) {
119: Collection updatedTaskInstances = taskMgmtInstance.getTaskInstancesWithVariableUpdates();
120: if (updatedTaskInstances!=null) {
121: Iterator iter = updatedTaskInstances.iterator();
122: while (iter.hasNext()) {
123: TaskInstance taskInstance = (TaskInstance) iter.next();
124: mergeVariableUpdates(taskInstance.getVariableUpdates(), taskInstance.getVariableInstances(), null, taskInstance, taskInstance.getToken());
125: }
126: }
127: }
128: }
129: */
130:
131: // private static final Log log = LogFactory.getLog(ContextSession.class);
132: }
|