KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > shark > repository > EntityRepositoryMgr


1 /*
2  * $Id: EntityRepositoryMgr.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  */

5 package org.ofbiz.shark.repository;
6
7 import java.util.List JavaDoc;
8 import java.util.ArrayList JavaDoc;
9 import java.util.Iterator JavaDoc;
10
11 import org.ofbiz.shark.transaction.JtaTransaction;
12 import org.ofbiz.shark.container.SharkContainer;
13 import org.ofbiz.entity.GenericDelegator;
14 import org.ofbiz.entity.GenericValue;
15 import org.ofbiz.entity.GenericEntityException;
16 import org.ofbiz.entity.condition.EntityCondition;
17 import org.ofbiz.entity.condition.EntityExpr;
18 import org.ofbiz.entity.condition.EntityOperator;
19 import org.ofbiz.entity.condition.EntityConditionList;
20 import org.ofbiz.entity.util.EntityUtil;
21 import org.ofbiz.base.util.UtilDateTime;
22 import org.ofbiz.base.util.UtilMisc;
23 import org.ofbiz.base.util.UtilValidate;
24 import org.ofbiz.base.util.StringUtil;
25 import org.ofbiz.base.util.Debug;
26
27 import org.enhydra.shark.api.internal.repositorypersistence.RepositoryPersistenceManager;
28 import org.enhydra.shark.api.internal.repositorypersistence.RepositoryException;
29 import org.enhydra.shark.api.internal.working.CallbackUtilities;
30 import org.enhydra.shark.api.RootException;
31 import org.enhydra.shark.api.RepositoryTransaction;
32 import org.enhydra.shark.api.TransactionException;
33
34 /**
35  *
36  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
37  * @version $Rev: 5462 $
38  * @since 3.1
39  */

40 public class EntityRepositoryMgr implements RepositoryPersistenceManager {
41
42     public static final String JavaDoc module = EntityRepositoryMgr.class.getName();
43     protected CallbackUtilities callBack = null;
44
45     public void configure(CallbackUtilities callBack) throws RootException {
46         this.callBack = callBack;
47     }
48
49     public void uploadXPDL(RepositoryTransaction t, String JavaDoc xpdlId, byte[] xpdl) throws RepositoryException {
50         Debug.log("XPDL Upload : " + xpdlId, module);
51         GenericDelegator delegator = SharkContainer.getDelegator();
52         try {
53             GenericValue v = delegator.makeValue("WfRepository", null);
54             v.set("xpdlId", xpdlId);
55             v.set("xpdlVersion", UtilDateTime.nowDateString());
56             v.set("isHistorical", "N");
57             v.setBytes("xpdlData", xpdl);
58             delegator.create(v);
59             Debug.log("Created Value - " + v, module);
60         } catch (GenericEntityException e) {
61             throw new RepositoryException(e);
62         }
63     }
64
65     public void updateXPDL(RepositoryTransaction t, String JavaDoc xpdlId, String JavaDoc xpdlVersion, byte[] xpdl) throws RepositoryException {
66         Debug.log("XPDL Update : " + xpdlId + "/" + xpdlVersion + " - " + StringUtil.toHexString(xpdl), module);
67         GenericValue value = this.getXpdlValue(xpdlId, xpdlVersion, false);
68         if (value != null) {
69             value.setBytes("xpdlData", xpdl);
70             try {
71                value.store();
72             } catch (GenericEntityException e) {
73                 throw new RepositoryException(e);
74             }
75         }
76     }
77
78     public void deleteXPDL(RepositoryTransaction t, String JavaDoc xpdlId, String JavaDoc xpdlVersion) throws RepositoryException {
79         Debug.log("XPDL Delete : " + xpdlId + "/" + xpdlVersion, module);
80         GenericValue value = this.getXpdlValue(xpdlId, xpdlVersion, false);
81         if (value != null) {
82             try {
83                 value.remove();
84             } catch (GenericEntityException e) {
85                 throw new RepositoryException(e);
86             }
87         }
88     }
89
90     public void moveToHistory(RepositoryTransaction t, String JavaDoc xpdlId, String JavaDoc xpdlVersion) throws RepositoryException {
91         Debug.log("XPDL Move to History : " + xpdlId + "/" + xpdlVersion, module);
92         GenericValue value = this.getXpdlValue(xpdlId, xpdlVersion, false);
93         value.set("isHistorical", "Y");
94         try {
95             value.store();
96         } catch (GenericEntityException e) {
97             throw new RepositoryException(e);
98         }
99     }
100
101     public void deleteFromHistory(RepositoryTransaction t, String JavaDoc xpdlId, String JavaDoc xpdlVersion) throws RepositoryException {
102         Debug.log("XPDL Delete from History: " + xpdlId + "/" + xpdlVersion, module);
103         GenericValue value = this.getXpdlValue(xpdlId, xpdlVersion, true);
104         if (value != null) {
105             try {
106                 value.remove();
107             } catch (GenericEntityException e) {
108                 throw new RepositoryException(e);
109             }
110         }
111     }
112
113     public void clearRepository(RepositoryTransaction t) throws RepositoryException {
114         Debug.log("XPDL Clear Repository", module);
115         GenericDelegator delegator = SharkContainer.getDelegator();
116         try {
117             delegator.removeByAnd("WfRepository", null);
118         } catch (GenericEntityException e) {
119             throw new RepositoryException(e);
120         }
121     }
122
123     public String JavaDoc getCurrentVersion(RepositoryTransaction t, String JavaDoc xpdlId) throws RepositoryException {
124         Debug.log("XPDL get current version : " + xpdlId, module);
125         List JavaDoc lookupList = this.getXpdlValues(xpdlId, null, false);
126         GenericValue value = EntityUtil.getFirst(lookupList);
127         if (value != null) {
128             return value.getString("xpdlVersion");
129         } else {
130             throw new RepositoryException("XPDL not found in repository!");
131         }
132     }
133
134     public String JavaDoc getNextVersion(RepositoryTransaction t, String JavaDoc xpdlId) throws RepositoryException {
135         return UtilDateTime.nowDateString();
136     }
137
138     public byte[] getXPDL(RepositoryTransaction t, String JavaDoc xpdlId) throws RepositoryException {
139         Debug.log("XPDL Get : " + xpdlId, module);
140         List JavaDoc lookupList = this.getXpdlValues(xpdlId, null, false);
141         GenericValue value = EntityUtil.getFirst(lookupList);
142         if (value != null) {
143             return value.getBytes("xpdlData");
144         } else {
145             throw new RepositoryException("XPDL not found in repository!");
146         }
147     }
148
149     public byte[] getXPDL(RepositoryTransaction t, String JavaDoc xpdlId, String JavaDoc xpdlVersion) throws RepositoryException {
150         Debug.log("XPDL Get : " + xpdlId + "/" + xpdlVersion, module);
151         GenericValue value = this.getXpdlValue(xpdlId, xpdlVersion, false);
152         if (value != null) {
153             return value.getBytes("xpdlData");
154         } else {
155             throw new RepositoryException("XPDL not found in repository!");
156         }
157     }
158
159     public List JavaDoc getXPDLVersions(RepositoryTransaction t, String JavaDoc xpdlId) throws RepositoryException {
160         Debug.log("XPDL Get Versions : " + xpdlId, module);
161         List JavaDoc lookupList = this.getXpdlValues(xpdlId, null, false);
162         List JavaDoc versionList = new ArrayList JavaDoc();
163         if (!UtilValidate.isEmpty(lookupList)) {
164             Iterator JavaDoc i = lookupList.iterator();
165             while (i.hasNext()) {
166                 GenericValue v = (GenericValue) i.next();
167                 versionList.add(v.getString("xpdlVersion"));
168             }
169         }
170         return versionList;
171     }
172
173     public boolean doesXPDLExist(RepositoryTransaction t, String JavaDoc xpdlId) throws RepositoryException {
174         List JavaDoc xpdls = this.getXpdlValues(xpdlId, null, false);
175         Debug.log("Does XPDL [" + xpdlId + "] Exist - " + xpdls + "(" + (xpdls != null && xpdls.size() > 0 ? true : false) + ")", module);
176         return (xpdls != null && xpdls.size() > 0 ? true : false);
177     }
178
179     public boolean doesXPDLExist(RepositoryTransaction t, String JavaDoc xpdlId, String JavaDoc xpdlVersion) throws RepositoryException {
180         GenericValue xpdl = this.getXpdlValue(xpdlId, xpdlVersion, false);
181         Debug.log("Does XPDL [" + xpdlId + "/" + xpdlVersion + "] Exist - " + xpdl + "(" + (xpdl != null) + ")", module);
182         return (xpdl != null);
183     }
184
185     public List JavaDoc getExistingXPDLIds(RepositoryTransaction t) throws RepositoryException {
186         Debug.log("Get Existing XPDL IDs", module);
187         List JavaDoc lookupList = this.getXpdlValues(null, null, false);
188         List JavaDoc idList = new ArrayList JavaDoc();
189         if (UtilValidate.isNotEmpty(lookupList)) {
190             Iterator JavaDoc i = lookupList.iterator();
191             while (i.hasNext()) {
192                 GenericValue v = (GenericValue) i.next();
193                 Debug.log("Checking - " + v, module);
194                 String JavaDoc id = v.getString("xpdlId");
195                 if (!idList.contains(id)) {
196                     idList.add(id);
197                 }
198             }
199         }
200         return idList;
201     }
202
203     public void addXPDLReference(RepositoryTransaction t, String JavaDoc referredXPDLId, String JavaDoc referringXPDLId, String JavaDoc referringXPDLVersion) throws RepositoryException {
204         Debug.log("Add XPDL Reference", module);
205         GenericDelegator delegator = SharkContainer.getDelegator();
206         GenericValue ref = delegator.makeValue("WfRepositoryRef", null);
207         ref.set("xpdlId", referringXPDLId);
208         ref.set("xpdlVersion", referringXPDLVersion);
209         ref.set("refXpdlId", referredXPDLId);
210         try {
211             delegator.create(ref);
212         } catch (GenericEntityException e) {
213             throw new RepositoryException(e);
214         }
215     }
216
217     public List JavaDoc getReferringXPDLIds(RepositoryTransaction t, String JavaDoc referredXPDLId) throws RepositoryException {
218         Debug.log("Get XPDL Reference IDs", module);
219         GenericDelegator delegator = SharkContainer.getDelegator();
220         List JavaDoc referringIds = new ArrayList JavaDoc();
221         List JavaDoc refs = null;
222         try {
223             refs = delegator.findByAnd("WfRepositoryRef", UtilMisc.toMap("refXpdlId", referredXPDLId));
224         } catch (GenericEntityException e) {
225             throw new RepositoryException(e);
226         }
227         if (!UtilValidate.isEmpty(refs)) {
228             Iterator JavaDoc i = refs.iterator();
229             while (i.hasNext()) {
230                 GenericValue v = (GenericValue) i.next();
231                 referringIds.add(v.getString("xpdlId"));
232             }
233         }
234         return referringIds;
235     }
236
237     public List JavaDoc getReferringXPDLVersions(RepositoryTransaction t, String JavaDoc referredXPDLId, String JavaDoc referringXPDLId) throws RepositoryException {
238         Debug.log("Get Referring XPDL Versions", module);
239         GenericDelegator delegator = SharkContainer.getDelegator();
240         List JavaDoc referringVers = new ArrayList JavaDoc();
241         List JavaDoc refs = null;
242         try {
243             refs = delegator.findByAnd("WfRepositoryRef", UtilMisc.toMap("refXpdlId", referredXPDLId, "xpdlId", referringXPDLId));
244         } catch (GenericEntityException e) {
245             throw new RepositoryException(e);
246         }
247         if (!UtilValidate.isEmpty(refs)) {
248             Iterator JavaDoc i = refs.iterator();
249             while (i.hasNext()) {
250                 GenericValue v = (GenericValue) i.next();
251                 referringVers.add(v.getString("xpdlVersion"));
252             }
253         }
254         return referringVers;
255     }
256
257     public List JavaDoc getReferredXPDLIds(RepositoryTransaction t, String JavaDoc referringXPDLId, String JavaDoc referringXPDLVersion) throws RepositoryException {
258         Debug.log("Get Referring XPDL IDs", module);
259         GenericDelegator delegator = SharkContainer.getDelegator();
260         List JavaDoc referringIds = new ArrayList JavaDoc();
261         List JavaDoc refs = null;
262         try {
263             refs = delegator.findByAnd("WfRepositoryRef", UtilMisc.toMap("xpdlId", referringXPDLId, "xpdlVersion", referringXPDLVersion));
264         } catch (GenericEntityException e) {
265             throw new RepositoryException(e);
266         }
267         if (!UtilValidate.isEmpty(refs)) {
268             Iterator JavaDoc i = refs.iterator();
269             while (i.hasNext()) {
270                 GenericValue v = (GenericValue) i.next();
271                 referringIds.add(v.getString("refXpdlId"));
272             }
273         }
274         return referringIds;
275     }
276
277     public RepositoryTransaction createTransaction() throws TransactionException {
278         return new JtaTransaction();
279     }
280
281     private GenericValue getXpdlValue(String JavaDoc xpdlId, String JavaDoc xpdlVersion, boolean includeHistorical) throws RepositoryException {
282         GenericDelegator delegator = SharkContainer.getDelegator();
283         GenericValue xpdl = null;
284         try {
285             xpdl = delegator.findByPrimaryKey("WfRepository", UtilMisc.toMap("xpdlId", xpdlId, "xpdlVersion", xpdlVersion));
286             if (!includeHistorical && xpdl.get("isHistorical") != null && xpdl.getString("isHistorical").equalsIgnoreCase("Y")) {
287                 xpdl = null;
288
289             }
290         } catch (GenericEntityException e) {
291             throw new RepositoryException(e);
292         }
293         return xpdl;
294     }
295
296     private List JavaDoc getXpdlValues(String JavaDoc xpdlId, String JavaDoc xpdlVersion, boolean includeHistory) throws RepositoryException {
297         GenericDelegator delegator = SharkContainer.getDelegator();
298         List JavaDoc exprList = new ArrayList JavaDoc();
299         if (xpdlId != null) {
300             exprList.add(new EntityExpr("xpdlId", EntityOperator.EQUALS, xpdlId));
301         }
302         if (xpdlVersion != null) {
303             exprList.add(new EntityExpr("xpdlVersion", EntityOperator.EQUALS, xpdlVersion));
304         }
305         if (!includeHistory) {
306             exprList.add(new EntityExpr("isHistorical", EntityOperator.NOT_EQUAL, "Y"));
307         }
308
309         EntityCondition cond = new EntityConditionList(exprList, EntityOperator.AND);
310         List JavaDoc lookupList = null;
311         try {
312             lookupList = delegator.findByCondition("WfRepository", cond, null, UtilMisc.toList("-xpdlVersion"));
313         } catch (GenericEntityException e) {
314             throw new RepositoryException(e);
315         } finally {
316             Debug.set(Debug.VERBOSE, false);
317         }
318         return lookupList;
319     }
320 }
321
Popular Tags