1 4 package org.enhydra.shark.repositorypersistence; 5 6 import java.sql.SQLException ; 7 import java.util.ArrayList ; 8 import java.util.HashSet ; 9 import java.util.Iterator ; 10 import java.util.List ; 11 import java.util.Set ; 12 13 import net.sf.hibernate.JDBCException; 14 import net.sf.hibernate.Query; 15 import net.sf.hibernate.Session; 16 17 import org.enhydra.shark.api.RepositoryTransaction; 18 import org.enhydra.shark.api.RootException; 19 import org.enhydra.shark.api.TransactionException; 20 import org.enhydra.shark.api.internal.repositorypersistence.RepositoryException; 21 import org.enhydra.shark.api.internal.repositorypersistence.RepositoryPersistenceManager; 22 import org.enhydra.shark.api.internal.working.CallbackUtilities; 23 import org.enhydra.shark.repositorypersistence.data.HibernateNextXPDLVersion; 24 import org.enhydra.shark.repositorypersistence.data.HibernateXPDL; 25 import org.enhydra.shark.repositorypersistence.data.HibernateXPDLHistory; 26 import org.enhydra.shark.utilities.hibernate.HibernateUtilities; 27 28 32 public class HibernateRepositoryPersistenceManager implements RepositoryPersistenceManager { 33 34 static boolean _debug_ = false; 35 private static final String DBG_PARAM_NAME = "HibernateRepositoryPersistenceManager.debug"; 36 private static final String INITIAL_VERSION = "1"; 37 38 private static final String LDB_PARAM_NAME = "HibernateRepositoryPersistenceManager.DatabaseName"; 39 private CallbackUtilities cus; 40 41 44 public HibernateRepositoryPersistenceManager() { 45 } 46 47 58 public void configure(CallbackUtilities cus) throws RootException { 59 if (null == cus) 60 throw new RootException("Cannot configure without call back impl."); 61 this.cus = cus; 62 _debug_ = Boolean.valueOf(cus.getProperty(DBG_PARAM_NAME, "false")).booleanValue(); 63 HibernateUtilities.init(cus.getProperties()); 64 cus.debug("HibernateRepositoryPersistenceManager configured"); 65 } 66 67 public void uploadXPDL(RepositoryTransaction t, String xpdlId, byte[] xpdl) throws RepositoryException { 68 try { 69 Session session = ((HibernateRepositoryTransaction) t).getSession(); 70 String xpdlVer = updateNextVersion(session, xpdlId); 71 HibernateXPDL hibXpdl = createHibernateXPDL(); 72 hibXpdl.setXPDLId(xpdlId); 73 hibXpdl.setXPDLVersion(xpdlVer); 74 hibXpdl.setXPDLContent(xpdl); 75 session.save(hibXpdl); 76 } catch (Throwable thr) { 77 throw new RepositoryException("HibernateRepositoryPersistenceManager -> The upload of xpdl " + xpdlId + " failed", thr); 78 } 79 80 } 81 82 public void updateXPDL(RepositoryTransaction t, String xpdlId, String xpdlVersion, byte[] xpdl) throws RepositoryException { 83 try { 84 Session session = ((HibernateRepositoryTransaction) t).getSession(); 85 HibernateXPDL hibXpdl = getXPDL(session, xpdlId, xpdlVersion); hibXpdl.setXPDLContent(xpdl); 87 session.update(hibXpdl); 88 } catch (Exception ex) { 89 cus.error("HibernateRepositoryPersistenceManager -> The update of the xpdl with Id=" + xpdlId + ", and verison=" + xpdlVersion + " failed"); 90 throw new RepositoryException(ex); 91 } 92 } 93 94 public void deleteXPDL(RepositoryTransaction t, String xpdlId, String xpdlVersion) throws RepositoryException { 95 try { 96 Session session = ((HibernateRepositoryTransaction) t).getSession(); 97 HibernateXPDL hibXpdl = getXPDL(session, xpdlId, xpdlVersion); session.delete(hibXpdl); 99 } catch (Exception ex) { 100 throw new RepositoryException("XPDL [" + xpdlId + "," + xpdlVersion + "] is not deleted from repository", ex); 101 } 102 } 103 104 public void moveToHistory(RepositoryTransaction t, String xpdlId, String xpdlVersion) throws RepositoryException { 105 try { 106 Session session = ((HibernateRepositoryTransaction) t).getSession(); 107 HibernateXPDL hibXpdl = getXPDL(session, xpdlId, xpdlVersion); HibernateXPDLHistory xpdlHist = createHibernateXPDLHistory(); 109 xpdlHist.setXPDLId(hibXpdl.getXPDLId()); 110 xpdlHist.setXPDLVersion(hibXpdl.getXPDLVersion()); 111 xpdlHist.setXPDLContent(hibXpdl.getXPDLContent()); 112 session.delete(hibXpdl); 113 session.save(xpdlHist); 114 } catch (Exception ex) { 115 throw new RepositoryException("XPDL [" + xpdlId + "," + xpdlVersion + "] is not moved to history", ex); 116 } 117 } 118 119 public void deleteFromHistory(RepositoryTransaction t, String xpdlId, String xpdlVersion) throws RepositoryException { 120 try { 121 Session session = ((HibernateRepositoryTransaction) t).getSession(); 122 HibernateXPDLHistory xpdlHist = getXPDLHistory(session,xpdlId,xpdlVersion); 123 if (xpdlHist != null) 124 session.delete(xpdlHist); 125 } catch (Exception ex) { 126 throw new RepositoryException("XPDL [" + xpdlId + "," + xpdlVersion + "] is not deleted from history", ex); 127 } 128 } 129 130 public void clearRepository(RepositoryTransaction t) throws RepositoryException { 131 try { 132 Session session = ((HibernateRepositoryTransaction) t).getSession(); 133 List xpdls = getAllXPDLs(session); 134 if (xpdls.size() != 0l) { 135 for (Iterator it = xpdls.iterator();it.hasNext();) { 136 session.delete((HibernateXPDL)it.next()); 137 } 138 } 139 } catch (Exception ex) { 140 throw new RepositoryException("Some xpdl is not deleted from repository while clearing it", ex); 141 } 142 } 143 144 public String getCurrentVersion(RepositoryTransaction t, String xpdlId) throws RepositoryException { 145 try { 146 Session session = ((HibernateRepositoryTransaction) t).getSession(); 147 return getLastVersionXPDL(session, xpdlId).getXPDLVersion(); 148 } catch (Exception ex) { 149 throw new RepositoryException("No xpdl with Id=" + xpdlId, ex); 150 } 151 } 152 153 public String getNextVersion(RepositoryTransaction t, String xpdlId) throws RepositoryException { 154 try { 155 Session session = ((HibernateRepositoryTransaction) t).getSession(); 156 157 Query qNextVersion = session.createQuery("from HibernateNextXPDLVersion nextXPDLVersion where nextXPDLVersion.XPDLId = :keyValueParam"); 158 qNextVersion.setString("keyValueParam", xpdlId); 159 HibernateNextXPDLVersion nextXPDLVersion = (HibernateNextXPDLVersion) qNextVersion.uniqueResult(); 160 161 if (nextXPDLVersion == null) { 162 return INITIAL_VERSION; 163 } else { 164 return nextXPDLVersion.getNextVersion(); 165 } 166 } catch (Exception ex) { 167 throw new RepositoryException(ex); 168 } 169 } 170 171 private String updateNextVersion(Session session, String xpdlId) throws RepositoryException { 172 String curVersion = INITIAL_VERSION; 173 try{ 174 Query qNextVersion = session.createQuery("from HibernateNextXPDLVersion nextXPDLVersion where nextXPDLVersion.XPDLId = :keyValueParam"); 175 qNextVersion.setString("keyValueParam", xpdlId); 176 HibernateNextXPDLVersion nextXPDLVersion = (HibernateNextXPDLVersion) qNextVersion.uniqueResult(); 177 if (nextXPDLVersion == null) { 178 nextXPDLVersion = createHibernateNextXPDLVersion(); 179 nextXPDLVersion.setXPDLId(xpdlId); 180 int nver = Integer.parseInt(INITIAL_VERSION) + 1; 181 String nextVersion = String.valueOf(nver); 182 nextXPDLVersion.setNextVersion(nextVersion); 183 session.save(nextXPDLVersion); 184 } else { 185 curVersion = nextXPDLVersion.getNextVersion(); 186 int nver = Integer.parseInt(nextXPDLVersion.getNextVersion()) + 1; 187 String nextVersion = String.valueOf(nver); 188 nextXPDLVersion.setNextVersion(nextVersion); 189 session.update(nextXPDLVersion); 190 } 191 } catch (Exception ex) { 192 throw new RepositoryException("Internal problem while updating Next XPDL Version", ex); 193 } 194 return curVersion; 195 } 196 197 public byte[] getXPDL(RepositoryTransaction t, String xpdlId) throws RepositoryException { 198 try { 199 Session session = ((HibernateRepositoryTransaction) t).getSession(); 200 return getLastVersionXPDL(session, xpdlId).getXPDLContent(); 201 } catch (Exception ex) { 202 throw new RepositoryException("No xpdl with Id=" + xpdlId + " in repository", ex); 203 } 204 } 205 206 public byte[] getXPDL(RepositoryTransaction t, String xpdlId, String xpdlVersion) throws RepositoryException { 207 try { 208 Session session = ((HibernateRepositoryTransaction) t).getSession(); 209 return getXPDL(session, xpdlId, xpdlVersion).getXPDLContent(); 210 } catch (Exception ex) { 211 throw new RepositoryException("No xpdl [" + xpdlId + "," + xpdlVersion + "] in repository", ex); 212 } 213 } 214 215 public List getXPDLVersions(RepositoryTransaction t, String xpdlId) throws RepositoryException { 216 try { 217 List xpdlVersions = new ArrayList (); 218 Session session = ((HibernateRepositoryTransaction) t).getSession(); 219 List xpdls = getAllXPDLs(session, xpdlId); 220 221 for (Iterator it = xpdls.iterator();it.hasNext();) { 222 xpdlVersions.add(((HibernateXPDL)it.next()).getXPDLVersion()); 223 } 224 return xpdlVersions; 225 } catch (Exception ex) { 226 throw new RepositoryException("No xpdl with Id=" + xpdlId + " in repository", ex); 227 } 228 } 229 230 public boolean doesXPDLExist(RepositoryTransaction t, String xpdlId) throws RepositoryException { 231 try { 232 Session session = ((HibernateRepositoryTransaction) t).getSession(); 233 HibernateXPDL xpdl = getLastVersionXPDL(session, xpdlId); 234 return getLastVersionXPDL(session, xpdlId) != null; 235 } catch (Exception ex) { 236 return false; 237 } 238 } 239 240 public boolean doesXPDLExist(RepositoryTransaction t, String xpdlId, String xpdlVersion) throws RepositoryException { 241 try { 242 Session session = ((HibernateRepositoryTransaction) t).getSession(); 243 getXPDL(session, xpdlId, xpdlVersion); 244 return true; 245 } catch (Exception ex) { 246 return false; 247 } 248 } 249 250 public List getExistingXPDLIds(RepositoryTransaction t) throws RepositoryException { 251 try { 252 Session session = ((HibernateRepositoryTransaction) t).getSession(); 253 List xpdls = getAllXPDLs(session); 254 Set ids = new HashSet (); 255 if (xpdls.size() != 0) { 256 for (Iterator it = xpdls.iterator();it.hasNext();) { 257 ids.add(((HibernateXPDL)it.next()).getXPDLId()); 258 } 259 } 260 return new ArrayList (ids); 261 } catch (Exception ex) { 262 throw new RepositoryException(ex); 263 } 264 } 265 266 public void addXPDLReference(RepositoryTransaction t, String referredXPDLId, String referringXPDLId, String referringXPDLVersion) 268 throws RepositoryException { 269 try { 270 Session session = ((HibernateRepositoryTransaction) t).getSession(); 271 HibernateXPDL refferringXPDL = getXPDL(session, referringXPDLId, referringXPDLVersion); 272 if ((refferringXPDL != null) && (!refferringXPDL.getXpdlReference().contains(referredXPDLId))){ 273 refferringXPDL.addXpdlReference(referredXPDLId); 274 } 275 session.update(refferringXPDL); 276 } catch (Exception ex) { 277 throw new RepositoryException(ex); 278 } 279 } 280 281 public List getReferringXPDLIds(RepositoryTransaction t, String referredXPDLId) throws RepositoryException { 282 try { 283 List referrers = new ArrayList (); 284 285 Session session = ((HibernateRepositoryTransaction) t).getSession(); 286 287 List xpdls = session.find("from HibernateXPDL"); 288 for (Iterator it = xpdls.iterator();it.hasNext();){ 289 HibernateXPDL xpdl = (HibernateXPDL)it.next(); 290 for (Iterator itXpdlRef = xpdl.getXpdlReference().iterator();itXpdlRef.hasNext();){ 291 String refferingXPDLId = (String )itXpdlRef.next(); 292 if (refferingXPDLId.equals(referredXPDLId)){ 293 if (!referrers.contains(xpdl.getXPDLId())){ 294 referrers.add(xpdl.getXPDLId()); 295 } 296 } 297 } 298 } 299 return referrers; 300 } 301 catch (Exception ex) { 302 throw new RepositoryException(ex); 303 } 304 } 305 306 public List getReferringXPDLVersions(RepositoryTransaction t, String referredXPDLId, String refferingXPDLId) throws RepositoryException { 307 try { 308 List referrers = new ArrayList (); 309 310 Session session = ((HibernateRepositoryTransaction) t).getSession(); 311 312 List xpdls = session.find("from HibernateXPDL"); 313 for (Iterator it = xpdls.iterator();it.hasNext();){ 314 HibernateXPDL xpdl = (HibernateXPDL)it.next(); 315 for (Iterator itXpdlRef = xpdl.getXpdlReference().iterator();itXpdlRef.hasNext();){ 316 String refferingXPDLIdQuery = (String )itXpdlRef.next(); 317 if (refferingXPDLIdQuery.equals(referredXPDLId) && xpdl.getXPDLId().equals(refferingXPDLId)){ 318 if (!referrers.contains(xpdl.getXPDLVersion())){ 319 referrers.add(xpdl.getXPDLVersion()); 320 } 321 } 322 } 323 } 324 325 return referrers; 326 327 } catch (Exception ex) { 328 throw new RepositoryException(ex); 329 } 330 } 331 332 public List getReferredXPDLIds(RepositoryTransaction t, String refferingXPDLId, String refferingXPDLVersion) throws RepositoryException { 333 try { 334 List ret = new ArrayList (); 335 Session session = ((HibernateRepositoryTransaction) t).getSession(); 336 337 HibernateXPDL xpdl = getXPDL(session, refferingXPDLId, refferingXPDLVersion); 338 ret.addAll(xpdl.getXpdlReference()); 339 340 return ret; 341 342 } catch (Exception ex) { 343 throw new RepositoryException(ex); 344 } 345 } 346 347 public RepositoryTransaction createTransaction() throws TransactionException { 348 try { 349 return new HibernateRepositoryTransaction(ThreadLocalSession.currentSession().beginTransaction()); 350 } catch (Exception ex) { 351 throw new TransactionException(ex); 352 } 353 } 354 355 private HibernateXPDL getXPDL(Session session, String xpdlId, String xpdlVersion) throws Exception { 356 Query qXPDL = session.createQuery("from HibernateXPDL xpdl where xpdl.XPDLId = :keyValueParam1" + " and xpdl.XPDLVersion = :keyValueParam2"); 357 qXPDL.setString("keyValueParam1", xpdlId); 358 qXPDL.setString("keyValueParam2", xpdlVersion); 359 HibernateXPDL xpdl = (HibernateXPDL) qXPDL.uniqueResult(); 360 if (xpdl == null) { 361 throw new Exception ("There is no xpdl with Id=" + xpdlId + ", and version " + xpdlVersion + " in the repository"); 362 } 363 return xpdl; 364 } 365 366 private List getAllXPDLs(Session session, String xpdlId) throws Exception { 367 Query qXPDL = session.createQuery("from HibernateXPDL xpdl where xpdl.XPDLId = :keyValueParam"); 368 qXPDL.setString("keyValueParam", xpdlId); 369 return qXPDL.list(); 370 } 371 372 private List getAllXPDLs(Session session) throws Exception { 373 return session.find("from HibernateXPDL"); 374 } 375 376 private HibernateXPDL getLastVersionXPDL(Session session, String xpdlId) throws Exception { 377 List xpdls = getAllXPDLs(session, xpdlId); 378 if (xpdls.size() == 0) { 379 return null; 380 } 381 382 HibernateXPDL lastVersion = null; 383 int maxVer = -1; 384 for (Iterator it = xpdls.iterator();it.hasNext();){ 385 HibernateXPDL xpdlTemp = (HibernateXPDL)it.next(); 386 String xpdlVer = xpdlTemp.getXPDLVersion(); 387 int ver = Integer.parseInt(xpdlVer); 388 if (ver > maxVer) { 389 maxVer = ver; 390 lastVersion = xpdlTemp; 391 } 392 } 393 394 if (lastVersion == null) 395 throw new Exception ("Something is wrong in XPDL repository - can't determine XPDL version"); 396 397 return lastVersion; 398 } 399 400 private HibernateXPDLHistory getXPDLHistory(Session session, String xpdlId, String xpdlVersion) throws Exception { 401 Query qXPDLHistory = session.createQuery("from HibernateXPDLHistory xpdlHistory where xpdlHistory.XPDLId = :keyValueParam1" 402 + " and xpdlHistory.XPDLVersion = :keyValueParam2"); 403 qXPDLHistory.setString("keyValueParam1", xpdlId); 404 qXPDLHistory.setString("keyValueParam2", xpdlVersion); 405 HibernateXPDLHistory xpdlHistory = (HibernateXPDLHistory)qXPDLHistory.uniqueResult(); 406 407 if (xpdlHistory == null) { 408 throw new Exception ("There is no xpdl with Id=" + xpdlId + ", and version " + xpdlVersion + " in the history repository"); 409 } 410 return xpdlHistory; 411 } 412 413 public HibernateNextXPDLVersion createHibernateNextXPDLVersion() { 414 return new HibernateNextXPDLVersion(); 415 } 416 417 public HibernateXPDL createHibernateXPDL() { 418 return new HibernateXPDL(); 419 } 420 421 public HibernateXPDLHistory createHibernateXPDLHistory() { 422 return new HibernateXPDLHistory(); 423 } 424 425 } 426 | Popular Tags |