KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > store > impl > rdbms > OldJDBCAdapter


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/stores/org/apache/slide/store/impl/rdbms/OldJDBCAdapter.java,v 1.4 2004/07/28 09:34:17 ib Exp $
3  * $Revision: 1.4 $
4  * $Date: 2004/07/28 09:34:17 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.store.impl.rdbms;
25
26 import java.lang.reflect.Constructor JavaDoc;
27 import java.sql.Connection JavaDoc;
28 import java.sql.PreparedStatement JavaDoc;
29 import java.sql.ResultSet JavaDoc;
30 import java.sql.SQLException JavaDoc;
31 import java.sql.Statement JavaDoc;
32 import java.util.Date JavaDoc;
33 import java.util.Enumeration JavaDoc;
34 import java.util.Hashtable JavaDoc;
35 import java.util.Vector JavaDoc;
36
37 import org.apache.slide.common.Service;
38 import org.apache.slide.common.ServiceAccessException;
39 import org.apache.slide.common.Uri;
40 import org.apache.slide.content.NodeProperty;
41 import org.apache.slide.content.NodeRevisionContent;
42 import org.apache.slide.content.NodeRevisionDescriptor;
43 import org.apache.slide.content.NodeRevisionDescriptors;
44 import org.apache.slide.content.NodeRevisionNumber;
45 import org.apache.slide.content.RevisionAlreadyExistException;
46 import org.apache.slide.content.RevisionDescriptorNotFoundException;
47 import org.apache.slide.content.RevisionNotFoundException;
48 import org.apache.slide.lock.LockTokenNotFoundException;
49 import org.apache.slide.lock.NodeLock;
50 import org.apache.slide.security.NodePermission;
51 import org.apache.slide.structure.LinkNode;
52 import org.apache.slide.structure.ObjectAlreadyExistsException;
53 import org.apache.slide.structure.ObjectNode;
54 import org.apache.slide.structure.ObjectNotFoundException;
55 import org.apache.slide.util.logger.Logger;
56
57 /**
58 * A database adapter, which is using almost the same SQL scheme as the
59 * deprecated slidestore.reference.JDBCDescriptorsStore. This adapter this
60 * meant for backward compatibility with Slide 1.x. Don't use this adapter
61 * for new projects.
62 * <strong>The content store methods are not supported.</strong>
63 *
64 * This adapter requires a minor change of table locks. It should look like
65 * this (Postgres dialect, adapt it for your database):
66 * <pre>
67 * create table locks(id varchar(4000), object varchar(4000),
68 * subject varchar(4000), type varchar(4000),
69 * expirationdate varchar(15), inheritable int,
70 * xexclusive int, ownerinfo varchar(255));
71 * </pre>
72 *
73 * You may overide the method {@link #createException} for
74 * better error messages in case of parallel access.
75 *
76 * Oliver Zeigermann.
77 * @version $Revision: 1.4 $ $Date: 2004/07/28 09:34:17 $
78 *
79 */

80 public class OldJDBCAdapter extends AbstractRDBMSAdapter {
81
82     protected static final String JavaDoc LOG_CHANNEL =
83         StandardRDBMSAdapter.class.getName();
84     // -------------------------------------------------------------- Constants
85

86     // Column numbers
87

88     // Structure descriptors
89

90     protected static final int OBJECTS_URI = 1;
91     protected static final int OBJECTS_CLASS = 2;
92
93     protected static final int CHILDREN_URI = 1;
94     protected static final int CHILDREN_CHILDURI = 2;
95
96     protected static final int LINKS_LINK = 1;
97     protected static final int LINKS_LINKTO = 2;
98
99     // Security descriptors
100

101     protected static final int PERMISSIONS_OBJECT = 1;
102     protected static final int PERMISSIONS_REVISION_NUMBER = 2;
103     protected static final int PERMISSIONS_SUBJECT = 3;
104     protected static final int PERMISSIONS_ACTION = 4;
105     protected static final int PERMISSIONS_INHERITABLE = 5;
106     protected static final int PERMISSIONS_NEGATIVE = 6;
107
108     // Lock descriptors
109

110     protected static final int LOCKS_ID = 1;
111     protected static final int LOCKS_OBJECT = 2;
112     protected static final int LOCKS_SUBJECT = 3;
113     protected static final int LOCKS_TYPE = 4;
114     protected static final int LOCKS_EXPIRATIONDATE = 5;
115     protected static final int LOCKS_INHERITABLE = 6;
116     protected static final int LOCKS_EXCLUSIVE = 7;
117     protected static final int LOCKS_OWNER = 8;
118
119     // Content descriptors
120

121     protected static final int REVISIONS_URI = 1;
122     protected static final int REVISIONS_ISVERSIONED = 2;
123     protected static final int REVISIONS_INITIALREVISION = 3;
124
125     protected static final int WORKINGREVISION_URI = 1;
126     protected static final int WORKINGREVISION_BASEREVISION = 2;
127     protected static final int WORKINGREVISION_NUMBER = 3;
128
129     protected static final int LATESTREVISIONS_URI = 1;
130     protected static final int LATESTREVISIONS_BRANCHNAME = 2;
131     protected static final int LATESTREVISIONS_NUMBER = 3;
132
133     protected static final int BRANCHES_URI = 1;
134     protected static final int BRANCHES_NUMBER = 2;
135     protected static final int BRANCHES_CHILDNUMBER = 3;
136
137     protected static final int REVISION_URI = 1;
138     protected static final int REVISION_NUMBER = 2;
139     protected static final int REVISION_BRANCHNAME = 3;
140
141     protected static final int LABEL_URI = 1;
142     protected static final int LABEL_NUMBER = 2;
143     protected static final int LABEL_LABEL = 3;
144
145     protected static final int PROPERTY_URI = 1;
146     protected static final int PROPERTY_NUMBER = 2;
147     protected static final int PROPERTY_NAME = 3;
148     protected static final int PROPERTY_VALUE = 4;
149     protected static final int PROPERTY_NAMESPACE = 5;
150     protected static final int PROPERTY_TYPE = 6;
151     protected static final int PROPERTY_PROTECTED = 7;
152
153     public OldJDBCAdapter(Service service, Logger logger) {
154         super(service, logger);
155     }
156
157     // ----------------------------------------------- DescriptorsStore Methods
158

159     /**
160      * Retrive an object.
161      *
162      * @param uri Uri of the object we want to retrieve
163      * @exception ServiceAccessException Error accessing the Service
164      * @exception ObjectNotFoundException The object to retrieve was not found
165      */

166     public ObjectNode retrieveObject(Connection JavaDoc connection, Uri uri)
167         throws ServiceAccessException, ObjectNotFoundException {
168
169         ObjectNode result = null;
170         PreparedStatement JavaDoc statement = null;
171
172         try {
173
174             statement =
175                 connection.prepareStatement(
176                     "select * from objects where uri= ?");
177             statement.setString(1, uri.toString());
178
179             ResultSet JavaDoc res = statement.executeQuery();
180
181             // Parsing result set
182

183             String JavaDoc className;
184
185             if (res.next()) {
186                 // Retrieving and loading the object
187
className = res.getString(OBJECTS_CLASS);
188             } else {
189                 // Object was not found ...
190
throw new ObjectNotFoundException(uri);
191             }
192
193             closeStatement(statement);
194
195             // Then, retrieve the children
196
statement =
197                 connection.prepareStatement(
198                     "select * from children where uri= ?");
199             statement.setString(1, uri.toString());
200             res = statement.executeQuery();
201
202             Vector JavaDoc childrenVector = new Vector JavaDoc();
203
204             // Parse result set
205
while (res.next()) {
206                 // Load each permission
207
childrenVector.addElement(res.getString(CHILDREN_CHILDURI));
208             }
209             closeStatement(statement);
210
211             statement =
212                 connection.prepareStatement(
213                     "select * from links where linkto= ?");
214             statement.setString(1, uri.toString());
215             res = statement.executeQuery();
216
217             Vector JavaDoc linksVector = new Vector JavaDoc();
218
219             // Parse result set
220
while (res.next()) {
221                 // Load each permission
222
linksVector.addElement(res.getString(LINKS_LINKTO));
223             }
224
225             closeStatement(statement);
226
227             if (className.equals("org.apache.slide.structure.LinkNode")) {
228
229                 String JavaDoc linkTo = new String JavaDoc();
230                 statement =
231                     connection.prepareStatement(
232                         "select * from links where link= ?");
233                 statement.setString(1, uri.toString());
234                 res = statement.executeQuery();
235
236                 if (res.next())
237                     linkTo = res.getString(LINKS_LINKTO);
238
239                 closeStatement(statement);
240
241                 result =
242                     new LinkNode(
243                         uri.toString(),
244                         childrenVector,
245                         linksVector,
246                         linkTo);
247
248             } else {
249
250                 try {
251                     Class JavaDoc objclass = Class.forName(className);
252
253                     Class JavaDoc[] argClasses =
254                         {
255                             Class.forName("java.lang.String"),
256                             Class.forName("java.util.Vector"),
257                             Class.forName("java.util.Vector")};
258                     Object JavaDoc[] arguments =
259                         { uri.toString(), childrenVector, linksVector };
260
261                     Constructor JavaDoc constructor =
262                         objclass.getConstructor(argClasses);
263                     result = (ObjectNode) constructor.newInstance(arguments);
264                 } catch (Exception JavaDoc e) {
265                     // ClassNotFoundException, NoSuchMethodException, etc.
266
throw createException(e, uri);
267                 }
268
269             }
270
271         } catch (SQLException JavaDoc e) {
272             throw createException(e, uri);
273         } finally {
274             closeStatement(statement);
275         }
276         return result;
277     }
278
279     /**
280      * Update an object.
281      *
282      * @param object Object to update
283      * @exception ServiceAccessException Error accessing the Service
284      * @exception ObjectNotFoundException The object to update was not found
285      */

286     public void storeObject(Connection JavaDoc connection, Uri uri, ObjectNode object)
287         throws ServiceAccessException, ObjectNotFoundException {
288
289         PreparedStatement JavaDoc statement = null;
290
291         try {
292             statement =
293                 connection.prepareStatement(
294                     "select * from objects where uri= ?");
295             statement.setString(1, uri.toString());
296
297             ResultSet JavaDoc res = statement.executeQuery();
298
299             // Parsing result set
300

301             if (!res.next()) {
302                 throw new ObjectNotFoundException(uri);
303             }
304
305             closeStatement(statement);
306
307             // Updating children
308
statement =
309                 connection.prepareStatement(
310                     "delete from children where uri= ?");
311             statement.setString(1, object.getUri());
312             statement.execute();
313             closeStatement(statement);
314
315             statement = null;
316             Enumeration JavaDoc children = object.enumerateChildren();
317             while (children.hasMoreElements()) {
318                 if (statement == null) {
319                     statement =
320                         connection.prepareStatement(
321                             "insert into children values(?, ?)");
322                 }
323                 statement.setString(1, object.getUri());
324                 statement.setString(2, (String JavaDoc) children.nextElement());
325                 statement.execute();
326             }
327             closeStatement(statement);
328
329             // Updating inbound links
330
/*
331             s = "delete from links where linkto='" + object.getUri() + "'";
332             statement.execute(s);
333             Enumeration links = object.enumerateLinks();
334             while (children.hasMoreElements()) {
335                 s = "insert into links values('"
336                     + (String) links.nextElement() + "', '"
337                     + object.getUri() + "')";
338                 statement.execute(s);
339             }
340             */

341
342             // Updating links
343
statement =
344                 connection.prepareStatement("delete from links where link= ?");
345             statement.setString(1, object.getUri());
346             statement.execute();
347             closeStatement(statement);
348
349             if (object instanceof LinkNode) {
350                 statement =
351                     connection.prepareStatement(
352                         "insert into links values(?,?)");
353                 statement.setString(1, object.getUri());
354                 statement.setString(2, ((LinkNode) object).getLinkedUri());
355                 statement.execute();
356                 closeStatement(statement);
357             }
358
359         } catch (SQLException JavaDoc e) {
360             throw createException(e, uri);
361         } finally {
362             closeStatement(statement);
363         }
364
365     }
366
367     /**
368       * Create a new object.
369       *
370       * @param object ObjectNode
371        * @param uri Uri of the object we want to create
372        * @exception ServiceAccessException Error accessing the Service
373        * @exception ObjectAlreadyExistsException An object already exists
374        * at this Uri
375        */

376     public void createObject(Connection JavaDoc connection, Uri uri, ObjectNode object)
377         throws ServiceAccessException, ObjectAlreadyExistsException {
378
379         PreparedStatement JavaDoc statement = null;
380         try {
381
382             String JavaDoc className = object.getClass().getName();
383             statement =
384                 connection.prepareStatement(
385                     "select * from objects where uri= ?");
386             statement.setString(1, uri.toString());
387             ResultSet JavaDoc res = statement.executeQuery();
388             // Parsing result set
389
if (res.next()) {
390                 throw new ObjectAlreadyExistsException(uri.toString());
391             }
392
393             closeStatement(statement);
394             statement =
395                 connection.prepareStatement("insert into objects values(?,?)");
396             statement.setString(1, uri.toString());
397             statement.setString(2, className);
398             statement.execute();
399             closeStatement(statement);
400             statement = null;
401             // Inserting children
402
Enumeration JavaDoc children = object.enumerateChildren();
403             while (children.hasMoreElements()) {
404                 if (statement == null) {
405                     statement =
406                         connection.prepareStatement(
407                             "insert into children values(?,?)");
408                 }
409                 statement.setString(1, uri.toString());
410                 statement.setString(2, (String JavaDoc) children.nextElement());
411                 statement.execute();
412             }
413             closeStatement(statement);
414             // Updating inbound links
415
/*
416             Enumeration links = object.enumerateLinks();
417             while (children.hasMoreElements()) {
418                 s = "insert into links values('"
419                     + (String) links.nextElement() + "', '"
420                     + object.getUri() + "')";
421                 statement.execute(s);
422             }
423             */
// If the object is a link, also store the link information
424
if (object instanceof LinkNode) {
425                 statement =
426                     connection.prepareStatement(
427                         "insert into links values(?,?)");
428                 statement.setString(1, uri.toString());
429                 statement.setString(2, ((LinkNode) object).getLinkedUri());
430                 statement.execute();
431                 closeStatement(statement);
432             }
433
434         } catch (SQLException JavaDoc e) {
435             throw createException(e, uri);
436         } finally {
437             closeStatement(statement);
438         }
439
440     }
441
442     /**
443       * Remove an object.
444       *
445       * @param object Object to remove
446       * @exception ServiceAccessException Error accessing the Service
447       * @exception ObjectNotFoundException The object to remove was not found
448       */

449     public void removeObject(Connection JavaDoc connection, Uri uri, ObjectNode object)
450         throws ServiceAccessException, ObjectNotFoundException {
451
452         PreparedStatement JavaDoc statement = null;
453
454         try {
455
456             // Removing object
457
statement =
458                 connection.prepareStatement("delete from objects where uri= ?");
459             statement.setString(1, object.getUri());
460             statement.execute();
461             closeStatement(statement);
462
463             // Removing children
464
statement =
465                 connection.prepareStatement("delete from children where uri=?");
466             statement.setString(1, object.getUri());
467             statement.execute();
468             closeStatement(statement);
469
470             // Removing inbound links
471
/*
472             s = "delete from links where linkto='" + object.getUri() + "'";
473             statement.execute(s);
474             */

475
476             // Removing links
477
statement =
478                 connection.prepareStatement("delete from links where link= ?");
479             statement.setString(1, object.getUri());
480             statement.execute();
481             closeStatement(statement);
482
483         } catch (SQLException JavaDoc e) {
484             throw createException(e, uri);
485         }
486     }
487     
488     /**
489      * Grant a new permission.
490      *
491      * @param permission Permission we want to create
492      * @exception ServiceAccessException Error accessing the Service
493      */

494     public void grantPermission(Connection JavaDoc connection, Uri uri, NodePermission permission)
495         throws ServiceAccessException {
496         
497         PreparedStatement JavaDoc statement = null;
498         
499         try {
500             int inheritable = 0;
501             if (permission.isInheritable()) {
502                 inheritable = 1;
503             }
504             
505             int negative = 0;
506             if (permission.isNegative()) {
507                 negative = 1;
508             }
509             
510             NodeRevisionNumber revisionNumber = permission.getRevisionNumber();
511             String JavaDoc revisionNumberStr =
512                 (revisionNumber == null) ? null : revisionNumber.toString();
513             
514             statement = connection.prepareStatement
515                 ("insert into permissions values(?,?,?,?,?,?)");
516             statement.setString(1, permission.getObjectUri());
517             statement.setString(2, revisionNumberStr);
518             statement.setString(3, permission.getSubjectUri());
519             statement.setString(4, permission.getActionUri());
520             statement.setInt(5, inheritable);
521             statement.setInt(6, negative);
522             statement.execute();
523         } catch (SQLException JavaDoc e) {
524             throw createException(e,uri);
525         } finally {
526             closeStatement(statement);
527         }
528         
529     }
530     
531     
532     /**
533      * Revoke a permission.
534      *
535      * @param permission Permission we want to create
536      * @exception ServiceAccessException Error accessing the Service
537      */

538     public void revokePermission(Connection JavaDoc connection,Uri uri, NodePermission permission)
539         throws ServiceAccessException {
540         
541         /* Warning changes to this method should also be done to CloudscapeDescriptorsStore */
542         
543         PreparedStatement JavaDoc statement = null;
544         
545         try {
546             NodeRevisionNumber revisionNumber = permission.getRevisionNumber();
547             if(revisionNumber != null) {
548                 statement = connection.prepareStatement
549                     ("delete from permissions where object= ? and subject = ? and action = ? and revisionnumber = ? ");
550                 statement.setString(4, revisionNumber.toString());
551             }
552             else {
553                 statement = connection.prepareStatement
554                     ("delete from permissions where object = ? and subject = ? and action = ? and revisionnumber is NULL");
555             }
556
557             statement.setString(1, permission.getObjectUri());
558             statement.setString(2, permission.getSubjectUri());
559             statement.setString(3, permission.getActionUri());
560         
561             statement.execute();
562         } catch (SQLException JavaDoc e) {
563             throw createException(e,uri);
564         } finally {
565             closeStatement(statement);
566         }
567         
568     }
569     
570     
571     /**
572      * Revoke all the permissions on an object.
573      *
574      * @param permission Permission we want to create
575      * @exception ServiceAccessException Error accessing the Service
576      */

577     public void revokePermissions(Connection JavaDoc connection,Uri uri)
578         throws ServiceAccessException {
579         
580         PreparedStatement JavaDoc statement = null;
581         
582         try {
583             
584             statement = connection.prepareStatement
585                 ("delete from permissions where object= ?");
586             statement.setString(1, uri.toString());
587             statement.execute();
588         } catch (SQLException JavaDoc e) {
589             throw createException(e,uri);
590         } finally {
591             closeStatement(statement);
592         }
593         
594     }
595     
596     
597     /**
598      * Enumerate permissions on an object.
599      *
600      * @param permission Permission we want to create
601      * @exception ServiceAccessException Error accessing the Service
602      */

603     public Enumeration JavaDoc enumeratePermissions(Connection JavaDoc connection,Uri uri)
604         throws ServiceAccessException {
605         
606         Vector JavaDoc permissionVector = new Vector JavaDoc();
607         PreparedStatement JavaDoc statement = null;
608         
609         try {
610             statement = connection.prepareStatement
611                 ("select * from permissions where object= ?");
612             statement.setString(1, uri.toString());
613             ResultSet JavaDoc res = statement.executeQuery();
614             
615             while (res.next()) {
616                 String JavaDoc object = res.getString(PERMISSIONS_OBJECT);
617                 String JavaDoc revision = res.getString(PERMISSIONS_REVISION_NUMBER);
618                 String JavaDoc subject = res.getString(PERMISSIONS_SUBJECT);
619                 String JavaDoc action = res.getString(PERMISSIONS_ACTION);
620  
621                 boolean inheritable = false;
622                 if (res.getInt(PERMISSIONS_INHERITABLE) == 1) {
623                     inheritable = true;
624                 }
625                 boolean negative = false;
626                 if (res.getInt(PERMISSIONS_NEGATIVE) == 1) {
627                     negative = true;
628                 }
629                 NodePermission permission =
630                     new NodePermission(object,revision,subject,
631                                        action,inheritable,negative);
632                 permissionVector.addElement(permission);
633             }
634             
635         } catch (SQLException JavaDoc e) {
636             throw createException(e,uri);
637         } finally {
638             closeStatement(statement);
639         }
640         
641         return permissionVector.elements();
642     }
643     
644     /**
645      * Create a new lock.
646      *
647      * @param lock Lock token
648      * @exception ServiceAccessException Service access error
649      */

650     public void putLock(Connection JavaDoc connection,Uri uri, NodeLock lock)
651         throws ServiceAccessException {
652         
653         PreparedStatement JavaDoc statement = null;
654         
655         try {
656             int inheritable = 0;
657             if (lock.isInheritable()) {
658                 inheritable = 1;
659             }
660             
661             int exclusive = 0;
662             if (lock.isExclusive()) {
663                 exclusive = 1;
664             }
665             
666             statement = connection.prepareStatement
667                 ("insert into locks values(?,?,?,?,?,?,?,?)");
668             statement.setString(1, lock.getLockId());
669             statement.setString(2, lock.getObjectUri());
670             statement.setString(3, lock.getSubjectUri());
671             statement.setString(4, lock.getTypeUri());
672             statement.setString
673                 (5, String.valueOf(lock.getExpirationDate().getTime()));
674             statement.setInt(6,inheritable);
675             statement.setInt(7, exclusive);
676             statement.setString(8,lock.getOwnerInfo());
677             statement.execute();
678         } catch (SQLException JavaDoc e) {
679             throw createException(e,uri);
680         } finally {
681             closeStatement(statement);
682         }
683         
684     }
685     
686     
687     /**
688      * Renew a lock.
689      *
690      * @param lock Token to renew
691      * @exception ServiceAccessException Service access error
692      * @exception LockTokenNotFoundException Lock token was not found
693      */

694     public void renewLock(Connection JavaDoc connection,Uri uri, NodeLock lock)
695         throws ServiceAccessException, LockTokenNotFoundException {
696         
697         PreparedStatement JavaDoc statement = null;
698         
699         try {
700             
701             int inheritable = 0;
702             if (lock.isInheritable()) {
703                 inheritable = 1;
704             }
705             
706             int exclusive = 0;
707             if (lock.isExclusive()) {
708                 exclusive = 1;
709             }
710             
711             statement = connection.prepareStatement
712                 ("delete from locks where id=?");
713             statement.setString(1, lock.getLockId());
714             statement.execute();
715             closeStatement(statement);
716             
717             statement = connection.prepareStatement
718                 ("insert into locks values(?,?,?,?,?,?,?,?)");
719             statement.setString(1, lock.getLockId());
720             statement.setString(2, lock.getObjectUri());
721             statement.setString(3, lock.getSubjectUri());
722             statement.setString(4, lock.getTypeUri());
723             statement.setString
724                 (5, String.valueOf(lock.getExpirationDate().getTime()));
725             statement.setInt(6, inheritable);
726             statement.setInt(7, exclusive);
727             statement.setString(8,lock.getOwnerInfo());
728             statement.execute();
729             
730         } catch (SQLException JavaDoc e) {
731             throw createException(e,uri);
732         } finally {
733             closeStatement(statement);
734         }
735         
736     }
737     
738     
739     /**
740      * Unlock.
741      *
742      * @param lock Token to remove
743      * @exception ServiceAccessException Service access error
744      * @exception LockTokenNotFoundException Lock token was not found
745      */

746     public void removeLock(Connection JavaDoc connection,Uri uri, NodeLock lock)
747         throws ServiceAccessException, LockTokenNotFoundException {
748         
749         Statement JavaDoc statement = null;
750         
751         try {
752             
753             statement = connection.createStatement();
754             
755             String JavaDoc s = null;
756             
757             s = "delete from locks where id='" + lock.getLockId() + "'";
758             statement.execute(s);
759             
760         } catch (SQLException JavaDoc e) {
761             throw createException(e,uri);
762         } finally {
763             closeStatement(statement);
764         }
765         
766     }
767     
768     
769     /**
770      * Kill a lock.
771      *
772      * @param lock Token to remove
773      * @exception ServiceAccessException Service access error
774      * @exception LockTokenNotFoundException Lock token was not found
775      */

776     public void killLock(Connection JavaDoc connection,Uri uri, NodeLock lock)
777         throws ServiceAccessException, LockTokenNotFoundException {
778         
779         removeLock(connection,uri, lock);
780         
781     }
782     
783     
784     /**
785      * Enumerate locks on an object.
786      *
787      * @param subject Subject
788      * @return Enumeration List of locks which have been put on the subject
789      * @exception ServiceAccessException Service access error
790      */

791     public Enumeration JavaDoc enumerateLocks(Connection JavaDoc connection,Uri uri)
792         throws ServiceAccessException {
793         
794         Vector JavaDoc lockVector = new Vector JavaDoc();
795         PreparedStatement JavaDoc statement = null;
796         
797         try {
798             
799             statement = connection.prepareStatement
800                 ("select * from locks where object= ?");
801             statement.setString(1, uri.toString());
802             statement.execute();
803             ResultSet JavaDoc res = statement.getResultSet();
804             
805             while (res.next()) {
806                 Date JavaDoc expirationDate = null;
807                 try {
808                     Long JavaDoc timeValue = new Long JavaDoc(res.getString
809                                               (LOCKS_EXPIRATIONDATE));
810                     expirationDate = new Date JavaDoc(timeValue.longValue());
811                 } catch (NumberFormatException JavaDoc e) {
812                     expirationDate = new Date JavaDoc();
813                 }
814                 NodeLock lock =
815                     new NodeLock(res.getString(LOCKS_ID),
816                                  res.getString(LOCKS_OBJECT),
817                                  res.getString(LOCKS_SUBJECT),
818                                  res.getString(LOCKS_TYPE),
819                                  expirationDate,
820                                  (res.getInt(LOCKS_INHERITABLE) == 1),
821                                  (res.getInt(LOCKS_EXCLUSIVE) == 1),
822                                  res.getString(LOCKS_OWNER)
823                                  );
824                 lockVector.addElement(lock);
825             }
826             
827         } catch (SQLException JavaDoc e) {
828             throw createException(e,uri);
829         } finally {
830             closeStatement(statement);
831         }
832         return lockVector.elements();
833     }
834     
835     /**
836        * Retrieve the revisions informations of an object.
837        *
838        * @param uri Uri
839        * @exception ServiceAccessException Service access error
840        * @exception RevisionDescriptorNotFoundException Revision descriptor
841        * was not found
842        */

843       public NodeRevisionDescriptors retrieveRevisionDescriptors(Connection JavaDoc connection,Uri uri)
844           throws ServiceAccessException, RevisionDescriptorNotFoundException {
845         
846           NodeRevisionDescriptors revisionDescriptors = null;
847           PreparedStatement JavaDoc statement = null;
848           PreparedStatement JavaDoc statement2 = null;
849         
850           try {
851               ResultSet JavaDoc res = null;
852             
853               NodeRevisionNumber initialRevision = new NodeRevisionNumber();
854               Hashtable JavaDoc workingRevisions = new Hashtable JavaDoc();
855               Hashtable JavaDoc latestRevisionNumbers = new Hashtable JavaDoc();
856               Hashtable JavaDoc branches = new Hashtable JavaDoc();
857               boolean isVersioned = false;
858             
859               statement = connection.prepareStatement
860                   ("select * from revisions where uri= ?");
861               statement.setString(1, uri.toString());
862               res = statement.executeQuery();
863             
864               if (res.next()) {
865                   int isVersionedInt = res.getInt(REVISIONS_ISVERSIONED);
866                   if (isVersionedInt == 1) {
867                       isVersioned = true;
868                   }
869               } else {
870                   throw new RevisionDescriptorNotFoundException(uri.toString());
871               }
872             
873               closeStatement(statement);
874             
875               statement = connection.prepareStatement
876                   ("select * from workingrevision where uri= ?");
877               statement.setString(1, uri.toString());
878               res = statement.executeQuery();
879             
880               while(res.next()) {
881                   // TODO : Parse each working revision definition
882
}
883             
884               closeStatement(statement);
885             
886               statement = connection.prepareStatement
887                   ("select * from latestrevisions where uri=?");
888               statement.setString(1, uri.toString());
889               res = statement.executeQuery();
890             
891               while(res.next()) {
892                   latestRevisionNumbers
893                       .put(res.getString(LATESTREVISIONS_BRANCHNAME),
894                            new NodeRevisionNumber
895                                (res.getString(LATESTREVISIONS_NUMBER)));
896               }
897               closeStatement(statement);
898             
899               statement = connection.prepareStatement
900                   ("select * from revision where uri= ?");
901               statement.setString(1, uri.toString());
902               res = statement.executeQuery();
903             
904               while(res.next()) {
905                   String JavaDoc currentRevisionNumber = res.getString(REVISION_NUMBER);
906                 
907                   // We parse the revision list of the object
908
if (statement2 == null){
909                       statement2 = connection.prepareStatement
910                           ("select * from branches where uri = ? and xnumber = ?");
911                   }
912                   statement2.setString(1, uri.toString());
913                   statement2.setString(2, currentRevisionNumber);
914                   ResultSet JavaDoc res2 = statement2.executeQuery();
915                   Vector JavaDoc childList = new Vector JavaDoc();
916                 
917                   while (res2.next()) {
918                       childList.addElement(new NodeRevisionNumber
919                           (res2.getString(BRANCHES_CHILDNUMBER)));
920                   }
921                 
922                   branches.put(new NodeRevisionNumber(currentRevisionNumber),
923                                childList);
924                 
925                   res2.close();
926               }
927               closeStatement(statement2);
928             
929               revisionDescriptors = new NodeRevisionDescriptors
930                   (uri.toString(), initialRevision, workingRevisions,
931                    latestRevisionNumbers, branches, isVersioned);
932             
933           } catch (SQLException JavaDoc e) {
934               throw createException(e,uri);
935           } finally {
936               closeStatement(statement);
937               closeStatement(statement2);
938           }
939           return revisionDescriptors;
940       }
941     
942     
943       /**
944        * Create a new revision information object.
945        *
946        * @param uri Uri
947        * @param revisionDescriptors Node revision descriptors
948        * @exception ServiceAccessException Service access error
949        */

950       public void createRevisionDescriptors
951           (Connection JavaDoc connection,Uri uri, NodeRevisionDescriptors revisionDescriptors)
952           throws ServiceAccessException {
953         
954           // TODO : Here, we have the option of "cleaning up" before
955
// creating the new records in the database.
956

957           PreparedStatement JavaDoc statement = null;
958         
959           try {
960             
961               // Creating record in revisions tables
962

963               int isVersioned = 0;
964               if (revisionDescriptors.isVersioned()) {
965                   isVersioned = 1;
966               }
967             
968               statement = connection.prepareStatement
969                   ("insert into revisions values(?,?,?)");
970               statement.setString(1,uri.toString());
971               statement.setInt(2, isVersioned);
972               statement.setString
973                   (3, revisionDescriptors.getInitialRevision().toString());
974               statement.execute();
975               closeStatement(statement);
976             
977               // Creating records in working revisions table
978
// ... TODO (working revisions are not used for now)
979

980               // Creating records in latest revisions table
981

982               // For now, only the latest revision from the main branch is stored
983
if (revisionDescriptors.getLatestRevision() != null) {
984                   statement = connection.prepareStatement
985                       ("insert into latestrevisions values(?,?,?)");
986                   statement.setString(1, uri.toString());
987                   statement.setString
988                       (2, NodeRevisionDescriptors.MAIN_BRANCH.toString());
989                   statement.setString
990                       (3, revisionDescriptors.getLatestRevision().toString());
991                   statement.execute();
992                   closeStatement(statement);
993               }
994             
995               // Creating records in the branches table
996
// TODO
997

998           } catch (SQLException JavaDoc e) {
999               throw createException(e,uri);
1000          } finally {
1001              closeStatement(statement);
1002          }
1003        
1004      }
1005    
1006    
1007      /**
1008       * Update revision information.
1009       *
1010       * @param uri Uri
1011       * @param revisionDescriptors Node revision descriptors
1012       * @exception ServiceAccessException Service access error
1013       * @exception RevisionDescriptorNotFoundException Revision descriptor
1014       * was not found
1015       */

1016      public void storeRevisionDescriptors
1017          (Connection JavaDoc connection,Uri uri, NodeRevisionDescriptors revisionDescriptors)
1018          throws ServiceAccessException, RevisionDescriptorNotFoundException {
1019        
1020          removeRevisionDescriptors(connection,uri);
1021          createRevisionDescriptors(connection,uri, revisionDescriptors);
1022        
1023      }
1024    
1025    
1026      /**
1027       * Remove revision information.
1028       *
1029       * @param uri Uri
1030       * @exception ServiceAccessException Service access error
1031       */

1032      public void removeRevisionDescriptors(Connection JavaDoc connection,Uri uri)
1033          throws ServiceAccessException {
1034        
1035          PreparedStatement JavaDoc statement = null;
1036        
1037          try {
1038            
1039              statement = connection.prepareStatement
1040                  ("delete from revisions where uri= ?");
1041              statement.setString(1, uri.toString());
1042              statement.execute();
1043              closeStatement(statement);
1044            
1045              statement = connection.prepareStatement
1046                  ("delete from workingrevision where uri= ?");
1047              statement.setString(1, uri.toString());
1048              statement.execute();
1049              closeStatement(statement);
1050            
1051              statement = connection.prepareStatement
1052                  ("delete from latestrevisions where uri= ?");
1053              statement.setString(1, uri.toString());
1054              statement.execute();
1055              closeStatement(statement);
1056            
1057              statement = connection.prepareStatement
1058                  ("delete from branches where uri= ?");
1059              statement.setString(1, uri.toString());
1060              statement.execute();
1061              closeStatement(statement);
1062            
1063          } catch (SQLException JavaDoc e) {
1064              throw createException(e,uri);
1065          } finally {
1066              closeStatement(statement);
1067          }
1068        
1069      }
1070    
1071    
1072      /**
1073       * Retrieve an individual object's revision descriptor.
1074       *
1075       * @param Uri uri
1076       * @param revisionNumber Node revision number
1077       */

1078      public NodeRevisionDescriptor retrieveRevisionDescriptor
1079          (Connection JavaDoc connection,Uri uri, NodeRevisionNumber revisionNumber)
1080          throws ServiceAccessException, RevisionDescriptorNotFoundException {
1081        
1082          NodeRevisionDescriptor revisionDescriptor = null;
1083          PreparedStatement JavaDoc statement = null;
1084
1085      if(revisionNumber == null)
1086          throw new RevisionDescriptorNotFoundException(uri.toString());
1087        
1088          try {
1089            
1090              ResultSet JavaDoc res = null;
1091            
1092              String JavaDoc branchName = null;
1093              Vector JavaDoc labels = new Vector JavaDoc();
1094              Hashtable JavaDoc properties = new Hashtable JavaDoc();
1095            
1096              // Retrieving branch name (and also check that revision
1097
// does indeed exist)
1098

1099              statement = connection.prepareStatement
1100                  ("select * from revision where uri= ? and xnumber = ?");
1101              statement.setString(1, uri.toString());
1102              statement.setString(2, revisionNumber.toString());
1103              res = statement.executeQuery();
1104            
1105              if (res.next()) {
1106                  branchName = res.getString(REVISION_BRANCHNAME);
1107              } else {
1108                  throw new RevisionDescriptorNotFoundException(uri.toString());
1109              }
1110            
1111              closeStatement(statement);
1112            
1113              // Retrieve labels
1114

1115              statement = connection.prepareStatement
1116                  ("select * from label where uri= ? and xnumber = ?");
1117              statement.setString(1, uri.toString());
1118              statement.setString(2, revisionNumber.toString());
1119              res = statement.executeQuery();
1120            
1121              while (res.next()) {
1122                  labels.addElement(res.getString(LABEL_LABEL));
1123              }
1124            
1125              closeStatement(statement);
1126            
1127              // Retrieve properties
1128

1129              statement = connection.prepareStatement
1130                  ("select * from property where uri= ? and xnumber = ?");
1131              statement.setString(1, uri.toString());
1132              statement.setString(2, revisionNumber.toString());
1133              res = statement.executeQuery();
1134            
1135              while (res.next()) {
1136                  String JavaDoc propertyName = res.getString(PROPERTY_NAME);
1137                  String JavaDoc propertyNamespace = res.getString(PROPERTY_NAMESPACE);
1138                  NodeProperty property =
1139                      new NodeProperty(propertyName,
1140                                       res.getString(PROPERTY_VALUE),
1141                                       propertyNamespace,
1142                                       res.getString(PROPERTY_TYPE),
1143                                       (res.getInt(PROPERTY_PROTECTED) == 1));
1144                  properties.put(propertyNamespace + propertyName, property);
1145              }
1146            
1147              revisionDescriptor =
1148                  new NodeRevisionDescriptor(revisionNumber, branchName,
1149                                             labels, properties);
1150            
1151          } catch (SQLException JavaDoc e) {
1152              throw createException(e,uri);
1153          } finally {
1154              closeStatement(statement);
1155          }
1156        
1157          return revisionDescriptor;
1158      }
1159    
1160    
1161      /**
1162       * Create a new revision descriptor.
1163       *
1164       * @param uri Uri
1165       * @param revisionDescriptor Node revision descriptor
1166       * @exception ServiceAccessException Service access error
1167       */

1168      public void createRevisionDescriptor
1169          (Connection JavaDoc connection,Uri uri, NodeRevisionDescriptor revisionDescriptor)
1170          throws ServiceAccessException {
1171        
1172          PreparedStatement JavaDoc statement = null;
1173        
1174          try {
1175               
1176              statement = connection.prepareStatement
1177                  ("insert into revision values(?, ?, ?)");
1178              statement.setString(1, uri.toString());
1179              statement.setString
1180                  (2, revisionDescriptor.getRevisionNumber().toString());
1181              statement.setString(3, revisionDescriptor.getBranchName());
1182              statement.execute();
1183              closeStatement(statement);
1184            
1185              // Creating revision labels
1186
statement = null;
1187              Enumeration JavaDoc labels = revisionDescriptor.enumerateLabels();
1188              while (labels.hasMoreElements()) {
1189                  if (statement == null){
1190                      statement = connection.prepareStatement
1191                          ("insert into label values(?,?,?)");
1192                  }
1193                  statement.setString(1, uri.toString());
1194                  statement.setString
1195                      (2, revisionDescriptor.getRevisionNumber().toString());
1196                  statement.setString(3, (String JavaDoc)labels.nextElement());
1197                  statement.execute();
1198              }
1199              closeStatement(statement);
1200
1201              // Creating associated properties
1202
statement = null;
1203              Enumeration JavaDoc properties = revisionDescriptor.enumerateProperties();
1204              while (properties.hasMoreElements()) {
1205                  NodeProperty property =
1206                      (NodeProperty) properties.nextElement();
1207                  int protectedProperty = 0;
1208                  if (property.isProtected()) {
1209                      protectedProperty = 1;
1210                  }
1211                  if (statement == null){
1212                      statement = connection.prepareStatement
1213                          ("insert into property values(?,?,?,?,?,?,?)");
1214                  }
1215                  statement.setString(1, uri.toString());
1216                  statement.setString
1217                      (2, revisionDescriptor.getRevisionNumber().toString());
1218                  statement.setString(3, property.getName());
1219                  statement.setString(4, property.getValue().toString());
1220                  statement.setString(5, property.getNamespace());
1221                  statement.setString(6, property.getType());
1222                  statement.setInt(7, protectedProperty);
1223                  statement.execute();
1224              }
1225              closeStatement(statement);
1226            
1227          } catch (SQLException JavaDoc e) {
1228              throw createException(e,uri);
1229          } finally {
1230              closeStatement(statement);
1231          }
1232        
1233      }
1234    
1235    
1236      /**
1237       * Update a revision descriptor.
1238       *
1239       * @param uri Uri
1240       * @param revisionDescriptors Node revision descriptor
1241       * @exception ServiceAccessException Service access error
1242       * @exception RevisionDescriptorNotFoundException Revision descriptor
1243       * was not found
1244       */

1245      public void storeRevisionDescriptor
1246          (Connection JavaDoc connection,Uri uri, NodeRevisionDescriptor revisionDescriptor)
1247          throws ServiceAccessException, RevisionDescriptorNotFoundException {
1248        
1249          removeRevisionDescriptor(connection,uri, revisionDescriptor.getRevisionNumber());
1250          createRevisionDescriptor(connection,uri, revisionDescriptor);
1251      }
1252    
1253    
1254      /**
1255       * Remove a revision descriptor.
1256       *
1257       * @param uri Uri
1258       * @param revisionNumber Revision number
1259       * @exception ServiceAccessException Service access error
1260       */

1261      public void removeRevisionDescriptor(Connection JavaDoc connection,Uri uri, NodeRevisionNumber number)
1262          throws ServiceAccessException {
1263        
1264          PreparedStatement JavaDoc statement = null;
1265        
1266          try {
1267            
1268              statement = connection.prepareStatement
1269                  ("delete from revision where uri= ? and xnumber = ?");
1270              statement.setString(1, uri.toString());
1271              statement.setString(2, number.toString());
1272              statement.execute();
1273              closeStatement(statement);
1274            
1275              // Removing revision labels
1276

1277              statement = connection.prepareStatement
1278                  ("delete from label where uri= ? and xnumber = ?");
1279              statement.setString(1, uri.toString());
1280              statement.setString(2, number.toString());
1281              statement.execute();
1282              closeStatement(statement);
1283            
1284              // Removing associated properties
1285

1286              statement = connection.prepareStatement
1287                  ("delete from property where uri= ? and xnumber = ?");
1288              statement.setString(1, uri.toString());
1289              statement.setString(2, number.toString());
1290              statement.execute();
1291            
1292          } catch (SQLException JavaDoc e) {
1293              throw createException(e,uri);
1294          } finally {
1295              closeStatement(statement);
1296          }
1297        
1298      }
1299    
1300
1301    protected void closeStatement(Statement JavaDoc statement) {
1302        try {
1303            if (statement != null) {
1304                statement.close();
1305            }
1306        } catch (SQLException JavaDoc e) {
1307            getLogger().log(e, LOG_CHANNEL, Logger.WARNING);
1308        }
1309    }
1310
1311    protected void close(PreparedStatement JavaDoc statement, ResultSet JavaDoc resultSet) {
1312        try {
1313            if (resultSet != null) {
1314                resultSet.close();
1315            }
1316        } catch (SQLException JavaDoc e) {
1317            getLogger().log(
1318                this,
1319                e,
1320                OldJDBCAdapter.LOG_CHANNEL,
1321                Logger.WARNING);
1322        } finally {
1323            try {
1324                if (statement != null) {
1325                    statement.close();
1326                }
1327            } catch (SQLException JavaDoc e) {
1328                getLogger().log(e, LOG_CHANNEL, Logger.WARNING);
1329            }
1330        }
1331    }
1332
1333    /**
1334      * overload this method to have a more detailed error handling
1335      */

1336    protected ServiceAccessException createException(Exception JavaDoc e, Uri uri) {
1337        getLogger().log(
1338            "Error on " + uri.toString() + ": " + e.getMessage(),
1339            LOG_CHANNEL,
1340            Logger.ERROR);
1341        return new ServiceAccessException(service, e);
1342    }
1343
1344    /**
1345     * overload this method to have a more detailed error handling
1346     */

1347    protected ServiceAccessException createException(SQLException JavaDoc e, Uri uri) {
1348        e.printStackTrace();
1349        getLogger().log(
1350            "Error "
1351                + e.getErrorCode()
1352                + "," + e.getSQLState()
1353                + " on "
1354                + uri.toString()
1355                + ": "
1356                + e.getMessage(),
1357            LOG_CHANNEL,
1358            Logger.ERROR);
1359        return new ServiceAccessException(service, e);
1360    }
1361
1362    /**
1363     * This method will always fail, since the ContentStore interface is not supported.
1364     */

1365    public NodeRevisionContent retrieveRevisionContent(Connection JavaDoc conn, Uri uri, NodeRevisionDescriptor revisionDescriptor, boolean temporaryConnection) throws ServiceAccessException, RevisionNotFoundException {
1366       throw createException(new UnsupportedOperationException JavaDoc("ContentStore interface not suported"),uri);
1367    }
1368
1369    /**
1370     * This method will always fail, since the ContentStore interface is not supported.
1371     */

1372    public void createRevisionContent(Connection JavaDoc conn, Uri uri, NodeRevisionDescriptor revisionDescriptor, NodeRevisionContent revisionContent) throws ServiceAccessException, RevisionAlreadyExistException {
1373        throw createException(new UnsupportedOperationException JavaDoc("ContentStore interface not suported"),uri);
1374        
1375    }
1376    /**
1377     * This method will always fail, since the ContentStore interface is not supported.
1378     */

1379    public void storeRevisionContent(Connection JavaDoc conn, Uri uri, NodeRevisionDescriptor revisionDescriptor, NodeRevisionContent revisionContent) throws ServiceAccessException, RevisionNotFoundException {
1380        throw createException(new UnsupportedOperationException JavaDoc("ContentStore interface not suported"),uri);
1381    }
1382
1383    /**
1384     * This method will always fail, since the ContentStore interface is not supported.
1385     */

1386    public void removeRevisionContent(Connection JavaDoc conn, Uri uri, NodeRevisionDescriptor revisionDescriptor) throws ServiceAccessException {
1387        throw createException(new UnsupportedOperationException JavaDoc("ContentStore interface not suported"),uri);
1388    }
1389}
1390
Popular Tags