0001: /*
0002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/db/generic/CmsVfsDriver.java,v $
0003: * Date : $Date: 2008-03-03 11:54:29 $
0004: * Version: $Revision: 1.276 $
0005: *
0006: * This library is part of OpenCms -
0007: * the Open Source Content Management System
0008: *
0009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
0010: *
0011: * This library is free software; you can redistribute it and/or
0012: * modify it under the terms of the GNU Lesser General Public
0013: * License as published by the Free Software Foundation; either
0014: * version 2.1 of the License, or (at your option) any later version.
0015: *
0016: * This library is distributed in the hope that it will be useful,
0017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
0018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0019: * Lesser General Public License for more details.
0020: *
0021: * For further information about Alkacon Software GmbH, please see the
0022: * company website: http://www.alkacon.com
0023: *
0024: * For further information about OpenCms, please see the
0025: * project website: http://www.opencms.org
0026: *
0027: * You should have received a copy of the GNU Lesser General Public
0028: * License along with this library; if not, write to the Free Software
0029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0030: */
0031:
0032: package org.opencms.db.generic;
0033:
0034: import org.opencms.configuration.CmsConfigurationManager;
0035: import org.opencms.db.CmsDbConsistencyException;
0036: import org.opencms.db.CmsDbContext;
0037: import org.opencms.db.CmsDbEntryNotFoundException;
0038: import org.opencms.db.CmsDbSqlException;
0039: import org.opencms.db.CmsDriverManager;
0040: import org.opencms.db.CmsResourceState;
0041: import org.opencms.db.CmsVfsOnlineResourceAlreadyExistsException;
0042: import org.opencms.db.I_CmsDriver;
0043: import org.opencms.db.I_CmsVfsDriver;
0044: import org.opencms.file.CmsDataAccessException;
0045: import org.opencms.file.CmsFile;
0046: import org.opencms.file.CmsFolder;
0047: import org.opencms.file.CmsProject;
0048: import org.opencms.file.CmsProperty;
0049: import org.opencms.file.CmsPropertyDefinition;
0050: import org.opencms.file.CmsResource;
0051: import org.opencms.file.CmsResourceFilter;
0052: import org.opencms.file.CmsVfsException;
0053: import org.opencms.file.CmsVfsResourceAlreadyExistsException;
0054: import org.opencms.file.CmsVfsResourceNotFoundException;
0055: import org.opencms.file.history.I_CmsHistoryResource;
0056: import org.opencms.file.types.CmsResourceTypeJsp;
0057: import org.opencms.main.CmsEvent;
0058: import org.opencms.main.CmsException;
0059: import org.opencms.main.CmsLog;
0060: import org.opencms.main.I_CmsEventListener;
0061: import org.opencms.main.OpenCms;
0062: import org.opencms.relations.CmsRelation;
0063: import org.opencms.relations.CmsRelationFilter;
0064: import org.opencms.relations.CmsRelationType;
0065: import org.opencms.security.CmsPermissionSet;
0066: import org.opencms.util.CmsFileUtil;
0067: import org.opencms.util.CmsStringUtil;
0068: import org.opencms.util.CmsUUID;
0069:
0070: import java.io.ByteArrayInputStream;
0071: import java.sql.Connection;
0072: import java.sql.PreparedStatement;
0073: import java.sql.ResultSet;
0074: import java.sql.SQLException;
0075: import java.util.ArrayList;
0076: import java.util.Collections;
0077: import java.util.HashMap;
0078: import java.util.HashSet;
0079: import java.util.Iterator;
0080: import java.util.List;
0081: import java.util.Map;
0082: import java.util.Set;
0083:
0084: import org.apache.commons.logging.Log;
0085:
0086: /**
0087: * Generic (ANSI-SQL) database server implementation of the VFS driver methods.<p>
0088: *
0089: * @author Thomas Weckert
0090: * @author Michael Emmerich
0091: *
0092: * @version $Revision: 1.276 $
0093: *
0094: * @since 6.0.0
0095: */
0096: public class CmsVfsDriver implements I_CmsDriver, I_CmsVfsDriver {
0097:
0098: /** The log object for this class. */
0099: private static final Log LOG = CmsLog
0100: .getLog(org.opencms.db.generic.CmsVfsDriver.class);
0101:
0102: /** The driver manager. */
0103: protected CmsDriverManager m_driverManager;
0104:
0105: /**
0106: * This field is temporarily used to compute the versions during publishing.<p>
0107: *
0108: * @see #publishVersions(CmsDbContext, CmsResource, boolean)
0109: */
0110: protected List m_resOp = new ArrayList();
0111:
0112: /** The sql manager. */
0113: protected org.opencms.db.generic.CmsSqlManager m_sqlManager;
0114:
0115: /**
0116: * Counts the number of siblings of a resource.<p>
0117: *
0118: * @param dbc the current database context
0119: * @param projectId the current project id
0120: * @param resourceId the resource id to count the number of siblings from
0121: *
0122: * @return number of siblings
0123: * @throws CmsDataAccessException if something goes wrong
0124: */
0125: public int countSiblings(CmsDbContext dbc, CmsUUID projectId,
0126: CmsUUID resourceId) throws CmsDataAccessException {
0127:
0128: Connection conn = null;
0129: PreparedStatement stmt = null;
0130: ResultSet res = null;
0131: int count = 0;
0132:
0133: try {
0134: conn = m_sqlManager.getConnection(dbc);
0135:
0136: stmt = m_sqlManager.getPreparedStatement(conn, projectId,
0137: "C_RESOURCES_COUNT_SIBLINGS");
0138: stmt.setString(1, resourceId.toString());
0139: res = stmt.executeQuery();
0140:
0141: if (res.next()) {
0142: count = res.getInt(1);
0143: while (res.next()) {
0144: // do nothing only move through all rows because of mssql odbc driver
0145: }
0146: }
0147: } catch (SQLException e) {
0148: throw new CmsDbSqlException(Messages.get().container(
0149: Messages.ERR_GENERIC_SQL_1,
0150: CmsDbSqlException.getErrorQuery(stmt)), e);
0151: } finally {
0152: m_sqlManager.closeAll(dbc, conn, stmt, res);
0153: }
0154:
0155: return count;
0156: }
0157:
0158: /**
0159: * @see org.opencms.db.I_CmsVfsDriver#createContent(CmsDbContext, CmsUUID, CmsUUID, byte[])
0160: */
0161: public void createContent(CmsDbContext dbc, CmsUUID projectId,
0162: CmsUUID resourceId, byte[] content)
0163: throws CmsDataAccessException {
0164:
0165: Connection conn = null;
0166: PreparedStatement stmt = null;
0167:
0168: try {
0169: conn = m_sqlManager.getConnection(dbc);
0170: // create new offline content
0171: stmt = m_sqlManager.getPreparedStatement(conn,
0172: "C_OFFLINE_CONTENTS_WRITE");
0173: stmt.setString(1, resourceId.toString());
0174: if (content.length < 2000) {
0175: stmt.setBytes(2, content);
0176: } else {
0177: stmt.setBinaryStream(2, new ByteArrayInputStream(
0178: content), content.length);
0179: }
0180: stmt.executeUpdate();
0181: } catch (SQLException e) {
0182: throw new CmsDbSqlException(Messages.get().container(
0183: Messages.ERR_GENERIC_SQL_1,
0184: CmsDbSqlException.getErrorQuery(stmt)), e);
0185: } finally {
0186: m_sqlManager.closeAll(dbc, conn, stmt, null);
0187: }
0188: }
0189:
0190: /**
0191: * @see org.opencms.db.I_CmsVfsDriver#createFile(java.sql.ResultSet, CmsUUID)
0192: */
0193: public CmsFile createFile(ResultSet res, CmsUUID projectId)
0194: throws SQLException {
0195:
0196: CmsUUID structureId = new CmsUUID(res.getString(m_sqlManager
0197: .readQuery("C_RESOURCES_STRUCTURE_ID")));
0198: CmsUUID resourceId = new CmsUUID(res.getString(m_sqlManager
0199: .readQuery("C_RESOURCES_RESOURCE_ID")));
0200: int resourceType = res.getInt(m_sqlManager
0201: .readQuery("C_RESOURCES_RESOURCE_TYPE"));
0202: String resourcePath = res.getString(m_sqlManager
0203: .readQuery("C_RESOURCES_RESOURCE_PATH"));
0204: int resourceFlags = res.getInt(m_sqlManager
0205: .readQuery("C_RESOURCES_RESOURCE_FLAGS"));
0206: int resourceState = res.getInt(m_sqlManager
0207: .readQuery("C_RESOURCES_STATE"));
0208: int structureState = res.getInt(m_sqlManager
0209: .readQuery("C_RESOURCES_STRUCTURE_STATE"));
0210: long dateCreated = res.getLong(m_sqlManager
0211: .readQuery("C_RESOURCES_DATE_CREATED"));
0212: long dateLastModified = res.getLong(m_sqlManager
0213: .readQuery("C_RESOURCES_DATE_LASTMODIFIED"));
0214: long dateReleased = res.getLong(m_sqlManager
0215: .readQuery("C_RESOURCES_DATE_RELEASED"));
0216: long dateExpired = res.getLong(m_sqlManager
0217: .readQuery("C_RESOURCES_DATE_EXPIRED"));
0218: int resourceSize = res.getInt(m_sqlManager
0219: .readQuery("C_RESOURCES_SIZE"));
0220: CmsUUID userCreated = new CmsUUID(res.getString(m_sqlManager
0221: .readQuery("C_RESOURCES_USER_CREATED")));
0222: CmsUUID userLastModified = new CmsUUID(res
0223: .getString(m_sqlManager
0224: .readQuery("C_RESOURCES_USER_LASTMODIFIED")));
0225: byte[] content = m_sqlManager.getBytes(res, m_sqlManager
0226: .readQuery("C_RESOURCES_FILE_CONTENT"));
0227: int siblingCount = res.getInt(m_sqlManager
0228: .readQuery("C_RESOURCES_SIBLING_COUNT"));
0229: long dateContent = res.getLong(m_sqlManager
0230: .readQuery("C_RESOURCES_DATE_CONTENT"));
0231: int resourceVersion = res.getInt(m_sqlManager
0232: .readQuery("C_RESOURCES_VERSION"));
0233: int structureVersion = res.getInt(m_sqlManager
0234: .readQuery("C_RESOURCES_STRUCTURE_VERSION"));
0235:
0236: // calculate the overall state
0237: int newState = (structureState > resourceState) ? structureState
0238: : resourceState;
0239:
0240: // in case of folder type ensure, that the root path has a trailing slash
0241: if (CmsFolder.isFolderType(resourceType)) {
0242: resourcePath = CmsFileUtil
0243: .addTrailingSeparator(resourcePath);
0244: }
0245:
0246: return new CmsFile(structureId, resourceId, resourcePath,
0247: resourceType, resourceFlags, projectId,
0248: CmsResourceState.valueOf(newState), dateCreated,
0249: userCreated, dateLastModified, userLastModified,
0250: dateReleased, dateExpired, siblingCount, resourceSize,
0251: dateContent, resourceVersion + structureVersion,
0252: content);
0253: }
0254:
0255: /**
0256: * @see org.opencms.db.I_CmsVfsDriver#createFile(java.sql.ResultSet, CmsUUID, boolean)
0257: */
0258: public CmsFile createFile(ResultSet res, CmsUUID projectId,
0259: boolean hasFileContentInResultSet) throws SQLException {
0260:
0261: byte[] content = null;
0262:
0263: CmsUUID resProjectId = null;
0264:
0265: CmsUUID structureId = new CmsUUID(res.getString(m_sqlManager
0266: .readQuery("C_RESOURCES_STRUCTURE_ID")));
0267: CmsUUID resourceId = new CmsUUID(res.getString(m_sqlManager
0268: .readQuery("C_RESOURCES_RESOURCE_ID")));
0269: String resourcePath = res.getString(m_sqlManager
0270: .readQuery("C_RESOURCES_RESOURCE_PATH"));
0271: int resourceType = res.getInt(m_sqlManager
0272: .readQuery("C_RESOURCES_RESOURCE_TYPE"));
0273: int resourceFlags = res.getInt(m_sqlManager
0274: .readQuery("C_RESOURCES_RESOURCE_FLAGS"));
0275: int resourceState = res.getInt(m_sqlManager
0276: .readQuery("C_RESOURCES_STATE"));
0277: int structureState = res.getInt(m_sqlManager
0278: .readQuery("C_RESOURCES_STRUCTURE_STATE"));
0279: long dateCreated = res.getLong(m_sqlManager
0280: .readQuery("C_RESOURCES_DATE_CREATED"));
0281: long dateLastModified = res.getLong(m_sqlManager
0282: .readQuery("C_RESOURCES_DATE_LASTMODIFIED"));
0283: long dateReleased = res.getLong(m_sqlManager
0284: .readQuery("C_RESOURCES_DATE_RELEASED"));
0285: long dateExpired = res.getLong(m_sqlManager
0286: .readQuery("C_RESOURCES_DATE_EXPIRED"));
0287: int resourceSize = res.getInt(m_sqlManager
0288: .readQuery("C_RESOURCES_SIZE"));
0289: CmsUUID userCreated = new CmsUUID(res.getString(m_sqlManager
0290: .readQuery("C_RESOURCES_USER_CREATED")));
0291: CmsUUID userLastModified = new CmsUUID(res
0292: .getString(m_sqlManager
0293: .readQuery("C_RESOURCES_USER_LASTMODIFIED")));
0294: CmsUUID lockedInProject = new CmsUUID(res
0295: .getString("LOCKED_IN_PROJECT"));
0296: int siblingCount = res.getInt(m_sqlManager
0297: .readQuery("C_RESOURCES_SIBLING_COUNT"));
0298: long dateContent = res.getLong(m_sqlManager
0299: .readQuery("C_RESOURCES_DATE_CONTENT"));
0300: int resourceVersion = res.getInt(m_sqlManager
0301: .readQuery("C_RESOURCES_VERSION"));
0302: int structureVersion = res.getInt(m_sqlManager
0303: .readQuery("C_RESOURCES_STRUCTURE_VERSION"));
0304:
0305: // in case of folder type ensure, that the root path has a trailing slash
0306: if (CmsFolder.isFolderType(resourceType)) {
0307: resourcePath = CmsFileUtil
0308: .addTrailingSeparator(resourcePath);
0309: }
0310: if (hasFileContentInResultSet) {
0311: content = m_sqlManager.getBytes(res, m_sqlManager
0312: .readQuery("C_RESOURCES_FILE_CONTENT"));
0313: }
0314: resProjectId = lockedInProject;
0315: int newState = (structureState > resourceState) ? structureState
0316: : resourceState;
0317:
0318: return new CmsFile(structureId, resourceId, resourcePath,
0319: resourceType, resourceFlags, resProjectId,
0320: CmsResourceState.valueOf(newState), dateCreated,
0321: userCreated, dateLastModified, userLastModified,
0322: dateReleased, dateExpired, siblingCount, resourceSize,
0323: dateContent, resourceVersion + structureVersion,
0324: content);
0325: }
0326:
0327: /**
0328: * @see org.opencms.db.I_CmsVfsDriver#createFolder(java.sql.ResultSet, CmsUUID, boolean)
0329: */
0330: public CmsFolder createFolder(ResultSet res, CmsUUID projectId,
0331: boolean hasProjectIdInResultSet) throws SQLException {
0332:
0333: CmsUUID structureId = new CmsUUID(res.getString(m_sqlManager
0334: .readQuery("C_RESOURCES_STRUCTURE_ID")));
0335: CmsUUID resourceId = new CmsUUID(res.getString(m_sqlManager
0336: .readQuery("C_RESOURCES_RESOURCE_ID")));
0337: String resourcePath = res.getString(m_sqlManager
0338: .readQuery("C_RESOURCES_RESOURCE_PATH"));
0339: int resourceType = res.getInt(m_sqlManager
0340: .readQuery("C_RESOURCES_RESOURCE_TYPE"));
0341: int resourceFlags = res.getInt(m_sqlManager
0342: .readQuery("C_RESOURCES_RESOURCE_FLAGS"));
0343: int resourceState = res.getInt(m_sqlManager
0344: .readQuery("C_RESOURCES_STATE"));
0345: int structureState = res.getInt(m_sqlManager
0346: .readQuery("C_RESOURCES_STRUCTURE_STATE"));
0347: long dateCreated = res.getLong(m_sqlManager
0348: .readQuery("C_RESOURCES_DATE_CREATED"));
0349: long dateLastModified = res.getLong(m_sqlManager
0350: .readQuery("C_RESOURCES_DATE_LASTMODIFIED"));
0351: long dateReleased = res.getLong(m_sqlManager
0352: .readQuery("C_RESOURCES_DATE_RELEASED"));
0353: long dateExpired = res.getLong(m_sqlManager
0354: .readQuery("C_RESOURCES_DATE_EXPIRED"));
0355: CmsUUID userCreated = new CmsUUID(res.getString(m_sqlManager
0356: .readQuery("C_RESOURCES_USER_CREATED")));
0357: CmsUUID userLastModified = new CmsUUID(res
0358: .getString(m_sqlManager
0359: .readQuery("C_RESOURCES_USER_LASTMODIFIED")));
0360: CmsUUID resProjectId = new CmsUUID(res
0361: .getString("LOCKED_IN_PROJECT"));
0362: int resourceVersion = res.getInt(m_sqlManager
0363: .readQuery("C_RESOURCES_VERSION"));
0364: int structureVersion = res.getInt(m_sqlManager
0365: .readQuery("C_RESOURCES_STRUCTURE_VERSION"));
0366: int resourceSize = res.getInt(m_sqlManager
0367: .readQuery("C_RESOURCES_SIZE"));
0368:
0369: // in case of folder type ensure, that the root path has a trailing slash
0370: if (CmsFolder.isFolderSize(resourceSize)) {
0371: resourcePath = CmsFileUtil
0372: .addTrailingSeparator(resourcePath);
0373: }
0374:
0375: int newState = (structureState > resourceState) ? structureState
0376: : resourceState;
0377:
0378: return new CmsFolder(structureId, resourceId, resourcePath,
0379: resourceType, resourceFlags, resProjectId,
0380: CmsResourceState.valueOf(newState), dateCreated,
0381: userCreated, dateLastModified, userLastModified,
0382: dateReleased, dateExpired, resourceVersion
0383: + structureVersion);
0384: }
0385:
0386: /**
0387: * @see org.opencms.db.I_CmsVfsDriver#createOnlineContent(org.opencms.db.CmsDbContext, org.opencms.util.CmsUUID, byte[], int, boolean, boolean)
0388: */
0389: public void createOnlineContent(CmsDbContext dbc,
0390: CmsUUID resourceId, byte[] contents, int publishTag,
0391: boolean keepOnline, boolean needToUpdateContent)
0392: throws CmsDataAccessException {
0393:
0394: Connection conn = null;
0395: PreparedStatement stmt = null;
0396:
0397: try {
0398: conn = m_sqlManager.getConnection(dbc);
0399:
0400: if (needToUpdateContent) {
0401: // put the online content in the history, only if explicit requested
0402: stmt = m_sqlManager.getPreparedStatement(conn,
0403: "C_ONLINE_CONTENTS_HISTORY");
0404: stmt.setString(1, resourceId.toString());
0405: stmt.executeUpdate();
0406: m_sqlManager.closeAll(dbc, null, stmt, null);
0407:
0408: // create new online content
0409: stmt = m_sqlManager.getPreparedStatement(conn,
0410: "C_ONLINE_CONTENTS_WRITE");
0411:
0412: stmt.setString(1, resourceId.toString());
0413: if (contents.length < 2000) {
0414: stmt.setBytes(2, contents);
0415: } else {
0416: stmt.setBinaryStream(2, new ByteArrayInputStream(
0417: contents), contents.length);
0418: }
0419: stmt.setInt(3, publishTag);
0420: stmt.setInt(4, publishTag);
0421: stmt.setInt(5, keepOnline ? 1 : 0);
0422: stmt.executeUpdate();
0423: m_sqlManager.closeAll(dbc, null, stmt, null);
0424: } else {
0425: // update old content entry
0426: stmt = m_sqlManager.getPreparedStatement(conn,
0427: "C_HISTORY_CONTENTS_UPDATE");
0428: stmt.setInt(1, publishTag);
0429: stmt.setString(2, resourceId.toString());
0430: stmt.executeUpdate();
0431: m_sqlManager.closeAll(dbc, null, stmt, null);
0432:
0433: if (!keepOnline) {
0434: // put the online content in the history
0435: stmt = m_sqlManager.getPreparedStatement(conn,
0436: "C_ONLINE_CONTENTS_HISTORY");
0437: stmt.setString(1, resourceId.toString());
0438: stmt.executeUpdate();
0439: m_sqlManager.closeAll(dbc, null, stmt, null);
0440: }
0441: }
0442: } catch (SQLException e) {
0443: throw new CmsDbSqlException(Messages.get().container(
0444: Messages.ERR_GENERIC_SQL_1,
0445: CmsDbSqlException.getErrorQuery(stmt)), e);
0446: } finally {
0447: m_sqlManager.closeAll(dbc, conn, stmt, null);
0448: }
0449: }
0450:
0451: /**
0452: * @see org.opencms.db.I_CmsVfsDriver#createPropertyDefinition(org.opencms.db.CmsDbContext, org.opencms.util.CmsUUID, java.lang.String, org.opencms.file.CmsPropertyDefinition.CmsPropertyType)
0453: */
0454: public CmsPropertyDefinition createPropertyDefinition(
0455: CmsDbContext dbc, CmsUUID projectId, String name,
0456: CmsPropertyDefinition.CmsPropertyType type)
0457: throws CmsDataAccessException {
0458:
0459: Connection conn = null;
0460: PreparedStatement stmt = null;
0461:
0462: try {
0463: conn = m_sqlManager.getConnection(dbc);
0464: stmt = m_sqlManager.getPreparedStatement(conn, projectId,
0465: "C_PROPERTYDEF_CREATE");
0466: stmt.setString(1, new CmsUUID().toString());
0467: stmt.setString(2, name);
0468: stmt.setInt(3, type.getMode());
0469: stmt.executeUpdate();
0470: } catch (SQLException e) {
0471: throw new CmsDbSqlException(Messages.get().container(
0472: Messages.ERR_GENERIC_SQL_1,
0473: CmsDbSqlException.getErrorQuery(stmt)), e);
0474: } finally {
0475: m_sqlManager.closeAll(dbc, conn, stmt, null);
0476: }
0477:
0478: return readPropertyDefinition(dbc, name, projectId);
0479: }
0480:
0481: /**
0482: * @see org.opencms.db.I_CmsVfsDriver#createRelation(org.opencms.db.CmsDbContext, CmsUUID, org.opencms.relations.CmsRelation)
0483: */
0484: public void createRelation(CmsDbContext dbc, CmsUUID projectId,
0485: CmsRelation relation) throws CmsDataAccessException {
0486:
0487: Connection conn = null;
0488: PreparedStatement stmt = null;
0489:
0490: try {
0491: conn = m_sqlManager.getConnection(dbc);
0492: stmt = m_sqlManager.getPreparedStatement(conn, projectId,
0493: "C_CREATE_RELATION");
0494: stmt.setString(1, relation.getSourceId().toString());
0495: stmt.setString(2, relation.getSourcePath());
0496: stmt.setString(3, relation.getTargetId().toString());
0497: stmt.setString(4, relation.getTargetPath());
0498: stmt.setInt(5, relation.getType().getId());
0499:
0500: if (LOG.isDebugEnabled()) {
0501: LOG.debug(Messages.get().getBundle().key(
0502: Messages.LOG_CREATE_RELATION_2,
0503: String.valueOf(projectId), relation));
0504: }
0505: stmt.executeUpdate();
0506: } catch (SQLException e) {
0507: throw new CmsDbSqlException(Messages.get().container(
0508: Messages.ERR_GENERIC_SQL_1,
0509: CmsDbSqlException.getErrorQuery(stmt)), e);
0510: } finally {
0511: m_sqlManager.closeAll(dbc, conn, stmt, null);
0512: }
0513: }
0514:
0515: /**
0516: * @see org.opencms.db.I_CmsVfsDriver#createResource(org.opencms.db.CmsDbContext, CmsUUID, org.opencms.file.CmsResource, byte[])
0517: */
0518: public CmsResource createResource(CmsDbContext dbc,
0519: CmsUUID projectId, CmsResource resource, byte[] content)
0520: throws CmsDataAccessException {
0521:
0522: CmsUUID newStructureId = null;
0523: Connection conn = null;
0524: PreparedStatement stmt = null;
0525:
0526: // check the resource path
0527: String resourcePath = CmsFileUtil
0528: .removeTrailingSeparator(resource.getRootPath());
0529: if (resourcePath.length() > CmsDriverManager.MAX_VFS_RESOURCE_PATH_LENGTH) {
0530: throw new CmsDataAccessException(
0531: Messages
0532: .get()
0533: .container(
0534: Messages.ERR_RESOURCENAME_TOO_LONG_2,
0535: resourcePath,
0536: new Integer(
0537: CmsDriverManager.MAX_VFS_RESOURCE_PATH_LENGTH)));
0538: }
0539:
0540: // check if the parent folder of the resource exists and if is not deleted
0541: if (!resource.getRootPath().equals("/")) {
0542: String parentFolderName = CmsResource
0543: .getParentFolder(resource.getRootPath());
0544: CmsFolder parentFolder = m_driverManager.getVfsDriver()
0545: .readFolder(dbc, projectId, parentFolderName);
0546: if (parentFolder.getState().isDeleted()) {
0547: throw new CmsDbEntryNotFoundException(Messages.get()
0548: .container(
0549: Messages.ERR_PARENT_FOLDER_DELETED_1,
0550: resource.getRootPath()));
0551: }
0552: }
0553:
0554: // validate the resource length
0555: internalValidateResourceLength(resource);
0556:
0557: // set the resource state and modification dates
0558: CmsResourceState newState;
0559: long dateModified;
0560: long dateCreated;
0561: long dateContent = System.currentTimeMillis();
0562:
0563: if (projectId.equals(CmsProject.ONLINE_PROJECT_ID)) {
0564: newState = CmsResource.STATE_UNCHANGED;
0565: dateCreated = resource.getDateCreated();
0566: dateModified = resource.getDateLastModified();
0567: } else {
0568: newState = CmsResource.STATE_NEW;
0569: if (resource.isTouched()) {
0570: dateCreated = resource.getDateCreated();
0571: dateModified = resource.getDateLastModified();
0572: } else {
0573: dateCreated = System.currentTimeMillis();
0574: dateModified = dateCreated;
0575: }
0576: }
0577:
0578: // check if the resource already exists
0579: newStructureId = resource.getStructureId();
0580:
0581: try {
0582: CmsResource existingResource = m_driverManager
0583: .getVfsDriver().readResource(
0584: dbc,
0585: dbc.getProjectId().isNullUUID() ? projectId
0586: : dbc.getProjectId(), resourcePath,
0587: true);
0588: if (existingResource.getState().isDeleted()) {
0589: // if an existing resource is deleted, it will be finally removed now.
0590: // but we have to reuse its id in order to avoid orphans in the online project
0591: newStructureId = existingResource.getStructureId();
0592: newState = CmsResource.STATE_CHANGED;
0593:
0594: // remove the existing file and it's properties
0595: List modifiedResources = m_driverManager.getVfsDriver()
0596: .readSiblings(dbc, projectId, existingResource,
0597: false);
0598: int propertyDeleteOption = (existingResource
0599: .getSiblingCount() > 1) ? CmsProperty.DELETE_OPTION_DELETE_STRUCTURE_VALUES
0600: : CmsProperty.DELETE_OPTION_DELETE_STRUCTURE_AND_RESOURCE_VALUES;
0601: deletePropertyObjects(dbc, projectId, existingResource,
0602: propertyDeleteOption);
0603: removeFile(dbc, projectId, existingResource);
0604:
0605: OpenCms.fireCmsEvent(new CmsEvent(
0606: I_CmsEventListener.EVENT_RESOURCES_MODIFIED,
0607: Collections.singletonMap("resources",
0608: modifiedResources)));
0609: OpenCms
0610: .fireCmsEvent(new CmsEvent(
0611: I_CmsEventListener.EVENT_RESOURCE_AND_PROPERTIES_MODIFIED,
0612: Collections.singletonMap("resource",
0613: existingResource)));
0614: } else {
0615: // we have a collision: there exists already a resource with the same path/name which cannot be removed
0616: throw new CmsVfsResourceAlreadyExistsException(
0617: Messages
0618: .get()
0619: .container(
0620: Messages.ERR_RESOURCE_WITH_NAME_ALREADY_EXISTS_1,
0621: dbc.removeSiteRoot(resource
0622: .getRootPath())));
0623: }
0624: } catch (CmsVfsResourceNotFoundException e) {
0625: // that's what we want in the best case- anything else should be thrown
0626: }
0627:
0628: try {
0629: // read the parent id
0630: String parentId = internalReadParentId(dbc, projectId,
0631: resourcePath);
0632:
0633: // use consistent version numbers if the file is being restored
0634: int lastVersion = m_driverManager.getHistoryDriver()
0635: .readLastVersion(dbc, newStructureId);
0636: int newStrVersion = 0;
0637: int newResVersion = 0;
0638: if (lastVersion > 0) {
0639: I_CmsHistoryResource histRes = m_driverManager
0640: .getHistoryDriver().readResource(dbc,
0641: newStructureId, lastVersion);
0642: newStrVersion = histRes.getStructureVersion();
0643: newResVersion = histRes.getResourceVersion();
0644: }
0645:
0646: conn = m_sqlManager.getConnection(dbc);
0647:
0648: // write the structure
0649: stmt = m_sqlManager.getPreparedStatement(conn, projectId,
0650: "C_STRUCTURE_WRITE");
0651: stmt.setString(1, newStructureId.toString());
0652: stmt.setString(2, resource.getResourceId().toString());
0653: stmt.setString(3, resourcePath);
0654: stmt.setInt(4, newState.getState());
0655: stmt.setLong(5, resource.getDateReleased());
0656: stmt.setLong(6, resource.getDateExpired());
0657: stmt.setString(7, parentId);
0658: stmt.setInt(8, newStrVersion); // starting version number
0659: stmt.executeUpdate();
0660: m_sqlManager.closeAll(dbc, null, stmt, null);
0661:
0662: if (!validateResourceIdExists(dbc, projectId, resource
0663: .getResourceId())) {
0664: try {
0665: // create the resource record
0666: stmt = m_sqlManager.getPreparedStatement(conn,
0667: projectId, "C_RESOURCES_WRITE");
0668: stmt.setString(1, resource.getResourceId()
0669: .toString());
0670: stmt.setInt(2, resource.getTypeId());
0671: stmt.setInt(3, resource.getFlags());
0672: stmt.setLong(4, dateCreated);
0673: stmt.setString(5, resource.getUserCreated()
0674: .toString());
0675: stmt.setLong(6, dateModified);
0676: stmt.setString(7, resource.getUserLastModified()
0677: .toString());
0678: stmt.setInt(8, newState.getState());
0679: stmt.setInt(9, resource.getLength());
0680: stmt.setLong(10, dateContent);
0681: stmt.setString(11, projectId.toString());
0682: stmt.setInt(12, 1); // sibling count
0683: stmt.setInt(13, newResVersion); // version number
0684: stmt.executeUpdate();
0685: } finally {
0686: m_sqlManager.closeAll(dbc, null, stmt, null);
0687: }
0688:
0689: if (resource.isFile() && (content != null)) {
0690: // create the file content
0691: createContent(dbc, projectId, resource
0692: .getResourceId(), content);
0693: }
0694: } else {
0695: if ((content != null) || !resource.getState().isKeep()) {
0696: CmsUUID projLastMod = projectId;
0697: CmsResourceState state = CmsResource.STATE_CHANGED;
0698: if (projectId.equals(CmsProject.ONLINE_PROJECT_ID)) {
0699: // in case a sibling is being published
0700: projLastMod = resource.getProjectLastModified();
0701: state = CmsResource.STATE_UNCHANGED;
0702: }
0703: // update the resource record only if state has changed or new content is provided
0704: stmt = m_sqlManager.getPreparedStatement(conn,
0705: projectId, "C_RESOURCES_UPDATE_RESOURCES");
0706: stmt.setInt(1, resource.getTypeId());
0707: stmt.setInt(2, resource.getFlags());
0708: stmt.setLong(3, dateModified);
0709: stmt.setString(4, resource.getUserLastModified()
0710: .toString());
0711: stmt.setInt(5, state.getState());
0712: stmt.setInt(6, resource.getLength());
0713: stmt.setLong(7, resource.getDateContent());
0714: stmt.setString(8, projLastMod.toString());
0715: stmt.setInt(9, countSiblings(dbc, projectId,
0716: resource.getResourceId()));
0717: stmt.setString(10, resource.getResourceId()
0718: .toString());
0719: stmt.executeUpdate();
0720:
0721: m_sqlManager.closeAll(dbc, null, stmt, null);
0722: }
0723:
0724: if (resource.isFile()) {
0725: if (content != null) {
0726: // update the file content
0727: writeContent(dbc, resource.getResourceId(),
0728: content);
0729: } else if (resource.getState().isKeep()) {
0730: // special case sibling creation - update the link Count
0731: stmt = m_sqlManager.getPreparedStatement(conn,
0732: projectId,
0733: "C_RESOURCES_UPDATE_SIBLING_COUNT");
0734: stmt.setInt(1, countSiblings(dbc, projectId,
0735: resource.getResourceId()));
0736: stmt.setString(2, resource.getResourceId()
0737: .toString());
0738: stmt.executeUpdate();
0739: m_sqlManager.closeAll(dbc, null, stmt, null);
0740:
0741: // update the resource flags
0742: stmt = m_sqlManager.getPreparedStatement(conn,
0743: projectId, "C_RESOURCES_UPDATE_FLAGS");
0744: stmt.setInt(1, resource.getFlags());
0745: stmt.setString(2, resource.getResourceId()
0746: .toString());
0747: stmt.executeUpdate();
0748: m_sqlManager.closeAll(dbc, null, stmt, null);
0749: }
0750: }
0751: }
0752: } catch (SQLException e) {
0753: throw new CmsDbSqlException(Messages.get().container(
0754: Messages.ERR_GENERIC_SQL_1,
0755: CmsDbSqlException.getErrorQuery(stmt)), e);
0756: } finally {
0757: m_sqlManager.closeAll(dbc, conn, stmt, null);
0758: }
0759: repairBrokenRelations(dbc, projectId,
0760: resource.getStructureId(), resource.getRootPath());
0761: return readResource(dbc, projectId, newStructureId, false);
0762: }
0763:
0764: /**
0765: * @see org.opencms.db.I_CmsVfsDriver#createResource(java.sql.ResultSet, CmsUUID)
0766: */
0767: public CmsResource createResource(ResultSet res, CmsUUID projectId)
0768: throws SQLException {
0769:
0770: CmsUUID structureId = new CmsUUID(res.getString(m_sqlManager
0771: .readQuery("C_RESOURCES_STRUCTURE_ID")));
0772: CmsUUID resourceId = new CmsUUID(res.getString(m_sqlManager
0773: .readQuery("C_RESOURCES_RESOURCE_ID")));
0774: String resourcePath = res.getString(m_sqlManager
0775: .readQuery("C_RESOURCES_RESOURCE_PATH"));
0776: int resourceType = res.getInt(m_sqlManager
0777: .readQuery("C_RESOURCES_RESOURCE_TYPE"));
0778: int resourceFlags = res.getInt(m_sqlManager
0779: .readQuery("C_RESOURCES_RESOURCE_FLAGS"));
0780: CmsUUID resourceProjectLastModified = new CmsUUID(res
0781: .getString(m_sqlManager
0782: .readQuery("C_RESOURCES_PROJECT_LASTMODIFIED")));
0783: int resourceState = res.getInt(m_sqlManager
0784: .readQuery("C_RESOURCES_STATE"));
0785: int structureState = res.getInt(m_sqlManager
0786: .readQuery("C_RESOURCES_STRUCTURE_STATE"));
0787: long dateCreated = res.getLong(m_sqlManager
0788: .readQuery("C_RESOURCES_DATE_CREATED"));
0789: long dateLastModified = res.getLong(m_sqlManager
0790: .readQuery("C_RESOURCES_DATE_LASTMODIFIED"));
0791: long dateReleased = res.getLong(m_sqlManager
0792: .readQuery("C_RESOURCES_DATE_RELEASED"));
0793: long dateExpired = res.getLong(m_sqlManager
0794: .readQuery("C_RESOURCES_DATE_EXPIRED"));
0795: int resourceSize = res.getInt(m_sqlManager
0796: .readQuery("C_RESOURCES_SIZE"));
0797: boolean isFolder = CmsFolder.isFolderSize(resourceSize);
0798: if (isFolder) {
0799: // in case of folder type ensure, that the root path has a trailing slash
0800: resourcePath = CmsFileUtil
0801: .addTrailingSeparator(resourcePath);
0802: }
0803: long dateContent = isFolder ? -1 : res.getLong(m_sqlManager
0804: .readQuery("C_RESOURCES_DATE_CONTENT"));
0805: CmsUUID userCreated = new CmsUUID(res.getString(m_sqlManager
0806: .readQuery("C_RESOURCES_USER_CREATED")));
0807: CmsUUID userLastModified = new CmsUUID(res
0808: .getString(m_sqlManager
0809: .readQuery("C_RESOURCES_USER_LASTMODIFIED")));
0810: int siblingCount = res.getInt(m_sqlManager
0811: .readQuery("C_RESOURCES_SIBLING_COUNT"));
0812: int resourceVersion = res.getInt(m_sqlManager
0813: .readQuery("C_RESOURCES_VERSION"));
0814: int structureVersion = res.getInt(m_sqlManager
0815: .readQuery("C_RESOURCES_STRUCTURE_VERSION"));
0816:
0817: int newState = (structureState > resourceState) ? structureState
0818: : resourceState;
0819: // if there is a change increase the version number
0820: int newVersion = resourceVersion + structureVersion
0821: + (newState > 0 ? 1 : 0);
0822:
0823: CmsResource newResource = new CmsResource(structureId,
0824: resourceId, resourcePath, resourceType, isFolder,
0825: resourceFlags, resourceProjectLastModified,
0826: CmsResourceState.valueOf(newState), dateCreated,
0827: userCreated, dateLastModified, userLastModified,
0828: dateReleased, dateExpired, siblingCount, resourceSize,
0829: dateContent, newVersion);
0830:
0831: return newResource;
0832: }
0833:
0834: /**
0835: * @see org.opencms.db.I_CmsVfsDriver#createSibling(org.opencms.db.CmsDbContext, org.opencms.file.CmsProject, org.opencms.file.CmsResource)
0836: */
0837: public void createSibling(CmsDbContext dbc, CmsProject project,
0838: CmsResource resource) throws CmsDataAccessException {
0839:
0840: if (!project.getUuid().equals(CmsProject.ONLINE_PROJECT_ID)) {
0841: // this method is only intended to be used during publishing
0842: return;
0843: }
0844:
0845: // check if the resource already exists
0846: CmsResource existingSibling = null;
0847: CmsUUID newStructureId = resource.getStructureId();
0848:
0849: Connection conn = null;
0850: PreparedStatement stmt = null;
0851: try {
0852: existingSibling = readResource(dbc, project.getUuid(),
0853: resource.getRootPath(), true);
0854:
0855: if (existingSibling.getState().isDeleted()) {
0856: // if an existing resource is deleted, it will be finally removed now.
0857: // but we have to reuse its id in order to avoid orphans in the online project.
0858: newStructureId = existingSibling.getStructureId();
0859:
0860: // remove the existing file and it's properties
0861: List modifiedResources = readSiblings(dbc, project
0862: .getUuid(), existingSibling, false);
0863: int propertyDeleteOption = (existingSibling
0864: .getSiblingCount() > 1) ? CmsProperty.DELETE_OPTION_DELETE_STRUCTURE_VALUES
0865: : CmsProperty.DELETE_OPTION_DELETE_STRUCTURE_AND_RESOURCE_VALUES;
0866: deletePropertyObjects(dbc, project.getUuid(),
0867: existingSibling, propertyDeleteOption);
0868: removeFile(dbc, project.getUuid(), existingSibling);
0869:
0870: OpenCms.fireCmsEvent(new CmsEvent(
0871: I_CmsEventListener.EVENT_RESOURCES_MODIFIED,
0872: Collections.singletonMap("resources",
0873: modifiedResources)));
0874: OpenCms
0875: .fireCmsEvent(new CmsEvent(
0876: I_CmsEventListener.EVENT_RESOURCE_AND_PROPERTIES_MODIFIED,
0877: Collections.singletonMap("resource",
0878: existingSibling)));
0879: } else {
0880: // we have a collision: there exists already a resource with the same path/name which could not be removed
0881: throw new CmsVfsResourceAlreadyExistsException(
0882: Messages
0883: .get()
0884: .container(
0885: Messages.ERR_RESOURCE_WITH_NAME_ALREADY_EXISTS_1,
0886: dbc.removeSiteRoot(resource
0887: .getRootPath())));
0888: }
0889: } catch (CmsVfsResourceNotFoundException e) {
0890: // that's what we want in the best case- anything else should be thrown
0891: }
0892:
0893: // check if a resource with the specified ID already exists
0894: if (!validateResourceIdExists(dbc, project.getUuid(), resource
0895: .getResourceId())) {
0896: throw new CmsVfsResourceNotFoundException(
0897: Messages
0898: .get()
0899: .container(
0900: Messages.ERR_CREATE_SIBLING_FILE_NOT_FOUND_1,
0901: dbc.removeSiteRoot(resource
0902: .getRootPath())));
0903: }
0904:
0905: // write a new structure referring to the resource
0906: try {
0907: // use consistent version numbers if the file is being restored
0908: int lastVersion = m_driverManager.getHistoryDriver()
0909: .readLastVersion(dbc, newStructureId);
0910: int newStrVersion = 0;
0911: if (lastVersion > 0) {
0912: I_CmsHistoryResource histRes = m_driverManager
0913: .getHistoryDriver().readResource(dbc,
0914: newStructureId, lastVersion);
0915: newStrVersion = histRes.getStructureVersion();
0916: }
0917:
0918: conn = m_sqlManager.getConnection(dbc);
0919:
0920: // read the parent id
0921: String parentId = internalReadParentId(dbc, project
0922: .getUuid(), resource.getRootPath());
0923:
0924: // write the structure
0925: stmt = m_sqlManager.getPreparedStatement(conn, project,
0926: "C_STRUCTURE_WRITE");
0927: stmt.setString(1, newStructureId.toString());
0928: stmt.setString(2, resource.getResourceId().toString());
0929: stmt.setString(3, resource.getRootPath());
0930: stmt.setInt(4, CmsResource.STATE_UNCHANGED.getState());
0931: stmt.setLong(5, resource.getDateReleased());
0932: stmt.setLong(6, resource.getDateExpired());
0933: stmt.setString(7, parentId);
0934: stmt.setInt(8, newStrVersion); // initial structure version number
0935: stmt.executeUpdate();
0936:
0937: m_sqlManager.closeAll(dbc, null, stmt, null);
0938:
0939: // update the link Count
0940: stmt = m_sqlManager.getPreparedStatement(conn, project,
0941: "C_RESOURCES_UPDATE_SIBLING_COUNT");
0942: stmt.setInt(1, countSiblings(dbc, project.getUuid(),
0943: resource.getResourceId()));
0944: stmt.setString(2, resource.getResourceId().toString());
0945: stmt.executeUpdate();
0946:
0947: m_sqlManager.closeAll(dbc, null, stmt, null);
0948:
0949: // update the project last modified and flags
0950: stmt = m_sqlManager.getPreparedStatement(conn, project,
0951: "C_RESOURCES_UPDATE_RESOURCE_PROJECT");
0952: stmt.setInt(1, resource.getFlags());
0953: stmt.setString(2, resource.getProjectLastModified()
0954: .toString());
0955: stmt.setString(3, resource.getResourceId().toString());
0956: stmt.executeUpdate();
0957: } catch (SQLException e) {
0958: throw new CmsDbSqlException(Messages.get().container(
0959: Messages.ERR_GENERIC_SQL_1,
0960: CmsDbSqlException.getErrorQuery(stmt)), e);
0961: } finally {
0962: m_sqlManager.closeAll(dbc, conn, stmt, null);
0963: }
0964: repairBrokenRelations(dbc, project.getUuid(), resource
0965: .getStructureId(), resource.getRootPath());
0966: }
0967:
0968: /**
0969: * @see org.opencms.db.I_CmsVfsDriver#deletePropertyDefinition(org.opencms.db.CmsDbContext, org.opencms.file.CmsPropertyDefinition)
0970: */
0971: public void deletePropertyDefinition(CmsDbContext dbc,
0972: CmsPropertyDefinition metadef)
0973: throws CmsDataAccessException {
0974:
0975: Connection conn = null;
0976: PreparedStatement stmt = null;
0977:
0978: try {
0979: if ((internalCountProperties(dbc, metadef,
0980: CmsProject.ONLINE_PROJECT_ID) != 0)
0981: || (internalCountProperties(dbc, metadef, CmsUUID
0982: .getOpenCmsUUID()) != 0)) { // HACK: to get an offline project
0983:
0984: throw new CmsDataAccessException(Messages.get()
0985: .container(Messages.ERR_DELETE_USED_PROPERTY_1,
0986: metadef.getName()));
0987: }
0988:
0989: conn = m_sqlManager.getConnection(dbc);
0990:
0991: for (int i = 0; i < 2; i++) {
0992: if (i == 0) {
0993: // delete the offline property definition
0994: stmt = m_sqlManager.getPreparedStatement(conn,
0995: CmsUUID.getOpenCmsUUID(),
0996: "C_PROPERTYDEF_DELETE"); // HACK: to get an offline project
0997: } else {
0998: // delete the online property definition
0999: stmt = m_sqlManager.getPreparedStatement(conn,
1000: CmsProject.ONLINE_PROJECT_ID,
1001: "C_PROPERTYDEF_DELETE");
1002: }
1003:
1004: stmt.setString(1, metadef.getId().toString());
1005: stmt.executeUpdate();
1006: m_sqlManager.closeAll(dbc, null, stmt, null);
1007: }
1008: } catch (SQLException e) {
1009: throw new CmsDbSqlException(Messages.get().container(
1010: Messages.ERR_GENERIC_SQL_1,
1011: CmsDbSqlException.getErrorQuery(stmt)), e);
1012: } finally {
1013: m_sqlManager.closeAll(dbc, conn, stmt, null);
1014: }
1015: }
1016:
1017: /**
1018: * @see org.opencms.db.I_CmsVfsDriver#deletePropertyObjects(org.opencms.db.CmsDbContext, CmsUUID, org.opencms.file.CmsResource, int)
1019: */
1020: public void deletePropertyObjects(CmsDbContext dbc,
1021: CmsUUID projectId, CmsResource resource, int deleteOption)
1022: throws CmsDataAccessException {
1023:
1024: Connection conn = null;
1025: PreparedStatement stmt = null;
1026:
1027: try {
1028: conn = m_sqlManager.getConnection(dbc);
1029:
1030: if (deleteOption == CmsProperty.DELETE_OPTION_DELETE_STRUCTURE_AND_RESOURCE_VALUES) {
1031: // delete both the structure and resource property values mapped to the specified resource
1032: stmt = m_sqlManager
1033: .getPreparedStatement(conn, projectId,
1034: "C_PROPERTIES_DELETE_ALL_STRUCTURE_AND_RESOURCE_VALUES");
1035: stmt.setString(1, resource.getResourceId().toString());
1036: stmt.setInt(2, CmsProperty.RESOURCE_RECORD_MAPPING);
1037: stmt.setString(3, resource.getStructureId().toString());
1038: stmt.setInt(4, CmsProperty.STRUCTURE_RECORD_MAPPING);
1039: } else if (deleteOption == CmsProperty.DELETE_OPTION_DELETE_STRUCTURE_VALUES) {
1040: // delete the structure values mapped to the specified resource
1041: stmt = m_sqlManager
1042: .getPreparedStatement(conn, projectId,
1043: "C_PROPERTIES_DELETE_ALL_VALUES_FOR_MAPPING_TYPE");
1044: stmt.setString(1, resource.getStructureId().toString());
1045: stmt.setInt(2, CmsProperty.STRUCTURE_RECORD_MAPPING);
1046: } else if (deleteOption == CmsProperty.DELETE_OPTION_DELETE_RESOURCE_VALUES) {
1047: // delete the resource property values mapped to the specified resource
1048: stmt = m_sqlManager
1049: .getPreparedStatement(conn, projectId,
1050: "C_PROPERTIES_DELETE_ALL_VALUES_FOR_MAPPING_TYPE");
1051: stmt.setString(1, resource.getResourceId().toString());
1052: stmt.setInt(2, CmsProperty.RESOURCE_RECORD_MAPPING);
1053: } else {
1054: throw new CmsDataAccessException(
1055: Messages.get().container(
1056: Messages.ERR_INVALID_DELETE_OPTION_1));
1057: }
1058:
1059: stmt.executeUpdate();
1060: } catch (SQLException e) {
1061: throw new CmsDbSqlException(Messages.get().container(
1062: Messages.ERR_GENERIC_SQL_1,
1063: CmsDbSqlException.getErrorQuery(stmt)), e);
1064: } finally {
1065: m_sqlManager.closeAll(dbc, conn, stmt, null);
1066: }
1067: }
1068:
1069: /**
1070: * @see org.opencms.db.I_CmsVfsDriver#deleteRelations(org.opencms.db.CmsDbContext, CmsUUID, CmsResource, org.opencms.relations.CmsRelationFilter)
1071: */
1072: public void deleteRelations(CmsDbContext dbc, CmsUUID projectId,
1073: CmsResource resource, CmsRelationFilter filter)
1074: throws CmsDataAccessException {
1075:
1076: Connection conn = null;
1077: PreparedStatement stmt = null;
1078:
1079: try {
1080: conn = m_sqlManager.getConnection(dbc);
1081:
1082: if (filter.isSource()) {
1083: List params = new ArrayList(7);
1084:
1085: StringBuffer queryBuf = new StringBuffer(256);
1086: queryBuf.append(m_sqlManager.readQuery(projectId,
1087: "C_DELETE_RELATIONS"));
1088: queryBuf.append(prepareRelationConditions(projectId,
1089: filter, resource, params, true));
1090:
1091: stmt = m_sqlManager.getPreparedStatementForSql(conn,
1092: queryBuf.toString());
1093: for (int i = 0; i < params.size(); i++) {
1094: if (params.get(i) instanceof Integer) {
1095: stmt.setInt(i + 1, ((Integer) params.get(i))
1096: .intValue());
1097: } else {
1098: stmt.setString(i + 1, (String) params.get(i));
1099: }
1100: }
1101: stmt.executeUpdate();
1102: m_sqlManager.closeAll(dbc, null, stmt, null);
1103: }
1104: if (filter.isTarget()) {
1105: List params = new ArrayList(7);
1106:
1107: StringBuffer queryBuf = new StringBuffer(256);
1108: queryBuf.append(m_sqlManager.readQuery(projectId,
1109: "C_DELETE_RELATIONS"));
1110: queryBuf.append(prepareRelationConditions(projectId,
1111: filter, resource, params, false));
1112:
1113: stmt = m_sqlManager.getPreparedStatementForSql(conn,
1114: queryBuf.toString());
1115: for (int i = 0; i < params.size(); i++) {
1116: if (params.get(i) instanceof Integer) {
1117: stmt.setInt(i + 1, ((Integer) params.get(i))
1118: .intValue());
1119: } else {
1120: stmt.setString(i + 1, (String) params.get(i));
1121: }
1122: }
1123: stmt.executeUpdate();
1124: m_sqlManager.closeAll(dbc, null, stmt, null);
1125: }
1126: } catch (SQLException e) {
1127: throw new CmsDbSqlException(Messages.get().container(
1128: Messages.ERR_GENERIC_SQL_1,
1129: CmsDbSqlException.getErrorQuery(stmt)), e);
1130: } finally {
1131: m_sqlManager.closeAll(dbc, conn, stmt, null);
1132: }
1133: // update broken remaining relations
1134: updateBrokenRelations(dbc, projectId, resource.getRootPath());
1135: }
1136:
1137: /**
1138: * @see org.opencms.db.I_CmsVfsDriver#destroy()
1139: */
1140: public void destroy() throws Throwable {
1141:
1142: m_sqlManager = null;
1143: m_driverManager = null;
1144:
1145: if (CmsLog.INIT.isInfoEnabled()) {
1146: CmsLog.INIT.info(Messages.get().getBundle().key(
1147: Messages.INIT_SHUTDOWN_DRIVER_1,
1148: getClass().getName()));
1149: }
1150: }
1151:
1152: /**
1153: * @see org.opencms.db.I_CmsVfsDriver#getSqlManager()
1154: */
1155: public CmsSqlManager getSqlManager() {
1156:
1157: return m_sqlManager;
1158: }
1159:
1160: /**
1161: * @see org.opencms.db.I_CmsDriver#init(org.opencms.db.CmsDbContext, org.opencms.configuration.CmsConfigurationManager, java.util.List, org.opencms.db.CmsDriverManager)
1162: */
1163: public void init(CmsDbContext dbc,
1164: CmsConfigurationManager configurationManager,
1165: List successiveDrivers, CmsDriverManager driverManager) {
1166:
1167: Map configuration = configurationManager.getConfiguration();
1168: String poolUrl = (String) configuration.get("db.vfs.pool");
1169: String classname = (String) configuration
1170: .get("db.vfs.sqlmanager");
1171: m_sqlManager = this .initSqlManager(classname);
1172: m_sqlManager.init(I_CmsVfsDriver.DRIVER_TYPE_ID, poolUrl);
1173:
1174: m_driverManager = driverManager;
1175:
1176: if (CmsLog.INIT.isInfoEnabled()) {
1177: CmsLog.INIT.info(Messages.get().getBundle().key(
1178: Messages.INIT_ASSIGNED_POOL_1, poolUrl));
1179: }
1180:
1181: if ((successiveDrivers != null) && !successiveDrivers.isEmpty()) {
1182: if (LOG.isWarnEnabled()) {
1183: LOG.warn(Messages.get().getBundle().key(
1184: Messages.LOG_SUCCESSIVE_DRIVERS_UNSUPPORTED_1,
1185: getClass().getName()));
1186: }
1187: }
1188: }
1189:
1190: /**
1191: * @see org.opencms.db.I_CmsVfsDriver#initSqlManager(String)
1192: */
1193: public org.opencms.db.generic.CmsSqlManager initSqlManager(
1194: String classname) {
1195:
1196: return CmsSqlManager.getInstance(classname);
1197: }
1198:
1199: /**
1200: * @see org.opencms.db.I_CmsVfsDriver#moveResource(CmsDbContext, CmsUUID, CmsResource, String)
1201: */
1202: public void moveResource(CmsDbContext dbc, CmsUUID projectId,
1203: CmsResource source, String destinationPath)
1204: throws CmsDataAccessException {
1205:
1206: if ((dbc.getRequestContext() != null)
1207: && (dbc.getRequestContext().getAttribute(
1208: REQ_ATTR_CHECK_PERMISSIONS) != null)) {
1209: // only check write permissions
1210: checkWritePermissionsInFolder(dbc, source);
1211: return;
1212: }
1213:
1214: // do not allow to move a resource into an as deleted marked folder
1215: CmsResourceFilter filter = CmsResourceFilter.IGNORE_EXPIRATION;
1216: if (projectId.equals(CmsProject.ONLINE_PROJECT_ID)) {
1217: // does not matter online
1218: filter = CmsResourceFilter.ALL;
1219: }
1220:
1221: // determine destination folder
1222: String destinationFoldername = CmsResource
1223: .getParentFolder(destinationPath);
1224:
1225: // read the destination folder (will also check read permissions)
1226: CmsFolder destinationFolder = m_driverManager.readFolder(dbc,
1227: destinationFoldername, filter);
1228:
1229: if (!projectId.equals(CmsProject.ONLINE_PROJECT_ID)) {
1230: // check online resource
1231: try {
1232: CmsResource onlineResource = m_driverManager
1233: .getVfsDriver().readResource(dbc,
1234: CmsProject.ONLINE_PROJECT_ID,
1235: destinationPath, true);
1236:
1237: if (!onlineResource.getStructureId().equals(
1238: source.getStructureId())) {
1239: // source resource has been moved and it is not the
1240: // same as the resource that is being trying to move back
1241: CmsResource offlineResource = null;
1242: try {
1243: // read new location in offline project
1244: offlineResource = readResource(dbc, dbc
1245: .getRequestContext().currentProject()
1246: .getUuid(), onlineResource
1247: .getStructureId(), true);
1248: } catch (CmsException e) {
1249: // should never happen
1250: if (LOG.isErrorEnabled()) {
1251: LOG.error(e.getMessage(), e);
1252: }
1253: }
1254:
1255: throw new CmsVfsOnlineResourceAlreadyExistsException(
1256: Messages
1257: .get()
1258: .container(
1259: Messages.ERR_OVERWRITE_MOVED_RESOURCE_3,
1260: dbc.removeSiteRoot(source
1261: .getRootPath()),
1262: dbc
1263: .removeSiteRoot(destinationPath),
1264: dbc
1265: .removeSiteRoot(offlineResource == null ? "__ERROR__"
1266: : offlineResource
1267: .getRootPath())));
1268: }
1269: } catch (CmsVfsResourceNotFoundException e) {
1270: // ok, no online resource
1271: }
1272: }
1273:
1274: Connection conn = null;
1275: PreparedStatement stmt = null;
1276: ResultSet res = null;
1277:
1278: try {
1279: conn = m_sqlManager.getConnection(dbc);
1280:
1281: stmt = m_sqlManager.getPreparedStatement(conn, projectId,
1282: "C_RESOURCES_MOVE");
1283: stmt.setString(1, CmsFileUtil
1284: .removeTrailingSeparator(destinationPath)); // must remove trailing slash
1285: stmt.setString(2, destinationFolder.getStructureId()
1286: .toString());
1287: stmt.setString(3, source.getStructureId().toString());
1288: stmt.executeUpdate();
1289: } catch (SQLException e) {
1290: throw new CmsDbSqlException(Messages.get().container(
1291: Messages.ERR_GENERIC_SQL_1,
1292: CmsDbSqlException.getErrorQuery(stmt)), e);
1293: } finally {
1294: m_sqlManager.closeAll(dbc, conn, stmt, res);
1295: }
1296:
1297: moveRelations(dbc, projectId, source.getStructureId(),
1298: destinationPath);
1299: repairBrokenRelations(dbc, projectId, source.getStructureId(),
1300: destinationPath);
1301: }
1302:
1303: /**
1304: * @see org.opencms.db.I_CmsVfsDriver#publishResource(org.opencms.db.CmsDbContext, org.opencms.file.CmsProject, org.opencms.file.CmsResource, org.opencms.file.CmsResource)
1305: */
1306: public void publishResource(CmsDbContext dbc,
1307: CmsProject onlineProject, CmsResource onlineResource,
1308: CmsResource offlineResource) throws CmsDataAccessException {
1309:
1310: Connection conn = null;
1311: PreparedStatement stmt = null;
1312:
1313: // validate the resource length
1314: internalValidateResourceLength(offlineResource);
1315: int resourceSize = offlineResource.getLength();
1316:
1317: String resourcePath = CmsFileUtil
1318: .removeTrailingSeparator(offlineResource.getRootPath());
1319:
1320: try {
1321: conn = m_sqlManager.getConnection(dbc);
1322: boolean resourceExists = validateResourceIdExists(dbc,
1323: onlineProject.getUuid(), offlineResource
1324: .getResourceId());
1325: if (resourceExists) {
1326: // the resource record exists online already
1327: // update the online resource record
1328: stmt = m_sqlManager.getPreparedStatement(conn,
1329: onlineProject, "C_RESOURCES_UPDATE_RESOURCES");
1330: stmt.setInt(1, offlineResource.getTypeId());
1331: stmt.setInt(2, offlineResource.getFlags());
1332: stmt.setLong(3, offlineResource.getDateLastModified());
1333: stmt.setString(4, offlineResource.getUserLastModified()
1334: .toString());
1335: stmt.setInt(5, CmsResource.STATE_UNCHANGED.getState());
1336: stmt.setInt(6, resourceSize);
1337: stmt.setLong(7, offlineResource.getDateContent());
1338: stmt.setString(8, offlineResource
1339: .getProjectLastModified().toString());
1340: stmt.setInt(9, countSiblings(dbc, onlineProject
1341: .getUuid(), onlineResource.getResourceId()));
1342: stmt.setString(10, offlineResource.getResourceId()
1343: .toString());
1344: stmt.executeUpdate();
1345: m_sqlManager.closeAll(dbc, null, stmt, null);
1346: } else {
1347: // the resource record does NOT exist online yet
1348: // create the resource record online
1349: stmt = m_sqlManager.getPreparedStatement(conn,
1350: onlineProject, "C_RESOURCES_WRITE");
1351: stmt.setString(1, offlineResource.getResourceId()
1352: .toString());
1353: stmt.setInt(2, offlineResource.getTypeId());
1354: stmt.setInt(3, offlineResource.getFlags());
1355: stmt.setLong(4, offlineResource.getDateCreated());
1356: stmt.setString(5, offlineResource.getUserCreated()
1357: .toString());
1358: stmt.setLong(6, offlineResource.getDateLastModified());
1359: stmt.setString(7, offlineResource.getUserLastModified()
1360: .toString());
1361: stmt.setInt(8, CmsResource.STATE_UNCHANGED.getState());
1362: stmt.setInt(9, resourceSize);
1363: stmt.setLong(10, offlineResource.getDateContent());
1364: stmt.setString(11, offlineResource
1365: .getProjectLastModified().toString());
1366: stmt.setInt(12, 1); // initial siblings count
1367: stmt.setInt(13, 1); // initial resource version
1368: stmt.executeUpdate();
1369: m_sqlManager.closeAll(dbc, null, stmt, null);
1370: }
1371:
1372: // read the parent id
1373: String parentId = internalReadParentId(dbc, onlineProject
1374: .getUuid(), resourcePath);
1375:
1376: if (validateStructureIdExists(dbc, onlineProject.getUuid(),
1377: offlineResource.getStructureId())) {
1378: // update the online structure record
1379: stmt = m_sqlManager.getPreparedStatement(conn,
1380: onlineProject, "C_RESOURCES_UPDATE_STRUCTURE");
1381: stmt.setString(1, offlineResource.getResourceId()
1382: .toString());
1383: stmt.setString(2, resourcePath);
1384: stmt.setInt(3, CmsResource.STATE_UNCHANGED.getState());
1385: stmt.setLong(4, offlineResource.getDateReleased());
1386: stmt.setLong(5, offlineResource.getDateExpired());
1387: stmt.setString(6, parentId);
1388: stmt.setString(7, offlineResource.getStructureId()
1389: .toString());
1390: stmt.executeUpdate();
1391: m_sqlManager.closeAll(dbc, null, stmt, null);
1392: } else {
1393: // create the structure record online
1394: stmt = m_sqlManager.getPreparedStatement(conn,
1395: onlineProject, "C_STRUCTURE_WRITE");
1396: stmt.setString(1, offlineResource.getStructureId()
1397: .toString());
1398: stmt.setString(2, offlineResource.getResourceId()
1399: .toString());
1400: stmt.setString(3, resourcePath);
1401: stmt.setInt(4, CmsResource.STATE_UNCHANGED.getState());
1402: stmt.setLong(5, offlineResource.getDateReleased());
1403: stmt.setLong(6, offlineResource.getDateExpired());
1404: stmt.setString(7, parentId);
1405: stmt.setInt(8, resourceExists ? 1 : 0); // new resources start with 0, new siblings with 1
1406: stmt.executeUpdate();
1407: m_sqlManager.closeAll(dbc, null, stmt, null);
1408: }
1409: } catch (SQLException e) {
1410: throw new CmsDbSqlException(Messages.get().container(
1411: Messages.ERR_GENERIC_SQL_1,
1412: CmsDbSqlException.getErrorQuery(stmt)), e);
1413: } finally {
1414: m_sqlManager.closeAll(dbc, conn, stmt, null);
1415: }
1416: }
1417:
1418: /**
1419: * @see org.opencms.db.I_CmsVfsDriver#publishVersions(org.opencms.db.CmsDbContext, org.opencms.file.CmsResource, boolean)
1420: */
1421: public void publishVersions(CmsDbContext dbc, CmsResource resource,
1422: boolean firstSibling) throws CmsDataAccessException {
1423:
1424: // if resource is null just flush the internal cache
1425: if (resource == null) {
1426: m_resOp.clear();
1427: return;
1428: }
1429:
1430: if (!dbc.getProjectId().isNullUUID()
1431: || dbc.currentProject().isOnlineProject()) {
1432: // this method is supposed to be used only in the offline project
1433: return;
1434: }
1435:
1436: if (firstSibling) {
1437: // reset the resource operation flag
1438: m_resOp.remove(resource.getResourceId());
1439: }
1440:
1441: boolean resOp = false; // assume structure operation
1442:
1443: CmsResourceState resState = internalReadResourceState(dbc, dbc
1444: .currentProject().getUuid(), resource);
1445: CmsResourceState strState = internalReadStructureState(dbc, dbc
1446: .currentProject().getUuid(), resource);
1447:
1448: if (!resState.isUnchanged()) {
1449: if (strState.isDeleted()) {
1450: resOp = (resState.isDeleted()
1451: || (resource.getSiblingCount() == 1) || (countSiblings(
1452: dbc, dbc.currentProject().getUuid(), resource
1453: .getResourceId()) == 1));
1454: } else {
1455: resOp = true;
1456: }
1457: }
1458:
1459: if (!firstSibling) {
1460: if (resOp) {
1461: return;
1462: }
1463: if (m_resOp.contains(resource.getResourceId())) {
1464: return;
1465: }
1466: }
1467:
1468: // read the offline version numbers
1469: Map versions = readVersions(dbc,
1470: dbc.currentProject().getUuid(), resource
1471: .getResourceId(), resource.getStructureId());
1472: int strVersion = ((Integer) versions.get("structure"))
1473: .intValue();
1474: int resVersion = ((Integer) versions.get("resource"))
1475: .intValue();
1476:
1477: if (resOp) {
1478: if (resource.getSiblingCount() > 1) {
1479: m_resOp.add(resource.getResourceId());
1480: }
1481: resVersion++;
1482: }
1483: if (!resOp) {
1484: strVersion++;
1485: }
1486:
1487: Connection conn = null;
1488: PreparedStatement stmt = null;
1489: ResultSet res = null;
1490:
1491: try {
1492: conn = m_sqlManager.getConnection(dbc);
1493:
1494: if (resOp) {
1495: // update the resource version
1496: stmt = m_sqlManager.getPreparedStatement(conn,
1497: CmsProject.ONLINE_PROJECT_ID,
1498: "C_RESOURCES_UPDATE_RESOURCE_VERSION");
1499: stmt.setInt(1, resVersion);
1500: stmt.setString(2, resource.getResourceId().toString());
1501: stmt.executeUpdate();
1502: m_sqlManager.closeAll(dbc, null, stmt, null);
1503: }
1504: if (!resOp || strState.isNew()) {
1505: // update the structure version
1506: stmt = m_sqlManager.getPreparedStatement(conn,
1507: CmsProject.ONLINE_PROJECT_ID,
1508: "C_RESOURCES_UPDATE_STRUCTURE_VERSION");
1509: stmt.setInt(1, strVersion);
1510: stmt.setString(2, resource.getStructureId().toString());
1511: stmt.executeUpdate();
1512: m_sqlManager.closeAll(dbc, null, stmt, null);
1513: }
1514: } catch (SQLException e) {
1515: throw new CmsDbSqlException(Messages.get().container(
1516: Messages.ERR_GENERIC_SQL_1,
1517: CmsDbSqlException.getErrorQuery(stmt)), e);
1518: } finally {
1519: m_sqlManager.closeAll(dbc, conn, stmt, res);
1520: }
1521: }
1522:
1523: /**
1524: * @see org.opencms.db.I_CmsVfsDriver#readChildResources(org.opencms.db.CmsDbContext, org.opencms.file.CmsProject, org.opencms.file.CmsResource, boolean, boolean)
1525: */
1526: public List readChildResources(CmsDbContext dbc,
1527: CmsProject currentProject, CmsResource resource,
1528: boolean getFolders, boolean getFiles)
1529: throws CmsDataAccessException {
1530:
1531: List result = new ArrayList();
1532: CmsUUID projectId = currentProject.getUuid();
1533:
1534: String resourceTypeClause;
1535: if (getFolders && getFiles) {
1536: resourceTypeClause = null;
1537: } else if (getFolders) {
1538: resourceTypeClause = m_sqlManager.readQuery(projectId,
1539: "C_RESOURCES_GET_SUBRESOURCES_GET_FOLDERS");
1540: } else {
1541: resourceTypeClause = m_sqlManager.readQuery(projectId,
1542: "C_RESOURCES_GET_SUBRESOURCES_GET_FILES");
1543: }
1544: StringBuffer query = new StringBuffer();
1545: query.append(m_sqlManager.readQuery(projectId,
1546: "C_RESOURCES_GET_SUBRESOURCES"));
1547: if (resourceTypeClause != null) {
1548: query.append(' ');
1549: query.append(resourceTypeClause);
1550: }
1551:
1552: String sizeColumn = m_sqlManager.readQuery("C_RESOURCES_SIZE");
1553:
1554: Connection conn = null;
1555: PreparedStatement stmt = null;
1556: ResultSet res = null;
1557: try {
1558: conn = m_sqlManager.getConnection(dbc);
1559: stmt = m_sqlManager.getPreparedStatementForSql(conn, query
1560: .toString());
1561: stmt.setString(1, resource.getStructureId().toString());
1562: res = stmt.executeQuery();
1563:
1564: while (res.next()) {
1565: long size = res.getInt(sizeColumn);
1566: if (CmsFolder.isFolderSize(size)) {
1567: result.add(createFolder(res, projectId, false));
1568: } else {
1569: result.add(createFile(res, projectId, false));
1570: }
1571: }
1572: } catch (SQLException e) {
1573: throw new CmsDbSqlException(Messages.get().container(
1574: Messages.ERR_GENERIC_SQL_1,
1575: CmsDbSqlException.getErrorQuery(stmt)), e);
1576: } finally {
1577: m_sqlManager.closeAll(dbc, conn, stmt, res);
1578: }
1579:
1580: // sort result in memory, this is to avoid DB dependencies in the result order
1581: Collections
1582: .sort(
1583: result,
1584: CmsResource.COMPARE_ROOT_PATH_IGNORE_CASE_FOLDERS_FIRST);
1585: return result;
1586: }
1587:
1588: /**
1589: * @see org.opencms.db.I_CmsVfsDriver#readContent(org.opencms.db.CmsDbContext, CmsUUID, org.opencms.util.CmsUUID)
1590: */
1591: public byte[] readContent(CmsDbContext dbc, CmsUUID projectId,
1592: CmsUUID resourceId) throws CmsDataAccessException {
1593:
1594: PreparedStatement stmt = null;
1595: ResultSet res = null;
1596: Connection conn = null;
1597: byte[] byteRes = null;
1598:
1599: try {
1600: conn = m_sqlManager.getConnection(dbc);
1601: if (projectId.equals(CmsProject.ONLINE_PROJECT_ID)) {
1602: stmt = m_sqlManager.getPreparedStatement(conn,
1603: projectId, "C_ONLINE_FILES_CONTENT");
1604: } else {
1605: stmt = m_sqlManager.getPreparedStatement(conn,
1606: projectId, "C_OFFLINE_FILES_CONTENT");
1607: }
1608: stmt.setString(1, resourceId.toString());
1609: res = stmt.executeQuery();
1610:
1611: if (res.next()) {
1612: //query to read Array of bytes for the attribute FILE_CONTENT
1613: byteRes = m_sqlManager.getBytes(res, m_sqlManager
1614: .readQuery("C_RESOURCES_FILE_CONTENT"));
1615: while (res.next()) {
1616: // do nothing only move through all rows because of mssql odbc driver
1617: }
1618: } else {
1619: throw new CmsVfsResourceNotFoundException(
1620: Messages
1621: .get()
1622: .container(
1623: Messages.ERR_READ_CONTENT_WITH_RESOURCE_ID_2,
1624: resourceId,
1625: Boolean
1626: .valueOf(projectId
1627: .equals(CmsProject.ONLINE_PROJECT_ID))));
1628: }
1629: } catch (SQLException e) {
1630: throw new CmsDbSqlException(Messages.get().container(
1631: Messages.ERR_GENERIC_SQL_1,
1632: CmsDbSqlException.getErrorQuery(stmt)), e);
1633: } finally {
1634: m_sqlManager.closeAll(dbc, conn, stmt, res);
1635: }
1636: return byteRes;
1637: }
1638:
1639: /**
1640: * @see org.opencms.db.I_CmsVfsDriver#readFolder(org.opencms.db.CmsDbContext, CmsUUID, org.opencms.util.CmsUUID)
1641: */
1642: public CmsFolder readFolder(CmsDbContext dbc, CmsUUID projectId,
1643: CmsUUID folderId) throws CmsDataAccessException {
1644:
1645: CmsFolder folder = null;
1646: ResultSet res = null;
1647: PreparedStatement stmt = null;
1648: Connection conn = null;
1649:
1650: try {
1651: conn = m_sqlManager.getConnection(dbc);
1652: stmt = m_sqlManager.getPreparedStatement(conn, projectId,
1653: "C_RESOURCES_READBYID");
1654: stmt.setString(1, folderId.toString());
1655: res = stmt.executeQuery();
1656:
1657: if (res.next()) {
1658: folder = createFolder(res, projectId, true);
1659: while (res.next()) {
1660: // do nothing only move through all rows because of mssql odbc driver
1661: }
1662: } else {
1663: throw new CmsVfsResourceNotFoundException(Messages
1664: .get().container(
1665: Messages.ERR_READ_FOLDER_WITH_ID_1,
1666: folderId));
1667: }
1668: } catch (SQLException e) {
1669: throw new CmsDbSqlException(Messages.get().container(
1670: Messages.ERR_GENERIC_SQL_1,
1671: CmsDbSqlException.getErrorQuery(stmt)), e);
1672: } finally {
1673: m_sqlManager.closeAll(dbc, conn, stmt, res);
1674: }
1675:
1676: return folder;
1677: }
1678:
1679: /**
1680: * @see org.opencms.db.I_CmsVfsDriver#readFolder(org.opencms.db.CmsDbContext, CmsUUID, java.lang.String)
1681: */
1682: public CmsFolder readFolder(CmsDbContext dbc, CmsUUID projectId,
1683: String folderPath) throws CmsDataAccessException {
1684:
1685: CmsFolder folder = null;
1686: ResultSet res = null;
1687: PreparedStatement stmt = null;
1688: Connection conn = null;
1689:
1690: folderPath = CmsFileUtil.removeTrailingSeparator(folderPath);
1691: try {
1692: conn = m_sqlManager.getConnection(dbc);
1693: stmt = m_sqlManager.getPreparedStatement(conn, projectId,
1694: "C_RESOURCES_READ");
1695:
1696: stmt.setString(1, folderPath);
1697: res = stmt.executeQuery();
1698:
1699: if (res.next()) {
1700: folder = createFolder(res, projectId, true);
1701: while (res.next()) {
1702: // do nothing only move through all rows because of mssql odbc driver
1703: }
1704: } else {
1705: throw new CmsVfsResourceNotFoundException(Messages
1706: .get().container(Messages.ERR_READ_FOLDER_1,
1707: dbc.removeSiteRoot(folderPath)));
1708: }
1709: } catch (SQLException e) {
1710: throw new CmsDbSqlException(Messages.get().container(
1711: Messages.ERR_GENERIC_SQL_1,
1712: CmsDbSqlException.getErrorQuery(stmt)), e);
1713: } finally {
1714: m_sqlManager.closeAll(dbc, conn, stmt, res);
1715: }
1716:
1717: return folder;
1718:
1719: }
1720:
1721: /**
1722: * @see org.opencms.db.I_CmsVfsDriver#readParentFolder(org.opencms.db.CmsDbContext, CmsUUID, org.opencms.util.CmsUUID)
1723: */
1724: public CmsFolder readParentFolder(CmsDbContext dbc,
1725: CmsUUID projectId, CmsUUID structureId)
1726: throws CmsDataAccessException {
1727:
1728: CmsFolder parent = null;
1729: ResultSet res = null;
1730: PreparedStatement stmt = null;
1731: Connection conn = null;
1732: try {
1733: conn = m_sqlManager.getConnection(dbc);
1734: stmt = m_sqlManager.getPreparedStatement(conn, projectId,
1735: "C_RESOURCES_READ_PARENT_BY_ID");
1736: stmt.setString(1, structureId.toString());
1737: res = stmt.executeQuery();
1738:
1739: if (res.next()) {
1740: parent = new CmsFolder(createResource(res, projectId));
1741: while (res.next()) {
1742: // do nothing only move through all rows because of mssql odbc driver
1743: }
1744: }
1745: } catch (SQLException e) {
1746: throw new CmsDbSqlException(Messages.get().container(
1747: Messages.ERR_GENERIC_SQL_1,
1748: CmsDbSqlException.getErrorQuery(stmt)), e);
1749: } finally {
1750: m_sqlManager.closeAll(dbc, conn, stmt, res);
1751: }
1752: return parent;
1753: }
1754:
1755: /**
1756: * @see org.opencms.db.I_CmsVfsDriver#readPropertyDefinition(org.opencms.db.CmsDbContext, java.lang.String, CmsUUID)
1757: */
1758: public CmsPropertyDefinition readPropertyDefinition(
1759: CmsDbContext dbc, String name, CmsUUID projectId)
1760: throws CmsDataAccessException {
1761:
1762: CmsPropertyDefinition propDef = null;
1763: ResultSet res = null;
1764: PreparedStatement stmt = null;
1765: Connection conn = null;
1766:
1767: try {
1768: conn = m_sqlManager.getConnection(dbc);
1769: stmt = m_sqlManager.getPreparedStatement(conn, projectId,
1770: "C_PROPERTYDEF_READ");
1771: stmt.setString(1, name);
1772: res = stmt.executeQuery();
1773:
1774: // if result set exists - return it
1775: if (res.next()) {
1776: propDef = new CmsPropertyDefinition(
1777: new CmsUUID(res.getString(m_sqlManager
1778: .readQuery("C_PROPERTYDEF_ID"))),
1779: res.getString(m_sqlManager
1780: .readQuery("C_PROPERTYDEF_NAME")),
1781: CmsPropertyDefinition.CmsPropertyType
1782: .valueOf(res
1783: .getInt(m_sqlManager
1784: .readQuery("C_PROPERTYDEF_TYPE"))));
1785: while (res.next()) {
1786: // do nothing only move through all rows because of mssql odbc driver
1787: }
1788: } else {
1789: throw new CmsDbEntryNotFoundException(
1790: Messages
1791: .get()
1792: .container(
1793: Messages.ERR_NO_PROPERTYDEF_WITH_NAME_1,
1794: name));
1795: }
1796: } catch (SQLException e) {
1797: throw new CmsDbSqlException(Messages.get().container(
1798: Messages.ERR_GENERIC_SQL_1,
1799: CmsDbSqlException.getErrorQuery(stmt)), e);
1800: } finally {
1801: m_sqlManager.closeAll(dbc, conn, stmt, res);
1802: }
1803:
1804: return propDef;
1805: }
1806:
1807: /**
1808: * @see org.opencms.db.I_CmsVfsDriver#readPropertyDefinitions(org.opencms.db.CmsDbContext, CmsUUID)
1809: */
1810: public List readPropertyDefinitions(CmsDbContext dbc,
1811: CmsUUID projectId) throws CmsDataAccessException {
1812:
1813: ArrayList propertyDefinitions = new ArrayList();
1814: ResultSet res = null;
1815: PreparedStatement stmt = null;
1816: Connection conn = null;
1817:
1818: try {
1819: conn = m_sqlManager.getConnection(dbc);
1820: stmt = m_sqlManager.getPreparedStatement(conn, projectId,
1821: "C_PROPERTYDEF_READALL");
1822:
1823: res = stmt.executeQuery();
1824: while (res.next()) {
1825: propertyDefinitions
1826: .add(new CmsPropertyDefinition(
1827: new CmsUUID(res.getString(m_sqlManager
1828: .readQuery("C_PROPERTYDEF_ID"))),
1829: res
1830: .getString(m_sqlManager
1831: .readQuery("C_PROPERTYDEF_NAME")),
1832: CmsPropertyDefinition.CmsPropertyType
1833: .valueOf(res
1834: .getInt(m_sqlManager
1835: .readQuery("C_PROPERTYDEF_TYPE")))));
1836: }
1837: } catch (SQLException e) {
1838: throw new CmsDbSqlException(Messages.get().container(
1839: Messages.ERR_GENERIC_SQL_1,
1840: CmsDbSqlException.getErrorQuery(stmt)), e);
1841: } finally {
1842: m_sqlManager.closeAll(dbc, conn, stmt, res);
1843: }
1844: return propertyDefinitions;
1845: }
1846:
1847: /**
1848: * @see org.opencms.db.I_CmsVfsDriver#readPropertyObject(org.opencms.db.CmsDbContext, java.lang.String, org.opencms.file.CmsProject, org.opencms.file.CmsResource)
1849: */
1850: public CmsProperty readPropertyObject(CmsDbContext dbc, String key,
1851: CmsProject project, CmsResource resource)
1852: throws CmsDataAccessException {
1853:
1854: ResultSet res = null;
1855: PreparedStatement stmt = null;
1856: Connection conn = null;
1857: String propertyValue = null;
1858: int mappingType = -1;
1859: CmsProperty property = null;
1860: int resultSize = 0;
1861:
1862: try {
1863: conn = m_sqlManager.getConnection(dbc);
1864: stmt = m_sqlManager.getPreparedStatement(conn, project
1865: .getUuid(), "C_PROPERTIES_READ");
1866:
1867: stmt.setString(1, key);
1868: stmt.setString(2, resource.getStructureId().toString());
1869: stmt.setString(3, resource.getResourceId().toString());
1870: res = stmt.executeQuery();
1871:
1872: while (res.next()) {
1873: if (resultSize >= 2) {
1874: throw new CmsDbConsistencyException(Messages.get()
1875: .container(
1876: Messages.ERR_TOO_MANY_PROPERTIES_3,
1877: key, resource.getRootPath(),
1878: new Integer(resultSize)));
1879: }
1880:
1881: if (property == null) {
1882: property = new CmsProperty();
1883: property.setName(key);
1884: }
1885:
1886: propertyValue = res.getString(1);
1887: mappingType = res.getInt(2);
1888:
1889: if (mappingType == CmsProperty.STRUCTURE_RECORD_MAPPING) {
1890: property.setStructureValue(propertyValue);
1891: } else if (mappingType == CmsProperty.RESOURCE_RECORD_MAPPING) {
1892: property.setResourceValue(propertyValue);
1893: } else {
1894: throw new CmsDbConsistencyException(
1895: Messages
1896: .get()
1897: .container(
1898: Messages.ERR_UNKNOWN_PROPERTY_VALUE_MAPPING_3,
1899: resource.getRootPath(),
1900: new Integer(mappingType),
1901: key));
1902: }
1903:
1904: resultSize++;
1905: }
1906: } catch (SQLException e) {
1907: throw new CmsDbSqlException(Messages.get().container(
1908: Messages.ERR_GENERIC_SQL_1,
1909: CmsDbSqlException.getErrorQuery(stmt)), e);
1910: } finally {
1911: m_sqlManager.closeAll(dbc, conn, stmt, res);
1912: }
1913:
1914: return (property != null) ? property : CmsProperty
1915: .getNullProperty();
1916: }
1917:
1918: /**
1919: * @see org.opencms.db.I_CmsVfsDriver#readPropertyObjects(org.opencms.db.CmsDbContext, org.opencms.file.CmsProject, org.opencms.file.CmsResource)
1920: */
1921: public List readPropertyObjects(CmsDbContext dbc,
1922: CmsProject project, CmsResource resource)
1923: throws CmsDataAccessException {
1924:
1925: ResultSet res = null;
1926: PreparedStatement stmt = null;
1927: Connection conn = null;
1928: int mappingType = -1;
1929: Map propertyMap = new HashMap();
1930:
1931: String propertyKey;
1932: String propertyValue;
1933: CmsProperty property;
1934:
1935: try {
1936: conn = m_sqlManager.getConnection(dbc);
1937: stmt = m_sqlManager.getPreparedStatement(conn, project
1938: .getUuid(), "C_PROPERTIES_READALL");
1939: stmt.setString(1, resource.getStructureId().toString());
1940: stmt.setString(2, resource.getResourceId().toString());
1941: res = stmt.executeQuery();
1942:
1943: while (res.next()) {
1944: propertyKey = null;
1945: propertyValue = null;
1946: mappingType = -1;
1947:
1948: propertyKey = res.getString(1);
1949: propertyValue = res.getString(2);
1950: mappingType = res.getInt(3);
1951:
1952: property = (CmsProperty) propertyMap.get(propertyKey);
1953: if (property == null) {
1954: // there doesn't exist a property object for this key yet
1955: property = new CmsProperty();
1956: property.setName(propertyKey);
1957: propertyMap.put(propertyKey, property);
1958: }
1959:
1960: if (mappingType == CmsProperty.STRUCTURE_RECORD_MAPPING) {
1961: // this property value is mapped to a structure record
1962: property.setStructureValue(propertyValue);
1963: } else if (mappingType == CmsProperty.RESOURCE_RECORD_MAPPING) {
1964: // this property value is mapped to a resource record
1965: property.setResourceValue(propertyValue);
1966: } else {
1967: throw new CmsDbConsistencyException(
1968: Messages
1969: .get()
1970: .container(
1971: Messages.ERR_UNKNOWN_PROPERTY_VALUE_MAPPING_3,
1972: resource.getRootPath(),
1973: new Integer(mappingType),
1974: propertyKey));
1975: }
1976: }
1977: } catch (SQLException e) {
1978: throw new CmsDbSqlException(Messages.get().container(
1979: Messages.ERR_GENERIC_SQL_1,
1980: CmsDbSqlException.getErrorQuery(stmt)), e);
1981: } finally {
1982: m_sqlManager.closeAll(dbc, conn, stmt, res);
1983: }
1984:
1985: return new ArrayList(propertyMap.values());
1986: }
1987:
1988: /**
1989: * @see org.opencms.db.I_CmsVfsDriver#readRelations(org.opencms.db.CmsDbContext, CmsUUID, CmsResource, org.opencms.relations.CmsRelationFilter)
1990: */
1991: public List readRelations(CmsDbContext dbc, CmsUUID projectId,
1992: CmsResource resource, CmsRelationFilter filter)
1993: throws CmsDataAccessException {
1994:
1995: Set relations = new HashSet();
1996:
1997: Connection conn = null;
1998: PreparedStatement stmt = null;
1999: ResultSet res = null;
2000:
2001: try {
2002: conn = m_sqlManager.getConnection(dbc);
2003: if (filter.isSource()) {
2004: List params = new ArrayList(7);
2005:
2006: StringBuffer queryBuf = new StringBuffer(256);
2007: queryBuf.append(m_sqlManager.readQuery(projectId,
2008: "C_READ_RELATIONS"));
2009: queryBuf.append(prepareRelationConditions(projectId,
2010: filter, resource, params, true));
2011: if (LOG.isDebugEnabled()) {
2012: LOG.debug(queryBuf.toString());
2013: }
2014:
2015: stmt = m_sqlManager.getPreparedStatementForSql(conn,
2016: queryBuf.toString());
2017: for (int i = 0; i < params.size(); i++) {
2018: if (params.get(i) instanceof Integer) {
2019: stmt.setInt(i + 1, ((Integer) params.get(i))
2020: .intValue());
2021: } else {
2022: stmt.setString(i + 1, (String) params.get(i));
2023: }
2024: }
2025: res = stmt.executeQuery();
2026: while (res.next()) {
2027: relations.add(internalReadRelation(res));
2028: }
2029: m_sqlManager.closeAll(dbc, null, stmt, res);
2030: }
2031:
2032: if (filter.isTarget()) {
2033: List params = new ArrayList(7);
2034:
2035: StringBuffer queryBuf = new StringBuffer(256);
2036: queryBuf.append(m_sqlManager.readQuery(projectId,
2037: "C_READ_RELATIONS"));
2038: queryBuf.append(prepareRelationConditions(projectId,
2039: filter, resource, params, false));
2040: if (LOG.isDebugEnabled()) {
2041: LOG.debug(queryBuf.toString());
2042: }
2043:
2044: stmt = m_sqlManager.getPreparedStatementForSql(conn,
2045: queryBuf.toString());
2046: for (int i = 0; i < params.size(); i++) {
2047: if (params.get(i) instanceof Integer) {
2048: stmt.setInt(i + 1, ((Integer) params.get(i))
2049: .intValue());
2050: } else {
2051: stmt.setString(i + 1, (String) params.get(i));
2052: }
2053: }
2054: res = stmt.executeQuery();
2055: while (res.next()) {
2056: relations.add(internalReadRelation(res));
2057: }
2058: m_sqlManager.closeAll(dbc, null, stmt, res);
2059: }
2060: } catch (SQLException e) {
2061: throw new CmsDbSqlException(Messages.get().container(
2062: Messages.ERR_GENERIC_SQL_1,
2063: CmsDbSqlException.getErrorQuery(stmt)), e);
2064: } finally {
2065: m_sqlManager.closeAll(dbc, conn, stmt, res);
2066: }
2067:
2068: List result = new ArrayList(relations);
2069: Collections.sort(result, CmsRelation.COMPARATOR);
2070: return result;
2071: }
2072:
2073: /**
2074: * @see org.opencms.db.I_CmsVfsDriver#readResource(org.opencms.db.CmsDbContext, CmsUUID, org.opencms.util.CmsUUID, boolean)
2075: */
2076: public CmsResource readResource(CmsDbContext dbc,
2077: CmsUUID projectId, CmsUUID structureId,
2078: boolean includeDeleted) throws CmsDataAccessException {
2079:
2080: CmsResource resource = null;
2081: ResultSet res = null;
2082: PreparedStatement stmt = null;
2083: Connection conn = null;
2084:
2085: try {
2086: conn = m_sqlManager.getConnection(dbc);
2087: stmt = m_sqlManager.getPreparedStatement(conn, projectId,
2088: "C_RESOURCES_READBYID");
2089:
2090: stmt.setString(1, structureId.toString());
2091: res = stmt.executeQuery();
2092:
2093: if (res.next()) {
2094: resource = createResource(res, projectId);
2095: while (res.next()) {
2096: // do nothing only move through all rows because of mssql odbc driver
2097: }
2098: } else {
2099: throw new CmsVfsResourceNotFoundException(Messages
2100: .get().container(
2101: Messages.ERR_READ_RESOURCE_WITH_ID_1,
2102: structureId));
2103: }
2104: } catch (SQLException e) {
2105: throw new CmsDbSqlException(Messages.get().container(
2106: Messages.ERR_GENERIC_SQL_1,
2107: CmsDbSqlException.getErrorQuery(stmt)), e);
2108: } finally {
2109: m_sqlManager.closeAll(dbc, conn, stmt, res);
2110: }
2111:
2112: // check if this resource is marked as deleted and if we are allowed to return a deleted resource
2113: if ((resource != null) && resource.getState().isDeleted()
2114: && !includeDeleted) {
2115: throw new CmsVfsException(Messages.get().container(
2116: Messages.ERR_READ_DELETED_RESOURCE_1,
2117: dbc.removeSiteRoot(resource.getRootPath())));
2118: }
2119:
2120: return resource;
2121: }
2122:
2123: /**
2124: * @see org.opencms.db.I_CmsVfsDriver#readResource(org.opencms.db.CmsDbContext, CmsUUID, java.lang.String, boolean)
2125: */
2126: public CmsResource readResource(CmsDbContext dbc,
2127: CmsUUID projectId, String path, boolean includeDeleted)
2128: throws CmsDataAccessException {
2129:
2130: CmsResource resource = null;
2131: ResultSet res = null;
2132: PreparedStatement stmt = null;
2133: Connection conn = null;
2134:
2135: // must remove trailing slash
2136: path = CmsFileUtil.removeTrailingSeparator(path);
2137:
2138: try {
2139: conn = m_sqlManager.getConnection(dbc);
2140: stmt = m_sqlManager.getPreparedStatement(conn, projectId,
2141: "C_RESOURCES_READ");
2142:
2143: stmt.setString(1, path);
2144: res = stmt.executeQuery();
2145:
2146: if (res.next()) {
2147: resource = createResource(res, projectId);
2148: while (res.next()) {
2149: // do nothing only move through all rows because of mssql odbc driver
2150: }
2151: } else {
2152: throw new CmsVfsResourceNotFoundException(Messages
2153: .get().container(Messages.ERR_READ_RESOURCE_1,
2154: dbc.removeSiteRoot(path)));
2155: }
2156: } catch (SQLException e) {
2157: throw new CmsDbSqlException(Messages.get().container(
2158: Messages.ERR_GENERIC_SQL_1,
2159: CmsDbSqlException.getErrorQuery(stmt)), e);
2160: } finally {
2161: m_sqlManager.closeAll(dbc, conn, stmt, res);
2162: }
2163:
2164: // check if this resource is marked as deleted and if we are allowed to return a deleted resource
2165: if ((resource != null) && resource.getState().isDeleted()
2166: && !includeDeleted) {
2167: throw new CmsVfsResourceNotFoundException(Messages.get()
2168: .container(Messages.ERR_READ_DELETED_RESOURCE_1,
2169: dbc.removeSiteRoot(resource.getRootPath())));
2170: }
2171:
2172: return resource;
2173: }
2174:
2175: /**
2176: * @see org.opencms.db.I_CmsVfsDriver#readResources(org.opencms.db.CmsDbContext, CmsUUID, CmsResourceState, int)
2177: */
2178: public List readResources(CmsDbContext dbc, CmsUUID projectId,
2179: CmsResourceState state, int mode)
2180: throws CmsDataAccessException {
2181:
2182: List result = new ArrayList();
2183:
2184: ResultSet res = null;
2185: PreparedStatement stmt = null;
2186: Connection conn = null;
2187:
2188: try {
2189: conn = m_sqlManager.getConnection(dbc);
2190: if (mode == CmsDriverManager.READMODE_MATCHSTATE) {
2191: stmt = m_sqlManager
2192: .getPreparedStatement(conn, projectId,
2193: "C_RESOURCES_GET_RESOURCE_IN_PROJECT_WITH_STATE");
2194: stmt.setString(1, projectId.toString());
2195: stmt.setInt(2, state.getState());
2196: stmt.setInt(3, state.getState());
2197: stmt.setInt(4, state.getState());
2198: stmt.setInt(5, state.getState());
2199: } else if (mode == CmsDriverManager.READMODE_UNMATCHSTATE) {
2200: stmt = m_sqlManager
2201: .getPreparedStatement(conn, projectId,
2202: "C_RESOURCES_GET_RESOURCE_IN_PROJECT_WITHOUT_STATE");
2203: stmt.setString(1, projectId.toString());
2204: stmt.setInt(2, state.getState());
2205: stmt.setInt(3, state.getState());
2206: } else {
2207: stmt = m_sqlManager
2208: .getPreparedStatement(conn, projectId,
2209: "C_RESOURCES_GET_RESOURCE_IN_PROJECT_IGNORE_STATE");
2210: stmt.setString(1, projectId.toString());
2211: }
2212:
2213: res = stmt.executeQuery();
2214: while (res.next()) {
2215: CmsResource resource = createResource(res, projectId);
2216: result.add(resource);
2217: }
2218: } catch (SQLException e) {
2219: throw new CmsDbSqlException(Messages.get().container(
2220: Messages.ERR_GENERIC_SQL_1,
2221: CmsDbSqlException.getErrorQuery(stmt)), e);
2222: } finally {
2223: m_sqlManager.closeAll(dbc, conn, stmt, res);
2224: }
2225:
2226: return result;
2227: }
2228:
2229: /**
2230: * @see org.opencms.db.I_CmsVfsDriver#readResourcesForPrincipalACE(org.opencms.db.CmsDbContext, org.opencms.file.CmsProject, org.opencms.util.CmsUUID)
2231: */
2232: public List readResourcesForPrincipalACE(CmsDbContext dbc,
2233: CmsProject project, CmsUUID principalId)
2234: throws CmsDataAccessException {
2235:
2236: PreparedStatement stmt = null;
2237: Connection conn = null;
2238: ResultSet res = null;
2239: CmsResource currentResource = null;
2240: List resources = new ArrayList();
2241:
2242: try {
2243: conn = m_sqlManager.getConnection(dbc);
2244: stmt = m_sqlManager.getPreparedStatement(conn, project,
2245: "C_SELECT_RESOURCES_FOR_PRINCIPAL_ACE");
2246:
2247: stmt.setString(1, principalId.toString());
2248: res = stmt.executeQuery();
2249:
2250: while (res.next()) {
2251: currentResource = createFile(res, project.getUuid(),
2252: false);
2253: resources.add(currentResource);
2254: }
2255: } catch (SQLException e) {
2256: throw new CmsDbSqlException(Messages.get().container(
2257: Messages.ERR_GENERIC_SQL_1,
2258: CmsDbSqlException.getErrorQuery(stmt)), e);
2259: } finally {
2260: m_sqlManager.closeAll(dbc, conn, stmt, res);
2261: }
2262: return resources;
2263: }
2264:
2265: /**
2266: * @see org.opencms.db.I_CmsVfsDriver#readResourcesForPrincipalAttr(org.opencms.db.CmsDbContext, org.opencms.file.CmsProject, org.opencms.util.CmsUUID)
2267: */
2268: public List readResourcesForPrincipalAttr(CmsDbContext dbc,
2269: CmsProject project, CmsUUID principalId)
2270: throws CmsDataAccessException {
2271:
2272: PreparedStatement stmt = null;
2273: Connection conn = null;
2274: ResultSet res = null;
2275: CmsResource currentResource = null;
2276: List resources = new ArrayList();
2277:
2278: try {
2279: conn = m_sqlManager.getConnection(dbc);
2280: stmt = m_sqlManager.getPreparedStatement(conn, project,
2281: "C_SELECT_RESOURCES_FOR_PRINCIPAL_ATTR");
2282:
2283: stmt.setString(1, principalId.toString());
2284: stmt.setString(2, principalId.toString());
2285: res = stmt.executeQuery();
2286:
2287: while (res.next()) {
2288: currentResource = createFile(res, project.getUuid(),
2289: false);
2290: resources.add(currentResource);
2291: }
2292: } catch (SQLException e) {
2293: throw new CmsDbSqlException(Messages.get().container(
2294: Messages.ERR_GENERIC_SQL_1,
2295: CmsDbSqlException.getErrorQuery(stmt)), e);
2296: } finally {
2297: m_sqlManager.closeAll(dbc, conn, stmt, res);
2298: }
2299: return resources;
2300: }
2301:
2302: /**
2303: * @see org.opencms.db.I_CmsVfsDriver#readResourcesWithProperty(org.opencms.db.CmsDbContext, CmsUUID, org.opencms.util.CmsUUID, String, String)
2304: */
2305: public List readResourcesWithProperty(CmsDbContext dbc,
2306: CmsUUID projectId, CmsUUID propertyDef, String path,
2307: String value) throws CmsDataAccessException {
2308:
2309: List resources = new ArrayList();
2310: ResultSet res = null;
2311: PreparedStatement stmt = null;
2312: Connection conn = null;
2313:
2314: try {
2315: conn = m_sqlManager.getConnection(dbc);
2316: if (value == null) {
2317: stmt = m_sqlManager.getPreparedStatement(conn,
2318: projectId,
2319: "C_RESOURCES_GET_RESOURCE_WITH_PROPERTYDEF");
2320: stmt.setString(1, propertyDef.toString());
2321: stmt.setString(2, path + "%");
2322: stmt.setString(3, propertyDef.toString());
2323: stmt.setString(4, path + "%");
2324: } else {
2325: stmt = m_sqlManager
2326: .getPreparedStatement(conn, projectId,
2327: "C_RESOURCES_GET_RESOURCE_WITH_PROPERTYDEF_VALUE");
2328: stmt.setString(1, propertyDef.toString());
2329: stmt.setString(2, path + "%");
2330: stmt.setString(3, "%" + value + "%");
2331: stmt.setString(4, propertyDef.toString());
2332: stmt.setString(5, "%" + path + "%");
2333: stmt.setString(6, value + "%");
2334: }
2335: res = stmt.executeQuery();
2336:
2337: while (res.next()) {
2338: CmsResource resource = createResource(res, projectId);
2339: resources.add(resource);
2340: }
2341: } catch (SQLException e) {
2342: throw new CmsDbSqlException(Messages.get().container(
2343: Messages.ERR_GENERIC_SQL_1,
2344: CmsDbSqlException.getErrorQuery(stmt)), e);
2345: } finally {
2346: m_sqlManager.closeAll(dbc, conn, stmt, res);
2347: }
2348:
2349: return resources;
2350: }
2351:
2352: /**
2353: * @see org.opencms.db.I_CmsVfsDriver#readResourceTree(org.opencms.db.CmsDbContext, CmsUUID, java.lang.String, int, CmsResourceState, long, long, long, long, long, long, int)
2354: */
2355: public List readResourceTree(CmsDbContext dbc, CmsUUID projectId,
2356: String parentPath, int type, CmsResourceState state,
2357: long lastModifiedAfter, long lastModifiedBefore,
2358: long releasedAfter, long releasedBefore, long expiredAfter,
2359: long expiredBefore, int mode) throws CmsDataAccessException {
2360:
2361: List result = new ArrayList();
2362:
2363: StringBuffer conditions = new StringBuffer();
2364: List params = new ArrayList(5);
2365:
2366: // prepare the selection criteria
2367: prepareProjectCondition(projectId, mode, conditions, params);
2368: prepareResourceCondition(projectId, mode, conditions);
2369: prepareTypeCondition(projectId, type, mode, conditions, params);
2370: prepareTimeRangeCondition(projectId, lastModifiedAfter,
2371: lastModifiedBefore, conditions, params);
2372: prepareReleasedTimeRangeCondition(projectId, releasedAfter,
2373: releasedBefore, conditions, params);
2374: prepareExpiredTimeRangeCondition(projectId, expiredAfter,
2375: expiredBefore, conditions, params);
2376: preparePathCondition(projectId, parentPath, mode, conditions,
2377: params);
2378: prepareStateCondition(projectId, state, mode, conditions,
2379: params);
2380:
2381: // now read matching resources within the subtree
2382: ResultSet res = null;
2383: PreparedStatement stmt = null;
2384: Connection conn = null;
2385:
2386: try {
2387: conn = m_sqlManager.getConnection(dbc);
2388: StringBuffer queryBuf = new StringBuffer(256);
2389: queryBuf.append(m_sqlManager.readQuery(projectId,
2390: "C_RESOURCES_READ_TREE"));
2391: queryBuf.append(conditions);
2392: queryBuf.append(" ");
2393: queryBuf.append(m_sqlManager.readQuery(projectId,
2394: "C_RESOURCES_ORDER_BY_PATH"));
2395: stmt = m_sqlManager.getPreparedStatementForSql(conn,
2396: queryBuf.toString());
2397:
2398: for (int i = 0; i < params.size(); i++) {
2399: if (params.get(i) instanceof Integer) {
2400: stmt.setInt(i + 1, ((Integer) params.get(i))
2401: .intValue());
2402: } else if (params.get(i) instanceof Long) {
2403: stmt.setLong(i + 1, ((Long) params.get(i))
2404: .longValue());
2405: } else {
2406: stmt.setString(i + 1, (String) params.get(i));
2407: }
2408: }
2409:
2410: res = stmt.executeQuery();
2411: while (res.next()) {
2412: CmsResource resource = createResource(res, projectId);
2413: result.add(resource);
2414: }
2415:
2416: } catch (SQLException e) {
2417: throw new CmsDbSqlException(Messages.get().container(
2418: Messages.ERR_GENERIC_SQL_1,
2419: CmsDbSqlException.getErrorQuery(stmt)), e);
2420: } finally {
2421: m_sqlManager.closeAll(dbc, conn, stmt, res);
2422: }
2423:
2424: return result;
2425: }
2426:
2427: /**
2428: * @see org.opencms.db.I_CmsVfsDriver#readSiblings(org.opencms.db.CmsDbContext, CmsUUID, org.opencms.file.CmsResource, boolean)
2429: */
2430: public List readSiblings(CmsDbContext dbc, CmsUUID projectId,
2431: CmsResource resource, boolean includeDeleted)
2432: throws CmsDataAccessException {
2433:
2434: PreparedStatement stmt = null;
2435: Connection conn = null;
2436: ResultSet res = null;
2437: CmsResource currentResource = null;
2438: List vfsLinks = new ArrayList();
2439:
2440: try {
2441: conn = m_sqlManager.getConnection(dbc);
2442:
2443: if (includeDeleted) {
2444: stmt = m_sqlManager.getPreparedStatement(conn,
2445: projectId, "C_SELECT_VFS_SIBLINGS");
2446: } else {
2447: stmt = m_sqlManager.getPreparedStatement(conn,
2448: projectId, "C_SELECT_NONDELETED_VFS_SIBLINGS");
2449: }
2450:
2451: stmt.setString(1, resource.getResourceId().toString());
2452: res = stmt.executeQuery();
2453:
2454: while (res.next()) {
2455: currentResource = createFile(res, projectId, false);
2456: vfsLinks.add(currentResource);
2457: }
2458: } catch (SQLException e) {
2459: throw new CmsDbSqlException(Messages.get().container(
2460: Messages.ERR_GENERIC_SQL_1,
2461: CmsDbSqlException.getErrorQuery(stmt)), e);
2462: } finally {
2463: m_sqlManager.closeAll(dbc, conn, stmt, res);
2464: }
2465:
2466: return vfsLinks;
2467: }
2468:
2469: /**
2470: * @see org.opencms.db.I_CmsVfsDriver#readVersions(org.opencms.db.CmsDbContext, org.opencms.util.CmsUUID, org.opencms.util.CmsUUID, org.opencms.util.CmsUUID)
2471: */
2472: public Map readVersions(CmsDbContext dbc, CmsUUID projectId,
2473: CmsUUID resourceId, CmsUUID structureId)
2474: throws CmsDataAccessException {
2475:
2476: int structureVersion = -1;
2477: int resourceVersion = -1;
2478:
2479: Connection conn = null;
2480: PreparedStatement stmt = null;
2481: ResultSet res = null;
2482:
2483: try {
2484: conn = m_sqlManager.getConnection(dbc);
2485:
2486: // read the offline version numbers, first for the resource entry
2487: stmt = m_sqlManager.getPreparedStatement(conn, projectId,
2488: "C_RESOURCES_READ_VERSION_RES");
2489: stmt.setString(1, resourceId.toString());
2490: res = stmt.executeQuery();
2491: if (res.next()) {
2492: resourceVersion = res.getInt(m_sqlManager
2493: .readQuery("C_RESOURCES_VERSION"));
2494: while (res.next()) {
2495: // do nothing only move through all rows because of mssql odbc driver
2496: }
2497: }
2498: m_sqlManager.closeAll(dbc, null, stmt, res);
2499: // then for the structure entry
2500: stmt = m_sqlManager.getPreparedStatement(conn, projectId,
2501: "C_RESOURCES_READ_VERSION_STR");
2502: stmt.setString(1, structureId.toString());
2503: res = stmt.executeQuery();
2504: if (res.next()) {
2505: structureVersion = res.getInt(m_sqlManager
2506: .readQuery("C_RESOURCES_STRUCTURE_VERSION"));
2507: while (res.next()) {
2508: // do nothing only move through all rows because of mssql odbc driver
2509: }
2510: }
2511: } catch (SQLException e) {
2512: throw new CmsDbSqlException(Messages.get().container(
2513: Messages.ERR_GENERIC_SQL_1,
2514: CmsDbSqlException.getErrorQuery(stmt)), e);
2515: } finally {
2516: m_sqlManager.closeAll(dbc, conn, stmt, res);
2517: }
2518: Map result = new HashMap();
2519: result.put("structure", new Integer(structureVersion));
2520: result.put("resource", new Integer(resourceVersion));
2521: return result;
2522: }
2523:
2524: /**
2525: * @see org.opencms.db.I_CmsVfsDriver#removeFile(org.opencms.db.CmsDbContext, CmsUUID, org.opencms.file.CmsResource)
2526: */
2527: public void removeFile(CmsDbContext dbc, CmsUUID projectId,
2528: CmsResource resource) throws CmsDataAccessException {
2529:
2530: PreparedStatement stmt = null;
2531: Connection conn = null;
2532: int siblingCount = 0;
2533:
2534: try {
2535: conn = m_sqlManager.getConnection(dbc);
2536:
2537: // delete the structure record
2538: stmt = m_sqlManager.getPreparedStatement(conn, projectId,
2539: "C_STRUCTURE_DELETE_BY_STRUCTUREID");
2540: stmt.setString(1, resource.getStructureId().toString());
2541: stmt.executeUpdate();
2542:
2543: m_sqlManager.closeAll(dbc, null, stmt, null);
2544:
2545: // count the references to the resource
2546: siblingCount = countSiblings(dbc, projectId, resource
2547: .getResourceId());
2548:
2549: if (siblingCount > 0) {
2550: // update the link Count
2551: stmt = m_sqlManager.getPreparedStatement(conn,
2552: projectId, "C_RESOURCES_UPDATE_SIBLING_COUNT");
2553: stmt.setInt(1, siblingCount);
2554: stmt.setString(2, resource.getResourceId().toString());
2555: stmt.executeUpdate();
2556:
2557: m_sqlManager.closeAll(dbc, null, stmt, null);
2558:
2559: // update the resource flags
2560: stmt = m_sqlManager.getPreparedStatement(conn,
2561: projectId, "C_RESOURCES_UPDATE_FLAGS");
2562: stmt.setInt(1, resource.getFlags());
2563: stmt.setString(2, resource.getResourceId().toString());
2564: stmt.executeUpdate();
2565:
2566: } else {
2567: // if not referenced any longer, also delete the resource and the content record
2568: stmt = m_sqlManager.getPreparedStatement(conn,
2569: projectId, "C_RESOURCES_DELETE_BY_RESOURCEID");
2570: stmt.setString(1, resource.getResourceId().toString());
2571: stmt.executeUpdate();
2572:
2573: m_sqlManager.closeAll(dbc, null, stmt, null);
2574:
2575: // if online we have to keep historical content
2576: if (!projectId.equals(CmsProject.ONLINE_PROJECT_ID)
2577: || ((dbc.getProjectId() != null) && !dbc
2578: .getProjectId().isNullUUID())) {
2579: // delete content records with this resource id
2580: stmt = m_sqlManager.getPreparedStatement(conn,
2581: projectId, "C_OFFLINE_FILE_CONTENT_DELETE");
2582: stmt.setString(1, resource.getResourceId()
2583: .toString());
2584: stmt.executeUpdate();
2585: } else {
2586: // put the online content in the history
2587: stmt = m_sqlManager.getPreparedStatement(conn,
2588: "C_ONLINE_CONTENTS_HISTORY");
2589: stmt.setString(1, resource.getResourceId()
2590: .toString());
2591: stmt.executeUpdate();
2592: m_sqlManager.closeAll(dbc, null, stmt, null);
2593: }
2594: }
2595: } catch (SQLException e) {
2596: throw new CmsDbSqlException(Messages.get().container(
2597: Messages.ERR_GENERIC_SQL_1,
2598: CmsDbSqlException.getErrorQuery(stmt)), e);
2599: } finally {
2600: m_sqlManager.closeAll(dbc, conn, stmt, null);
2601: }
2602: }
2603:
2604: /**
2605: * @see org.opencms.db.I_CmsVfsDriver#removeFolder(org.opencms.db.CmsDbContext, org.opencms.file.CmsProject, org.opencms.file.CmsResource)
2606: */
2607: public void removeFolder(CmsDbContext dbc,
2608: CmsProject currentProject, CmsResource resource)
2609: throws CmsDataAccessException {
2610:
2611: if ((dbc.getRequestContext() != null)
2612: && (dbc.getRequestContext().getAttribute(
2613: REQ_ATTR_CHECK_PERMISSIONS) != null)) {
2614: // only check write permissions
2615: checkWritePermissionsInFolder(dbc, resource);
2616: return;
2617: }
2618:
2619: // check if the folder has any resources in it
2620: Iterator childResources = readChildResources(dbc,
2621: currentProject, resource, true, true).iterator();
2622:
2623: CmsUUID projectId = CmsProject.ONLINE_PROJECT_ID;
2624: if (currentProject.isOnlineProject()) {
2625: projectId = CmsUUID.getOpenCmsUUID(); // HACK: to get an offline project id
2626: }
2627:
2628: // collect the names of the resources inside the folder, excluding the moved resources
2629: StringBuffer errorResNames = new StringBuffer(128);
2630: while (childResources.hasNext()) {
2631: CmsResource errorRes = (CmsResource) childResources.next();
2632: // if deleting offline, or not moved, or just renamed inside the deleted folder
2633: // so, it may remain some orphan online entries for moved resources
2634: // which will be fixed during the publishing of the moved resources
2635: boolean error = !currentProject.isOnlineProject();
2636: if (!error) {
2637: try {
2638: String originalPath = m_driverManager
2639: .getVfsDriver().readResource(dbc,
2640: projectId, errorRes.getRootPath(),
2641: true).getRootPath();
2642: error = originalPath.equals(errorRes.getRootPath())
2643: || originalPath.startsWith(resource
2644: .getRootPath());
2645: } catch (CmsVfsResourceNotFoundException e) {
2646: // ignore
2647: }
2648: }
2649: if (error) {
2650: if (errorResNames.length() != 0) {
2651: errorResNames.append(", ");
2652: }
2653: errorResNames.append("["
2654: + dbc.removeSiteRoot(errorRes.getRootPath())
2655: + "]");
2656: }
2657: }
2658:
2659: // the current implementation only deletes empty folders
2660: if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(errorResNames
2661: .toString())) {
2662:
2663: throw new CmsVfsException(Messages.get().container(
2664: Messages.ERR_DELETE_NONEMTY_FOLDER_2,
2665: dbc.removeSiteRoot(resource.getRootPath()),
2666: errorResNames.toString()));
2667: }
2668: internalRemoveFolder(dbc, currentProject, resource);
2669: }
2670:
2671: /**
2672: * @see org.opencms.db.I_CmsVfsDriver#replaceResource(org.opencms.db.CmsDbContext, org.opencms.file.CmsResource, byte[], int)
2673: */
2674: public void replaceResource(CmsDbContext dbc,
2675: CmsResource newResource, byte[] resContent,
2676: int newResourceType) throws CmsDataAccessException {
2677:
2678: if (resContent == null) {
2679: // nothing to do
2680: return;
2681: }
2682: Connection conn = null;
2683: PreparedStatement stmt = null;
2684: try {
2685: // write the file content
2686: writeContent(dbc, newResource.getResourceId(), resContent);
2687:
2688: // update the resource record
2689: conn = m_sqlManager.getConnection(dbc);
2690: stmt = m_sqlManager.getPreparedStatement(conn, dbc
2691: .currentProject(), "C_RESOURCE_REPLACE");
2692: stmt.setInt(1, newResourceType);
2693: stmt.setInt(2, resContent.length);
2694: stmt.setLong(3, System.currentTimeMillis());
2695: stmt.setString(4, newResource.getResourceId().toString());
2696: stmt.executeUpdate();
2697:
2698: } catch (SQLException e) {
2699: throw new CmsDbSqlException(Messages.get().container(
2700: Messages.ERR_GENERIC_SQL_1,
2701: CmsDbSqlException.getErrorQuery(stmt)), e);
2702: } finally {
2703: m_sqlManager.closeAll(dbc, conn, stmt, null);
2704: }
2705: }
2706:
2707: /**
2708: * @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)
2709: */
2710: public void transferResource(CmsDbContext dbc, CmsProject project,
2711: CmsResource resource, CmsUUID createdUser,
2712: CmsUUID lastModifiedUser) throws CmsDataAccessException {
2713:
2714: if (createdUser == null) {
2715: createdUser = resource.getUserCreated();
2716: }
2717: if (lastModifiedUser == null) {
2718: lastModifiedUser = resource.getUserLastModified();
2719: }
2720:
2721: PreparedStatement stmt = null;
2722: Connection conn = null;
2723: try {
2724: conn = m_sqlManager.getConnection(dbc);
2725: stmt = m_sqlManager.getPreparedStatement(conn, project,
2726: "C_RESOURCES_TRANSFER_RESOURCE");
2727: stmt.setString(1, createdUser.toString());
2728: stmt.setString(2, lastModifiedUser.toString());
2729: stmt.setString(3, resource.getResourceId().toString());
2730: stmt.executeUpdate();
2731: } catch (SQLException e) {
2732: throw new CmsDbSqlException(Messages.get().container(
2733: Messages.ERR_GENERIC_SQL_1,
2734: CmsDbSqlException.getErrorQuery(stmt)), e);
2735: } finally {
2736: m_sqlManager.closeAll(dbc, conn, stmt, null);
2737: }
2738: }
2739:
2740: /**
2741: * @see org.opencms.db.I_CmsVfsDriver#updateRelations(CmsDbContext, CmsProject, CmsResource)
2742: */
2743: public void updateRelations(CmsDbContext dbc,
2744: CmsProject onlineProject, CmsResource offlineResource)
2745: throws CmsDataAccessException {
2746:
2747: // delete online relations
2748: m_driverManager.getVfsDriver().deleteRelations(dbc,
2749: onlineProject.getUuid(), offlineResource,
2750: CmsRelationFilter.TARGETS);
2751:
2752: CmsUUID projectId;
2753: if (!dbc.getProjectId().isNullUUID()) {
2754: projectId = CmsProject.ONLINE_PROJECT_ID;
2755: } else {
2756: projectId = dbc.currentProject().getUuid();
2757: }
2758:
2759: // copy offline to online relations
2760: CmsUUID dbcProjectId = dbc.getProjectId();
2761: dbc.setProjectId(CmsUUID.getNullUUID());
2762: Iterator itRelations = m_driverManager.getVfsDriver()
2763: .readRelations(dbc, projectId, offlineResource,
2764: CmsRelationFilter.TARGETS).iterator();
2765: dbc.setProjectId(dbcProjectId);
2766: while (itRelations.hasNext()) {
2767: m_driverManager.getVfsDriver().createRelation(dbc,
2768: onlineProject.getUuid(),
2769: (CmsRelation) itRelations.next());
2770: }
2771: }
2772:
2773: /**
2774: * @see org.opencms.db.I_CmsVfsDriver#validateResourceIdExists(org.opencms.db.CmsDbContext, CmsUUID, org.opencms.util.CmsUUID)
2775: */
2776: public boolean validateResourceIdExists(CmsDbContext dbc,
2777: CmsUUID projectId, CmsUUID resourceId)
2778: throws CmsDataAccessException {
2779:
2780: Connection conn = null;
2781: PreparedStatement stmt = null;
2782: ResultSet res = null;
2783: boolean exists = false;
2784:
2785: try {
2786: conn = m_sqlManager.getConnection(dbc);
2787: stmt = m_sqlManager.getPreparedStatement(conn, projectId,
2788: "C_RESOURCES_READ_RESOURCE_STATE");
2789: stmt.setString(1, resourceId.toString());
2790:
2791: res = stmt.executeQuery();
2792: exists = res.next();
2793: } catch (SQLException e) {
2794: throw new CmsDbSqlException(Messages.get().container(
2795: Messages.ERR_GENERIC_SQL_1,
2796: CmsDbSqlException.getErrorQuery(stmt)), e);
2797: } finally {
2798: m_sqlManager.closeAll(dbc, conn, stmt, res);
2799: }
2800:
2801: return exists;
2802: }
2803:
2804: /**
2805: * @see org.opencms.db.I_CmsVfsDriver#validateStructureIdExists(org.opencms.db.CmsDbContext, CmsUUID, org.opencms.util.CmsUUID)
2806: */
2807: public boolean validateStructureIdExists(CmsDbContext dbc,
2808: CmsUUID projectId, CmsUUID structureId)
2809: throws CmsDataAccessException {
2810:
2811: Connection conn = null;
2812: PreparedStatement stmt = null;
2813: ResultSet res = null;
2814: boolean found = false;
2815: int count = 0;
2816:
2817: try {
2818: conn = m_sqlManager.getConnection(dbc);
2819: stmt = m_sqlManager.getPreparedStatement(conn, projectId,
2820: "C_RESOURCES_SELECT_STRUCTURE_ID");
2821: stmt.setString(1, structureId.toString());
2822:
2823: res = stmt.executeQuery();
2824: if (res.next()) {
2825: count = res.getInt(1);
2826: found = (count == 1);
2827: while (res.next()) {
2828: // do nothing only move through all rows because of mssql odbc driver
2829: }
2830: } else {
2831: found = false;
2832: }
2833: } catch (SQLException e) {
2834: throw new CmsDbSqlException(Messages.get().container(
2835: Messages.ERR_GENERIC_SQL_1,
2836: CmsDbSqlException.getErrorQuery(stmt)), e);
2837: } finally {
2838: m_sqlManager.closeAll(dbc, conn, stmt, res);
2839: }
2840:
2841: return found;
2842: }
2843:
2844: /**
2845: * @see org.opencms.db.I_CmsVfsDriver#writeContent(org.opencms.db.CmsDbContext, org.opencms.util.CmsUUID, byte[])
2846: */
2847: public void writeContent(CmsDbContext dbc, CmsUUID resourceId,
2848: byte[] content) throws CmsDataAccessException {
2849:
2850: Connection conn = null;
2851: PreparedStatement stmt = null;
2852:
2853: try {
2854: conn = m_sqlManager.getConnection(dbc);
2855: stmt = m_sqlManager.getPreparedStatement(conn, dbc
2856: .currentProject(), "C_OFFLINE_CONTENTS_UPDATE");
2857: // update the file content in the database.
2858: if (content.length < 2000) {
2859: stmt.setBytes(1, content);
2860: } else {
2861: stmt.setBinaryStream(1, new ByteArrayInputStream(
2862: content), content.length);
2863: }
2864: stmt.setString(2, resourceId.toString());
2865: stmt.executeUpdate();
2866: } catch (SQLException e) {
2867: throw new CmsDbSqlException(Messages.get().container(
2868: Messages.ERR_GENERIC_SQL_1,
2869: CmsDbSqlException.getErrorQuery(stmt)), e);
2870: } finally {
2871: m_sqlManager.closeAll(dbc, conn, stmt, null);
2872: }
2873: }
2874:
2875: /**
2876: * @see org.opencms.db.I_CmsVfsDriver#writeLastModifiedProjectId(org.opencms.db.CmsDbContext, org.opencms.file.CmsProject, CmsUUID, org.opencms.file.CmsResource)
2877: */
2878: public void writeLastModifiedProjectId(CmsDbContext dbc,
2879: CmsProject project, CmsUUID projectId, CmsResource resource)
2880: throws CmsDataAccessException {
2881:
2882: Connection conn = null;
2883: PreparedStatement stmt = null;
2884:
2885: try {
2886: conn = m_sqlManager.getConnection(dbc);
2887: stmt = m_sqlManager.getPreparedStatement(conn, project,
2888: "C_RESOURCES_UPDATE_PROJECT_LASTMODIFIED");
2889: stmt.setString(1, projectId.toString());
2890: stmt.setString(2, resource.getResourceId().toString());
2891: stmt.executeUpdate();
2892: } catch (SQLException e) {
2893: throw new CmsDbSqlException(Messages.get().container(
2894: Messages.ERR_GENERIC_SQL_1,
2895: CmsDbSqlException.getErrorQuery(stmt)), e);
2896: } finally {
2897: m_sqlManager.closeAll(dbc, conn, stmt, null);
2898: }
2899: }
2900:
2901: /**
2902: * @see org.opencms.db.I_CmsVfsDriver#writePropertyObject(org.opencms.db.CmsDbContext, org.opencms.file.CmsProject, org.opencms.file.CmsResource, org.opencms.file.CmsProperty)
2903: */
2904: public void writePropertyObject(CmsDbContext dbc,
2905: CmsProject project, CmsResource resource,
2906: CmsProperty property) throws CmsDataAccessException {
2907:
2908: // TODO: check if we need autocreation for link property definition types too
2909: CmsPropertyDefinition propertyDefinition = null;
2910: try {
2911: // read the property definition
2912: propertyDefinition = readPropertyDefinition(dbc, property
2913: .getName(), project.getUuid());
2914: } catch (CmsDbEntryNotFoundException e) {
2915: if (property.autoCreatePropertyDefinition()) {
2916: propertyDefinition = createPropertyDefinition(dbc,
2917: project.getUuid(), property.getName(),
2918: CmsPropertyDefinition.TYPE_NORMAL);
2919: try {
2920: readPropertyDefinition(dbc, property.getName(),
2921: CmsProject.ONLINE_PROJECT_ID);
2922: } catch (CmsDataAccessException e1) {
2923: createPropertyDefinition(dbc,
2924: CmsProject.ONLINE_PROJECT_ID, property
2925: .getName(),
2926: CmsPropertyDefinition.TYPE_NORMAL);
2927: }
2928: try {
2929: m_driverManager.getHistoryDriver()
2930: .readPropertyDefinition(dbc,
2931: property.getName());
2932: } catch (CmsDataAccessException e1) {
2933: m_driverManager.getHistoryDriver()
2934: .createPropertyDefinition(dbc,
2935: property.getName(),
2936: CmsPropertyDefinition.TYPE_NORMAL);
2937: }
2938: } else {
2939: throw new CmsDbEntryNotFoundException(
2940: Messages
2941: .get()
2942: .container(
2943: Messages.ERR_NO_PROPERTYDEF_WITH_NAME_1,
2944: property.getName()));
2945: }
2946: }
2947:
2948: PreparedStatement stmt = null;
2949: Connection conn = null;
2950:
2951: try {
2952: // read the existing property to test if we need the
2953: // insert or update query to write a property value
2954: CmsProperty existingProperty = readPropertyObject(dbc,
2955: propertyDefinition.getName(), project, resource);
2956:
2957: if (existingProperty.isIdentical(property)) {
2958: // property already has the identical values set, no write required
2959: return;
2960: }
2961:
2962: conn = m_sqlManager.getConnection(dbc);
2963:
2964: for (int i = 0; i < 2; i++) {
2965: int mappingType = -1;
2966: String value = null;
2967: CmsUUID id = null;
2968: boolean existsPropertyValue = false;
2969: boolean deletePropertyValue = false;
2970:
2971: // 1) take any required decisions to choose and fill the correct SQL query
2972:
2973: if (i == 0) {
2974: // write/delete the *structure value* on the first cycle
2975: if ((existingProperty.getStructureValue() != null)
2976: && property.isDeleteStructureValue()) {
2977: // this property value is marked to be deleted
2978: deletePropertyValue = true;
2979: } else {
2980: value = property.getStructureValue();
2981: if (CmsStringUtil
2982: .isEmptyOrWhitespaceOnly(value)) {
2983: // no structure value set or the structure value is an empty string,
2984: // continue with the resource value
2985: continue;
2986: }
2987: }
2988:
2989: // set the vars to be written to the database
2990: mappingType = CmsProperty.STRUCTURE_RECORD_MAPPING;
2991: id = resource.getStructureId();
2992: existsPropertyValue = existingProperty
2993: .getStructureValue() != null;
2994: } else {
2995: // write/delete the *resource value* on the second cycle
2996: if ((existingProperty.getResourceValue() != null)
2997: && property.isDeleteResourceValue()) {
2998: // this property value is marked to be deleted
2999: deletePropertyValue = true;
3000: } else {
3001: value = property.getResourceValue();
3002: if (CmsStringUtil
3003: .isEmptyOrWhitespaceOnly(value)) {
3004: // no resource value set or the resource value is an empty string,
3005: // break out of the loop
3006: break;
3007: }
3008: }
3009:
3010: // set the vars to be written to the database
3011: mappingType = CmsProperty.RESOURCE_RECORD_MAPPING;
3012: id = resource.getResourceId();
3013: existsPropertyValue = existingProperty
3014: .getResourceValue() != null;
3015: }
3016:
3017: // 2) execute the SQL query
3018: try {
3019: if (!deletePropertyValue) {
3020: // insert/update the property value
3021: if (existsPropertyValue) {
3022: // {structure|resource} property value already exists- use update statement
3023: stmt = m_sqlManager.getPreparedStatement(
3024: conn, project.getUuid(),
3025: "C_PROPERTIES_UPDATE");
3026: stmt.setString(1, m_sqlManager
3027: .validateEmpty(value));
3028: stmt.setString(2, id.toString());
3029: stmt.setInt(3, mappingType);
3030: stmt.setString(4, propertyDefinition
3031: .getId().toString());
3032: } else {
3033: // {structure|resource} property value doesn't exist- use create statement
3034: stmt = m_sqlManager.getPreparedStatement(
3035: conn, project.getUuid(),
3036: "C_PROPERTIES_CREATE");
3037: stmt.setString(1, new CmsUUID().toString());
3038: stmt.setString(2, propertyDefinition
3039: .getId().toString());
3040: stmt.setString(3, id.toString());
3041: stmt.setInt(4, mappingType);
3042: stmt.setString(5, m_sqlManager
3043: .validateEmpty(value));
3044: }
3045: } else {
3046: // {structure|resource} property value marked as deleted- use delete statement
3047: stmt = m_sqlManager.getPreparedStatement(conn,
3048: project.getUuid(),
3049: "C_PROPERTIES_DELETE");
3050: stmt.setString(1, propertyDefinition.getId()
3051: .toString());
3052: stmt.setString(2, id.toString());
3053: stmt.setInt(3, mappingType);
3054: }
3055: stmt.executeUpdate();
3056: } finally {
3057: m_sqlManager.closeAll(dbc, null, stmt, null);
3058: }
3059: }
3060: } catch (SQLException e) {
3061: throw new CmsDbSqlException(Messages.get().container(
3062: Messages.ERR_GENERIC_SQL_1,
3063: CmsDbSqlException.getErrorQuery(stmt)), e);
3064: } finally {
3065: m_sqlManager.closeAll(dbc, conn, stmt, null);
3066: }
3067: }
3068:
3069: /**
3070: * @see org.opencms.db.I_CmsVfsDriver#writePropertyObjects(org.opencms.db.CmsDbContext, org.opencms.file.CmsProject, org.opencms.file.CmsResource, java.util.List)
3071: */
3072: public void writePropertyObjects(CmsDbContext dbc,
3073: CmsProject project, CmsResource resource, List properties)
3074: throws CmsDataAccessException {
3075:
3076: CmsProperty property = null;
3077:
3078: for (int i = 0; i < properties.size(); i++) {
3079: property = (CmsProperty) properties.get(i);
3080: writePropertyObject(dbc, project, resource, property);
3081: }
3082: }
3083:
3084: /**
3085: * @see org.opencms.db.I_CmsVfsDriver#writeResource(org.opencms.db.CmsDbContext, CmsUUID, org.opencms.file.CmsResource, int)
3086: */
3087: public void writeResource(CmsDbContext dbc, CmsUUID projectId,
3088: CmsResource resource, int changed)
3089: throws CmsDataAccessException {
3090:
3091: // validate the resource length
3092: internalValidateResourceLength(resource);
3093:
3094: String resourcePath = CmsFileUtil
3095: .removeTrailingSeparator(resource.getRootPath());
3096:
3097: // this task is split into two statements because some DBs (e.g. Oracle) doesn't support multi-table updates
3098: PreparedStatement stmt = null;
3099: Connection conn = null;
3100: long resourceDateModified;
3101:
3102: if (resource.isTouched()) {
3103: resourceDateModified = resource.getDateLastModified();
3104: } else {
3105: resourceDateModified = System.currentTimeMillis();
3106: }
3107:
3108: CmsResourceState structureState = resource.getState();
3109: CmsResourceState resourceState = resource.getState();
3110: CmsResourceState structureStateOld = internalReadStructureState(
3111: dbc, projectId, resource);
3112: CmsResourceState resourceStateOld = internalReadResourceState(
3113: dbc, projectId, resource);
3114: CmsUUID projectLastModified = projectId;
3115:
3116: if (changed == CmsDriverManager.UPDATE_RESOURCE_STATE) {
3117: resourceState = resourceStateOld;
3118: resourceState = (resourceState.isNew() ? CmsResource.STATE_NEW
3119: : CmsResource.STATE_CHANGED);
3120: structureState = structureStateOld;
3121: } else if (changed == CmsDriverManager.UPDATE_STRUCTURE_STATE) {
3122: structureState = structureStateOld;
3123: structureState = (structureState.isNew() ? CmsResource.STATE_NEW
3124: : CmsResource.STATE_CHANGED);
3125: } else if (changed == CmsDriverManager.NOTHING_CHANGED) {
3126: projectLastModified = resource.getProjectLastModified();
3127: } else {
3128: resourceState = resourceStateOld;
3129: resourceState = (resourceState.isNew() ? CmsResource.STATE_NEW
3130: : CmsResource.STATE_CHANGED);
3131: structureState = structureStateOld;
3132: structureState = (structureState.isNew() ? CmsResource.STATE_NEW
3133: : CmsResource.STATE_CHANGED);
3134: }
3135:
3136: try {
3137: conn = m_sqlManager.getConnection(dbc);
3138:
3139: if (changed != CmsDriverManager.UPDATE_STRUCTURE_STATE) {
3140: // if the resource was unchanged
3141: stmt = m_sqlManager.getPreparedStatement(conn,
3142: projectId, "C_RESOURCES_UPDATE_RESOURCES");
3143: stmt.setInt(1, resource.getTypeId());
3144: stmt.setInt(2, resource.getFlags());
3145: stmt.setLong(3, resourceDateModified);
3146: stmt.setString(4, resource.getUserLastModified()
3147: .toString());
3148: stmt.setInt(5, resourceState.getState());
3149: stmt.setInt(6, resource.getLength());
3150: stmt.setLong(7, resource.getDateContent());
3151: stmt.setString(8, projectLastModified.toString());
3152: stmt.setInt(9, countSiblings(dbc, projectId, resource
3153: .getResourceId()));
3154: stmt.setString(10, resource.getResourceId().toString());
3155: stmt.executeUpdate();
3156: m_sqlManager.closeAll(dbc, null, stmt, null);
3157: } else {
3158: stmt = m_sqlManager.getPreparedStatement(conn,
3159: projectId,
3160: "C_RESOURCES_UPDATE_RESOURCES_WITHOUT_STATE");
3161: stmt.setInt(1, resource.getTypeId());
3162: stmt.setInt(2, resource.getFlags());
3163: stmt.setLong(3, resourceDateModified);
3164: stmt.setString(4, resource.getUserLastModified()
3165: .toString());
3166: stmt.setInt(5, resource.getLength());
3167: stmt.setLong(6, resource.getDateContent());
3168: stmt.setString(7, projectLastModified.toString());
3169: stmt.setInt(8, countSiblings(dbc, projectId, resource
3170: .getResourceId()));
3171: stmt.setString(9, resource.getResourceId().toString());
3172: stmt.executeUpdate();
3173: m_sqlManager.closeAll(dbc, null, stmt, null);
3174: }
3175:
3176: // read the parent id
3177: String parentId = internalReadParentId(dbc, projectId,
3178: resourcePath);
3179:
3180: // update the structure
3181: stmt = m_sqlManager.getPreparedStatement(conn, projectId,
3182: "C_RESOURCES_UPDATE_STRUCTURE");
3183: stmt.setString(1, resource.getResourceId().toString());
3184: stmt.setString(2, resourcePath);
3185: stmt.setInt(3, structureState.getState());
3186: stmt.setLong(4, resource.getDateReleased());
3187: stmt.setLong(5, resource.getDateExpired());
3188: stmt.setString(6, parentId);
3189: stmt.setString(7, resource.getStructureId().toString());
3190: stmt.executeUpdate();
3191: } catch (SQLException e) {
3192: throw new CmsDbSqlException(Messages.get().container(
3193: Messages.ERR_GENERIC_SQL_1,
3194: CmsDbSqlException.getErrorQuery(stmt)), e);
3195: } finally {
3196: m_sqlManager.closeAll(dbc, conn, stmt, null);
3197: }
3198: }
3199:
3200: /**
3201: * @see org.opencms.db.I_CmsVfsDriver#writeResourceState(org.opencms.db.CmsDbContext, org.opencms.file.CmsProject, org.opencms.file.CmsResource, int, boolean)
3202: */
3203: public void writeResourceState(CmsDbContext dbc,
3204: CmsProject project, CmsResource resource, int changed,
3205: boolean isPublishing) throws CmsDataAccessException {
3206:
3207: PreparedStatement stmt = null;
3208: Connection conn = null;
3209:
3210: if (project.getUuid().equals(CmsProject.ONLINE_PROJECT_ID)) {
3211: return;
3212: }
3213:
3214: try {
3215: conn = m_sqlManager.getConnection(dbc);
3216:
3217: if (changed == CmsDriverManager.UPDATE_RESOURCE_PROJECT) {
3218: stmt = m_sqlManager.getPreparedStatement(conn, project,
3219: "C_RESOURCES_UPDATE_RESOURCE_PROJECT");
3220: stmt.setInt(1, resource.getFlags());
3221: stmt.setString(2, project.getUuid().toString());
3222: stmt.setString(3, resource.getResourceId().toString());
3223: stmt.executeUpdate();
3224: m_sqlManager.closeAll(dbc, null, stmt, null);
3225: }
3226:
3227: if (changed == CmsDriverManager.UPDATE_RESOURCE) {
3228: stmt = m_sqlManager
3229: .getPreparedStatement(conn, project,
3230: "C_RESOURCES_UPDATE_RESOURCE_STATELASTMODIFIED");
3231: stmt.setInt(1, resource.getState().getState());
3232: stmt.setLong(2, resource.getDateLastModified());
3233: stmt.setString(3, resource.getUserLastModified()
3234: .toString());
3235: stmt.setString(4, project.getUuid().toString());
3236: stmt.setString(5, resource.getResourceId().toString());
3237: stmt.executeUpdate();
3238: m_sqlManager.closeAll(dbc, null, stmt, null);
3239: }
3240:
3241: if ((changed == CmsDriverManager.UPDATE_RESOURCE_STATE)
3242: || (changed == CmsDriverManager.UPDATE_ALL)) {
3243: stmt = m_sqlManager.getPreparedStatement(conn, project,
3244: "C_RESOURCES_UPDATE_RESOURCE_STATE");
3245: stmt.setInt(1, resource.getState().getState());
3246: stmt.setString(2, project.getUuid().toString());
3247: stmt.setString(3, resource.getResourceId().toString());
3248: stmt.executeUpdate();
3249: m_sqlManager.closeAll(dbc, null, stmt, null);
3250: }
3251:
3252: if ((changed == CmsDriverManager.UPDATE_STRUCTURE)
3253: || (changed == CmsDriverManager.UPDATE_ALL)
3254: || (changed == CmsDriverManager.UPDATE_STRUCTURE_STATE)) {
3255: stmt = m_sqlManager.getPreparedStatement(conn, project,
3256: "C_RESOURCES_UPDATE_STRUCTURE_STATE");
3257: stmt.setInt(1, resource.getState().getState());
3258: stmt.setString(2, resource.getStructureId().toString());
3259: stmt.executeUpdate();
3260: m_sqlManager.closeAll(dbc, null, stmt, null);
3261: }
3262:
3263: if ((changed == CmsDriverManager.UPDATE_STRUCTURE)
3264: || (changed == CmsDriverManager.UPDATE_ALL)) {
3265: stmt = m_sqlManager.getPreparedStatement(conn, project,
3266: "C_RESOURCES_UPDATE_RELEASE_EXPIRED");
3267: stmt.setLong(1, resource.getDateReleased());
3268: stmt.setLong(2, resource.getDateExpired());
3269: stmt.setString(3, resource.getStructureId().toString());
3270: stmt.executeUpdate();
3271: m_sqlManager.closeAll(dbc, null, stmt, null);
3272: }
3273: } catch (SQLException e) {
3274: throw new CmsDbSqlException(Messages.get().container(
3275: Messages.ERR_GENERIC_SQL_1,
3276: CmsDbSqlException.getErrorQuery(stmt)), e);
3277: } finally {
3278: m_sqlManager.closeAll(dbc, conn, stmt, null);
3279: }
3280:
3281: if (isPublishing) {
3282: internalUpdateVersions(dbc, resource);
3283: }
3284: }
3285:
3286: /**
3287: * Checks that the current user has write permissions for all subresources of the given folder.<p>
3288: *
3289: * @param dbc the current database context
3290: * @param folder the folder to check
3291: *
3292: * @throws CmsDataAccessException if something goes wrong
3293: */
3294: protected void checkWritePermissionsInFolder(CmsDbContext dbc,
3295: CmsResource folder) throws CmsDataAccessException {
3296:
3297: ResultSet res = null;
3298: PreparedStatement stmt = null;
3299: Connection conn = null;
3300:
3301: CmsUUID projectId = dbc.getRequestContext().currentProject()
3302: .getUuid();
3303:
3304: // first read all subresources with ACEs
3305: List resources = new ArrayList();
3306: try {
3307: conn = m_sqlManager.getConnection(dbc);
3308: stmt = m_sqlManager.getPreparedStatement(conn, projectId,
3309: "C_RESOURCES_READ_WITH_ACE_1");
3310: stmt.setString(1, folder.getRootPath() + "%");
3311: res = stmt.executeQuery();
3312:
3313: while (res.next()) {
3314: resources.add(createResource(res, projectId));
3315: }
3316: } catch (SQLException e) {
3317: throw new CmsDbSqlException(Messages.get().container(
3318: Messages.ERR_GENERIC_SQL_1,
3319: CmsDbSqlException.getErrorQuery(stmt)), e);
3320: } finally {
3321: m_sqlManager.closeAll(dbc, conn, stmt, res);
3322: }
3323:
3324: // check current user write permission for each of these resources
3325: Iterator itResources = resources.iterator();
3326: while (itResources.hasNext()) {
3327: CmsResource resource = (CmsResource) itResources.next();
3328: try {
3329: m_driverManager.getSecurityManager().checkPermissions(
3330: dbc.getRequestContext(), resource,
3331: CmsPermissionSet.ACCESS_WRITE, false,
3332: CmsResourceFilter.ALL);
3333: } catch (CmsException e) {
3334: throw new CmsDataAccessException(e
3335: .getMessageContainer(), e);
3336: }
3337: }
3338:
3339: // then check for possible jsp pages without permissions
3340: CmsResourceFilter filter = CmsResourceFilter.ALL
3341: .addRequireType(CmsResourceTypeJsp.getStaticTypeId());
3342: itResources = readResourceTree(dbc, projectId,
3343: folder.getRootPath(), filter.getType(),
3344: filter.getState(), filter.getModifiedAfter(),
3345: filter.getModifiedBefore(), filter.getReleaseAfter(),
3346: filter.getReleaseBefore(), filter.getExpireAfter(),
3347: filter.getExpireBefore(),
3348: CmsDriverManager.READMODE_INCLUDE_TREE).iterator();
3349: while (itResources.hasNext()) {
3350: CmsResource resource = (CmsResource) itResources.next();
3351: try {
3352: m_driverManager.getSecurityManager().checkPermissions(
3353: dbc.getRequestContext(), resource,
3354: CmsPermissionSet.ACCESS_WRITE, false,
3355: CmsResourceFilter.ALL);
3356: } catch (CmsException e) {
3357: throw new CmsDataAccessException(e
3358: .getMessageContainer(), e);
3359: }
3360: }
3361: }
3362:
3363: /**
3364: * Escapes the database wildcards within the resource path.<p>
3365: *
3366: * This method is required to ensure chars in the resource path that have a special
3367: * meaning in SQL (for example "_", which is the "any char" operator) are escaped.<p>
3368: *
3369: * It will escape the following chars:
3370: * <ul>
3371: * <li>"_" to "|_"</li>
3372: * </ul>
3373: *
3374: * @param path the resource path
3375: * @return the escaped resource path
3376: */
3377: protected String escapeDbWildcard(String path) {
3378:
3379: return CmsStringUtil.substitute(path, "_", "|_");
3380: }
3381:
3382: /**
3383: * Returns the count of properties for a property definition.<p>
3384: *
3385: * @param dbc the current database context
3386: * @param propertyDefinition the property definition to test
3387: * @param projectId the ID of the current project
3388: *
3389: * @return the amount of properties for a property definition
3390: * @throws CmsDataAccessException if something goes wrong
3391: */
3392: protected int internalCountProperties(CmsDbContext dbc,
3393: CmsPropertyDefinition propertyDefinition, CmsUUID projectId)
3394: throws CmsDataAccessException {
3395:
3396: ResultSet res = null;
3397: PreparedStatement stmt = null;
3398: Connection conn = null;
3399: int count = 0;
3400:
3401: try {
3402: // create statement
3403: conn = m_sqlManager.getConnection(dbc);
3404: stmt = m_sqlManager.getPreparedStatement(conn, projectId,
3405: "C_PROPERTIES_READALL_COUNT");
3406: stmt.setString(1, propertyDefinition.getId().toString());
3407: res = stmt.executeQuery();
3408:
3409: if (res.next()) {
3410: count = res.getInt(1);
3411: while (res.next()) {
3412: // do nothing only move through all rows because of mssql odbc driver
3413: }
3414: } else {
3415: throw new CmsDbConsistencyException(Messages.get()
3416: .container(Messages.ERR_COUNTING_PROPERTIES_1,
3417: propertyDefinition.getName()));
3418: }
3419: } catch (SQLException e) {
3420: throw new CmsDbSqlException(Messages.get().container(
3421: Messages.ERR_GENERIC_SQL_1,
3422: CmsDbSqlException.getErrorQuery(stmt)), e);
3423: } finally {
3424: m_sqlManager.closeAll(dbc, conn, stmt, res);
3425: }
3426:
3427: return count;
3428: }
3429:
3430: /**
3431: * Returns the parent id of the given resource.<p>
3432: *
3433: * @param dbc the current database context
3434: * @param projectId the current project id
3435: * @param resourcename the resource name to read the parent id for
3436: *
3437: * @return the parent id of the given resource
3438: *
3439: * @throws CmsDataAccessException if something goes wrong
3440: */
3441: protected String internalReadParentId(CmsDbContext dbc,
3442: CmsUUID projectId, String resourcename)
3443: throws CmsDataAccessException {
3444:
3445: if ("/".equalsIgnoreCase(resourcename)) {
3446: return CmsUUID.getNullUUID().toString();
3447: }
3448:
3449: String parent = CmsResource.getParentFolder(resourcename);
3450: parent = CmsFileUtil.removeTrailingSeparator(parent);
3451:
3452: ResultSet res = null;
3453: PreparedStatement stmt = null;
3454: Connection conn = null;
3455: String parentId = null;
3456:
3457: try {
3458: conn = m_sqlManager.getConnection(dbc);
3459: stmt = m_sqlManager.getPreparedStatement(conn, projectId,
3460: "C_RESOURCES_READ_PARENT_STRUCTURE_ID");
3461: stmt.setString(1, parent);
3462: res = stmt.executeQuery();
3463:
3464: if (res.next()) {
3465: parentId = res.getString(1);
3466: while (res.next()) {
3467: // do nothing only move through all rows because of mssql odbc driver
3468: }
3469: } else {
3470: throw new CmsVfsResourceNotFoundException(Messages
3471: .get().container(Messages.ERR_READ_PARENT_ID_1,
3472: dbc.removeSiteRoot(resourcename)));
3473: }
3474: } catch (SQLException e) {
3475: throw new CmsDbSqlException(Messages.get().container(
3476: Messages.ERR_GENERIC_SQL_1,
3477: CmsDbSqlException.getErrorQuery(stmt)), e);
3478: } finally {
3479: m_sqlManager.closeAll(dbc, conn, stmt, res);
3480: }
3481:
3482: return parentId;
3483: }
3484:
3485: /**
3486: * Creates a new {@link CmsRelation} object from the given result set entry.<p>
3487: *
3488: * @param res the result set
3489: *
3490: * @return the new {@link CmsRelation} object
3491: *
3492: * @throws SQLException if something goes wrong
3493: */
3494: protected CmsRelation internalReadRelation(ResultSet res)
3495: throws SQLException {
3496:
3497: CmsUUID sourceId = new CmsUUID(res.getString(m_sqlManager
3498: .readQuery("C_RELATION_SOURCE_ID")));
3499: String sourcePath = res.getString(m_sqlManager
3500: .readQuery("C_RELATION_SOURCE_PATH"));
3501: CmsUUID targetId = new CmsUUID(res.getString(m_sqlManager
3502: .readQuery("C_RELATION_TARGET_ID")));
3503: String targetPath = res.getString(m_sqlManager
3504: .readQuery("C_RELATION_TARGET_PATH"));
3505: int type = res
3506: .getInt(m_sqlManager.readQuery("C_RELATION_TYPE"));
3507: return new CmsRelation(sourceId, sourcePath, targetId,
3508: targetPath, CmsRelationType.valueOf(type));
3509: }
3510:
3511: /**
3512: * Returns the resource state of the given resource.<p>
3513: *
3514: * @param dbc the database context
3515: * @param projectId the id of the project
3516: * @param resource the resource to read the resource state for
3517: *
3518: * @return the resource state of the given resource
3519: *
3520: * @throws CmsDbSqlException if something goes wrong
3521: */
3522: protected CmsResourceState internalReadResourceState(
3523: CmsDbContext dbc, CmsUUID projectId, CmsResource resource)
3524: throws CmsDbSqlException {
3525:
3526: CmsResourceState state = CmsResource.STATE_KEEP;
3527:
3528: Connection conn = null;
3529: PreparedStatement stmt = null;
3530: ResultSet res = null;
3531: try {
3532: conn = m_sqlManager.getConnection(dbc);
3533: stmt = m_sqlManager.getPreparedStatement(conn, projectId,
3534: "C_READ_RESOURCE_STATE");
3535: stmt.setString(1, resource.getResourceId().toString());
3536: res = stmt.executeQuery();
3537: if (res.next()) {
3538: state = CmsResourceState.valueOf(res
3539: .getInt(m_sqlManager
3540: .readQuery("C_RESOURCES_STATE")));
3541: while (res.next()) {
3542: // do nothing only move through all rows because of mssql odbc driver
3543: }
3544: }
3545: } catch (SQLException e) {
3546: throw new CmsDbSqlException(Messages.get().container(
3547: Messages.ERR_GENERIC_SQL_1,
3548: CmsDbSqlException.getErrorQuery(stmt)), e);
3549: } finally {
3550: m_sqlManager.closeAll(dbc, conn, stmt, res);
3551: }
3552: return state;
3553: }
3554:
3555: /**
3556: * Returns the structure state of the given resource.<p>
3557: *
3558: * @param dbc the database context
3559: * @param projectId the id of the project
3560: * @param resource the resource to read the structure state for
3561: *
3562: * @return the structure state of the given resource
3563: *
3564: * @throws CmsDbSqlException if something goes wrong
3565: */
3566: protected CmsResourceState internalReadStructureState(
3567: CmsDbContext dbc, CmsUUID projectId, CmsResource resource)
3568: throws CmsDbSqlException {
3569:
3570: CmsResourceState state = CmsResource.STATE_KEEP;
3571:
3572: Connection conn = null;
3573: PreparedStatement stmt = null;
3574: ResultSet res = null;
3575: try {
3576: conn = m_sqlManager.getConnection(dbc);
3577: stmt = m_sqlManager.getPreparedStatement(conn, projectId,
3578: "C_READ_STRUCTURE_STATE");
3579: stmt.setString(1, resource.getStructureId().toString());
3580: res = stmt.executeQuery();
3581: if (res.next()) {
3582: state = CmsResourceState
3583: .valueOf(res
3584: .getInt(m_sqlManager
3585: .readQuery("C_RESOURCES_STRUCTURE_STATE")));
3586: while (res.next()) {
3587: // do nothing only move through all rows because of mssql odbc driver
3588: }
3589: }
3590: } catch (SQLException e) {
3591: throw new CmsDbSqlException(Messages.get().container(
3592: Messages.ERR_GENERIC_SQL_1,
3593: CmsDbSqlException.getErrorQuery(stmt)), e);
3594: } finally {
3595: m_sqlManager.closeAll(dbc, conn, stmt, res);
3596: }
3597: return state;
3598: }
3599:
3600: /**
3601: * Removes a resource physically in the database.<p>
3602: *
3603: * @param dbc the current database context
3604: * @param currentProject the current project
3605: * @param resource the folder to remove
3606: *
3607: * @throws CmsDataAccessException if something goes wrong
3608: */
3609: protected void internalRemoveFolder(CmsDbContext dbc,
3610: CmsProject currentProject, CmsResource resource)
3611: throws CmsDataAccessException {
3612:
3613: PreparedStatement stmt = null;
3614: Connection conn = null;
3615:
3616: try {
3617: conn = m_sqlManager.getConnection(dbc);
3618:
3619: // delete the structure record
3620: stmt = m_sqlManager
3621: .getPreparedStatement(conn, currentProject,
3622: "C_STRUCTURE_DELETE_BY_STRUCTUREID");
3623: stmt.setString(1, resource.getStructureId().toString());
3624: stmt.executeUpdate();
3625:
3626: m_sqlManager.closeAll(dbc, null, stmt, null);
3627:
3628: // delete the resource record
3629: stmt = m_sqlManager.getPreparedStatement(conn,
3630: currentProject, "C_RESOURCES_DELETE_BY_RESOURCEID");
3631: stmt.setString(1, resource.getResourceId().toString());
3632: stmt.executeUpdate();
3633: } catch (SQLException e) {
3634: throw new CmsDbSqlException(Messages.get().container(
3635: Messages.ERR_GENERIC_SQL_1,
3636: CmsDbSqlException.getErrorQuery(stmt)), e);
3637: } finally {
3638: m_sqlManager.closeAll(dbc, conn, stmt, null);
3639: }
3640: }
3641:
3642: /**
3643: * Updates the offline version numbers.<p>
3644: *
3645: * @param dbc the current database context
3646: * @param resource the resource to update the version number for
3647: *
3648: * @throws CmsDataAccessException if something goes wrong
3649: */
3650: protected void internalUpdateVersions(CmsDbContext dbc,
3651: CmsResource resource) throws CmsDataAccessException {
3652:
3653: if (dbc.getRequestContext() == null) {
3654: // no needed during initialization
3655: return;
3656: }
3657: if (dbc.currentProject().isOnlineProject()) {
3658: // this method is supposed to be used only in the offline project
3659: return;
3660: }
3661:
3662: // read the online version numbers
3663: Map onlineVersions = readVersions(dbc,
3664: CmsProject.ONLINE_PROJECT_ID, resource.getResourceId(),
3665: resource.getStructureId());
3666: int onlineStructureVersion = ((Integer) onlineVersions
3667: .get("structure")).intValue();
3668: int onlineResourceVersion = ((Integer) onlineVersions
3669: .get("resource")).intValue();
3670:
3671: Connection conn = null;
3672: PreparedStatement stmt = null;
3673: ResultSet res = null;
3674:
3675: try {
3676: conn = m_sqlManager.getConnection(dbc);
3677:
3678: // update the resource version
3679: stmt = m_sqlManager.getPreparedStatement(conn, dbc
3680: .currentProject(),
3681: "C_RESOURCES_UPDATE_RESOURCE_VERSION");
3682: stmt.setInt(1, onlineResourceVersion);
3683: stmt.setString(2, resource.getResourceId().toString());
3684: stmt.executeUpdate();
3685: m_sqlManager.closeAll(dbc, null, stmt, null);
3686:
3687: // update the structure version
3688: stmt = m_sqlManager.getPreparedStatement(conn, dbc
3689: .currentProject(),
3690: "C_RESOURCES_UPDATE_STRUCTURE_VERSION");
3691: stmt.setInt(1, onlineStructureVersion);
3692: stmt.setString(2, resource.getStructureId().toString());
3693: stmt.executeUpdate();
3694: m_sqlManager.closeAll(dbc, null, stmt, null);
3695:
3696: } catch (SQLException e) {
3697: throw new CmsDbSqlException(Messages.get().container(
3698: Messages.ERR_GENERIC_SQL_1,
3699: CmsDbSqlException.getErrorQuery(stmt)), e);
3700: } finally {
3701: m_sqlManager.closeAll(dbc, conn, stmt, res);
3702: }
3703: }
3704:
3705: /**
3706: * Validates that the length setting of a resource is always correct.<p>
3707: *
3708: * Files need to have a resource length of >= 0, while folders require
3709: * a resource length of -1.<p>
3710: *
3711: * @param resource the resource to check the length for
3712: * @throws CmsDataAccessException if the resource length is not correct
3713: */
3714: protected void internalValidateResourceLength(CmsResource resource)
3715: throws CmsDataAccessException {
3716:
3717: if (resource.isFolder() && (resource.getLength() == -1)) {
3718: return;
3719: }
3720:
3721: if (resource.isFile() && (resource.getLength() >= 0)) {
3722: return;
3723: }
3724:
3725: throw new CmsDataAccessException(Messages.get().container(
3726: Messages.ERR_INVALID_RESOURCE_LENGTH_2,
3727: new Integer(resource.getLength()),
3728: resource.getRootPath()));
3729: }
3730:
3731: /**
3732: * Moves all relations of a resource to the new path.<p>
3733: *
3734: * @param dbc the current database context
3735: * @param projectId the id of the project to apply the changes
3736: * @param structureId the structure id of the resource to apply the changes to
3737: * @param rootPath the new root path
3738: *
3739: * @throws CmsDataAccessException if something goes wrong
3740: */
3741: protected void moveRelations(CmsDbContext dbc, CmsUUID projectId,
3742: CmsUUID structureId, String rootPath)
3743: throws CmsDataAccessException {
3744:
3745: Connection conn = null;
3746: PreparedStatement stmt = null;
3747:
3748: try {
3749: conn = m_sqlManager.getConnection(dbc);
3750: stmt = m_sqlManager.getPreparedStatement(conn, projectId,
3751: "C_MOVE_RELATIONS_SOURCE");
3752: stmt.setString(1, rootPath);
3753: stmt.setString(2, structureId.toString());
3754:
3755: stmt.executeUpdate();
3756: m_sqlManager.closeAll(dbc, null, stmt, null);
3757:
3758: stmt = m_sqlManager.getPreparedStatement(conn, projectId,
3759: "C_MOVE_RELATIONS_TARGET");
3760: stmt.setString(1, rootPath);
3761: stmt.setString(2, structureId.toString());
3762:
3763: stmt.executeUpdate();
3764: } catch (SQLException e) {
3765: throw new CmsDbSqlException(Messages.get().container(
3766: Messages.ERR_GENERIC_SQL_1,
3767: CmsDbSqlException.getErrorQuery(stmt)), e);
3768: } finally {
3769: m_sqlManager.closeAll(dbc, conn, stmt, null);
3770: }
3771: }
3772:
3773: /**
3774: * Appends the appropriate selection criteria related with the expiration date.<p>
3775: *
3776: * @param projectId the id of the project of the resources
3777: * @param startTime the start time
3778: * @param endTime the end time
3779: * @param conditions buffer to append the selection criteria
3780: * @param params list to append the selection parameters
3781: */
3782: protected void prepareExpiredTimeRangeCondition(CmsUUID projectId,
3783: long startTime, long endTime, StringBuffer conditions,
3784: List params) {
3785:
3786: if (startTime > 0L) {
3787: // READ_IGNORE_TIME: if NOT set, add condition to match expired date against startTime
3788: conditions.append(BEGIN_INCLUDE_CONDITION);
3789: conditions.append(m_sqlManager.readQuery(projectId,
3790: "C_STRUCTURE_SELECT_BY_DATE_EXPIRED_AFTER"));
3791: conditions.append(END_CONDITION);
3792: params.add(new Long(startTime));
3793: }
3794:
3795: if (endTime > 0L) {
3796: // READ_IGNORE_TIME: if NOT set, add condition to match expired date against endTime
3797: conditions.append(BEGIN_INCLUDE_CONDITION);
3798: conditions.append(m_sqlManager.readQuery(projectId,
3799: "C_STRUCTURE_SELECT_BY_DATE_EXPIRED_BEFORE"));
3800: conditions.append(END_CONDITION);
3801: params.add(new Long(endTime));
3802: }
3803: }
3804:
3805: /**
3806: * Appends the appropriate selection criteria related with the parentPath.<p>
3807: *
3808: * @param projectId the id of the project of the resources
3809: * @param parent the parent path or UUID (if mode is C_READMODE_EXCLUDE_TREE)
3810: * @param mode the selection mode
3811: * @param conditions buffer to append the selection criteria
3812: * @param params list to append the selection parameters
3813: */
3814: protected void preparePathCondition(CmsUUID projectId,
3815: String parent, int mode, StringBuffer conditions,
3816: List params) {
3817:
3818: if (parent == CmsDriverManager.READ_IGNORE_PARENT) {
3819: // parent can be ignored
3820: return;
3821: }
3822:
3823: if ((mode & CmsDriverManager.READMODE_EXCLUDE_TREE) > 0) {
3824: // only return immediate children - use UUID optimization
3825: conditions.append(BEGIN_INCLUDE_CONDITION);
3826: conditions.append(m_sqlManager.readQuery(projectId,
3827: "C_RESOURCES_SELECT_BY_PARENT_UUID"));
3828: conditions.append(END_CONDITION);
3829: params.add(parent);
3830: return;
3831: }
3832:
3833: if ("/".equalsIgnoreCase(parent)) {
3834: // if root folder is parent, no additional condition is needed since all resources match anyway
3835: return;
3836: }
3837:
3838: // add condition to read path subtree
3839: conditions.append(BEGIN_INCLUDE_CONDITION);
3840: conditions.append(m_sqlManager.readQuery(projectId,
3841: "C_RESOURCES_SELECT_BY_PATH_PREFIX"));
3842: conditions.append(END_CONDITION);
3843: params.add(CmsFileUtil
3844: .addTrailingSeparator(escapeDbWildcard(parent))
3845: + "%");
3846: }
3847:
3848: /**
3849: * Appends the appropriate selection criteria related with the projectId.<p>
3850: *
3851: * @param projectId the id of the project of the resources
3852: * @param mode the selection mode
3853: * @param conditions buffer to append the selection criteria
3854: * @param params list to append the selection parameters
3855: */
3856: protected void prepareProjectCondition(CmsUUID projectId, int mode,
3857: StringBuffer conditions, List params) {
3858:
3859: if ((mode & CmsDriverManager.READMODE_INCLUDE_PROJECT) > 0) {
3860: // C_READMODE_INCLUDE_PROJECT: add condition to match the PROJECT_ID
3861: conditions.append(BEGIN_INCLUDE_CONDITION);
3862: conditions.append(m_sqlManager.readQuery(projectId,
3863: "C_RESOURCES_SELECT_BY_PROJECT_LASTMODIFIED"));
3864: conditions.append(END_CONDITION);
3865: params.add(projectId.toString());
3866: }
3867: }
3868:
3869: /**
3870: * Build the whole WHERE sql statement part for the given relation filter.<p>
3871: *
3872: * @param projectId the current project id
3873: * @param filter the filter
3874: * @param resource the resource (may be null, if you want to delete all relations for the resource in the filter)
3875: * @param params the parameter values (return parameter)
3876: * @param checkSource if the query is for the source relations
3877: *
3878: * @return the WHERE sql statement part string
3879: */
3880: protected String prepareRelationConditions(CmsUUID projectId,
3881: CmsRelationFilter filter, CmsResource resource,
3882: List params, boolean checkSource) {
3883:
3884: StringBuffer conditions = new StringBuffer(128);
3885: params.clear(); // be sure the parameters list is clear
3886:
3887: // source or target filter
3888: if (filter.isSource() || filter.isTarget()) {
3889: // source or target id filter from resource
3890: if (resource != null) {
3891: conditions.append(BEGIN_CONDITION);
3892: if (filter.isSource() && checkSource) {
3893: if (!filter.isIncludeSubresources()) {
3894: conditions.append(m_sqlManager.readQuery(
3895: projectId,
3896: "C_RELATION_FILTER_TARGET_ID"));
3897: params
3898: .add(resource.getStructureId()
3899: .toString());
3900: } else {
3901: conditions.append(m_sqlManager.readQuery(
3902: projectId,
3903: "C_RELATION_FILTER_TARGET_PATH"));
3904: params.add(resource.getRootPath() + '%');
3905: }
3906: } else if (filter.isTarget() && !checkSource) {
3907: if (!filter.isIncludeSubresources()) {
3908: conditions.append(m_sqlManager.readQuery(
3909: projectId,
3910: "C_RELATION_FILTER_SOURCE_ID"));
3911: params
3912: .add(resource.getStructureId()
3913: .toString());
3914: } else {
3915: conditions.append(m_sqlManager.readQuery(
3916: projectId,
3917: "C_RELATION_FILTER_SOURCE_PATH"));
3918: params.add(resource.getRootPath() + '%');
3919: }
3920: }
3921: conditions.append(END_CONDITION);
3922: }
3923:
3924: // target or source id filter from filter parameter
3925: if ((filter.getStructureId() != null)
3926: && (!filter.getStructureId().isNullUUID())) {
3927: if (conditions.length() == 0) {
3928: conditions.append(BEGIN_CONDITION);
3929: } else {
3930: conditions.append(BEGIN_INCLUDE_CONDITION);
3931: }
3932:
3933: if (filter.isSource() && checkSource) {
3934: conditions.append(m_sqlManager.readQuery(projectId,
3935: "C_RELATION_FILTER_SOURCE_ID"));
3936: params.add(filter.getStructureId().toString());
3937: } else if (filter.isTarget() && !checkSource) {
3938: conditions.append(m_sqlManager.readQuery(projectId,
3939: "C_RELATION_FILTER_TARGET_ID"));
3940: params.add(filter.getStructureId().toString());
3941: }
3942: conditions.append(END_CONDITION);
3943: }
3944:
3945: // target or source path filter from filter parameter
3946: if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(filter
3947: .getPath())) {
3948: if (conditions.length() == 0) {
3949: conditions.append(BEGIN_CONDITION);
3950: } else {
3951: conditions.append(BEGIN_INCLUDE_CONDITION);
3952: }
3953:
3954: String queryPath = filter.getPath();
3955: if (filter.isIncludeSubresources()) {
3956: queryPath += '%';
3957: }
3958: if (filter.isSource() && checkSource) {
3959: conditions.append(m_sqlManager.readQuery(projectId,
3960: "C_RELATION_FILTER_SOURCE_PATH"));
3961: params.add(queryPath);
3962: } else if (filter.isTarget() && !checkSource) {
3963: conditions.append(m_sqlManager.readQuery(projectId,
3964: "C_RELATION_FILTER_TARGET_PATH"));
3965: params.add(queryPath);
3966: }
3967: conditions.append(END_CONDITION);
3968: }
3969: }
3970:
3971: // relation type filter
3972: Set types = filter.getTypes();
3973: if (!types.isEmpty()) {
3974: if (conditions.length() == 0) {
3975: conditions.append(BEGIN_CONDITION);
3976: } else {
3977: conditions.append(BEGIN_INCLUDE_CONDITION);
3978: }
3979: conditions.append(m_sqlManager.readQuery(projectId,
3980: "C_RELATION_FILTER_TYPE"));
3981: conditions.append(BEGIN_CONDITION);
3982: Iterator it = types.iterator();
3983: while (it.hasNext()) {
3984: CmsRelationType type = (CmsRelationType) it.next();
3985: conditions.append("?");
3986: params.add(new Integer(type.getId()));
3987: if (it.hasNext()) {
3988: conditions.append(", ");
3989: }
3990: }
3991: conditions.append(END_CONDITION);
3992: conditions.append(END_CONDITION);
3993: }
3994: return conditions.toString();
3995: }
3996:
3997: /**
3998: * Appends the appropriate selection criteria related with the released date.<p>
3999: *
4000: * @param projectId the id of the project
4001: * @param startTime the start time
4002: * @param endTime the stop time
4003: * @param conditions buffer to append the selection criteria
4004: * @param params list to append the selection parameters
4005: */
4006: protected void prepareReleasedTimeRangeCondition(CmsUUID projectId,
4007: long startTime, long endTime, StringBuffer conditions,
4008: List params) {
4009:
4010: if (startTime > 0L) {
4011: // READ_IGNORE_TIME: if NOT set, add condition to match released date against startTime
4012: conditions.append(BEGIN_INCLUDE_CONDITION);
4013: conditions.append(m_sqlManager.readQuery(projectId,
4014: "C_STRUCTURE_SELECT_BY_DATE_RELEASED_AFTER"));
4015: conditions.append(END_CONDITION);
4016: params.add(new Long(startTime));
4017: }
4018:
4019: if (endTime > 0L) {
4020: // READ_IGNORE_TIME: if NOT set, add condition to match released date against endTime
4021: conditions.append(BEGIN_INCLUDE_CONDITION);
4022: conditions.append(m_sqlManager.readQuery(projectId,
4023: "C_STRUCTURE_SELECT_BY_DATE_RELEASED_BEFORE"));
4024: conditions.append(END_CONDITION);
4025: params.add(new Long(endTime));
4026: }
4027: }
4028:
4029: /**
4030: * Appends the appropriate selection criteria related with the read mode.<p>
4031: *
4032: * @param projectId the id of the project of the resources
4033: * @param mode the selection mode
4034: * @param conditions buffer to append the selection criteria
4035: */
4036: protected void prepareResourceCondition(CmsUUID projectId,
4037: int mode, StringBuffer conditions) {
4038:
4039: if ((mode & CmsDriverManager.READMODE_ONLY_FOLDERS) > 0) {
4040: // C_READMODE_ONLY_FOLDERS: add condition to match only folders
4041: conditions.append(BEGIN_INCLUDE_CONDITION);
4042: conditions.append(m_sqlManager.readQuery(projectId,
4043: "C_RESOURCES_SELECT_ONLY_FOLDERS"));
4044: conditions.append(END_CONDITION);
4045: } else if ((mode & CmsDriverManager.READMODE_ONLY_FILES) > 0) {
4046: // C_READMODE_ONLY_FILES: add condition to match only files
4047: conditions.append(BEGIN_INCLUDE_CONDITION);
4048: conditions.append(m_sqlManager.readQuery(projectId,
4049: "C_RESOURCES_SELECT_ONLY_FILES"));
4050: conditions.append(END_CONDITION);
4051: }
4052: }
4053:
4054: /**
4055: * Appends the appropriate selection criteria related with the resource state.<p>
4056: *
4057: * @param projectId the id of the project of the resources
4058: * @param state the resource state
4059: * @param mode the selection mode
4060: * @param conditions buffer to append the selection criteria
4061: * @param params list to append the selection parameters
4062: */
4063: protected void prepareStateCondition(CmsUUID projectId,
4064: CmsResourceState state, int mode, StringBuffer conditions,
4065: List params) {
4066:
4067: if (state != null) {
4068: if ((mode & CmsDriverManager.READMODE_EXCLUDE_STATE) > 0) {
4069: // C_READ_MODIFIED_STATES: add condition to match against any state but not given state
4070: conditions.append(BEGIN_EXCLUDE_CONDITION);
4071: } else {
4072: // otherwise add condition to match against given state if necessary
4073: conditions.append(BEGIN_INCLUDE_CONDITION);
4074: }
4075: conditions.append(m_sqlManager.readQuery(projectId,
4076: "C_RESOURCES_SELECT_BY_RESOURCE_STATE"));
4077: conditions.append(END_CONDITION);
4078: params.add(new Integer(state.getState()));
4079: params.add(new Integer(state.getState()));
4080: }
4081: }
4082:
4083: /**
4084: * Appends the appropriate selection criteria related with the date of the last modification.<p>
4085: *
4086: * @param projectId the id of the project of the resources
4087: * @param startTime start of the time range
4088: * @param endTime end of the time range
4089: * @param conditions buffer to append the selection criteria
4090: * @param params list to append the selection parameters
4091: */
4092: protected void prepareTimeRangeCondition(CmsUUID projectId,
4093: long startTime, long endTime, StringBuffer conditions,
4094: List params) {
4095:
4096: if (startTime > 0L) {
4097: // READ_IGNORE_TIME: if NOT set, add condition to match last modified date against startTime
4098: conditions.append(BEGIN_INCLUDE_CONDITION);
4099: conditions.append(m_sqlManager.readQuery(projectId,
4100: "C_RESOURCES_SELECT_BY_DATE_LASTMODIFIED_AFTER"));
4101: conditions.append(END_CONDITION);
4102: params.add(new Long(startTime));
4103: }
4104:
4105: if (endTime > 0L) {
4106: // READ_IGNORE_TIME: if NOT set, add condition to match last modified date against endTime
4107: conditions.append(BEGIN_INCLUDE_CONDITION);
4108: conditions.append(m_sqlManager.readQuery(projectId,
4109: "C_RESOURCES_SELECT_BY_DATE_LASTMODIFIED_BEFORE"));
4110: conditions.append(END_CONDITION);
4111: params.add(new Long(endTime));
4112: }
4113: }
4114:
4115: /**
4116: * Appends the appropriate selection criteria related with the resource type.<p>
4117: *
4118: * @param projectId the id of the project of the resources
4119: * @param type the resource type
4120: * @param mode the selection mode
4121: * @param conditions buffer to append the selection criteria
4122: * @param params list to append the selection parameters
4123: */
4124: protected void prepareTypeCondition(CmsUUID projectId, int type,
4125: int mode, StringBuffer conditions, List params) {
4126:
4127: if (type != CmsDriverManager.READ_IGNORE_TYPE) {
4128: if ((mode & CmsDriverManager.READMODE_EXCLUDE_TYPE) > 0) {
4129: // C_READ_FILE_TYPES: add condition to match against any type, but not given type
4130: conditions.append(BEGIN_EXCLUDE_CONDITION);
4131: conditions.append(m_sqlManager.readQuery(projectId,
4132: "C_RESOURCES_SELECT_BY_RESOURCE_TYPE"));
4133: conditions.append(END_CONDITION);
4134: params.add(new Integer(type));
4135: } else {
4136: //otherwise add condition to match against given type if necessary
4137: conditions.append(BEGIN_INCLUDE_CONDITION);
4138: conditions.append(m_sqlManager.readQuery(projectId,
4139: "C_RESOURCES_SELECT_BY_RESOURCE_TYPE"));
4140: conditions.append(END_CONDITION);
4141: params.add(new Integer(type));
4142: }
4143: }
4144: }
4145:
4146: /**
4147: * Repairs broken links.<p>
4148: *
4149: * When a resource is created any relation pointing to it is updated to use the right id.<p>
4150: *
4151: * @param dbc the current database context
4152: * @param projectId the project id
4153: * @param structureId the structure id of the resource that may help to repair broken links
4154: * @param rootPath the path of the resource that may help to repair broken links
4155: *
4156: * @throws CmsDataAccessException if something goes wrong
4157: */
4158: protected void repairBrokenRelations(CmsDbContext dbc,
4159: CmsUUID projectId, CmsUUID structureId, String rootPath)
4160: throws CmsDataAccessException {
4161:
4162: PreparedStatement stmt = null;
4163: Connection conn = null;
4164:
4165: try {
4166: conn = m_sqlManager.getConnection(dbc);
4167: stmt = m_sqlManager.getPreparedStatement(conn, projectId,
4168: "C_RELATIONS_REPAIR_BROKEN");
4169: stmt.setString(1, structureId.toString());
4170: stmt.setString(2, rootPath);
4171: stmt.executeUpdate();
4172: } catch (SQLException e) {
4173: throw new CmsDbSqlException(Messages.get().container(
4174: Messages.ERR_GENERIC_SQL_1,
4175: CmsDbSqlException.getErrorQuery(stmt)), e);
4176: } finally {
4177: m_sqlManager.closeAll(dbc, conn, stmt, null);
4178: }
4179: }
4180:
4181: /**
4182: * Updates broken links.<p>
4183: *
4184: * When a resource is deleted, then the relations pointing to
4185: * the deleted resource are set to the null uuid.<p>
4186: *
4187: * @param dbc the current database context
4188: * @param projectId the project id
4189: * @param rootPath the root path of the resource that has been deleted
4190: *
4191: * @throws CmsDataAccessException if something goes wrong
4192: */
4193: protected void updateBrokenRelations(CmsDbContext dbc,
4194: CmsUUID projectId, String rootPath)
4195: throws CmsDataAccessException {
4196:
4197: PreparedStatement stmt = null;
4198: Connection conn = null;
4199:
4200: try {
4201: conn = m_sqlManager.getConnection(dbc);
4202: stmt = m_sqlManager.getPreparedStatement(conn, projectId,
4203: "C_RELATIONS_UPDATE_BROKEN");
4204: stmt.setString(1, rootPath);
4205: stmt.executeUpdate();
4206: } catch (SQLException e) {
4207: throw new CmsDbSqlException(Messages.get().container(
4208: Messages.ERR_GENERIC_SQL_1,
4209: CmsDbSqlException.getErrorQuery(stmt)), e);
4210: } finally {
4211: m_sqlManager.closeAll(dbc, conn, stmt, null);
4212: }
4213: }
4214: }
|