001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.rm.resources.publishing;
020:
021: import java.sql.*;
022:
023: import org.openharmonise.commons.cache.*;
024: import org.openharmonise.commons.dsi.*;
025: import org.openharmonise.commons.dsi.dml.*;
026: import org.openharmonise.rm.*;
027: import org.openharmonise.rm.dsi.*;
028: import org.openharmonise.rm.factory.*;
029: import org.openharmonise.rm.resources.*;
030: import org.openharmonise.rm.resources.lifecycle.*;
031: import org.openharmonise.rm.resources.xml.*;
032:
033: /**
034: * This class represents the definition of a page in Harmonise,
035: * defining what XML page template to use and which XSLT resource to use to publish
036: * a page of content.
037: *
038: * @author Michael Bell
039: * @version $Revision: 1.2 $
040: *
041: */
042: public class WebPage extends AbstractChildObject implements Editable,
043: DataStoreObject {
044:
045: //XML constants
046: public static final String TAG_PAGE = "Page";
047: public static final String TAG_HARMONISE = "HarmonisePage";
048: public static final String TAG_PAGETITLE = "PageTitle";
049: public static final String TAG_NAVIGATION = "Navigation";
050: public static final String TAG_EXTERNAL_LINK = "ExternalLink";
051: public static final String TAG_ANCILLARY_TEXT = "AncillaryText";
052: public static final String ATTRIB_PAGEID = "page_id";
053:
054: //DB constants
055: private static final String TBL_PAGE = "page";
056: private static final String CLMN_TIMEOUT = "timeout";
057: private static final String CLMN_XML = "xml_id";
058: private static final String CLMN_XSL = "xsl_id";
059:
060: private int m_nTimeout = -1;
061: private CachePointer m_XML = null;
062: private CachePointer m_XSL = null;
063: private String m_sDocContent = "";
064:
065: /**
066: * Constructs an empty object.
067: */
068: public WebPage() {
069: super ();
070: }
071:
072: /**
073: * Constructs a new object with an interface to the DB.
074: *
075: * @param con
076: *
077: */
078: public WebPage(AbstractDataStoreInterface con) {
079: super (con);
080: }
081:
082: /**
083: * Standard constructor for a known object.
084: *
085: * @param con
086: * @param nPageId
087: *
088: */
089: public WebPage(AbstractDataStoreInterface con, int nPageId) {
090: super (con, nPageId);
091: }
092:
093: /**
094: * Standard constructor for a historical object which may be historical.
095: *
096: * @param con
097: * @param nId
098: * @param bIsHistorical
099: *
100: */
101: public WebPage(AbstractDataStoreInterface con, int nId, int nKey,
102: boolean bIsHistorical) {
103: super (con, nId, nKey, bIsHistorical);
104: }
105:
106: /**
107: * Sets the <code>XMLResource</code> which determines the content to be presented
108: * in the display of this page..
109: *
110: * @param newDoc
111: */
112: public void setXML(XMLResource xml) throws PopulateException {
113: if (isPopulated() == true && m_XML.equals(xml) == false) {
114: setIsChanged(true);
115: }
116:
117: try {
118: m_XML = CacheHandler.getInstance(m_dsi)
119: .getCachePointer(xml);
120: } catch (CacheException e) {
121: throw new PopulateException(e.getLocalizedMessage(), e);
122: }
123: }
124:
125: /**
126: * Sets the <code>XSLResource</code> which will be used to transform the output of
127: * the processing of the <code>XMLResource</code> associated with this page definition.
128: *
129: * @param newDoc
130: */
131: public void setXSL(XSLResource xsl) throws PopulateException {
132: if (isPopulated() == true && m_XSL.equals(xsl) == false) {
133: setIsChanged(true);
134: }
135:
136: try {
137: m_XSL = CacheHandler.getInstance(m_dsi)
138: .getCachePointer(xsl);
139: } catch (CacheException e) {
140: throw new PopulateException(e.getLocalizedMessage(), e);
141: }
142: }
143:
144: /**
145: * Returns the <code>XMLResource</code> which determines the content to be presented
146: * in the display of this page.
147: *
148: *
149: * @return Page XML document
150: * @throws Exception
151: */
152: public XMLResource getXML() throws DataAccessException {
153: if (isPopulated() == false) {
154: try {
155: populateFromDatabase();
156: } catch (PopulateException e) {
157: throw new DataAccessException(
158: "Error occured populating object", e);
159: }
160: }
161:
162: XMLResource xml = null;
163: try {
164: if (m_XML != null) {
165: xml = (XMLResource) m_XML.getObject();
166: }
167: } catch (CacheException e) {
168: throw new DataAccessException(e.getLocalizedMessage(), e);
169: }
170:
171: return xml;
172: }
173:
174: /**
175: * Returns the <code>XSLResource</code> that is to process the output of the processing
176: * on the <code>XMLResource</code> associated to this page definition.
177: *
178: * @return
179: * @throws Exception
180: */
181: public XSLResource getXSL() throws DataAccessException {
182: if (isPopulated() == false) {
183: try {
184: populateFromDatabase();
185: } catch (PopulateException e) {
186: throw new DataAccessException(
187: "Error occured populating object", e);
188: }
189: }
190:
191: XSLResource xsl = null;
192: try {
193: if (m_XSL != null) {
194: xsl = (XSLResource) m_XSL.getObject();
195: }
196:
197: } catch (CacheException e) {
198: throw new DataAccessException(e.getLocalizedMessage(), e);
199: }
200:
201: return xsl;
202: }
203:
204: /**
205: * Sets the timeout value associated to this page.
206: *
207: * @param nTimeout
208: */
209: public void setTimeout(int nTimeout) {
210: if (isPopulated() == true && m_nTimeout != nTimeout) {
211: setIsChanged(true);
212: }
213:
214: m_nTimeout = nTimeout;
215: }
216:
217: /**
218: * Returns the timeout value associated to this webpage.
219: *
220: * @return
221: * @throws DataAccessException
222: */
223: public int getTimeout() throws DataAccessException {
224: if (isPopulated() == false) {
225: try {
226: populateFromDatabase();
227: } catch (PopulateException e) {
228: throw new DataAccessException(
229: "Error occured populating object", e);
230: }
231: }
232:
233: return m_nTimeout;
234: }
235:
236: /* (non-Javadoc)
237: * @see org.openharmonise.rm.resources.AbstractChildObject#getParentObjectClassName()
238: */
239: public String getParentObjectClassName() {
240: return WebPageGroup.class.getName();
241: }
242:
243: /* (non-Javadoc)
244: * @see org.openharmonise.rm.resources.AbstractEditableObject#saveNonCoreData()
245: */
246: protected void saveNonCoreData() throws EditException {
247: //no non core data
248:
249: }
250:
251: /* (non-Javadoc)
252: * @see org.openharmonise.rm.dsi.DataStoreObject#getDBTableName()
253: */
254: public String getDBTableName() {
255: return TBL_PAGE;
256: }
257:
258: /* (non-Javadoc)
259: * @see org.openharmonise.rm.publishing.Publishable#getTagName()
260: */
261: public String getTagName() {
262: return TAG_PAGE;
263: }
264:
265: /* (non-Javadoc)
266: * @see org.openharmonise.rm.dsi.DataStoreObject#getInstanceJoinConditions(java.lang.String, boolean)
267: */
268: public JoinConditions getInstanceJoinConditions(String sObjectTag,
269: boolean bIsOuter) throws DataStoreException {
270: //nothing to do
271: return null;
272: }
273:
274: /* (non-Javadoc)
275: * @see org.openharmonise.rm.dsi.DataStoreObject#getInstanceColumnRef(java.lang.String, boolean)
276: */
277: public ColumnRef getInstanceColumnRef(String sColumn,
278: boolean bIsHist) throws DataStoreException {
279: ColumnRef colref = null;
280:
281: String sTable = getTableName(bIsHist);
282:
283: if (sColumn.equals(CLMN_XML) == true) {
284: colref = new ColumnRef(sTable, CLMN_XML, ColumnRef.NUMBER);
285: } else if (sColumn.equals(CLMN_XSL) == true) {
286: colref = new ColumnRef(sTable, CLMN_XSL, ColumnRef.NUMBER);
287: } else if (sColumn.equals(CLMN_TIMEOUT) == true) {
288: colref = new ColumnRef(sTable, CLMN_TIMEOUT,
289: ColumnRef.NUMBER);
290: } else {
291: colref = super .getInstanceColumnRef(sColumn, bIsHist);
292: }
293:
294: return colref;
295: }
296:
297: /*----------------------------------------------------------------------------
298: Protected Functions
299: -----------------------------------------------------------------------------*/
300:
301: /* (non-Javadoc)
302: * @see org.openharmonise.rm.resources.AbstractEditableObject#addDataToSave(org.openharmonise.commons.dsi.dml.InsertStatement)
303: */
304: protected void addDataToSave(InsertStatement insert)
305: throws DataStoreException {
306:
307: boolean bIsHist = isHistorical();
308:
309: try {
310: if (m_XML != null) {
311:
312: int nXMLId = getXML().getId();
313:
314: if (nXMLId > 0) {
315: insert.addColumnValue(getInstanceColumnRef(
316: CLMN_XML, bIsHist), nXMLId);
317: }
318: }
319:
320: if (m_XSL != null) {
321:
322: int nXSLId = getXSL().getId();
323:
324: if (nXSLId > 0) {
325: insert.addColumnValue(getInstanceColumnRef(
326: CLMN_XSL, bIsHist), nXSLId);
327: }
328:
329: }
330: } catch (DataAccessException e) {
331: throw new DataStoreException(e.getLocalizedMessage(), e);
332: }
333:
334: insert.addColumnValue(getInstanceColumnRef(CLMN_TIMEOUT,
335: bIsHist), m_nTimeout);
336:
337: super .addDataToSave(insert);
338: }
339:
340: /* (non-Javadoc)
341: * @see org.openharmonise.rm.resources.AbstractObject#addColumnsToPopulateQuery(org.openharmonise.commons.dsi.dml.SelectStatement, boolean)
342: */
343: protected void addColumnsToPopulateQuery(SelectStatement select,
344: boolean bIsHist) throws DataStoreException {
345:
346: select.addSelectColumn(getInstanceColumnRef(CLMN_XML, bIsHist));
347: select.addSelectColumn(getInstanceColumnRef(CLMN_XSL, bIsHist));
348: select.addSelectColumn(getInstanceColumnRef(CLMN_TIMEOUT,
349: bIsHist));
350:
351: super .addColumnsToPopulateQuery(select, bIsHist);
352: }
353:
354: /* (non-Javadoc)
355: * @see org.openharmonise.rm.resources.AbstractObject#populateFromResultSetRow(java.sql.ResultSet, org.openharmonise.commons.dsi.dml.SelectStatement)
356: */
357: protected void populateFromResultSetRow(ResultSet rs,
358: SelectStatement select) throws PopulateException {
359: int nTemp = -1;
360:
361: try {
362: boolean bIsHist = isHistorical();
363: ColumnRef colref = ColumnRefCache.getInstance()
364: .getColumnRef(this , CLMN_XML, bIsHist);
365: if (select.containsSelectColumn(colref) == true) {
366: nTemp = rs.getInt(select.getResultSetIndex(colref));
367:
368: if (m_XML != null) {
369:
370: int nXMLId = ((AbstractObject) m_XML.getObject())
371: .getId();
372:
373: if (nXMLId != nTemp) {
374: setIsChanged(true);
375: }
376:
377: } else if (m_XML == null && nTemp > 0) {
378:
379: XMLResource xml = (XMLResource) HarmoniseObjectFactory
380: .instantiateHarmoniseObject(m_dsi,
381: XMLResource.class.getName(), nTemp);
382:
383: m_XML = CacheHandler.getInstance(m_dsi)
384: .getCachePointer(xml);
385: }
386: }
387:
388: colref = ColumnRefCache.getInstance().getColumnRef(this ,
389: CLMN_XSL, bIsHist);
390: if (select.containsSelectColumn(colref) == true) {
391: nTemp = rs.getInt(select.getResultSetIndex(colref));
392:
393: if (m_XSL != null) {
394: int nXSLId = ((AbstractObject) m_XSL.getObject())
395: .getId();
396:
397: if (nXSLId != nTemp) {
398: setIsChanged(true);
399: }
400: } else if (m_XSL == null && nTemp > 0) {
401: XSLResource xsl = (XSLResource) HarmoniseObjectFactory
402: .instantiateHarmoniseObject(m_dsi,
403: XSLResource.class.getName(), nTemp);
404:
405: m_XSL = CacheHandler.getInstance(m_dsi)
406: .getCachePointer(xsl);
407: }
408: }
409:
410: colref = ColumnRefCache.getInstance().getColumnRef(this ,
411: CLMN_TIMEOUT, bIsHist);
412: if (select.containsSelectColumn(colref) == true) {
413: nTemp = rs.getInt(select.getResultSetIndex(colref));
414:
415: if (m_nTimeout > 0 && m_nTimeout != nTemp) {
416: setIsChanged(true);
417: } else {
418: m_nTimeout = nTemp;
419: }
420: }
421: } catch (HarmoniseFactoryException e) {
422: throw new PopulateException(
423: "Error occured getting object from factory", e);
424: } catch (SQLException e) {
425: throw new PopulateException(
426: "Error occured getting data from resultset", e);
427: } catch (CacheException e) {
428: throw new PopulateException("Cache error", e);
429: }
430:
431: super.populateFromResultSetRow(rs, select);
432: }
433:
434: }
|