1 17 18 package org.pentaho.repository.content; 19 20 import java.util.ArrayList ; 21 import java.util.Date ; 22 import java.util.List ; 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 47 private static final long serialVersionUID = -1096153176439041908L; 48 49 private static final Log logger = LogFactory.getLog(ContentRepository.class); 50 51 private static final ThreadLocal threadSession = new ThreadLocal (); 52 53 56 public static IPentahoSession getUserSession() { 57 IPentahoSession userSession = (IPentahoSession) threadSession.get(); 58 return userSession; 59 } 60 61 64 65 public ContentRepository() { 66 67 } 68 69 public List 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 thePath, String theName, String description, String solId, boolean createIfNotExists) { 87 debug(Messages.getString("CONTREP.DEBUG_NEW_LOCATION", thePath)); Session session = HibernateUtil.getSession(); 89 String locId = UUIDUtil.getUUIDAsString(); 90 ContentLocation cl = new ContentLocation(locId, thePath, theName, description, solId, createIfNotExists); 91 debug(Messages.getString("CONTREP.DEBUG_CREATE_LOCATION_ID", locId)); try { 93 session.save(cl); 94 } catch (HibernateException ex) { 95 error(Messages.getErrorString("CONTREP.ERROR_0004_SAVING_LOCATION"), ex); throw new RepositoryException(Messages.getErrorString("CONTREP.ERROR_0004_SAVING_LOCATION"), ex); } 98 return cl; 99 } 100 101 public IContentLocation getContentLocationById(String 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); } 108 } 109 110 public IContentLocation getContentLocationByPath(String thePath) { 111 Session session = HibernateUtil.getSession(); 112 Query qry = session.getNamedQuery("org.pentaho.repository.content.ContentLocation.findContentLocationByPath"); qry.setString("inPath", thePath); Object rtn = null; 115 try { 116 rtn = qry.uniqueResult(); 117 } catch (Exception ignored) { 118 } 119 return (ContentLocation) rtn; 120 } 121 122 public IContentItem getContentItemByPath(String thePath) { 123 Session session = HibernateUtil.getSession(); 124 Query qry = session.getNamedQuery("org.pentaho.repository.content.ContentItem.findItemByPath"); qry.setString("inPath", thePath); Object rtn = null; 127 try { 128 rtn = qry.uniqueResult(); 129 } catch (Exception ignored) { 130 } 131 return (IContentItem) rtn; 132 } 133 134 public IContentItem getContentItemById(String 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); } 141 } 142 143 public List getAllContentLocations() { 144 Session session = HibernateUtil.getSession(); 145 Query qry = session.getNamedQuery("org.pentaho.repository.content.ContentLocation.findAllContentLocations"); return qry.list(); 147 } 148 149 public List searchLocationsForTerms(String searchTerm, int searchType) { 150 ISearchable location = new ContentLocation(); 151 return HibernateUtil.searchForTerm(location, searchTerm, searchType); 152 } 153 154 public List searchContentItemsForTerms(String searchTerm, int searchType) { 155 ISearchable anItem = new ContentItem(); 156 return HibernateUtil.searchForTerm(anItem, searchTerm, searchType); 157 } 158 159 public List searchLocationsAndItemsForTerms(String searchTerm, int searchType) { 160 List rtn1 = searchLocationsForTerms(searchTerm, searchType); 161 List rtn2 = searchContentItemsForTerms(searchTerm, searchType); 162 List rtn = new ArrayList (rtn1); 163 rtn.addAll(rtn2); 164 return rtn; 165 } 166 167 public int deleteContentOlderThanDate(Date agingDate) { 168 int removedCount = 0; 171 Session session = HibernateUtil.getSession(); 172 Query qry = session.getNamedQuery("org.pentaho.repository.content.ContentItemFile.agingContentSearcher").setDate("archiveDate", agingDate); List 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 190 public Log getLogger() { 191 return logger; 192 } 193 194 200 public IBackgroundExecutedContentId newBackgroundExecutedContentId(IPentahoSession session, String contentId) { 201 Session hibSession = HibernateUtil.getSession(); 202 String 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); throw new RepositoryException(Messages.getErrorString("CONTREP.ERROR_0005_SAVING_BACKGROUND_CONTENT_ID", contentId), ex); } 210 return beci; 211 212 } 213 214 219 public List getBackgroundExecutedContentItemsForUser(IPentahoSession session) { 220 Session hibSession = HibernateUtil.getSession(); 221 String userName = (session.getName() != null ? session.getName() : IBackgroundExecution.DEFAULT_USER_NAME); 222 Query qry = hibSession.getNamedQuery("org.pentaho.repository.content.BackgroundExecutedContentId.findBackgroundContentItemsForUsers"); qry.setString("user", userName); return qry.list(); 225 226 } 227 228 233 public List getAllBackgroundExecutedContentItems(IPentahoSession session) { 234 Session hibSession = HibernateUtil.getSession(); 235 Query qry = hibSession.getNamedQuery("org.pentaho.repository.content.BackgroundExecutedContentId.findAllBackgroundContent"); return qry.list(); 237 } 238 239 244 public void removeBackgroundExecutedContentId(IPentahoSession session, String 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 |