001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: *
013: * @created Jun 23, 2005
014: * @author Marc Batchelor
015: *
016: */
017:
018: package org.pentaho.repository.content;
019:
020: import java.io.File;
021: import java.io.IOException;
022: import java.text.MessageFormat;
023: import java.util.Iterator;
024: import java.util.List;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.hibernate.Query;
029: import org.hibernate.Session;
030: import org.pentaho.core.repository.IContentItem;
031: import org.pentaho.core.repository.IContentLocation;
032: import org.pentaho.core.repository.content.ContentException;
033: import org.pentaho.core.system.PentahoBase;
034: import org.pentaho.core.system.PentahoSystem;
035: import org.pentaho.messages.Messages;
036: import org.pentaho.repository.HibernateUtil;
037: import org.pentaho.repository.ISearchable;
038: import org.pentaho.util.UUIDUtil;
039:
040: public class ContentLocation extends PentahoBase implements
041: IContentLocation, ISearchable {
042: public static final int ClassVersionNumber = 1;
043:
044: private static final long serialVersionUID = -86133203446335770L;
045:
046: private static final Log logger = LogFactory
047: .getLog(ContentLocation.class);
048:
049: private String dirPath;
050:
051: private String name;
052:
053: private String description;
054:
055: private String solutionId;
056:
057: private String id;
058:
059: private int revision = -1;
060:
061: private static final String[] SearchableColumns = { "name", //$NON-NLS-1$
062: "description", //$NON-NLS-1$
063: "dirPath" }; //$NON-NLS-1$
064:
065: private static final String SearchableTable = "org.pentaho.repository.content.ContentLocation"; //$NON-NLS-1$
066:
067: private static final String SearchablePhraseNamedQuery = "org.pentaho.repository.content.ContentLocation.locationSearcher"; //$NON-NLS-1$
068:
069: /**
070: * Constructor for Hibernate
071: *
072: */
073: protected ContentLocation() {
074: }
075:
076: /**
077: * Constructor
078: *
079: * @param thePath
080: * The path in the file system
081: * @param theName
082: * The "nice name" of the location
083: * @param solId
084: * The solutionId it's associated with
085: * @throws ContentException
086: */
087: protected ContentLocation(String locId, String thePath,
088: String theName, String theDescription, String solId,
089: boolean createIfNotExist) throws ContentException {
090: checkPath(thePath, createIfNotExist);
091: dirPath = thePath;
092: name = theName;
093: solutionId = solId;
094: description = theDescription;
095: id = locId;
096: }
097:
098: public IContentItem newContentItem(String itemName, String title,
099: String extension, String mType, String url, int writeMode)
100: throws ContentException {
101: String cntId = UUIDUtil.getUUIDAsString();
102: return newContentItem(cntId, itemName, title, extension, mType,
103: url, writeMode);
104: }
105:
106: public IContentItem newContentItem(String cntId, String itemName,
107: String title, String extension, String mType, String url,
108: int writeMode) throws ContentException {
109: IContentItem rtn = new ContentItem(cntId, this , itemName,
110: title, mType, extension, url, writeMode);
111: HibernateUtil.makePersistent(rtn);
112: HibernateUtil.flushSession();
113: return rtn;
114: }
115:
116: public IContentItem getContentItemByPath(String path) {
117: Session session = HibernateUtil.getSession();
118: Query qry = session
119: .getNamedQuery("org.pentaho.repository.content.ContentItem.findItemByPath"); //$NON-NLS-1$
120: qry.setString("inPath", path); //$NON-NLS-1$
121: Object rtn = null;
122: try {
123: rtn = qry.uniqueResult();
124: } catch (Exception ignored) {
125: }
126: return (ContentItem) rtn;
127: }
128:
129: public List getMessages() {
130: return null;
131: }
132:
133: /**
134: * @return Returns the revision.
135: */
136: public int getRevision() {
137: return revision;
138: }
139:
140: /**
141: * @param revision
142: * The revision to set.
143: */
144: public void setRevision(int revision) {
145: this .revision = revision;
146: }
147:
148: /**
149: * Iterates over registered content items.
150: *
151: * @return Iterator of the child content
152: */
153: public Iterator getContentItemIterator() {
154: Session session = HibernateUtil.getSession();
155: Query qry = session
156: .createQuery("from ContentItem where parent = :contentParent"); //$NON-NLS-1$
157: qry.setParameter("contentParent", this ); //$NON-NLS-1$
158: List list = qry.list();
159: if (list != null) {
160: return list.iterator();
161: } else {
162: return null;
163: }
164: }
165:
166: public IContentItem getContentItemById(String itemId) {
167: Session session = HibernateUtil.getSession();
168: return (ContentItem) session.get(ContentItem.class, itemId);
169: }
170:
171: public IContentItem getContentItemByName(String itemName) {
172: Session session = HibernateUtil.getSession();
173: Query qry = session
174: .getNamedQuery("org.pentaho.repository.content.ContentItem.findItemByName"); //$NON-NLS-1$
175: qry.setEntity("parent", this ); //$NON-NLS-1$
176: qry.setString("name", itemName); //$NON-NLS-1$
177: Object rtn = null;
178: try {
179: rtn = qry.uniqueResult();
180: } catch (Exception ignored) {
181: logger.debug(ignored);
182: }
183: return (ContentItem) rtn;
184: }
185:
186: /**
187: * Gets a temporary file in the location.
188: *
189: * @param fileSuffix
190: * What the file suffix should be. If null, then .tmp will be
191: * used.
192: * @return File that is unique within the directory inside this location.
193: * @throws ContentException
194: */
195: public File getTmpFile(String fileSuffix) throws ContentException {
196: return getTmpFile(fileSuffix, false);
197: }
198:
199: /**
200: * Gets a temporary file in the location.
201: *
202: * @param fileSuffix
203: * What the file suffix should be. If null, then .tmp will be
204: * used.
205: * @param deleteOnExit
206: * If true, will call the files' deleteOnExit method which will
207: * attempt to delete the file on VM termination.
208: * @return File that is unique within the directory inside this location.
209: * @throws ContentException
210: */
211: public File getTmpFile(String fileSuffix, boolean deleteOnExit)
212: throws ContentException {
213: File f = checkPath();
214: try {
215: File rtn = File.createTempFile("PENTAHOTMP", fileSuffix, f); //$NON-NLS-1$
216: if (deleteOnExit) {
217: rtn.deleteOnExit();
218: }
219: return rtn;
220: } catch (IOException ex) {
221: throwError(Messages
222: .getErrorString(
223: "CONTLOC.ERROR_0002_CANNOT_CREATE_TMPFILE", getDirPath())); //$NON-NLS-1$
224: return null; // Unreachable
225: }
226: }
227:
228: /**
229: * Creates a subdirectory in the content location.
230: *
231: * @param subDirName
232: * The directory name to create
233: * @return File created
234: * @throws ContentException
235: */
236: public File makeSubdirectory(String subDirName)
237: throws ContentException {
238: File f = checkPath();
239: File newDir = new File(f, subDirName);
240: if (newDir.mkdirs()) {
241: return newDir;
242: }
243: throwError(Messages.getErrorString(
244: "CONTLOC.ERROR_0003_MKDIR", newDir.getAbsolutePath())); //$NON-NLS-1$
245: return null; // Unreachable
246: }
247:
248: /*
249: * Utility Methods
250: */
251: protected File checkPath() throws ContentException {
252: return checkPath(getDirPath());
253: }
254:
255: protected File checkPath(String thePath) throws ContentException {
256: return checkPath(thePath, false);
257: }
258:
259: protected File checkPath(String thePath, boolean createIfNotExist) {
260: File f = new File(PentahoSystem.getApplicationContext()
261: .getFileOutputPath("system/content") + "/" + thePath); //$NON-NLS-1$ //$NON-NLS-2$
262: if ((!f.exists()) || (!f.isDirectory())) {
263: if (!createIfNotExist) {
264: throwError(Messages
265: .getErrorString(
266: "CONTLOC.ERROR_0004_PATH_DOES_NOT_EXIST", thePath)); //$NON-NLS-1$
267: } else {
268: if (!f.mkdirs()) {
269: throwError(Messages.getErrorString(
270: "CONTLOC.ERROR_0003_MKDIR", thePath)); //$NON-NLS-1$
271: }
272: }
273: }
274: return f;
275: }
276:
277: protected void throwError(String msg) throws ContentException {
278: logger.error(msg);
279: throw new ContentException(msg);
280: }
281:
282: /*
283: * ************* * Accessors * *************
284: */
285:
286: /**
287: * @return Returns the dirPath.
288: */
289: public String getDirPath() {
290: return dirPath;
291: }
292:
293: /**
294: * @param dirPath
295: * The dirPath to set.
296: */
297: public void setDirPath(String dirPath) {
298: this .dirPath = dirPath;
299: checkPath(dirPath, true);
300: }
301:
302: /**
303: * @return Returns the id.
304: */
305: public String getId() {
306: return id;
307: }
308:
309: /**
310: * @param id
311: * The id to set.
312: */
313: public void setId(String id) {
314: this .id = id;
315: }
316:
317: /**
318: * @return Returns the name.
319: */
320: public String getName() {
321: return name;
322: }
323:
324: /**
325: * @param name
326: * The name to set.
327: */
328: public void setName(String name) {
329: this .name = name;
330: }
331:
332: /**
333: * @return Returns the solutionId.
334: */
335: public String getSolutionId() {
336: return solutionId;
337: }
338:
339: /**
340: * @param solutionId
341: * The solutionId to set.
342: */
343: public void setSolutionId(String solutionId) {
344: this .solutionId = solutionId;
345: }
346:
347: /**
348: * @return Returns the description.
349: */
350: public String getDescription() {
351: return description;
352: }
353:
354: /**
355: * @param description
356: * The description to set.
357: */
358: public void setDescription(String description) {
359: this .description = description;
360: }
361:
362: /*
363: * (non-Javadoc)
364: *
365: * @see org.pentaho.core.system.PentahoBase#getLogger()
366: */
367: public Log getLogger() {
368: return logger;
369: }
370:
371: /* ISearchable Needs */
372: /*
373: * (non-Javadoc)
374: *
375: * @see org.pentaho.repository.ISearchable#getSearchableColumns()
376: */
377: public String[] getSearchableColumns() {
378: return SearchableColumns;
379: }
380:
381: /*
382: * (non-Javadoc)
383: *
384: * @see org.pentaho.repository.ISearchable#getSearchableTable()
385: */
386: public String getSearchableTable() {
387: return SearchableTable;
388: }
389:
390: /*
391: * (non-Javadoc)
392: *
393: * @see org.pentaho.repository.ISearchable#getPhraseSearchQueryName()
394: */
395: public String getPhraseSearchQueryName() {
396: return SearchablePhraseNamedQuery;
397: }
398:
399: public String toString() {
400: return MessageFormat
401: .format(
402: "{0}, {1}", new String[] { this .getDescription(), this .getDirPath() }); //$NON-NLS-1$
403: }
404: }
|