KickJava   Java API By Example, From Geeks To Geeks.

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


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

17
18 package org.pentaho.repository.content;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Date JavaDoc;
22 import java.util.List JavaDoc;
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.hibernate.HibernateException;
26 import org.hibernate.Query;
27 import org.hibernate.Session;
28 import org.pentaho.core.repository.content.ContentException;
29 import org.pentaho.core.repository.IBackgroundExecutedContentId;
30 import org.pentaho.core.repository.IContentItem;
31 import org.pentaho.core.repository.IContentLocation;
32 import org.pentaho.core.repository.IContentRepository;
33 import org.pentaho.core.session.IPentahoSession;
34 import org.pentaho.core.system.PentahoBase;
35 import org.pentaho.messages.Messages;
36 import org.pentaho.repository.HibernateUtil;
37 import org.pentaho.repository.ISearchable;
38 import org.pentaho.core.repository.RepositoryException;
39 import org.pentaho.core.runtime.IBackgroundExecution;
40 import org.pentaho.util.UUIDUtil;
41
42 public class ContentRepository extends PentahoBase implements IContentRepository {
43
44     /**
45      *
46      */

47     private static final long serialVersionUID = -1096153176439041908L;
48
49     private static final Log logger = LogFactory.getLog(ContentRepository.class);
50
51     private static final ThreadLocal JavaDoc threadSession = new ThreadLocal JavaDoc();
52
53     /**
54      * @return Returns the userSession.
55      */

56     public static IPentahoSession getUserSession() {
57         IPentahoSession userSession = (IPentahoSession) threadSession.get();
58         return userSession;
59     }
60
61     /**
62      * Constructor
63      */

64
65     public ContentRepository() {
66
67     }
68
69     public List JavaDoc getMessages() {
70         return null;
71     }
72
73     public void setSession(IPentahoSession session) {
74         threadSession.set(session);
75         genLogIdFromSession(session);
76         HibernateUtil.beginTransaction();
77     }
78
79     public static IContentRepository getInstance(IPentahoSession sess) {
80         threadSession.set(sess);
81         IContentRepository rtn = new ContentRepository();
82         rtn.setSession(sess);
83         return rtn;
84     }
85
86     public IContentLocation newContentLocation(String JavaDoc thePath, String JavaDoc theName, String JavaDoc description, String JavaDoc solId, boolean createIfNotExists) {
87         debug(Messages.getString("CONTREP.DEBUG_NEW_LOCATION", thePath)); //$NON-NLS-1$
88
Session session = HibernateUtil.getSession();
89         String JavaDoc locId = UUIDUtil.getUUIDAsString();
90         ContentLocation cl = new ContentLocation(locId, thePath, theName, description, solId, createIfNotExists);
91         debug(Messages.getString("CONTREP.DEBUG_CREATE_LOCATION_ID", locId)); //$NON-NLS-1$
92
try {
93             session.save(cl);
94         } catch (HibernateException ex) {
95             error(Messages.getErrorString("CONTREP.ERROR_0004_SAVING_LOCATION"), ex); //$NON-NLS-1$
96
throw new RepositoryException(Messages.getErrorString("CONTREP.ERROR_0004_SAVING_LOCATION"), ex); //$NON-NLS-1$
97
}
98         return cl;
99     }
100
101     public IContentLocation getContentLocationById(String JavaDoc theId) {
102         Session session = HibernateUtil.getSession();
103         try {
104             return (ContentLocation) session.get(ContentLocation.class, theId);
105         } catch (HibernateException ex) {
106             throw new ContentException(Messages.getErrorString("CONTREP.ERROR_0002_GETTING_LOCATION", theId), ex); //$NON-NLS-1$
107
}
108     }
109
110     public IContentLocation getContentLocationByPath(String JavaDoc thePath) {
111         Session session = HibernateUtil.getSession();
112         Query qry = session.getNamedQuery("org.pentaho.repository.content.ContentLocation.findContentLocationByPath"); //$NON-NLS-1$
113
qry.setString("inPath", thePath); //$NON-NLS-1$
114
Object JavaDoc rtn = null;
115         try {
116             rtn = qry.uniqueResult();
117         } catch (Exception JavaDoc ignored) {
118         }
119         return (ContentLocation) rtn;
120     }
121
122     public IContentItem getContentItemByPath(String JavaDoc thePath) {
123         Session session = HibernateUtil.getSession();
124         Query qry = session.getNamedQuery("org.pentaho.repository.content.ContentItem.findItemByPath"); //$NON-NLS-1$
125
qry.setString("inPath", thePath); //$NON-NLS-1$
126
Object JavaDoc rtn = null;
127         try {
128             rtn = qry.uniqueResult();
129         } catch (Exception JavaDoc ignored) {
130         }
131         return (IContentItem) rtn;
132     }
133
134     public IContentItem getContentItemById(String JavaDoc theId) {
135         Session session = HibernateUtil.getSession();
136         try {
137             return (IContentItem) session.get(ContentItem.class, theId);
138         } catch (HibernateException ex) {
139             throw new ContentException(Messages.getErrorString("CONTREP.ERROR_0003_GETTING_CONTENT_BY_ID", theId), ex); //$NON-NLS-1$
140
}
141     }
142
143     public List JavaDoc getAllContentLocations() {
144         Session session = HibernateUtil.getSession();
145         Query qry = session.getNamedQuery("org.pentaho.repository.content.ContentLocation.findAllContentLocations"); //$NON-NLS-1$
146
return qry.list();
147     }
148
149     public List JavaDoc searchLocationsForTerms(String JavaDoc searchTerm, int searchType) {
150         ISearchable location = new ContentLocation();
151         return HibernateUtil.searchForTerm(location, searchTerm, searchType);
152     }
153
154     public List JavaDoc searchContentItemsForTerms(String JavaDoc searchTerm, int searchType) {
155         ISearchable anItem = new ContentItem();
156         return HibernateUtil.searchForTerm(anItem, searchTerm, searchType);
157     }
158
159     public List JavaDoc searchLocationsAndItemsForTerms(String JavaDoc searchTerm, int searchType) {
160         List JavaDoc rtn1 = searchLocationsForTerms(searchTerm, searchType);
161         List JavaDoc rtn2 = searchContentItemsForTerms(searchTerm, searchType);
162         List JavaDoc rtn = new ArrayList JavaDoc(rtn1);
163         rtn.addAll(rtn2);
164         return rtn;
165     }
166
167     public int deleteContentOlderThanDate(Date JavaDoc agingDate) {
168         // First, get the list of content older than the specified
169
// date.
170
int removedCount = 0;
171         Session session = HibernateUtil.getSession();
172         Query qry = session.getNamedQuery("org.pentaho.repository.content.ContentItemFile.agingContentSearcher").setDate("archiveDate", agingDate); //$NON-NLS-1$ //$NON-NLS-2$
173
List JavaDoc contentItemFilesForDeletion = qry.list();
174         ContentItem parentContentItem;
175         ContentItemFile fileForDeletion;
176         for (int i = 0; i < contentItemFilesForDeletion.size(); i++) {
177             fileForDeletion = (ContentItemFile) contentItemFilesForDeletion.get(i);
178             parentContentItem = fileForDeletion.getParent();
179             parentContentItem.removeVersion(fileForDeletion);
180             removedCount++;
181         }
182         return removedCount;
183     }
184
185     /*
186      * (non-Javadoc)
187      *
188      * @see org.pentaho.core.system.PentahoBase#getLogger()
189      */

190     public Log getLogger() {
191         return logger;
192     }
193
194     /**
195      * Returns a new background executed content object
196      * @param session Users' session object
197      * @param contentId The content id to reference.
198      * @return new BackgroundExecutedContent
199      */

200     public IBackgroundExecutedContentId newBackgroundExecutedContentId(IPentahoSession session, String JavaDoc contentId) {
201       Session hibSession = HibernateUtil.getSession();
202       String JavaDoc userName = (session.getName() != null ? session.getName() : IBackgroundExecution.DEFAULT_USER_NAME);
203       BackgroundExecutedContentId beci = new BackgroundExecutedContentId(userName, contentId);
204       try {
205         hibSession.save(beci);
206       } catch (HibernateException ex) {
207           error(Messages.getErrorString("CONTREP.ERROR_0005_SAVING_BACKGROUND_CONTENT_ID", contentId), ex); //$NON-NLS-1$
208
throw new RepositoryException(Messages.getErrorString("CONTREP.ERROR_0005_SAVING_BACKGROUND_CONTENT_ID", contentId), ex); //$NON-NLS-1$
209
}
210       return beci;
211       
212     }
213     
214     /**
215      * Gets list of users' background execution content ids.
216      * @param session The users' session
217      * @return List of IBackgroundExecutedContentId objects
218      */

219     public List JavaDoc getBackgroundExecutedContentItemsForUser(IPentahoSession session) {
220       Session hibSession = HibernateUtil.getSession();
221       String JavaDoc userName = (session.getName() != null ? session.getName() : IBackgroundExecution.DEFAULT_USER_NAME);
222       Query qry = hibSession.getNamedQuery("org.pentaho.repository.content.BackgroundExecutedContentId.findBackgroundContentItemsForUsers"); //$NON-NLS-1$
223
qry.setString("user", userName); //$NON-NLS-1$
224
return qry.list();
225       
226     }
227     
228     /**
229      * Gets list of all background content ids. Should only be used in an administrative capacity
230      * @param session Users session
231      * @return List of IBackgroundExecutedContentId objects
232      */

233     public List JavaDoc getAllBackgroundExecutedContentItems(IPentahoSession session) {
234       Session hibSession = HibernateUtil.getSession();
235       Query qry = hibSession.getNamedQuery("org.pentaho.repository.content.BackgroundExecutedContentId.findAllBackgroundContent"); //$NON-NLS-1$
236
return qry.list();
237     }
238     
239     /**
240      * Removes an ID from the background executed content Id list
241      * @param session Users' session
242      * @param contentId The content id to remove.
243      */

244     public void removeBackgroundExecutedContentId(IPentahoSession session, String JavaDoc contentId) {
245       Session hibSession = HibernateUtil.getSession();
246       try {
247         BackgroundExecutedContentId beci = (BackgroundExecutedContentId) hibSession.get(BackgroundExecutedContentId.class, contentId);
248         if (beci != null) {
249           HibernateUtil.makeTransient(beci);
250         }
251       } catch (HibernateException ignored) {
252           
253       }
254       
255     }
256     
257     
258 }
259
Popular Tags