KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > rm > resources > content > TextResource


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.rm.resources.content;
20
21
22 import java.sql.*;
23 import java.util.Vector JavaDoc;
24 import java.util.logging.*;
25
26 import org.openharmonise.commons.cache.CacheException;
27 import org.openharmonise.commons.dsi.*;
28 import org.openharmonise.commons.dsi.dml.*;
29 import org.openharmonise.commons.net.MimeTypeMapping;
30 import org.openharmonise.rm.*;
31 import org.openharmonise.rm.dsi.ColumnRefCache;
32 import org.openharmonise.rm.publishing.*;
33 import org.openharmonise.rm.resources.AbstractChildObject;
34 import org.openharmonise.rm.resources.lifecycle.EditException;
35 import org.w3c.dom.*;
36
37
38 /**
39  * General abstract class to represent plain text resources within Harmonise.
40  *
41  * @author Michael Bell
42  * @version $Revision: 1.4 $
43  *
44  */

45 abstract public class TextResource extends AbstractChildObject {
46
47     //XML constants
48
/**
49      * Content tag name
50      */

51     public static final String JavaDoc TAG_CONTENT = "Content";
52     
53     /**
54      * Content type tag name
55      */

56     public static final String JavaDoc TAG_CONTENT_TYPE = "ContentType";
57
58     //DB constants
59
/**
60      * Content database column name
61      */

62     private static final String JavaDoc CLMN_CONTENT = "content";
63     /**
64      * Mime-type database column name
65      */

66     private static final String JavaDoc CLMN_MIME_TYPE = "mime_type";
67
68     //member fields
69
/**
70      * This <code>TextResource</code>'s content
71      */

72     protected String JavaDoc m_sContent = null;
73     
74     /**
75      * This <code>TextResource</code>'s mime-type, plain text by default.
76      */

77     protected String JavaDoc m_sContentType = MimeTypeMapping.TEXT.getMimeType();
78     
79     /**
80      * <code>boolean</code> flag which indicates whether the content of this
81      * resource has been populated from the database.
82      */

83     private boolean m_bIsContentPopulated = false;
84     
85     /**
86      * Logger for this class.
87      */

88     private static final Logger m_logger = Logger.getLogger(TextResource.class.getName());
89   
90     /**
91      * Contructs a new instance with no data store interface.
92      */

93     public TextResource() {
94         super();
95     }
96
97     /**
98      * Contructs a new or anonymous resource.
99      *
100      * @param dbintrf the data store interface
101      */

102     public TextResource(AbstractDataStoreInterface dbintrf) {
103         super(dbintrf);
104     }
105
106     /**
107      * Constructs a known resource which may be historical.
108      *
109      * @param dbintrf the data store interface
110      * @param nId the id resource
111      * @param nKey the unique key of the version of the resource
112      * @param bIsHist <code>true</code> if the version of the resource is historical, otherwise <code>false</code>
113      */

114     public TextResource(
115         AbstractDataStoreInterface dbintrf,
116         int nId,
117         int nKey,
118         boolean bIsHist) {
119         super(dbintrf, nId,nKey, bIsHist);
120     }
121
122     /**
123      * Contructs a known resurce object.
124      *
125      * @param dbintrf the data store interface
126      * @param nId the resource id
127      */

128     public TextResource(AbstractDataStoreInterface dbintrf, int nId) {
129         super(dbintrf, nId);
130     }
131
132     /**
133      * Returns the text content of this resource object.
134      *
135      * @return the text content of this resource object
136      * @throws DataAccessException if there is an error populating the
137      * object from the database
138      */

139     public String JavaDoc getContent() throws DataAccessException {
140         try {
141             if (isPopulated() == false) {
142               populateFromDatabase();
143             }
144         } catch (PopulateException e) {
145             throw new DataAccessException("Problem occurred getting content", e);
146         }
147
148         return m_sContent;
149     }
150
151   /**
152    * Populates the resource's content from the database.
153    *
154    * @throws DataAccessException if there is an error getting data
155    * from the database
156    */

157     private void populateContent() throws DataAccessException {
158         if (m_nId > NOTDBSAVED_ID && isContentPopulated() == false && isPopulated() == true) {
159             try {
160                 String JavaDoc sContent =
161                     m_dsi.getClob(
162                         getTableName(isHistorical()),
163                         CLMN_CONTENT,
164                         this
165                             .getInstanceColumnRef(ATTRIB_KEY, isHistorical())
166                             .getColumn()
167                             + "="
168                             + getKey());
169                             
170                 if ((sContent != null) && (sContent.length() > 0)) {
171                     if ((m_sContent == null) || (m_sContent.length() == 0)) {
172                         m_sContent = sContent;
173                     } else if (m_sContent.equals(sContent) == false) {
174                         m_bIsChanged = true;
175                     }
176                 }
177                 m_bIsContentPopulated = true;
178             } catch (DataStoreException e) {
179                 throw new DataAccessException("Error occured getting content from DB:", e);
180             }
181         }
182     }
183
184     /**
185      * Sets the text content of this resource.
186      *
187      * @param sContent the text content
188      * @throws PopulateException if the specified content is invalid for this
189      * resource
190      */

191     public void setContent(String JavaDoc sContent) throws PopulateException {
192         if (isContentPopulated() == true) {
193             if (m_sContent != null && m_sContent.equals(sContent) == false) {
194                 setIsChanged(true);
195             }
196         }
197
198         m_sContent = sContent;
199     }
200
201     /**
202      * Returns <code>true</code> if the text content has been populated
203      * from the database.
204      *
205      * @return <code>true</code> if the content is populated and <code>false</code> otherwise.
206      */

207     public boolean isContentPopulated() {
208         return m_bIsContentPopulated;
209     }
210
211     /**
212      * Returns the content type/mime-type of this text resource.
213      *
214      * @return the content type/mime-type of this text resource
215      * @throws DataAccessException if an error occurs populating this object
216      * from the database
217      */

218     public String JavaDoc getContentType() throws DataAccessException {
219         if (isPopulated() == false) {
220             try {
221                 populateFromDatabase();
222             } catch (PopulateException e) {
223                 throw new DataAccessException(
224                     "Error occured populating from DB",e);
225             }
226         }
227
228         return m_sContentType;
229     }
230
231     /**
232      * Sets the content type/mime-type of this text resource.
233      *
234      * @param sContentType the mime-type
235      */

236     public void setContentType(String JavaDoc sContentType) {
237         if (isPopulated() == true) {
238             if ( (m_sContentType == null && sContentType != null) || m_sContentType.equals(sContentType) == false) {
239                 setIsChanged(true);
240             }
241         }
242
243         m_sContentType = sContentType;
244     }
245
246     /* (non-Javadoc)
247      * @see org.openharmonise.rm.dsi.DataStoreObject#getInstanceColumnRef(java.lang.String, boolean)
248      */

249     public ColumnRef getInstanceColumnRef(String JavaDoc sColumn, boolean bIsHist)
250         throws DataStoreException {
251         ColumnRef colref = null;
252
253         String JavaDoc sTable = getTableName(bIsHist);
254
255         if (sColumn.equals(CLMN_CONTENT) == true || sColumn.equals(TAG_CONTENT) == true) {
256             colref = new ColumnRef(sTable, CLMN_CONTENT, ColumnRef.TEXT);
257         } else if (sColumn.equals(CLMN_MIME_TYPE) == true) {
258             colref = new ColumnRef(sTable, CLMN_MIME_TYPE, ColumnRef.TEXT);
259         } else {
260             colref = super.getInstanceColumnRef(sColumn, bIsHist);
261         }
262
263         return colref;
264     }
265
266     /* (non-Javadoc)
267      * @see org.openharmonise.rm.publishing.Publishable#populate(org.w3c.dom.Element, org.openharmonise.rm.publishing.State)
268      */

269     public void populate(Element xmlElement, State state)
270         throws PopulateException {
271         Text txt = null;
272         String JavaDoc sTagname = xmlElement.getTagName();
273
274         if (sTagname.equals(TAG_CONTENT) == true) {
275             txt = (Text) xmlElement.getFirstChild();
276             setContent(txt.getNodeValue());
277         } else if (sTagname.equals(TAG_CONTENT_TYPE) == true) {
278             txt = (Text) xmlElement.getFirstChild();
279             setContentType(txt.getNodeValue());
280         } else {
281             super.populate(xmlElement, state);
282         }
283
284     }
285
286     /* (non-Javadoc)
287      * @see org.openharmonise.rm.publishing.Publishable#publish(org.w3c.dom.Element, org.openharmonise.rm.publishing.HarmoniseOutput, org.openharmonise.rm.publishing.State)
288      */

289     public Element publish(Element topEl, HarmoniseOutput xmlDoc, State state)
290         throws PublishException {
291         Element docEl = null;
292         Text txt = null;
293
294         String JavaDoc sTagname = topEl.getTagName();
295
296         if (sTagname.equals(TAG_CONTENT) == true) {
297             docEl = xmlDoc.createElement(sTagname);
298             try {
299                 txt = xmlDoc.createTextNode(getContent());
300             } catch (DataAccessException e) {
301                 throw new PublishException(
302                     "Error occured getting content:" + e.getLocalizedMessage());
303             }
304             docEl.appendChild(txt);
305             xmlDoc.copyChildren(docEl, topEl, new Vector JavaDoc());
306         } else if (sTagname.equals(TAG_CONTENT_TYPE) == true) {
307             docEl = xmlDoc.createElement(sTagname);
308             try {
309                 txt = xmlDoc.createTextNode(getContentType());
310             } catch (DataAccessException e) {
311                 throw new PublishException(
312                     "Error occured getting content type:"
313                         + e.getLocalizedMessage());
314             }
315             docEl.appendChild(txt);
316             xmlDoc.copyChildren(docEl, topEl, new Vector JavaDoc());
317         } else {
318             docEl = super.publish(topEl, xmlDoc, state);
319         }
320         return docEl;
321     }
322
323     /*----------------------------------------------------------------------------
324     Protected methods
325     -----------------------------------------------------------------------------*/

326
327     /* (non-Javadoc)
328      * @see org.openharmonise.rm.resources.AbstractEditableObject#saveCoreData()
329      */

330     protected void saveCoreData() throws EditException {
331
332         super.saveCoreData();
333
334         try {
335             m_dsi.updateClob(
336                 getTableName(isHistorical()),
337                 CLMN_CONTENT,
338                 m_sContent,
339                 this
340                     .getInstanceColumnRef(ATTRIB_KEY, isHistorical())
341                     .getColumn()
342                     + "="
343                     + getKey());
344         
345             m_bIsContentPopulated = true;
346         } catch (DataAccessException e) {
347             throw new EditException(
348                 "Error occured accessing object key",e);
349         } catch (DataStoreException e) {
350             throw new EditException(
351                 "Error occured saving content to DB",e);
352         }
353     }
354
355     /* (non-Javadoc)
356      * @see org.openharmonise.rm.resources.AbstractEditableObject#update()
357      */

358     protected void update() throws EditException, DataStoreException {
359
360         super.update();
361
362         try {
363             m_dsi.updateClob(
364                 m_sTable,
365                 CLMN_CONTENT,
366                 m_sContent,
367                 this
368                     .getInstanceColumnRef(ATTRIB_KEY, isHistorical())
369                     .getColumn()
370                     + "="
371                     + getKey());
372         } catch (DataAccessException e) {
373             throw new DataStoreException(
374                 "Error occured accessing object key:"
375                     + e.getLocalizedMessage());
376         }
377     }
378
379     /* (non-Javadoc)
380      * @see org.openharmonise.rm.resources.AbstractEditableObject#addDataToSave(org.openharmonise.commons.dsi.dml.InsertStatement)
381      */

382     protected void addDataToSave(InsertStatement insert)
383         throws DataStoreException {
384         
385         insert.addColumnValue(
386             this.getInstanceColumnRef(CLMN_MIME_TYPE, isHistorical()),
387             m_sContentType);
388
389         super.addDataToSave(insert);
390     }
391
392     /* (non-Javadoc)
393      * @see org.openharmonise.rm.resources.AbstractObject#populateFromResultSetRow(java.sql.ResultSet, org.openharmonise.commons.dsi.dml.SelectStatement)
394      */

395     protected void populateFromResultSetRow(
396         ResultSet rs,
397         SelectStatement select)
398         throws PopulateException {
399
400         if (isPopulated() == false) {
401             String JavaDoc sTemp = null;
402             ColumnRef colref = null;
403
404             try {
405                 ColumnRefCache cache = ColumnRefCache.getInstance();
406                 
407                 boolean bIsHist = isHistorical();
408                 colref = cache.getColumnRef(this,CLMN_CONTENT, bIsHist);
409
410                 if (select.containsSelectColumn(colref) == true) {
411                     sTemp = rs.getString(select.getResultSetIndex(colref));
412
413                     if ((sTemp != null) && (sTemp.length() > 0)) {
414                         if ((m_sContent == null)
415                             || (m_sContent.length() == 0)) {
416                             m_sContent = sTemp;
417                         } else if (m_sContent.equals(sTemp) == false) {
418                             setIsChanged(true);
419                         }
420                     }
421                 }
422
423                 colref = cache.getColumnRef(this,CLMN_MIME_TYPE, bIsHist);
424                 if (select.containsSelectColumn(colref) == true) {
425                     sTemp = rs.getString(select.getResultSetIndex(colref));
426
427                     if ((sTemp != null) && (sTemp.length() > 0)) {
428                         if ((m_sContentType == null)
429                             || (m_sContentType.length() == 0)) {
430                             m_sContentType = sTemp;
431                         } else if (m_sContentType.equals(sTemp) == false) {
432                             setIsChanged(true);
433                         }
434                     }
435                 }
436             } catch (SQLException e) {
437                 throw new PopulateException(
438                     "SQL error occured populating" ,e);
439             } catch (CacheException e) {
440                 throw new PopulateException(
441                     "Cache error occured populating" ,e);
442             }
443
444             super.populateFromResultSetRow(rs, select);
445         }
446     }
447     
448     /* (non-Javadoc)
449      * @see java.lang.Object#clone()
450      */

451     public Object JavaDoc clone() {
452         try {
453             if (m_bIsPopulated == false) {
454                 populateFromDatabase();
455             }
456
457             TextResource other = (TextResource) super.clone();
458
459             return other;
460         } catch (PopulateException e) {
461             m_logger.log(Level.WARNING,e.getLocalizedMessage(), e);
462
463             return null;
464         }
465     }
466
467     /* (non-Javadoc)
468      * @see org.openharmonise.rm.resources.AbstractObject#addColumnsToPopulateQuery(org.openharmonise.commons.dsi.dml.SelectStatement, boolean)
469      */

470     protected void addColumnsToPopulateQuery(
471         SelectStatement select,
472         boolean bIsHist)
473         throws DataStoreException {
474         
475         try {
476             ColumnRefCache cache = ColumnRefCache.getInstance();
477             
478             select.addSelectColumn(cache.getColumnRef(this,CLMN_MIME_TYPE, bIsHist));
479             
480         } catch (CacheException e) {
481             throw new DataStoreException("Error with cached colref",e);
482         }
483         super.addColumnsToPopulateQuery(select, bIsHist);
484     }
485
486     /* (non-Javadoc)
487      * @see org.openharmonise.rm.resources.AbstractObject#populateFromDatabase()
488      */

489     protected void populateFromDatabase() throws PopulateException {
490         super.populateFromDatabase(); // populate the main data
491

492         // now populate the content.
493
try {
494           populateContent();
495         } catch (DataAccessException e) {
496           throw new PopulateException("Error occured populating content:", e);
497         }
498     }
499 }
500
Popular Tags