KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > webdav > stores > JahiaJDBCAdapter


1 /*
2  * Copyright (c) 2004 Your Corporation. All Rights Reserved.
3  */

4 package org.jahia.services.webdav.stores;
5
6 import org.apache.slide.store.impl.rdbms.AbstractRDBMSAdapter;
7 import org.apache.slide.store.impl.rdbms.StandardRDBMSAdapter;
8 import org.apache.slide.common.*;
9 import org.apache.slide.util.logger.Logger;
10 import org.apache.slide.structure.ObjectNode;
11 import org.apache.slide.structure.ObjectAlreadyExistsException;
12 import org.apache.slide.structure.ObjectNotFoundException;
13 import org.apache.slide.structure.LinkNode;
14 import org.apache.slide.lock.NodeLock;
15 import org.apache.slide.lock.LockTokenNotFoundException;
16 import org.apache.slide.security.NodePermission;
17 import org.apache.slide.content.*;
18 import org.jahia.registries.ServicesRegistry;
19 import org.jahia.exceptions.JahiaException;
20
21 import java.util.*;
22 import java.sql.Connection JavaDoc;
23 import java.sql.PreparedStatement JavaDoc;
24 import java.sql.ResultSet JavaDoc;
25 import java.sql.SQLException JavaDoc;
26 import java.lang.reflect.Constructor JavaDoc;
27
28 /**
29  * Created by IntelliJ IDEA.
30  * User: toto
31  * Date: 7 sept. 2004
32  * Time: 17:28:45
33  * <p/>
34  * $Author: bpapez $
35  * $Date: 2006-11-24 20:11:27 +0100 (Fri, 24 Nov 2006) $
36  * $Id: JahiaJDBCAdapter.java 15746 2006-11-24 19:11:27Z bpapez $
37  * $RCSfile: JahiaJDBCAdapter.java,v $
38  * $Revision: 15746 $
39  * $Source: d:/rd/cvs/cvsrepo/commaro/frame/COFRJAHIA_DEVELOP/source/java/org/jahia/services/webdav/stores/JahiaJDBCAdapter.java,v $
40  * $State: Exp $
41  */

42 public class JahiaJDBCAdapter extends AbstractRDBMSAdapter {
43
44
45     protected static final String JavaDoc LOG_CHANNEL = StandardRDBMSAdapter.class.getName();
46
47     protected boolean bcompress;
48
49     public JahiaJDBCAdapter(Service aService, Logger aLogger) {
50         super(aService, aLogger);
51         bcompress = false;
52     }
53
54     public void setParameters(Hashtable parameters)
55         throws ServiceParameterErrorException, ServiceParameterMissingException {
56         try {
57             bcompress = "true".equalsIgnoreCase((String JavaDoc) parameters.get("compress"));
58             if (bcompress)
59                 getLogger().log("Switching on content compression", LOG_CHANNEL, Logger.INFO);
60             super.setParameters(parameters);
61         } catch (Exception JavaDoc e) {
62             getLogger().log(e.toString(), LOG_CHANNEL, Logger.ERROR);
63         }
64     }
65
66     // Object
67

68     public void createObject(Connection JavaDoc connection, Uri uri, ObjectNode object)
69         throws ObjectAlreadyExistsException, ServiceAccessException {
70         if (!storeObject(connection, uri, object, true))
71             throw new ObjectAlreadyExistsException(uri.toString());
72     }
73
74     public void storeObject(Connection JavaDoc connection, Uri uri, ObjectNode object)
75         throws ObjectNotFoundException, ServiceAccessException {
76         if (!storeObject(connection, uri, object, false))
77             throw new ObjectNotFoundException(uri.toString());
78     }
79
80     protected synchronized boolean storeObject(Connection JavaDoc connection, Uri uri, ObjectNode object, boolean create)
81         throws ServiceAccessException {
82         String JavaDoc className = object.getClass().getName();
83         long uriid;
84         try {
85             PreparedStatement JavaDoc statement = null;
86             ResultSet JavaDoc res = null;
87             try {
88                 uriid = assureUriId(connection, uri.toString());
89                 statement =
90                     connection.prepareStatement(
91                         "select 1 from jahia_sl2_object o, jahia_sl2_uri u where o.uri_id=u.uri_id and u.uri_string=? and u.namespace=?");
92                 statement.setString(1, uri.toString());
93                 statement.setInt(2, ((JahiaDescriptorsStore)service).getSiteId());
94                 res = statement.executeQuery();
95                 if (res.next()) {
96                     if (create)
97                         return false;
98                 } else {
99                     if (!create)
100                         return false;
101                 }
102             } finally {
103                 close(statement, res);
104             }
105
106             if (create) {
107                 // create object in database
108
try {
109                     statement = connection.prepareStatement("insert into jahia_sl2_object (uri_id,class_name) values (?,?)");
110                     statement.setLong(1, uriid);
111                     statement.setString(2, className);
112                     statement.executeUpdate();
113                 } finally {
114                     close(statement);
115                 }
116             }
117
118             // update binding...
119

120             Set JavaDoc updatedBindings = object.getUpdatedBindings();
121             Set JavaDoc updatedBindingNames = new HashSet();
122             for (Iterator iterator = updatedBindings.iterator(); iterator.hasNext();) {
123                 String JavaDoc bindingUri = (String JavaDoc) iterator.next();
124                 String JavaDoc bindingName = bindingUri.substring(bindingUri.lastIndexOf('/')+1);
125                 updatedBindingNames.add(bindingName);
126             }
127
128             statement = connection.prepareStatement("select name, child_uuri_id from jahia_sl2_binding where uri_id=?");
129             statement.setLong(1, uriid);
130             ResultSet JavaDoc s = statement.executeQuery();
131             while (s.next()) {
132                 String JavaDoc bindingName = s.getString(1);
133                 long cuuid = s.getLong(2);
134
135                 PreparedStatement JavaDoc updateStatement = null;
136
137                 if (updatedBindingNames.remove(bindingName)) {
138                     if (object.getBindingUuri(bindingName) == null) {
139                         try {
140                             updateStatement =
141                                     connection.prepareStatement(
142                                             "delete from jahia_sl2_binding where uri_id=? and name=? and child_uuri_id=?");
143                             updateStatement.setLong(1, uriid);
144                             updateStatement.setString(2, bindingName);
145                             updateStatement.setLong(3, cuuid);
146                             updateStatement.executeUpdate();
147                         } finally {
148                             close(updateStatement);
149                         }
150                         try {
151                             updateStatement =
152                                     connection.prepareStatement(
153                                             "delete from jahia_sl2_parent_binding where uri_id=? and name=? and parent_uuri_id=?");
154                             updateStatement.setLong(1, cuuid);
155                             updateStatement.setString(2, bindingName);
156                             updateStatement.setLong(3, uriid);
157                             updateStatement.executeUpdate();
158                         } finally {
159                             close(updateStatement);
160                         }
161                     }
162                 }
163             }
164
165             for (Iterator iterator = updatedBindingNames.iterator(); iterator.hasNext();) {
166                 String JavaDoc bindingName = (String JavaDoc) iterator.next();
167                 if (object.getBindingUuri(bindingName) != null) {
168                     long cuuid = assureUriId(connection, object.getBindingUuri(bindingName));
169                     try {
170                         statement =
171                                 connection.prepareStatement(
172                                         "insert into jahia_sl2_binding (uri_id, name, child_uuri_id) values (?, ?, ?)");
173                         statement.setLong(1, uriid);
174                         statement.setString(2, bindingName);
175                         statement.setLong(3, cuuid);
176                         statement.executeUpdate();
177                     } finally {
178                         close(statement);
179                     }
180                     try {
181                         statement =
182                                 connection.prepareStatement(
183                                         "insert into jahia_sl2_parent_binding (uri_id, name, parent_uuri_id) values (?, ?, ?)");
184                         statement.setLong(1, cuuid);
185                         statement.setString(2, bindingName);
186                         statement.setLong(3, uriid);
187                         statement.executeUpdate();
188                     } finally {
189                         close(statement);
190                     }
191                 }
192             }
193
194             if (object instanceof LinkNode) {
195                 // update link target
196
try {
197                     statement = connection.prepareStatement("delete from jahia_sl2_links where uri_id = ?");
198                     statement.setLong(1, uriid);
199                     statement.executeUpdate();
200                 } finally {
201                     close(statement);
202                 }
203                 try {
204                     long luuid = assureUriId(connection, ((LinkNode) object).getLinkedUri());
205                     statement =
206                             connection.prepareStatement(
207                                     "insert into jahia_sl2_links (uri_id, link_to_id) values (?, ?)");
208                     statement.setLong(1, uriid);
209                     statement.setLong(2, luuid);
210                     statement.executeUpdate();
211                 } finally {
212                     close(statement);
213                 }
214             }
215         } catch (SQLException JavaDoc e) {
216             throw createException(e, uri.toString());
217         } catch (JahiaException e) {
218             throw createException(e, uri.toString());
219         }
220         object.resetUpdatedBindings();
221         return true;
222     }
223
224     public synchronized void removeObject(Connection JavaDoc connection, Uri uri, ObjectNode object)
225         throws ServiceAccessException, ObjectNotFoundException {
226         PreparedStatement JavaDoc statement = null;
227         try {
228
229             long uriId = getUriId(connection, uri.toString());
230             clearBinding(connection, uriId);
231
232             // delete links
233
try {
234                 statement =
235                     connection.prepareStatement(
236                         "delete from jahia_sl2_links where uri_id = ?");
237                 statement.setLong(1, uriId);
238                 statement.executeUpdate();
239             } finally {
240                 close(statement);
241             }
242             // delete version history
243
// FIXME: Is this true??? Should the version history be removed if the object is removed???
244
try {
245                 statement =
246                     connection.prepareStatement(
247                         "delete from jahia_sl2_version_history where uri_id = ?");
248                 statement.setLong(1, uriId);
249                 statement.executeUpdate();
250             } finally {
251                 close(statement);
252             }
253             // delete version
254
try {
255                 statement =
256                     connection.prepareStatement(
257                         "delete from jahia_sl2_version where uri_id = ?");
258                 statement.setLong(1, uriId);
259                 statement.executeUpdate();
260             } finally {
261                 close(statement);
262             }
263             // delete the object itself
264
try {
265                 statement =
266                     connection.prepareStatement(
267                         "delete from jahia_sl2_object where uri_id = ?");
268                 statement.setLong(1, uriId);
269                 statement.executeUpdate();
270             } finally {
271                 close(statement);
272             }
273             // finally delete the uri
274
try {
275                 statement = connection.prepareStatement("delete from jahia_sl2_uri where uri_string = ? and namespace = ?");
276                 statement.setString(1, uri.toString());
277                 statement.setInt(2, ((JahiaDescriptorsStore)service).getSiteId());
278                 statement.executeUpdate();
279             } finally {
280                 close(statement);
281             }
282         } catch (SQLException JavaDoc e) {
283             throw createException(e, uri.toString());
284         } catch (JahiaException e) {
285             throw createException(e, uri.toString());
286
287         }
288     }
289
290     public ObjectNode retrieveObject(Connection JavaDoc connection, Uri uri)
291         throws ServiceAccessException, ObjectNotFoundException {
292         ObjectNode result = null;
293         try {
294             PreparedStatement JavaDoc statement = null;
295             ResultSet JavaDoc res = null;
296             String JavaDoc className;
297             Vector children = new Vector();
298             Vector parents = new Vector();
299             Vector links = new Vector();
300             try {
301                 statement =
302                     connection.prepareStatement(
303                         "select o.class_name from jahia_sl2_object o, jahia_sl2_uri u where o.uri_id = u.uri_id and u.uri_string = ? and u.namespace = ?");
304                 statement.setString(1, uri.toString());
305                 statement.setInt(2, ((JahiaDescriptorsStore)service).getSiteId());
306                 res = statement.executeQuery();
307                 if (res.next()) {
308                     className = res.getString(1);
309                 } else {
310                     throw new ObjectNotFoundException(uri);
311                 }
312             } finally {
313                 close(statement, res);
314             }
315
316             try {
317                 statement =
318                     connection.prepareStatement(
319                         "SELECT c.name, cu.uri_string FROM jahia_sl2_uri u, jahia_sl2_uri cu, jahia_sl2_binding c WHERE cu.uri_id = c.child_uuri_id AND c.uri_id = u.uri_id and u.uri_string = ? and u.namespace = ?");
320                 statement.setString(1, uri.toString());
321                 statement.setInt(2, ((JahiaDescriptorsStore)service).getSiteId());
322                 res = statement.executeQuery();
323                 while (res.next()) {
324                     children.addElement(new ObjectNode.Binding(res.getString(1), res.getString(2)));
325                 }
326             } finally {
327                 close(statement, res);
328             }
329
330             try {
331                 statement =
332                     connection.prepareStatement(
333                         "SELECT c.name, cu.uri_string FROM jahia_sl2_uri u, jahia_sl2_uri cu, jahia_sl2_parent_binding c WHERE cu.uri_id = c.parent_uuri_id AND c.uri_id = u.uri_id and u.uri_string = ? and u.namespace = ?");
334                 statement.setString(1, uri.toString());
335                 statement.setInt(2, ((JahiaDescriptorsStore)service).getSiteId());
336                 res = statement.executeQuery();
337                 while (res.next()) {
338                     parents.addElement(new ObjectNode.ParentBinding(res.getString(1), res.getString(2)));
339                 }
340             } finally {
341                 close(statement, res);
342             }
343
344             try {
345                 statement =
346                     connection.prepareStatement(
347                         "SELECT lu.uri_string FROM jahia_sl2_uri u, jahia_sl2_uri lu, jahia_sl2_links l WHERE lu.uri_id = l.uri_id AND l.link_to_id = u.uri_id and u.uri_string = ? and u.namespace = ?");
348                 statement.setString(1, uri.toString());
349                 statement.setInt(2, ((JahiaDescriptorsStore)service).getSiteId());
350                 res = statement.executeQuery();
351                 while (res.next()) {
352                     links.addElement(res.getString(1));
353                 }
354             } finally {
355                 close(statement, res);
356             }
357             if (className.equals(LinkNode.class.getName())) {
358                 try {
359                     statement =
360                         connection.prepareStatement(
361                             "SELECT lu.uri_string FROM jahia_sl2_uri u, jahia_sl2_uri lu, jahia_sl2_links l WHERE lu.uri_id = l.link_to_id AND l.uri_id = u.uri_id and u.uri_string = ? and u.namespace = ?");
362                     statement.setString(1, uri.toString());
363                     statement.setInt(2, ((JahiaDescriptorsStore)service).getSiteId());
364                     res = statement.executeQuery();
365                     if (res.next()) {
366                         String JavaDoc linkTarget = res.getString(1);
367                         result = new LinkNode(uri.toString(), children, links, linkTarget);
368                     } else {
369                         result = new LinkNode(uri.toString(), children, links);
370                     }
371                 } finally {
372                     close(statement, res);
373                 }
374             } else {
375                 try {
376                     Class JavaDoc objclass = Class.forName(className);
377                     Class JavaDoc argClasses[] = { String JavaDoc.class, Vector.class, Vector.class, Vector.class };
378                     Object JavaDoc arguments[] = { uri.toString(), children, parents, links };
379                     Constructor JavaDoc constructor = objclass.getConstructor(argClasses);
380                     result = (ObjectNode) constructor.newInstance(arguments);
381                     result.setUri(result.getUuri());
382                 } catch (Exception JavaDoc e) {
383                     throw new ServiceAccessException(service, e);
384                 }
385             }
386         } catch (SQLException JavaDoc e) {
387             throw createException(e, uri.toString());
388         }
389         return result;
390     }
391
392     // Locks
393
public Enumeration enumerateLocks(Connection JavaDoc connection, Uri uri) throws ServiceAccessException {
394
395         Vector lockVector = new Vector();
396         PreparedStatement JavaDoc statement = null;
397         ResultSet JavaDoc res = null;
398         try {
399             statement =
400                 connection.prepareStatement(
401                     "select l.expiration_date, l.is_inheritable, l.is_exclusive, u2.uri_string as lck, u3.uri_string as subject, u4.uri_string as type, l.owner from jahia_sl2_locks l, jahia_sl2_uri u, jahia_sl2_uri u2, jahia_sl2_uri u3, jahia_sl2_uri u4 where l.object_id=u.uri_id and u.uri_string=? and u.namespace=? and l.lock_id=u2.uri_id and l.subject_id=u3.uri_id and l.type_id = u4.uri_id");
402             statement.setString(1, uri.toString());
403             statement.setInt(2, ((JahiaDescriptorsStore)service).getSiteId());
404             res = statement.executeQuery();
405
406             while (res.next()) {
407                 Date expirationDate = null;
408                 try {
409                     Long JavaDoc timeValue = new Long JavaDoc(res.getLong("expiration_date"));
410                     expirationDate = new Date(timeValue.longValue());
411                 } catch (NumberFormatException JavaDoc e) {
412                     getLogger().log(e, LOG_CHANNEL, Logger.WARNING);
413                     expirationDate = new Date();
414                 }
415                 NodeLock lock =
416                     new NodeLock(
417                         res.getString("lck"),
418                         uri.toString(),
419                         res.getString("subject"),
420                         res.getString("type"),
421                         expirationDate,
422                         res.getInt("is_inheritable") == 1,
423                         res.getInt("is_exclusive") == 1,
424             res.getString("owner"));
425
426                 lockVector.addElement(lock);
427             }
428         } catch (SQLException JavaDoc e) {
429             throw createException(e, uri.toString());
430         } finally {
431             close(statement, res);
432         }
433         return lockVector.elements();
434     }
435
436     public synchronized void killLock(Connection JavaDoc connection, Uri uri, NodeLock lock)
437         throws ServiceAccessException, LockTokenNotFoundException {
438         removeLock(connection, uri, lock);
439     }
440
441     public synchronized void putLock(Connection JavaDoc connection, Uri uri, NodeLock lock) throws ServiceAccessException {
442         PreparedStatement JavaDoc statement = null;
443         try {
444             int inheritable = lock.isInheritable() ? 1 : 0;
445             int exclusive = lock.isExclusive() ? 1 : 0;
446             long lockid = assureUriId(connection, lock.getLockId());
447             long objectId = getUriId(connection, lock.getObjectUri());
448             long subjectId = assureUriId(connection, lock.getSubjectUri());
449             long typeId = getUriId(connection, lock.getTypeUri());
450
451             statement =
452                 connection.prepareStatement(
453                     "insert into jahia_sl2_locks (lock_id,object_id,subject_id,type_id,expiration_date,is_inheritable,is_exclusive,owner) values (?,?,?,?,?,?,?,?)");
454             statement.setLong(1, lockid);
455             statement.setLong(2, objectId);
456             statement.setLong(3, subjectId);
457             statement.setLong(4, typeId);
458             statement.setLong(5, lock.getExpirationDate().getTime());
459             statement.setInt(6, inheritable);
460             statement.setInt(7, exclusive);
461             statement.setString(8, lock.getOwnerInfo());
462             statement.execute();
463         } catch (SQLException JavaDoc e) {
464             throw createException(e, uri.toString());
465         } catch (JahiaException e) {
466             throw createException(e, uri.toString());
467         } finally {
468             close(statement);
469         }
470     }
471
472     public synchronized void renewLock(Connection JavaDoc connection, Uri uri, NodeLock lock)
473         throws ServiceAccessException, LockTokenNotFoundException {
474         try {
475             PreparedStatement JavaDoc statement = null;
476             ResultSet JavaDoc rslt = null;
477             try {
478                 statement =
479                     connection.prepareStatement(
480                         "select 1 from jahia_sl2_locks l, jahia_sl2_uri u where l.lock_id = u.uri_id and u.uri_string = ? and u.namespace=?");
481                 statement.setString(1, lock.getLockId());
482                 statement.setInt(2, ((JahiaDescriptorsStore)service).getSiteId());
483                 rslt = statement.executeQuery();
484                 if (rslt.next()) {
485                     removeLock(connection,uri,lock);
486                     putLock(connection, uri, lock);
487                 } else {
488                     throw new LockTokenNotFoundException(lock);
489                 }
490             } finally {
491                 close(statement, rslt);
492             }
493         } catch (SQLException JavaDoc e) {
494             throw createException(e, uri.toString());
495         }
496     }
497
498     public synchronized void removeLock(Connection JavaDoc connection, Uri uri, NodeLock lock)
499         throws ServiceAccessException, LockTokenNotFoundException {
500         PreparedStatement JavaDoc statement = null;
501         try {
502             try {
503                 statement =
504                     connection.prepareStatement(
505                         "delete from jahia_sl2_locks where lock_id = ?");
506                 statement.setLong(1, getUriId(connection, uri.toString()));
507                 statement.executeUpdate();
508             } finally {
509                 close(statement);
510             }
511             try {
512 // statement =
513
// connection.prepareStatement(
514
// "delete jahia_sl2_uri from jahia_sl2_uri, jahia_sl2_locks l where uri_string=? and namespace=?");
515
// statement.setString(1, lock.getLockId());
516
// statement.setInt(2, ((JahiaDescriptorsStore)service).getSiteId());
517
// statement.executeUpdate();
518
} finally {
519                 close(statement);
520             }
521         } catch (SQLException JavaDoc e) {
522             throw createException(e, uri.toString());
523         } catch (JahiaException e) {
524             throw createException(e, uri.toString());
525         }
526     }
527
528     // Permissions
529

530     public Enumeration enumeratePermissions(Connection JavaDoc connection, Uri uri) throws ServiceAccessException {
531         Vector permissions = new Vector();
532         PreparedStatement JavaDoc statement = null;
533         ResultSet JavaDoc res = null;
534         try {
535             statement =
536                 connection.prepareStatement(
537                     "select ou.uri_string, su.uri_string, au.uri_string, p.version_no, p.is_inheritable, p.is_negative from jahia_sl2_permissions p, jahia_sl2_uri ou, jahia_sl2_uri su, jahia_sl2_uri au where p.object_id = ou.uri_id and ou.uri_string = ? and ou.namespace = ? and p.subject_id = su.uri_id and p.action_id = au.uri_id order by succession");
538             statement.setString(1, uri.toString());
539             statement.setInt(2, ((JahiaDescriptorsStore)service).getSiteId());
540             res = statement.executeQuery();
541             while (res.next()) {
542                 String JavaDoc object = res.getString(1);
543                 String JavaDoc subject = res.getString(2);
544                 String JavaDoc action = res.getString(3);
545                 String JavaDoc revision = res.getString(4);
546                 if ("NULL".equals(revision) || "".equals(revision)) {
547                     revision = null;
548                 }
549                 boolean inheritable = (res.getInt(5) == 1);
550                 boolean negative = (res.getInt(6) == 1);
551                 NodePermission permission =
552                     new NodePermission(object, revision, subject, action, inheritable, negative);
553                 permissions.add(permission);
554             }
555         } catch (SQLException JavaDoc e) {
556             throw createException(e, uri.toString());
557         } finally {
558             close(statement, res);
559         }
560         return permissions.elements();
561     }
562
563     public synchronized void grantPermission(Connection JavaDoc connection, Uri uri, NodePermission permission)
564         throws ServiceAccessException {
565         PreparedStatement JavaDoc statement = null;
566         ResultSet JavaDoc res = null;
567         int succession = 0;
568
569         try {
570             // FIXME: This might be useless if insert inserts nothing if insert...select works a i expect
571
// FIXME: What happens, if only revision number, inheritable or negative changes?
572
// FIXME
573
// revokePermission(connection, uri, permission);
574

575             try {
576                 statement =
577                     connection.prepareStatement(
578                         "select max(p.succession) from jahia_sl2_uri ou, jahia_sl2_permissions p where p.object_id = ou.uri_id and ou.uri_string = ? and ou.namespace = ? ");
579                 statement.setString(1, permission.getObjectUri());
580                 statement.setInt(2, ((JahiaDescriptorsStore)service).getSiteId());
581                 res = statement.executeQuery();
582                 res.next();
583                 succession = res.getInt(1) + 1;
584             } finally {
585                 close(statement, res);
586             }
587
588             assureUriId(connection,permission.getSubjectUri());
589             assureUriId(connection,permission.getActionUri());
590
591             try {
592
593                 int inheritable = permission.isInheritable() ? 1 : 0;
594                 int negative = permission.isNegative() ? 1 : 0;
595
596                 statement =
597                     connection.prepareStatement(
598                         "insert into jahia_sl2_permissions (object_id,subject_id,action_id,version_no, is_inheritable,is_negative,succession) select ou.uri_id, su.uri_id, au.uri_id, ?, ?, ?, ? from jahia_sl2_uri ou, jahia_sl2_uri su, jahia_sl2_uri au where ou.uri_string = ? and su.uri_string = ? and au.uri_string = ? and ou.namespace = ? and su.namespace = ? and au.namespace = ?");
599                 statement.setString(1, getRevisionNumberAsString(permission.getRevisionNumber()));
600                 statement.setInt(2, inheritable);
601                 statement.setInt(3, negative);
602                 statement.setInt(4, succession);
603                 statement.setString(5, permission.getObjectUri());
604                 statement.setString(6, permission.getSubjectUri());
605                 statement.setString(7, permission.getActionUri());
606                 statement.setInt(8, ((JahiaDescriptorsStore)service).getSiteId());
607                 statement.setInt(9, ((JahiaDescriptorsStore)service).getSiteId());
608                 statement.setInt(10, ((JahiaDescriptorsStore)service).getSiteId());
609                 if (statement.executeUpdate() != 1) {
610                     String JavaDoc msg = "Failed to insert permission ("
611                     + permission.getObjectUri()
612                     + "," + permission.getSubjectUri()
613                     + "," + permission.getActionUri()
614                     + ")";
615                     getLogger().log(msg,LOG_CHANNEL,Logger.ERROR);
616                 }
617             } finally {
618                 close(statement);
619             }
620         } catch (SQLException JavaDoc e) {
621             throw createException(e, uri.toString());
622         } catch (JahiaException e) {
623             throw createException(e, uri.toString());
624         }
625     }
626
627     public synchronized void revokePermission(Connection JavaDoc connection, Uri uri, NodePermission permission)
628         throws ServiceAccessException {
629         PreparedStatement JavaDoc statement = null;
630         try {
631             statement =
632                 connection.prepareStatement(
633                     "delete from jahia_sl2_permissions where object_id = ? and subject_id = ? and action_id = ?");
634             statement.setLong(1, getUriId(connection, permission.getObjectUri()));
635             statement.setLong(2, getUriId(connection, permission.getSubjectUri()));
636             statement.setLong(3, getUriId(connection, permission.getActionUri()));
637 // statement.setString(4, revisionNumber.toString());
638
statement.executeUpdate();
639         } catch (SQLException JavaDoc e) {
640             throw createException(e, uri.toString());
641         } catch (JahiaException e) {
642             throw createException(e, uri.toString());
643         } finally {
644             close(statement);
645         }
646     }
647
648     public void revokePermissions(Connection JavaDoc connection, Uri uri) throws ServiceAccessException {
649         PreparedStatement JavaDoc statement = null;
650         try {
651             statement =
652                 connection.prepareStatement(
653                     "delete from jahia_sl2_permissions where object_id = ?");
654             statement.setLong(1, getUriId(connection, uri.toString()));
655             statement.executeUpdate();
656         } catch (SQLException JavaDoc e) {
657             throw createException(e, uri.toString());
658         } catch (JahiaException e) {
659             throw createException(e, uri.toString());
660         } finally {
661             close(statement);
662         }
663     }
664
665     public synchronized void createRevisionDescriptor(Connection JavaDoc connection, Uri uri, NodeRevisionDescriptor revisionDescriptor)
666         throws ServiceAccessException {
667
668         PreparedStatement JavaDoc statement = null;
669         try {
670
671             assureVersionInfo(connection, uri, revisionDescriptor);
672             createVersionLabels(connection, uri, revisionDescriptor);
673             for (Enumeration properties = revisionDescriptor.enumerateProperties(); properties.hasMoreElements();) {
674                 try {
675                     NodeProperty property = (NodeProperty) properties.nextElement();
676                     long versionId = getVersionId(connection, uri.toString(), revisionDescriptor.getRevisionNumber().toString());
677                     statement =
678                         connection.prepareStatement(
679                             "insert into jahia_sl2_properties (version_id,property_namespace,property_name,property_value,property_type,is_protected) values (?, ?, ?, ?, ?, ?) ");
680                     int protectedProperty = property.isProtected() ? 1 : 0;
681                     statement.setLong(1, versionId);
682                     statement.setString(2, property.getNamespace());
683                     statement.setString(3, property.getName());
684                     statement.setString(4, property.getValue().toString());
685                     statement.setString(5, property.getType());
686                     statement.setInt(6, protectedProperty);
687                     statement.executeUpdate();
688                 } finally {
689                     close(statement);
690                 }
691             }
692         } catch (JahiaException e) {
693             throw createException(e, uri.toString());
694         } catch (SQLException JavaDoc e) {
695             throw createException(e, uri.toString());
696         }
697     }
698
699     public synchronized void removeRevisionDescriptor(Connection JavaDoc connection, Uri uri, NodeRevisionNumber revisionNumber)
700         throws ServiceAccessException {
701         PreparedStatement JavaDoc statement = null;
702         try {
703             removeVersionLabels(connection, uri, revisionNumber);
704             try {
705                 statement =
706                     connection.prepareStatement("delete from jahia_sl2_properties where version_id = ?");
707                 statement.setLong(1, getVersionId(connection, uri.toString(), revisionNumber.toString()));
708                 statement.executeUpdate();
709             } finally {
710                 close(statement);
711             }
712         } catch (SQLException JavaDoc e) {
713             throw createException(e, uri.toString());
714         } catch (JahiaException e) {
715             throw createException(e, uri.toString());
716         }
717     }
718
719     public synchronized void removeRevisionDescriptors(Connection JavaDoc connection, Uri uri) throws ServiceAccessException {
720         PreparedStatement JavaDoc statement = null;
721         PreparedStatement JavaDoc select = null;
722         ResultSet JavaDoc s = null;
723         try {
724             statement = connection.prepareStatement( "delete from jahia_sl2_version_preds where version_id = ?");
725             select = connection.prepareStatement( "select vh.version_id from jahia_sl2_version_history vh where vh.uri_id=?");
726             select.setLong(1, getUriId(connection, uri.toString()));
727             s = select.executeQuery();
728             while (s.next()) {
729                 statement.setLong(1, s.getLong(1));
730                 statement.executeUpdate();
731             }
732         } catch (JahiaException e) {
733             throw createException(e, uri.toString());
734         } catch (SQLException JavaDoc e) {
735             throw createException(e, uri.toString());
736         } finally {
737             close(statement);
738             close(select, s);
739         }
740     }
741
742     public NodeRevisionDescriptor retrieveRevisionDescriptor(
743         Connection JavaDoc connection,
744         Uri uri,
745         NodeRevisionNumber revisionNumber)
746         throws ServiceAccessException, RevisionDescriptorNotFoundException {
747         NodeRevisionDescriptor revisionDescriptor = null;
748         if (revisionNumber == null)
749             throw new RevisionDescriptorNotFoundException(uri.toString());
750         try {
751             PreparedStatement JavaDoc statement = null;
752             ResultSet JavaDoc res = null;
753
754             String JavaDoc branch = null;
755             Vector labels = new Vector();
756             List properties = new ArrayList();
757             long versionId = 0;
758             try {
759                 statement =
760                     connection.prepareStatement(
761                         "select vh.version_id, b.branch_string from jahia_sl2_version_history vh, jahia_sl2_branch b, jahia_sl2_uri u where vh.uri_id = u.uri_id and u.uri_string = ? and u.namespace=? and b.branch_id = vh.branch_id and vh.revision_no = ?");
762                 statement.setString(1, uri.toString());
763                 statement.setInt(2, ((JahiaDescriptorsStore)service).getSiteId());
764                 statement.setString(3, revisionNumber.toString());
765                 res = statement.executeQuery();
766                 if (res.next()) {
767                     versionId = res.getLong(1);
768                     branch = res.getString(2);
769                 } else {
770                     throw new RevisionDescriptorNotFoundException(uri.toString());
771                 }
772             } finally {
773                 close(statement, res);
774             }
775             try {
776                 statement =
777                     connection.prepareStatement(
778                         "select l.label_string from jahia_sl2_version_labels vl, jahia_sl2_label l where vl.version_id = ? and l.label_id = vl.label_id");
779                 statement.setLong(1, versionId);
780                 res = statement.executeQuery();
781                 while (res.next()) {
782                     labels.addElement(res.getString(1));
783                 }
784             } finally {
785                 close(statement, res);
786             }
787             try {
788                 statement =
789                     connection.prepareStatement(
790                         "select property_name, property_namespace, property_value, property_type, is_protected from jahia_sl2_properties where version_id = ?");
791                 statement.setLong(1, versionId);
792                 res = statement.executeQuery();
793                 while (res.next()) {
794                     String JavaDoc propertyName = res.getString(1);
795                     String JavaDoc propertyNamespace = res.getString(2);
796                     NodeProperty property =
797                         new NodeProperty(
798                             propertyName,
799                             res.getString(3),
800                             propertyNamespace,
801                             res.getString(4),
802                             res.getInt(5) == 1);
803                     properties.add(property);
804                 }
805             } finally {
806                 close(statement, res);
807             }
808             revisionDescriptor = new NodeRevisionDescriptor(revisionNumber, branch, labels, properties);
809         } catch (SQLException JavaDoc e) {
810             throw createException(e, uri.toString());
811         }
812         return revisionDescriptor;
813     }
814
815     public NodeRevisionDescriptors retrieveRevisionDescriptors(Connection JavaDoc connection, Uri uri)
816         throws ServiceAccessException, RevisionDescriptorNotFoundException {
817
818         NodeRevisionDescriptors revisionDescriptors = null;
819         PreparedStatement JavaDoc statement = null;
820         ResultSet JavaDoc res = null;
821         try {
822             NodeRevisionNumber initialRevision = new NodeRevisionNumber();
823             // FIXME: Some code might be lost: workingRevisions is not filled with values
824
Hashtable workingRevisions = new Hashtable();
825             Hashtable latestRevisionNumbers = new Hashtable();
826             Hashtable branches = new Hashtable();
827             Vector allRevisions = new Vector();
828
829             boolean isVersioned = false;
830             // check if versioned
831
try {
832                 statement =
833                     connection.prepareStatement(
834                         "select is_versioned from jahia_sl2_version v, jahia_sl2_uri u where v.uri_id = u.uri_id and u.uri_string = ? and u.namespace=?");
835                 statement.setString(1, uri.toString());
836                 statement.setInt(2, ((JahiaDescriptorsStore)service).getSiteId());
837                 res = statement.executeQuery();
838                 if (res.next()) {
839                     isVersioned = (res.getInt("is_versioned") == 1);
840                 } else {
841                     throw new RevisionDescriptorNotFoundException(uri.toString());
842                 }
843             } finally {
844                 close(statement, res);
845             }
846             // retrieve revision numbers
847
try {
848                 statement =
849                     connection
850                         .prepareStatement(
851                             "select vh.revision_no, b.branch_string from jahia_sl2_version_history vh, jahia_sl2_branch b, jahia_sl2_uri u where vh.branch_id = b.branch_id and vh.uri_id = u.uri_id and u.uri_string = ? and u.namespace=? ");
852 // + convertRevisionNumberToComparable("vh.revision_no"));
853
// todo replace sql sort by java sort
854
statement.setString(1, uri.toString());
855                 statement.setInt(2, ((JahiaDescriptorsStore)service).getSiteId());
856                 res = statement.executeQuery();
857                 while (res.next()) {
858                     NodeRevisionNumber revisionNumber = new NodeRevisionNumber(res.getString(1));
859                     allRevisions.add(revisionNumber); // will be used to get revisions successor
860
latestRevisionNumbers.put(res.getString(2), revisionNumber); // store the lastest revision / branch
861
}
862             } finally {
863                 close(statement, res);
864             }
865
866             for (Enumeration e = allRevisions.elements(); e.hasMoreElements();) {
867                 NodeRevisionNumber revisionNumber = (NodeRevisionNumber) e.nextElement();
868                 // get successors
869
try {
870                     statement =
871                         connection.prepareStatement(
872                             "select pvh.revision_no from jahia_sl2_version_history vh, jahia_sl2_version_history pvh, jahia_sl2_version_preds vp, jahia_sl2_uri u where pvh.version_id = vp.version_id and vp.version_id = vh.version_id and vh.uri_id = u.uri_id and u.uri_string = ? and u.namespace=? and vh.revision_no = ?");
873                     statement.setString(1, uri.toString());
874                     statement.setInt(2, ((JahiaDescriptorsStore)service).getSiteId());
875                     statement.setString(3, revisionNumber.toString());
876                     res = statement.executeQuery();
877                     Vector predecessors = new Vector();
878                     while (res.next()) {
879                         predecessors.add(new NodeRevisionNumber(res.getString(1)));
880                     }
881                     // XXX it is important to call ctor with String, otherwise it does an increment in minor number :(
882
branches.put(new NodeRevisionNumber(revisionNumber.toString()), predecessors);
883                 } finally {
884                     close(statement, res);
885                 }
886             }
887             revisionDescriptors =
888                 new NodeRevisionDescriptors(
889                     uri.toString(),
890                     initialRevision,
891                     workingRevisions,
892                     latestRevisionNumbers,
893                     branches,
894                     isVersioned);
895         } catch (SQLException JavaDoc e) {
896             throw createException(e, uri.toString());
897         }
898         return revisionDescriptors;
899     }
900
901     public synchronized void createRevisionDescriptors(Connection JavaDoc connection, Uri uri, NodeRevisionDescriptors revisionDescriptors)
902         throws ServiceAccessException {
903
904         PreparedStatement JavaDoc statement = null;
905         ResultSet JavaDoc res = null;
906         try {
907             long uriid = getUriId(connection,uri.toString());
908             int isVersioned = 0;
909             if (revisionDescriptors.isVersioned())
910                 isVersioned = 1;
911             boolean revisionExists;
912             try {
913                 statement =
914                     connection.prepareStatement(
915                         "SELECT 1 FROM jahia_sl2_version v, jahia_sl2_uri u WHERE v.uri_id = u.uri_id and u.uri_string = ? and u.namespace=?");
916                 statement.setString(1, uri.toString());
917                 statement.setInt(2, ((JahiaDescriptorsStore)service).getSiteId());
918                 res = statement.executeQuery();
919                 revisionExists = res.next();
920             } finally {
921                 close(statement, res);
922             }
923             if (!revisionExists) {
924                 try {
925                     statement =
926                         connection.prepareStatement(
927                             "insert into jahia_sl2_version (uri_id, is_versioned) values (?, ?)");
928                     statement.setLong(1, uriid);
929                     statement.setInt(2, isVersioned);
930                     statement.executeUpdate();
931                 } finally {
932                     close(statement, res);
933                 }
934             }
935             boolean versionHistoryExists;
936             NodeRevisionNumber versionNumber = revisionDescriptors.hasRevisions() ? revisionDescriptors.getLatestRevision() : new NodeRevisionNumber();
937             try {
938                 statement =
939                     connection.prepareStatement(
940                         "SELECT 1 FROM jahia_sl2_version_history vh, jahia_sl2_uri u WHERE vh.uri_id = u.uri_id and u.uri_string = ? and u.namespace=? and revision_no = ?");
941                 statement.setString(1, uri.toString());
942                 statement.setInt(2, ((JahiaDescriptorsStore)service).getSiteId());
943                 statement.setString(3, versionNumber.toString());
944                 res = statement.executeQuery();
945                 versionHistoryExists = res.next();
946             } finally {
947                 close(statement, res);
948             }
949             if (!versionHistoryExists && revisionDescriptors.getLatestRevision() != null) {
950                 try {
951                     long branchId = getBranchId(connection, NodeRevisionDescriptors.MAIN_BRANCH);
952                     statement =
953                         connection.prepareStatement(
954                             "insert into jahia_sl2_version_history (version_id, uri_id, branch_id, revision_no) values (?,?,?,?)");
955                     statement.setInt(1, ServicesRegistry.getInstance().getJahiaIncrementorsDBService().autoIncrement("jahia_sl2_version_history"));
956                     statement.setLong(2, uriid);
957                     statement.setLong(3, branchId);
958                     statement.setString(4, getRevisionNumberAsString(versionNumber));
959                     // FIXME: Create new revisions on the main branch???
960
statement.executeUpdate();
961                 } finally {
962                     close(statement, res);
963                 }
964             }
965
966
967             // Add revision successors
968
Enumeration revisionNumbers = revisionDescriptors.enumerateRevisionNumbers();
969             while (revisionNumbers.hasMoreElements()) {
970                 NodeRevisionNumber nodeRevisionNumber = (NodeRevisionNumber) revisionNumbers.nextElement();
971
972                 Enumeration successors = revisionDescriptors.getSuccessors(nodeRevisionNumber);
973                 while (successors.hasMoreElements())
974                 {
975                     try {
976                         NodeRevisionNumber successor = (NodeRevisionNumber) successors.nextElement();
977
978                         statement =
979                             connection.prepareStatement(
980                                     "insert into jahia_sl2_version_preds (version_id, predecessor_id) " +
981                                     " select vr.version_id, suc.version_id" +
982                                     " FROM jahia_sl2_uri uri, jahia_sl2_version_history vr, jahia_sl2_version_history suc " +
983                                     " where vr.uri_id = uri.uri_id " +
984                                     " and suc.uri_id = uri.uri_id " +
985                                     " and uri.uri_string = ? " +
986                                     " and uri.namespace = ? " +
987                                     " and vr.revision_no = ? " +
988                                     " and suc.revision_no = ? " );
989
990                         statement.setString(1, uri.toString());
991                         statement.setInt(2, ((JahiaDescriptorsStore)service).getSiteId());
992                         statement.setString(3, nodeRevisionNumber.toString());
993                         statement.setString(4, successor.toString());
994                         statement.executeUpdate();
995                     } finally {
996                         close(statement);
997                     }
998                 }
999             }
1000            getLogger().log(
1001                revisionDescriptors.getOriginalUri() + revisionDescriptors.getInitialRevision(),
1002                LOG_CHANNEL,
1003                Logger.DEBUG);
1004        } catch (SQLException JavaDoc e) {
1005            throw createException(e, uri.toString());
1006        } catch (JahiaException e) {
1007            throw createException(e, uri.toString());
1008        }
1009    }
1010
1011    protected long getVersionId(Connection JavaDoc connection, String JavaDoc uri, String JavaDoc revNo)
1012            throws SQLException JavaDoc, JahiaException {
1013        PreparedStatement JavaDoc statement = null;
1014        ResultSet JavaDoc res = null;
1015        try {
1016            statement =
1017                connection.prepareStatement(
1018                    "select vh.version_id from jahia_sl2_version_history vh, jahia_sl2_uri u where vh.uri_id = u.uri_id and u.uri_string = ? and u.namespace=? and revision_no = ?");
1019            statement.setString(1, uri);
1020            statement.setInt(2, ((JahiaDescriptorsStore)service).getSiteId());
1021            statement.setString(3, revNo);
1022            res = statement.executeQuery();
1023            if (res.next()) {
1024                long id = res.getLong(1);
1025                return id;
1026            }
1027        } finally {
1028            close(statement, res);
1029        }
1030        return -1;
1031    }
1032    protected void assureVersionInfo(Connection JavaDoc connection, Uri uri, NodeRevisionDescriptor revisionDescriptor)
1033        throws SQLException JavaDoc, JahiaException {
1034        PreparedStatement JavaDoc statement = null;
1035        ResultSet JavaDoc res = null;
1036        long uriid = getUriId(connection, uri.toString());
1037        boolean revisionExists;
1038        try {
1039            statement =
1040                connection.prepareStatement(
1041                    "select 1 from jahia_sl2_version v, jahia_sl2_uri u where v.uri_id = u.uri_id and u.uri_string = ? and u.namespace=?");
1042            statement.setString(1, uri.toString());
1043            statement.setInt(2, ((JahiaDescriptorsStore)service).getSiteId());
1044            res = statement.executeQuery();
1045            revisionExists = res.next();
1046        } finally {
1047            close(statement, res);
1048        }
1049        // FIXME: Is it true, that the default for is_versioned is 0 ??
1050
if (!revisionExists) {
1051            try {
1052                statement =
1053                    connection.prepareStatement(
1054                        "insert into jahia_sl2_version (uri_id, is_versioned) values (?, ?)");
1055                statement.setLong(1, uriid);
1056                statement.setInt(2, 0);
1057                statement.executeUpdate();
1058            } finally {
1059                close(statement);
1060            }
1061        }
1062        boolean versionHistoryExists;
1063        try {
1064            statement =
1065                connection.prepareStatement(
1066                    "select 1 from jahia_sl2_version_history vh, jahia_sl2_uri u where vh.uri_id = u.uri_id and u.uri_string = ? and u.namespace=? and revision_no = ?");
1067            statement.setString(1, uri.toString());
1068            statement.setInt(2, ((JahiaDescriptorsStore)service).getSiteId());
1069            statement.setString(3, revisionDescriptor.getRevisionNumber().toString());
1070            res = statement.executeQuery();
1071            versionHistoryExists = res.next();
1072        } finally {
1073            close(statement, res);
1074        }
1075        if (!versionHistoryExists) {
1076            long branchId = assureBranchId(connection, revisionDescriptor.getBranchName());
1077            try {
1078                statement =
1079                    connection.prepareStatement(
1080                        "insert into jahia_sl2_version_history (version_id, uri_id, branch_id, revision_no) values (?, ?, ?, ?)");
1081                statement.setInt(1, ServicesRegistry.getInstance().getJahiaIncrementorsDBService().autoIncrement("jahia_sl2_version_history"));
1082                statement.setLong(2, uriid);
1083                statement.setLong(3, branchId);
1084                statement.setString(4, getRevisionNumberAsString(revisionDescriptor.getRevisionNumber()));
1085                statement.executeUpdate();
1086            } finally {
1087                close(statement);
1088            }
1089        }
1090    }
1091
1092
1093    public synchronized void storeRevisionDescriptor(Connection JavaDoc connection, Uri uri, NodeRevisionDescriptor revisionDescriptor)
1094        throws ServiceAccessException, RevisionDescriptorNotFoundException {
1095        removeRevisionDescriptor(connection, uri, revisionDescriptor.getRevisionNumber());
1096        createRevisionDescriptor(connection, uri, revisionDescriptor);
1097    }
1098
1099    public synchronized void storeRevisionDescriptors(Connection JavaDoc connection, Uri uri, NodeRevisionDescriptors revisionDescriptors)
1100        throws ServiceAccessException, RevisionDescriptorNotFoundException {
1101        removeRevisionDescriptors(connection, uri);
1102        createRevisionDescriptors(connection, uri, revisionDescriptors);
1103    }
1104
1105    protected synchronized void removeVersionLabels(Connection JavaDoc connection, Uri uri, NodeRevisionNumber revisionNumber)
1106            throws SQLException JavaDoc, JahiaException {
1107        PreparedStatement JavaDoc statement = null;
1108        try {
1109            statement = connection
1110                    .prepareStatement("delete from jahia_sl2_version_labels where version_id = ?");
1111            statement.setLong(1, getVersionId(connection, uri.toString(), revisionNumber.toString()));
1112            statement.executeUpdate();
1113        } finally {
1114            close(statement);
1115        }
1116    }
1117
1118    protected synchronized void createVersionLabels(Connection JavaDoc connection, Uri uri, NodeRevisionDescriptor revisionDescriptor)
1119            throws SQLException JavaDoc, JahiaException {
1120
1121        PreparedStatement JavaDoc statement = null;
1122        for (Enumeration labels = revisionDescriptor.enumerateLabels(); labels.hasMoreElements();) {
1123            long labelId = assureLabelId(connection, (String JavaDoc) labels.nextElement());
1124            try {
1125                statement = connection
1126                        .prepareStatement("insert into jahia_sl2_version_labels (version_id, label_id) select version_id, ? from jahia_sl2_version_history vh, jahia_sl2_uri u where vh.uri_id = u.uri_id and u.uri_string = ? and u.namespace=? and vh.revision_no = ?");
1127                statement.setLong(1, labelId);
1128                statement.setString(2, uri.toString());
1129                statement.setInt(3, ((JahiaDescriptorsStore)service).getSiteId());
1130                statement.setString(4, revisionDescriptor.getRevisionNumber().toString());
1131                statement.executeUpdate();
1132            } finally {
1133                close(statement);
1134            }
1135        }
1136    }
1137
1138    protected long getUriId(Connection JavaDoc connection, String JavaDoc uri) throws JahiaException, SQLException JavaDoc {
1139        PreparedStatement JavaDoc statement = null;
1140        ResultSet JavaDoc res = null;
1141        try {
1142            statement = connection.prepareStatement("select uri_id from jahia_sl2_uri where uri_string=? and namespace=?");
1143            statement.setString(1, uri);
1144            statement.setInt(2, ((JahiaDescriptorsStore)service).getSiteId());
1145                    res = statement.executeQuery();
1146            if (res.next()) {
1147                long id = res.getLong(1);
1148                return id;
1149            }
1150        } finally {
1151            close(statement, res);
1152        }
1153        return -1;
1154    }
1155
1156    protected long assureUriId(Connection JavaDoc connection, String JavaDoc uri) throws JahiaException, SQLException JavaDoc {
1157        PreparedStatement JavaDoc statement = null;
1158        ResultSet JavaDoc res = null;
1159        try {
1160            statement = connection.prepareStatement("select uri_id from jahia_sl2_uri where uri_string=? and namespace=?");
1161            statement.setString(1, uri);
1162            statement.setInt(2, ((JahiaDescriptorsStore)service).getSiteId());
1163                    res = statement.executeQuery();
1164            if (res.next()) {
1165                long id = res.getLong(1);
1166                return id;
1167            }
1168        } finally {
1169            close(statement, res);
1170        }
1171        try {
1172            statement = connection.prepareStatement("insert into jahia_sl2_uri (uri_id,uri_string,namespace) values (?,?,?)");
1173            statement.setInt(1, ServicesRegistry.getInstance().getJahiaIncrementorsDBService().autoIncrement("jahia_sl2_uri"));
1174            statement.setString(2, uri);
1175            statement.setInt(3, ((JahiaDescriptorsStore)service).getSiteId());
1176            statement.executeUpdate();
1177        } finally {
1178            close(statement);
1179        }
1180        return assureUriId(connection, uri);
1181    }
1182
1183    protected long getBranchId(Connection JavaDoc connection, String JavaDoc branch) throws JahiaException, SQLException JavaDoc {
1184        PreparedStatement JavaDoc statement = null;
1185        ResultSet JavaDoc res = null;
1186        try {
1187            statement = connection.prepareStatement("select branch_id from jahia_sl2_branch where branch_string=? and namespace=?");
1188            statement.setString(1, branch);
1189            statement.setInt(2, ((JahiaDescriptorsStore)service).getSiteId());
1190            res = statement.executeQuery();
1191            if (res.next()) {
1192                long id = res.getLong(1);
1193                return id;
1194            }
1195        } finally {
1196            close(statement, res);
1197        }
1198        return -1;
1199    }
1200
1201    protected long assureBranchId(Connection JavaDoc connection, String JavaDoc branch) throws JahiaException, SQLException JavaDoc {
1202        PreparedStatement JavaDoc statement = null;
1203        ResultSet JavaDoc res = null;
1204        try {
1205            statement = connection.prepareStatement("select branch_id from jahia_sl2_branch where branch_string=? and namespace=?");
1206            statement.setString(1, branch);
1207            statement.setInt(2, ((JahiaDescriptorsStore)service).getSiteId());
1208            res = statement.executeQuery();
1209            if (res.next()) {
1210                long id = res.getLong(1);
1211                return id;
1212            }
1213        } finally {
1214            close(statement, res);
1215        }
1216        try {
1217            statement = connection.prepareStatement("insert into jahia_sl2_branch (branch_id,branch_string,namespace) values (?,?,?)");
1218            statement.setInt(1, ServicesRegistry.getInstance().getJahiaIncrementorsDBService().autoIncrement("jahia_sl2_branch"));
1219            statement.setString(2, branch);
1220            statement.setInt(3, ((JahiaDescriptorsStore)service).getSiteId());
1221            statement.executeUpdate();
1222        } finally {
1223            close(statement);
1224        }
1225        return assureBranchId(connection, branch);
1226    }
1227
1228    protected long assureLabelId(Connection JavaDoc connection, String JavaDoc label) throws JahiaException, SQLException JavaDoc {
1229        PreparedStatement JavaDoc statement = null;
1230        ResultSet JavaDoc res = null;
1231        try {
1232            statement = connection.prepareStatement("select label_id from jahia_sl2_label where label_string=? and namespace=?");
1233            statement.setString(1, label);
1234            statement.setInt(2, ((JahiaDescriptorsStore)service).getSiteId());
1235            res = statement.executeQuery();
1236            if (res.next()) {
1237                long id = res.getLong(1);
1238                return id;
1239            }
1240        } finally {
1241            close(statement, res);
1242        }
1243        try {
1244            statement = connection.prepareStatement("insert into jahia_sl2_label (label_id,label_string,namespace) values (?,?,?)");
1245            statement.setInt(1, ServicesRegistry.getInstance().getJahiaIncrementorsDBService().autoIncrement("jahia_sl2_label"));
1246            statement.setString(2, label);
1247            statement.setInt(3, ((JahiaDescriptorsStore)service).getSiteId());
1248            statement.executeUpdate();
1249        } finally {
1250            close(statement);
1251        }
1252        return assureLabelId(connection, label);
1253    }
1254
1255    protected void clearBinding(Connection JavaDoc connection, long uriId)
1256        throws ServiceAccessException, ObjectNotFoundException, SQLException JavaDoc {
1257        PreparedStatement JavaDoc statement = null;
1258
1259        // clear this uri from having bindings and being bound
1260

1261        try {
1262            statement =
1263                connection.prepareStatement(
1264                    "delete from jahia_sl2_binding where uri_id = ?");
1265            statement.setLong(1, uriId);
1266            statement.executeUpdate();
1267        } finally {
1268            close(statement);
1269        }
1270
1271        try {
1272            statement =
1273                connection.prepareStatement(
1274                    "delete from jahia_sl2_parent_binding where uri_id = ?");
1275            statement.setLong(1, uriId);
1276            statement.executeUpdate();
1277        } finally {
1278            close(statement);
1279        }
1280    }
1281
1282    // null means permission is valid for all revisions
1283
protected String JavaDoc getRevisionNumberAsString(NodeRevisionNumber revisionNumber) {
1284        return revisionNumber != null ? revisionNumber.toString() : null;
1285    }
1286
1287    protected String JavaDoc convertRevisionNumberToComparable(String JavaDoc revisioNumber) {
1288        return "convert(numeric, SUBSTRING("+revisioNumber+",1,charindex('.',"+revisioNumber+"))), convert(numeric, SUBSTRING("+revisioNumber+",charindex('.',"+revisioNumber+")+1,100))";
1289    }
1290
1291    protected void close(PreparedStatement JavaDoc statement) {
1292        try {
1293            if (statement != null) {
1294                statement.close();
1295            }
1296        } catch (SQLException JavaDoc e) {
1297            getLogger().log(e, LOG_CHANNEL, Logger.WARNING);
1298        }
1299    }
1300
1301    protected void close(PreparedStatement JavaDoc statement, ResultSet JavaDoc resultSet) {
1302        try {
1303            if (resultSet != null) {
1304                resultSet.close();
1305            }
1306        } catch (SQLException JavaDoc e) {
1307            getLogger().log(e, LOG_CHANNEL, Logger.WARNING);
1308        } finally {
1309            try {
1310                if (statement != null) {
1311                    statement.close();
1312                }
1313            } catch (SQLException JavaDoc e) {
1314                getLogger().log(e, LOG_CHANNEL, Logger.WARNING);
1315            }
1316        }
1317    }
1318
1319    // overload this method to have a more detailed error handling
1320
protected ServiceAccessException createException(SQLException JavaDoc e, String JavaDoc uri) {
1321        getLogger().log(
1322            "SQL error " + e.getErrorCode() + " on " + uri + ": " + e.getMessage(),
1323            LOG_CHANNEL,
1324            Logger.ERROR);
1325        return new ServiceAccessException(service, e);
1326    }
1327
1328    protected ServiceAccessException createException(JahiaException e, String JavaDoc uri) {
1329        getLogger().log(
1330            "Jahia error " + e.getErrorCode() + " on " + uri + ": " + e.getMessage(),
1331            LOG_CHANNEL,
1332            Logger.ERROR);
1333        return new ServiceAccessException(service, e);
1334    }
1335
1336
1337    public NodeRevisionContent retrieveRevisionContent(Connection JavaDoc conn, Uri uri, NodeRevisionDescriptor revisionDescriptor, boolean temporaryConnection) throws ServiceAccessException, RevisionNotFoundException {
1338        throw new ServiceAccessException(service,"Content not supported");
1339    }
1340
1341    public void createRevisionContent(Connection JavaDoc conn, Uri uri, NodeRevisionDescriptor revisionDescriptor, NodeRevisionContent revisionContent) throws ServiceAccessException, RevisionAlreadyExistException {
1342        throw new ServiceAccessException(service,"Content not supported");
1343    }
1344
1345    public void storeRevisionContent(Connection JavaDoc conn, Uri uri, NodeRevisionDescriptor revisionDescriptor, NodeRevisionContent revisionContent) throws ServiceAccessException, RevisionNotFoundException {
1346        throw new ServiceAccessException(service,"Content not supported");
1347    }
1348
1349    public void removeRevisionContent(Connection JavaDoc conn, Uri uri, NodeRevisionDescriptor revisionDescriptor) throws ServiceAccessException {
1350        throw new ServiceAccessException(service,"Content not supported");
1351    }
1352
1353}
1354
Popular Tags