001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: DatabaseContentStore.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.cmf.dam.contentstores;
009:
010: import com.uwyn.rife.database.*;
011:
012: import com.uwyn.rife.cmf.MimeType;
013: import com.uwyn.rife.cmf.dam.ContentStore;
014: import com.uwyn.rife.cmf.dam.contentstores.exceptions.DeleteContentDataErrorException;
015: import com.uwyn.rife.cmf.dam.contentstores.exceptions.HasContentDataErrorException;
016: import com.uwyn.rife.cmf.dam.contentstores.exceptions.InstallContentStoreErrorException;
017: import com.uwyn.rife.cmf.dam.contentstores.exceptions.RemoveContentStoreErrorException;
018: import com.uwyn.rife.cmf.dam.contentstores.exceptions.RetrieveSizeErrorException;
019: import com.uwyn.rife.cmf.dam.exceptions.ContentManagerException;
020: import com.uwyn.rife.database.exceptions.DatabaseException;
021: import com.uwyn.rife.database.queries.CreateTable;
022: import com.uwyn.rife.database.queries.Delete;
023: import com.uwyn.rife.database.queries.DropTable;
024: import com.uwyn.rife.database.queries.Select;
025: import com.uwyn.rife.engine.ElementSupport;
026: import com.uwyn.rife.tools.ExceptionUtils;
027: import com.uwyn.rife.tools.InnerClassException;
028: import java.io.OutputStream;
029: import java.sql.ResultSet;
030: import java.sql.SQLException;
031: import java.util.ArrayList;
032: import java.util.Collection;
033: import java.util.logging.Logger;
034: import javax.servlet.http.HttpServletResponse;
035:
036: public abstract class DatabaseContentStore extends DbQueryManager
037: implements ContentStore {
038: private ArrayList<MimeType> mMimeTypes = new ArrayList<MimeType>();
039:
040: public DatabaseContentStore(Datasource datasource) {
041: super (datasource);
042: }
043:
044: protected void addMimeType(MimeType mimeType) {
045: mMimeTypes.add(mimeType);
046: }
047:
048: public Collection<MimeType> getSupportedMimeTypes() {
049: return mMimeTypes;
050: }
051:
052: protected boolean _install(CreateTable createTableContentStore)
053: throws ContentManagerException {
054: assert createTableContentStore != null;
055:
056: try {
057: executeUpdate(createTableContentStore);
058: } catch (DatabaseException e) {
059: throw new InstallContentStoreErrorException(e);
060: }
061:
062: return true;
063: }
064:
065: protected boolean _remove(DropTable dropTableContentStore)
066: throws ContentManagerException {
067: assert dropTableContentStore != null;
068:
069: try {
070: executeUpdate(dropTableContentStore);
071: } catch (DatabaseException e) {
072: throw new RemoveContentStoreErrorException(e);
073: }
074:
075: return true;
076: }
077:
078: protected boolean _deleteContentData(
079: final Delete deleteContentData, final int id)
080: throws ContentManagerException {
081: if (id < 0)
082: throw new IllegalArgumentException("id must be positive");
083:
084: assert deleteContentData != null;
085:
086: Boolean result = null;
087:
088: try {
089: result = inTransaction(new DbTransactionUser() {
090: public Boolean useTransaction()
091: throws InnerClassException {
092: return 0 != executeUpdate(deleteContentData,
093: new DbPreparedStatementHandler() {
094: public void setParameters(
095: DbPreparedStatement statement) {
096: statement.setInt("contentId", id);
097: }
098: });
099:
100: }
101: });
102: } catch (DatabaseException e) {
103: throw new DeleteContentDataErrorException(id, e);
104: }
105:
106: return result != null && result.booleanValue();
107: }
108:
109: protected int _getSize(Select retrieveSize, final int id)
110: throws ContentManagerException {
111: if (id < 0)
112: throw new IllegalArgumentException("id must be positive");
113:
114: assert retrieveSize != null;
115:
116: try {
117: return executeGetFirstInt(retrieveSize,
118: new DbPreparedStatementHandler() {
119: public void setParameters(
120: DbPreparedStatement statement) {
121: statement.setInt("contentId", id);
122: }
123: });
124: } catch (DatabaseException e) {
125: throw new RetrieveSizeErrorException(id, e);
126: }
127: }
128:
129: protected boolean _hasContentData(Select hasContentData,
130: final int id) throws ContentManagerException {
131: if (id < 0)
132: throw new IllegalArgumentException("id must be positive");
133:
134: assert hasContentData != null;
135:
136: try {
137: return executeHasResultRows(hasContentData,
138: new DbPreparedStatementHandler() {
139: public void setParameters(
140: DbPreparedStatement statement) {
141: statement.setInt("contentId", id);
142: }
143: });
144: } catch (DatabaseException e) {
145: throw new HasContentDataErrorException(id, e);
146: }
147: }
148:
149: protected String getContentSizeColumnName() {
150: return "size";
151: }
152:
153: protected void _serveContentData(Select retrieveContent,
154: final ElementSupport element, final int id)
155: throws ContentManagerException {
156: if (null == element)
157: throw new IllegalArgumentException("element can't be null");
158:
159: if (id < 0) {
160: element.defer();
161: return;
162: }
163:
164: assert retrieveContent != null;
165:
166: try {
167: if (!executeFetchFirst(retrieveContent,
168: new DbRowProcessor() {
169: public boolean processRow(ResultSet resultSet)
170: throws SQLException {
171: // set the content length header
172: element
173: .setContentLength(resultSet
174: .getInt(getContentSizeColumnName()));
175:
176: // output the content
177: OutputStream os = element.getOutputStream();
178: outputContentColumn(resultSet, os);
179:
180: return true;
181: }
182: }, new DbPreparedStatementHandler() {
183: public void setParameters(
184: DbPreparedStatement statement) {
185: statement.setInt("contentId", id);
186: }
187: })) {
188: element.defer();
189: return;
190: }
191: } catch (DatabaseException e) {
192: Logger.getLogger("com.uwyn.rife.cmf").severe(
193: ExceptionUtils.getExceptionStackTrace(e));
194: element
195: .setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
196: }
197: }
198:
199: protected abstract void outputContentColumn(ResultSet resultSet,
200: OutputStream os) throws SQLException;
201: }
|