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: generic.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.cmf.dam.contentmanagers.databasedrivers;
009:
010: import com.uwyn.rife.database.queries.*;
011:
012: import com.uwyn.rife.cmf.Content;
013: import com.uwyn.rife.cmf.ContentRepository;
014: import com.uwyn.rife.cmf.dam.ContentDataUser;
015: import com.uwyn.rife.cmf.dam.contentmanagers.DatabaseContent;
016: import com.uwyn.rife.cmf.dam.contentmanagers.DatabaseContentInfo;
017: import com.uwyn.rife.cmf.dam.contentmanagers.exceptions.InstallContentErrorException;
018: import com.uwyn.rife.cmf.dam.contentmanagers.exceptions.RemoveContentErrorException;
019: import com.uwyn.rife.cmf.dam.exceptions.ContentManagerException;
020: import com.uwyn.rife.cmf.transform.ContentTransformer;
021: import com.uwyn.rife.config.RifeConfig;
022: import com.uwyn.rife.database.Datasource;
023: import com.uwyn.rife.database.exceptions.DatabaseException;
024: import com.uwyn.rife.engine.ElementSupport;
025:
026: public class generic extends DatabaseContent {
027: protected CreateSequence mCreateSequenceContentRepository = null;
028: protected CreateSequence mCreateSequenceContentInfo = null;
029: protected CreateTable mCreateTableContentRepository = null;
030: protected CreateTable mCreateTableContentInfo = null;
031: protected CreateTable mCreateTableContentAttribute = null;
032: protected CreateTable mCreateTableContentProperty = null;
033: protected String mCreateContentInfoPathIndex = null;
034: protected String mCreateContentInfoPathNameIndex = null;
035: protected DropSequence mDropSequenceContentRepository = null;
036: protected DropSequence mDropSequenceContentInfo = null;
037: protected DropTable mDropTableContentRepository = null;
038: protected DropTable mDropTableContentInfo = null;
039: protected DropTable mDropTableContentAttribute = null;
040: protected DropTable mDropTableContentProperties = null;
041: protected String mDropContentInfoPathIndex = null;
042: protected String mDropContentInfoPathNameIndex = null;
043: protected SequenceValue mGetNewContentRepositoryId = null;
044: protected SequenceValue mGetNewContentId = null;
045: protected Select mGetContentRepositoryId = null;
046: protected Select mGetContentInfo = null;
047: protected Select mGetVersion = null;
048: protected Insert mStoreContentRepository = null;
049: protected Select mContainsContentRepository = null;
050: protected Insert mStoreContentInfo = null;
051: protected Insert mStoreContentAttribute = null;
052: protected Insert mStoreContentProperty = null;
053: protected Delete mDeleteContentInfo = null;
054: protected Delete mDeleteContentAttributes = null;
055: protected Delete mDeleteContentProperties = null;
056: protected Select mGetLatestContentInfo = null;
057: protected Select mGetContentAttributes = null;
058: protected Select mGetContentProperties = null;
059:
060: public generic(Datasource datasource) {
061: super (datasource);
062:
063: mCreateSequenceContentRepository = new CreateSequence(
064: getDatasource()).name(RifeConfig.Cmf
065: .getSequenceContentRepository());
066:
067: mCreateSequenceContentInfo = new CreateSequence(getDatasource())
068: .name(RifeConfig.Cmf.getSequenceContentInfo());
069:
070: mCreateTableContentRepository = new CreateTable(getDatasource())
071: .table(RifeConfig.Cmf.getTableContentRepository())
072: .columns(ContentRepository.class).column(
073: "repositoryId", int.class).primaryKey(
074: "PK_"
075: + RifeConfig.Cmf
076: .getTableContentRepository(),
077: "repositoryId");
078:
079: mCreateTableContentInfo = new CreateTable(getDatasource())
080: .table(RifeConfig.Cmf.getTableContentInfo()).columns(
081: DatabaseContentInfo.class).column(
082: "repositoryId", int.class, CreateTable.NOTNULL)
083: .defaultFunction("created", "CURRENT_TIMESTAMP")
084: .unique(
085: ("UQ_" + RifeConfig.Cmf.getTableContentInfo())
086: .toUpperCase(),
087: new String[] { "repositoryId", "path",
088: "version" }).foreignKey(
089: "FK_" + RifeConfig.Cmf.getTableContentInfo()
090: + "_REPOSITORYID",
091: RifeConfig.Cmf.getTableContentRepository(),
092: "repositoryId", "repositoryId");
093:
094: mCreateTableContentAttribute = new CreateTable(getDatasource())
095: .table(RifeConfig.Cmf.getTableContentAttribute())
096: .column("contentId", int.class, CreateTable.NOTNULL)
097: .column("name", String.class, 255, CreateTable.NOTNULL)
098: .column(getValueColumnName(), String.class, 255,
099: CreateTable.NOTNULL).foreignKey(
100: ("FK_" + RifeConfig.Cmf
101: .getTableContentAttribute())
102: .toUpperCase(),
103: RifeConfig.Cmf.getTableContentInfo(),
104: "contentId", "contentId");
105:
106: mCreateTableContentProperty = new CreateTable(getDatasource())
107: .table(RifeConfig.Cmf.getTableContentProperty())
108: .column("contentId", int.class, CreateTable.NOTNULL)
109: .column("name", String.class, 255, CreateTable.NOTNULL)
110: .column(getValueColumnName(), String.class, 255,
111: CreateTable.NOTNULL).foreignKey(
112: ("FK_" + RifeConfig.Cmf
113: .getTableContentProperty())
114: .toUpperCase(),
115: RifeConfig.Cmf.getTableContentInfo(),
116: "contentId", "contentId");
117:
118: mCreateContentInfoPathIndex = "CREATE INDEX "
119: + RifeConfig.Cmf.getTableContentInfo() + "_path ON "
120: + RifeConfig.Cmf.getTableContentInfo() + " (path)";
121: mCreateContentInfoPathNameIndex = "CREATE INDEX "
122: + RifeConfig.Cmf.getTableContentInfo()
123: + "_pathname ON "
124: + RifeConfig.Cmf.getTableContentInfo()
125: + " (path, name)";
126:
127: mDropSequenceContentRepository = new DropSequence(
128: getDatasource()).name(RifeConfig.Cmf
129: .getSequenceContentRepository());
130:
131: mDropSequenceContentInfo = new DropSequence(getDatasource())
132: .name(RifeConfig.Cmf.getSequenceContentInfo());
133:
134: mDropTableContentRepository = new DropTable(getDatasource())
135: .table(RifeConfig.Cmf.getTableContentRepository());
136:
137: mDropTableContentInfo = new DropTable(getDatasource())
138: .table(RifeConfig.Cmf.getTableContentInfo());
139:
140: mDropTableContentAttribute = new DropTable(getDatasource())
141: .table(RifeConfig.Cmf.getTableContentAttribute());
142:
143: mDropTableContentProperties = new DropTable(getDatasource())
144: .table(RifeConfig.Cmf.getTableContentProperty());
145:
146: mDropContentInfoPathIndex = "DROP INDEX "
147: + RifeConfig.Cmf.getTableContentInfo() + "_path";
148:
149: mDropContentInfoPathNameIndex = "DROP INDEX "
150: + RifeConfig.Cmf.getTableContentInfo() + "_pathname";
151:
152: mGetNewContentRepositoryId = new SequenceValue(getDatasource())
153: .name(RifeConfig.Cmf.getSequenceContentRepository())
154: .next();
155:
156: mGetNewContentId = new SequenceValue(getDatasource()).name(
157: RifeConfig.Cmf.getSequenceContentInfo()).next();
158:
159: mGetContentRepositoryId = new Select(getDatasource()).from(
160: RifeConfig.Cmf.getTableContentRepository()).field(
161: "repositoryId").whereParameter("name", "repository",
162: "=");
163:
164: mGetVersion = new Select(getDatasource()).from(
165: RifeConfig.Cmf.getTableContentInfo()).field(
166: "COALESCE(MAX(version)+1, 0)").whereParameter(
167: "repositoryId", "=").whereParameterAnd("path", "=");
168:
169: mGetContentInfo = new Select(getDatasource()).from(
170: RifeConfig.Cmf.getTableContentInfo()).join(
171: RifeConfig.Cmf.getTableContentRepository()).field(
172: RifeConfig.Cmf.getTableContentInfo() + ".*").where(
173: RifeConfig.Cmf.getTableContentInfo()
174: + ".repositoryId = "
175: + RifeConfig.Cmf.getTableContentRepository()
176: + ".repositoryId").whereParameter("path", "=")
177: .whereParameterAnd(
178: RifeConfig.Cmf.getTableContentRepository()
179: + ".name", "repository", "=").orderBy(
180: "version", Select.DESC);
181:
182: mStoreContentRepository = new Insert(getDatasource()).into(
183: RifeConfig.Cmf.getTableContentRepository())
184: .fieldsParameters(ContentRepository.class)
185: .fieldParameter("repositoryId");
186:
187: mContainsContentRepository = new Select(getDatasource()).from(
188: RifeConfig.Cmf.getTableContentRepository()).field(
189: "count(1)").whereParameter("name", "=");
190:
191: mStoreContentInfo = new Insert(getDatasource()).into(
192: RifeConfig.Cmf.getTableContentInfo()).fieldsParameters(
193: DatabaseContentInfo.class).fieldParameter(
194: "repositoryId").field("version", mGetVersion);
195:
196: mStoreContentAttribute = new Insert(getDatasource()).into(
197: RifeConfig.Cmf.getTableContentAttribute())
198: .fieldParameter("contentId").fieldParameter("name")
199: .fieldParameter(getValueColumnName());
200:
201: mStoreContentProperty = new Insert(getDatasource()).into(
202: RifeConfig.Cmf.getTableContentProperty())
203: .fieldParameter("contentId").fieldParameter("name")
204: .fieldParameter(getValueColumnName());
205:
206: mDeleteContentInfo = new Delete(getDatasource()).from(
207: RifeConfig.Cmf.getTableContentInfo()).whereParameter(
208: "contentId", "=");
209:
210: mDeleteContentAttributes = new Delete(getDatasource()).from(
211: RifeConfig.Cmf.getTableContentAttribute())
212: .whereParameter("contentId", "=");
213:
214: mDeleteContentProperties = new Delete(getDatasource()).from(
215: RifeConfig.Cmf.getTableContentProperty())
216: .whereParameter("contentId", "=");
217:
218: mGetLatestContentInfo = new Select(getDatasource()).from(
219: RifeConfig.Cmf.getTableContentInfo()).join(
220: RifeConfig.Cmf.getTableContentRepository()).field(
221: RifeConfig.Cmf.getTableContentInfo() + ".*").where(
222: RifeConfig.Cmf.getTableContentInfo()
223: + ".repositoryId = "
224: + RifeConfig.Cmf.getTableContentRepository()
225: + ".repositoryId").whereParameterAnd(
226: RifeConfig.Cmf.getTableContentRepository() + ".name",
227: "repository", "=").startWhereAnd().whereParameter(
228: "path", "=").startWhereOr().whereParameter("path",
229: "pathpart", "=").whereParameterAnd(
230: RifeConfig.Cmf.getTableContentInfo() + ".name",
231: "namepart", "=").end().end().orderBy("version",
232: Select.DESC).limit(1);
233:
234: mGetContentAttributes = new Select(getDatasource()).from(
235: RifeConfig.Cmf.getTableContentAttribute()).field(
236: "contentId").field("name").field(getValueColumnName())
237: .whereParameter("contentId", "=");
238:
239: mGetContentProperties = new Select(getDatasource()).from(
240: RifeConfig.Cmf.getTableContentProperty()).field(
241: "contentId").field("name").field(getValueColumnName())
242: .whereParameter("contentId", "=");
243: }
244:
245: public boolean install() throws ContentManagerException {
246: boolean result = _install(mCreateSequenceContentRepository,
247: mCreateSequenceContentInfo,
248: mCreateTableContentRepository, mCreateTableContentInfo,
249: mCreateTableContentAttribute,
250: mCreateTableContentProperty);
251: try {
252: executeUpdate(mCreateContentInfoPathIndex);
253: executeUpdate(mCreateContentInfoPathNameIndex);
254: } catch (DatabaseException e) {
255: throw new InstallContentErrorException(e);
256: }
257: return result;
258: }
259:
260: public boolean remove() throws ContentManagerException {
261: try {
262: executeUpdate(mDropContentInfoPathNameIndex);
263: executeUpdate(mDropContentInfoPathIndex);
264: } catch (DatabaseException e) {
265: throw new RemoveContentErrorException(e);
266: }
267: return _remove(mDropSequenceContentRepository,
268: mDropSequenceContentInfo, mDropTableContentRepository,
269: mDropTableContentInfo, mDropTableContentAttribute,
270: mDropTableContentProperties);
271: }
272:
273: public boolean createRepository(String name)
274: throws ContentManagerException {
275: return _createRepository(mGetNewContentRepositoryId,
276: mStoreContentRepository, name);
277: }
278:
279: public boolean containsRepository(String name)
280: throws ContentManagerException {
281: return _containsRepository(mContainsContentRepository, name);
282: }
283:
284: public boolean storeContent(String location, Content content,
285: ContentTransformer transformer)
286: throws ContentManagerException {
287: return _storeContent(mGetNewContentId, mGetContentRepositoryId,
288: mStoreContentInfo, mStoreContentAttribute,
289: mStoreContentProperty, location, content, transformer);
290: }
291:
292: public boolean deleteContent(String location)
293: throws ContentManagerException {
294: return _deleteContent(mGetContentInfo, mDeleteContentInfo,
295: mDeleteContentAttributes, mDeleteContentProperties,
296: location);
297: }
298:
299: public <ResultType> ResultType useContentData(String location,
300: ContentDataUser user) throws ContentManagerException {
301: return (ResultType) _useContentData(mGetLatestContentInfo,
302: location, user);
303: }
304:
305: public boolean hasContentData(String location)
306: throws ContentManagerException {
307: return _hasContentData(mGetLatestContentInfo, location);
308: }
309:
310: public void serveContentData(ElementSupport element, String location)
311: throws ContentManagerException {
312: _serveContentData(element, location);
313: }
314:
315: public DatabaseContentInfo getContentInfo(String location)
316: throws ContentManagerException {
317: return _getContentInfo(mGetLatestContentInfo,
318: mGetContentAttributes, mGetContentProperties, location);
319: }
320:
321: public String getContentForHtml(String location,
322: ElementSupport element, String serveContentExitName)
323: throws ContentManagerException {
324: return _getContentForHtml(location, element,
325: serveContentExitName);
326: }
327: }
|