KickJava   Java API By Example, From Geeks To Geeks.

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


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 23, 2005
14  * @author Marc Batchelor
15  *
16  */

17
18 package org.pentaho.repository.content;
19
20 import java.io.File JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.text.MessageFormat JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Set JavaDoc;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.hibernate.Query;
30 import org.hibernate.Session;
31 import org.pentaho.core.repository.content.ContentException;
32 import org.pentaho.core.repository.IContentItem;
33 import org.pentaho.core.repository.IContentLocation;
34 import org.pentaho.core.system.PentahoBase;
35 import org.pentaho.core.system.PentahoSystem;
36 import org.pentaho.messages.Messages;
37 import org.pentaho.repository.HibernateUtil;
38 import org.pentaho.repository.ISearchable;
39 import org.pentaho.util.UUIDUtil;
40
41 public class ContentLocation extends PentahoBase implements IContentLocation, ISearchable {
42     public static final int ClassVersionNumber = 1;
43
44     private static final long serialVersionUID = -86133203446335770L;
45
46     private static final Log logger = LogFactory.getLog(ContentLocation.class);
47
48     private String JavaDoc dirPath;
49
50     private String JavaDoc name;
51
52     private String JavaDoc description;
53
54     private String JavaDoc solutionId;
55
56     private String JavaDoc id;
57
58     private int revision = -1;
59
60     private Set JavaDoc childContent = new HashSet JavaDoc();
61
62     private static final String JavaDoc[] SearchableColumns = { "name", //$NON-NLS-1$
63
"description", //$NON-NLS-1$
64
"dirPath" }; //$NON-NLS-1$
65

66     private static final String JavaDoc SearchableTable = "org.pentaho.repository.content.ContentLocation"; //$NON-NLS-1$
67

68     private static final String JavaDoc SearchablePhraseNamedQuery = "org.pentaho.repository.content.ContentLocation.locationSearcher"; //$NON-NLS-1$
69

70     /**
71      * Constructor for Hibernate
72      *
73      */

74     protected ContentLocation() {
75     }
76
77     /**
78      * Constructor
79      *
80      * @param thePath
81      * The path in the file system
82      * @param theName
83      * The "nice name" of the location
84      * @param solId
85      * The solutionId it's associated with
86      * @throws ContentException
87      */

88     protected ContentLocation(String JavaDoc locId, String JavaDoc thePath, String JavaDoc theName, String JavaDoc theDescription, String JavaDoc solId, boolean createIfNotExist) throws ContentException {
89         checkPath(thePath, createIfNotExist);
90         dirPath = thePath;
91         name = theName;
92         solutionId = solId;
93         description = theDescription;
94         id = locId;
95     }
96
97     public IContentItem newContentItem(String JavaDoc itemName, String JavaDoc title, String JavaDoc extension, String JavaDoc mType, String JavaDoc url, int writeMode) throws ContentException {
98         String JavaDoc cntId = UUIDUtil.getUUIDAsString();
99         return newContentItem(cntId, itemName, title, extension, mType, url, writeMode);
100     }
101
102     public IContentItem newContentItem(String JavaDoc cntId, String JavaDoc itemName, String JavaDoc title, String JavaDoc extension, String JavaDoc mType, String JavaDoc url, int writeMode) throws ContentException {
103       IContentItem rtn = new ContentItem(cntId, this, itemName, title, mType, extension, url, writeMode);
104       getChildContent().add(rtn);
105       return rtn;
106     }
107     
108     public IContentItem getContentItemByPath(String JavaDoc path) {
109         Session session = HibernateUtil.getSession();
110         Query qry = session.getNamedQuery("org.pentaho.repository.content.ContentItem.findItemByPath"); //$NON-NLS-1$
111
qry.setString("inPath", path); //$NON-NLS-1$
112
Object JavaDoc rtn = null;
113         try {
114             rtn = qry.uniqueResult();
115         } catch (Exception JavaDoc ignored) {
116         }
117         return (ContentItem) rtn;
118     }
119
120     public List JavaDoc getMessages() {
121         return null;
122     }
123
124     /**
125      * @return Returns the revision.
126      */

127     public int getRevision() {
128         return revision;
129     }
130
131     /**
132      * @param revision
133      * The revision to set.
134      */

135     public void setRevision(int revision) {
136         this.revision = revision;
137     }
138
139     /**
140      * Iterates over registered content items.
141      *
142      * @return Iterator of the child content
143      */

144     public Iterator JavaDoc getContentItemIterator() {
145         return getChildContent().iterator();
146     }
147
148     /**
149      * Removes a specific content item registered to this location.
150      *
151      * @param theFileName
152      * The file name to remove
153      * @param tryFileDelete
154      * If true, it will try to delete the file from the Operating
155      * System too
156      */

157     public boolean removeContentItemByName(String JavaDoc theFileName, boolean tryFileDelete) throws ContentException {
158         IContentItem item = getContentItemByName(theFileName);
159         if (item != null) {
160             getChildContent().remove(item);
161             if (tryFileDelete) {
162                 try {
163                     item.removeAllVersions();
164                 } catch (Exception JavaDoc ex) {
165                     this.warn(Messages.getErrorString("CONTREP.ERROR_0001_REMOVING_ITEM", ex.getMessage())); //$NON-NLS-1$
166
}
167             }
168             return true;
169         }
170         return false;
171     }
172
173     public IContentItem getContentItemById(String JavaDoc itemId) {
174         Session session = HibernateUtil.getSession();
175         return (ContentItem) session.get(ContentItem.class, itemId);
176     }
177
178     public IContentItem getContentItemByName(String JavaDoc itemName) {
179         Session session = HibernateUtil.getSession();
180         Query qry = session.getNamedQuery("org.pentaho.repository.content.ContentItem.findItemByName"); //$NON-NLS-1$
181
qry.setEntity("parent", this); //$NON-NLS-1$
182
qry.setString("name", itemName); //$NON-NLS-1$
183
Object JavaDoc rtn = null;
184         try {
185             rtn = qry.uniqueResult();
186         } catch (Exception JavaDoc ignored) {
187         }
188         return (ContentItem) rtn;
189     }
190
191     /**
192      * Gets a temporary file in the location.
193      *
194      * @param fileSuffix
195      * What the file suffix should be. If null, then .tmp will be
196      * used.
197      * @return File that is unique within the directory inside this location.
198      * @throws ContentException
199      */

200     public File JavaDoc getTmpFile(String JavaDoc fileSuffix) throws ContentException {
201         return getTmpFile(fileSuffix, false);
202     }
203
204     /**
205      * Gets a temporary file in the location.
206      *
207      * @param fileSuffix
208      * What the file suffix should be. If null, then .tmp will be
209      * used.
210      * @param deleteOnExit
211      * If true, will call the files' deleteOnExit method which will
212      * attempt to delete the file on VM termination.
213      * @return File that is unique within the directory inside this location.
214      * @throws ContentException
215      */

216     public File JavaDoc getTmpFile(String JavaDoc fileSuffix, boolean deleteOnExit) throws ContentException {
217         File JavaDoc f = checkPath();
218         try {
219             File JavaDoc rtn = File.createTempFile("PENTAHOTMP", fileSuffix, f); //$NON-NLS-1$
220
if (deleteOnExit) {
221                 rtn.deleteOnExit();
222             }
223             return rtn;
224         } catch (IOException JavaDoc ex) {
225             throwError(Messages.getErrorString("CONTLOC.ERROR_0002_CANNOT_CREATE_TMPFILE", getDirPath())); //$NON-NLS-1$
226
return null; // Unreachable
227
}
228     }
229
230     /**
231      * Creates a subdirectory in the content location.
232      *
233      * @param subDirName
234      * The directory name to create
235      * @return File created
236      * @throws ContentException
237      */

238     public File JavaDoc makeSubdirectory(String JavaDoc subDirName) throws ContentException {
239         File JavaDoc f = checkPath();
240         File JavaDoc newDir = new File JavaDoc(f, subDirName);
241         if (newDir.mkdirs()) {
242             return newDir;
243         }
244         throwError(Messages.getErrorString("CONTLOC.ERROR_0003_MKDIR", newDir.getAbsolutePath())); //$NON-NLS-1$
245
return null; // Unreachable
246
}
247
248     /*
249      * Utility Methods
250      */

251     protected File JavaDoc checkPath() throws ContentException {
252         return checkPath(getDirPath());
253     }
254
255     protected File JavaDoc checkPath(String JavaDoc thePath) throws ContentException {
256         return checkPath(thePath, false);
257     }
258
259     protected File JavaDoc checkPath(String JavaDoc thePath, boolean createIfNotExist) {
260         File JavaDoc f = new File JavaDoc(PentahoSystem.getApplicationContext().getFileOutputPath("system/content") + "/" + thePath); //$NON-NLS-1$ //$NON-NLS-2$
261
if ((!f.exists()) || (!f.isDirectory())) {
262             if (!createIfNotExist) {
263                 throwError(Messages.getErrorString("CONTLOC.ERROR_0004_PATH_DOES_NOT_EXIST", thePath)); //$NON-NLS-1$
264
} else {
265                 if (!f.mkdirs()) {
266                     throwError(Messages.getErrorString("CONTLOC.ERROR_0003_MKDIR", thePath)); //$NON-NLS-1$
267
}
268             }
269         }
270         return f;
271     }
272
273     protected void throwError(String JavaDoc msg) throws ContentException {
274         logger.error(msg);
275         throw new ContentException(msg);
276     }
277
278     /*
279      * ************* * Accessors * *************
280      */

281
282     /**
283      * @return Returns the dirPath.
284      */

285     public String JavaDoc getDirPath() {
286         return dirPath;
287     }
288
289     /**
290      * @param dirPath
291      * The dirPath to set.
292      */

293     public void setDirPath(String JavaDoc dirPath) {
294         this.dirPath = dirPath;
295         checkPath(dirPath, true);
296     }
297
298     /**
299      * @return Returns the id.
300      */

301     public String JavaDoc getId() {
302         return id;
303     }
304
305     /**
306      * @param id
307      * The id to set.
308      */

309     public void setId(String JavaDoc id) {
310         this.id = id;
311     }
312
313     /**
314      * @return Returns the name.
315      */

316     public String JavaDoc getName() {
317         return name;
318     }
319
320     /**
321      * @param name
322      * The name to set.
323      */

324     public void setName(String JavaDoc name) {
325         this.name = name;
326     }
327
328     /**
329      * @return Returns the solutionId.
330      */

331     public String JavaDoc getSolutionId() {
332         return solutionId;
333     }
334
335     /**
336      * @param solutionId
337      * The solutionId to set.
338      */

339     public void setSolutionId(String JavaDoc solutionId) {
340         this.solutionId = solutionId;
341     }
342
343     /**
344      * @return Returns the description.
345      */

346     public String JavaDoc getDescription() {
347         return description;
348     }
349
350     /**
351      * @param description
352      * The description to set.
353      */

354     public void setDescription(String JavaDoc description) {
355         this.description = description;
356     }
357
358     /**
359      * @return Returns the childContent.
360      */

361     protected Set JavaDoc getChildContent() {
362         return childContent;
363     }
364
365     /**
366      * @param childContent
367      * The childContent to set.
368      */

369     protected void setChildContent(Set JavaDoc childContent) {
370         this.childContent = childContent;
371     }
372
373     /*
374      * (non-Javadoc)
375      *
376      * @see org.pentaho.core.system.PentahoBase#getLogger()
377      */

378     public Log getLogger() {
379         return logger;
380     }
381
382     /* ISearchable Needs */
383     /*
384      * (non-Javadoc)
385      *
386      * @see org.pentaho.repository.ISearchable#getSearchableColumns()
387      */

388     public String JavaDoc[] getSearchableColumns() {
389         return SearchableColumns;
390     }
391
392     /*
393      * (non-Javadoc)
394      *
395      * @see org.pentaho.repository.ISearchable#getSearchableTable()
396      */

397     public String JavaDoc getSearchableTable() {
398         return SearchableTable;
399     }
400
401     /*
402      * (non-Javadoc)
403      *
404      * @see org.pentaho.repository.ISearchable#getPhraseSearchQueryName()
405      */

406     public String JavaDoc getPhraseSearchQueryName() {
407         return SearchablePhraseNamedQuery;
408     }
409
410     public String JavaDoc toString() {
411       return MessageFormat.format("{0}, {1}", new String JavaDoc[] {this.getDescription(), this.getDirPath()}); //$NON-NLS-1$
412
}
413 }
414
Popular Tags