KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/stores/org/apache/slide/store/impl/rdbms/DB2RDBMSAdapter.java,v 1.3.2.5 2004/12/07 21:05:50 wburrows Exp $
3  * $Revision: 1.3.2.5 $
4  * $Date: 2004/12/07 21:05:50 $
5  *
6  * ====================================================================
7  *
8  * Copyright 2004 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 org.apache.slide.common.*;
27 import org.apache.slide.content.*;
28 import org.apache.slide.lock.NodeLock;
29 import org.apache.slide.security.NodePermission;
30 import org.apache.slide.structure.LinkNode;
31 import org.apache.slide.structure.ObjectNode;
32 import org.apache.slide.structure.ObjectNotFoundException;
33 import org.apache.slide.util.logger.Logger;
34
35 import java.sql.Connection JavaDoc;
36 import java.sql.PreparedStatement JavaDoc;
37 import java.sql.ResultSet JavaDoc;
38 import java.sql.SQLException JavaDoc;
39 import java.util.Enumeration JavaDoc;;
40
41 /**
42  * Adapter for DB2 8.
43  * @version $Revision: 1.3.2.5 $
44  */

45
46 public class DB2RDBMSAdapter extends CommonRDBMSAdapter
47  {
48 // The reason This adapter is needed is because DB2 does not like parameter markers in select columns. Ex from StandardRDBMSAdapter:
49
// "insert into BINDING (URI_ID, NAME, CHILD_UURI_ID) select ?, ?, URI_ID from URI where URI_STRING = ?"
50

51     public DB2RDBMSAdapter(Service service, Logger logger) {
52         super(service, logger);
53         bcompress = false;
54     }
55
56
57     protected boolean storeObject(Connection JavaDoc connection, Uri uri, ObjectNode object, boolean create)
58         throws ServiceAccessException {
59         String JavaDoc className = object.getClass().getName();
60         long uriid;
61         try {
62             PreparedStatement JavaDoc statement = null;
63             ResultSet JavaDoc res = null;
64             try {
65                 uriid = assureUriId(connection, uri.toString());
66                 statement =
67                     connection.prepareStatement(
68                         "select 1 from OBJECT o, URI u where o.URI_ID=u.URI_ID and u.URI_STRING=?");
69                 statement.setString(1, uri.toString());
70                 res = statement.executeQuery();
71                 if (res.next()) {
72                     if (create)
73                         return false;
74                 } else {
75                     if (!create)
76                         return false;
77                 }
78             } finally {
79                 close(statement, res);
80             }
81
82             if (create) {
83                 // create object in database
84
try {
85                     statement = connection.prepareStatement("insert into OBJECT (URI_ID,CLASS_NAME) values (?,?)");
86                     statement.setLong(1, uriid);
87                     statement.setString(2, className);
88                     statement.executeUpdate();
89                 } finally {
90                     close(statement);
91                 }
92             }
93
94             // update binding...
95

96             try {
97                 clearBinding(connection, uri);
98             } catch (ObjectNotFoundException e1) {
99                 // clear only if it existed
100
}
101
102             Enumeration JavaDoc bindings = object.enumerateBindings();
103             while (bindings.hasMoreElements()) {
104                 ObjectNode.Binding binding = (ObjectNode.Binding) bindings.nextElement();
105                 try {
106                     long childID = getID(connection,binding.getUuri());
107                     /*
108                      * childID can be 0 if we are the root "/" and the child is a mount point for
109                      * another store since it won't have been added to the URI table since its is
110                      * the root of another store. So when there's no URI for the child just create
111                      * a child URI for the binding table to refer to.
112                      */

113                     if (childID == 0)
114                         childID = assureUriId(connection, binding.getUuri());
115                         
116                     statement =
117                         connection.prepareStatement(
118                             "insert into BINDING (URI_ID, NAME, CHILD_UURI_ID) values (?,?,?)");
119                     statement.setLong(1, uriid);
120                     statement.setString(2, binding.getName());
121                     statement.setLong(3, childID);
122                     statement.executeUpdate();
123                 } finally {
124                     close(statement);
125                 }
126             }
127
128             Enumeration JavaDoc parentBindings = object.enumerateParentBindings();
129             while (parentBindings.hasMoreElements()) {
130                 ObjectNode.ParentBinding parentBinding = (ObjectNode.ParentBinding) parentBindings.nextElement();
131                 try {
132                      long parentID = getID(connection,parentBinding.getUuri());
133
134                     statement =
135                         connection.prepareStatement(
136                             "insert into PARENT_BINDING (URI_ID, NAME, PARENT_UURI_ID) values (?,?,?)");
137                     statement.setLong(1, uriid);
138                     statement.setString(2, parentBinding.getName());
139                     statement.setLong(3, parentID);
140                     statement.executeUpdate();
141                 } finally {
142                     close(statement);
143                 }
144             }
145
146             if (object instanceof LinkNode) {
147                 // update link target
148
try {
149                     statement = connection.prepareStatement("delete from LINKS where URI_ID = ?");
150                     statement.setLong(1, uriid);
151                     statement.executeUpdate();
152                 } finally {
153                     close(statement);
154                 }
155                 try {
156
157                     // First get the linked uri id
158
long linkedID = getID(connection,((LinkNode) object).getLinkedUri());
159
160                     // now insert link data
161
statement =
162                         connection.prepareStatement(
163                             "insert into LINKS (URI_ID, LINK_TO_ID) values (?,?)");
164                     statement.setLong(1, uriid);
165                     statement.setLong(2, linkedID);
166                     statement.executeUpdate();
167                 } finally {
168                     close(statement);
169                 }
170             }
171         } catch (SQLException JavaDoc e) {
172             throw createException(e, uri.toString());
173         }
174         return true;
175     }
176
177
178     public void putLock(Connection JavaDoc connection, Uri uri, NodeLock lock) throws ServiceAccessException {
179         PreparedStatement JavaDoc statement = null;
180         try {
181             int inheritable = lock.isInheritable() ? 1 : 0;
182             int exclusive = lock.isExclusive() ? 1 : 0;
183             long lockid = assureUriId(connection, lock.getLockId());
184             long objectID = getID(connection, lock.getObjectUri());
185             long subjectID = getID(connection, lock.getSubjectUri());
186             long typeID = getID(connection, lock.getTypeUri());
187
188             statement =
189                 connection.prepareStatement(
190                     "insert into LOCKS (LOCK_ID,OBJECT_ID,SUBJECT_ID,TYPE_ID,EXPIRATION_DATE,IS_INHERITABLE,IS_EXCLUSIVE,OWNER) values (?,?,?,?,?,?,?,?)");
191             statement.setLong(1, lockid);
192             statement.setLong(2, objectID);
193             statement.setLong(3, subjectID);
194             statement.setLong(4, typeID);
195             statement.setLong(5, lock.getExpirationDate().getTime());
196             statement.setInt(6, inheritable);
197             statement.setInt(7, exclusive);
198             statement.setString(8, lock.getOwnerInfo());
199
200             statement.execute();
201         } catch (SQLException JavaDoc e) {
202             throw createException(e, uri.toString());
203         } finally {
204             close(statement);
205         }
206     }
207
208     public void grantPermission(Connection JavaDoc connection, Uri uri, NodePermission permission)
209         throws ServiceAccessException {
210         PreparedStatement JavaDoc statement = null;
211         ResultSet JavaDoc res = null;
212         int succession = 0;
213
214         try {
215             // FIXME: This might be useless if insert inserts nothing if insert...select works a i expect
216
// FIXME: What happens, if only revision number, inheritable or negative changes?
217
// FIXME
218
// revokePermission(connection, uri, permission);
219

220             try {
221                 statement =
222                     connection.prepareStatement(
223                         "select max(p.SUCCESSION) from URI ou, PERMISSIONS p where p.OBJECT_ID = ou.URI_ID and ou.URI_STRING = ?");
224                 statement.setString(1, permission.getObjectUri());
225                 res = statement.executeQuery();
226                 res.next();
227                 succession = res.getInt(1) + 1;
228             } finally {
229                 close(statement, res);
230             }
231
232             assureUriId(connection,permission.getSubjectUri());
233             assureUriId(connection,permission.getActionUri());
234
235             try {
236
237                 int inheritable = permission.isInheritable() ? 1 : 0;
238                 int negative = permission.isNegative() ? 1 : 0;
239                 long objectID = getID(connection,permission.getObjectUri());
240                 long subjectID = getID(connection,permission.getSubjectUri());
241                 long actionID = getID(connection,permission.getActionUri());
242
243                 statement =
244                     connection.prepareStatement(
245                         "insert into PERMISSIONS (OBJECT_ID,SUBJECT_ID,ACTION_ID,VERSION_NO, IS_INHERITABLE,IS_NEGATIVE,SUCCESSION) values (?,?,?,?,?,?,?)");
246                 statement.setLong(1, objectID);
247                 statement.setLong(2, subjectID);
248                 statement.setLong(3, actionID);
249                 statement.setString(4, getRevisionNumberAsString(permission.getRevisionNumber()));
250                 statement.setInt(5, inheritable);
251                 statement.setInt(6, negative);
252                 statement.setInt(7, succession);
253                 if (statement.executeUpdate() != 1) {
254                     String JavaDoc msg = "Failed to insert permission ("
255                     + permission.getObjectUri()
256                     + "," + permission.getSubjectUri()
257                     + "," + permission.getActionUri()
258                     + ")";
259                     getLogger().log(msg,LOG_CHANNEL,Logger.ERROR);
260                 }
261             } finally {
262                 close(statement);
263             }
264         } catch (SQLException JavaDoc e) {
265             throw createException(e, uri.toString());
266         }
267     }
268
269     public void createRevisionDescriptor(Connection JavaDoc connection, Uri uri, NodeRevisionDescriptor revisionDescriptor)
270         throws ServiceAccessException {
271
272         PreparedStatement JavaDoc statement = null;
273         try {
274
275             assureVersionInfo(connection, uri, revisionDescriptor);
276
277             for (Enumeration JavaDoc labels = revisionDescriptor.enumerateLabels(); labels.hasMoreElements();) {
278                 long labelId = assureLabelId(connection, (String JavaDoc) labels.nextElement());
279                 try {
280                     long versionID = getVersionID(connection, uri.toString(), revisionDescriptor);
281                     statement =
282                         connection.prepareStatement(
283                             "insert into VERSION_LABELS (VERSION_ID, LABEL_ID) values (?,?)");
284                     statement.setLong(1, versionID);
285                     statement.setLong(2, labelId);
286                     statement.executeUpdate();
287                 } finally {
288                     close(statement);
289                 }
290             }
291             for (Enumeration JavaDoc properties = revisionDescriptor.enumerateProperties(); properties.hasMoreElements();) {
292                 try {
293                     NodeProperty property = (NodeProperty) properties.nextElement();
294                     long versionID = getVersionID(connection, uri.toString(), revisionDescriptor);
295                     statement =
296                         connection.prepareStatement(
297                             "insert into PROPERTIES (VERSION_ID,PROPERTY_NAMESPACE,PROPERTY_NAME,PROPERTY_VALUE,PROPERTY_TYPE,IS_PROTECTED) values (?,?,?,?,?,?)");
298                     int protectedProperty = property.isProtected() ? 1 : 0;
299                     statement.setLong(1, versionID);
300                     statement.setString(2, property.getNamespace());
301                     statement.setString(3, property.getName());
302                     statement.setString(4, property.getValue().toString());
303                     statement.setString(5, property.getType());
304                     statement.setInt(6, protectedProperty);
305                     statement.executeUpdate();
306                 } finally {
307                     close(statement);
308                 }
309             }
310         } catch (SQLException JavaDoc e) {
311             throw createException(e, uri.toString());
312         }
313     }
314
315     public void createRevisionDescriptors(Connection JavaDoc connection, Uri uri, NodeRevisionDescriptors revisionDescriptors)
316     throws ServiceAccessException {
317
318         PreparedStatement JavaDoc statement = null;
319         ResultSet JavaDoc res = null;
320         try {
321             int isVersioned = 0;
322             if (revisionDescriptors.isVersioned())
323                 isVersioned = 1;
324             boolean revisionExists;
325             try {
326                 statement =
327                     connection.prepareStatement(
328                     "SELECT u.URI_ID FROM VERSION v, URI u WHERE v.URI_ID = u.URI_ID and u.URI_STRING = ?");
329                 statement.setString(1, uri.toString());
330                 res = statement.executeQuery();
331                 revisionExists = res.next();
332
333             } finally {
334                 close(statement, res);
335             }
336             if (!revisionExists) {
337                 try {
338                     long id = getID(connection,uri.toString());
339                     statement =
340                         connection.prepareStatement(
341                         "insert into VERSION (URI_ID, IS_VERSIONED) values (?,?)");
342                     statement.setLong(1, id);
343                     statement.setInt(2, isVersioned);
344                     statement.executeUpdate();
345                 } finally {
346                     close(statement, res);
347                 }
348             }
349             boolean versionHistoryExists = false;
350             if (revisionDescriptors.getLatestRevision() != null) {
351                 try {
352                     statement =
353                         connection.prepareStatement(
354                                 "SELECT 1 FROM VERSION_HISTORY vh, URI u WHERE vh.URI_ID = u.URI_ID and u.URI_STRING = ? and REVISION_NO = ?");
355                     statement.setString(1, uri.toString());
356                     statement.setString(2, revisionDescriptors.getLatestRevision().toString());
357                     res = statement.executeQuery();
358                     versionHistoryExists = res.next();
359                 } finally {
360                     close(statement, res);
361                 }
362             }
363             if (!versionHistoryExists && revisionDescriptors.getLatestRevision() != null) {
364                 try {
365
366                     long[] ids = getBranchIdAndUriID(connection,uri.toString());
367                     long uriID=ids[0];
368                     long branchID=ids[1];
369
370                     statement =
371                         connection.prepareStatement(
372                         "insert into VERSION_HISTORY (URI_ID, BRANCH_ID, REVISION_NO) values(?,?,?)");
373                     statement.setLong(1, uriID);
374                     statement.setLong(2, branchID);
375                     statement.setString(3, getRevisionNumberAsString(revisionDescriptors.getLatestRevision()));
376
377                     // FIXME: Create new revisions on the main branch???
378
statement.executeUpdate();
379                 } finally {
380                     close(statement, res);
381                 }
382             }
383
384
385             // Add revision successors
386
Enumeration JavaDoc revisionNumbers = revisionDescriptors.enumerateRevisionNumbers();
387             while (revisionNumbers.hasMoreElements()) {
388                 NodeRevisionNumber nodeRevisionNumber = (NodeRevisionNumber) revisionNumbers.nextElement();
389
390                 Enumeration JavaDoc successors = revisionDescriptors.getSuccessors(nodeRevisionNumber);
391                 while (successors.hasMoreElements())
392                 {
393                     try {
394                         NodeRevisionNumber successor = (NodeRevisionNumber) successors.nextElement();
395
396                         statement =
397                             connection.prepareStatement(
398                                     "insert into VERSION_PREDS (VERSION_ID, PREDECESSOR_ID) " +
399                                     " select vr.VERSION_ID, suc.VERSION_ID" +
400                                     " FROM URI uri, VERSION_HISTORY vr, VERSION_HISTORY suc " +
401                                     " where vr.URI_ID = uri.URI_ID " +
402                                     " and suc.URI_ID = uri.URI_ID " +
403                                     " and uri.URI_STRING = ? " +
404                                     " and vr.REVISION_NO = ? " +
405                                     " and suc.REVISION_NO = ? " );
406
407                         statement.setString(1, uri.toString());
408                         statement.setString(2, nodeRevisionNumber.toString());
409                         statement.setString(3, successor.toString());
410                         statement.executeUpdate();
411                     } finally {
412                         close(statement);
413                     }
414                 }
415             }
416             getLogger().log(
417                     revisionDescriptors.getOriginalUri() + revisionDescriptors.getInitialRevision(),
418                     LOG_CHANNEL,
419                     Logger.INFO);
420
421
422         } catch (SQLException JavaDoc e) {
423             throw createException(e, uri.toString());
424         }
425     }
426
427     protected void assureVersionInfo(Connection JavaDoc connection, Uri uri, NodeRevisionDescriptor revisionDescriptor)
428         throws SQLException JavaDoc {
429         PreparedStatement JavaDoc statement = null;
430         ResultSet JavaDoc res = null;
431         boolean revisionExists;
432         try {
433             statement =
434                 connection.prepareStatement(
435                     "select 1 from VERSION v, URI u where v.URI_ID = u.URI_ID and u.URI_STRING = ?");
436             statement.setString(1, uri.toString());
437             res = statement.executeQuery();
438             revisionExists = res.next();
439         } finally {
440             close(statement, res);
441         }
442         // FIXME: Is it true, that the default for IS_VERSIONED is 0 ??
443
if (!revisionExists) {
444             try {
445                 long id = getID(connection,uri.toString());
446                 statement =
447                     connection.prepareStatement(
448                         "insert into VERSION (URI_ID, IS_VERSIONED) values (?,?)");
449                 statement.setLong(1,id);
450                 statement.setInt(2, 0);
451                 statement.executeUpdate();
452             } finally {
453                 close(statement);
454             }
455         }
456         boolean versionHistoryExists;
457         try {
458             statement =
459                 connection.prepareStatement(
460                     "select 1 from VERSION_HISTORY vh, URI u where vh.URI_ID = u.URI_ID and u.URI_STRING = ? and REVISION_NO = ?");
461             statement.setString(1, uri.toString());
462             statement.setString(2, revisionDescriptor.getRevisionNumber().toString());
463             res = statement.executeQuery();
464             versionHistoryExists = res.next();
465         } finally {
466             close(statement, res);
467         }
468         if (!versionHistoryExists) {
469             long branchId = assureBranchId(connection, revisionDescriptor.getBranchName());
470             try {
471
472                 long id = getID(connection,uri.toString());
473
474                 statement =
475                     connection.prepareStatement(
476                         "insert into VERSION_HISTORY (URI_ID, BRANCH_ID, REVISION_NO) values (?,?,?)");
477                 statement.setLong(1, id);
478                 statement.setLong(2, branchId);
479                 statement.setString(3, getRevisionNumberAsString(revisionDescriptor.getRevisionNumber()));
480                 statement.executeUpdate();
481             } finally {
482                 close(statement);
483             }
484         }
485     }
486
487
488     private long getID(Connection JavaDoc connection, String JavaDoc uriString) throws SQLException JavaDoc
489     {
490         PreparedStatement JavaDoc statement = null;
491         ResultSet JavaDoc rs = null;
492         long uriID=0l;
493         try
494         {
495             statement =
496                 connection.prepareStatement(
497                     "select URI_ID from URI where URI_STRING = ?");
498
499              statement.setString(1,uriString);
500              rs = statement.executeQuery();
501              if(rs.next())
502                  uriID= rs.getLong(1);
503          }
504          finally
505          {
506              close(statement,rs);
507          }
508
509          return uriID;
510     }
511
512   private long[] getBranchIdAndUriID(Connection JavaDoc connection, String JavaDoc uriString) throws SQLException JavaDoc
513   {
514     PreparedStatement JavaDoc statement = null;
515     ResultSet JavaDoc res = null;
516     long[] ids = new long[2];
517
518     try
519     {
520         statement =
521             connection.prepareStatement(
522                 "select u.URI_ID, b.BRANCH_ID from URI u, BRANCH b where u.URI_STRING = ? and b.BRANCH_STRING = ?");
523
524          statement.setString(1, uriString);
525          statement.setString(2,NodeRevisionDescriptors.MAIN_BRANCH);
526
527          res = statement.executeQuery();
528          if(res.next())
529          {
530              ids[0]=res.getLong(1);
531              ids[1] = res.getLong(2);
532          }
533      }
534      finally
535      {
536          close(statement,res);
537      }
538
539      return ids;
540   }
541
542   protected String JavaDoc convertRevisionNumberToComparable(String JavaDoc revisioNumber) {
543       return revisioNumber;
544   }
545 }
546
547
Popular Tags