[jboss-svn-commits] JBL Code SVN: r26622 - in labs/jbossrules/branches/salaboy_RelationalVariablePersistence/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance: persisters and 1 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Tue May 19 12:13:31 EDT 2009
Author: salaboy21
Date: 2009-05-19 12:13:31 -0400 (Tue, 19 May 2009)
New Revision: 26622
Modified:
labs/jbossrules/branches/salaboy_RelationalVariablePersistence/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/ProcessInstanceInfo.java
labs/jbossrules/branches/salaboy_RelationalVariablePersistence/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/persisters/JPAVariablePersister.java
labs/jbossrules/branches/salaboy_RelationalVariablePersistence/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/variabletypes/VariableInstanceInfo.java
Log:
last changes, VariableInstanceInfo inside the ProcessInstanceInfo from a List to a Map
Modified: labs/jbossrules/branches/salaboy_RelationalVariablePersistence/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/ProcessInstanceInfo.java
===================================================================
--- labs/jbossrules/branches/salaboy_RelationalVariablePersistence/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/ProcessInstanceInfo.java 2009-05-19 15:23:04 UTC (rev 26621)
+++ labs/jbossrules/branches/salaboy_RelationalVariablePersistence/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/ProcessInstanceInfo.java 2009-05-19 16:13:31 UTC (rev 26622)
@@ -11,6 +11,7 @@
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
+import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
@@ -51,12 +52,10 @@
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
- private Long processInstanceId;
-
+ private Long processInstanceId;
@Version
@Column(name = "OPTLOCK")
- private int version;
-
+ private int version;
private String processId;
private Date startDate;
private Date lastReadDate;
@@ -65,17 +64,19 @@
// TODO How do I mark a process instance info as dirty when the process
// instance
// has changed (so that byte array is regenerated and saved) ?
- private @Lob
+ private
+ @Lob
byte[] processInstanceByteArray;
@CollectionOfElements
private Set<String> eventTypes = new HashSet<String>();
- private @Transient
+ private
+ @Transient
ProcessInstance processInstance;
- @Transient private Environment env;
- private boolean serializeVariables=false;
- @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
- //@OneToMany(cascade=CascadeType.ALL, mappedBy="processInstanceInfo", fetch=FetchType.EAGER)
- private List<VariableInstanceInfo> variables = new ArrayList<VariableInstanceInfo>();
+ @Transient
+ private Environment env;
+ private boolean serializeVariables = false;
+ @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy="processInstanceInfo")
+ private Map<String,VariableInstanceInfo> variables = new HashMap<String,VariableInstanceInfo>();
ProcessInstanceInfo() {
}
@@ -86,7 +87,7 @@
startDate = new Date();
}
- public ProcessInstanceInfo(ProcessInstance processInstance, Environment env) {
+ public ProcessInstanceInfo(ProcessInstance processInstance, Environment env) {
this.processInstance = processInstance;
this.processId = processInstance.getProcessId();
startDate = new Date();
@@ -131,39 +132,41 @@
bais, (InternalRuleBase) workingMemory.getRuleBase(), null, null);
context.wm = (InternalWorkingMemory) workingMemory;
ProcessInstanceMarshaller marshaller = getMarshallerFromContext(context);
- processInstance = marshaller.readProcessInstance(context,false);
+ processInstance = marshaller.readProcessInstance(context, false);
restoreVariables();
context.close();
} catch (IOException e) {
e.printStackTrace();
throw new IllegalArgumentException(
- "IOException while loading process instance: "
- + e.getMessage());
+ "IOException while loading process instance: " + e.getMessage());
}
}
return processInstance;
}
- private void restoreVariables() {
- VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((RuleFlowProcessInstance)
- this.processInstance).getContextInstance(VariableScope.VARIABLE_SCOPE);
+ private void restoreVariables() {
- for(VariableInstanceInfo variableInfo : this.getVariables()){
+ VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((RuleFlowProcessInstance) this.processInstance).getContextInstance(VariableScope.VARIABLE_SCOPE);
+ Set<String> keySet = this.getVariables().keySet();
+ Iterator<String> keyIt = keySet.iterator();
+ while(keyIt.hasNext()){
+
VariablePersistenceStrategy persistenceStrategy =
- VariablePersistenceStrategyFactory.getVariablePersistenceStrategyFactory();
- variableScopeInstance.setVariable(variableInfo.getName(), persistenceStrategy.getVariable(variableInfo,this.env));
+ VariablePersistenceStrategyFactory.getVariablePersistenceStrategyFactory();
+ VariableInstanceInfo variableInfo = this.getVariables().get(keyIt.next());
+
+ variableScopeInstance.setVariable(variableInfo.getName(), persistenceStrategy.getVariable(variableInfo, this.env));
}
- //variableScopeInstance.setProcessInstance(this.processInstance);
-
+
+
}
private ProcessInstanceMarshaller getMarshallerFromContext(
MarshallerReaderContext context) throws IOException {
ObjectInputStream stream = context.stream;
String processInstanceType = stream.readUTF();
- return ProcessMarshallerRegistry.INSTANCE
- .getMarshaller(processInstanceType);
+ return ProcessMarshallerRegistry.INSTANCE.getMarshaller(processInstanceType);
}
private void saveProcessInstanceType(MarshallerWriteContext context,
@@ -180,18 +183,16 @@
try {
MarshallerWriteContext context = new MarshallerWriteContext(baos,
null, null, null, null);
- String processType = ((ProcessInstanceImpl) processInstance).getProcess()
- .getType();
+ String processType = ((ProcessInstanceImpl) processInstance).getProcess().getType();
saveProcessInstanceType(context, processInstance, processType);
ProcessInstanceMarshaller marshaller = ProcessMarshallerRegistry.INSTANCE.getMarshaller(processType);
marshaller.writeProcessInstance(
- context, processInstance,false);
+ context, processInstance, false);
persistVariables();
context.close();
} catch (IOException e) {
throw new IllegalArgumentException(
- "IOException while storing process instance "
- + processInstance.getId() + ": " + e.getMessage());
+ "IOException while storing process instance " + processInstance.getId() + ": " + e.getMessage());
}
byte[] newByteArray = baos.toByteArray();
if (!Arrays.equals(newByteArray, processInstanceByteArray)) {
@@ -204,16 +205,13 @@
}
}
}
- private void persistVariables() {
- // VariableScopeInstance variableScopeInstance =
- // (VariableScopeInstance)
- // ((RuleFlowProcessInstance)getProcessInstance(processInstanceInfo.getId()))
- // .getContextInstance( VariableScope.VARIABLE_SCOPE );
- VariableScopeInstance variableScopeInstance = (VariableScopeInstance)
- ((RuleFlowProcessInstance)
- this
- .processInstance)
- .getContextInstance( VariableScope.VARIABLE_SCOPE );
+
+ private void persistVariables() {
+ // VariableScopeInstance variableScopeInstance =
+ // (VariableScopeInstance)
+ // ((RuleFlowProcessInstance)getProcessInstance(processInstanceInfo.getId()))
+ // .getContextInstance( VariableScope.VARIABLE_SCOPE );
+ VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((RuleFlowProcessInstance) this.processInstance).getContextInstance(VariableScope.VARIABLE_SCOPE);
Map<String, Object> processVariables = variableScopeInstance.getVariables();
List<String> keys = new ArrayList<String>(processVariables.keySet());
Collections.sort(keys, new Comparator<String>() {
@@ -223,45 +221,47 @@
}
});
VariablePersistenceStrategy persistenceStrategy = VariablePersistenceStrategyFactory.getVariablePersistenceStrategyFactory("PersistenceStrategies.conf");
- this.clearVariables();
+ // this.clearVariables();
+
for (String key : keys) {
- Iterator<VariableInstanceInfo> it = this.getVariables().iterator();
- while(it.hasNext()){
- if(it.next().getName().equals(key)){
- //must update not persist again
+
+
+
+ VariableInstanceInfo variable = persistenceStrategy.persistVariable(key, processVariables.get(key), this.env);
+ if (variable != null) {
+ variable.setProcessInstanceInfo(this);
+ this.addVariable(key,variable);
}
- }
+
- VariableInstanceInfo variable = persistenceStrategy.persistVariable(key, processVariables.get(key),this.env);
- if(variable != null){
- // variable.setProcessInstanceInfo(this);
- this.addVariables(variable);
- }
}
//If we wont find any VariableInstanceInfo that mactches with the persistence strategy,
//we just serialize the variables inside the blob
- if(this.getVariables().size() == 0){
- this.setSerializeVariables(true);
- }
+// if (this.getVariables().size() == 0) {
+// this.setSerializeVariables(true);
+// }
}
+
/**
* @return the variables
*/
- public List<VariableInstanceInfo> getVariables() {
+ public Map<String,VariableInstanceInfo> getVariables() {
return variables;
}
/**
* @param variables the variables to set
*/
- public void setVariables(List<VariableInstanceInfo> variables) {
+ public void setVariables(Map<String,VariableInstanceInfo> variables) {
this.variables = variables;
}
- public void addVariables(VariableInstanceInfo variable){
- this.getVariables().add(variable);
+
+ public void addVariable(String name, VariableInstanceInfo variable) {
+ this.getVariables().put(name, variable);
}
- public void clearVariables(){
+
+ public void clearVariables() {
this.getVariables().clear();
}
Modified: labs/jbossrules/branches/salaboy_RelationalVariablePersistence/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/persisters/JPAVariablePersister.java
===================================================================
--- labs/jbossrules/branches/salaboy_RelationalVariablePersistence/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/persisters/JPAVariablePersister.java 2009-05-19 15:23:04 UTC (rev 26621)
+++ labs/jbossrules/branches/salaboy_RelationalVariablePersistence/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/persisters/JPAVariablePersister.java 2009-05-19 16:13:31 UTC (rev 26622)
@@ -53,28 +53,10 @@
EntityManager em = (EntityManager) env.get(EnvironmentName.ENTITY_MANAGER);
VariableInstanceInfo variable = null;
try {
- ut = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
+
- if (ut.getStatus() == Status.STATUS_NO_TRANSACTION) {
- // If there is no transaction then start one, we will commit within the same Command
- ut.begin();
- localTransaction = true;
- }
+ // em.joinTransaction();
- EntityManager localEm = (EntityManager) env.get(EnvironmentName.ENTITY_MANAGER);
- if (localEm == null || !localEm.isOpen()) {
- localEm = emf.createEntityManager(); // no need to call joinTransaction as it will do so if one already exists
- env.set(EnvironmentName.ENTITY_MANAGER, localEm);
- }
-
- if (em == null) {
- // there must have been a rollback to lazily re-initialise the state
- em = emf.createEntityManager();
-
- }
-
- em.joinTransaction();
-
em.persist(o);
variable = new JPAPersistedVariable();
@@ -96,42 +78,14 @@
((JPAPersistedVariable) variable).setVarid(idValue.longValue());
((JPAPersistedVariable) variable).setVartype(o.getClass().getCanonicalName());
- em.persist(variable);
-
+ // em.persist(variable);
- if (localTransaction) {
- // it's a locally created transaction so commit
- ut.commit();
-
- // cleanup local entity manager
- if (localEm.isOpen()) {
- localEm.close();
- }
- env.set(EnvironmentName.ENTITY_MANAGER, null);
-
- // clean up cached process and work item instances
-
- }
-
-
-
} catch (Throwable t1) {
t1.printStackTrace();
- if (localTransaction) {
- try {
- if (ut != null) {
- ut.rollback();
- }
- throw new RuntimeException("Could not execute command",
- t1);
- } catch (Throwable t2) {
- throw new RuntimeException("Could not rollback transaction",
- t2);
- }
- } else {
+
throw new RuntimeException("Could not execute command",
t1);
- }
+
}
return variable;
}
Modified: labs/jbossrules/branches/salaboy_RelationalVariablePersistence/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/variabletypes/VariableInstanceInfo.java
===================================================================
--- labs/jbossrules/branches/salaboy_RelationalVariablePersistence/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/variabletypes/VariableInstanceInfo.java 2009-05-19 15:23:04 UTC (rev 26621)
+++ labs/jbossrules/branches/salaboy_RelationalVariablePersistence/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/variabletypes/VariableInstanceInfo.java 2009-05-19 16:13:31 UTC (rev 26622)
@@ -17,7 +17,6 @@
package org.drools.persistence.processinstance.variabletypes;
-import org.drools.persistence.processinstance.*;
import java.io.Serializable;
@@ -31,8 +30,8 @@
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
-import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
+import org.drools.persistence.processinstance.ProcessInstanceInfo;
@@ -48,9 +47,11 @@
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String name;
-// @ManyToOne()
-// @JoinColumn(name="processInstanceInfoId")
-// private ProcessInstanceInfo processInstanceInfo;
+ @ManyToOne
+ private ProcessInstanceInfo processInstanceInfo;
+
+
+
private String persister;
public Long getId() {
@@ -75,19 +76,6 @@
this.name = name;
}
- /**
- * @return the processInstanceInfo
- */
-// public ProcessInstanceInfo getProcessInstanceInfo() {
-// return processInstanceInfo;
-// }
-//
-// /**
-// * @param processInstanceInfo the processInstanceInfo to set
-// */
-// public void setProcessInstanceInfo(ProcessInstanceInfo processInstanceInfo) {
-// this.processInstanceInfo = processInstanceInfo;
-// }
/**
* @return the persister
@@ -102,4 +90,11 @@
public void setPersister(String persister) {
this.persister = persister;
}
+ public ProcessInstanceInfo getProcessInstanceInfo() {
+ return processInstanceInfo;
+ }
+
+ public void setProcessInstanceInfo(ProcessInstanceInfo processInstanceInfo) {
+ this.processInstanceInfo = processInstanceInfo;
+ }
}
More information about the jboss-svn-commits
mailing list