KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > repositorypersistence > HibernateRepositoryPersistenceManager


1 /*
2  * Shark Hibernate RepositoryPersistence - Open Wide
3  */

4 package org.enhydra.shark.repositorypersistence;
5
6 import java.sql.SQLException JavaDoc;
7 import java.util.ArrayList JavaDoc;
8 import java.util.HashSet JavaDoc;
9 import java.util.Iterator JavaDoc;
10 import java.util.List JavaDoc;
11 import java.util.Set JavaDoc;
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 /**
29  * Database implementation of Repository persistence interface.
30  * @author Vladislav Pernin
31  */

32 public class HibernateRepositoryPersistenceManager implements RepositoryPersistenceManager {
33
34     static boolean _debug_ = false;
35     private static final String JavaDoc DBG_PARAM_NAME = "HibernateRepositoryPersistenceManager.debug";
36     private static final String JavaDoc INITIAL_VERSION = "1";
37
38     private static final String JavaDoc LDB_PARAM_NAME = "HibernateRepositoryPersistenceManager.DatabaseName";
39     private CallbackUtilities cus;
40
41     /**
42      * Public constructor ().
43      */

44     public HibernateRepositoryPersistenceManager() {
45     }
46
47     /**
48      * Method configure is called at Shark start up, to configure
49      * HibernateRepositoryPersistenceManager.
50      *
51      * @param cus
52      * an instance of CallbackUtilities used to get properties for
53      * configuring repository manager in Shark.
54      *
55      * @exception RootException
56      * thrown if configuring doesn't succeed.
57      */

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 JavaDoc xpdlId, byte[] xpdl) throws RepositoryException {
68         try {
69             Session session = ((HibernateRepositoryTransaction) t).getSession();
70             String JavaDoc 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 JavaDoc thr) {
77             throw new RepositoryException("HibernateRepositoryPersistenceManager -> The upload of xpdl " + xpdlId + " failed", thr);
78         }
79
80     }
81
82     public void updateXPDL(RepositoryTransaction t, String JavaDoc xpdlId, String JavaDoc xpdlVersion, byte[] xpdl) throws RepositoryException {
83         try {
84             Session session = ((HibernateRepositoryTransaction) t).getSession();
85             HibernateXPDL hibXpdl = getXPDL(session, xpdlId, xpdlVersion); // this will throw an exception if xpdl does not exists
86
hibXpdl.setXPDLContent(xpdl);
87             session.update(hibXpdl);
88         } catch (Exception JavaDoc 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 JavaDoc xpdlId, String JavaDoc xpdlVersion) throws RepositoryException {
95         try {
96             Session session = ((HibernateRepositoryTransaction) t).getSession();
97             HibernateXPDL hibXpdl = getXPDL(session, xpdlId, xpdlVersion); // this will throw an exception if xpdl does not exists
98
session.delete(hibXpdl);
99         } catch (Exception JavaDoc ex) {
100             throw new RepositoryException("XPDL [" + xpdlId + "," + xpdlVersion + "] is not deleted from repository", ex);
101         }
102     }
103
104     public void moveToHistory(RepositoryTransaction t, String JavaDoc xpdlId, String JavaDoc xpdlVersion) throws RepositoryException {
105         try {
106             Session session = ((HibernateRepositoryTransaction) t).getSession();
107             HibernateXPDL hibXpdl = getXPDL(session, xpdlId, xpdlVersion); // this will throw an exception if xpdl does not exists
108
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 JavaDoc ex) {
115             throw new RepositoryException("XPDL [" + xpdlId + "," + xpdlVersion + "] is not moved to history", ex);
116         }
117     }
118
119     public void deleteFromHistory(RepositoryTransaction t, String JavaDoc xpdlId, String JavaDoc 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 JavaDoc 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 JavaDoc xpdls = getAllXPDLs(session);
134             if (xpdls.size() != 0l) {
135                 for (Iterator JavaDoc it = xpdls.iterator();it.hasNext();) {
136                     session.delete((HibernateXPDL)it.next());
137                 }
138             }
139         } catch (Exception JavaDoc ex) {
140             throw new RepositoryException("Some xpdl is not deleted from repository while clearing it", ex);
141         }
142     }
143
144     public String JavaDoc getCurrentVersion(RepositoryTransaction t, String JavaDoc xpdlId) throws RepositoryException {
145         try {
146             Session session = ((HibernateRepositoryTransaction) t).getSession();
147             return getLastVersionXPDL(session, xpdlId).getXPDLVersion();
148         } catch (Exception JavaDoc ex) {
149             throw new RepositoryException("No xpdl with Id=" + xpdlId, ex);
150         }
151     }
152
153     public String JavaDoc getNextVersion(RepositoryTransaction t, String JavaDoc 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 JavaDoc ex) {
167             throw new RepositoryException(ex);
168         }
169     }
170
171     private String JavaDoc updateNextVersion(Session session, String JavaDoc xpdlId) throws RepositoryException {
172         String JavaDoc 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 JavaDoc 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 JavaDoc nextVersion = String.valueOf(nver);
188                 nextXPDLVersion.setNextVersion(nextVersion);
189                 session.update(nextXPDLVersion);
190             }
191         } catch (Exception JavaDoc 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 JavaDoc xpdlId) throws RepositoryException {
198         try {
199             Session session = ((HibernateRepositoryTransaction) t).getSession();
200             return getLastVersionXPDL(session, xpdlId).getXPDLContent();
201         } catch (Exception JavaDoc ex) {
202             throw new RepositoryException("No xpdl with Id=" + xpdlId + " in repository", ex);
203         }
204     }
205
206     public byte[] getXPDL(RepositoryTransaction t, String JavaDoc xpdlId, String JavaDoc xpdlVersion) throws RepositoryException {
207         try {
208             Session session = ((HibernateRepositoryTransaction) t).getSession();
209             return getXPDL(session, xpdlId, xpdlVersion).getXPDLContent();
210         } catch (Exception JavaDoc ex) {
211             throw new RepositoryException("No xpdl [" + xpdlId + "," + xpdlVersion + "] in repository", ex);
212         }
213     }
214
215     public List JavaDoc getXPDLVersions(RepositoryTransaction t, String JavaDoc xpdlId) throws RepositoryException {
216         try {
217             List JavaDoc xpdlVersions = new ArrayList JavaDoc();
218             Session session = ((HibernateRepositoryTransaction) t).getSession();
219             List JavaDoc xpdls = getAllXPDLs(session, xpdlId);
220
221             for (Iterator JavaDoc it = xpdls.iterator();it.hasNext();) {
222                 xpdlVersions.add(((HibernateXPDL)it.next()).getXPDLVersion());
223             }
224             return xpdlVersions;
225         } catch (Exception JavaDoc ex) {
226             throw new RepositoryException("No xpdl with Id=" + xpdlId + " in repository", ex);
227         }
228     }
229
230     public boolean doesXPDLExist(RepositoryTransaction t, String JavaDoc 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 JavaDoc ex) {
236             return false;
237         }
238     }
239
240     public boolean doesXPDLExist(RepositoryTransaction t, String JavaDoc xpdlId, String JavaDoc xpdlVersion) throws RepositoryException {
241         try {
242             Session session = ((HibernateRepositoryTransaction) t).getSession();
243             getXPDL(session, xpdlId, xpdlVersion);
244             return true;
245         } catch (Exception JavaDoc ex) {
246             return false;
247         }
248     }
249
250     public List JavaDoc getExistingXPDLIds(RepositoryTransaction t) throws RepositoryException {
251         try {
252             Session session = ((HibernateRepositoryTransaction) t).getSession();
253             List JavaDoc xpdls = getAllXPDLs(session);
254             Set JavaDoc ids = new HashSet JavaDoc();
255             if (xpdls.size() != 0) {
256                 for (Iterator JavaDoc it = xpdls.iterator();it.hasNext();) {
257                     ids.add(((HibernateXPDL)it.next()).getXPDLId());
258                 }
259             }
260             return new ArrayList JavaDoc(ids);
261         } catch (Exception JavaDoc ex) {
262             throw new RepositoryException(ex);
263         }
264     }
265
266     // TODO: see about references holding version information
267
public void addXPDLReference(RepositoryTransaction t, String JavaDoc referredXPDLId, String JavaDoc referringXPDLId, String JavaDoc 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 JavaDoc ex) {
277             throw new RepositoryException(ex);
278         }
279     }
280     
281     public List JavaDoc getReferringXPDLIds(RepositoryTransaction t, String JavaDoc referredXPDLId) throws RepositoryException {
282         try {
283             List JavaDoc referrers = new ArrayList JavaDoc();
284             
285             Session session = ((HibernateRepositoryTransaction) t).getSession();
286             
287             List JavaDoc xpdls = session.find("from HibernateXPDL");
288             for (Iterator JavaDoc it = xpdls.iterator();it.hasNext();){
289                 HibernateXPDL xpdl = (HibernateXPDL)it.next();
290                 for (Iterator JavaDoc itXpdlRef = xpdl.getXpdlReference().iterator();itXpdlRef.hasNext();){
291                     String JavaDoc refferingXPDLId = (String JavaDoc)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 JavaDoc ex) {
302             throw new RepositoryException(ex);
303         }
304     }
305
306     public List JavaDoc getReferringXPDLVersions(RepositoryTransaction t, String JavaDoc referredXPDLId, String JavaDoc refferingXPDLId) throws RepositoryException {
307         try {
308             List JavaDoc referrers = new ArrayList JavaDoc();
309             
310             Session session = ((HibernateRepositoryTransaction) t).getSession();
311             
312             List JavaDoc xpdls = session.find("from HibernateXPDL");
313             for (Iterator JavaDoc it = xpdls.iterator();it.hasNext();){
314                 HibernateXPDL xpdl = (HibernateXPDL)it.next();
315                 for (Iterator JavaDoc itXpdlRef = xpdl.getXpdlReference().iterator();itXpdlRef.hasNext();){
316                     String JavaDoc refferingXPDLIdQuery = (String JavaDoc)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 JavaDoc ex) {
328             throw new RepositoryException(ex);
329         }
330     }
331
332     public List JavaDoc getReferredXPDLIds(RepositoryTransaction t, String JavaDoc refferingXPDLId, String JavaDoc refferingXPDLVersion) throws RepositoryException {
333         try {
334             List JavaDoc ret = new ArrayList JavaDoc();
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 JavaDoc 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 JavaDoc ex) {
351             throw new TransactionException(ex);
352         }
353     }
354
355     private HibernateXPDL getXPDL(Session session, String JavaDoc xpdlId, String JavaDoc xpdlVersion) throws Exception JavaDoc {
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 JavaDoc("There is no xpdl with Id=" + xpdlId + ", and version " + xpdlVersion + " in the repository");
362         }
363         return xpdl;
364     }
365
366     private List JavaDoc getAllXPDLs(Session session, String JavaDoc xpdlId) throws Exception JavaDoc {
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 JavaDoc getAllXPDLs(Session session) throws Exception JavaDoc {
373         return session.find("from HibernateXPDL");
374     }
375
376     private HibernateXPDL getLastVersionXPDL(Session session, String JavaDoc xpdlId) throws Exception JavaDoc {
377         List JavaDoc 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 JavaDoc it = xpdls.iterator();it.hasNext();){
385             HibernateXPDL xpdlTemp = (HibernateXPDL)it.next();
386             String JavaDoc 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 JavaDoc("Something is wrong in XPDL repository - can't determine XPDL version");
396
397         return lastVersion;
398     }
399
400     private HibernateXPDLHistory getXPDLHistory(Session session, String JavaDoc xpdlId, String JavaDoc xpdlVersion) throws Exception JavaDoc {
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 JavaDoc("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