1 25 package org.ofbiz.shark.instance; 26 27 import org.enhydra.shark.api.internal.instancepersistence.PersistenceException; 28 import org.enhydra.shark.api.internal.instancepersistence.ProcessVariablePersistenceInterface; 29 import org.ofbiz.base.util.Debug; 30 import org.ofbiz.base.util.UtilMisc; 31 import org.ofbiz.base.util.UtilObject; 32 import org.ofbiz.entity.GenericDelegator; 33 import org.ofbiz.entity.GenericEntityException; 34 import org.ofbiz.entity.GenericValue; 35 import org.ofbiz.shark.container.SharkContainer; 36 37 44 public class ProcessVariable extends InstanceEntityObject implements ProcessVariablePersistenceInterface { 45 46 public static final String module = ProcessVariable.class.getName(); 47 48 protected GenericValue processVariable = null; 49 protected boolean newValue = false; 50 51 protected ProcessVariable(EntityPersistentMgr mgr, GenericDelegator delegator, String processVariableId) throws PersistenceException { 52 super(mgr, delegator); 53 if (this.delegator != null) { 54 try { 55 this.processVariable = delegator.findByPrimaryKey("WfProcessVariable", UtilMisc.toMap("processVariableId", processVariableId)); 56 } catch (GenericEntityException e) { 57 throw new PersistenceException(e); 58 } 59 } else { 60 Debug.logError("Invalid delegator object passed", module); 61 } 62 } 63 64 protected ProcessVariable(EntityPersistentMgr mgr, GenericValue processVariable) { 65 super(mgr, processVariable.getDelegator()); 66 this.processVariable = processVariable; 67 } 68 69 public ProcessVariable(EntityPersistentMgr mgr, GenericDelegator delegator) { 70 super(mgr, delegator); 71 this.newValue = true; 72 this.processVariable = delegator.makeValue("WfProcessVariable", UtilMisc.toMap("processVariableId", delegator.getNextSeqId("WfProcessVariable"))); 73 Debug.log("******* New process variable created", module); 74 } 75 76 public static ProcessVariable getInstance(EntityPersistentMgr mgr, GenericValue processVariable) { 77 ProcessVariable var = new ProcessVariable(mgr, processVariable); 78 if (var.isLoaded()) { 79 return var; 80 } 81 return null; 82 } 83 84 public static ProcessVariable getInstance(EntityPersistentMgr mgr, String processVariableId) throws PersistenceException { 85 ProcessVariable var = new ProcessVariable(mgr, SharkContainer.getDelegator(), processVariableId); 86 if (var.isLoaded()) { 87 return var; 88 } 89 return null; 90 } 91 92 public boolean isLoaded() { 93 if (processVariable == null) { 94 return false; 95 } 96 return true; 97 } 98 99 public void setProcessId(String pId) { 100 processVariable.set("processId", pId); 101 } 102 103 public String getProcessId() { 104 return processVariable.getString("processId"); 105 } 106 107 public void setDefinitionId(String defId) { 108 processVariable.set("definitionId", defId); 109 } 110 111 public String getDefinitionId() { 112 return processVariable.getString("definitionId"); 113 } 114 115 public void setValue(Object val) { 116 if (val instanceof String ) { 117 processVariable.set("valueField", "strValue"); 118 processVariable.set("strValue", val); 119 } else if (val instanceof Number ) { 120 if (val instanceof Double ) { 121 processVariable.set("valueField", "dblValue"); 122 processVariable.set("dblValue", val); 123 } else { 124 processVariable.set("valueField", "numValue"); 125 processVariable.set("numValue", val); 126 } 127 } else { 128 byte[] value = UtilObject.getBytes(val); 129 processVariable.setBytes("objValue", (value != null ? value : null)); 130 } 131 } 132 133 public Object getValue() { 134 String fieldName = processVariable.getString("valueField"); 135 if ("objValue".equals(fieldName)) { 136 byte[] value = processVariable.getBytes(fieldName); 137 return UtilObject.getObject(value); 138 } else { 139 return processVariable.get(fieldName); 140 } 141 } 142 143 public void store() throws GenericEntityException { 144 if (newValue) { 145 delegator.createOrStore(processVariable); 146 newValue = false; 147 } else { 148 delegator.store(processVariable); 149 } 150 } 151 152 public void reload() throws GenericEntityException { 153 if (!newValue) { 154 processVariable.refresh(); 155 } 156 } 157 158 public void remove() throws GenericEntityException { 159 if (!newValue) { 160 delegator.removeValue(processVariable); 161 } 162 } 163 } 164 | Popular Tags |