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 Jul 1, 2005
014: * @author Marc Batchelor
015: *
016: */
017:
018: package org.pentaho.repository.content;
019:
020: import java.util.ArrayList;
021: import java.util.Date;
022: import java.util.List;
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025: import org.hibernate.HibernateException;
026: import org.hibernate.Query;
027: import org.hibernate.Session;
028: import org.pentaho.core.repository.content.ContentException;
029: import org.pentaho.core.repository.IBackgroundExecutedContentId;
030: import org.pentaho.core.repository.IContentItem;
031: import org.pentaho.core.repository.IContentLocation;
032: import org.pentaho.core.repository.IContentRepository;
033: import org.pentaho.core.session.IPentahoSession;
034: import org.pentaho.core.system.IPentahoInitializer;
035: import org.pentaho.core.system.PentahoBase;
036: import org.pentaho.messages.Messages;
037: import org.pentaho.repository.HibernateUtil;
038: import org.pentaho.repository.ISearchable;
039: import org.pentaho.core.repository.RepositoryException;
040: import org.pentaho.core.runtime.IBackgroundExecution;
041: import org.pentaho.util.UUIDUtil;
042:
043: public class ContentRepository extends PentahoBase implements
044: IContentRepository, IPentahoInitializer {
045:
046: /**
047: *
048: */
049: private static final long serialVersionUID = -1096153176439041908L;
050:
051: private static final Log logger = LogFactory
052: .getLog(ContentRepository.class);
053:
054: private static final ThreadLocal threadSession = new ThreadLocal();
055:
056: /**
057: * @return Returns the userSession.
058: */
059: public static IPentahoSession getUserSession() {
060: IPentahoSession userSession = (IPentahoSession) threadSession
061: .get();
062: return userSession;
063: }
064:
065: /**
066: * Constructor
067: */
068:
069: public ContentRepository() {
070:
071: }
072:
073: public List getMessages() {
074: return null;
075: }
076:
077: public void setSession(IPentahoSession session) {
078: threadSession.set(session);
079: genLogIdFromSession(session);
080: HibernateUtil.beginTransaction();
081: }
082:
083: public void init(IPentahoSession session) {
084: this .setSession(session);
085: }
086:
087: public static IContentRepository getInstance(IPentahoSession sess) {
088: threadSession.set(sess);
089: IContentRepository rtn = new ContentRepository();
090: rtn.setSession(sess);
091: return rtn;
092: }
093:
094: public IContentLocation newContentLocation(String thePath,
095: String theName, String description, String solId,
096: boolean createIfNotExists) {
097: debug(Messages.getString("CONTREP.DEBUG_NEW_LOCATION", thePath)); //$NON-NLS-1$
098: Session session = HibernateUtil.getSession();
099: String locId = UUIDUtil.getUUIDAsString();
100: ContentLocation cl = new ContentLocation(locId, thePath,
101: theName, description, solId, createIfNotExists);
102: debug(Messages.getString(
103: "CONTREP.DEBUG_CREATE_LOCATION_ID", locId)); //$NON-NLS-1$
104: try {
105: session.save(cl);
106: session.flush();
107: } catch (HibernateException ex) {
108: error(
109: Messages
110: .getErrorString("CONTREP.ERROR_0004_SAVING_LOCATION"), ex); //$NON-NLS-1$
111: throw new RepositoryException(
112: Messages
113: .getErrorString("CONTREP.ERROR_0004_SAVING_LOCATION"), ex); //$NON-NLS-1$
114: }
115: return cl;
116: }
117:
118: public IContentLocation getContentLocationById(String theId) {
119: Session session = HibernateUtil.getSession();
120: try {
121: return (ContentLocation) session.get(ContentLocation.class,
122: theId);
123: } catch (HibernateException ex) {
124: throw new ContentException(Messages.getErrorString(
125: "CONTREP.ERROR_0002_GETTING_LOCATION", theId), ex); //$NON-NLS-1$
126: }
127: }
128:
129: public IContentLocation getContentLocationByPath(String thePath) {
130: Session session = HibernateUtil.getSession();
131: Query qry = session
132: .getNamedQuery("org.pentaho.repository.content.ContentLocation.findContentLocationByPath"); //$NON-NLS-1$
133: qry.setString("inPath", thePath); //$NON-NLS-1$
134: Object rtn = null;
135: try {
136: rtn = qry.uniqueResult();
137: } catch (Exception ignored) {
138: }
139: return (ContentLocation) rtn;
140: }
141:
142: public IContentItem getContentItemByPath(String thePath) {
143: Session session = HibernateUtil.getSession();
144: Query qry = session
145: .getNamedQuery("org.pentaho.repository.content.ContentItem.findItemByPath"); //$NON-NLS-1$
146: qry.setString("inPath", thePath); //$NON-NLS-1$
147: Object rtn = null;
148: try {
149: rtn = qry.uniqueResult();
150: } catch (Exception ignored) {
151: }
152: return (IContentItem) rtn;
153: }
154:
155: public IContentItem getContentItemById(String theId) {
156: Session session = HibernateUtil.getSession();
157: try {
158: return (IContentItem) session.get(ContentItem.class, theId);
159: } catch (HibernateException ex) {
160: throw new ContentException(
161: Messages
162: .getErrorString(
163: "CONTREP.ERROR_0003_GETTING_CONTENT_BY_ID", theId), ex); //$NON-NLS-1$
164: }
165: }
166:
167: public List getAllContentLocations() {
168: Session session = HibernateUtil.getSession();
169: Query qry = session
170: .getNamedQuery("org.pentaho.repository.content.ContentLocation.findAllContentLocations"); //$NON-NLS-1$
171: return qry.list();
172: }
173:
174: public List searchLocationsForTerms(String searchTerm,
175: int searchType) {
176: ISearchable location = new ContentLocation();
177: return HibernateUtil.searchForTerm(location, searchTerm,
178: searchType);
179: }
180:
181: public List searchContentItemsForTerms(String searchTerm,
182: int searchType) {
183: ISearchable anItem = new ContentItem();
184: return HibernateUtil.searchForTerm(anItem, searchTerm,
185: searchType);
186: }
187:
188: public List searchLocationsAndItemsForTerms(String searchTerm,
189: int searchType) {
190: List rtn1 = searchLocationsForTerms(searchTerm, searchType);
191: List rtn2 = searchContentItemsForTerms(searchTerm, searchType);
192: List rtn = new ArrayList(rtn1);
193: rtn.addAll(rtn2);
194: return rtn;
195: }
196:
197: public int deleteContentOlderThanDate(Date agingDate) {
198: // First, get the list of content older than the specified
199: // date.
200: int removedCount = 0;
201: Session session = HibernateUtil.getSession();
202: Query qry = session
203: .getNamedQuery(
204: "org.pentaho.repository.content.ContentItemFile.agingContentSearcher").setDate("archiveDate", agingDate); //$NON-NLS-1$ //$NON-NLS-2$
205: List contentItemFilesForDeletion = qry.list();
206: ContentItem parentContentItem;
207: ContentItemFile fileForDeletion;
208: for (int i = 0; i < contentItemFilesForDeletion.size(); i++) {
209: fileForDeletion = (ContentItemFile) contentItemFilesForDeletion
210: .get(i);
211: parentContentItem = fileForDeletion.getParent();
212: parentContentItem.removeVersion(fileForDeletion);
213: removedCount++;
214: }
215: return removedCount;
216: }
217:
218: /*
219: * (non-Javadoc)
220: *
221: * @see org.pentaho.core.system.PentahoBase#getLogger()
222: */
223: public Log getLogger() {
224: return logger;
225: }
226:
227: /**
228: * Returns a new background executed content object
229: * @param session Users' session object
230: * @param contentId The content id to reference.
231: * @return new BackgroundExecutedContent
232: */
233: public IBackgroundExecutedContentId newBackgroundExecutedContentId(
234: IPentahoSession session, String contentId) {
235: Session hibSession = HibernateUtil.getSession();
236: String userName = (session.getName() != null ? session
237: .getName() : IBackgroundExecution.DEFAULT_USER_NAME);
238: BackgroundExecutedContentId beci = new BackgroundExecutedContentId(
239: userName, contentId);
240: try {
241: hibSession.save(beci);
242: } catch (HibernateException ex) {
243: error(
244: Messages
245: .getErrorString(
246: "CONTREP.ERROR_0005_SAVING_BACKGROUND_CONTENT_ID", contentId), ex); //$NON-NLS-1$
247: throw new RepositoryException(
248: Messages
249: .getErrorString(
250: "CONTREP.ERROR_0005_SAVING_BACKGROUND_CONTENT_ID", contentId), ex); //$NON-NLS-1$
251: }
252: return beci;
253:
254: }
255:
256: /**
257: * Gets list of users' background execution content ids.
258: * @param session The users' session
259: * @return List of IBackgroundExecutedContentId objects
260: */
261: public List getBackgroundExecutedContentItemsForUser(
262: IPentahoSession session) {
263: Session hibSession = HibernateUtil.getSession();
264: String userName = (session.getName() != null ? session
265: .getName() : IBackgroundExecution.DEFAULT_USER_NAME);
266: Query qry = hibSession
267: .getNamedQuery("org.pentaho.repository.content.BackgroundExecutedContentId.findBackgroundContentItemsForUsers"); //$NON-NLS-1$
268: qry.setString("user", userName); //$NON-NLS-1$
269: return qry.list();
270:
271: }
272:
273: /**
274: * Gets list of all background content ids. Should only be used in an administrative capacity
275: * @param session Users session
276: * @return List of IBackgroundExecutedContentId objects
277: */
278: public List getAllBackgroundExecutedContentItems(
279: IPentahoSession session) {
280: Session hibSession = HibernateUtil.getSession();
281: Query qry = hibSession
282: .getNamedQuery("org.pentaho.repository.content.BackgroundExecutedContentId.findAllBackgroundContent"); //$NON-NLS-1$
283: return qry.list();
284: }
285:
286: /**
287: * Removes an ID from the background executed content Id list
288: * @param session Users' session
289: * @param contentId The content id to remove.
290: */
291: public void removeBackgroundExecutedContentId(
292: IPentahoSession session, String contentId) {
293: Session hibSession = HibernateUtil.getSession();
294: try {
295: BackgroundExecutedContentId beci = (BackgroundExecutedContentId) hibSession
296: .get(BackgroundExecutedContentId.class, contentId);
297: if (beci != null) {
298: HibernateUtil.makeTransient(beci);
299: }
300: } catch (HibernateException ignored) {
301:
302: }
303:
304: }
305:
306: }
|