1 package com.openedit.archive; 2 3 import java.io.File ; 4 import java.io.OutputStream ; 5 import java.io.Serializable ; 6 import java.util.ArrayList ; 7 import java.util.Iterator ; 8 import java.util.List ; 9 10 import org.apache.commons.logging.Log; 11 import org.apache.commons.logging.LogFactory; 12 13 import com.openedit.OpenEditException; 14 import com.openedit.OpenEditRuntimeException; 15 import com.openedit.archive.collection.Collection; 16 import com.openedit.archive.collection.UserCollections; 17 import com.openedit.archive.update.MetadataUpdater; 18 import com.openedit.config.Configuration; 19 import com.openedit.config.XMLConfiguration; 20 import com.openedit.error.EmailErrorHandler; 21 import com.openedit.modules.search.LuceneHitTracker; 22 import com.openedit.page.Page; 23 import com.openedit.page.manage.PageManager; 24 import com.openedit.store.Product; 25 import com.openedit.store.ProductArchive; 26 import com.openedit.store.Store; 27 import com.openedit.store.StoreException; 28 import com.openedit.store.Types; 29 import com.openedit.store.images.ImageMagickResizer; 30 import com.openedit.store.products.Detail; 31 import com.openedit.store.products.MirrorProductArchive; 32 import com.openedit.store.products.PropertyDetails; 33 import com.openedit.users.User; 34 import com.openedit.util.PathUtilities; 35 import com.openedit.util.XmlUtil; 36 public class Archive implements Serializable 37 { 38 private static final Log log = LogFactory.getLog(Archive.class); 39 public static final String ARCHIVE_PARAM = "archive"; 40 41 protected File BLANKFILE = new File ("blank"); 42 43 protected File fieldOriginalsRootDirectory; 44 protected Store fieldStore; 45 protected Configuration fieldSettings; 46 49 protected UserCollections fieldUserCollections; 51 protected int fieldHitsPerPage = 15; 52 protected boolean fieldSlideshow; 53 protected EmailErrorHandler fieldEmailErrorHandler; 54 protected User fieldUser; 55 protected PageManager fieldPageManager; 56 57 public Archive() 58 { 59 } 60 64 65 public String asLinkToOriginal(Product inProduct) 66 { 67 if( inProduct == null) 68 { 69 return null; 70 } 71 StringBuffer out = new StringBuffer (); 72 out.append(getCatalogHome() + "/downloads/originals/"); 73 out.append(inProduct.getId()); 74 out.append("/"); 75 out.append(inProduct.getName()); 76 return out.toString(); 77 } 78 79 89 public File getOriginalDocument( Product inProduct ) 90 { 91 if ( inProduct == null ) 92 { 93 return null; 94 } 95 96 File cached = findCachedFile( inProduct ); 98 if( cached != null && cached.exists()) 99 { 100 return cached; 101 } 102 String path = inProduct.get("originalpath"); 103 if ( path == null) 104 { 105 throw new OpenEditRuntimeException("Missing originalpath " + inProduct.getId()); 106 } 107 108 File file = new File ( path ); 109 110 return file; 111 } 112 public File findCachedFile(Product inProduct) 114 { 115 if ( getOriginalsRootDirectory() != null ) 116 { 117 String path = inProduct.get("originalpath"); 118 if ( path == null) 119 { 120 log.error("Product missing originalpath " + inProduct.getId() ); 121 return null; 122 } 123 String clean = path.replaceAll("\\\\\\\\",""); if ( File.separatorChar != '\\') { 127 clean = clean.replaceAll( "\\\\", File.separator ); 128 } 129 File localfile = new File ( getOriginalsRootDirectory(), clean ); 130 return localfile; 131 } 132 133 return null; 134 } 135 136 public PropertyDetails getProductPropertyDetails() throws StoreException 137 { 138 return getStore().getProductArchive().getPropertyDetails(); 139 } 140 141 public PropertyDetails getCatalogPropertyDetails() throws StoreException 142 { 143 return getStore().getCatalogArchive().getCatalogDetails(); 144 } 145 146 public Product getProduct( String inProductId ) throws StoreException 147 { 148 return getStore().getProduct( inProductId ); 149 } 150 151 public File getOriginalsRootDirectory() 152 { 153 if ( fieldOriginalsRootDirectory == null) 154 { 155 String path = getSettings().getChildValue("originals-root-directory"); 156 if ( path != null) 157 { 158 fieldOriginalsRootDirectory = new File ( path ); 159 } 160 else 161 { 162 fieldOriginalsRootDirectory = BLANKFILE; 163 } 164 } 165 return fieldOriginalsRootDirectory; 166 } 167 168 public void setOriginalsRootDirectory(File inOriginalsRootDirectory) 169 { 170 fieldOriginalsRootDirectory = inOriginalsRootDirectory; 171 } 172 173 public File getArchiveDirectory() 174 { 175 return getStore().getStoreDirectory(); 176 } 177 178 179 public Store getStore() 180 { 181 return fieldStore; 182 } 183 184 public void setStore( Store inStore ) 185 { 186 fieldStore = inStore; 187 } 188 189 public int getHitsPerPage() 190 { 191 return fieldHitsPerPage; 192 } 193 public void setHitsPerPage(int inCount) 194 { 195 fieldHitsPerPage = inCount; 196 } 197 public String asLinkToConversion(Product prod, String type) throws OpenEditException 198 { 199 if( prod == null) 200 { 201 return null; 202 } 203 File path = getOriginalDocument(prod); 204 String oritype = PathUtilities.extractPageType( path.getPath() ); 205 if ( type.equals("original") || oritype.equalsIgnoreCase(type)) 206 { 207 return asLinkToOriginal(prod); 208 } 209 String name = PathUtilities.extractPageName( prod.getName() ); 210 String link = getCatalogHome() + "/downloads/converted/cache/" + prod.getId() + "/" + name + "." + type; 211 212 return link; 213 } 214 215 public void updateHeaders() throws OpenEditException 216 { 217 LuceneHitTracker all = getStore().search("unit*"); 219 MetadataUpdater service = new MetadataUpdater(); 220 service.setProductArchive(getStore().getProductArchive()); 221 service.setArchive(this); 222 Iterator idIterator = all.getAllHits(); 223 service.run(idIterator); 224 } 225 226 public boolean isSlideshow() 227 { 228 return fieldSlideshow; 229 } 230 231 public void setSlideshow(boolean inSlideshow) 232 { 233 fieldSlideshow = inSlideshow; 234 } 235 236 public Configuration getSettings() 237 { 238 if ( fieldSettings ==null) 239 { 240 try 241 { 242 Page settingsFile = getPageManager().getPage(getCatalogHome() + "/configuration/archive.xml"); 243 fieldSettings = new XMLConfiguration(new XmlUtil().getXml(settingsFile.getReader(), "UTF-8")); 244 fieldOriginalsRootDirectory = null; 245 } 246 catch (Exception ex) 247 { 248 throw new OpenEditRuntimeException(ex); 249 } 250 } 251 return fieldSettings; 252 } 253 public void reladSettings() 254 { 255 fieldSettings = null; 256 getSettings(); 257 } 258 public void saveSettings() 259 { 260 File settingsFile = new File ( getArchiveDirectory(), "/configuration/archive.xml"); 261 XMLConfiguration config = (XMLConfiguration) getSettings(); 262 new XmlUtil().saveXmlConfiguration(config, settingsFile); 263 264 } 265 public void setSetting(Configuration inConfig) 266 { 267 fieldSettings = inConfig; 268 } 269 270 public void recordUploaded(String inSaveas, String inNotes) 271 { 272 273 } 274 275 285 public int getTreeHeight() 286 { 287 int height = 720; 289 if( getUser().hasProperty("showadvancedsearch") ) 290 { 291 height = height - 130; 292 } 293 if( getUser().hasProperty("showfavorites") ) 294 { 295 height = height - 130; 296 } 297 if( getUser().hasProperty("showbrowse") ) 298 { 299 height = height - 130; 300 } 301 return height; 302 } 303 304 public boolean hideCategory(String inId) 305 { 306 for (Iterator iter = getSettings().getChildIterator("hidecategory"); iter.hasNext();) 307 { 308 Configuration skip = (Configuration) iter.next(); 309 if( inId.equalsIgnoreCase( skip.getValue() ) ) 310 { 311 return true; 312 } 313 } 314 return false; 315 } 316 317 public boolean ignoreCategory(String inId) 318 { 319 for (Iterator iter = getSettings().getChildIterator("ignorecategory"); iter.hasNext();) 320 { 321 Configuration skip = (Configuration) iter.next(); 322 if( inId.equalsIgnoreCase( skip.getValue() ) ) 323 { 324 return true; 325 } 326 } 327 return false; 328 } 329 330 public UserCollections getUserCollections() 331 { 332 return fieldUserCollections; 333 } 334 335 public void setUserCollections(UserCollections inUserCollections) 336 { 337 fieldUserCollections = inUserCollections; 338 } 339 340 public int countSeries(String inProductID) throws StoreException 341 { 342 Product product = getProduct(inProductID); 343 344 String count = product.get("seriescount"); 345 346 if ( count == null) 347 { 348 int i = 0; 349 String series = product.get("Series"); 350 if( series != null) 351 { 352 LuceneHitTracker hits = getStore().getStoreSearcher().search("Series:" + series, null); 353 i = hits.getTotal(); 354 product.putAttribute("seriescount", String.valueOf(i)); 355 ProductArchive archive = getStore().getProductArchive(); 356 if( archive instanceof MirrorProductArchive) 357 { 358 archive = ((MirrorProductArchive)archive).getBaseArchive(); 359 } 360 archive.saveProduct(product); 361 } 362 return i; 363 } 364 else 365 { 366 return Integer.parseInt(count); 367 } 368 } 369 370 public File makeCacheFile(Product inProduct, String inPostfix) 371 { 372 File tmp = new File ( getArchiveDirectory() , "/temp/downloads/" + inPostfix ); 373 return tmp; 374 } 375 public String getCatalogId() 376 { 377 return getStore().getCatalogId(); 378 } 379 public String getCatalogHome() 380 { 381 return getStore().getStoreHome(); 382 } 383 384 394 public void zipCollectionDocuments(Collection inSelectedCollection, OutputStream inStream, boolean inFop) throws OpenEditException 395 { 396 ZipCollection zip = new ZipCollection(); 397 zip.setArchive(this); 398 399 zip.zipCollectionDocuments(inSelectedCollection, inStream, inFop); 400 401 } 402 403 public boolean convert(File inOriginalDocumentFile, File inConvertFile) throws OpenEditException 404 { 405 ImageMagickResizer resizer = new ImageMagickResizer(); 407 408 resizer.setCommandName(getStore().getConfigValue("convertpath") ); 409 resizer.setGhostScriptCommand(getStore().getConfigValue("ghostscriptpath") ); 410 411 422 return resizer.convert(inOriginalDocumentFile, inConvertFile); 423 } 424 425 426 427 428 429 public EmailErrorHandler getEmailErrorHandler() { 430 return fieldEmailErrorHandler; 431 } 432 433 434 public void setEmailErrorHandler(EmailErrorHandler fieldEmailErrorHandler) { 435 this.fieldEmailErrorHandler = fieldEmailErrorHandler; 436 } 437 438 439 public User getUser() 440 { 441 return fieldUser; 442 } 443 444 445 public void setUser(User inUser) 446 { 447 fieldUser = inUser; 448 } 449 450 public List getDataProperties(String inType ) throws Exception 451 { 452 String level = (String )getUser().getProperty("datalevel"); 453 Types types = null; 454 if( level != null) 455 { 456 types = getStore().getProperties(inType + level); 457 } 458 if( types == null) 459 { 460 types =getStore().getProperties(inType); 461 } 462 if( types != null) 463 { 464 List details = new ArrayList (); 465 for (Iterator iter = types.keys().iterator(); iter.hasNext();) 466 { 467 String key = (String ) iter.next(); 468 Detail detail = getStore().getProductArchive().getPropertyDetails().getDetail(key); 469 if( detail != null) 470 { 471 details.add(detail); 472 } 473 } 474 return details; 475 } 476 return null; 477 } 478 public PageManager getPageManager() 479 { 480 return fieldPageManager; 481 } 482 public void setPageManager(PageManager inPageManager) 483 { 484 fieldPageManager = inPageManager; 485 } 486 } | Popular Tags |