KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > db > generic > CmsVfsDriver


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/db/generic/CmsVfsDriver.java,v $
3  * Date : $Date: 2006/04/28 15:20:57 $
4  * Version: $Revision: 1.261 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.db.generic;
33
34 import org.opencms.configuration.CmsConfigurationManager;
35 import org.opencms.db.CmsDbConsistencyException;
36 import org.opencms.db.CmsDbContext;
37 import org.opencms.db.CmsDbEntryNotFoundException;
38 import org.opencms.db.CmsDbSqlException;
39 import org.opencms.db.CmsDbUtil;
40 import org.opencms.db.CmsDriverManager;
41 import org.opencms.db.I_CmsDriver;
42 import org.opencms.db.I_CmsVfsDriver;
43 import org.opencms.file.CmsDataAccessException;
44 import org.opencms.file.CmsFile;
45 import org.opencms.file.CmsFolder;
46 import org.opencms.file.CmsProject;
47 import org.opencms.file.CmsProperty;
48 import org.opencms.file.CmsPropertyDefinition;
49 import org.opencms.file.CmsResource;
50 import org.opencms.file.CmsVfsException;
51 import org.opencms.file.CmsVfsResourceAlreadyExistsException;
52 import org.opencms.file.CmsVfsResourceNotFoundException;
53 import org.opencms.main.CmsEvent;
54 import org.opencms.main.CmsLog;
55 import org.opencms.main.I_CmsEventListener;
56 import org.opencms.main.OpenCms;
57 import org.opencms.util.CmsStringUtil;
58 import org.opencms.util.CmsUUID;
59
60 import java.io.ByteArrayInputStream JavaDoc;
61 import java.sql.Connection JavaDoc;
62 import java.sql.PreparedStatement JavaDoc;
63 import java.sql.ResultSet JavaDoc;
64 import java.sql.SQLException JavaDoc;
65 import java.util.ArrayList JavaDoc;
66 import java.util.Collections JavaDoc;
67 import java.util.HashMap JavaDoc;
68 import java.util.List JavaDoc;
69 import java.util.Map JavaDoc;
70
71 import org.apache.commons.logging.Log;
72
73 /**
74  * Generic (ANSI-SQL) database server implementation of the VFS driver methods.<p>
75  *
76  * @author Thomas Weckert
77  * @author Michael Emmerich
78  *
79  * @version $Revision: 1.261 $
80  *
81  * @since 6.0.0
82  */

83 public class CmsVfsDriver implements I_CmsDriver, I_CmsVfsDriver {
84
85     /** Operator to concatenate exclude conditions. */
86     protected static final String JavaDoc BEGIN_EXCLUDE_CONDITION = " AND NOT (";
87
88     /** Operator to concatenate include conditions. */
89     protected static final String JavaDoc BEGIN_INCLUDE_CONDITION = " AND (";
90
91     /** String to end a single condition. */
92     protected static final String JavaDoc END_CONDITION = ") ";
93
94     /** The log object for this class. */
95     private static final Log LOG = CmsLog.getLog(org.opencms.db.generic.CmsVfsDriver.class);
96
97     /** The driver manager. */
98     protected CmsDriverManager m_driverManager;
99
100     /** The sql manager. */
101     protected org.opencms.db.generic.CmsSqlManager m_sqlManager;
102
103     /**
104      * Adds a trailing separator to a path if required.<p>
105      *
106      * @param path the path to add the trailing separator to
107      * @return the path with a trailing separator
108      */

109     private static String JavaDoc addTrailingSeparator(String JavaDoc path) {
110
111         int l = path.length();
112         if ((l == 0) || (path.charAt(l - 1) != '/')) {
113             return path.concat("/");
114         } else {
115             return path;
116         }
117     }
118
119     /**
120      * Removes a trailing separater from a path if required.<p>
121      *
122      * @param path the path to remove the trailing separator from
123      * @return the path without a trailing separator
124      */

125     private static String JavaDoc removeTrailingSeparator(String JavaDoc path) {
126
127         int l = path.length();
128         if ((l <= 1) || (path.charAt(l - 1) != '/')) {
129             return path;
130         } else {
131             return path.substring(0, l - 1);
132         }
133     }
134
135     /**
136      * @see org.opencms.db.I_CmsVfsDriver#createContent(org.opencms.db.CmsDbContext, org.opencms.file.CmsProject, org.opencms.util.CmsUUID, byte[], int)
137      */

138     public void createContent(CmsDbContext dbc, CmsProject project, CmsUUID resourceId, byte[] content, int versionId)
139     throws CmsDataAccessException {
140
141         Connection JavaDoc conn = null;
142         PreparedStatement JavaDoc stmt = null;
143
144         try {
145             conn = m_sqlManager.getConnection(dbc, project.getId());
146             stmt = m_sqlManager.getPreparedStatement(conn, project, "C_CONTENTS_WRITE");
147             stmt.setString(1, new CmsUUID().toString());
148             stmt.setString(2, resourceId.toString());
149
150             if (content.length < 2000) {
151                 stmt.setBytes(3, content);
152             } else {
153                 stmt.setBinaryStream(3, new ByteArrayInputStream JavaDoc(content), content.length);
154             }
155
156             stmt.executeUpdate();
157         } catch (SQLException JavaDoc e) {
158             throw new CmsDbSqlException(Messages.get().container(
159                 Messages.ERR_GENERIC_SQL_1,
160                 CmsDbSqlException.getErrorQuery(stmt)), e);
161         } finally {
162             m_sqlManager.closeAll(dbc, conn, stmt, null);
163         }
164     }
165
166     /**
167      * @see org.opencms.db.I_CmsVfsDriver#createFile(java.sql.ResultSet, int)
168      */

169     public CmsFile createFile(ResultSet JavaDoc res, int projectId) throws SQLException JavaDoc {
170
171         CmsUUID structureId = new CmsUUID(res.getString(m_sqlManager.readQuery("C_RESOURCES_STRUCTURE_ID")));
172         CmsUUID resourceId = new CmsUUID(res.getString(m_sqlManager.readQuery("C_RESOURCES_RESOURCE_ID")));
173         int resourceType = res.getInt(m_sqlManager.readQuery("C_RESOURCES_RESOURCE_TYPE"));
174         String JavaDoc resourcePath = res.getString(m_sqlManager.readQuery("C_RESOURCES_RESOURCE_PATH"));
175         int resourceFlags = res.getInt(m_sqlManager.readQuery("C_RESOURCES_RESOURCE_FLAGS"));
176         int resourceState = res.getInt(m_sqlManager.readQuery("C_RESOURCES_STATE"));
177         int structureState = res.getInt(m_sqlManager.readQuery("C_RESOURCES_STRUCTURE_STATE"));
178         long dateCreated = res.getLong(m_sqlManager.readQuery("C_RESOURCES_DATE_CREATED"));
179         long dateLastModified = res.getLong(m_sqlManager.readQuery("C_RESOURCES_DATE_LASTMODIFIED"));
180         long dateReleased = res.getLong(m_sqlManager.readQuery("C_RESOURCES_DATE_RELEASED"));
181         long dateExpired = res.getLong(m_sqlManager.readQuery("C_RESOURCES_DATE_EXPIRED"));
182         int resourceSize = res.getInt(m_sqlManager.readQuery("C_RESOURCES_SIZE"));
183         CmsUUID userCreated = new CmsUUID(res.getString(m_sqlManager.readQuery("C_RESOURCES_USER_CREATED")));
184         CmsUUID userLastModified = new CmsUUID(res.getString(m_sqlManager.readQuery("C_RESOURCES_USER_LASTMODIFIED")));
185         CmsUUID contentId = new CmsUUID(res.getString(m_sqlManager.readQuery("C_RESOURCES_CONTENT_ID")));
186         byte[] content = m_sqlManager.getBytes(res, m_sqlManager.readQuery("C_RESOURCES_FILE_CONTENT"));
187         int siblingCount = res.getInt(m_sqlManager.readQuery("C_RESOURCES_SIBLING_COUNT"));
188
189         // calculate the overall state
190
int newState = (structureState > resourceState) ? structureState : resourceState;
191
192         // in case of folder type ensure, that the root path has a trailing slash
193
if (CmsFolder.isFolderType(resourceType)) {
194             resourcePath = addTrailingSeparator(resourcePath);
195         }
196
197         return new CmsFile(
198             structureId,
199             resourceId,
200             contentId,
201             resourcePath,
202             resourceType,
203             resourceFlags,
204             projectId,
205             newState,
206             dateCreated,
207             userCreated,
208             dateLastModified,
209             userLastModified,
210             dateReleased,
211             dateExpired,
212             siblingCount,
213             resourceSize,
214             content);
215     }
216
217     /**
218      * @see org.opencms.db.I_CmsVfsDriver#createFile(java.sql.ResultSet, int, boolean)
219      */

220     public CmsFile createFile(ResultSet JavaDoc res, int projectId, boolean hasFileContentInResultSet) throws SQLException JavaDoc {
221
222         byte[] content = null;
223
224         int resProjectId = CmsDbUtil.UNKNOWN_ID;
225
226         CmsUUID structureId = new CmsUUID(res.getString(m_sqlManager.readQuery("C_RESOURCES_STRUCTURE_ID")));
227         CmsUUID resourceId = new CmsUUID(res.getString(m_sqlManager.readQuery("C_RESOURCES_RESOURCE_ID")));
228         String JavaDoc resourcePath = res.getString(m_sqlManager.readQuery("C_RESOURCES_RESOURCE_PATH"));
229         int resourceType = res.getInt(m_sqlManager.readQuery("C_RESOURCES_RESOURCE_TYPE"));
230         int resourceFlags = res.getInt(m_sqlManager.readQuery("C_RESOURCES_RESOURCE_FLAGS"));
231         int resourceState = res.getInt(m_sqlManager.readQuery("C_RESOURCES_STATE"));
232         int structureState = res.getInt(m_sqlManager.readQuery("C_RESOURCES_STRUCTURE_STATE"));
233         long dateCreated = res.getLong(m_sqlManager.readQuery("C_RESOURCES_DATE_CREATED"));
234         long dateLastModified = res.getLong(m_sqlManager.readQuery("C_RESOURCES_DATE_LASTMODIFIED"));
235         long dateReleased = res.getLong(m_sqlManager.readQuery("C_RESOURCES_DATE_RELEASED"));
236         long dateExpired = res.getLong(m_sqlManager.readQuery("C_RESOURCES_DATE_EXPIRED"));
237         int resourceSize = res.getInt(m_sqlManager.readQuery("C_RESOURCES_SIZE"));
238         CmsUUID userCreated = new CmsUUID(res.getString(m_sqlManager.readQuery("C_RESOURCES_USER_CREATED")));
239         CmsUUID userLastModified = new CmsUUID(res.getString(m_sqlManager.readQuery("C_RESOURCES_USER_LASTMODIFIED")));
240         int lockedInProject = res.getInt("LOCKED_IN_PROJECT");
241         int siblingCount = res.getInt(m_sqlManager.readQuery("C_RESOURCES_SIBLING_COUNT"));
242
243         // in case of folder type ensure, that the root path has a trailing slash
244
if (CmsFolder.isFolderType(resourceType)) {
245             resourcePath = addTrailingSeparator(resourcePath);
246         }
247
248         CmsUUID contentId;
249         if (hasFileContentInResultSet) {
250             content = m_sqlManager.getBytes(res, m_sqlManager.readQuery("C_RESOURCES_FILE_CONTENT"));
251             contentId = new CmsUUID(res.getString(m_sqlManager.readQuery("C_RESOURCES_CONTENT_ID")));
252         } else {
253             content = new byte[0];
254             contentId = CmsUUID.getNullUUID();
255         }
256
257         resProjectId = lockedInProject;
258
259         int newState = (structureState > resourceState) ? structureState : resourceState;
260
261         return new CmsFile(
262             structureId,
263             resourceId,
264             contentId,
265             resourcePath,
266             resourceType,
267             resourceFlags,
268             resProjectId,
269             newState,
270             dateCreated,
271             userCreated,
272             dateLastModified,
273             userLastModified,
274             dateReleased,
275             dateExpired,
276             siblingCount,
277             resourceSize,
278             content);
279     }
280
281     /**
282      * @see org.opencms.db.I_CmsVfsDriver#createFolder(java.sql.ResultSet, int, boolean)
283      */

284     public CmsFolder createFolder(ResultSet JavaDoc res, int projectId, boolean hasProjectIdInResultSet) throws SQLException JavaDoc {
285
286         CmsUUID structureId = new CmsUUID(res.getString(m_sqlManager.readQuery("C_RESOURCES_STRUCTURE_ID")));
287         CmsUUID resourceId = new CmsUUID(res.getString(m_sqlManager.readQuery("C_RESOURCES_RESOURCE_ID")));
288         String JavaDoc resourcePath = res.getString(m_sqlManager.readQuery("C_RESOURCES_RESOURCE_PATH"));
289         int resourceType = res.getInt(m_sqlManager.readQuery("C_RESOURCES_RESOURCE_TYPE"));
290         int resourceFlags = res.getInt(m_sqlManager.readQuery("C_RESOURCES_RESOURCE_FLAGS"));
291         int resourceState = res.getInt(m_sqlManager.readQuery("C_RESOURCES_STATE"));
292         int structureState = res.getInt(m_sqlManager.readQuery("C_RESOURCES_STRUCTURE_STATE"));
293         long dateCreated = res.getLong(m_sqlManager.readQuery("C_RESOURCES_DATE_CREATED"));
294         long dateLastModified = res.getLong(m_sqlManager.readQuery("C_RESOURCES_DATE_LASTMODIFIED"));
295         long dateReleased = res.getLong(m_sqlManager.readQuery("C_RESOURCES_DATE_RELEASED"));
296         long dateExpired = res.getLong(m_sqlManager.readQuery("C_RESOURCES_DATE_EXPIRED"));
297         CmsUUID userCreated = new CmsUUID(res.getString(m_sqlManager.readQuery("C_RESOURCES_USER_CREATED")));
298         CmsUUID userLastModified = new CmsUUID(res.getString(m_sqlManager.readQuery("C_RESOURCES_USER_LASTMODIFIED")));
299         int resProjectId = res.getInt("LOCKED_IN_PROJECT");
300         int siblingCount = res.getInt(m_sqlManager.readQuery("C_RESOURCES_SIBLING_COUNT"));
301
302         // in case of folder type ensure, that the root path has a trailing slash
303
if (CmsFolder.isFolderType(resourceType)) {
304             resourcePath = addTrailingSeparator(resourcePath);
305         }
306
307         int newState = (structureState > resourceState) ? structureState : resourceState;
308
309         return new CmsFolder(
310             structureId,
311             resourceId,
312             resourcePath,
313             resourceType,
314             resourceFlags,
315             resProjectId,
316             newState,
317             dateCreated,
318             userCreated,
319             dateLastModified,
320             userLastModified,
321             siblingCount,
322             dateReleased,
323             dateExpired);
324     }
325
326     /**
327      * @see org.opencms.db.I_CmsVfsDriver#createPropertyDefinition(org.opencms.db.CmsDbContext, int, java.lang.String)
328      */

329     public CmsPropertyDefinition createPropertyDefinition(CmsDbContext dbc, int projectId, String JavaDoc name)
330     throws CmsDataAccessException {
331
332         Connection JavaDoc conn = null;
333         PreparedStatement JavaDoc stmt = null;
334
335         // TODO switch the property def. PK into a CmsUUID PK
336

337         try {
338             conn = m_sqlManager.getConnection(dbc, projectId);
339             stmt = m_sqlManager.getPreparedStatement(conn, projectId, "C_PROPERTYDEF_CREATE");
340             stmt.setString(1, new CmsUUID().toString());
341             stmt.setString(2, name);
342             stmt.executeUpdate();
343         } catch (SQLException JavaDoc e) {
344             throw new CmsDbSqlException(Messages.get().container(
345                 Messages.ERR_GENERIC_SQL_1,
346                 CmsDbSqlException.getErrorQuery(stmt)), e);
347         } finally {
348             m_sqlManager.closeAll(dbc, conn, stmt, null);
349         }
350
351         return readPropertyDefinition(dbc, name, projectId);
352     }
353
354     /**
355      * @see org.opencms.db.I_CmsVfsDriver#createResource(org.opencms.db.CmsDbContext, org.opencms.file.CmsProject, org.opencms.file.CmsResource, byte[])
356      */

357     public CmsResource createResource(CmsDbContext dbc, CmsProject project, CmsResource resource, byte[] content)
358     throws CmsDataAccessException {
359
360         CmsUUID newStructureId = null;
361         Connection JavaDoc conn = null;
362         PreparedStatement JavaDoc stmt = null;
363
364         // check the resource path
365
String JavaDoc resourcePath = removeTrailingSeparator(resource.getRootPath());
366         if (resourcePath.length() > CmsDriverManager.MAX_VFS_RESOURCE_PATH_LENGTH) {
367             throw new CmsDataAccessException(Messages.get().container(
368                 Messages.ERR_RESOURCENAME_TOO_LONG_2,
369                 resourcePath,
370                 new Integer JavaDoc(CmsDriverManager.MAX_VFS_RESOURCE_PATH_LENGTH)));
371         }
372
373         // check if the parent folder of the resource exists and is not deleted
374
if (!resource.getRootPath().equals("/")) {
375             String JavaDoc parentFolderName = CmsResource.getParentFolder(resource.getRootPath());
376             CmsFolder parentFolder = readFolder(dbc, project.getId(), parentFolderName);
377             if (parentFolder.getState() == CmsResource.STATE_DELETED) {
378                 throw new CmsDbEntryNotFoundException(Messages.get().container(
379                     Messages.ERR_PARENT_FOLDER_DELETED_1,
380                     resource.getRootPath()));
381             }
382         }
383
384         // validate the resource length
385
internalValidateResourceLength(resource);
386
387         // set the resource state and modification dates
388
int newState;
389         long dateModified;
390         long dateCreated;
391
392         if (project.getId() == CmsProject.ONLINE_PROJECT_ID) {
393             newState = CmsResource.STATE_UNCHANGED;
394             dateCreated = resource.getDateCreated();
395             dateModified = resource.getDateLastModified();
396         } else {
397             newState = CmsResource.STATE_NEW;
398             if (resource.isTouched()) {
399                 dateCreated = resource.getDateCreated();
400                 dateModified = resource.getDateLastModified();
401             } else {
402                 dateCreated = System.currentTimeMillis();
403                 dateModified = dateCreated;
404             }
405         }
406
407         // check if the resource already exists
408
CmsResource existingResource = null;
409         newStructureId = resource.getStructureId();
410
411         try {
412             existingResource = readResource(dbc, project.getId(), resourcePath, true);
413
414             if (existingResource.getState() == CmsResource.STATE_DELETED) {
415
416                 // if an existing resource is deleted, it will be finally removed now.
417
// but we have to reuse its id in order to avoid orphanes in the online project
418
newStructureId = existingResource.getStructureId();
419                 newState = CmsResource.STATE_CHANGED;
420
421                 // remove the existing file and it's properties
422
List JavaDoc modifiedResources = readSiblings(dbc, project, existingResource, false);
423                 int propertyDeleteOption = (existingResource.getSiblingCount() > 1) ? CmsProperty.DELETE_OPTION_DELETE_STRUCTURE_VALUES
424                 : CmsProperty.DELETE_OPTION_DELETE_STRUCTURE_AND_RESOURCE_VALUES;
425                 deletePropertyObjects(dbc, project.getId(), existingResource, propertyDeleteOption);
426                 removeFile(dbc, project, existingResource, true);
427
428                 OpenCms.fireCmsEvent(new CmsEvent(
429                     I_CmsEventListener.EVENT_RESOURCES_MODIFIED,
430                     Collections.singletonMap("resources", modifiedResources)));
431                 OpenCms.fireCmsEvent(new CmsEvent(
432                     I_CmsEventListener.EVENT_RESOURCE_AND_PROPERTIES_MODIFIED,
433                     Collections.singletonMap("resource", existingResource)));
434             }
435         } catch (CmsVfsResourceNotFoundException e) {
436             // that's what we want in the best case- anything else should be thrown
437
}
438
439         if (existingResource != null && existingResource.getState() != CmsResource.STATE_DELETED) {
440             // we have a collision: there exists already a resource with the same path/name which cannot be removed
441
throw new CmsVfsResourceAlreadyExistsException(Messages.get().container(
442                 Messages.ERR_RESOURCE_WITH_NAME_ALREADY_EXISTS_1,
443                 dbc.removeSiteRoot(resource.getRootPath())));
444         }
445
446         try {
447
448             // read the parent id
449
String JavaDoc parentId = internalReadParentId(dbc, project.getId(), resourcePath);
450
451             conn = m_sqlManager.getConnection(dbc, project.getId());
452             stmt = m_sqlManager.getPreparedStatement(conn, project, "C_STRUCTURE_WRITE");
453             stmt.setString(1, newStructureId.toString());
454             stmt.setString(2, resource.getResourceId().toString());
455             stmt.setString(3, resourcePath);
456             stmt.setInt(4, newState);
457             stmt.setLong(5, resource.getDateReleased());
458             stmt.setLong(6, resource.getDateExpired());
459             stmt.setString(7, parentId);
460             stmt.executeUpdate();
461
462             m_sqlManager.closeAll(dbc, null, stmt, null);
463
464             if (!validateResourceIdExists(dbc, project.getId(), resource.getResourceId())) {
465
466                 // create the resource record
467
stmt = m_sqlManager.getPreparedStatement(conn, project, "C_RESOURCES_WRITE");
468                 stmt.setString(1, resource.getResourceId().toString());
469                 stmt.setInt(2, resource.getTypeId());
470                 stmt.setInt(3, resource.getFlags());
471                 stmt.setLong(4, dateCreated);
472                 stmt.setString(5, resource.getUserCreated().toString());
473                 stmt.setLong(6, dateModified);
474                 stmt.setString(7, resource.getUserLastModified().toString());
475                 stmt.setInt(8, newState);
476                 stmt.setInt(9, resource.getLength());
477                 stmt.setInt(10, project.getId());
478                 stmt.setInt(11, 1);
479                 stmt.executeUpdate();
480
481                 if (resource.isFile() && content != null) {
482                     // create the file content
483
createContent(dbc, project, resource.getResourceId(), content, 0);
484                 }
485
486             } else {
487
488                 if ((content != null) || (resource.getState() != CmsResource.STATE_KEEP)) {
489                     // update the resource record only if state has changed or new content is provided
490
stmt = m_sqlManager.getPreparedStatement(conn, project, "C_RESOURCES_UPDATE_RESOURCES");
491                     stmt.setInt(1, resource.getTypeId());
492                     stmt.setInt(2, resource.getFlags());
493                     stmt.setLong(3, dateModified);
494                     stmt.setString(4, resource.getUserLastModified().toString());
495                     stmt.setInt(5, CmsResource.STATE_CHANGED);
496                     stmt.setInt(6, resource.getLength());
497                     stmt.setInt(7, project.getId());
498                     stmt.setInt(8, internalCountSiblings(dbc, project.getId(), resource.getResourceId()));
499                     stmt.setString(9, resource.getResourceId().toString());
500                     stmt.executeUpdate();
501                     m_sqlManager.closeAll(dbc, null, stmt, null);
502                 }
503
504                 if (resource.isFile()) {
505                     if (content != null) {
506
507                         // update the file content
508
writeContent(dbc, project, resource.getResourceId(), content);
509
510                     } else if (resource.getState() == CmsResource.STATE_KEEP) {
511
512                         // special case sibling creation - update the link Count
513
stmt = m_sqlManager.getPreparedStatement(conn, project, "C_RESOURCES_UPDATE_SIBLING_COUNT");
514                         stmt.setInt(1, this.internalCountSiblings(dbc, project.getId(), resource.getResourceId()));
515                         stmt.setString(2, resource.getResourceId().toString());
516                         stmt.executeUpdate();
517                         m_sqlManager.closeAll(dbc, null, stmt, null);
518
519                         // update the resource flags
520
stmt = m_sqlManager.getPreparedStatement(conn, project, "C_RESOURCES_UPDATE_FLAGS");
521                         stmt.setInt(1, resource.getFlags());
522                         stmt.setString(2, resource.getResourceId().toString());
523                         stmt.executeUpdate();
524                         m_sqlManager.closeAll(dbc, null, stmt, null);
525                     }
526                 }
527             }
528         } catch (SQLException JavaDoc e) {
529             throw new CmsDbSqlException(Messages.get().container(
530                 Messages.ERR_GENERIC_SQL_1,
531                 CmsDbSqlException.getErrorQuery(stmt)), e);
532         } finally {
533             m_sqlManager.closeAll(dbc, conn, stmt, null);
534         }
535
536         return readResource(dbc, project.getId(), newStructureId, false);
537     }
538
539     /**
540      * @see org.opencms.db.I_CmsVfsDriver#createResource(java.sql.ResultSet, int)
541      */

542     public CmsResource createResource(ResultSet JavaDoc res, int projectId) throws SQLException JavaDoc {
543
544         CmsUUID structureId = new CmsUUID(res.getString(m_sqlManager.readQuery("C_RESOURCES_STRUCTURE_ID")));
545         CmsUUID resourceId = new CmsUUID(res.getString(m_sqlManager.readQuery("C_RESOURCES_RESOURCE_ID")));
546         String JavaDoc resourcePath = res.getString(m_sqlManager.readQuery("C_RESOURCES_RESOURCE_PATH"));
547         int resourceType = res.getInt(m_sqlManager.readQuery("C_RESOURCES_RESOURCE_TYPE"));
548         int resourceFlags = res.getInt(m_sqlManager.readQuery("C_RESOURCES_RESOURCE_FLAGS"));
549         int resourceProjectLastModified = res.getInt(m_sqlManager.readQuery("C_RESOURCES_PROJECT_LASTMODIFIED"));
550         int resourceState = res.getInt(m_sqlManager.readQuery("C_RESOURCES_STATE"));
551         int structureState = res.getInt(m_sqlManager.readQuery("C_RESOURCES_STRUCTURE_STATE"));
552         long dateCreated = res.getLong(m_sqlManager.readQuery("C_RESOURCES_DATE_CREATED"));
553         long dateLastModified = res.getLong(m_sqlManager.readQuery("C_RESOURCES_DATE_LASTMODIFIED"));
554         long dateReleased = res.getLong(m_sqlManager.readQuery("C_RESOURCES_DATE_RELEASED"));
555         long dateExpired = res.getLong(m_sqlManager.readQuery("C_RESOURCES_DATE_EXPIRED"));
556         int resourceSize;
557
558         // in case of folder type ensure, that the root path has a trailing slash
559
boolean isFolder = CmsFolder.isFolderType(resourceType);
560         if (isFolder) {
561             resourcePath = addTrailingSeparator(resourcePath);
562             // folders must have -1 size
563
resourceSize = -1;
564         } else {
565             // not a folder
566
resourceSize = res.getInt(m_sqlManager.readQuery("C_RESOURCES_SIZE"));
567         }
568         CmsUUID userCreated = new CmsUUID(res.getString(m_sqlManager.readQuery("C_RESOURCES_USER_CREATED")));
569         CmsUUID userLastModified = new CmsUUID(res.getString(m_sqlManager.readQuery("C_RESOURCES_USER_LASTMODIFIED")));
570         int siblingCount = res.getInt(m_sqlManager.readQuery("C_RESOURCES_SIBLING_COUNT"));
571
572         int newState = (structureState > resourceState) ? structureState : resourceState;
573
574         CmsResource newResource = new CmsResource(
575             structureId,
576             resourceId,
577             resourcePath,
578             resourceType,
579             isFolder,
580             resourceFlags,
581             resourceProjectLastModified,
582             newState,
583             dateCreated,
584             userCreated,
585             dateLastModified,
586             userLastModified,
587             dateReleased,
588             dateExpired,
589             siblingCount,
590             resourceSize);
591
592         return newResource;
593     }
594
595     /**
596      * @see org.opencms.db.I_CmsVfsDriver#createSibling(org.opencms.db.CmsDbContext, org.opencms.file.CmsProject, org.opencms.file.CmsResource)
597      */

598     public void createSibling(CmsDbContext dbc, CmsProject project, CmsResource resource) throws CmsDataAccessException {
599
600         Connection JavaDoc conn = null;
601         PreparedStatement JavaDoc stmt = null;
602         int newState = 0;
603
604         // force some attribs when creating or publishing a file
605
if (project.getId() == CmsProject.ONLINE_PROJECT_ID) {
606             newState = CmsResource.STATE_UNCHANGED;
607         } else {
608             newState = CmsResource.STATE_NEW;
609         }
610
611         // check if the resource already exists
612
CmsResource existingSibling = null;
613         CmsUUID newStructureId = resource.getStructureId();
614
615         try {
616             existingSibling = readResource(dbc, project.getId(), resource.getRootPath(), true);
617
618             if (existingSibling.getState() == CmsResource.STATE_DELETED) {
619                 // if an existing resource is deleted, it will be finally removed now.
620
// but we have to reuse its id in order to avoid orphanes in the online project.
621
newStructureId = existingSibling.getStructureId();
622                 newState = CmsResource.STATE_CHANGED;
623
624                 // remove the existing file and it's properties
625
List JavaDoc modifiedResources = readSiblings(dbc, project, existingSibling, false);
626                 int propertyDeleteOption = (existingSibling.getSiblingCount() > 1) ? CmsProperty.DELETE_OPTION_DELETE_STRUCTURE_VALUES
627                 : CmsProperty.DELETE_OPTION_DELETE_STRUCTURE_AND_RESOURCE_VALUES;
628                 deletePropertyObjects(dbc, project.getId(), existingSibling, propertyDeleteOption);
629                 removeFile(dbc, project, existingSibling, true);
630
631                 OpenCms.fireCmsEvent(new CmsEvent(
632                     I_CmsEventListener.EVENT_RESOURCES_MODIFIED,
633                     Collections.singletonMap("resources", modifiedResources)));
634                 OpenCms.fireCmsEvent(new CmsEvent(
635                     I_CmsEventListener.EVENT_RESOURCE_AND_PROPERTIES_MODIFIED,
636                     Collections.singletonMap("resource", existingSibling)));
637             }
638         } catch (CmsVfsResourceNotFoundException e) {
639             // that's what we want in the best case- anything else should be thrown
640
}
641
642         if (existingSibling != null && existingSibling.getState() != CmsResource.STATE_DELETED) {
643             // we have a collision: there exists already a resource with the same path/name which could not be removed
644
throw new CmsVfsResourceAlreadyExistsException(Messages.get().container(
645                 Messages.ERR_RESOURCE_WITH_NAME_ALREADY_EXISTS_1,
646                 dbc.removeSiteRoot(resource.getRootPath())));
647         }
648
649         // check if a resource with the specified ID already exists
650
if (!validateResourceIdExists(dbc, project.getId(), resource.getResourceId())) {
651             throw new CmsVfsResourceNotFoundException(Messages.get().container(
652                 Messages.ERR_CREATE_SIBLING_FILE_NOT_FOUND_1,
653                 dbc.removeSiteRoot(resource.getRootPath())));
654         }
655
656         // write a new structure referring to the resource
657
try {
658             conn = m_sqlManager.getConnection(dbc, project.getId());
659
660             // read the parent id
661
String JavaDoc parentId = internalReadParentId(dbc, project.getId(), resource.getRootPath());
662
663             // write the structure
664
stmt = m_sqlManager.getPreparedStatement(conn, project, "C_STRUCTURE_WRITE");
665             stmt.setString(1, newStructureId.toString());
666             stmt.setString(2, resource.getResourceId().toString());
667             stmt.setString(3, resource.getRootPath());
668             stmt.setInt(4, newState);
669             stmt.setLong(5, resource.getDateReleased());
670             stmt.setLong(6, resource.getDateExpired());
671             stmt.setString(7, parentId);
672             stmt.executeUpdate();
673
674             m_sqlManager.closeAll(dbc, null, stmt, null);
675
676             // update the link Count
677
stmt = m_sqlManager.getPreparedStatement(conn, project, "C_RESOURCES_UPDATE_SIBLING_COUNT");
678             stmt.setInt(1, this.internalCountSiblings(dbc, project.getId(), resource.getResourceId()));
679             stmt.setString(2, resource.getResourceId().toString());
680             stmt.executeUpdate();
681
682             m_sqlManager.closeAll(dbc, null, stmt, null);
683
684             // update the resource flags
685
stmt = m_sqlManager.getPreparedStatement(conn, project, "C_RESOURCES_UPDATE_FLAGS");
686             stmt.setInt(1, resource.getFlags());
687             stmt.setString(2, resource.getResourceId().toString());
688             stmt.executeUpdate();
689
690         } catch (SQLException JavaDoc e) {
691             throw new CmsDbSqlException(Messages.get().container(
692                 Messages.ERR_GENERIC_SQL_1,
693                 CmsDbSqlException.getErrorQuery(stmt)), e);
694         } finally {
695             m_sqlManager.closeAll(dbc, conn, stmt, null);
696         }
697     }
698
699     /**
700      * @see org.opencms.db.I_CmsVfsDriver#deletePropertyDefinition(org.opencms.db.CmsDbContext, org.opencms.file.CmsPropertyDefinition)
701      */

702     public void deletePropertyDefinition(CmsDbContext dbc, CmsPropertyDefinition metadef) throws CmsDataAccessException {
703
704         Connection JavaDoc conn = null;
705         PreparedStatement JavaDoc stmt = null;
706
707         try {
708             if (internalCountProperties(dbc, metadef, CmsProject.ONLINE_PROJECT_ID) != 0
709                 || internalCountProperties(dbc, metadef, Integer.MAX_VALUE) != 0) {
710
711                 throw new CmsDataAccessException(Messages.get().container(
712                     Messages.ERR_DELETE_USED_PROPERTY_1,
713                     metadef.getName()));
714             }
715
716             conn = m_sqlManager.getConnection(dbc);
717
718             for (int i = 0; i < 2; i++) {
719                 if (i == 0) {
720                     // delete the offline propertydef
721
stmt = m_sqlManager.getPreparedStatement(conn, Integer.MAX_VALUE, "C_PROPERTYDEF_DELETE");
722                 } else if (i == 1) {
723                     // delete the online propertydef
724
stmt = m_sqlManager.getPreparedStatement(conn, CmsProject.ONLINE_PROJECT_ID, "C_PROPERTYDEF_DELETE");
725                 }
726
727                 stmt.setString(1, metadef.getId().toString());
728                 stmt.executeUpdate();
729                 m_sqlManager.closeAll(dbc, null, stmt, null);
730             }
731         } catch (SQLException JavaDoc e) {
732             throw new CmsDbSqlException(Messages.get().container(
733                 Messages.ERR_GENERIC_SQL_1,
734                 CmsDbSqlException.getErrorQuery(stmt)), e);
735         } finally {
736             m_sqlManager.closeAll(dbc, conn, stmt, null);
737         }
738     }
739
740     /**
741      * @see org.opencms.db.I_CmsVfsDriver#deletePropertyObjects(org.opencms.db.CmsDbContext, int, org.opencms.file.CmsResource, int)
742      */

743     public void deletePropertyObjects(CmsDbContext dbc, int projectId, CmsResource resource, int deleteOption)
744     throws CmsDataAccessException {
745
746         Connection JavaDoc conn = null;
747         PreparedStatement JavaDoc stmt = null;
748         String JavaDoc resourceName = resource.getRootPath();
749
750         // add folder separator to folder name if it is not present
751
if (resource.isFolder() && !resourceName.endsWith("/")) {
752             resourceName += "/";
753         }
754
755         try {
756             conn = m_sqlManager.getConnection(dbc, projectId);
757
758             if (deleteOption == CmsProperty.DELETE_OPTION_DELETE_STRUCTURE_AND_RESOURCE_VALUES) {
759                 // delete both the structure and resource property values mapped to the specified resource
760
stmt = m_sqlManager.getPreparedStatement(
761                     conn,
762                     projectId,
763                     "C_PROPERTIES_DELETE_ALL_STRUCTURE_AND_RESOURCE_VALUES");
764                 stmt.setString(1, resource.getResourceId().toString());
765                 stmt.setInt(2, CmsProperty.RESOURCE_RECORD_MAPPING);
766                 stmt.setString(3, resource.getStructureId().toString());
767                 stmt.setInt(4, CmsProperty.STRUCTURE_RECORD_MAPPING);
768             } else if (deleteOption == CmsProperty.DELETE_OPTION_DELETE_STRUCTURE_VALUES) {
769                 // delete the structure values mapped to the specified resource
770
stmt = m_sqlManager.getPreparedStatement(
771                     conn,
772                     projectId,
773                     "C_PROPERTIES_DELETE_ALL_VALUES_FOR_MAPPING_TYPE");
774                 stmt.setString(1, resource.getStructureId().toString());
775                 stmt.setInt(2, CmsProperty.STRUCTURE_RECORD_MAPPING);
776             } else if (deleteOption == CmsProperty.DELETE_OPTION_DELETE_RESOURCE_VALUES) {
777                 // delete the resource property values mapped to the specified resource
778
stmt = m_sqlManager.getPreparedStatement(
779                     conn,
780                     projectId,
781                     "C_PROPERTIES_DELETE_ALL_VALUES_FOR_MAPPING_TYPE");
782                 stmt.setString(1, resource.getResourceId().toString());
783                 stmt.setInt(2, CmsProperty.RESOURCE_RECORD_MAPPING);
784             }
785
786             stmt.executeUpdate();
787         } catch (SQLException JavaDoc e) {
788             throw new CmsDbSqlException(Messages.get().container(
789                 Messages.ERR_GENERIC_SQL_1,
790                 CmsDbSqlException.getErrorQuery(stmt)), e);
791         } finally {
792             m_sqlManager.closeAll(dbc, conn, stmt, null);
793         }
794     }
795
796     /**
797      * @see org.opencms.db.I_CmsVfsDriver#destroy()
798      */

799     public void destroy() throws Throwable JavaDoc {
800
801         finalize();
802         if (CmsLog.INIT.isInfoEnabled()) {
803             CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_SHUTDOWN_DRIVER_1, getClass().getName()));
804         }
805     }
806
807     /**
808      * @see org.opencms.db.I_CmsVfsDriver#getSqlManager()
809      */

810     public CmsSqlManager getSqlManager() {
811
812         return m_sqlManager;
813     }
814
815     /**
816      * @see org.opencms.db.I_CmsDriver#init(org.opencms.db.CmsDbContext, org.opencms.configuration.CmsConfigurationManager, java.util.List, org.opencms.db.CmsDriverManager)
817      */

818     public void init(
819         CmsDbContext dbc,
820         CmsConfigurationManager configurationManager,
821         List JavaDoc successiveDrivers,
822         CmsDriverManager driverManager) {
823
824         Map JavaDoc configuration = configurationManager.getConfiguration();
825         String JavaDoc poolUrl = (String JavaDoc)configuration.get("db.vfs.pool");
826         String JavaDoc classname = (String JavaDoc)configuration.get("db.vfs.sqlmanager");
827         m_sqlManager = this.initSqlManager(classname);
828         m_sqlManager.init(I_CmsVfsDriver.DRIVER_TYPE_ID, poolUrl);
829
830         m_driverManager = driverManager;
831
832         if (CmsLog.INIT.isInfoEnabled()) {
833             CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_ASSIGNED_POOL_1, poolUrl));
834         }
835
836         if (successiveDrivers != null && !successiveDrivers.isEmpty()) {
837             if (LOG.isWarnEnabled()) {
838                 LOG.warn(Messages.get().getBundle().key(
839                     Messages.LOG_SUCCESSIVE_DRIVERS_UNSUPPORTED_1,
840                     getClass().getName()));
841             }
842         }
843     }
844
845     /**
846      * @see org.opencms.db.I_CmsVfsDriver#initSqlManager(String)
847      */

848     public org.opencms.db.generic.CmsSqlManager initSqlManager(String JavaDoc classname) {
849
850         return CmsSqlManager.getInstance(classname);
851     }
852
853     /**
854      * @see org.opencms.db.I_CmsVfsDriver#publishResource(org.opencms.db.CmsDbContext, org.opencms.file.CmsProject, org.opencms.file.CmsResource, org.opencms.file.CmsResource, boolean)
855      */

856     public void publishResource(
857         CmsDbContext dbc,
858         CmsProject onlineProject,
859         CmsResource onlineResource,
860         CmsResource offlineResource,
861         boolean writeFileContent) throws CmsDataAccessException {
862
863         Connection JavaDoc conn = null;
864         PreparedStatement JavaDoc stmt = null;
865
866         // validate the resource length
867
internalValidateResourceLength(offlineResource);
868         int resourceSize = offlineResource.getLength();
869
870         String JavaDoc resourcePath = removeTrailingSeparator(offlineResource.getRootPath());
871
872         try {
873             conn = m_sqlManager.getConnection(dbc, onlineProject.getId());
874
875             if (validateResourceIdExists(dbc, onlineProject.getId(), offlineResource.getResourceId())) {
876
877                 // the resource record exists online already
878

879                 if (writeFileContent && offlineResource.isFile()) {
880                     // update the online file content
881
writeContent(
882                         dbc,
883                         onlineProject,
884                         offlineResource.getResourceId(),
885                         ((CmsFile)offlineResource).getContents());
886                 }
887
888                 // update the online resource record
889
stmt = m_sqlManager.getPreparedStatement(conn, onlineProject, "C_RESOURCES_UPDATE_RESOURCES");
890                 stmt.setInt(1, offlineResource.getTypeId());
891                 stmt.setInt(2, offlineResource.getFlags());
892                 stmt.setLong(3, offlineResource.getDateLastModified());
893                 stmt.setString(4, offlineResource.getUserLastModified().toString());
894                 stmt.setInt(5, CmsResource.STATE_UNCHANGED);
895                 stmt.setInt(6, resourceSize);
896                 stmt.setInt(7, offlineResource.getProjectLastModified());
897                 stmt.setInt(8, this.internalCountSiblings(dbc, onlineProject.getId(), onlineResource.getResourceId()));
898                 stmt.setString(9, offlineResource.getResourceId().toString());
899                 stmt.executeUpdate();
900
901                 m_sqlManager.closeAll(dbc, null, stmt, null);
902
903                 // read the parent id
904
String JavaDoc parentId = internalReadParentId(dbc, onlineProject.getId(), resourcePath);
905
906                 // update the online structure record
907
stmt = m_sqlManager.getPreparedStatement(conn, onlineProject, "C_RESOURCES_UPDATE_STRUCTURE");
908                 stmt.setString(1, offlineResource.getResourceId().toString());
909                 stmt.setString(2, resourcePath);
910                 stmt.setInt(3, CmsResource.STATE_UNCHANGED);
911                 stmt.setLong(4, offlineResource.getDateReleased());
912                 stmt.setLong(5, offlineResource.getDateExpired());
913                 stmt.setString(6, parentId);
914                 stmt.setString(7, offlineResource.getStructureId().toString());
915                 stmt.executeUpdate();
916
917             } else {
918
919                 // the resource record does NOT exist online yet
920
if (writeFileContent && offlineResource.isFile()) {
921                     // create the file content online
922
resourceSize = offlineResource.getLength();
923                     createContent(
924                         dbc,
925                         onlineProject,
926                         offlineResource.getResourceId(),
927                         ((CmsFile)offlineResource).getContents(),
928                         0);
929                 }
930
931                 // create the resource record online
932
stmt = m_sqlManager.getPreparedStatement(conn, onlineProject, "C_RESOURCES_WRITE");
933                 stmt.setString(1, offlineResource.getResourceId().toString());
934                 stmt.setInt(2, offlineResource.getTypeId());
935                 stmt.setInt(3, offlineResource.getFlags());
936                 stmt.setLong(4, offlineResource.getDateCreated());
937                 stmt.setString(5, offlineResource.getUserCreated().toString());
938                 stmt.setLong(6, offlineResource.getDateLastModified());
939                 stmt.setString(7, offlineResource.getUserLastModified().toString());
940                 stmt.setInt(8, CmsResource.STATE_UNCHANGED);
941                 stmt.setInt(9, resourceSize);
942                 stmt.setInt(10, offlineResource.getProjectLastModified());
943                 stmt.setInt(11, 1);
944                 stmt.executeUpdate();
945
946                 m_sqlManager.closeAll(dbc, null, stmt, null);
947
948                 // read the parent id
949
String JavaDoc parentId = internalReadParentId(dbc, onlineProject.getId(), resourcePath);
950
951                 // create the structure record online
952
stmt = m_sqlManager.getPreparedStatement(conn, onlineProject, "C_STRUCTURE_WRITE");
953                 stmt.setString(1, offlineResource.getStructureId().toString());
954                 stmt.setString(2, offlineResource.getResourceId().toString());
955                 stmt.setString(3, resourcePath);
956                 stmt.setInt(4, CmsResource.STATE_UNCHANGED);
957                 stmt.setLong(5, offlineResource.getDateReleased());
958                 stmt.setLong(6, offlineResource.getDateExpired());
959                 stmt.setString(7, parentId);
960                 stmt.executeUpdate();
961             }
962         } catch (SQLException JavaDoc e) {
963             throw new CmsDbSqlException(Messages.get().container(
964                 Messages.ERR_GENERIC_SQL_1,
965                 CmsDbSqlException.getErrorQuery(stmt)), e);
966         } finally {
967             m_sqlManager.closeAll(dbc, conn, stmt, null);
968         }
969     }
970
971     /**
972      * @see org.opencms.db.I_CmsVfsDriver#readChildResources(org.opencms.db.CmsDbContext, org.opencms.file.CmsProject, org.opencms.file.CmsResource, boolean, boolean)
973      */

974     public List JavaDoc readChildResources(
975         CmsDbContext dbc,
976         CmsProject currentProject,
977         CmsResource resource,
978         boolean getFolders,
979         boolean getFiles) throws CmsDataAccessException {
980
981         List JavaDoc result = new ArrayList JavaDoc();
982         int projectId = currentProject.getId();
983
984         String JavaDoc resourceTypeClause;
985         if (getFolders && getFiles) {
986             resourceTypeClause = null;
987         } else if (getFolders) {
988             resourceTypeClause = m_sqlManager.readQuery(projectId, "C_RESOURCES_GET_SUBRESOURCES_GET_FOLDERS");
989         } else {
990             resourceTypeClause = m_sqlManager.readQuery(projectId, "C_RESOURCES_GET_SUBRESOURCES_GET_FILES");
991         }
992         StringBuffer JavaDoc query = new StringBuffer JavaDoc();
993         query.append(m_sqlManager.readQuery(projectId, "C_RESOURCES_GET_SUBRESOURCES"));
994         if (resourceTypeClause != null) {
995             query.append(' ');
996             query.append(resourceTypeClause);
997         }
998
999         String JavaDoc typeColumn = m_sqlManager.readQuery("C_RESOURCES_RESOURCE_TYPE");
1000
1001        Connection JavaDoc conn = null;
1002        PreparedStatement JavaDoc stmt = null;
1003        ResultSet JavaDoc res = null;
1004        try {
1005            conn = m_sqlManager.getConnection(dbc, currentProject.getId());
1006            stmt = m_sqlManager.getPreparedStatementForSql(conn, query.toString());
1007            stmt.setString(1, resource.getStructureId().toString());
1008            res = stmt.executeQuery();
1009
1010            while (res.next()) {
1011                int type = res.getInt(typeColumn);
1012                if (CmsFolder.isFolderType(type)) {
1013                    result.add(createFolder(res, projectId, false));
1014                } else {
1015                    result.add(createFile(res, projectId, false));
1016                }
1017            }
1018        } catch (SQLException JavaDoc e) {
1019            throw new CmsDbSqlException(Messages.get().container(
1020                Messages.ERR_GENERIC_SQL_1,
1021                CmsDbSqlException.getErrorQuery(stmt)), e);
1022        } finally {
1023            m_sqlManager.closeAll(dbc, conn, stmt, res);
1024        }
1025
1026        // sort result in memory, this is to avoid DB dependencies in the result order
1027
Collections.sort(result, CmsResource.COMPARE_ROOT_PATH_IGNORE_CASE_FOLDERS_FIRST);
1028        return result;
1029    }
1030
1031    /**
1032     * @see org.opencms.db.I_CmsVfsDriver#readFile(org.opencms.db.CmsDbContext, int, boolean, org.opencms.util.CmsUUID)
1033     */

1034    public CmsFile readFile(CmsDbContext dbc, int projectId, boolean includeDeleted, CmsUUID structureId)
1035    throws CmsDataAccessException {
1036
1037        CmsFile file = null;
1038        PreparedStatement JavaDoc stmt = null;
1039        ResultSet JavaDoc res = null;
1040        Connection JavaDoc conn = null;
1041
1042        try {
1043            conn = m_sqlManager.getConnection(dbc, projectId);
1044
1045            stmt = m_sqlManager.getPreparedStatement(conn, projectId, "C_FILES_READ");
1046            stmt.setString(1, structureId.toString());
1047            res = stmt.executeQuery();
1048
1049            if (res.next()) {
1050                file = createFile(res, projectId);
1051                while (res.next()) {
1052                    // do nothing only move through all rows because of mssql odbc driver
1053
}
1054            } else {
1055                throw new CmsVfsResourceNotFoundException(Messages.get().container(
1056                    Messages.ERR_READ_FILE_WITH_STRUCTURE_ID_1,
1057                    structureId));
1058            }
1059        } catch (SQLException JavaDoc e) {
1060            throw new CmsDbSqlException(Messages.get().container(
1061                Messages.ERR_GENERIC_SQL_1,
1062                CmsDbSqlException.getErrorQuery(stmt)), e);
1063        } finally {
1064            m_sqlManager.closeAll(dbc, conn, stmt, res);
1065        }
1066
1067        // check if this resource is marked as deleted and if we are allowed to return a deleted resource
1068
if (file != null && file.getState() == org.opencms.file.CmsResource.STATE_DELETED && !includeDeleted) {
1069            throw new CmsVfsException(Messages.get().container(
1070                Messages.ERR_READ_DELETED_FILE_1,
1071                dbc.removeSiteRoot(file.getRootPath())));
1072        }
1073
1074        return file;
1075    }
1076
1077    /**
1078     * @see org.opencms.db.I_CmsVfsDriver#readFolder(org.opencms.db.CmsDbContext, int, org.opencms.util.CmsUUID)
1079     */

1080    public CmsFolder readFolder(CmsDbContext dbc, int projectId, CmsUUID folderId) throws CmsDataAccessException {
1081
1082        CmsFolder folder = null;
1083        ResultSet JavaDoc res = null;
1084        PreparedStatement JavaDoc stmt = null;
1085        Connection JavaDoc conn = null;
1086
1087        try {
1088            conn = m_sqlManager.getConnection(dbc, projectId);
1089            stmt = m_sqlManager.getPreparedStatement(conn, projectId, "C_RESOURCES_READBYID");
1090            stmt.setString(1, folderId.toString());
1091            res = stmt.executeQuery();
1092
1093            if (res.next()) {
1094                folder = createFolder(res, projectId, true);
1095                while (res.next()) {
1096                    // do nothing only move through all rows because of mssql odbc driver
1097
}
1098            } else {
1099                throw new CmsVfsResourceNotFoundException(Messages.get().container(
1100                    Messages.ERR_READ_FOLDER_WITH_ID_1,
1101                    folderId));
1102            }
1103        } catch (SQLException JavaDoc e) {
1104            throw new CmsDbSqlException(Messages.get().container(
1105                Messages.ERR_GENERIC_SQL_1,
1106                CmsDbSqlException.getErrorQuery(stmt)), e);
1107        } finally {
1108            m_sqlManager.closeAll(dbc, conn, stmt, res);
1109        }
1110
1111        return folder;
1112    }
1113
1114    /**
1115     * @see org.opencms.db.I_CmsVfsDriver#readFolder(org.opencms.db.CmsDbContext, int, java.lang.String)
1116     */

1117    public CmsFolder readFolder(CmsDbContext dbc, int projectId, String JavaDoc folderPath) throws CmsDataAccessException {
1118
1119        CmsFolder folder = null;
1120        ResultSet JavaDoc res = null;
1121        PreparedStatement JavaDoc stmt = null;
1122        Connection JavaDoc conn = null;
1123
1124        folderPath = removeTrailingSeparator(folderPath);
1125
1126        try {
1127            conn = m_sqlManager.getConnection(dbc, projectId);
1128            stmt = m_sqlManager.getPreparedStatement(conn, projectId, "C_RESOURCES_READ");
1129
1130            stmt.setString(1, folderPath);
1131            res = stmt.executeQuery();
1132
1133            if (res.next()) {
1134                folder = createFolder(res, projectId, true);
1135                while (res.next()) {
1136                    // do nothing only move through all rows because of mssql odbc driver
1137
}
1138            } else {
1139                throw new CmsVfsResourceNotFoundException(Messages.get().container(
1140                    Messages.ERR_READ_FOLDER_1,
1141                    dbc.removeSiteRoot(folderPath)));
1142            }
1143        } catch (SQLException JavaDoc e) {
1144            throw new CmsDbSqlException(Messages.get().container(
1145                Messages.ERR_GENERIC_SQL_1,
1146                CmsDbSqlException.getErrorQuery(stmt)), e);
1147        } finally {
1148            m_sqlManager.closeAll(dbc, conn, stmt, res);
1149        }
1150
1151        return folder;
1152
1153    }
1154
1155    /**
1156     * @see org.opencms.db.I_CmsVfsDriver#readPropertyDefinition(org.opencms.db.CmsDbContext, java.lang.String, int)
1157     */

1158    public CmsPropertyDefinition readPropertyDefinition(CmsDbContext dbc, String JavaDoc name, int projectId)
1159    throws CmsDataAccessException {
1160
1161        CmsPropertyDefinition propDef = null;
1162        ResultSet JavaDoc res = null;
1163        PreparedStatement JavaDoc stmt = null;
1164        Connection JavaDoc conn = null;
1165
1166        try {
1167            conn = m_sqlManager.getConnection(dbc, projectId);
1168            stmt = m_sqlManager.getPreparedStatement(conn, projectId, "C_PROPERTYDEF_READ");
1169            stmt.setString(1, name);
1170            res = stmt.executeQuery();
1171
1172            // if resultset exists - return it
1173
if (res.next()) {
1174                propDef = new CmsPropertyDefinition(
1175                    new CmsUUID(res.getString(m_sqlManager.readQuery("C_PROPERTYDEF_ID"))),
1176                    res.getString(m_sqlManager.readQuery("C_PROPERTYDEF_NAME")));
1177            } else {
1178                throw new CmsDbEntryNotFoundException(Messages.get().container(
1179                    Messages.ERR_NO_PROPERTYDEF_WITH_NAME_1,
1180                    name));
1181            }
1182        } catch (SQLException JavaDoc e) {
1183            throw new CmsDbSqlException(Messages.get().container(
1184                Messages.ERR_GENERIC_SQL_1,
1185                CmsDbSqlException.getErrorQuery(stmt)), e);
1186        } finally {
1187            m_sqlManager.closeAll(dbc, conn, stmt, res);
1188        }
1189
1190        return propDef;
1191    }
1192
1193    /**
1194     * @see org.opencms.db.I_CmsVfsDriver#readPropertyDefinitions(org.opencms.db.CmsDbContext, int)
1195     */

1196    public List JavaDoc readPropertyDefinitions(CmsDbContext dbc, int projectId) throws CmsDataAccessException {
1197
1198        ArrayList JavaDoc propertyDefinitions = new ArrayList JavaDoc();
1199        ResultSet JavaDoc res = null;
1200        PreparedStatement JavaDoc stmt = null;
1201        Connection JavaDoc conn = null;
1202
1203        try {
1204            conn = m_sqlManager.getConnection(dbc);
1205            stmt = m_sqlManager.getPreparedStatement(conn, projectId, "C_PROPERTYDEF_READALL");
1206
1207            res = stmt.executeQuery();
1208            while (res.next()) {
1209                propertyDefinitions.add(new CmsPropertyDefinition(
1210                    new CmsUUID(res.getString(m_sqlManager.readQuery("C_PROPERTYDEF_ID"))),
1211                    res.getString(m_sqlManager.readQuery("C_PROPERTYDEF_NAME"))));
1212            }
1213        } catch (SQLException JavaDoc e) {
1214            throw new CmsDbSqlException(Messages.get().container(
1215                Messages.ERR_GENERIC_SQL_1,
1216                CmsDbSqlException.getErrorQuery(stmt)), e);
1217        } finally {
1218            m_sqlManager.closeAll(dbc, conn, stmt, res);
1219        }
1220        return propertyDefinitions;
1221    }
1222
1223    /**
1224     * @see org.opencms.db.I_CmsVfsDriver#readPropertyObject(org.opencms.db.CmsDbContext, java.lang.String, org.opencms.file.CmsProject, org.opencms.file.CmsResource)
1225     */

1226    public CmsProperty readPropertyObject(CmsDbContext dbc, String JavaDoc key, CmsProject project, CmsResource resource)
1227    throws CmsDataAccessException {
1228
1229        ResultSet JavaDoc res = null;
1230        PreparedStatement JavaDoc stmt = null;
1231        Connection JavaDoc conn = null;
1232        String JavaDoc propertyValue = null;
1233        int mappingType = -1;
1234        CmsProperty property = null;
1235        int resultSize = 0;
1236
1237        String JavaDoc resourceName = resource.getRootPath();
1238        if ((resource.isFolder()) && (!resourceName.endsWith("/"))) {
1239            resourceName += "/";
1240        }
1241        try {
1242            conn = m_sqlManager.getConnection(dbc, project.getId());
1243            stmt = m_sqlManager.getPreparedStatement(conn, project.getId(), "C_PROPERTIES_READ");
1244
1245            stmt.setString(1, key);
1246            stmt.setString(2, resource.getStructureId().toString());
1247            stmt.setString(3, resource.getResourceId().toString());
1248            res = stmt.executeQuery();
1249
1250            while (res.next()) {
1251                if (resultSize >= 2) {
1252                    throw new CmsDbConsistencyException(Messages.get().container(
1253                        Messages.ERR_TOO_MANY_PROPERTIES_3,
1254                        key,
1255                        resource.getRootPath(),
1256                        new Integer JavaDoc(resultSize)));
1257                }
1258
1259                if (property == null) {
1260                    property = new CmsProperty();
1261                    property.setName(key);
1262                }
1263
1264                propertyValue = res.getString(1);
1265                mappingType = res.getInt(2);
1266
1267                if (mappingType == CmsProperty.STRUCTURE_RECORD_MAPPING) {
1268                    property.setStructureValue(propertyValue);
1269                } else if (mappingType == CmsProperty.RESOURCE_RECORD_MAPPING) {
1270                    property.setResourceValue(propertyValue);
1271                } else {
1272                    throw new CmsDbConsistencyException(Messages.get().container(
1273                        Messages.ERR_UNKNOWN_PROPERTY_VALUE_MAPPING_3,
1274                        resource.getRootPath(),
1275                        new Integer JavaDoc(mappingType),
1276                        key));
1277                }
1278
1279                resultSize++;
1280            }
1281        } catch (SQLException JavaDoc e) {
1282            throw new CmsDbSqlException(Messages.get().container(
1283                Messages.ERR_GENERIC_SQL_1,
1284                CmsDbSqlException.getErrorQuery(stmt)), e);
1285        } finally {
1286            m_sqlManager.closeAll(dbc, conn, stmt, res);
1287        }
1288
1289        return (property != null) ? property : CmsProperty.getNullProperty();
1290    }
1291
1292    /**
1293     * @see org.opencms.db.I_CmsVfsDriver#readPropertyObjects(org.opencms.db.CmsDbContext, org.opencms.file.CmsProject, org.opencms.file.CmsResource)
1294     */

1295    public List JavaDoc readPropertyObjects(CmsDbContext dbc, CmsProject project, CmsResource resource)
1296    throws CmsDataAccessException {
1297
1298        ResultSet JavaDoc res = null;
1299        PreparedStatement JavaDoc stmt = null;
1300        Connection JavaDoc conn = null;
1301        String JavaDoc propertyKey = null;
1302        String JavaDoc propertyValue = null;
1303        int mappingType = -1;
1304        Map JavaDoc propertyMap = new HashMap JavaDoc();
1305        CmsProperty property = null;
1306
1307        String JavaDoc resourceName = resource.getRootPath();
1308        if ((resource.isFolder()) && (!resourceName.endsWith("/"))) {
1309            resourceName += "/";
1310        }
1311
1312        try {
1313            conn = m_sqlManager.getConnection(dbc, project.getId());
1314            stmt = m_sqlManager.getPreparedStatement(conn, project.getId(), "C_PROPERTIES_READALL");
1315            stmt.setString(1, resource.getStructureId().toString());
1316            stmt.setString(2, resource.getResourceId().toString());
1317            res = stmt.executeQuery();
1318
1319            while (res.next()) {
1320                propertyKey = null;
1321                propertyValue = null;
1322                mappingType = -1;
1323
1324                propertyKey = res.getString(1);
1325                propertyValue = res.getString(2);
1326                mappingType = res.getInt(3);
1327
1328                property = (CmsProperty)propertyMap.get(propertyKey);
1329                if (property == null) {
1330                    // there doesn't exist a property object for this key yet
1331
property = new CmsProperty();
1332                    property.setName(propertyKey);
1333                    propertyMap.put(propertyKey, property);
1334                }
1335
1336                if (mappingType == CmsProperty.STRUCTURE_RECORD_MAPPING) {
1337                    // this property value is mapped to a structure record
1338
property.setStructureValue(propertyValue);
1339                } else if (mappingType == CmsProperty.RESOURCE_RECORD_MAPPING) {
1340                    // this property value is mapped to a resource record
1341
property.setResourceValue(propertyValue);
1342                } else {
1343                    throw new CmsDbConsistencyException(Messages.get().container(
1344                        Messages.ERR_UNKNOWN_PROPERTY_VALUE_MAPPING_3,
1345                        resource.getRootPath(),
1346                        new Integer JavaDoc(mappingType),
1347                        propertyKey));
1348                }
1349            }
1350        } catch (SQLException JavaDoc e) {
1351            throw new CmsDbSqlException(Messages.get().container(
1352                Messages.ERR_GENERIC_SQL_1,
1353                CmsDbSqlException.getErrorQuery(stmt)), e);
1354        } finally {
1355            m_sqlManager.closeAll(dbc, conn, stmt, res);
1356        }
1357
1358        return new ArrayList JavaDoc(propertyMap.values());
1359    }
1360
1361    /**
1362     * @see org.opencms.db.I_CmsVfsDriver#readResource(org.opencms.db.CmsDbContext, int, org.opencms.util.CmsUUID, boolean)
1363     */

1364    public CmsResource readResource(CmsDbContext dbc, int projectId, CmsUUID structureId, boolean includeDeleted)
1365    throws CmsDataAccessException {
1366
1367        CmsResource resource = null;
1368        ResultSet JavaDoc res = null;
1369        PreparedStatement JavaDoc stmt = null;
1370        Connection JavaDoc conn = null;
1371
1372        try {
1373            conn = m_sqlManager.getConnection(dbc, projectId);
1374            stmt = m_sqlManager.getPreparedStatement(conn, projectId, "C_RESOURCES_READBYID");
1375
1376            stmt.setString(1, structureId.toString());
1377            res = stmt.executeQuery();
1378
1379            if (res.next()) {
1380                resource = createResource(res, projectId);
1381                while (res.next()) {
1382                    // do nothing only move through all rows because of mssql odbc driver
1383
}
1384            } else {
1385                throw new CmsVfsResourceNotFoundException(Messages.get().container(
1386                    Messages.ERR_READ_RESOURCE_WITH_ID_1,
1387                    structureId));
1388            }
1389        } catch (SQLException JavaDoc e) {
1390            throw new CmsDbSqlException(Messages.get().container(
1391                Messages.ERR_GENERIC_SQL_1,
1392                CmsDbSqlException.getErrorQuery(stmt)), e);
1393        } finally {
1394            m_sqlManager.closeAll(dbc, conn, stmt, res);
1395        }
1396
1397        // check if this resource is marked as deleted and if we are allowed to return a deleted resource
1398
if (resource != null && resource.getState() == org.opencms.file.CmsResource.STATE_DELETED && !includeDeleted) {
1399            throw new CmsVfsException(Messages.get().container(
1400                Messages.ERR_READ_DELETED_RESOURCE_1,
1401                dbc.removeSiteRoot(resource.getRootPath())));
1402        }
1403
1404        return resource;
1405    }
1406
1407    /**
1408     * @see org.opencms.db.I_CmsVfsDriver#readResource(org.opencms.db.CmsDbContext, int, java.lang.String, boolean)
1409     */

1410    public CmsResource readResource(CmsDbContext dbc, int projectId, String JavaDoc path, boolean includeDeleted)
1411    throws CmsDataAccessException {
1412
1413        CmsResource resource = null;
1414        ResultSet JavaDoc res = null;
1415        PreparedStatement JavaDoc stmt = null;
1416        Connection JavaDoc conn = null;
1417
1418        // must remove trailing slash
1419
path = removeTrailingSeparator(path);
1420
1421        try {
1422            conn = m_sqlManager.getConnection(dbc, projectId);
1423            stmt = m_sqlManager.getPreparedStatement(conn, projectId, "C_RESOURCES_READ");
1424
1425            stmt.setString(1, path);
1426            res = stmt.executeQuery();
1427
1428            if (res.next()) {
1429                resource = createResource(res, projectId);
1430                while (res.next()) {
1431                    // do nothing only move through all rows because of mssql odbc driver
1432
}
1433            } else {
1434                throw new CmsVfsResourceNotFoundException(Messages.get().container(
1435                    Messages.ERR_READ_RESOURCE_1,
1436                    dbc.removeSiteRoot(path)));
1437            }
1438        } catch (SQLException JavaDoc e) {
1439            throw new CmsDbSqlException(Messages.get().container(
1440                Messages.ERR_GENERIC_SQL_1,
1441                CmsDbSqlException.getErrorQuery(stmt)), e);
1442        } finally {
1443            m_sqlManager.closeAll(dbc, conn, stmt, res);
1444        }
1445
1446        // check if this resource is marked as deleted and if we are allowed to return a deleted resource
1447
if (resource != null && resource.getState() == CmsResource.STATE_DELETED && !includeDeleted) {
1448            throw new CmsVfsResourceNotFoundException(Messages.get().container(
1449                Messages.ERR_READ_DELETED_RESOURCE_1,
1450                dbc.removeSiteRoot(resource.getRootPath())));
1451        }
1452
1453        return resource;
1454    }
1455
1456    /**
1457     * @see org.opencms.db.I_CmsVfsDriver#readResources(org.opencms.db.CmsDbContext, int, int, int)
1458     */

1459    public List JavaDoc readResources(CmsDbContext dbc, int projectId, int state, int mode) throws CmsDataAccessException {
1460
1461        List JavaDoc result = new ArrayList JavaDoc();
1462
1463        ResultSet JavaDoc res = null;
1464        PreparedStatement JavaDoc stmt = null;
1465        Connection JavaDoc conn = null;
1466
1467        try {
1468            conn = m_sqlManager.getConnection(dbc, projectId);
1469            if (mode == CmsDriverManager.READMODE_MATCHSTATE) {
1470                stmt = m_sqlManager.getPreparedStatement(
1471                    conn,
1472                    projectId,
1473                    "C_RESOURCES_GET_RESOURCE_IN_PROJECT_WITH_STATE");
1474                stmt.setInt(1, projectId);
1475                stmt.setInt(2, state);
1476                stmt.setInt(3, state);
1477                stmt.setInt(4, state);
1478                stmt.setInt(5, state);
1479            } else if (mode == CmsDriverManager.READMODE_UNMATCHSTATE) {
1480                stmt = m_sqlManager.getPreparedStatement(
1481                    conn,
1482                    projectId,
1483                    "C_RESOURCES_GET_RESOURCE_IN_PROJECT_WITHOUT_STATE");
1484                stmt.setInt(1, projectId);
1485                stmt.setInt(2, state);
1486                stmt.setInt(3, state);
1487            } else {
1488                stmt = m_sqlManager.getPreparedStatement(
1489                    conn,
1490                    projectId,
1491                    "C_RESOURCES_GET_RESOURCE_IN_PROJECT_IGNORE_STATE");
1492                stmt.setInt(1, projectId);
1493            }
1494
1495            res = stmt.executeQuery();
1496            while (res.next()) {
1497                CmsResource resource = createResource(res, projectId);
1498                result.add(resource);
1499            }
1500        } catch (SQLException JavaDoc e) {
1501            throw new CmsDbSqlException(Messages.get().container(
1502                Messages.ERR_GENERIC_SQL_1,
1503                CmsDbSqlException.getErrorQuery(stmt)), e);
1504        } finally {
1505            m_sqlManager.closeAll(dbc, conn, stmt, res);
1506        }
1507
1508        return result;
1509    }
1510
1511    /**
1512     * @see org.opencms.db.I_CmsVfsDriver#readResourcesForPrincipalACE(org.opencms.db.CmsDbContext, org.opencms.file.CmsProject, org.opencms.util.CmsUUID)
1513     */

1514    public List JavaDoc readResourcesForPrincipalACE(CmsDbContext dbc, CmsProject project, CmsUUID principalId)
1515    throws CmsDataAccessException {
1516
1517        PreparedStatement JavaDoc stmt = null;
1518        Connection JavaDoc conn = null;
1519        ResultSet JavaDoc res = null;
1520        CmsResource currentResource = null;
1521        List JavaDoc resources = new ArrayList JavaDoc();
1522
1523        try {
1524            conn = m_sqlManager.getConnection(dbc, project.getId());
1525            stmt = m_sqlManager.getPreparedStatement(conn, project, "C_SELECT_RESOURCES_FOR_PRINCIPAL_ACE");
1526
1527            stmt.setString(1, principalId.toString());
1528            res = stmt.executeQuery();
1529
1530            while (res.next()) {
1531                currentResource = createFile(res, project.getId(), false);
1532                resources.add(currentResource);
1533            }
1534        } catch (SQLException JavaDoc e) {
1535            throw new CmsDbSqlException(Messages.get().container(
1536                Messages.ERR_GENERIC_SQL_1,
1537                CmsDbSqlException.getErrorQuery(stmt)), e);
1538        } finally {
1539            m_sqlManager.closeAll(dbc, conn, stmt, res);
1540        }
1541        return resources;
1542    }
1543
1544    /**
1545     * @see org.opencms.db.I_CmsVfsDriver#readResourcesForPrincipalAttr(org.opencms.db.CmsDbContext, org.opencms.file.CmsProject, org.opencms.util.CmsUUID)
1546     */

1547    public List JavaDoc readResourcesForPrincipalAttr(CmsDbContext dbc, CmsProject project, CmsUUID principalId)
1548    throws CmsDataAccessException {
1549
1550        PreparedStatement JavaDoc stmt = null;
1551        Connection JavaDoc conn = null;
1552        ResultSet JavaDoc res = null;
1553        CmsResource currentResource = null;
1554        List JavaDoc resources = new ArrayList JavaDoc();
1555
1556        try {
1557            conn = m_sqlManager.getConnection(dbc, project.getId());
1558            stmt = m_sqlManager.getPreparedStatement(conn, project, "C_SELECT_RESOURCES_FOR_PRINCIPAL_ATTR");
1559
1560            stmt.setString(1, principalId.toString());
1561            stmt.setString(2, principalId.toString());
1562            res = stmt.executeQuery();
1563
1564            while (res.next()) {
1565                currentResource = createFile(res, project.getId(), false);
1566                resources.add(currentResource);
1567            }
1568        } catch (SQLException JavaDoc e) {
1569            throw new CmsDbSqlException(Messages.get().container(
1570                Messages.ERR_GENERIC_SQL_1,
1571                CmsDbSqlException.getErrorQuery(stmt)), e);
1572        } finally {
1573            m_sqlManager.closeAll(dbc, conn, stmt, res);
1574        }
1575        return resources;
1576    }
1577
1578    /**
1579     * @see org.opencms.db.I_CmsVfsDriver#readResourcesWithProperty(org.opencms.db.CmsDbContext, int, org.opencms.util.CmsUUID, String)
1580     */

1581    public List JavaDoc readResourcesWithProperty(CmsDbContext dbc, int projectId, CmsUUID propertyDef, String JavaDoc path)
1582    throws CmsDataAccessException {
1583
1584        List JavaDoc resources = new ArrayList JavaDoc();
1585        ResultSet JavaDoc res = null;
1586        PreparedStatement JavaDoc stmt = null;
1587        Connection JavaDoc conn = null;
1588
1589        try {
1590            conn = m_sqlManager.getConnection(dbc, projectId);
1591            stmt = m_sqlManager.getPreparedStatement(conn, projectId, "C_RESOURCES_GET_RESOURCE_WITH_PROPERTYDEF");
1592            stmt.setString(1, propertyDef.toString());
1593            stmt.setString(2, path + "%");
1594            stmt.setString(3, propertyDef.toString());
1595            stmt.setString(4, path + "%");
1596            res = stmt.executeQuery();
1597
1598            while (res.next()) {
1599                CmsResource resource = createResource(res, projectId);
1600                resources.add(resource);
1601            }
1602        } catch (SQLException JavaDoc e) {
1603            throw new CmsDbSqlException(Messages.get().container(
1604                Messages.ERR_GENERIC_SQL_1,
1605                CmsDbSqlException.getErrorQuery(stmt)), e);
1606        } finally {
1607            m_sqlManager.closeAll(dbc, conn, stmt, res);
1608        }
1609
1610        return resources;
1611    }
1612
1613    /**
1614     * @see org.opencms.db.I_CmsVfsDriver#readResourcesWithProperty(org.opencms.db.CmsDbContext, int, org.opencms.util.CmsUUID, String, String)
1615     */

1616    public List JavaDoc readResourcesWithProperty(
1617        CmsDbContext dbc,
1618        int projectId,
1619        CmsUUID propertyDef,
1620        String JavaDoc path,
1621        String JavaDoc value) throws CmsDataAccessException {
1622
1623        List JavaDoc resources = new ArrayList JavaDoc();
1624        ResultSet JavaDoc res = null;
1625        PreparedStatement JavaDoc stmt = null;
1626        Connection JavaDoc conn = null;
1627
1628        try {
1629            conn = m_sqlManager.getConnection(dbc, projectId);
1630            stmt = m_sqlManager.getPreparedStatement(conn, projectId, "C_RESOURCES_GET_RESOURCE_WITH_PROPERTYDEF_VALUE");
1631            stmt.setString(1, propertyDef.toString());
1632            stmt.setString(2, path + "%");
1633            stmt.setString(3, "%" + value + "%");
1634            stmt.setString(4, propertyDef.toString());
1635            stmt.setString(5, "%" + path + "%");
1636            stmt.setString(6, value + "%");
1637            res = stmt.executeQuery();
1638
1639            while (res.next()) {
1640                CmsResource resource = createResource(res, projectId);
1641                resources.add(resource);
1642            }
1643        } catch (SQLException JavaDoc e) {
1644            throw new CmsDbSqlException(Messages.get().container(
1645                Messages.ERR_GENERIC_SQL_1,
1646                CmsDbSqlException.getErrorQuery(stmt)), e);
1647        } finally {
1648            m_sqlManager.closeAll(dbc, conn, stmt, res);
1649        }
1650
1651        return resources;
1652    }
1653
1654    /**
1655     * @see org.opencms.db.I_CmsVfsDriver#readResourceTree(org.opencms.db.CmsDbContext, int, java.lang.String, int, int, long, long, long, long, long, long, int)
1656     */

1657    public List JavaDoc readResourceTree(
1658        CmsDbContext dbc,
1659        int projectId,
1660        String JavaDoc parentPath,
1661        int type,
1662        int state,
1663        long lastModifiedAfter,
1664        long lastModifiedBefore,
1665        long releasedAfter,
1666        long releasedBefore,
1667        long expiredAfter,
1668        long expiredBefore,
1669        int mode) throws CmsDataAccessException {
1670
1671        List JavaDoc result = new ArrayList JavaDoc();
1672
1673        StringBuffer JavaDoc conditions = new StringBuffer JavaDoc();
1674        List JavaDoc params = new ArrayList JavaDoc(5);
1675
1676        // prepare the selection criteria
1677
prepareProjectCondition(projectId, mode, conditions, params);
1678        prepareResourceCondition(projectId, mode, conditions);
1679        prepareTypeCondition(projectId, type, mode, conditions, params);
1680        prepareTimeRangeCondition(projectId, lastModifiedAfter, lastModifiedBefore, conditions, params);
1681        prepareReleasedTimeRangeCondition(projectId, releasedAfter, releasedBefore, conditions, params);
1682        prepareExpiredTimeRangeCondition(projectId, expiredAfter, expiredBefore, conditions, params);
1683        preparePathCondition(projectId, parentPath, mode, conditions, params);
1684        prepareStateCondition(projectId, state, mode, conditions, params);
1685
1686        // now read matching resources within the subtree
1687
ResultSet JavaDoc res = null;
1688        PreparedStatement JavaDoc stmt = null;
1689        Connection JavaDoc conn = null;
1690
1691        try {
1692            conn = m_sqlManager.getConnection(dbc, projectId);
1693            StringBuffer JavaDoc queryBuf = new StringBuffer JavaDoc(256);
1694            queryBuf.append(m_sqlManager.readQuery(projectId, "C_RESOURCES_READ_TREE"));
1695            queryBuf.append(conditions);
1696            queryBuf.append(" ");
1697            queryBuf.append(m_sqlManager.readQuery(projectId, "C_RESOURCES_ORDER_BY_PATH"));
1698            stmt = m_sqlManager.getPreparedStatementForSql(conn, queryBuf.toString());
1699
1700            for (int i = 0; i < params.size(); i++) {
1701                stmt.setString(i + 1, (String JavaDoc)params.get(i));
1702            }
1703
1704            res = stmt.executeQuery();
1705            while (res.next()) {
1706                CmsResource resource = createResource(res, projectId);
1707                result.add(resource);
1708            }
1709
1710        } catch (SQLException JavaDoc e) {
1711            throw new CmsDbSqlException(Messages.get().container(
1712                Messages.ERR_GENERIC_SQL_1,
1713                CmsDbSqlException.getErrorQuery(stmt)), e);
1714        } finally {
1715            m_sqlManager.closeAll(dbc, conn, stmt, res);
1716        }
1717
1718        return result;
1719    }
1720
1721    /**
1722     * @see org.opencms.db.I_CmsVfsDriver#readSiblings(org.opencms.db.CmsDbContext, org.opencms.file.CmsProject, org.opencms.file.CmsResource, boolean)
1723     */

1724    public List JavaDoc readSiblings(CmsDbContext dbc, CmsProject currentProject, CmsResource resource, boolean includeDeleted)
1725    throws CmsDataAccessException {
1726
1727        PreparedStatement JavaDoc stmt = null;
1728        Connection JavaDoc conn = null;
1729        ResultSet JavaDoc res = null;
1730        CmsResource currentResource = null;
1731        List JavaDoc vfsLinks = new ArrayList JavaDoc();
1732
1733        try {
1734            conn = m_sqlManager.getConnection(dbc, currentProject.getId());
1735
1736            if (includeDeleted) {
1737                stmt = m_sqlManager.getPreparedStatement(conn, currentProject, "C_SELECT_VFS_SIBLINGS");
1738            } else {
1739                stmt = m_sqlManager.getPreparedStatement(conn, currentProject, "C_SELECT_NONDELETED_VFS_SIBLINGS");
1740            }
1741
1742            stmt.setString(1, resource.getResourceId().toString());
1743            res = stmt.executeQuery();
1744
1745            while (res.next()) {
1746                currentResource = createFile(res, currentProject.getId(), false);
1747                vfsLinks.add(currentResource);
1748            }
1749        } catch (SQLException JavaDoc e) {
1750            throw new CmsDbSqlException(Messages.get().container(
1751                Messages.ERR_GENERIC_SQL_1,
1752                CmsDbSqlException.getErrorQuery(stmt)), e);
1753        } finally {
1754            m_sqlManager.closeAll(dbc, conn, stmt, res);
1755        }
1756
1757        return vfsLinks;
1758    }
1759
1760    /**
1761     * @see org.opencms.db.I_CmsVfsDriver#removeFile(org.opencms.db.CmsDbContext, org.opencms.file.CmsProject, org.opencms.file.CmsResource, boolean)
1762     */

1763    public void removeFile(CmsDbContext dbc, CmsProject currentProject, CmsResource resource, boolean removeFileContent)
1764    throws CmsDataAccessException {
1765
1766        PreparedStatement JavaDoc stmt = null;
1767        Connection JavaDoc conn = null;
1768        int siblingCount = 0;
1769
1770        try {
1771            conn = m_sqlManager.getConnection(dbc, currentProject.getId());
1772
1773            // delete the structure recourd
1774
stmt = m_sqlManager.getPreparedStatement(conn, currentProject, "C_STRUCTURE_DELETE_BY_STRUCTUREID");
1775            stmt.setString(1, resource.getStructureId().toString());
1776            stmt.executeUpdate();
1777
1778            m_sqlManager.closeAll(dbc, null, stmt, null);
1779
1780            // count the references to the resource
1781
siblingCount = internalCountSiblings(dbc, currentProject.getId(), resource.getResourceId());
1782
1783            if (siblingCount > 0) {
1784
1785                // update the link Count
1786
stmt = m_sqlManager.getPreparedStatement(conn, currentProject, "C_RESOURCES_UPDATE_SIBLING_COUNT");
1787                stmt.setInt(1, this.internalCountSiblings(dbc, currentProject.getId(), resource.getResourceId()));
1788                stmt.setString(2, resource.getResourceId().toString());
1789                stmt.executeUpdate();
1790
1791                m_sqlManager.closeAll(dbc, null, stmt, null);
1792
1793                // update the resource flags
1794
stmt = m_sqlManager.getPreparedStatement(conn, currentProject, "C_RESOURCES_UPDATE_FLAGS");
1795                stmt.setInt(1, resource.getFlags());
1796                stmt.setString(2, resource.getResourceId().toString());
1797                stmt.executeUpdate();
1798
1799            } else {
1800
1801                // if not referenced any longer, also delete the resource and the content record
1802
stmt = m_sqlManager.getPreparedStatement(conn, currentProject, "C_RESOURCES_DELETE_BY_RESOURCEID");
1803                stmt.setString(1, resource.getResourceId().toString());
1804                stmt.executeUpdate();
1805
1806                m_sqlManager.closeAll(dbc, null, stmt, null);
1807
1808                // delete content records with this resource id
1809
stmt = m_sqlManager.getPreparedStatement(conn, currentProject, "C_FILE_CONTENT_DELETE");
1810                stmt.setString(1, resource.getResourceId().toString());
1811                stmt.executeUpdate();
1812            }
1813
1814        } catch (SQLException JavaDoc e) {
1815            throw new CmsDbSqlException(Messages.get().container(
1816                Messages.ERR_GENERIC_SQL_1,
1817                CmsDbSqlException.getErrorQuery(stmt)), e);
1818        } finally {
1819            m_sqlManager.closeAll(dbc, conn, stmt, null);
1820        }
1821    }
1822
1823    /**
1824     * @see org.opencms.db.I_CmsVfsDriver#removeFolder(org.opencms.db.CmsDbContext, org.opencms.file.CmsProject, org.opencms.file.CmsResource)
1825     */

1826    public void removeFolder(CmsDbContext dbc, CmsProject currentProject, CmsResource resource)
1827    throws CmsDataAccessException {
1828
1829        // the current implementation only deletes empty folders
1830

1831        // check if the folder has any files in it
1832
List JavaDoc files = readChildResources(dbc, currentProject, resource, false, true);
1833
1834        // remove deleted files from the child resources
1835
if (files.size() == 0) {
1836
1837            // check if the folder has any folders in it
1838
List JavaDoc folders = readChildResources(dbc, currentProject, resource, true, false);
1839
1840            // remove deleted folders from the child resources
1841
if (folders.size() == 0) {
1842                internalRemoveFolder(dbc, currentProject, resource);
1843            } else {
1844                String JavaDoc errorResNames = "";
1845
1846                for (int i = 0, n = folders.size(); i < n; i++) {
1847                    CmsResource errorRes = (CmsResource)folders.get(i);
1848                    errorResNames += "[" + dbc.removeSiteRoot(errorRes.getRootPath()) + "]";
1849                }
1850
1851                throw new CmsVfsException(Messages.get().container(
1852                    Messages.ERR_DELETE_NONEMTY_FOLDER_2,
1853                    dbc.removeSiteRoot(resource.getRootPath()),
1854                    errorResNames));
1855            }
1856        } else {
1857            String JavaDoc errorResNames = "";
1858
1859            for (int i = 0, n = files.size(); i < n; i++) {
1860                CmsResource errorRes = (CmsResource)files.get(i);
1861                errorResNames += "[" + errorRes.getName() + "]";
1862            }
1863
1864            throw new CmsVfsException(Messages.get().container(
1865                Messages.ERR_DELETE_NONEMTY_FOLDER_2,
1866                dbc.removeSiteRoot(resource.getRootPath()),
1867                errorResNames));
1868        }
1869    }
1870
1871    /**
1872     * @see org.opencms.db.I_CmsVfsDriver#replaceResource(org.opencms.db.CmsDbContext, org.opencms.file.CmsResource, byte[], int)
1873     */

1874    public void replaceResource(CmsDbContext dbc, CmsResource newResource, byte[] resContent, int newResourceType)
1875    throws CmsDataAccessException {
1876
1877        Connection JavaDoc conn = null;
1878        PreparedStatement JavaDoc stmt = null;
1879
1880        try {
1881            // write the file content
1882
if (resContent != null) {
1883                writeContent(dbc, dbc.currentProject(), newResource.getResourceId(), resContent);
1884            }
1885
1886            // update the resource record
1887
conn = m_sqlManager.getConnection(dbc, dbc.currentProject().getId());
1888            stmt = m_sqlManager.getPreparedStatement(conn, dbc.currentProject(), "C_RESOURCE_REPLACE");
1889            stmt.setInt(1, newResourceType);
1890            stmt.setInt(2, resContent.length);
1891            stmt.setString(3, newResource.getResourceId().toString());
1892            stmt.executeUpdate();
1893
1894        } catch (SQLException JavaDoc e) {
1895            throw new CmsDbSqlException(Messages.get().container(
1896                Messages.ERR_GENERIC_SQL_1,
1897                CmsDbSqlException.getErrorQuery(stmt)), e);
1898        } finally {
1899            m_sqlManager.closeAll(dbc, conn, stmt, null);
1900        }
1901    }
1902
1903    /**
1904     * @see org.opencms.db.I_CmsVfsDriver#transferResource(org.opencms.db.CmsDbContext, org.opencms.file.CmsProject, org.opencms.file.CmsResource, org.opencms.util.CmsUUID, org.opencms.util.CmsUUID)
1905     */

1906    public void transferResource(
1907        CmsDbContext dbc,
1908        CmsProject project,
1909        CmsResource resource,
1910        CmsUUID createdUser,
1911        CmsUUID lastModifiedUser) throws CmsDataAccessException {
1912
1913        if (createdUser == null) {
1914            createdUser = resource.getUserCreated();
1915        }
1916        if (lastModifiedUser == null) {
1917            lastModifiedUser = resource.getUserLastModified();
1918        }
1919
1920        PreparedStatement JavaDoc stmt = null;
1921        Connection JavaDoc conn = null;
1922        try {
1923            conn = m_sqlManager.getConnection(dbc, project.getId());
1924            stmt = m_sqlManager.getPreparedStatement(conn, project, "C_RESOURCES_TRANSFER_RESOURCE");
1925            stmt.setString(1, createdUser.toString());
1926            stmt.setString(2, lastModifiedUser.toString());
1927            stmt.setString(3, resource.getResourceId().toString());
1928            stmt.executeUpdate();
1929        } catch (SQLException JavaDoc e) {
1930            throw new CmsDbSqlException(Messages.get().container(
1931                Messages.ERR_GENERIC_SQL_1,
1932                CmsDbSqlException.getErrorQuery(stmt)), e);
1933        } finally {
1934            m_sqlManager.closeAll(dbc, conn, stmt, null);
1935        }
1936    }
1937
1938    /**
1939     * @see org.opencms.db.I_CmsVfsDriver#validateResourceIdExists(org.opencms.db.CmsDbContext, int, org.opencms.util.CmsUUID)
1940     */

1941    public boolean validateResourceIdExists(CmsDbContext dbc, int projectId, CmsUUID resourceId)
1942    throws CmsDataAccessException {
1943
1944        Connection JavaDoc conn = null;
1945        PreparedStatement JavaDoc stmt = null;
1946        ResultSet JavaDoc res = null;
1947        boolean exists = false;
1948
1949        try {
1950            conn = m_sqlManager.getConnection(dbc, projectId);
1951            stmt = m_sqlManager.getPreparedStatement(conn, projectId, "C_RESOURCES_READ_RESOURCE_STATE");
1952            stmt.setString(1, resourceId.toString());
1953
1954            res = stmt.executeQuery();
1955            exists = res.next();
1956        } catch (SQLException JavaDoc e) {
1957            throw new CmsDbSqlException(Messages.get().container(
1958                Messages.ERR_GENERIC_SQL_1,
1959                CmsDbSqlException.getErrorQuery(stmt)), e);
1960        } finally {
1961            m_sqlManager.closeAll(dbc, conn, stmt, res);
1962        }
1963
1964        return exists;
1965    }
1966
1967    /**
1968     * @see org.opencms.db.I_CmsVfsDriver#validateStructureIdExists(org.opencms.db.CmsDbContext, int, org.opencms.util.CmsUUID)
1969     */

1970    public boolean validateStructureIdExists(CmsDbContext dbc, int projectId, CmsUUID structureId)
1971    throws CmsDataAccessException {
1972
1973        Connection JavaDoc conn = null;
1974        PreparedStatement JavaDoc stmt = null;
1975        ResultSet JavaDoc res = null;
1976        boolean found = false;
1977        int count = 0;
1978
1979        try {
1980            conn = m_sqlManager.getConnection(dbc, projectId);
1981            stmt = m_sqlManager.getPreparedStatement(conn, projectId, "C_RESOURCES_SELECT_STRUCTURE_ID");
1982            stmt.setString(1, structureId.toString());
1983
1984            res = stmt.executeQuery();
1985            if (res.next()) {
1986                count = res.getInt(1);
1987                found = (count == 1);
1988            } else {
1989                found = false;
1990            }
1991        } catch (SQLException JavaDoc e) {
1992            throw new CmsDbSqlException(Messages.get().container(
1993                Messages.ERR_GENERIC_SQL_1,
1994                CmsDbSqlException.getErrorQuery(stmt)), e);
1995        } finally {
1996            m_sqlManager.closeAll(dbc, conn, stmt, res);
1997        }
1998
1999        return found;
2000    }
2001
2002    /**
2003     * @see org.opencms.db.I_CmsVfsDriver#writeContent(org.opencms.db.CmsDbContext, org.opencms.file.CmsProject, org.opencms.util.CmsUUID, byte[])
2004     */

2005    public void writeContent(CmsDbContext dbc, CmsProject project, CmsUUID resourceId, byte[] content)
2006    throws CmsDataAccessException {
2007
2008        Connection JavaDoc conn = null;
2009        PreparedStatement JavaDoc stmt = null;
2010
2011        try {
2012            conn = m_sqlManager.getConnection(dbc, project.getId());
2013            stmt = m_sqlManager.getPreparedStatement(conn, project, "C_CONTENTS_UPDATE");
2014
2015            // update the file content in the FILES database.
2016
if (content.length < 2000) {
2017                stmt.setBytes(1, content);
2018            } else {
2019                stmt.setBinaryStream(1, new ByteArrayInputStream JavaDoc(content), content.length);
2020            }
2021
2022            stmt.setString(2, resourceId.toString());
2023            stmt.executeUpdate();
2024        } catch (SQLException JavaDoc e) {
2025            throw new CmsDbSqlException(Messages.get().container(
2026                Messages.ERR_GENERIC_SQL_1,
2027                CmsDbSqlException.getErrorQuery(stmt)), e);
2028        } finally {
2029            m_sqlManager.closeAll(dbc, conn, stmt, null);
2030        }
2031    }
2032
2033    /**
2034     * @see org.opencms.db.I_CmsVfsDriver#writeLastModifiedProjectId(org.opencms.db.CmsDbContext, org.opencms.file.CmsProject, int, org.opencms.file.CmsResource)
2035     */

2036    public void writeLastModifiedProjectId(CmsDbContext dbc, CmsProject project, int projectId, CmsResource resource)
2037    throws CmsDataAccessException {
2038
2039        Connection JavaDoc conn = null;
2040        PreparedStatement JavaDoc stmt = null;
2041
2042        try {
2043            conn = m_sqlManager.getConnection(dbc, project.getId());
2044            stmt = m_sqlManager.getPreparedStatement(conn, project, "C_RESOURCES_UPDATE_PROJECT_LASTMODIFIED");
2045            stmt.setInt(1, projectId);
2046            stmt.setString(2, resource.getResourceId().toString());
2047            stmt.executeUpdate();
2048        } catch (SQLException JavaDoc e) {
2049            throw new CmsDbSqlException(Messages.get().container(
2050                Messages.ERR_GENERIC_SQL_1,
2051                CmsDbSqlException.getErrorQuery(stmt)), e);
2052        } finally {
2053            m_sqlManager.closeAll(dbc, conn, stmt, null);
2054        }
2055    }
2056
2057    /**
2058     * @see org.opencms.db.I_CmsVfsDriver#writePropertyObject(org.opencms.db.CmsDbContext, org.opencms.file.CmsProject, org.opencms.file.CmsResource, org.opencms.file.CmsProperty)
2059     */

2060    public void writePropertyObject(CmsDbContext dbc, CmsProject project, CmsResource resource, CmsProperty property)
2061    throws CmsDataAccessException {
2062
2063        CmsPropertyDefinition propertyDefinition = null;
2064        PreparedStatement JavaDoc stmt = null;
2065        Connection JavaDoc conn = null;
2066        String JavaDoc value = null;
2067        int mappingType = -1;
2068        CmsUUID id = null;
2069        CmsProperty existingProperty = null;
2070        boolean existsPropertyValue = false;
2071        boolean deletePropertyValue = false;
2072
2073        try {
2074            // read the property definition
2075
propertyDefinition = readPropertyDefinition(dbc, property.getName(), project.getId());
2076        } catch (CmsDbEntryNotFoundException e) {
2077            propertyDefinition = null;
2078        }
2079
2080        if (propertyDefinition == null) {
2081            if (property.autoCreatePropertyDefinition()) {
2082                propertyDefinition = createPropertyDefinition(dbc, project.getId(), property.getName());
2083
2084                try {
2085                    readPropertyDefinition(dbc, property.getName(), CmsProject.ONLINE_PROJECT_ID);
2086                } catch (CmsDataAccessException e) {
2087                    createPropertyDefinition(dbc, CmsProject.ONLINE_PROJECT_ID, property.getName());
2088                }
2089
2090                try {
2091                    m_driverManager.getBackupDriver().readBackupPropertyDefinition(dbc, property.getName());
2092                } catch (CmsDataAccessException e) {
2093                    m_driverManager.getBackupDriver().createBackupPropertyDefinition(dbc, property.getName());
2094                }
2095            } else {
2096                throw new CmsDbEntryNotFoundException(Messages.get().container(
2097                    Messages.ERR_NO_PROPERTYDEF_WITH_NAME_1,
2098                    property.getName()));
2099            }
2100        }
2101
2102        String JavaDoc resourceName = resource.getRootPath();
2103        if (resource.isFolder() && !resourceName.endsWith("/")) {
2104            resourceName += "/";
2105        }
2106
2107        try {
2108            // read the existing property to test if we need the
2109
// insert or update query to write a property value
2110
existingProperty = readPropertyObject(dbc, propertyDefinition.getName(), project, resource);
2111
2112            if (existingProperty.isIdentical(property)) {
2113
2114                // property already has the identical values set, no write required
2115
return;
2116            }
2117
2118            conn = m_sqlManager.getConnection(dbc, project.getId());
2119
2120            for (int i = 0; i < 2; i++) {
2121                mappingType = -1;
2122                value = null;
2123                id = null;
2124                existsPropertyValue = false;
2125                deletePropertyValue = false;
2126
2127                // 1) take any required decisions to choose and fill the correct SQL query
2128

2129                if (i == 0) {
2130                    // write/delete the *structure value* on the first cycle
2131
if (existingProperty.getStructureValue() != null && property.isDeleteStructureValue()) {
2132                        // this property value is marked to be deleted
2133
deletePropertyValue = true;
2134                    } else {
2135                        value = property.getStructureValue();
2136                        if (CmsStringUtil.isEmptyOrWhitespaceOnly(value)) {
2137                            // no structure value set or the structure value is an empty string,
2138
// continue with the resource value
2139
continue;
2140                        }
2141                    }
2142
2143                    // set the vars to be written to the database
2144
mappingType = CmsProperty.STRUCTURE_RECORD_MAPPING;
2145                    id = resource.getStructureId();
2146                    existsPropertyValue = existingProperty.getStructureValue() != null;
2147                } else if (i == 1) {
2148                    // write/delete the *resource value* on the second cycle
2149
if (existingProperty.getResourceValue() != null && property.isDeleteResourceValue()) {
2150                        // this property value is marked to be deleted
2151
deletePropertyValue = true;
2152                    } else {
2153                        value = property.getResourceValue();
2154                        if (CmsStringUtil.isEmptyOrWhitespaceOnly(value)) {
2155                            // no resource value set or the resource value is an empty string,
2156
// break out of the loop
2157
break;
2158                        }
2159                    }
2160
2161                    // set the vars to be written to the database
2162
mappingType = CmsProperty.RESOURCE_RECORD_MAPPING;
2163                    id = resource.getResourceId();
2164                    existsPropertyValue = existingProperty.getResourceValue() != null;
2165                }
2166
2167                // 2) execute the SQL query
2168

2169                if (!deletePropertyValue) {
2170                    // insert/update the property value
2171
if (existsPropertyValue) {
2172                        // {structure|resource} property value already exists- use update statement
2173
stmt = m_sqlManager.getPreparedStatement(conn, project.getId(), "C_PROPERTIES_UPDATE");
2174                        stmt.setString(1, m_sqlManager.validateEmpty(value));
2175                        stmt.setString(2, id.toString());
2176                        stmt.setInt(3, mappingType);
2177                        stmt.setString(4, propertyDefinition.getId().toString());
2178                    } else {
2179                        // {structure|resource} property value doesen't exist- use create statement
2180
stmt = m_sqlManager.getPreparedStatement(conn, project.getId(), "C_PROPERTIES_CREATE");
2181                        stmt.setString(1, new CmsUUID().toString());
2182                        stmt.setString(2, propertyDefinition.getId().toString());
2183                        stmt.setString(3, id.toString());
2184                        stmt.setInt(4, mappingType);
2185                        stmt.setString(5, m_sqlManager.validateEmpty(value));
2186                    }
2187                } else {
2188                    // {structure|resource} property value marked as deleted- use delete statement
2189
stmt = m_sqlManager.getPreparedStatement(conn, project.getId(), "C_PROPERTIES_DELETE");
2190                    stmt.setString(1, propertyDefinition.getId().toString());
2191                    stmt.setString(2, id.toString());
2192                    stmt.setInt(3, mappingType);
2193                }
2194
2195                stmt.executeUpdate();
2196                m_sqlManager.closeAll(dbc, null, stmt, null);
2197            }
2198        } catch (SQLException JavaDoc e) {
2199            throw new CmsDbSqlException(Messages.get().container(
2200                Messages.ERR_GENERIC_SQL_1,
2201                CmsDbSqlException.getErrorQuery(stmt)), e);
2202        } finally {
2203            m_sqlManager.closeAll(dbc, conn, stmt, null);
2204        }
2205    }
2206
2207    /**
2208     * @see org.opencms.db.I_CmsVfsDriver#writePropertyObjects(org.opencms.db.CmsDbContext, org.opencms.file.CmsProject, org.opencms.file.CmsResource, java.util.List)
2209     */

2210    public void writePropertyObjects(CmsDbContext dbc, CmsProject project, CmsResource resource, List JavaDoc properties)
2211    throws CmsDataAccessException {
2212
2213        CmsProperty property = null;
2214
2215        for (int i = 0; i < properties.size(); i++) {
2216            property = (CmsProperty)properties.get(i);
2217            writePropertyObject(dbc, project, resource, property);
2218        }
2219    }
2220
2221    /**
2222     * @see org.opencms.db.I_CmsVfsDriver#writeResource(org.opencms.db.CmsDbContext, org.opencms.file.CmsProject, org.opencms.file.CmsResource, int)
2223     */

2224    public void writeResource(CmsDbContext dbc, CmsProject project, CmsResource resource, int changed)
2225    throws CmsDataAccessException {
2226
2227        // validate the resource length
2228
internalValidateResourceLength(resource);
2229
2230        String JavaDoc resourcePath = removeTrailingSeparator(resource.getRootPath());
2231
2232        // this task is split into two statements because some DBs (e.g. Oracle) doesnt support muti-table updates
2233
PreparedStatement JavaDoc stmt = null;
2234        Connection JavaDoc conn = null;
2235        long resourceDateModified;
2236
2237        if (resource.isTouched()) {
2238            resourceDateModified = resource.getDateLastModified();
2239        } else {
2240            resourceDateModified = System.currentTimeMillis();
2241        }
2242
2243        int structureState = resource.getState();
2244        int resourceState = resource.getState();
2245        int projectId = project.getId();
2246
2247        if (changed == CmsDriverManager.UPDATE_RESOURCE_STATE) {
2248            resourceState = org.opencms.file.CmsResource.STATE_CHANGED;
2249        } else if (changed == CmsDriverManager.UPDATE_STRUCTURE_STATE) {
2250            structureState = org.opencms.file.CmsResource.STATE_CHANGED;
2251        } else if (changed == CmsDriverManager.NOTHING_CHANGED) {
2252            projectId = resource.getProjectLastModified();
2253        } else {
2254            resourceState = org.opencms.file.CmsResource.STATE_CHANGED;
2255            structureState = org.opencms.file.CmsResource.STATE_CHANGED;
2256        }
2257
2258        try {
2259            conn = m_sqlManager.getConnection(dbc, project.getId());
2260            stmt = m_sqlManager.getPreparedStatement(conn, project, "C_RESOURCES_UPDATE_RESOURCES");
2261            stmt.setInt(1, resource.getTypeId());
2262            stmt.setInt(2, resource.getFlags());
2263            stmt.setLong(3, resourceDateModified);
2264            stmt.setString(4, resource.getUserLastModified().toString());
2265            stmt.setInt(5, resourceState);
2266            stmt.setInt(6, resource.getLength());
2267            stmt.setInt(7, projectId);
2268            stmt.setInt(8, internalCountSiblings(dbc, project.getId(), resource.getResourceId()));
2269            stmt.setString(9, resource.getResourceId().toString());
2270            stmt.executeUpdate();
2271
2272            m_sqlManager.closeAll(dbc, null, stmt, null);
2273
2274            // read the parent id
2275
String JavaDoc parentId = internalReadParentId(dbc, project.getId(), resourcePath);
2276
2277            // update the structure
2278
stmt = m_sqlManager.getPreparedStatement(conn, project, "C_RESOURCES_UPDATE_STRUCTURE");
2279            stmt.setString(1, resource.getResourceId().toString());
2280            stmt.setString(2, resourcePath);
2281            stmt.setInt(3, structureState);
2282            stmt.setLong(4, resource.getDateReleased());
2283            stmt.setLong(5, resource.getDateExpired());
2284            stmt.setString(6, parentId);
2285            stmt.setString(7, resource.getStructureId().toString());
2286            stmt.executeUpdate();
2287        } catch (SQLException JavaDoc e) {
2288            throw new CmsDbSqlException(Messages.get().container(
2289                Messages.ERR_GENERIC_SQL_1,
2290                CmsDbSqlException.getErrorQuery(stmt)), e);
2291        } finally {
2292            m_sqlManager.closeAll(dbc, conn, stmt, null);
2293        }
2294    }
2295
2296    /**
2297     * @see org.opencms.db.I_CmsVfsDriver#writeResourceState(org.opencms.db.CmsDbContext, org.opencms.file.CmsProject, org.opencms.file.CmsResource, int)
2298     */

2299    public void writeResourceState(CmsDbContext dbc, CmsProject project, CmsResource resource, int changed)
2300    throws CmsDataAccessException {
2301
2302        PreparedStatement JavaDoc stmt = null;
2303        Connection JavaDoc conn = null;
2304
2305        if (project.getId() == CmsProject.ONLINE_PROJECT_ID) {
2306            return;
2307        }
2308
2309        try {
2310            conn = m_sqlManager.getConnection(dbc, project.getId());
2311
2312            if (changed == CmsDriverManager.UPDATE_RESOURCE) {
2313                stmt = m_sqlManager.getPreparedStatement(conn, project, "C_RESOURCES_UPDATE_RESOURCE_STATELASTMODIFIED");
2314                stmt.setInt(1, resource.getState());
2315                stmt.setLong(2, resource.getDateLastModified());
2316                stmt.setString(3, resource.getUserLastModified().toString());
2317                stmt.setInt(4, project.getId());
2318                stmt.setString(5, resource.getResourceId().toString());
2319                stmt.executeUpdate();
2320                m_sqlManager.closeAll(dbc, null, stmt, null);
2321            }
2322
2323            if (changed == CmsDriverManager.UPDATE_RESOURCE_STATE || changed == CmsDriverManager.UPDATE_ALL) {
2324                stmt = m_sqlManager.getPreparedStatement(conn, project, "C_RESOURCES_UPDATE_RESOURCE_STATE");
2325                stmt.setInt(1, resource.getState());
2326                stmt.setInt(2, project.getId());
2327                stmt.setString(3, resource.getResourceId().toString());
2328                stmt.executeUpdate();
2329                m_sqlManager.closeAll(dbc, null, stmt, null);
2330            }
2331
2332            if (changed == CmsDriverManager.UPDATE_STRUCTURE
2333                || changed == CmsDriverManager.UPDATE_STRUCTURE_STATE
2334                || changed == CmsDriverManager.UPDATE_ALL) {
2335                stmt = m_sqlManager.getPreparedStatement(conn, project, "C_RESOURCES_UPDATE_STRUCTURE_STATE");
2336                stmt.setInt(1, resource.getState());
2337                stmt.setString(2, resource.getStructureId().toString());
2338                stmt.executeUpdate();
2339                m_sqlManager.closeAll(dbc, null, stmt, null);
2340                
2341                stmt = m_sqlManager.getPreparedStatement(conn, project, "C_RESOURCES_UPDATE_RELEASE_EXPIRED");
2342                stmt.setLong(1, resource.getDateReleased());
2343                stmt.setLong(2, resource.getDateExpired());
2344                stmt.setString(3, resource.getStructureId().toString());
2345                stmt.executeUpdate();
2346                m_sqlManager.closeAll(dbc, null, stmt, null);
2347            }
2348
2349        } catch (SQLException JavaDoc e) {
2350            throw new CmsDbSqlException(Messages.get().container(
2351                Messages.ERR_GENERIC_SQL_1,
2352                CmsDbSqlException.getErrorQuery(stmt)), e);
2353        } finally {
2354            m_sqlManager.closeAll(dbc, conn, stmt, null);
2355        }
2356    }
2357
2358    /**
2359     * Escapes the database wildcards within the resource path.<p>
2360     *
2361     * This method is required to ensure chars in the resource path that have a special
2362     * meaning in SQL (for example "_", which is the "any char" operator) are escaped.<p>
2363     *
2364     * It will escape the following chars:
2365     * <ul>
2366     * <li>"_" to "|_"</li>
2367     * </ul>
2368     *
2369     * @param path the resource path
2370     * @return the escaped resource path
2371     */

2372    protected String JavaDoc escapeDbWildcard(String JavaDoc path) {
2373
2374        return CmsStringUtil.substitute(path, "_", "|_");
2375    }
2376
2377    /**
2378     * @see java.lang.Object#finalize()
2379     */

2380    protected void finalize() throws Throwable JavaDoc {
2381
2382        try {
2383            m_sqlManager = null;
2384            m_driverManager = null;
2385        } catch (Throwable JavaDoc t) {
2386            // ignore
2387
}
2388        super.finalize();
2389    }
2390
2391    /**
2392     * Returns the count of properties for a property definition.<p>
2393     *
2394     * @param dbc the current database context
2395     * @param propertyDefinition the propertydefinition to test
2396     * @param projectId the ID of the current project
2397     *
2398     * @return the amount of properties for a propertydefinition
2399     * @throws CmsDataAccessException if something goes wrong
2400     */

2401    protected int internalCountProperties(CmsDbContext dbc, CmsPropertyDefinition propertyDefinition, int projectId)
2402    throws CmsDataAccessException {
2403
2404        ResultSet JavaDoc res = null;
2405        PreparedStatement JavaDoc stmt = null;
2406        Connection JavaDoc conn = null;
2407        int count = 0;
2408
2409        try {
2410            // create statement
2411
conn = m_sqlManager.getConnection(dbc);
2412            stmt = m_sqlManager.getPreparedStatement(conn, projectId, "C_PROPERTIES_READALL_COUNT");
2413            stmt.setString(1, propertyDefinition.getId().toString());
2414            res = stmt.executeQuery();
2415
2416            if (res.next()) {
2417                count = res.getInt(1);
2418            } else {
2419                throw new CmsDbConsistencyException(Messages.get().container(
2420                    Messages.ERR_COUNTING_PROPERTIES_1,
2421                    propertyDefinition.getName()));
2422            }
2423        } catch (SQLException JavaDoc e) {
2424            throw new CmsDbSqlException(Messages.get().container(
2425                Messages.ERR_GENERIC_SQL_1,
2426                CmsDbSqlException.getErrorQuery(stmt)), e);
2427        } finally {
2428            m_sqlManager.closeAll(dbc, conn, stmt, res);
2429        }
2430
2431        return count;
2432    }
2433
2434    /**
2435     * Counts the number of siblings of a resource.<p>
2436     *
2437     * @param dbc the current database context
2438     * @param projectId the current project id
2439     * @param resourceId the resource id to count the number of siblings from
2440     *
2441     * @return number of siblings
2442     * @throws CmsDataAccessException if something goes wrong
2443     */

2444    protected int internalCountSiblings(CmsDbContext dbc, int projectId, CmsUUID resourceId)
2445    throws CmsDataAccessException {
2446
2447        Connection JavaDoc conn = null;
2448        PreparedStatement JavaDoc stmt = null;
2449        ResultSet JavaDoc res = null;
2450        int count = 0;
2451
2452        try {
2453            conn = m_sqlManager.getConnection(dbc, projectId);
2454
2455            stmt = m_sqlManager.getPreparedStatement(conn, projectId, "C_RESOURCES_COUNT_SIBLINGS");
2456            stmt.setString(1, resourceId.toString());
2457            res = stmt.executeQuery();
2458
2459            if (res.next()) {
2460                count = res.getInt(1);
2461            }
2462        } catch (SQLException JavaDoc e) {
2463            throw new CmsDbSqlException(Messages.get().container(
2464                Messages.ERR_GENERIC_SQL_1,
2465                CmsDbSqlException.getErrorQuery(stmt)), e);
2466        } finally {
2467            m_sqlManager.closeAll(dbc, conn, stmt, res);
2468        }
2469
2470        return count;
2471    }
2472
2473    /**
2474     * Returns the parent id of the given resource.<p>
2475     *
2476     * @param dbc the current database context
2477     * @param projectId the current project id
2478     * @param resourcename the resource name to read the parent id for
2479     * @return the parent id of the given resource
2480     * @throws CmsDataAccessException if something goes wrong
2481     */

2482    protected String JavaDoc internalReadParentId(CmsDbContext dbc, int projectId, String JavaDoc resourcename)
2483    throws CmsDataAccessException {
2484
2485        if ("/".equalsIgnoreCase(resourcename)) {
2486            return CmsUUID.getNullUUID().toString();
2487        }
2488
2489        String JavaDoc parent = CmsResource.getParentFolder(resourcename);
2490        parent = removeTrailingSeparator(parent);
2491
2492        ResultSet JavaDoc res = null;
2493        PreparedStatement JavaDoc stmt = null;
2494        Connection JavaDoc conn = null;
2495        String JavaDoc parentId = null;
2496
2497        try {
2498            conn = m_sqlManager.getConnection(dbc, projectId);
2499            stmt = m_sqlManager.getPreparedStatement(conn, projectId, "C_RESOURCES_READ_PARENT_STRUCTURE_ID");
2500            stmt.setString(1, parent);
2501            res = stmt.executeQuery();
2502
2503            if (res.next()) {
2504                parentId = res.getString(1);
2505            } else {
2506                throw new CmsVfsResourceNotFoundException(Messages.get().container(
2507                    Messages.ERR_READ_PARENT_ID_1,
2508                    dbc.removeSiteRoot(resourcename)));
2509            }
2510        } catch (SQLException JavaDoc e) {
2511            throw new CmsDbSqlException(Messages.get().container(
2512                Messages.ERR_GENERIC_SQL_1,
2513                CmsDbSqlException.getErrorQuery(stmt)), e);
2514        } finally {
2515            m_sqlManager.closeAll(dbc, conn, stmt, res);
2516        }
2517
2518        return parentId;
2519    }
2520
2521    /**
2522     * Removes a resource physically in the database.<p>
2523     *
2524     * @param dbc the current database context
2525     * @param currentProject the current project
2526     * @param resource the folder to remove
2527     *
2528     * @throws CmsDataAccessException if something goes wrong
2529     */

2530    protected void internalRemoveFolder(CmsDbContext dbc, CmsProject currentProject, CmsResource resource)
2531    throws CmsDataAccessException {
2532
2533        PreparedStatement JavaDoc stmt = null;
2534        Connection JavaDoc conn = null;
2535
2536        try {
2537            conn = m_sqlManager.getConnection(dbc, currentProject.getId());
2538
2539            // delete the structure record
2540
stmt = m_sqlManager.getPreparedStatement(conn, currentProject, "C_STRUCTURE_DELETE_BY_STRUCTUREID");
2541            stmt.setString(1, resource.getStructureId().toString());
2542            stmt.executeUpdate();
2543
2544            m_sqlManager.closeAll(dbc, null, stmt, null);
2545
2546            // delete the resource record
2547
stmt = m_sqlManager.getPreparedStatement(conn, currentProject, "C_RESOURCES_DELETE_BY_RESOURCEID");
2548            stmt.setString(1, resource.getResourceId().toString());
2549            stmt.executeUpdate();
2550        } catch (SQLException JavaDoc e) {
2551            throw new CmsDbSqlException(Messages.get().container(
2552                Messages.ERR_GENERIC_SQL_1,
2553                CmsDbSqlException.getErrorQuery(stmt)), e);
2554        } finally {
2555            m_sqlManager.closeAll(dbc, conn, stmt, null);
2556        }
2557    }
2558
2559    /**
2560     * Validates that the length setting of a resource is always correct.<p>
2561     *
2562     * Files need to have a resource length of >= 0, while folders require
2563     * a resource length of -1.<p>
2564     *
2565     * @param resource the resource to check the length for
2566     * @throws CmsDataAccessException if the resource length is not correct
2567     */

2568    protected void internalValidateResourceLength(CmsResource resource) throws CmsDataAccessException {
2569
2570        if (resource.isFolder() && (resource.getLength() == -1)) {
2571            return;
2572        }
2573
2574        if (resource.isFile() && (resource.getLength() >= 0)) {
2575            return;
2576        }
2577
2578        throw new CmsDataAccessException(Messages.get().container(
2579            Messages.ERR_INVALID_RESOURCE_LENGTH_2,
2580            new Integer JavaDoc(resource.getLength()),
2581            resource.getRootPath()));
2582    }
2583
2584    private void prepareExpiredTimeRangeCondition(
2585        int projectId,
2586        long startTime,
2587        long endTime,
2588        StringBuffer JavaDoc conditions,
2589        List JavaDoc params) {
2590
2591        if (startTime > 0L) {
2592            // READ_IGNORE_TIME: if NOT set, add condition to match expired date against startTime
2593
conditions.append(BEGIN_INCLUDE_CONDITION);
2594            conditions.append(m_sqlManager.readQuery(projectId, "C_STRUCTURE_SELECT_BY_DATE_EXPIRED_AFTER"));
2595            conditions.append(END_CONDITION);
2596            params.add(String.valueOf(startTime));
2597        }
2598
2599        if (endTime > 0L) {
2600            // READ_IGNORE_TIME: if NOT set, add condition to match expired date against endTime
2601
conditions.append(BEGIN_INCLUDE_CONDITION);
2602            conditions.append(m_sqlManager.readQuery(projectId, "C_STRUCTURE_SELECT_BY_DATE_EXPIRED_BEFORE"));
2603            conditions.append(END_CONDITION);
2604            params.add(String.valueOf(endTime));
2605        }
2606    }
2607
2608    /**
2609     * Appends the appropriate selection criteria related with the parentPath.<p>
2610     *
2611     * @param projectId the id of the project of the resources
2612     * @param parent the parent path or UUID (if mode is C_READMODE_EXCLUDE_TREE)
2613     * @param mode the selection mode
2614     * @param conditions buffer to append the selection criteria
2615     * @param params list to append the selection params
2616     */

2617    private void preparePathCondition(int projectId, String JavaDoc parent, int mode, StringBuffer JavaDoc conditions, List JavaDoc params) {
2618
2619        if (parent == CmsDriverManager.READ_IGNORE_PARENT) {
2620            // parent can be ignored
2621
return;
2622        }
2623
2624        if ((mode & CmsDriverManager.READMODE_EXCLUDE_TREE) > 0) {
2625            // only return immediate children - use UUID optimization
2626
conditions.append(BEGIN_INCLUDE_CONDITION);
2627            conditions.append(m_sqlManager.readQuery(projectId, "C_RESOURCES_SELECT_BY_PARENT_UUID"));
2628            conditions.append(END_CONDITION);
2629            params.add(parent);
2630            return;
2631        }
2632
2633        if ("/".equalsIgnoreCase(parent)) {
2634            // if root folder is parent, no additional condition is needed since all resources match anyway
2635
return;
2636        }
2637
2638        // add condition to read path subtree
2639
conditions.append(BEGIN_INCLUDE_CONDITION);
2640        conditions.append(m_sqlManager.readQuery(projectId, "C_RESOURCES_SELECT_BY_PATH_PREFIX"));
2641        conditions.append(END_CONDITION);
2642        params.add(addTrailingSeparator(escapeDbWildcard(parent)) + "%");
2643    }
2644
2645    /**
2646     * Appends the appropriate selection criteria related with the projectId.<p>
2647     *
2648     * @param projectId the id of the project of the resources
2649     * @param mode the selection mode
2650     * @param conditions buffer to append the selection criteria
2651     * @param params list to append the selection params
2652     */

2653    private void prepareProjectCondition(int projectId, int mode, StringBuffer JavaDoc conditions, List JavaDoc params) {
2654
2655        if ((mode & CmsDriverManager.READMODE_INCLUDE_PROJECT) > 0) {
2656            // C_READMODE_INCLUDE_PROJECT: add condition to match the PROJECT_ID
2657
conditions.append(BEGIN_INCLUDE_CONDITION);
2658            conditions.append(m_sqlManager.readQuery(projectId, "C_RESOURCES_SELECT_BY_PROJECT_LASTMODIFIED"));
2659            conditions.append(END_CONDITION);
2660            params.add(String.valueOf(projectId));
2661        }
2662    }
2663
2664    private void prepareReleasedTimeRangeCondition(
2665        int projectId,
2666        long startTime,
2667        long endTime,
2668        StringBuffer JavaDoc conditions,
2669        List JavaDoc params) {
2670
2671        if (startTime > 0L) {
2672            // READ_IGNORE_TIME: if NOT set, add condition to match released date against startTime
2673
conditions.append(BEGIN_INCLUDE_CONDITION);
2674            conditions.append(m_sqlManager.readQuery(projectId, "C_STRUCTURE_SELECT_BY_DATE_RELEASED_AFTER"));
2675            conditions.append(END_CONDITION);
2676            params.add(String.valueOf(startTime));
2677        }
2678
2679        if (endTime > 0L) {
2680            // READ_IGNORE_TIME: if NOT set, add condition to match released date against endTime
2681
conditions.append(BEGIN_INCLUDE_CONDITION);
2682            conditions.append(m_sqlManager.readQuery(projectId, "C_STRUCTURE_SELECT_BY_DATE_RELEASED_BEFORE"));
2683            conditions.append(END_CONDITION);
2684            params.add(String.valueOf(endTime));
2685        }
2686    }
2687
2688    /**
2689     * Appends the appropriate selection criteria related with the read mode.<p>
2690     *
2691     * @param projectId the id of the project of the resources
2692     * @param mode the selection mode
2693     * @param conditions buffer to append the selection criteria
2694     */

2695    private void prepareResourceCondition(int projectId, int mode, StringBuffer JavaDoc conditions) {
2696
2697        if ((mode & CmsDriverManager.READMODE_ONLY_FOLDERS) > 0) {
2698            // C_READMODE_ONLY_FOLDERS: add condition to match only folders
2699
conditions.append(BEGIN_INCLUDE_CONDITION);
2700            conditions.append(m_sqlManager.readQuery(projectId, "C_RESOURCES_SELECT_ONLY_FOLDERS"));
2701            conditions.append(END_CONDITION);
2702        } else if ((mode & CmsDriverManager.READMODE_ONLY_FILES) > 0) {
2703            // C_READMODE_ONLY_FILES: add condition to match only files
2704
conditions.append(BEGIN_INCLUDE_CONDITION);
2705            conditions.append(m_sqlManager.readQuery(projectId, "C_RESOURCES_SELECT_ONLY_FILES"));
2706            conditions.append(END_CONDITION);
2707        }
2708    }
2709
2710    /**
2711     * Appends the appropriate selection criteria related with the resource state.<p>
2712     *
2713     * @param projectId the id of the project of the resources
2714     * @param state the resource state
2715     * @param mode the selection mode
2716     * @param conditions buffer to append the selection criteria
2717     * @param params list to append the selection params
2718     */

2719    private void prepareStateCondition(int projectId, int state, int mode, StringBuffer JavaDoc conditions, List JavaDoc params) {
2720
2721        if (state != CmsDriverManager.READ_IGNORE_STATE) {
2722            if ((mode & CmsDriverManager.READMODE_EXCLUDE_STATE) > 0) {
2723                // C_READ_MODIFIED_STATES: add condition to match against any state but not given state
2724
conditions.append(BEGIN_EXCLUDE_CONDITION);
2725                conditions.append(m_sqlManager.readQuery(projectId, "C_RESOURCES_SELECT_BY_RESOURCE_STATE"));
2726                conditions.append(END_CONDITION);
2727                params.add(String.valueOf(state));
2728                params.add(String.valueOf(state));
2729            } else {
2730                // otherwise add condition to match against given state if neccessary
2731
conditions.append(BEGIN_INCLUDE_CONDITION);
2732                conditions.append(m_sqlManager.readQuery(projectId, "C_RESOURCES_SELECT_BY_RESOURCE_STATE"));
2733                conditions.append(END_CONDITION);
2734                params.add(String.valueOf(state));
2735                params.add(String.valueOf(state));
2736            }
2737        }
2738    }
2739
2740    /**
2741     * Appends the appropriate selection criteria related with the date of the last modification.<p>
2742     *
2743     * @param projectId the id of the project of the resources
2744     * @param startTime start of the time range
2745     * @param endTime end of the time range
2746     * @param conditions buffer to append the selection criteria
2747     * @param params list to append the selection params
2748     */

2749    private void prepareTimeRangeCondition(
2750        int projectId,
2751        long startTime,
2752        long endTime,
2753        StringBuffer JavaDoc conditions,
2754        List JavaDoc params) {
2755
2756        if (startTime > 0L) {
2757            // READ_IGNORE_TIME: if NOT set, add condition to match lastmodified date against startTime
2758
conditions.append(BEGIN_INCLUDE_CONDITION);
2759            conditions.append(m_sqlManager.readQuery(projectId, "C_RESOURCES_SELECT_BY_DATE_LASTMODIFIED_AFTER"));
2760            conditions.append(END_CONDITION);
2761            params.add(String.valueOf(startTime));
2762        }
2763
2764        if (endTime > 0L) {
2765            // READ_IGNORE_TIME: if NOT set, add condition to match lastmodified date against endTime
2766
conditions.append(BEGIN_INCLUDE_CONDITION);
2767            conditions.append(m_sqlManager.readQuery(projectId, "C_RESOURCES_SELECT_BY_DATE_LASTMODIFIED_BEFORE"));
2768            conditions.append(END_CONDITION);
2769            params.add(String.valueOf(endTime));
2770        }
2771    }
2772
2773    /**
2774     * Appends the appropriate selection criteria related with the resource type.<p>
2775     *
2776     * @param projectId the id of the project of the resources
2777     * @param type the resource type
2778     * @param mode the selection mode
2779     * @param conditions buffer to append the selection criteria
2780     * @param params list to append the selection params
2781     */

2782    private void prepareTypeCondition(int projectId, int type, int mode, StringBuffer JavaDoc conditions, List JavaDoc params) {
2783
2784        if (type != CmsDriverManager.READ_IGNORE_TYPE) {
2785            if ((mode & CmsDriverManager.READMODE_EXCLUDE_TYPE) > 0) {
2786                // C_READ_FILE_TYPES: add condition to match against any type, but not given type
2787
conditions.append(BEGIN_EXCLUDE_CONDITION);
2788                conditions.append(m_sqlManager.readQuery(projectId, "C_RESOURCES_SELECT_BY_RESOURCE_TYPE"));
2789                conditions.append(END_CONDITION);
2790                params.add(String.valueOf(type));
2791            } else {
2792                //otherwise add condition to match against given type if neccessary
2793
conditions.append(BEGIN_INCLUDE_CONDITION);
2794                conditions.append(m_sqlManager.readQuery(projectId, "C_RESOURCES_SELECT_BY_RESOURCE_TYPE"));
2795                conditions.append(END_CONDITION);
2796                params.add(String.valueOf(type));
2797            }
2798        }
2799    }
2800}
Popular Tags