1 17 18 package org.pentaho.repository.content; 19 20 import java.io.File ; 21 import java.io.IOException ; 22 import java.text.MessageFormat ; 23 import java.util.HashSet ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.Set ; 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 dirPath; 49 50 private String name; 51 52 private String description; 53 54 private String solutionId; 55 56 private String id; 57 58 private int revision = -1; 59 60 private Set childContent = new HashSet (); 61 62 private static final String [] SearchableColumns = { "name", "description", "dirPath" }; 66 private static final String SearchableTable = "org.pentaho.repository.content.ContentLocation"; 68 private static final String SearchablePhraseNamedQuery = "org.pentaho.repository.content.ContentLocation.locationSearcher"; 70 74 protected ContentLocation() { 75 } 76 77 88 protected ContentLocation(String locId, String thePath, String theName, String theDescription, String 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 itemName, String title, String extension, String mType, String url, int writeMode) throws ContentException { 98 String cntId = UUIDUtil.getUUIDAsString(); 99 return newContentItem(cntId, itemName, title, extension, mType, url, writeMode); 100 } 101 102 public IContentItem newContentItem(String cntId, String itemName, String title, String extension, String mType, String 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 path) { 109 Session session = HibernateUtil.getSession(); 110 Query qry = session.getNamedQuery("org.pentaho.repository.content.ContentItem.findItemByPath"); qry.setString("inPath", path); Object rtn = null; 113 try { 114 rtn = qry.uniqueResult(); 115 } catch (Exception ignored) { 116 } 117 return (ContentItem) rtn; 118 } 119 120 public List getMessages() { 121 return null; 122 } 123 124 127 public int getRevision() { 128 return revision; 129 } 130 131 135 public void setRevision(int revision) { 136 this.revision = revision; 137 } 138 139 144 public Iterator getContentItemIterator() { 145 return getChildContent().iterator(); 146 } 147 148 157 public boolean removeContentItemByName(String 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 ex) { 165 this.warn(Messages.getErrorString("CONTREP.ERROR_0001_REMOVING_ITEM", ex.getMessage())); } 167 } 168 return true; 169 } 170 return false; 171 } 172 173 public IContentItem getContentItemById(String itemId) { 174 Session session = HibernateUtil.getSession(); 175 return (ContentItem) session.get(ContentItem.class, itemId); 176 } 177 178 public IContentItem getContentItemByName(String itemName) { 179 Session session = HibernateUtil.getSession(); 180 Query qry = session.getNamedQuery("org.pentaho.repository.content.ContentItem.findItemByName"); qry.setEntity("parent", this); qry.setString("name", itemName); Object rtn = null; 184 try { 185 rtn = qry.uniqueResult(); 186 } catch (Exception ignored) { 187 } 188 return (ContentItem) rtn; 189 } 190 191 200 public File getTmpFile(String fileSuffix) throws ContentException { 201 return getTmpFile(fileSuffix, false); 202 } 203 204 216 public File getTmpFile(String fileSuffix, boolean deleteOnExit) throws ContentException { 217 File f = checkPath(); 218 try { 219 File rtn = File.createTempFile("PENTAHOTMP", fileSuffix, f); if (deleteOnExit) { 221 rtn.deleteOnExit(); 222 } 223 return rtn; 224 } catch (IOException ex) { 225 throwError(Messages.getErrorString("CONTLOC.ERROR_0002_CANNOT_CREATE_TMPFILE", getDirPath())); return null; } 228 } 229 230 238 public File makeSubdirectory(String subDirName) throws ContentException { 239 File f = checkPath(); 240 File newDir = new File (f, subDirName); 241 if (newDir.mkdirs()) { 242 return newDir; 243 } 244 throwError(Messages.getErrorString("CONTLOC.ERROR_0003_MKDIR", newDir.getAbsolutePath())); return null; } 247 248 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().getFileOutputPath("system/content") + "/" + thePath); if ((!f.exists()) || (!f.isDirectory())) { 262 if (!createIfNotExist) { 263 throwError(Messages.getErrorString("CONTLOC.ERROR_0004_PATH_DOES_NOT_EXIST", thePath)); } else { 265 if (!f.mkdirs()) { 266 throwError(Messages.getErrorString("CONTLOC.ERROR_0003_MKDIR", thePath)); } 268 } 269 } 270 return f; 271 } 272 273 protected void throwError(String msg) throws ContentException { 274 logger.error(msg); 275 throw new ContentException(msg); 276 } 277 278 281 282 285 public String getDirPath() { 286 return dirPath; 287 } 288 289 293 public void setDirPath(String dirPath) { 294 this.dirPath = dirPath; 295 checkPath(dirPath, true); 296 } 297 298 301 public String getId() { 302 return id; 303 } 304 305 309 public void setId(String id) { 310 this.id = id; 311 } 312 313 316 public String getName() { 317 return name; 318 } 319 320 324 public void setName(String name) { 325 this.name = name; 326 } 327 328 331 public String getSolutionId() { 332 return solutionId; 333 } 334 335 339 public void setSolutionId(String solutionId) { 340 this.solutionId = solutionId; 341 } 342 343 346 public String getDescription() { 347 return description; 348 } 349 350 354 public void setDescription(String description) { 355 this.description = description; 356 } 357 358 361 protected Set getChildContent() { 362 return childContent; 363 } 364 365 369 protected void setChildContent(Set childContent) { 370 this.childContent = childContent; 371 } 372 373 378 public Log getLogger() { 379 return logger; 380 } 381 382 383 388 public String [] getSearchableColumns() { 389 return SearchableColumns; 390 } 391 392 397 public String getSearchableTable() { 398 return SearchableTable; 399 } 400 401 406 public String getPhraseSearchQueryName() { 407 return SearchablePhraseNamedQuery; 408 } 409 410 public String toString() { 411 return MessageFormat.format("{0}, {1}", new String [] {this.getDescription(), this.getDirPath()}); } 413 } 414 | Popular Tags |