KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.enhydra.shark.repositorypersistence;
2
3 import java.io.*;
4 import java.sql.Timestamp JavaDoc;
5 import java.util.*;
6
7 import com.lutris.appserver.server.sql.DatabaseManagerException;
8 import com.lutris.appserver.server.sql.LogicalDatabase;
9 import com.lutris.appserver.server.sql.DBTransaction;
10 import org.enhydra.dods.DODS;
11 import org.enhydra.shark.api.RepositoryTransaction;
12 import org.enhydra.shark.api.RootException;
13 import org.enhydra.shark.api.TransactionException;
14 import org.enhydra.shark.api.internal.repositorypersistence.RepositoryException;
15 import org.enhydra.shark.api.internal.repositorypersistence.RepositoryPersistenceManager;
16 import org.enhydra.shark.api.internal.working.CallbackUtilities;
17 import org.enhydra.shark.utilities.MiscUtilities;
18 import org.enhydra.shark.utilities.dods.DODSUtilities;
19 import org.enhydra.shark.repositorypersistence.data.*;
20
21 /**
22  * Database implementation of Repository persistence interface.
23  *
24  * @author Sasa Bojanic
25  *
26  */

27 public class DODSRepositoryPersistenceManager implements RepositoryPersistenceManager {
28
29    static boolean _debug_ = false;
30    private static final String JavaDoc DBG_PARAM_NAME = "DODSRepositoryPersistenceManager.debug";
31    private static final String JavaDoc INITIAL_VERSION = "1";
32
33    private static final String JavaDoc LDB_PARAM_NAME = "DODSRepositoryPersistenceManager.DatabaseName";
34    private CallbackUtilities cus;
35    private LogicalDatabase db;
36
37    /**
38     * Public constructor ().
39     */

40    public DODSRepositoryPersistenceManager () {
41       db = null;
42    }
43
44    /**
45     * Method configure is called at Shark start up, to configure
46     * DODSRepositoryPersistenceManager.
47     *
48     * @param cus an instance of CallbackUtilities used to get
49     * properties for configuring repository manager in Shark.
50     *
51     * @exception RootException thrown if configuring doesn't succeed.
52     */

53    public void configure (CallbackUtilities cus) throws RootException {
54       if (null == cus)
55          throw new RootException("Cannot configure without call back impl.");
56       this.cus = cus;
57       _debug_ = Boolean
58          .valueOf(cus.getProperty(DBG_PARAM_NAME, "false"))
59          .booleanValue();
60       DODSUtilities.init(cus.getProperties());
61       String JavaDoc dbName = cus
62          .getProperty(LDB_PARAM_NAME, DODS.getDatabaseManager().getDefaultDB());
63       try {
64          db = DODS.getDatabaseManager().findLogicalDatabase(dbName);
65       } catch (DatabaseManagerException e) {
66          throw new RootException("Couldn't find logical database.", e);
67       }
68       cus.debug("DODSRepositoryPersistenceManager configured");
69    }
70
71
72    public void uploadXPDL (RepositoryTransaction t,
73                            String JavaDoc xpdlId,
74                            byte[] xpdl,
75                            byte[] serializedPkg,
76                            long xpdlClassVer) throws RepositoryException {
77       cus.info("DODSRepositoryPersistenceManager -> Storing XPDL "+xpdlId+", BLOB1 size="+xpdl.length+", BLOB2 size="+serializedPkg.length);
78       try {
79          DBTransaction dbt=((DODSRepositoryTransaction)t).getDODSTransaction();
80          String JavaDoc xpdlVer=updateNextVersion(dbt,xpdlId);
81          XPDLDO newXPDLDO=XPDLDO.createVirgin(dbt);
82          newXPDLDO.setXPDLId(xpdlId);
83          newXPDLDO.setXPDLVersion(xpdlVer);
84          newXPDLDO.setXPDLClassVersion(xpdlClassVer);
85          newXPDLDO.setXPDLUploadTime(new Timestamp JavaDoc(System.currentTimeMillis()));
86          XPDLDataDO cont=XPDLDataDO.createVirgin(dbt);
87          cont.setXPDLContent(xpdl);
88          cont.setXPDLClassContent(serializedPkg);
89          cont.setXPDL(newXPDLDO);
90          cont.setCNT(DODSUtilities.getNext("_xpdldata_"));
91          newXPDLDO.save(dbt);
92          cont.save(dbt);
93          dbt.write();
94       } catch (Throwable JavaDoc thr) {
95          throw new RepositoryException("DODSRepositoryPersistenceManager -> The upload of xpdl "+xpdlId+" failed",thr);
96       }
97
98    }
99
100    public void updateXPDL (RepositoryTransaction t,
101                            String JavaDoc xpdlId,
102                            String JavaDoc xpdlVersion,
103                            byte[] xpdl,
104                            byte[] serializedPkg,
105                            long xpdlClassVer) throws RepositoryException {
106       try {
107          DBTransaction dbt=((DODSRepositoryTransaction)t).getDODSTransaction();
108          XPDLDO DO=getXPDLDO(dbt,xpdlId,xpdlVersion); // this will throw an exception if xpdl doesn't exist
109
DO.setXPDLClassVersion(xpdlClassVer);
110          XPDLDataDO cont=DO.getXPDLDataDO();
111          cont.setXPDLContent(xpdl);
112          cont.setXPDLClassContent(serializedPkg);
113          cont.save(dbt);
114          dbt.write();
115       } catch (Exception JavaDoc ex) {
116          cus.error("DODSRepositoryPersistenceManager -> The update of the xpdl with Id="+xpdlId+", and version="+xpdlVersion+" failed");
117          throw new RepositoryException(ex);
118       }
119    }
120
121    public void deleteXPDL (RepositoryTransaction t,
122                            String JavaDoc xpdlId,
123                            String JavaDoc xpdlVersion) throws RepositoryException {
124       try {
125          DBTransaction dbt=((DODSRepositoryTransaction)t).getDODSTransaction();
126          XPDLDO DO=getXPDLDO(dbt,xpdlId,xpdlVersion); // this will throw an exception if xpdl doesn't exist
127
DO.delete(dbt);
128          dbt.write();
129       } catch (Exception JavaDoc ex) {
130          throw new RepositoryException("XPDL ["+xpdlId+","+xpdlVersion+"] is not deleted from repository",ex);
131       }
132    }
133
134    public void moveToHistory (RepositoryTransaction t,
135                               String JavaDoc xpdlId,
136                               String JavaDoc xpdlVersion) throws RepositoryException {
137       try {
138          DBTransaction dbt=((DODSRepositoryTransaction)t).getDODSTransaction();
139          XPDLDO DO=getXPDLDO(dbt,xpdlId,xpdlVersion); // this will throw an exception if xpdl doesn't exist
140
XPDLHistoryDO xpdlHist=XPDLHistoryDO.createVirgin(dbt);
141          xpdlHist.setXPDLId(DO.getXPDLId());
142          xpdlHist.setXPDLVersion(DO.getXPDLVersion());
143          xpdlHist.setXPDLUploadTime(DO.getXPDLUploadTime());
144          xpdlHist.setXPDLHistoryUploadTime(new Timestamp JavaDoc(System.currentTimeMillis()));
145          XPDLHistoryDataDO cont=XPDLHistoryDataDO.createVirgin(dbt);
146          cont.setXPDLContent(DO.getXPDLDataDO().getXPDLContent());
147          cont.setXPDLHistory(xpdlHist);
148          cont.setCNT(DODSUtilities.getNext("_xpdlhistorydata_"));
149          DO.delete(dbt);
150          xpdlHist.save(dbt);
151          cont.save(dbt);
152          dbt.write();
153       } catch (Exception JavaDoc ex) {
154          throw new RepositoryException("XPDL ["+xpdlId+","+xpdlVersion+"] is not moved to history",ex);
155       }
156    }
157
158    public void deleteFromHistory (RepositoryTransaction t,
159                                   String JavaDoc xpdlId,
160                                   String JavaDoc xpdlVersion) throws RepositoryException {
161       try {
162          DBTransaction dbt=((DODSRepositoryTransaction)t).getDODSTransaction();
163          XPDLHistoryDO DO=getXPDLHistoryDO(dbt,xpdlId,xpdlVersion); // this will throw an exception if xpdl doesn't exist
164
DO.delete(dbt);
165          dbt.write();
166       } catch (Exception JavaDoc ex) {
167          throw new RepositoryException("XPDL ["+xpdlId+","+xpdlVersion+"] is not deleted from history",ex);
168       }
169    }
170
171    public void clearRepository (RepositoryTransaction t) throws RepositoryException {
172       try {
173          DBTransaction dbt=((DODSRepositoryTransaction)t).getDODSTransaction();
174          XPDLDO[] DOs=getAllXPDLs(dbt);
175          if (DOs!=null) {
176             for (int i=0; i<DOs.length; i++) {
177                DOs[i].delete();
178             }
179          }
180          dbt.write();
181       } catch (Exception JavaDoc ex) {
182          throw new RepositoryException("Some xpdl is not deleted from repository while clearing it",ex);
183       }
184    }
185
186    public String JavaDoc getCurrentVersion (RepositoryTransaction t,
187                                     String JavaDoc xpdlId) throws RepositoryException {
188       try {
189          DBTransaction dbt=((DODSRepositoryTransaction)t).getDODSTransaction();
190          return getLastVersionXPDLDO(dbt,xpdlId).getXPDLVersion();
191       } catch (Exception JavaDoc ex) {
192          throw new RepositoryException("No xpdl with Id="+xpdlId,ex);
193       }
194    }
195
196    public String JavaDoc getNextVersion (RepositoryTransaction t,
197                                  String JavaDoc xpdlId) throws RepositoryException {
198       try {
199          DBTransaction dbt=((DODSRepositoryTransaction)t).getDODSTransaction();
200          NextXPDLVersionQuery query=new NextXPDLVersionQuery(dbt);
201          query.setQueryXPDLId(xpdlId);
202          NextXPDLVersionDO nvDO=query.getNextDO();
203          if (nvDO==null) {
204             return INITIAL_VERSION;
205          } else {
206             return nvDO.getNextVersion();
207          }
208       } catch (Exception JavaDoc ex) {
209          throw new RepositoryException(ex);
210       }
211    }
212
213    public long getSerializedXPDLObjectVersion (RepositoryTransaction t,String JavaDoc xpdlId,String JavaDoc xpdlVersion) throws RepositoryException {
214       try {
215          DBTransaction dbt=((DODSRepositoryTransaction)t).getDODSTransaction();
216          return getXPDLDO(dbt,xpdlId,xpdlVersion).getXPDLClassVersion();
217       } catch (Exception JavaDoc ex) {
218          throw new RepositoryException("No xpdl with Id="+xpdlId,ex);
219       }
220    }
221
222    private String JavaDoc updateNextVersion (DBTransaction dbt,
223                                      String JavaDoc xpdlId) throws Exception JavaDoc {
224       NextXPDLVersionQuery query=new NextXPDLVersionQuery(dbt);
225       query.setQueryXPDLId(xpdlId);
226       NextXPDLVersionDO nvDO=query.getNextDO();
227       String JavaDoc curVersion=INITIAL_VERSION;
228       if (nvDO==null) {
229          nvDO=NextXPDLVersionDO.createVirgin(dbt);
230          nvDO.setXPDLId(xpdlId);
231          nvDO.setNextVersion(INITIAL_VERSION);
232       } else {
233          curVersion=nvDO.getNextVersion();
234       }
235       int nver=Integer.parseInt(nvDO.getNextVersion())+1;
236       String JavaDoc nextVersion=String.valueOf(nver);
237       nvDO.setNextVersion(nextVersion);
238       nvDO.save(dbt);
239       dbt.write();
240       return curVersion;
241    }
242
243    public byte[] getXPDL (RepositoryTransaction t,
244                           String JavaDoc xpdlId) throws RepositoryException {
245       try {
246          DBTransaction dbt=((DODSRepositoryTransaction)t).getDODSTransaction();
247          return getLastVersionXPDLDO(dbt,xpdlId).getXPDLDataDO().getXPDLContent();
248       } catch (Exception JavaDoc ex) {
249          throw new RepositoryException("No xpdl with Id="+xpdlId+" in repository",ex);
250       }
251    }
252
253    public byte[] getSerializedXPDLObject (RepositoryTransaction t,String JavaDoc xpdlId) throws RepositoryException {
254       try {
255          DBTransaction dbt=((DODSRepositoryTransaction)t).getDODSTransaction();
256          return getLastVersionXPDLDO(dbt,xpdlId).getXPDLDataDO().getXPDLClassContent();
257       } catch (Exception JavaDoc ex) {
258          throw new RepositoryException("No xpdl with Id="+xpdlId+" in repository",ex);
259       }
260    }
261
262    public byte[] getXPDL (RepositoryTransaction t,
263                           String JavaDoc xpdlId,
264                           String JavaDoc xpdlVersion) throws RepositoryException {
265       try {
266          DBTransaction dbt=((DODSRepositoryTransaction)t).getDODSTransaction();
267          return getXPDLDO(dbt,xpdlId,xpdlVersion).getXPDLDataDO().getXPDLContent();
268       } catch (Exception JavaDoc ex) {
269          throw new RepositoryException("No xpdl ["+xpdlId+","+xpdlVersion+"] in repository",ex);
270       }
271    }
272
273    public byte[] getSerializedXPDLObject (RepositoryTransaction t,String JavaDoc xpdlId,String JavaDoc xpdlVersion) throws RepositoryException {
274       try {
275          DBTransaction dbt=((DODSRepositoryTransaction)t).getDODSTransaction();
276          return getXPDLDO(dbt,xpdlId,xpdlVersion).getXPDLDataDO().getXPDLClassContent();
277       } catch (Exception JavaDoc ex) {
278          throw new RepositoryException("No xpdl ["+xpdlId+","+xpdlVersion+"] in repository",ex);
279       }
280    }
281    
282    public List getXPDLVersions (RepositoryTransaction t,
283                                 String JavaDoc xpdlId) throws RepositoryException {
284       try {
285          List xpdlVersions=new ArrayList();
286          DBTransaction dbt=((DODSRepositoryTransaction)t).getDODSTransaction();
287          XPDLDO[] DOs=getAllXPDLs(dbt,xpdlId);
288
289          for (int i=0; i<DOs.length; i++) {
290             xpdlVersions.add(DOs[i].getXPDLVersion());
291          }
292          return xpdlVersions;
293       } catch (Exception JavaDoc ex) {
294          throw new RepositoryException("No xpdl with Id="+xpdlId+" in repository",ex);
295       }
296    }
297
298    public boolean doesXPDLExist (RepositoryTransaction t,
299                                  String JavaDoc xpdlId) throws RepositoryException {
300       try {
301          DBTransaction dbt=((DODSRepositoryTransaction)t).getDODSTransaction();
302          XPDLDO DO=getLastVersionXPDLDO(dbt,xpdlId);
303          return getLastVersionXPDLDO(dbt,xpdlId)!=null;
304       } catch (Exception JavaDoc ex) {
305          return false;
306       }
307    }
308
309    public boolean doesXPDLExist (RepositoryTransaction t,
310                                  String JavaDoc xpdlId,
311                                  String JavaDoc xpdlVersion) throws RepositoryException {
312       try {
313          DBTransaction dbt=((DODSRepositoryTransaction)t).getDODSTransaction();
314          getXPDLDO(dbt,xpdlId,xpdlVersion);
315          return true;
316       } catch (Exception JavaDoc ex) {
317          return false;
318       }
319    }
320
321    public List getExistingXPDLIds (RepositoryTransaction t) throws RepositoryException {
322       try {
323          DBTransaction dbt=((DODSRepositoryTransaction)t).getDODSTransaction();
324          XPDLDO[] DOs=getAllXPDLs(dbt);
325          Set ids=new HashSet();
326          if (DOs!=null) {
327             for (int i=0; i<DOs.length; i++) {
328                ids.add(DOs[i].getXPDLId());
329             }
330          }
331          return new ArrayList(ids);
332       } catch (Exception JavaDoc ex) {
333          throw new RepositoryException(ex);
334       }
335    }
336
337    // TODO: see about references holding version information
338
public void addXPDLReference (RepositoryTransaction t,
339                                  String JavaDoc referredXPDLId,
340                                  String JavaDoc referringXPDLId,
341                                  String JavaDoc referringXPDLVersion,
342                                  int referredXPDLNumber) throws RepositoryException {
343       try {
344          DBTransaction dbt=((DODSRepositoryTransaction)t).getDODSTransaction();
345          if (getXPDLReference(dbt,referredXPDLId,referringXPDLId,referringXPDLVersion)==null) {
346             XPDLReferenceDO ref=XPDLReferenceDO.createVirgin(dbt);
347             ref.setReferredXPDLId(referredXPDLId);
348             ref.setReferringXPDL(getXPDLDO(dbt,referringXPDLId,referringXPDLVersion));
349             ref.setReferredXPDLNumber(referredXPDLNumber);
350             ref.save(dbt);
351          }
352          dbt.write();
353       } catch (Exception JavaDoc ex) {
354          throw new RepositoryException(ex);
355       }
356    }
357
358    public List getReferringXPDLIds (RepositoryTransaction t,
359                                     String JavaDoc referredXPDLId) throws RepositoryException {
360       try {
361          DBTransaction dbt=((DODSRepositoryTransaction)t).getDODSTransaction();
362          XPDLReferenceQuery query=new XPDLReferenceQuery(dbt);
363          query.setQueryReferredXPDLId(referredXPDLId);
364          XPDLReferenceDO[] refs=query.getDOArray();
365          Set referrers=new HashSet();
366          if (refs!=null) {
367             for (int i=0; i<refs.length; i++) {
368                referrers.add(refs[i].getReferringXPDL().getXPDLId());
369             }
370          }
371          return new ArrayList(referrers);
372
373       } catch (Exception JavaDoc ex) {
374          throw new RepositoryException(ex);
375       }
376    }
377
378    public List getReferringXPDLVersions (RepositoryTransaction t,
379                                          String JavaDoc referredXPDLId,
380                                          String JavaDoc refferingXPDLId) throws RepositoryException {
381       try {
382          DBTransaction dbt=((DODSRepositoryTransaction)t).getDODSTransaction();
383          XPDLReferenceQuery query=new XPDLReferenceQuery(dbt);
384          query.setQueryReferredXPDLId(referredXPDLId);
385          XPDLReferenceDO[] refs=query.getDOArray();
386          List referrers=new ArrayList();
387          if (refs!=null) {
388             for (int i=0; i<refs.length; i++) {
389                if (refs[i].getReferringXPDL().getXPDLId().equals(refferingXPDLId)) {
390                   referrers.add(refs[i].getReferringXPDL().getXPDLVersion());
391                }
392             }
393          }
394          return referrers;
395
396       } catch (Exception JavaDoc ex) {
397          throw new RepositoryException(ex);
398       }
399    }
400
401
402    public List getReferredXPDLIds (RepositoryTransaction t,
403                                    String JavaDoc refferingXPDLId,
404                                    String JavaDoc refferingXPDLVersion) throws RepositoryException {
405       try {
406          List ret=new ArrayList();
407          DBTransaction dbt=((DODSRepositoryTransaction)t).getDODSTransaction();
408          XPDLDO DO=getXPDLDO(dbt,refferingXPDLId,refferingXPDLVersion);
409          XPDLReferenceQuery query=new XPDLReferenceQuery(dbt);
410          query.setQueryReferringXPDL(DO);
411          XPDLReferenceDO[] refs=query.getDOArray();
412          if (refs!=null) {
413             Map temp=new HashMap();
414             for (int i=0; i<refs.length; i++) {
415                temp.put(new Integer JavaDoc(refs[i].getReferredXPDLNumber()),refs[i].getReferredXPDLId());
416             }
417             List tmp=new ArrayList(temp.keySet());
418             // sort by number
419
Collections.sort(tmp);
420             for (int i=0; i<tmp.size(); i++) {
421                ret.add(temp.get(tmp.get(i)));
422             }
423          }
424          return ret;
425
426       } catch (Exception JavaDoc ex) {
427          throw new RepositoryException(ex);
428       }
429    }
430
431    private XPDLReferenceDO getXPDLReference (DBTransaction dbt,
432                                              String JavaDoc referredId,
433                                              String JavaDoc referringId,
434                                              String JavaDoc referringVersion) throws Exception JavaDoc {
435       XPDLReferenceQuery query=new XPDLReferenceQuery(dbt);
436       query.setQueryReferredXPDLId(referredId);
437       query.setQueryReferringXPDL(getXPDLDO(dbt,referringId,referringVersion));
438       query.requireUniqueInstance();
439       XPDLReferenceDO ref=query.getNextDO();
440       return ref;
441    }
442
443    public RepositoryTransaction createTransaction() throws TransactionException {
444       try {
445          return new DODSRepositoryTransaction(DODS.getDatabaseManager().createTransaction());
446       } catch (Exception JavaDoc ex) {
447          throw new TransactionException(ex);
448       }
449    }
450
451    private XPDLDO getXPDLDO (DBTransaction dbt,String JavaDoc xpdlId,String JavaDoc xpdlVersion) throws Exception JavaDoc {
452       XPDLQuery query=new XPDLQuery(dbt);
453       //set query
454
query.setQueryXPDLId(xpdlId);
455       query.setQueryXPDLVersion(xpdlVersion);
456       query.requireUniqueInstance();
457
458       XPDLDO DO=query.getNextDO();
459       if (DO==null) {
460          throw new Exception JavaDoc("There is no xpdl with Id="+xpdlId+", and version "+xpdlVersion+" in the repository");
461       }
462       return DO;
463    }
464
465    private XPDLDO[] getAllXPDLs (DBTransaction dbt,String JavaDoc xpdlId) throws Exception JavaDoc {
466       XPDLQuery query=new XPDLQuery(dbt);
467       //set query
468
query.setQueryXPDLId(xpdlId);
469       return query.getDOArray();
470    }
471
472    private XPDLDO[] getAllXPDLs (DBTransaction dbt) throws Exception JavaDoc {
473       XPDLQuery query=new XPDLQuery(dbt);
474       return query.getDOArray();
475    }
476
477    private XPDLDO getLastVersionXPDLDO (DBTransaction dbt,String JavaDoc xpdlId) throws Exception JavaDoc {
478       XPDLDO[] xpdls=getAllXPDLs(dbt,xpdlId);
479       if (xpdls==null || xpdls.length==0) {
480          return null;
481       }
482
483       XPDLDO lastVersionDO=null;
484       int maxVer=-1;
485       for (int i=0; i<xpdls.length; i++) {
486          String JavaDoc xpdlVer=xpdls[i].getXPDLVersion();
487          int ver=Integer.parseInt(xpdlVer);
488          if (ver>maxVer) {
489             maxVer=ver;
490             lastVersionDO=xpdls[i];
491          }
492       }
493
494       if (lastVersionDO==null) throw new Exception JavaDoc ("Something is wrong in XPDL repository - can't determine XPDL version");
495
496       return lastVersionDO;
497    }
498
499    private XPDLHistoryDO getXPDLHistoryDO (DBTransaction dbt,String JavaDoc xpdlId,String JavaDoc xpdlVersion) throws Exception JavaDoc {
500       XPDLHistoryQuery query=new XPDLHistoryQuery(dbt);
501       //set query
502
query.setQueryXPDLId(xpdlId);
503       query.setQueryXPDLVersion(xpdlVersion);
504       query.requireUniqueInstance();
505
506       XPDLHistoryDO DO=query.getNextDO();
507       if (DO==null) {
508          throw new Exception JavaDoc("There is no xpdl with Id="+xpdlId+", and version "+xpdlVersion+" in the history repository");
509       }
510       return DO;
511    }
512
513 }
514
515
Popular Tags