KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > services > cms > repository > slide > JDBCDescriptorsStore


1 /*
2  *
3  * The Apache Software License, Version 1.1
4  *
5  * Copyright (c) 1999 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution, if
21  * any, must include the following acknowlegement:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowlegement may appear in the software itself,
25  * if and wherever such third-party acknowlegements normally appear.
26  *
27  * 4. The names "The Jakarta Project", "Jetspeed", and "Apache Software
28  * Foundation" must not be used to endorse or promote products derived
29  * from this software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache"
33  * nor may "Apache" appear in their names without prior written
34  * permission of the Apache Group.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  *
55  * [Additional notices, if required by prior licensing conditions]
56  *
57  */

58
59 package org.apache.jetspeed.services.cms.repository.slide;
60
61 import java.lang.reflect.Constructor JavaDoc;
62 import java.util.Hashtable JavaDoc;
63 import java.util.Enumeration JavaDoc;
64 import java.util.Vector JavaDoc;
65 import java.util.Date JavaDoc;
66 import java.io.FileWriter JavaDoc;
67 import java.io.IOException JavaDoc;
68 import java.sql.*;
69 import javax.transaction.xa.XAException JavaDoc;
70 import javax.transaction.xa.Xid JavaDoc;
71 import org.apache.slide.common.*;
72 import org.apache.slide.store.*;
73 import org.apache.slide.structure.*;
74 import org.apache.slide.security.*;
75 import org.apache.slide.lock.*;
76 import org.apache.slide.content.*;
77 import org.apache.slide.util.logger.Logger;
78
79 /**
80  * JDBC 1.0 and 2.0 compliant store implementation.
81  * This class comes from Slide code. Can be replace by another descriptor
82  * store.
83  *
84  *
85  *
86  * TO DO : Use J2EE Datastore instead of this class
87  *
88  * @author <a HREF="mailto:remm@apache.org">Remy Maucherat</a>
89  * @author Dirk Verbeeck
90  * @author <a HREF="mailto:christophe.lombart@skynet.be">Christophe Lombart</a>
91  */

92
93 public class JDBCDescriptorsStore
94     extends AbstractSimpleService
95     implements LockStore, NodeStore, RevisionDescriptorsStore,
96     RevisionDescriptorStore, SecurityStore
97 {
98
99     // -------------------------------------------------------------- Constants
100

101     // Column numbers
102

103     // Structure descriptors
104

105     protected static final int OBJECTS_URI = 1;
106     protected static final int OBJECTS_CLASS = 2;
107
108     protected static final int CHILDREN_URI = 1;
109     protected static final int CHILDREN_CHILDURI = 2;
110
111     protected static final int LINKS_LINK = 1;
112     protected static final int LINKS_LINKTO = 2;
113
114     // Security descriptors
115

116     protected static final int PERMISSIONS_OBJECT = 1;
117     protected static final int PERMISSIONS_REVISION_NUMBER = 2;
118     protected static final int PERMISSIONS_SUBJECT = 3;
119     protected static final int PERMISSIONS_ACTION = 4;
120     protected static final int PERMISSIONS_INHERITABLE = 5;
121     protected static final int PERMISSIONS_NEGATIVE = 6;
122
123     // Lock descriptors
124

125     protected static final int LOCKS_ID = 1;
126     protected static final int LOCKS_OBJECT = 2;
127     protected static final int LOCKS_SUBJECT = 3;
128     protected static final int LOCKS_TYPE = 4;
129     protected static final int LOCKS_EXPIRATIONDATE = 5;
130     protected static final int LOCKS_INHERITABLE = 6;
131     protected static final int LOCKS_EXCLUSIVE = 7;
132
133     // Content descriptors
134

135     protected static final int REVISIONS_URI = 1;
136     protected static final int REVISIONS_ISVERSIONED = 2;
137     protected static final int REVISIONS_INITIALREVISION = 3;
138
139     protected static final int WORKINGREVISION_URI = 1;
140     protected static final int WORKINGREVISION_BASEREVISION = 2;
141     protected static final int WORKINGREVISION_NUMBER = 3;
142
143     protected static final int LATESTREVISIONS_URI = 1;
144     protected static final int LATESTREVISIONS_BRANCHNAME = 2;
145     protected static final int LATESTREVISIONS_NUMBER = 3;
146
147     protected static final int BRANCHES_URI = 1;
148     protected static final int BRANCHES_NUMBER = 2;
149     protected static final int BRANCHES_CHILDNUMBER = 3;
150
151     protected static final int REVISION_URI = 1;
152     protected static final int REVISION_NUMBER = 2;
153     protected static final int REVISION_BRANCHNAME = 3;
154
155     protected static final int LABEL_URI = 1;
156     protected static final int LABEL_NUMBER = 2;
157     protected static final int LABEL_LABEL = 3;
158
159     protected static final int PROPERTY_URI = 1;
160     protected static final int PROPERTY_NUMBER = 2;
161     protected static final int PROPERTY_NAME = 3;
162     protected static final int PROPERTY_VALUE = 4;
163     protected static final int PROPERTY_NAMESPACE = 5;
164     protected static final int PROPERTY_TYPE = 6;
165     protected static final int PROPERTY_PROTECTED = 7;
166
167
168     // ----------------------------------------------------- Instance Variables
169

170
171     /**
172      * Database connection.
173      */

174     protected Connection connection;
175
176
177     /**
178      * Driver class name.
179      */

180     protected String JavaDoc driver;
181
182
183     /**
184      * Connection URL.
185      */

186     protected String JavaDoc url;
187
188
189     /**
190      * User name.
191      */

192     protected String JavaDoc user;
193
194
195     /**
196      * Password.
197      */

198     protected String JavaDoc password;
199
200
201     /**
202      * JDBC Version to use.
203      */

204     protected int jdbcVersion;
205
206     /**
207      * This store doesn't handle nested transactions, this variable keeps track
208      * if the store is already enlisted to a transaction.
209      */

210     protected boolean alreadyEnlisted=false;
211
212     // -------------------------------------------------------- Service Methods
213

214     /**
215      * Returns the sql statements to create the database objects.
216      */

217     protected String JavaDoc[] getDatabaseCreateStatements()
218     {
219         String JavaDoc[] statements = {
220             "create table objects(uri varchar(65536) primary key," +
221             " classname varchar(4096))",
222             "create table children(uri varchar(65536), " +
223             " childuri varchar(65536))",
224             "create table links(link varchar(65536), " +
225             " linkto varchar(65536))",
226             "create table permissions(object varchar(65536)," +
227             " revisionnumber varchar(20), " +
228             " subject varchar(65536), action varchar(65536), " +
229             " inheritable int, negative int)",
230             "create table locks(id varchar(65536), object varchar(4096)," +
231             " subject varchar(4096), type varchar(4096), " +
232             " expirationdate varchar(15), inheritable int, " +
233             " xexclusive int)",
234             "create table revisions(uri varchar(65536) primary key, " +
235             " isversioned int, initialrevision varchar(10))",
236             "create table workingrevision(uri varchar(65536), " +
237             " baserevision varchar(20), xnumber varchar(20))",
238             "create table latestrevisions(uri varchar(65536), " +
239             " branchname varchar(4096), xnumber varchar(20))",
240             "create table branches(uri varchar(65536), xnumber varchar(20)," +
241             " childnumber varchar(20))",
242             "create table revision(uri varchar(65536), xnumber varchar(20)," +
243             " branchname varchar(4096))",
244             "create table label(uri varchar(65536), xnumber varchar(20)," +
245             " label varchar(4096))",
246             "create table property(uri varchar(65536), xnumber varchar(20)," +
247             " name varchar(4096), value varchar(65536), " +
248             " namespace varchar(4096), type varchar(100), protected int)"};
249
250         return statements;
251     }
252
253     /**
254      * Initializes the data source with a set of parameters.
255      *
256      * @param parameters Hashtable containing the parameters' name
257      * and associated value
258      * @exception ServiceParameterErrorException Incorrect service parameter
259      * @exception ServiceParameterMissingException Service parameter missing
260      */

261     public void setParameters(Hashtable JavaDoc parameters)
262         throws ServiceParameterErrorException,
263         ServiceParameterMissingException
264     {
265
266         // Driver classname
267
driver = (String JavaDoc) parameters.get("driver");
268
269         // Connection url
270
url = (String JavaDoc) parameters.get("url");
271
272         // FIXME: before slide 1.0.12 the database url was passed
273
// without "jdbc:" this compatibility code sould be removed in the
274
// future code changed 18 jul 2001
275
if (!url.startsWith("jdbc:"))
276         {
277             url="jdbc:" + url;
278         }
279         // end compatibility code
280

281         // User name
282
user = (String JavaDoc) parameters.get("user");
283         if (user == null)
284         {
285             user = new String JavaDoc();
286         }
287
288         // Password
289
password = (String JavaDoc) parameters.get("password");
290         if (password == null)
291         {
292             password = new String JavaDoc();
293         }
294
295         // JDBC version to use
296
jdbcVersion = 1;
297         String JavaDoc version = (String JavaDoc) parameters.get("jdbcversion");
298         if (version != null)
299         {
300             jdbcVersion = (new Integer JavaDoc(version)).intValue();
301         }
302     }
303
304     /**
305      * Connects to JDBC and creates the basic table structure.
306      *
307      * @exception ServiceConnectionFailedException Connection to the
308      * database failed
309      */

310     public synchronized void connect()
311         throws ServiceConnectionFailedException
312     {
313         getLogger().log("Connecting to \"" + url + "\" as user \"" + user + "\"",LOG_CHANNEL,Logger.INFO);
314         try
315         {
316             connection = DriverManager.getConnection(url, user, password);
317         }
318         catch (SQLException e)
319         {
320             getLogger().log("Connecting to \"" + url + "\" as user \"" + user + "\" failed",LOG_CHANNEL,Logger.ERROR);
321             getLogger().log(e.toString(),LOG_CHANNEL,Logger.ERROR);
322             throw new ServiceConnectionFailedException(this, e);
323         }
324
325         // all updates must be done inside a transaction, no auto commits
326
try
327         {
328             connection.setAutoCommit(false);
329         }
330         catch (SQLException e)
331         {
332         }
333        /*
334         Statement statement = null;
335         try {
336             statement = connection.createStatement();
337             String[] statements = getDatabaseCreateStatements();
338             for (int i=0; i<statements.length ; i++ ) {
339                 statement.execute(statements[i]);
340             }
341
342             // Cloudscape needs a commit on DDL statements (create,...)
343             connection.commit();
344
345         } catch (SQLException e) {
346             try { connection.rollback(); } catch (SQLException ex) { }
347         } finally {
348             closeStatement(statement);
349         }
350         */

351         // we are just connected and are not enlisted
352
alreadyEnlisted=false;
353     }
354
355     /**
356      * Disconnects from data source.
357      *
358      * @exception ServiceDisconnectionFailedException Disconnection
359      * from database failed
360      */

361     public void disconnect()
362         throws ServiceDisconnectionFailedException
363     {
364         getLogger().log("Disconnecting from \"" + url + "\" as user \"" + user + "\"",LOG_CHANNEL,Logger.INFO);
365         try
366         {
367             if (connection != null)
368             {
369                 connection.close();
370             }
371             connection = null;
372         }
373         catch (SQLException e)
374         {
375             getLogger().log("Disconnecting from \"" + url + "\" as user \"" + user + "\" failed",LOG_CHANNEL,Logger.ERROR);
376             getLogger().log(e.toString(),LOG_CHANNEL,Logger.ERROR);
377             throw new ServiceDisconnectionFailedException(this, e);
378         }
379     }
380
381     /**
382      * Initializes data source.
383      * <p/>
384      * Occurs in four steps :
385      * <li>Driver class is loaded</li>
386      * <li>Driver is intantiated</li>
387      * <li>Driver registration in the driver manager</li>
388      * <li>Creation of the basic tables, if they didn't exist before</li>
389      *
390      * @exception ServiceInitializationFailedException Throws an exception
391      * if the data source has already been initialized before
392      */

393     public synchronized void initialize(NamespaceAccessToken token)
394         throws ServiceInitializationFailedException
395     {
396         try
397         {
398             // Loading and registering driver
399
token.getLogger().log("Loading and registering driver: " + driver,LOG_CHANNEL,Logger.INFO);
400             Class JavaDoc driverClass = Class.forName(driver);
401             Driver databaseDriver = (Driver) driverClass.newInstance();
402             DriverManager.registerDriver(databaseDriver);
403         }
404         catch (ClassNotFoundException JavaDoc e)
405         {
406             token.getLogger().log("Loading and registering driver " + driver + " failed",LOG_CHANNEL,Logger.ERROR);
407             token.getLogger().log(e.toString(),LOG_CHANNEL,Logger.ERROR);
408             throw new ServiceInitializationFailedException(this, e.getMessage());
409         }
410         catch (InstantiationException JavaDoc e)
411         {
412             token.getLogger().log("Loading and registering driver " + driver + " failed",LOG_CHANNEL,Logger.ERROR);
413             token.getLogger().log(e.toString(),LOG_CHANNEL,Logger.ERROR);
414             throw new ServiceInitializationFailedException(this, e.getMessage());
415         }
416         catch (IllegalAccessException JavaDoc e)
417         {
418             token.getLogger().log("Loading and registering driver " + driver + " failed",LOG_CHANNEL,Logger.ERROR);
419             token.getLogger().log(e.toString(),LOG_CHANNEL,Logger.ERROR);
420             throw new ServiceInitializationFailedException(this, e.getMessage());
421         }
422         catch (SQLException e)
423         {
424             token.getLogger().log("Loading and registering driver " + driver + " failed",LOG_CHANNEL,Logger.ERROR);
425             token.getLogger().log(e.toString(),LOG_CHANNEL,Logger.ERROR);
426             throw new ServiceInitializationFailedException(this, e.getMessage());
427         }
428         catch (ClassCastException JavaDoc e)
429         {
430             token.getLogger().log("Loading and registering driver " + driver + " failed",LOG_CHANNEL,Logger.ERROR);
431             token.getLogger().log(e.toString(),LOG_CHANNEL,Logger.ERROR);
432             throw new ServiceInitializationFailedException(this, e.getMessage());
433         }
434         catch (Exception JavaDoc e)
435         {
436             token.getLogger().log("Loading and registering driver " + driver + " failed",LOG_CHANNEL,Logger.ERROR);
437             token.getLogger().log(e.toString(),LOG_CHANNEL,Logger.ERROR);
438             throw new ServiceInitializationFailedException(this, e.getMessage());
439         }
440     }
441
442
443     /**
444      * Deletes data source. Should remove stored data if possible.
445      *
446      * @exception ServiceResetFailedException Reset failed
447      */

448     public synchronized void reset()
449         throws ServiceResetFailedException
450     {
451         Statement statement = null;
452        /*
453         try {
454             connectIfNeeded();
455
456             statement = connection.createStatement();
457             String s = null;
458
459             s = "drop table objects";
460             statement.execute(s);
461
462             s = "drop table children";
463             statement.execute(s);
464
465             s = "drop table links";
466             statement.execute(s);
467
468             s = "drop table permissions";
469             statement.execute(s);
470
471             s = "drop table locks";
472             statement.execute(s);
473
474             s = "drop table revisions";
475             statement.execute(s);
476
477             s = "drop table workingrevision";
478             statement.execute(s);
479
480             s = "drop table latestrevisions";
481             statement.execute(s);
482
483             s = "drop table branches";
484             statement.execute(s);
485
486             s = "drop table revision";
487             statement.execute(s);
488
489             s = "drop table label";
490             statement.execute(s);
491
492             s = "drop table property";
493             statement.execute(s);
494
495             statement.close();
496             disconnect();
497         } catch (SQLException e) {
498             throw new ServiceResetFailedException(this, e.getMessage());
499         } catch (ServiceAccessException e) {
500             throw new ServiceResetFailedException(this, e.getMessage());
501         } catch (ServiceConnectionFailedException e) {
502             throw new ServiceResetFailedException(this, e.getMessage());
503         } catch (ServiceDisconnectionFailedException e) {
504             throw new ServiceResetFailedException(this, e.getMessage());
505         } finally {
506             closeStatement(statement);
507         }
508         */

509     }
510
511
512     /**
513      * This function tells whether or not the data source is connected.
514      *
515      * @return boolean true if we are connected
516      * @exception ServiceAccessException Error accessing DataSource
517      */

518     public boolean isConnected()
519         throws ServiceAccessException
520     {
521         try
522         {
523             return ((connection != null) && (!connection.isClosed()));
524         }
525         catch (SQLException e)
526         {
527             throw new ServiceAccessException(this, e);
528         }
529     }
530
531
532     // ----------------------------------------------------- XAResource Methods
533

534
535     /**
536      * Commit the global transaction specified by xid.
537      */

538     public void commit(Xid JavaDoc xid, boolean onePhase)
539         throws XAException JavaDoc
540     {
541         super.commit(xid, onePhase);
542
543         try
544         {
545 // getLogger().log("commit",LOG_CHANNEL,Logger.DEBUG);
546
connection.commit();
547         }
548         catch (SQLException e)
549         {
550             throw new XAException JavaDoc(XAException.XA_RBCOMMFAIL);
551         }
552         alreadyEnlisted=false;
553     }
554
555
556     /**
557      * Inform the resource manager to roll back work done on behalf of a
558      * transaction branch.
559      */

560     public void rollback(Xid JavaDoc xid)
561         throws XAException JavaDoc
562     {
563         super.rollback(xid);
564
565         try
566         {
567 // getLogger().log("rollback",LOG_CHANNEL,Logger.DEBUG);
568
connection.rollback();
569         }
570         catch (SQLException e)
571         {
572             throw new XAException JavaDoc(XAException.XA_HEURCOM);
573         }
574         alreadyEnlisted=false;
575     }
576
577
578     /**
579      * Start work on behalf of a transaction branch specified in xid.
580      */

581     public void start(Xid JavaDoc xid, int flags)
582         throws XAException JavaDoc
583     {
584         super.start(xid, flags);
585         if (!alreadyEnlisted)
586         {
587             try
588             {
589 // getLogger().log("start",LOG_CHANNEL,Logger.DEBUG);
590
// discard changes made outside a tranaction
591
connection.rollback();
592             }
593             catch (SQLException e)
594             {
595                 throw new XAException JavaDoc(XAException.XAER_RMERR);
596             }
597             alreadyEnlisted=true;
598         }
599     }
600
601
602     // ----------------------------------------------- DescriptorsStore Methods
603

604
605     /**
606      * Retrive an object.
607      *
608      * @param uri Uri of the object we want to retrieve
609      * @exception ServiceAccessException Error accessing the Service
610      * @exception ObjectNotFoundException The object to retrieve was not found
611      */

612     public ObjectNode retrieveObject(Uri uri)
613         throws ServiceAccessException, ObjectNotFoundException
614     {
615
616         ObjectNode result = null;
617         PreparedStatement statement = null;
618
619         try
620         {
621
622             statement = connection.prepareStatement
623                 ("select * from objects where uri= ?");
624             statement.setString(1, uri.toString());
625
626             ResultSet res = statement.executeQuery();
627
628             // Parsing result set
629

630             String JavaDoc className;
631
632             if (res.next())
633             {
634                 // Retrieving and loading the object
635
className = res.getString(OBJECTS_CLASS);
636             }
637             else
638             {
639                 // Object was not found ...
640
throw new ObjectNotFoundException(uri);
641             }
642
643             closeStatement(statement);
644
645             // Then, retrieve the children
646
statement = connection.prepareStatement
647                 ("select * from children where uri= ?");
648             statement.setString(1,uri.toString());
649             res = statement.executeQuery();
650
651             Vector JavaDoc childrenVector = new Vector JavaDoc();
652
653             // Parse result set
654
while (res.next())
655             {
656                 // Load each permission
657
childrenVector.addElement(res.getString(CHILDREN_CHILDURI));
658             }
659             closeStatement(statement);
660
661             statement = connection.prepareStatement
662                 ("select * from links where linkto= ?");
663             statement.setString(1,uri.toString());
664             res = statement.executeQuery();
665
666             Vector JavaDoc linksVector = new Vector JavaDoc();
667
668             // Parse result set
669
while (res.next())
670             {
671                 // Load each permission
672
linksVector.addElement(res.getString(LINKS_LINKTO));
673             }
674
675             closeStatement(statement);
676
677             if (className.equals("org.apache.slide.structure.LinkNode"))
678             {
679
680                 String JavaDoc linkTo = new String JavaDoc();
681                 statement = connection.prepareStatement
682                     ("select * from links where link= ?");
683                 statement.setString(1,uri.toString());
684                 res = statement.executeQuery();
685
686                 if(res.next())
687                 {
688                     linkTo = res.getString(LINKS_LINKTO);
689                 }
690
691                 closeStatement(statement);
692
693                 result = new LinkNode(uri.toString(), childrenVector,
694                                       linksVector, linkTo);
695
696             }
697             else
698             {
699
700                 try
701                 {
702                     Class JavaDoc objclass = Class.forName(className);
703
704                     Class JavaDoc[] argClasses =
705                         {
706                             Class.forName("java.lang.String"),
707                             Class.forName("java.util.Vector"),
708                             Class.forName("java.util.Vector")
709                         };
710                     Object JavaDoc[] arguments =
711                         {
712                             uri.toString(),
713                             childrenVector,
714                             linksVector
715                         };
716
717                     Constructor JavaDoc constructor =
718                         objclass.getConstructor(argClasses);
719                     result = (ObjectNode)constructor.newInstance(arguments);
720                 }
721                 catch(Exception JavaDoc e)
722                 {
723                     // ClassNotFoundException, NoSuchMethodException, etc.
724
throw new ServiceAccessException(this, e);
725                 }
726             }
727         }
728         catch (SQLException e)
729         {
730             getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
731             throw new ServiceAccessException(this, e);
732         }
733         finally
734         {
735             closeStatement(statement);
736         }
737         return result;
738     }
739
740
741     /**
742      * Update an object.
743      *
744      * @param object Object to update
745      * @exception ServiceAccessException Error accessing the Service
746      * @exception ObjectNotFoundException The object to update was not found
747      */

748     public void storeObject(Uri uri, ObjectNode object)
749         throws ServiceAccessException, ObjectNotFoundException
750     {
751
752         PreparedStatement statement = null;
753
754         try
755         {
756             statement = connection.prepareStatement
757                 ("select * from objects where uri= ?");
758             statement.setString(1, uri.toString());
759
760             ResultSet res = statement.executeQuery();
761
762             // Parsing result set
763

764             if (!res.next())
765             {
766                 throw new ObjectNotFoundException(uri);
767             }
768
769             closeStatement(statement);
770
771             // Updating children
772
statement = connection.prepareStatement
773                 ("delete from children where uri= ?");
774             statement.setString(1, object.getUri());
775             statement.execute();
776             closeStatement(statement);
777
778             statement = null;
779             Enumeration JavaDoc children = object.enumerateChildren();
780             while (children.hasMoreElements())
781             {
782                 if (statement == null)
783                 {
784                     statement = connection.prepareStatement
785                         ("insert into children values(?, ?)");
786                 }
787                 statement.setString(1, object.getUri());
788                 statement.setString(2, (String JavaDoc)children.nextElement());
789                 statement.execute();
790             }
791             closeStatement(statement);
792
793             // Updating inbound links
794
/*
795             s = "delete from links where linkto='" + object.getUri() + "'";
796             statement.execute(s);
797             Enumeration links = object.enumerateLinks();
798             while (children.hasMoreElements()) {
799                 s = "insert into links values('"
800                     + (String) links.nextElement() + "', '"
801                     + object.getUri() + "')";
802                 statement.execute(s);
803             }
804             */

805
806             // Updating links
807
statement = connection.prepareStatement
808                 ("delete from links where link= ?");
809             statement.setString(1, object.getUri());
810             statement.execute();
811             closeStatement(statement);
812
813             if (object instanceof LinkNode)
814             {
815                 statement = connection.prepareStatement
816                     ("insert into links values(?,?)");
817                 statement.setString(1, object.getUri());
818                 statement.setString(2, ((LinkNode) object).getLinkedUri());
819                 statement.execute();
820                 closeStatement(statement);
821             }
822         }
823         catch (SQLException e)
824         {
825             getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
826             throw new ServiceAccessException(this, e);
827         }
828         finally
829         {
830             closeStatement(statement);
831         }
832     }
833
834
835     /**
836      * Create a new object.
837      *
838      * @param object ObjectNode
839      * @param uri Uri of the object we want to create
840      * @exception ServiceAccessException Error accessing the Service
841      * @exception ObjectAlreadyExistsException An object already exists
842      * at this Uri
843      */

844     public void createObject(Uri uri, ObjectNode object)
845         throws ServiceAccessException, ObjectAlreadyExistsException
846     {
847
848         PreparedStatement statement = null;
849
850         try
851         {
852
853             String JavaDoc className = object.getClass().getName();
854
855             statement = connection.prepareStatement
856                 ("select * from objects where uri= ?");
857             statement.setString(1, uri.toString());
858
859             ResultSet res = statement.executeQuery();
860
861             // Parsing result set
862

863             if (res.next())
864             {
865                 throw new ObjectAlreadyExistsException(uri.toString());
866             }
867
868             closeStatement(statement);
869
870             statement = connection.prepareStatement
871                 ("insert into objects values(?,?)");
872             statement.setString(1, uri.toString());
873             statement.setString(2, className );
874
875             statement.execute();
876             closeStatement(statement);
877
878             statement = null;
879             // Inserting children
880
Enumeration JavaDoc children = object.enumerateChildren();
881             while (children.hasMoreElements())
882             {
883                 if (statement == null)
884                 {
885                     statement = connection.prepareStatement
886                         ("insert into children values(?,?)");
887                 }
888                 statement.setString(1, uri.toString());
889                 statement.setString(2, (String JavaDoc) children.nextElement());
890                 statement.execute();
891             }
892             closeStatement(statement);
893
894             // Updating inbound links
895
/*
896             Enumeration links = object.enumerateLinks();
897             while (children.hasMoreElements()) {
898                 s = "insert into links values('"
899                     + (String) links.nextElement() + "', '"
900                     + object.getUri() + "')";
901                 statement.execute(s);
902             }
903             */

904
905             // If the object is a link, also store the link information
906
if (object instanceof LinkNode)
907             {
908                 statement = connection.prepareStatement
909                     ("insert into links values(?,?)");
910                 statement.setString(1, uri.toString());
911                 statement.setString(2, ((LinkNode) object).getLinkedUri());
912                 statement.execute();
913                 closeStatement(statement);
914             }
915         }
916         catch (SQLException e)
917         {
918             getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
919             throw new ServiceAccessException(this, e);
920         }
921         finally
922         {
923             closeStatement(statement);
924         }
925
926     }
927
928
929     /**
930      * Remove an object.
931      *
932      * @param object Object to remove
933      * @exception ServiceAccessException Error accessing the Service
934      * @exception ObjectNotFoundException The object to remove was not found
935      */

936     public void removeObject(Uri uri, ObjectNode object)
937         throws ServiceAccessException, ObjectNotFoundException
938     {
939
940         PreparedStatement statement = null;
941
942         try
943         {
944             // Removing object
945
statement = connection.prepareStatement
946                 ("delete from objects where uri= ?");
947             statement.setString(1,object.getUri());
948             statement.execute();
949             closeStatement(statement);
950
951             // Removing children
952
statement = connection.prepareStatement
953                 ("delete from children where uri=?");
954             statement.setString(1, object.getUri());
955             statement.execute();
956             closeStatement(statement);
957
958             // Removing inbound links
959
/*
960             s = "delete from links where linkto='" + object.getUri() + "'";
961             statement.execute(s);
962             */

963
964             // Removing links
965
statement = connection.prepareStatement
966                 ("delete from links where link= ?");
967             statement.setString(1, object.getUri());
968             statement.execute();
969             closeStatement(statement);
970         }
971         catch (SQLException e)
972         {
973             getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
974             throw new ServiceAccessException(this, e);
975         }
976     }
977
978
979     /**
980      * Grant a new permission.
981      *
982      * @param permission Permission we want to create
983      * @exception ServiceAccessException Error accessing the Service
984      */

985     public void grantPermission(Uri uri, NodePermission permission)
986         throws ServiceAccessException
987     {
988
989         PreparedStatement statement = null;
990
991         try
992         {
993             int inheritable = 0;
994             if (permission.isInheritable())
995             {
996                 inheritable = 1;
997             }
998
999             int negative = 0;
1000            if (permission.isNegative())
1001            {
1002                negative = 1;
1003            }
1004
1005            NodeRevisionNumber revisionNumber = permission.getRevisionNumber();
1006            String JavaDoc revisionNumberStr =
1007                (revisionNumber == null) ? null : revisionNumber.toString();
1008
1009            statement = connection.prepareStatement
1010                ("insert into permissions values(?,?,?,?,?,?)");
1011            statement.setString(1, permission.getObjectUri());
1012            statement.setString(2, revisionNumberStr);
1013            statement.setString(3, permission.getSubjectUri());
1014            statement.setString(4, permission.getActionUri());
1015            statement.setInt(5, inheritable);
1016            statement.setInt(6, negative);
1017            statement.execute();
1018        }
1019        catch (SQLException e)
1020        {
1021            getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
1022            throw new ServiceAccessException(this, e);
1023        }
1024        finally
1025        {
1026            closeStatement(statement);
1027        }
1028    }
1029
1030
1031    /**
1032     * Revoke a permission.
1033     *
1034     * @param permission Permission we want to create
1035     * @exception ServiceAccessException Error accessing the Service
1036     */

1037    public void revokePermission(Uri uri, NodePermission permission)
1038        throws ServiceAccessException
1039    {
1040
1041        PreparedStatement statement = null;
1042
1043        try
1044        {
1045            NodeRevisionNumber revisionNumber = permission.getRevisionNumber();
1046
1047            if(revisionNumber != null)
1048            {
1049                statement = connection.prepareStatement
1050                    ("delete from permissions where object= ? and subject = ? and \"ACTION\" = ? and revisionnumber = ? ");
1051                statement.setString(4, revisionNumber.toString());
1052            }
1053            else
1054            {
1055                statement = connection.prepareStatement
1056                    ("delete from permissions where object = ? and subject = ? and \"ACTION\" = ? and revisionnumber is NULL");
1057            }
1058
1059            statement.setString(1, permission.getObjectUri());
1060            statement.setString(2, permission.getSubjectUri());
1061            statement.setString(3, permission.getActionUri());
1062
1063            statement.execute();
1064        }
1065        catch (SQLException e)
1066        {
1067            getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
1068            throw new ServiceAccessException(this, e);
1069        }
1070        finally
1071        {
1072            closeStatement(statement);
1073        }
1074    }
1075
1076
1077    /**
1078     * Revoke all the permissions on an object.
1079     *
1080     * @param permission Permission we want to create
1081     * @exception ServiceAccessException Error accessing the Service
1082     */

1083    public void revokePermissions(Uri uri)
1084        throws ServiceAccessException
1085    {
1086
1087        PreparedStatement statement = null;
1088
1089        try
1090        {
1091            statement = connection.prepareStatement
1092                ("delete from permissions where object= ?");
1093            statement.setString(1, uri.toString());
1094            statement.execute();
1095        }
1096        catch (SQLException e)
1097        {
1098            getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
1099            throw new ServiceAccessException(this, e);
1100        }
1101        finally
1102        {
1103            closeStatement(statement);
1104        }
1105    }
1106
1107
1108    /**
1109     * Enumerate permissions on an object.
1110     *
1111     * @param permission Permission we want to create
1112     * @exception ServiceAccessException Error accessing the Service
1113     */

1114    public Enumeration JavaDoc enumeratePermissions(Uri uri)
1115        throws ServiceAccessException
1116    {
1117
1118        Vector JavaDoc permissionVector = new Vector JavaDoc();
1119        PreparedStatement statement = null;
1120
1121        try
1122        {
1123            statement = connection.prepareStatement
1124                ("select * from permissions where object= ?");
1125            statement.setString(1, uri.toString());
1126            ResultSet res = statement.executeQuery();
1127
1128            while (res.next())
1129            {
1130                String JavaDoc object = res.getString(PERMISSIONS_OBJECT);
1131                String JavaDoc revision = res.getString(PERMISSIONS_REVISION_NUMBER);
1132                String JavaDoc subject = res.getString(PERMISSIONS_SUBJECT);
1133                String JavaDoc action = res.getString(PERMISSIONS_ACTION);
1134
1135                boolean inheritable = false;
1136                if (res.getInt(PERMISSIONS_INHERITABLE) == 1)
1137                {
1138                    inheritable = true;
1139                }
1140                boolean negative = false;
1141                if (res.getInt(PERMISSIONS_NEGATIVE) == 1)
1142                {
1143                    negative = true;
1144                }
1145                NodePermission permission =
1146                    new NodePermission(object,revision,subject,
1147                                       action,inheritable,negative);
1148                permissionVector.addElement(permission);
1149            }
1150        }
1151        catch (SQLException e)
1152        {
1153            getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
1154            throw new ServiceAccessException(this, e);
1155        }
1156        finally
1157        {
1158            closeStatement(statement);
1159        }
1160
1161        return permissionVector.elements();
1162    }
1163
1164
1165    /**
1166     * Create a new lock.
1167     *
1168     * @param lock Lock token
1169     * @exception ServiceAccessException Service access error
1170     */

1171    public void putLock(Uri uri, NodeLock lock)
1172        throws ServiceAccessException
1173    {
1174
1175        PreparedStatement statement = null;
1176
1177        try
1178        {
1179            int inheritable = 0;
1180            if (lock.isInheritable())
1181            {
1182                inheritable = 1;
1183            }
1184
1185            int exclusive = 0;
1186            if (lock.isExclusive())
1187            {
1188                exclusive = 1;
1189            }
1190
1191            statement = connection.prepareStatement
1192                ("insert into locks values(?,?,?,?,?,?,?)");
1193            statement.setString(1, lock.getLockId());
1194            statement.setString(2, lock.getObjectUri());
1195            statement.setString(3, lock.getSubjectUri());
1196            statement.setString(4, lock.getTypeUri());
1197            statement.setString
1198                (5, String.valueOf(lock.getExpirationDate().getTime()));
1199            statement.setInt(6,inheritable);
1200            statement.setInt(7, exclusive);
1201            statement.execute();
1202        }
1203        catch (SQLException e)
1204        {
1205            getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
1206            throw new ServiceAccessException(this, e);
1207        }
1208        finally
1209        {
1210            closeStatement(statement);
1211        }
1212
1213    }
1214
1215
1216    /**
1217     * Renew a lock.
1218     *
1219     * @param lock Token to renew
1220     * @exception ServiceAccessException Service access error
1221     * @exception LockTokenNotFoundException Lock token was not found
1222     */

1223    public void renewLock(Uri uri, NodeLock lock)
1224        throws ServiceAccessException, LockTokenNotFoundException
1225    {
1226
1227        PreparedStatement statement = null;
1228
1229        try
1230        {
1231
1232            int inheritable = 0;
1233            if (lock.isInheritable())
1234            {
1235                inheritable = 1;
1236            }
1237
1238            int exclusive = 0;
1239            if (lock.isExclusive())
1240            {
1241                exclusive = 1;
1242            }
1243
1244            statement = connection.prepareStatement
1245                ("delete from locks where id=?");
1246            statement.setString(1, lock.getLockId());
1247            statement.execute();
1248            closeStatement(statement);
1249
1250            statement = connection.prepareStatement
1251                ("insert into locks values(?,?,?,?,?,?,?)");
1252            statement.setString(1, lock.getLockId());
1253            statement.setString(2, lock.getObjectUri());
1254            statement.setString(3, lock.getSubjectUri());
1255            statement.setString(4, lock.getTypeUri());
1256            statement.setString
1257                (5, String.valueOf(lock.getExpirationDate().getTime()));
1258            statement.setInt(6, inheritable);
1259            statement.setInt(7, exclusive);
1260            statement.execute();
1261
1262        }
1263        catch (SQLException e)
1264        {
1265            getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
1266            throw new ServiceAccessException(this, e);
1267        }
1268        finally
1269        {
1270            closeStatement(statement);
1271        }
1272    }
1273
1274
1275    /**
1276     * Unlock.
1277     *
1278     * @param lock Token to remove
1279     * @exception ServiceAccessException Service access error
1280     * @exception LockTokenNotFoundException Lock token was not found
1281     */

1282    public void removeLock(Uri uri, NodeLock lock)
1283        throws ServiceAccessException, LockTokenNotFoundException
1284    {
1285
1286        Statement statement = null;
1287
1288        try
1289        {
1290
1291            statement = connection.createStatement();
1292
1293            int inheritable = 0;
1294            if (lock.isInheritable())
1295            {
1296                inheritable = 1;
1297            }
1298
1299            String JavaDoc s = null;
1300
1301            s = "delete from locks where id='" + lock.getLockId() + "'";
1302            statement.execute(s);
1303
1304        }
1305        catch (SQLException e)
1306        {
1307            getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
1308            throw new ServiceAccessException(this, e);
1309        }
1310        finally
1311        {
1312            closeStatement(statement);
1313        }
1314    }
1315
1316
1317    /**
1318     * Kill a lock.
1319     *
1320     * @param lock Token to remove
1321     * @exception ServiceAccessException Service access error
1322     * @exception LockTokenNotFoundException Lock token was not found
1323     */

1324    public void killLock(Uri uri, NodeLock lock)
1325        throws ServiceAccessException, LockTokenNotFoundException
1326    {
1327        removeLock(uri, lock);
1328    }
1329
1330
1331    /**
1332     * Enumerate locks on an object.
1333     *
1334     * @param subject Subject
1335     * @return Enumeration List of locks which have been put on the subject
1336     * @exception ServiceAccessException Service access error
1337     */

1338    public Enumeration JavaDoc enumerateLocks(Uri uri)
1339        throws ServiceAccessException
1340    {
1341
1342        Vector JavaDoc lockVector = new Vector JavaDoc();
1343        PreparedStatement statement = null;
1344
1345        try
1346        {
1347            statement = connection.prepareStatement
1348                ("select * from locks where object= ?");
1349            statement.setString(1, uri.toString());
1350            statement.execute();
1351            ResultSet res = statement.getResultSet();
1352
1353            while (res.next())
1354            {
1355                Date JavaDoc expirationDate = null;
1356                try
1357                {
1358                    Long JavaDoc timeValue = new Long JavaDoc(res.getString
1359                                              (LOCKS_EXPIRATIONDATE));
1360                    expirationDate = new Date JavaDoc(timeValue.longValue());
1361                }
1362                catch (NumberFormatException JavaDoc e)
1363                {
1364                    expirationDate = new Date JavaDoc();
1365                }
1366                NodeLock lock =
1367                    new NodeLock(res.getString(LOCKS_ID),
1368                                 res.getString(LOCKS_OBJECT),
1369                                 res.getString(LOCKS_SUBJECT),
1370                                 res.getString(LOCKS_TYPE),
1371                                 expirationDate,
1372                                 (res.getInt(LOCKS_INHERITABLE) == 1),
1373                                 (res.getInt(LOCKS_EXCLUSIVE) == 1));
1374                lockVector.addElement(lock);
1375            }
1376        }
1377        catch (SQLException e)
1378        {
1379            getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
1380            throw new ServiceAccessException(this, e);
1381        }
1382        finally
1383        {
1384            closeStatement(statement);
1385        }
1386
1387        return lockVector.elements();
1388    }
1389
1390
1391    /**
1392     * Retrieve the revisions informations of an object.
1393     *
1394     * @param uri Uri
1395     * @exception ServiceAccessException Service access error
1396     * @exception RevisionDescriptorNotFoundException Revision descriptor
1397     * was not found
1398     */

1399    public NodeRevisionDescriptors retrieveRevisionDescriptors(Uri uri)
1400        throws ServiceAccessException, RevisionDescriptorNotFoundException
1401    {
1402
1403        NodeRevisionDescriptors revisionDescriptors = null;
1404        PreparedStatement statement = null;
1405        PreparedStatement statement2 = null;
1406
1407        try
1408        {
1409            ResultSet res = null;
1410
1411            NodeRevisionNumber initialRevision = new NodeRevisionNumber();
1412            Hashtable JavaDoc workingRevisions = new Hashtable JavaDoc();
1413            Hashtable JavaDoc latestRevisionNumbers = new Hashtable JavaDoc();
1414            Hashtable JavaDoc branches = new Hashtable JavaDoc();
1415            boolean isVersioned = false;
1416
1417            statement = connection.prepareStatement
1418                ("select * from revisions where uri= ?");
1419            statement.setString(1, uri.toString());
1420            res = statement.executeQuery();
1421
1422            if (res.next())
1423            {
1424                int isVersionedInt = res.getInt(REVISIONS_ISVERSIONED);
1425                if (isVersionedInt == 1)
1426                {
1427                    isVersioned = true;
1428                }
1429            }
1430            else
1431            {
1432                throw new RevisionDescriptorNotFoundException(uri.toString());
1433            }
1434
1435            closeStatement(statement);
1436
1437            statement = connection.prepareStatement
1438                ("select * from workingrevision where uri= ?");
1439            statement.setString(1, uri.toString());
1440            res = statement.executeQuery();
1441
1442            while(res.next())
1443            {
1444                // TODO : Parse each working revision definition
1445
}
1446
1447            closeStatement(statement);
1448
1449            statement = connection.prepareStatement
1450                ("select * from latestrevisions where uri=?");
1451            statement.setString(1, uri.toString());
1452            res = statement.executeQuery();
1453
1454            while(res.next())
1455            {
1456                latestRevisionNumbers
1457                    .put(res.getString(LATESTREVISIONS_BRANCHNAME),
1458                         new NodeRevisionNumber
1459                             (res.getString(LATESTREVISIONS_NUMBER)));
1460            }
1461            closeStatement(statement);
1462
1463            statement = connection.prepareStatement
1464                ("select * from revision where uri= ?");
1465            statement.setString(1, uri.toString());
1466            res = statement.executeQuery();
1467
1468            while(res.next())
1469            {
1470                String JavaDoc currentRevisionNumber = res.getString(REVISION_NUMBER);
1471
1472                // We parse the revision list of the object
1473
if (statement2 == null)
1474                {
1475                    statement2 = connection.prepareStatement
1476                        ("select * from branches where uri = ? and xnumber = ?");
1477                }
1478                statement2.setString(1, uri.toString());
1479                statement2.setString(2, currentRevisionNumber);
1480                ResultSet res2 = statement2.executeQuery();
1481                Vector JavaDoc childList = new Vector JavaDoc();
1482
1483                while (res2.next())
1484                {
1485                    childList.addElement(new NodeRevisionNumber
1486                        (res2.getString(BRANCHES_CHILDNUMBER)));
1487                }
1488
1489                branches.put(new NodeRevisionNumber(currentRevisionNumber),
1490                             childList);
1491
1492                res2.close();
1493            }
1494            closeStatement(statement2);
1495
1496            revisionDescriptors = new NodeRevisionDescriptors
1497                (uri.toString(), initialRevision, workingRevisions,
1498                 latestRevisionNumbers, branches, isVersioned);
1499
1500        }
1501        catch (SQLException e)
1502        {
1503            getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
1504            throw new ServiceAccessException(this, e);
1505        }
1506        finally
1507        {
1508            closeStatement(statement);
1509            closeStatement(statement2);
1510        }
1511        return revisionDescriptors;
1512    }
1513
1514
1515    /**
1516     * Create a new revision information object.
1517     *
1518     * @param uri Uri
1519     * @param revisionDescriptors Node revision descriptors
1520     * @exception ServiceAccessException Service access error
1521     */

1522    public void createRevisionDescriptors
1523        (Uri uri, NodeRevisionDescriptors revisionDescriptors)
1524        throws ServiceAccessException
1525    {
1526
1527        // TODO : Here, we have the option of "cleaning up" before
1528
// creating the new records in the database.
1529

1530        PreparedStatement statement = null;
1531
1532        try
1533        {
1534            ResultSet res = null;
1535
1536            // Creating record in revisions tables
1537

1538            int isVersioned = 0;
1539            if (revisionDescriptors.isVersioned())
1540            {
1541                isVersioned = 1;
1542            }
1543
1544            statement = connection.prepareStatement
1545                ("insert into revisions values(?,?,?)");
1546            statement.setString(1,uri.toString());
1547            statement.setInt(2, isVersioned);
1548            statement.setString
1549                (3, revisionDescriptors.getInitialRevision().toString());
1550            statement.execute();
1551            closeStatement(statement);
1552
1553            // Creating records in working revisions table
1554
// ... TODO (working revisions are not used for now)
1555

1556            // Creating records in latest revisions table
1557

1558            // For now, only the latest revision from the main branch is stored
1559
if (revisionDescriptors.getLatestRevision() != null)
1560            {
1561                statement = connection.prepareStatement
1562                    ("insert into latestrevisions values(?,?,?)");
1563                statement.setString(1, uri.toString());
1564                statement.setString
1565                    (2, NodeRevisionDescriptors.MAIN_BRANCH.toString());
1566                statement.setString
1567                    (3, revisionDescriptors.getLatestRevision().toString());
1568                statement.execute();
1569                closeStatement(statement);
1570            }
1571
1572            // Creating records in the branches table
1573
// TODO
1574

1575        }
1576        catch (SQLException e)
1577        {
1578            getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
1579            throw new ServiceAccessException(this, e);
1580        }
1581        finally
1582        {
1583            closeStatement(statement);
1584        }
1585
1586    }
1587
1588
1589    /**
1590     * Update revision information.
1591     *
1592     * @param uri Uri
1593     * @param revisionDescriptors Node revision descriptors
1594     * @exception ServiceAccessException Service access error
1595     * @exception RevisionDescriptorNotFoundException Revision descriptor
1596     * was not found
1597     */

1598    public void storeRevisionDescriptors
1599        (Uri uri, NodeRevisionDescriptors revisionDescriptors)
1600        throws ServiceAccessException, RevisionDescriptorNotFoundException
1601    {
1602        removeRevisionDescriptors(uri);
1603        createRevisionDescriptors(uri, revisionDescriptors);
1604    }
1605
1606
1607    /**
1608     * Remove revision information.
1609     *
1610     * @param uri Uri
1611     * @exception ServiceAccessException Service access error
1612     */

1613    public void removeRevisionDescriptors(Uri uri)
1614        throws ServiceAccessException
1615    {
1616
1617        PreparedStatement statement = null;
1618
1619        try
1620        {
1621            statement = connection.prepareStatement
1622                ("delete from revisions where uri= ?");
1623            statement.setString(1, uri.toString());
1624            statement.execute();
1625            closeStatement(statement);
1626
1627            statement = connection.prepareStatement
1628                ("delete from workingrevision where uri= ?");
1629            statement.setString(1, uri.toString());
1630            statement.execute();
1631            closeStatement(statement);
1632
1633            statement = connection.prepareStatement
1634                ("delete from latestrevisions where uri= ?");
1635            statement.setString(1, uri.toString());
1636            statement.execute();
1637            closeStatement(statement);
1638
1639            statement = connection.prepareStatement
1640                ("delete from branches where uri= ?");
1641            statement.setString(1, uri.toString());
1642            statement.execute();
1643            closeStatement(statement);
1644        }
1645        catch (SQLException e)
1646        {
1647            getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
1648            throw new ServiceAccessException(this, e);
1649        }
1650        finally
1651        {
1652            closeStatement(statement);
1653        }
1654    }
1655
1656
1657    /**
1658     * Retrieve an individual object's revision descriptor.
1659     *
1660     * @param Uri uri
1661     * @param revisionNumber Node revision number
1662     */

1663    public NodeRevisionDescriptor retrieveRevisionDescriptor
1664        (Uri uri, NodeRevisionNumber revisionNumber)
1665        throws ServiceAccessException, RevisionDescriptorNotFoundException
1666    {
1667
1668        NodeRevisionDescriptor revisionDescriptor = null;
1669        PreparedStatement statement = null;
1670
1671        if(revisionNumber == null)
1672        {
1673            throw new RevisionDescriptorNotFoundException(uri.toString());
1674        }
1675
1676        try
1677        {
1678            ResultSet res = null;
1679
1680            String JavaDoc branchName = null;
1681            Vector JavaDoc labels = new Vector JavaDoc();
1682            Hashtable JavaDoc properties = new Hashtable JavaDoc();
1683
1684            // Retrieving branch name (and also check that revision
1685
// does indeed exist)
1686

1687            statement = connection.prepareStatement
1688                ("select * from revision where uri= ? and xnumber = ?");
1689            statement.setString(1, uri.toString());
1690            statement.setString(2, revisionNumber.toString());
1691            res = statement.executeQuery();
1692
1693            if (res.next())
1694            {
1695                branchName = res.getString(REVISION_BRANCHNAME);
1696            }
1697            else
1698            {
1699                throw new RevisionDescriptorNotFoundException(uri.toString());
1700            }
1701
1702            closeStatement(statement);
1703
1704            // Retrieve labels
1705

1706            statement = connection.prepareStatement
1707                ("select * from label where uri= ? and xnumber = ?");
1708            statement.setString(1, uri.toString());
1709            statement.setString(2, revisionNumber.toString());
1710            res = statement.executeQuery();
1711
1712            while (res.next())
1713            {
1714                labels.addElement(res.getString(LABEL_LABEL));
1715            }
1716
1717            closeStatement(statement);
1718
1719            // Retrieve properties
1720

1721            statement = connection.prepareStatement
1722                ("select * from property where uri= ? and xnumber = ?");
1723            statement.setString(1, uri.toString());
1724            statement.setString(2, revisionNumber.toString());
1725            res = statement.executeQuery();
1726
1727            while (res.next())
1728            {
1729                String JavaDoc propertyName = res.getString(PROPERTY_NAME);
1730                String JavaDoc propertyNamespace = res.getString(PROPERTY_NAMESPACE);
1731                NodeProperty property =
1732                    new NodeProperty(propertyName,
1733                                     res.getString(PROPERTY_VALUE),
1734                                     propertyNamespace,
1735                                     res.getString(PROPERTY_TYPE),
1736                                     (res.getInt(PROPERTY_PROTECTED) == 1));
1737                properties.put(propertyNamespace + propertyName, property);
1738            }
1739
1740            revisionDescriptor =
1741                new NodeRevisionDescriptor(revisionNumber, branchName,
1742                                           labels, properties);
1743        }
1744        catch (SQLException e)
1745        {
1746            getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
1747            throw new ServiceAccessException(this, e);
1748        }
1749        finally
1750        {
1751            closeStatement(statement);
1752        }
1753
1754        return revisionDescriptor;
1755    }
1756
1757
1758    /**
1759     * Create a new revision descriptor.
1760     *
1761     * @param uri Uri
1762     * @param revisionDescriptor Node revision descriptor
1763     * @exception ServiceAccessException Service access error
1764     */

1765    public void createRevisionDescriptor
1766        (Uri uri, NodeRevisionDescriptor revisionDescriptor)
1767        throws ServiceAccessException
1768    {
1769
1770        PreparedStatement statement = null;
1771
1772        try
1773        {
1774
1775            ResultSet res = null;
1776
1777            statement = connection.prepareStatement
1778                ("insert into revision values(?, ?, ?)");
1779            statement.setString(1, uri.toString());
1780            statement.setString
1781                (2, revisionDescriptor.getRevisionNumber().toString());
1782            statement.setString(3, revisionDescriptor.getBranchName());
1783            statement.execute();
1784            closeStatement(statement);
1785
1786            // Creating revision labels
1787
statement = null;
1788            Enumeration JavaDoc labels = revisionDescriptor.enumerateLabels();
1789            while (labels.hasMoreElements())
1790            {
1791                if (statement == null)
1792                {
1793                    statement = connection.prepareStatement
1794                        ("insert into label values(?,?,?)");
1795                }
1796                statement.setString(1, uri.toString());
1797                statement.setString
1798                    (2, revisionDescriptor.getRevisionNumber().toString());
1799                statement.setString(3, (String JavaDoc)labels.nextElement());
1800                statement.execute();
1801            }
1802            closeStatement(statement);
1803
1804            // Creating associated properties
1805
statement = null;
1806            Enumeration JavaDoc properties = revisionDescriptor.enumerateProperties();
1807            while (properties.hasMoreElements())
1808            {
1809                NodeProperty property =
1810                    (NodeProperty) properties.nextElement();
1811                int protectedProperty = 0;
1812                if (property.isProtected())
1813                {
1814                    protectedProperty = 1;
1815                }
1816                if (statement == null)
1817                {
1818                    statement = connection.prepareStatement
1819                        ("insert into property values(?,?,?,?,?,?,?)");
1820                }
1821                statement.setString(1, uri.toString());
1822                statement.setString
1823                    (2, revisionDescriptor.getRevisionNumber().toString());
1824                statement.setString(3, property.getName());
1825                statement.setString(4, property.getValue().toString());
1826                statement.setString(5, property.getNamespace());
1827                statement.setString(6, property.getType());
1828                statement.setInt(7, protectedProperty);
1829                statement.execute();
1830            }
1831            closeStatement(statement);
1832
1833        }
1834        catch (SQLException e)
1835        {
1836            getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
1837            throw new ServiceAccessException(this, e);
1838        }
1839        finally
1840        {
1841            closeStatement(statement);
1842        }
1843    }
1844
1845
1846    /**
1847     * Update a revision descriptor.
1848     *
1849     * @param uri Uri
1850     * @param revisionDescriptors Node revision descriptor
1851     * @exception ServiceAccessException Service access error
1852     * @exception RevisionDescriptorNotFoundException Revision descriptor
1853     * was not found
1854     */

1855    public void storeRevisionDescriptor
1856        (Uri uri, NodeRevisionDescriptor revisionDescriptor)
1857        throws ServiceAccessException, RevisionDescriptorNotFoundException
1858    {
1859        removeRevisionDescriptor(uri, revisionDescriptor.getRevisionNumber());
1860        createRevisionDescriptor(uri, revisionDescriptor);
1861    }
1862
1863
1864    /**
1865     * Remove a revision descriptor.
1866     *
1867     * @param uri Uri
1868     * @param revisionNumber Revision number
1869     * @exception ServiceAccessException Service access error
1870     */

1871    public void removeRevisionDescriptor(Uri uri, NodeRevisionNumber number)
1872        throws ServiceAccessException
1873    {
1874
1875        PreparedStatement statement = null;
1876
1877        try
1878        {
1879            statement = connection.prepareStatement
1880                ("delete from revision where uri= ? and xnumber = ?");
1881            statement.setString(1, uri.toString());
1882            statement.setString(2, number.toString());
1883            statement.execute();
1884            closeStatement(statement);
1885
1886            // Removing revision labels
1887

1888            statement = connection.prepareStatement
1889                ("delete from label where uri= ? and xnumber = ?");
1890            statement.setString(1, uri.toString());
1891            statement.setString(2, number.toString());
1892            statement.execute();
1893            closeStatement(statement);
1894
1895            // Removing associated properties
1896

1897            statement = connection.prepareStatement
1898                ("delete from property where uri= ? and xnumber = ?");
1899            statement.setString(1, uri.toString());
1900            statement.setString(2, number.toString());
1901            statement.execute();
1902
1903        }
1904        catch (SQLException e)
1905        {
1906            getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
1907            throw new ServiceAccessException(this, e);
1908        }
1909        finally
1910        {
1911            closeStatement(statement);
1912        }
1913
1914    }
1915
1916
1917    // ------------------------------------------------------ Protected Methods
1918

1919
1920    /**
1921     * Close specified statement.
1922     */

1923    protected void closeStatement(Statement statement)
1924    {
1925        if (statement != null)
1926        {
1927            try
1928            {
1929                statement.close();
1930            }
1931            catch (SQLException e)
1932            {
1933            }
1934        }
1935    }
1936}
1937
Popular Tags