KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > repository > content > ContentItem


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  * @created Jun 17, 2005
14  * @author Marc Batchelor
15  *
16  */

17
18 package org.pentaho.repository.content;
19
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.OutputStream JavaDoc;
23 import java.io.Reader JavaDoc;
24 import java.text.MessageFormat JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Date JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import javax.activation.FileDataSource JavaDoc;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.pentaho.core.repository.content.ContentException;
33 import org.pentaho.core.repository.IContentItem;
34 import org.pentaho.core.system.PentahoBase;
35 import org.pentaho.core.system.PentahoSystem;
36 import org.pentaho.messages.MessageUtil;
37 import org.pentaho.messages.Messages;
38 import org.pentaho.repository.HibernateUtil;
39 import org.pentaho.repository.ISearchable;
40 import org.pentaho.util.UUIDUtil;
41
42 public class ContentItem extends PentahoBase implements IContentItem, ISearchable {
43     public static final int ClassVersionNumber = 1;
44
45     private static final long serialVersionUID = 823604019645900631L;
46
47     private static final Log logger = LogFactory.getLog(ContentItem.class);
48
49     private String JavaDoc id; // Required
50

51     private ContentLocation parent; // Required
52

53     private String JavaDoc path; // Derived
54

55     private String JavaDoc name; // Required
56

57     private String JavaDoc title; // required
58

59     private String JavaDoc mimeType; // Required
60

61     private String JavaDoc url; // Optional
62

63     private String JavaDoc extension; // Required
64

65     private List JavaDoc fileVersions = new ArrayList JavaDoc();
66
67     private ContentItemFile latestFile;
68
69     private int latestVersionNum; // generated by this class
70

71     private int revision = -1; // Hibernate Revision
72

73     private int writeMode; // Indicates whether this kind of file is versioned
74

75     // or not.
76

77     private static final String JavaDoc PATH_BUILDER = "{0}/{1}"; //$NON-NLS-1$
78

79     private static final String JavaDoc[] SearchableColumns = { "name", //$NON-NLS-1$
80
"title", //$NON-NLS-1$
81
"path" }; //$NON-NLS-1$
82

83     private static final String JavaDoc SearchableTable = "org.pentaho.repository.content.ContentItem"; //$NON-NLS-1$
84

85     private static final String JavaDoc SearchablePhraseNamedQuery = "org.pentaho.repository.content.ContentItem.itemSearcher"; //$NON-NLS-1$
86

87     /* Constructor for Hibernate */
88
89     protected ContentItem() {
90     }
91
92     /**
93      * Constructor
94      *
95      * @param cntId
96      * @param theParent
97      * @param theName
98      * @param title
99      * @param mType
100      * @param url
101      */

102     protected ContentItem(String JavaDoc cntId, ContentLocation theParent, String JavaDoc theName, String JavaDoc title, String JavaDoc mType, String JavaDoc extension, String JavaDoc url, int writeMode) {
103         name = theName;
104         mimeType = mType;
105         parent = theParent;
106         id = cntId;
107         String JavaDoc extnSep = "."; //$NON-NLS-1$
108
if (!extension.startsWith(extnSep)) {
109             this.extension = extnSep + extension;
110         } else {
111             this.extension = extension;
112         }
113         this.title = title;
114         this.url = url;
115         this.path = MessageUtil.formatMessage(PATH_BUILDER, parent.getDirPath(), this.getName());
116         this.writeMode = writeMode;
117     }
118
119     public List JavaDoc getMessages() {
120         return null;
121     }
122
123     protected ContentItemFile newContentFile(String JavaDoc actionName) {
124         String JavaDoc fileGuid = UUIDUtil.getUUIDAsString();
125         String JavaDoc fileName = fileGuid + this.getExtension();
126         latestVersionNum++;
127         ContentItemFile theFile = new ContentItemFile(this, fileGuid, parent.getDirPath(), fileName, latestVersionNum, actionName);
128         this.latestFile = theFile;
129         fileVersions.add(theFile);
130         return theFile;
131     }
132
133     public InputStream JavaDoc getInputStream() throws ContentException {
134         ContentItemFile cif = getLatestFile();
135         if (this.latestFile == null) {
136             throw new ContentException(Messages.getErrorString("CONTITEM.ERROR_0001_NO_EXISTING_FILES", this.getName())); //$NON-NLS-1$
137
}
138         return cif.getInputStream();
139     }
140
141     public FileDataSource JavaDoc getDataSource() {
142         ContentItemFile cif = getLatestFile();
143         if (this.latestFile == null) {
144             throw new ContentException(Messages.getErrorString("CONTITEM.ERROR_0001_NO_EXISTING_FILES", this.getName())); //$NON-NLS-1$
145
}
146
147         String JavaDoc fullPath = PentahoSystem.getApplicationContext().getFileOutputPath("system/content/" + cif.getOsPath() + "/" + cif.getId() + extension); //$NON-NLS-1$ //$NON-NLS-2$
148
FileDataSource JavaDoc dataSource = new FileDataSource JavaDoc(fullPath);
149         return dataSource;
150     }
151
152     public Reader JavaDoc getReader() throws ContentException {
153         ContentItemFile cif = getLatestFile();
154         if (this.latestFile == null) {
155             throw new ContentException(Messages.getErrorString("CONTITEM.ERROR_0002_NO_EXISTING_FILES", this.getName())); //$NON-NLS-1$
156
}
157         return cif.getReader();
158     }
159
160     public OutputStream JavaDoc getOutputStream(String JavaDoc actionName) throws IOException JavaDoc {
161       if (actionName == null) {
162         throw new IllegalArgumentException JavaDoc(Messages.getErrorString("CONTITEM.ERROR_0006_ACTION_NAME_CANNOT_BE_NULL")); //$NON-NLS-1$
163
}
164         switch (getWriteMode()) {
165         case WRITEMODE_KEEPVERSIONS: {
166             ContentItemFile cif = newContentFile(actionName);
167             return cif.getOutputStream(false);
168         }
169         case WRITEMODE_OVERWRITE: {
170             ContentItemFile cif = getLatestFile();
171             if (cif == null) {
172                 cif = newContentFile(actionName);
173             }
174             if (cif != null) {
175                 return cif.getOutputStream(true);
176             }
177             throw new IOException JavaDoc(Messages.getErrorString("CONTITEM.ERROR_0004_OUTPUT_STREAM_NOT_AVAILABLE")); //$NON-NLS-1$
178
}
179         case WRITEMODE_APPEND: {
180             ContentItemFile cif = getLatestFile();
181             if (cif == null) {
182                 cif = newContentFile(actionName);
183             }
184             if (cif != null) {
185                 return cif.getOutputStream(true, true);
186             }
187             throw new IOException JavaDoc(Messages.getErrorString("CONTITEM.ERROR_0004_OUTPUT_STREAM_NOT_AVAILABLE")); //$NON-NLS-1$
188
}
189         default: {
190             throw new ContentException(Messages.getErrorString("CONTITEM.ERROR_0003_BAD_WRITE_MODE", Integer.toString(getWriteMode()))); //$NON-NLS-1$
191
}
192         }
193     }
194
195     public String JavaDoc getActionName() {
196         return this.getLatestFile() != null ? this.getLatestFile().getActionName() : null;
197     }
198
199     public String JavaDoc getFileId() {
200         return this.getLatestFile() != null ? this.getLatestFile().getId() : null;
201     }
202
203     public long getFileSize() {
204         return this.getLatestFile() != null ? this.getLatestFile().getFileSize() : -1;
205     }
206
207     public Date JavaDoc getFileDateTime() {
208         return this.getLatestFile() != null ? this.getLatestFile().getFileDateTime() : null;
209     }
210
211     /**
212      * @return Returns the writeMode.
213      */

214     public int getWriteMode() {
215         return writeMode;
216     }
217
218     /**
219      * @param writeMode
220      * The writeMode to set.
221      */

222     public void setWriteMode(int writeMode) {
223         this.writeMode = writeMode;
224     }
225
226     /**
227      * equals override for Hibernate
228      *
229      * @see java.lang.Object#equals(java.lang.Object)
230      */

231     public boolean equals(Object JavaDoc other) {
232         if (this == other) {
233             return true;
234         }
235         if (!(other instanceof ContentItem)) {
236             return false;
237         }
238         final ContentItem that = (ContentItem) other;
239         return this.getId().equals(that.getId());
240     }
241
242     /**
243      * hashcode override for Hibernate
244      *
245      * @see java.lang.Object#hashCode()
246      */

247     public int hashCode() {
248         return getId().hashCode();
249     }
250
251     /**
252      * @return Returns the extension.
253      */

254     public String JavaDoc getExtension() {
255         return extension;
256     }
257
258     /**
259      * @param extension
260      * The extension to set.
261      */

262     public void setExtension(String JavaDoc extension) {
263         this.extension = extension;
264     }
265
266     /**
267      * @return Returns the latestFile.
268      */

269     protected ContentItemFile getLatestFile() {
270         if (latestFile == null) {
271             List JavaDoc revs = this.getFileVersions();
272             if ((revs != null) && (revs.size() > 0)) {
273                 this.latestFile = (ContentItemFile) revs.get(revs.size() - 1);
274             }
275         }
276         return latestFile;
277     }
278
279     /**
280      * @param latestFile
281      * The latestFile to set.
282      */

283     public void setLatestFile(ContentItemFile latestFile) {
284         this.latestFile = latestFile;
285     }
286
287     /**
288      * @return Returns the latestVersionNum.
289      */

290     public int getLatestVersionNum() {
291         return latestVersionNum;
292     }
293
294     /**
295      * @param latestVersionNum
296      * The latestVersionNum to set.
297      */

298     public void setLatestVersionNum(int latestVersionNum) {
299         this.latestVersionNum = latestVersionNum;
300     }
301
302     /**
303      * @return Returns the revision.
304      */

305     public int getRevision() {
306         return revision;
307     }
308
309     /**
310      * @param revision
311      * The revision to set.
312      */

313     public void setRevision(int revision) {
314         this.revision = revision;
315     }
316
317     public void removeVersion(ContentItemFile cif) {
318         try {
319             cif.deleteOsFile();
320         } catch (Exception JavaDoc ex) {
321             logger.error(Messages.getErrorString("CONTITEM.ERROR_0005_COULD_NOT_DELETE_OS_FILE", cif.getCompleteFileName()), ex); //$NON-NLS-1$
322
}
323         HibernateUtil.makeTransient(cif);
324         getFileVersions().remove(cif);
325         if (latestFile == null || latestFile.getId().equals(cif.getId())) {
326             // we removed the latest file
327
if (getFileVersions().size() == 0) {
328                 // ths list is now empty so there is no current file
329
this.latestFile = null;
330             } else {
331                 // take the last item in the file
332
this.latestFile = (ContentItemFile) getFileVersions().get(getFileVersions().size() - 1);
333             }
334         }
335     }
336
337     public void removeVersion(String JavaDoc fileId) {
338         Iterator JavaDoc it = getFileVersions().iterator();
339         ContentItemFile cif;
340         while (it.hasNext()) {
341             cif = (ContentItemFile) it.next();
342             if (fileId.equalsIgnoreCase(cif.getId())) {
343                 removeVersion(cif);
344                 break;
345             }
346         }
347     }
348
349     /**
350      * Removes all version files.
351      */

352     public void removeAllVersions() {
353         Iterator JavaDoc it = getFileVersions().iterator();
354         ContentItemFile cif;
355         while (it.hasNext()) {
356             cif = (ContentItemFile) it.next();
357             cif.deleteOsFile();
358             HibernateUtil.makeTransient(cif);
359         }
360         getFileVersions().clear();
361         this.latestFile = null;
362     }
363
364     /*
365      * Accessors
366      */

367
368     /**
369      * @return Returns the parent.
370      */

371     public ContentLocation getParent() {
372         return parent;
373     }
374
375     /**
376      * @param theParent
377      * The parent to set.
378      */

379     public void setParent(ContentLocation theParent) {
380         this.parent = theParent;
381     }
382
383     /**
384      * @return Returns the name.
385      */

386     public String JavaDoc getName() {
387         return name;
388     }
389
390     /**
391      * @param fName
392      * The name to set.
393      */

394     public void setName(String JavaDoc fName) {
395         this.name = fName;
396     }
397
398     /**
399      * @return Returns the id.
400      */

401     public String JavaDoc getId() {
402         return id;
403     }
404
405     /**
406      * @param id
407      * The id to set.
408      */

409     public void setId(String JavaDoc id) {
410         this.id = id;
411     }
412
413     /**
414      * @return Returns the mimeType.
415      */

416     public String JavaDoc getMimeType() {
417         return mimeType;
418     }
419
420     /**
421      * @param mimeType
422      * The mimeType to set.
423      */

424     public void setMimeType(String JavaDoc mimeType) {
425         this.mimeType = mimeType;
426     }
427
428     /**
429      * @return Returns the url.
430      */

431     public String JavaDoc getUrl() {
432         return url;
433     }
434
435     /**
436      * @param url
437      * The url to set.
438      */

439     public void setUrl(String JavaDoc url) {
440         this.url = url;
441     }
442
443     /**
444      * @return Returns the path.
445      */

446     public String JavaDoc getPath() {
447         return path;
448     }
449
450     /**
451      * @param path
452      * The path to set.
453      */

454     public void setPath(String JavaDoc path) {
455         this.path = path;
456     }
457
458     /**
459      * @return Returns the fileVersions.
460      */

461     public List JavaDoc getFileVersions() {
462         return fileVersions;
463     }
464
465     /**
466      * @param fileVersions
467      * The fileVersions to set.
468      */

469     public void setFileVersions(List JavaDoc fileVersions) {
470         this.fileVersions = fileVersions;
471     }
472
473     /**
474      * @return Returns the title.
475      */

476     public String JavaDoc getTitle() {
477         return title;
478     }
479
480     /**
481      * @param title
482      * The title to set.
483      */

484     public void setTitle(String JavaDoc title) {
485         this.title = title;
486     }
487
488     /**
489      * @return Returns the log.
490      */

491     public Log getLogger() {
492         return logger;
493     }
494
495     /* ISearchable Needs */
496     public String JavaDoc[] getSearchableColumns() {
497         return SearchableColumns;
498     }
499
500     public String JavaDoc getSearchableTable() {
501         return SearchableTable;
502     }
503
504     public String JavaDoc getPhraseSearchQueryName() {
505         return SearchablePhraseNamedQuery;
506     }
507
508     public void makeTransient() {
509       this.removeAllVersions();
510       HibernateUtil.makeTransient(this);
511     }
512     
513     public String JavaDoc toString() {
514       return MessageFormat.format("{0}, {1}, {2}", new String JavaDoc[] {this.getTitle(), this.getPath(), this.getMimeType()}); //$NON-NLS-1$
515
}
516     
517 }
518
Popular Tags