KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > shark > instance > ProcessVariable


1 /*
2  * $Id: ProcessVariable.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2004 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

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 /**
38  * Persistance Object
39  *
40  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
41  * @version $Rev: 5462 $
42  * @since 3.1
43  */

44 public class ProcessVariable extends InstanceEntityObject implements ProcessVariablePersistenceInterface {
45
46     public static final String JavaDoc module = ProcessVariable.class.getName();
47
48     protected GenericValue processVariable = null;
49     protected boolean newValue = false;
50
51     protected ProcessVariable(EntityPersistentMgr mgr, GenericDelegator delegator, String JavaDoc 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 JavaDoc 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 JavaDoc pId) {
100         processVariable.set("processId", pId);
101     }
102
103     public String JavaDoc getProcessId() {
104         return processVariable.getString("processId");
105     }
106
107     public void setDefinitionId(String JavaDoc defId) {
108         processVariable.set("definitionId", defId);
109     }
110
111     public String JavaDoc getDefinitionId() {
112         return processVariable.getString("definitionId");
113     }
114
115     public void setValue(Object JavaDoc val) {
116         if (val instanceof String JavaDoc) {
117             processVariable.set("valueField", "strValue");
118             processVariable.set("strValue", val);
119         } else if (val instanceof Number JavaDoc) {
120             if (val instanceof Double JavaDoc) {
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 JavaDoc getValue() {
134         String JavaDoc 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