1 5 package org.ofbiz.shark.repository; 6 7 import java.util.List ; 8 import java.util.ArrayList ; 9 import java.util.Iterator ; 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 40 public class EntityRepositoryMgr implements RepositoryPersistenceManager { 41 42 public static final String 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 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 xpdlId, String 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 xpdlId, String 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 xpdlId, String 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 xpdlId, String 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 getCurrentVersion(RepositoryTransaction t, String xpdlId) throws RepositoryException { 124 Debug.log("XPDL get current version : " + xpdlId, module); 125 List 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 getNextVersion(RepositoryTransaction t, String xpdlId) throws RepositoryException { 135 return UtilDateTime.nowDateString(); 136 } 137 138 public byte[] getXPDL(RepositoryTransaction t, String xpdlId) throws RepositoryException { 139 Debug.log("XPDL Get : " + xpdlId, module); 140 List 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 xpdlId, String 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 getXPDLVersions(RepositoryTransaction t, String xpdlId) throws RepositoryException { 160 Debug.log("XPDL Get Versions : " + xpdlId, module); 161 List lookupList = this.getXpdlValues(xpdlId, null, false); 162 List versionList = new ArrayList (); 163 if (!UtilValidate.isEmpty(lookupList)) { 164 Iterator 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 xpdlId) throws RepositoryException { 174 List 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 xpdlId, String 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 getExistingXPDLIds(RepositoryTransaction t) throws RepositoryException { 186 Debug.log("Get Existing XPDL IDs", module); 187 List lookupList = this.getXpdlValues(null, null, false); 188 List idList = new ArrayList (); 189 if (UtilValidate.isNotEmpty(lookupList)) { 190 Iterator i = lookupList.iterator(); 191 while (i.hasNext()) { 192 GenericValue v = (GenericValue) i.next(); 193 Debug.log("Checking - " + v, module); 194 String 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 referredXPDLId, String referringXPDLId, String 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 getReferringXPDLIds(RepositoryTransaction t, String referredXPDLId) throws RepositoryException { 218 Debug.log("Get XPDL Reference IDs", module); 219 GenericDelegator delegator = SharkContainer.getDelegator(); 220 List referringIds = new ArrayList (); 221 List 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 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 getReferringXPDLVersions(RepositoryTransaction t, String referredXPDLId, String referringXPDLId) throws RepositoryException { 238 Debug.log("Get Referring XPDL Versions", module); 239 GenericDelegator delegator = SharkContainer.getDelegator(); 240 List referringVers = new ArrayList (); 241 List 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 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 getReferredXPDLIds(RepositoryTransaction t, String referringXPDLId, String referringXPDLVersion) throws RepositoryException { 258 Debug.log("Get Referring XPDL IDs", module); 259 GenericDelegator delegator = SharkContainer.getDelegator(); 260 List referringIds = new ArrayList (); 261 List 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 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 xpdlId, String 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 getXpdlValues(String xpdlId, String xpdlVersion, boolean includeHistory) throws RepositoryException { 297 GenericDelegator delegator = SharkContainer.getDelegator(); 298 List exprList = new ArrayList (); 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 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 |