[jboss-svn-commits] JBL Code SVN: r24954 - labs/jbossrules/branches/salaboy_VariablePersistenceStrategy/drools-process/drools-process-enterprise/src/main/java/org/drools/persistence/processinstance.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Tue Jan 27 13:13:45 EST 2009


Author: salaboy21
Date: 2009-01-27 13:13:45 -0500 (Tue, 27 Jan 2009)
New Revision: 24954

Modified:
   labs/jbossrules/branches/salaboy_VariablePersistenceStrategy/drools-process/drools-process-enterprise/src/main/java/org/drools/persistence/processinstance/JPAProcessInstanceManager.java
Log:
When we add a new process now we need to take care about the process variables. Now there are not handled by the processInstance persistence, because now the variables can choose how each one of them will be persisted

Modified: labs/jbossrules/branches/salaboy_VariablePersistenceStrategy/drools-process/drools-process-enterprise/src/main/java/org/drools/persistence/processinstance/JPAProcessInstanceManager.java
===================================================================
--- labs/jbossrules/branches/salaboy_VariablePersistenceStrategy/drools-process/drools-process-enterprise/src/main/java/org/drools/persistence/processinstance/JPAProcessInstanceManager.java	2009-01-27 18:10:20 UTC (rev 24953)
+++ labs/jbossrules/branches/salaboy_VariablePersistenceStrategy/drools-process/drools-process-enterprise/src/main/java/org/drools/persistence/processinstance/JPAProcessInstanceManager.java	2009-01-27 18:13:45 UTC (rev 24954)
@@ -1,17 +1,30 @@
 package org.drools.persistence.processinstance;
 
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
 import java.util.ArrayList;
 import java.util.Collection;
 
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 import javax.persistence.EntityManager;
 
 import org.drools.WorkingMemory;
 import org.drools.common.InternalRuleBase;
 import org.drools.common.InternalWorkingMemory;
 import org.drools.process.core.Process;
+import org.drools.process.core.context.variable.VariableScope;
 import org.drools.process.instance.ProcessInstance;
 import org.drools.process.instance.ProcessInstanceManager;
+import org.drools.process.instance.context.variable.VariableScopeInstance;
 import org.drools.process.instance.impl.ProcessInstanceImpl;
+import org.drools.ruleflow.instance.RuleFlowProcessInstance;
+import org.drools.util.StringUtils;
 
 public class JPAProcessInstanceManager implements ProcessInstanceManager {
 
@@ -28,11 +41,37 @@
     
     public void addProcessInstance(ProcessInstance processInstance) {
         ProcessInstanceInfo processInstanceInfo = new ProcessInstanceInfo(processInstance);
+        resolveOutputVariables(processInstanceInfo);
         manager.persist(processInstanceInfo);
         ((ProcessInstance) processInstance).setId(processInstanceInfo.getId());
         processInstanceInfo.updateLastReadDate();
     }
-    
+
+    private void resolveOutputVariables(ProcessInstanceInfo processInstanceInfo) {
+        VariableScopeInstance variableScopeInstance =
+                    (VariableScopeInstance)
+                        ((RuleFlowProcessInstance)processInstanceInfo.getProcessInstance())
+                            .getContextInstance( VariableScope.VARIABLE_SCOPE );
+        Map<String, Object> processVariables = variableScopeInstance.getVariables();
+        List<String> keys = new ArrayList<String>(processVariables.keySet());
+        Collections.sort(keys, new Comparator<String>() {
+
+            public int compare(String o1, String o2) {
+                return o1.compareTo(o2);
+            }
+        });
+        VariablePersistenceStrategy persistenceStrategy = VariablePersistenceStrategyFactory.getVariablePersistenceStrategyFactory();
+        processInstanceInfo.clearVariables();
+        for (String key : keys) {
+            VariableInstanceInfo variable = persistenceStrategy.persistVariable(key, processVariables.get(key));
+            manager.persist(((JPAPersistedVariable)variable).getRealVariable());
+            updateVariableInstanceInfo(variable);
+            processInstanceInfo.addVariables(variable);
+        }
+    }
+
+
+
     public void internalAddProcessInstance(ProcessInstance processInstance) {
     }
 
@@ -70,4 +109,33 @@
     public void internalRemoveProcessInstance(ProcessInstance processInstance) {
     }
 
+    private void updateVariableInstanceInfo(VariableInstanceInfo variable) {
+         Object o=((JPAPersistedVariable)variable).getRealVariable();
+           Field[] fields= o.getClass().getDeclaredFields();
+        ((JPAPersistedVariable)variable).setVartype(o.getClass().getName());
+        for(Field field : fields){
+            if(field.isAnnotationPresent(javax.persistence.Id.class)){
+                String fieldName = field.getName();
+                Method getter;
+                try {
+                    getter = o.getClass().getDeclaredMethod("get" + StringUtils.ucFirst(fieldName), null);
+                    ((JPAPersistedVariable)variable).setVarid((Long) getter.invoke(o,null));
+                } catch (IllegalAccessException ex) {
+                    Logger.getLogger(JPAVariablePersister.class.getName()).log(Level.SEVERE, null, ex);
+                } catch (IllegalArgumentException ex) {
+                    Logger.getLogger(JPAVariablePersister.class.getName()).log(Level.SEVERE, null, ex);
+                } catch (InvocationTargetException ex) {
+                    Logger.getLogger(JPAVariablePersister.class.getName()).log(Level.SEVERE, null, ex);
+                } catch (NoSuchMethodException ex) {
+                    Logger.getLogger(JPAVariablePersister.class.getName()).log(Level.SEVERE, null, ex);
+                } catch (SecurityException ex) {
+                    Logger.getLogger(JPAVariablePersister.class.getName()).log(Level.SEVERE, null, ex);
+                }
+
+
+            }
+
+        }
+    }
+
 }




More information about the jboss-svn-commits mailing list