KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/stores/org/apache/slide/store/impl/rdbms/CommonRDBMSAdapter.java,v 1.4.2.3 2004/10/30 16:02:30 unico Exp $
3  * $Revision: 1.4.2.3 $
4  * $Date: 2004/10/30 16:02:30 $
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 java.io.*;
27 import java.sql.Connection JavaDoc;
28 import java.sql.PreparedStatement JavaDoc;
29 import java.sql.ResultSet JavaDoc;
30 import java.sql.SQLException JavaDoc;
31
32 import org.apache.slide.common.Service;
33 import org.apache.slide.common.ServiceAccessException;
34 import org.apache.slide.common.Uri;
35 import org.apache.slide.content.NodeRevisionDescriptor;
36 import org.apache.slide.content.NodeRevisionNumber;
37 import org.apache.slide.content.NodeRevisionContent;
38 import org.apache.slide.lock.LockTokenNotFoundException;
39 import org.apache.slide.lock.NodeLock;
40 import org.apache.slide.security.NodePermission;
41 import org.apache.slide.structure.ObjectNode;
42 import org.apache.slide.structure.ObjectNotFoundException;
43 import org.apache.slide.util.logger.Logger;
44
45
46 /**
47  * This adapter has code that has been ported from OracleRDBMSAdapter so that it can be
48  * resued by both OracleRDBMSAdapter and DB2RDBMSAdapter.
49  *
50  */

51 public class CommonRDBMSAdapter extends StandardRDBMSAdapter {
52
53     // Constructor
54

55     public CommonRDBMSAdapter(Service service, Logger logger) {
56         super(service, logger);
57     }
58
59
60
61     public void removeObject(Connection JavaDoc connection, Uri uri, ObjectNode object)
62         throws ServiceAccessException, ObjectNotFoundException
63     {
64         PreparedStatement JavaDoc statement = null;
65         try {
66             clearBinding(connection, uri);
67
68             // delete links
69
try {
70                 statement =
71                     connection.prepareStatement(
72                         "delete from LINKS l where l.URI_ID in (" +
73                         "select u.URI_ID from URI u where u.URI_STRING = ?)");
74                 statement.setString(1, uri.toString());
75                 statement.executeUpdate();
76             } finally {
77                 close(statement);
78             }
79             // delete version history
80
// FIXME: Is this true??? Should the version history be removed if the object is removed???
81
try {
82                 statement =
83                     connection.prepareStatement(
84                         "delete from VERSION_HISTORY vh where vh.URI_ID in (" +
85                         "select u.URI_ID from URI u where u.URI_STRING = ?)");
86                 statement.setString(1, uri.toString());
87                 statement.executeUpdate();
88             } finally {
89                 close(statement);
90             }
91             // delete version
92
try {
93                 statement =
94                     connection.prepareStatement(
95                         "delete from VERSION v where v.URI_ID in (" +
96                         "select u.URI_ID from URI u where u.URI_STRING = ?)");
97                 statement.setString(1, uri.toString());
98                 statement.executeUpdate();
99             } finally {
100                 close(statement);
101             }
102             // delete the object itself
103
try {
104                 statement =
105                     connection.prepareStatement(
106                         "delete from OBJECT o where o.URI_ID in (" +
107                         "select u.URI_ID from URI u where u.URI_STRING = ?)");
108                 statement.setString(1, uri.toString());
109                 statement.executeUpdate();
110             } finally {
111                 close(statement);
112             }
113             // finally delete the uri
114
try {
115                 statement = connection.prepareStatement("delete from URI where URI_STRING = ?");
116                 statement.setString(1, uri.toString());
117                 statement.executeUpdate();
118             } finally {
119                 close(statement);
120             }
121         } catch (SQLException JavaDoc e) {
122             throw createException(e, uri.toString());
123
124         }
125     }
126
127     public void removeRevisionContent(Connection JavaDoc connection, Uri uri, NodeRevisionDescriptor revisionDescriptor)
128         throws ServiceAccessException {
129         try {
130             PreparedStatement JavaDoc statement = null;
131             try {
132                 statement =
133                     connection.prepareStatement(
134                         "delete from VERSION_CONTENT vc where vc.VERSION_ID in (" +
135                         "select vh.VERSION_ID from VERSION_HISTORY vh, URI u where vh.REVISION_NO = ? and vh.URI_ID=u.URI_ID AND u.URI_STRING=?)");
136                 statement.setString(1, revisionDescriptor.getRevisionNumber().toString());
137                 statement.setString(2, uri.toString());
138                 statement.executeUpdate();
139             } finally {
140                 close(statement);
141             }
142         } catch (SQLException JavaDoc e) {
143             throw createException(e, uri.toString());
144         }
145     }
146
147     public void removeRevisionDescriptors(Connection JavaDoc connection, Uri uri) throws ServiceAccessException {
148         PreparedStatement JavaDoc statement = null;
149         try {
150             statement =
151                 connection.prepareStatement(
152                 "delete from VERSION_PREDS vp where vp.VERSION_ID in (" +
153                 "select vh.VERSION_ID from VERSION_HISTORY vh, URI u where vh.URI_ID = u.URI_ID and u.URI_STRING = ?)");
154             statement.setString(1, uri.toString());
155             statement.executeUpdate();
156         } catch (SQLException JavaDoc e) {
157             throw createException(e, uri.toString());
158         } finally {
159             close(statement);
160         }
161     }
162
163     public void removeRevisionDescriptor(Connection JavaDoc connection, Uri uri, NodeRevisionNumber revisionNumber)
164         throws ServiceAccessException
165     {
166         PreparedStatement JavaDoc statement = null;
167         try {
168             try {
169                 statement =
170                     connection.prepareStatement(
171                         "delete from VERSION_LABELS vl where vl.VERSION_ID in (" +
172                         "select vh.VERSION_ID from VERSION_HISTORY vh, URI u where vh.REVISION_NO = ? and vh.URI_ID = u.URI_ID AND u.URI_STRING = ?)");
173                 statement.setString(1, revisionNumber.toString());
174                 statement.setString(2, uri.toString());
175                 statement.executeUpdate();
176             } finally {
177                 close(statement);
178             }
179             try {
180                 statement =
181                     connection.prepareStatement(
182                         "delete from PROPERTIES p where p.VERSION_ID in (" +
183                         "select vh.VERSION_ID from VERSION_HISTORY vh, URI u where vh.REVISION_NO = ? and vh.URI_ID = u.URI_ID AND u.URI_STRING = ?)");
184                 statement.setString(1, revisionNumber.toString());
185                 statement.setString(2, uri.toString());
186                 statement.executeUpdate();
187             } finally {
188                 close(statement);
189             }
190         } catch (SQLException JavaDoc e) {
191             throw createException(e, uri.toString());
192         }
193     }
194
195     public void removeLock(Connection JavaDoc connection, Uri uri, NodeLock lock)
196         throws ServiceAccessException, LockTokenNotFoundException {
197         PreparedStatement JavaDoc statement = null;
198         try {
199             // FIXME: What about inheritage?
200
try {
201                 statement =
202                     connection.prepareStatement(
203                         "delete from LOCKS where LOCKS.LOCK_ID in (select u.URI_ID from URI u where u.URI_STRING=?)");
204                 statement.setString(1, lock.getLockId());
205                 statement.executeUpdate();
206             } finally {
207                 close(statement);
208             }
209             try {
210                 statement =
211                     connection.prepareStatement(
212                         "delete from URI where URI_STRING=?");
213                 statement.setString(1, lock.getLockId());
214                 statement.executeUpdate();
215             } finally {
216                 close(statement);
217             }
218         } catch (SQLException JavaDoc e) {
219             throw createException(e, uri.toString());
220         }
221     }
222
223     public void revokePermission(Connection JavaDoc connection, Uri uri, NodePermission permission)
224         throws ServiceAccessException {
225         if (permission == null) return;
226         PreparedStatement JavaDoc statement = null;
227         try {
228             NodeRevisionNumber revisionNumber = permission.getRevisionNumber();
229             statement =
230                 connection.prepareStatement(
231                     "delete from PERMISSIONS where PERMISSIONS.OBJECT_ID in (select ou.URI_ID from URI ou, URI su, URI au where ou.URI_STRING = ? and SUBJECT_ID = su.URI_ID and su.URI_STRING = ? and ACTION_ID = au.URI_ID and au.URI_STRING = ? and VERSION_NO" + ((revisionNumber == null) ? " IS NULL " : " = '" + revisionNumber.toString() + "'"));
232             statement.setString(1, permission.getObjectUri());
233             statement.setString(2, permission.getSubjectUri());
234             statement.setString(3, permission.getActionUri());
235             statement.executeUpdate();
236         } catch (SQLException JavaDoc e) {
237             throw createException(e, uri.toString());
238         } finally {
239             close(statement);
240         }
241     }
242
243     public void revokePermissions(Connection JavaDoc connection, Uri uri) throws ServiceAccessException {
244         PreparedStatement JavaDoc statement = null;
245         try {
246             statement =
247                 connection.prepareStatement(
248                     "delete from PERMISSIONS where PERMISSIONS.OBJECT_ID in (select u.URI_ID from URI u where u.URI_STRING = ?)");
249             statement.setString(1, uri.toString());
250             statement.executeUpdate();
251         } catch (SQLException JavaDoc e) {
252             throw createException(e, uri.toString());
253         } finally {
254             close(statement);
255         }
256     }
257
258     protected void clearBinding(Connection JavaDoc connection, Uri uri)
259         throws ServiceAccessException, ObjectNotFoundException, SQLException JavaDoc
260     {
261         PreparedStatement JavaDoc statement = null;
262
263         try {
264             statement =
265                 connection.prepareStatement(
266                     "delete from BINDING where BINDING.URI_ID in (select URI_ID from URI where URI.URI_STRING = ?)");
267             statement.setString(1, uri.toString());
268             statement.executeUpdate();
269         } finally {
270             close(statement);
271         }
272
273         try {
274             statement =
275                 connection.prepareStatement(
276                     "delete from PARENT_BINDING where PARENT_BINDING.URI_ID in (select URI_ID from URI where URI.URI_STRING = ?)");
277             statement.setString(1, uri.toString());
278             statement.executeUpdate();
279         } finally {
280             close(statement);
281         }
282     }
283
284     protected void storeContent(
285         Connection JavaDoc connection, Uri uri,
286     NodeRevisionDescriptor revisionDescriptor,
287     NodeRevisionContent revisionContent) throws IOException, SQLException JavaDoc
288     {
289         assureVersionInfo(connection, uri, revisionDescriptor);
290         
291         InputStream is = revisionContent.streamContent();
292         if (is != null) {
293             File tempFile = null;
294             
295             if (bcompress) {
296                 getLogger().log("Compressing the data", LOG_CHANNEL, Logger.DEBUG);
297                 StoreContentZip ziputil = new StoreContentZip();
298                 ziputil.Zip(is);
299                 is = ziputil.getInputStream();
300                 
301                 // fix RevisionDescriptor Content Length
302
if (revisionDescriptor.getContentLength() == -1) {
303                     revisionDescriptor.setContentLength(ziputil.getInitialContentLength());
304                 }
305             } else {
306                 // fix RevisionDescriptor Content Length
307
if (revisionDescriptor.getContentLength() == -1) {
308                     try {
309                         tempFile = File.createTempFile("content", null);
310                         FileOutputStream fos = new FileOutputStream(tempFile);
311                         try {
312                             byte buffer[] = new byte[2048];
313                             do {
314                                 int nChar = is.read(buffer);
315                                 if (nChar == -1) {
316                                     break;
317                                 }
318                                 fos.write(buffer, 0, nChar);
319                             } while (true);
320                         } finally {
321                             fos.close();
322                         }
323                         is.close();
324                         is = new FileInputStream(tempFile);
325                         
326                         revisionDescriptor.setContentLength(tempFile.length());
327                         
328                     } catch (IOException ex) {
329                         getLogger().log(ex.toString() + " during the calculation of the content length.",
330                             LOG_CHANNEL, Logger.ERROR);
331                         throw ex;
332                     }
333                 }
334             }
335                 
336             PreparedStatement JavaDoc statement = null;
337             try {
338                 long versionID = getVersionID(connection, uri.toString(), revisionDescriptor);
339                 statement = connection.prepareStatement(
340                     "insert into VERSION_CONTENT (VERSION_ID, CONTENT) values (?,?)");
341                 statement.setLong(1, versionID);
342                 statement.setBinaryStream(2, is, (int) revisionDescriptor.getContentLength());
343                 statement.executeUpdate();
344                 if (tempFile != null) {
345                     is.close();
346                     is = null;
347                     tempFile.delete();
348                 }
349             } finally {
350                 try {
351                     close(statement);
352                 } finally {
353                     if (is != null) {
354                         // XXX some JDBC drivers seem to close the stream upon closing of
355
// the statement; if so this will raise an IOException
356
// silently ignore it...
357
try {
358                             is.close();
359                         } catch (IOException ioe) {
360                             logger.log("Could not close stream", ioe, LOG_CHANNEL, Logger.DEBUG);
361                         }
362                     }
363                 }
364             }
365         }
366     }
367
368     protected long getVersionID(
369         Connection JavaDoc connection,
370         String JavaDoc uriString,
371         NodeRevisionDescriptor revisionDescriptor) throws SQLException JavaDoc
372     {
373         PreparedStatement JavaDoc statement = null;
374         ResultSet JavaDoc rs = null;
375         long versionID = 0l;
376         try {
377             statement = connection.prepareStatement(
378                 "select vh.VERSION_ID from VERSION_HISTORY vh, URI u where vh.URI_ID = u.URI_ID and u.URI_STRING = ? and vh.REVISION_NO = ?");
379             statement.setString(1,uriString);
380             statement.setString(2,revisionDescriptor.getRevisionNumber().toString());
381             rs = statement.executeQuery();
382             if (rs.next()) {
383                 versionID = rs.getLong(1);
384             }
385         } finally {
386             close(statement,rs);
387         }
388         
389         return versionID;
390     }
391
392     protected String JavaDoc convertRevisionNumberToComparable(String JavaDoc revisioNumber) {
393         return "to_number(substr("+revisioNumber+",1,instr("+revisioNumber+",'.')-1)), to_number(substr("+revisioNumber+",instr("+revisioNumber+",'.')+1))";
394     }
395
396 }
397
398
399
Popular Tags