KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > archive > Archive


1 package com.openedit.archive;
2
3 import java.io.File JavaDoc;
4 import java.io.OutputStream JavaDoc;
5 import java.io.Serializable JavaDoc;
6 import java.util.ArrayList JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.List JavaDoc;
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 JavaDoc
37 {
38     private static final Log log = LogFactory.getLog(Archive.class);
39     public static final String JavaDoc ARCHIVE_PARAM = "archive";
40     
41     protected File JavaDoc BLANKFILE = new File JavaDoc("blank");
42         
43     protected File JavaDoc fieldOriginalsRootDirectory;
44     protected Store fieldStore;
45     protected Configuration fieldSettings;
46     //protected CollectionArchive fieldCollectionArchive;
47
//protected JobArchive fieldJobArchive;
48

49     //protected HistoryArchive fieldHistoryArchive;
50
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     /**
61      * @param inProduct
62      * @return "/archive/downloads/originals/1073869002award_border/award border.eps";
63      */

64     
65     public String JavaDoc asLinkToOriginal(Product inProduct)
66     {
67         if( inProduct == null)
68         {
69             return null;
70         }
71         StringBuffer JavaDoc out = new StringBuffer JavaDoc();
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     /**
80      * Returns a {@link File} representing the original document for the given
81      * product. This file is not guaranteed to exist; it is simply where the
82      * document <em>ought</em> to be, not necessarily where it actually is.
83      *
84      * @param inProduct The product
85      *
86      * @return The location where the original document ought to be, or
87      * <code>null</code> if that could not be determined
88      */

89     public File JavaDoc getOriginalDocument( Product inProduct )
90     {
91         if ( inProduct == null )
92         {
93             return null;
94         }
95
96         //check locally first
97
File JavaDoc cached = findCachedFile( inProduct );
98         if( cached != null && cached.exists())
99         {
100             return cached;
101         }
102         String JavaDoc path = inProduct.get("originalpath");
103         if ( path == null)
104         {
105             throw new OpenEditRuntimeException("Missing originalpath " + inProduct.getId());
106         }
107
108         File JavaDoc file = new File JavaDoc( path );
109         
110         return file;
111     }
112     //Looks for a local copy of a file
113
public File JavaDoc findCachedFile(Product inProduct)
114     {
115         if ( getOriginalsRootDirectory() != null )
116         {
117             String JavaDoc path = inProduct.get("originalpath");
118             if ( path == null)
119             {
120                 log.error("Product missing originalpath " + inProduct.getId() );
121                 return null;
122             }
123             //this is for UMB names on them
124
String JavaDoc clean = path.replaceAll("\\\\\\\\",""); // \\servername\share\dir\dir\file.jpg
125
if ( File.separatorChar != '\\') //only change in Linux
126
{
127                 clean = clean.replaceAll( "\\\\", File.separator );
128             }
129             File JavaDoc localfile = new File JavaDoc( 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 JavaDoc inProductId ) throws StoreException
147     {
148         return getStore().getProduct( inProductId );
149     }
150
151     public File JavaDoc getOriginalsRootDirectory()
152     {
153         if ( fieldOriginalsRootDirectory == null)
154         {
155             String JavaDoc path = getSettings().getChildValue("originals-root-directory");
156             if ( path != null)
157             {
158                 fieldOriginalsRootDirectory = new File JavaDoc( path );
159             }
160             else
161             {
162                 fieldOriginalsRootDirectory = BLANKFILE;
163             }
164         }
165         return fieldOriginalsRootDirectory;
166     }
167
168     public void setOriginalsRootDirectory(File JavaDoc inOriginalsRootDirectory)
169     {
170         fieldOriginalsRootDirectory = inOriginalsRootDirectory;
171     }
172
173     public File JavaDoc 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 JavaDoc asLinkToConversion(Product prod, String JavaDoc type) throws OpenEditException
198     {
199         if( prod == null)
200         {
201             return null;
202         }
203         File JavaDoc path = getOriginalDocument(prod);
204         String JavaDoc oritype = PathUtilities.extractPageType( path.getPath() );
205         if ( type.equals("original") || oritype.equalsIgnoreCase(type))
206         {
207             return asLinkToOriginal(prod);
208         }
209         String JavaDoc name = PathUtilities.extractPageName( prod.getName() );
210         String JavaDoc link = getCatalogHome() + "/downloads/converted/cache/" + prod.getId() + "/" + name + "." + type;
211
212         return link;
213     }
214
215     public void updateHeaders() throws OpenEditException
216     {
217         //updates the metadata on all images
218
LuceneHitTracker all = getStore().search("unit*");
219         MetadataUpdater service = new MetadataUpdater();
220         service.setProductArchive(getStore().getProductArchive());
221         service.setArchive(this);
222         Iterator JavaDoc 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 JavaDoc 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 JavaDoc settingsFile = new File JavaDoc( 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 JavaDoc inSaveas, String JavaDoc inNotes)
271     {
272         
273     }
274
275 // public CollectionArchive getCollectionArchive()
276
// {
277
// return fieldCollectionArchive;
278
// }
279
//
280
// public void setCollectionArchive(CollectionArchive inCollectionArchive)
281
// {
282
// fieldCollectionArchive = inCollectionArchive;
283
// }
284

285     public int getTreeHeight()
286     {
287         //count the nodes that are open
288
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 JavaDoc inId)
305     {
306         for (Iterator JavaDoc 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 JavaDoc inId)
318     {
319         for (Iterator JavaDoc 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 JavaDoc inProductID) throws StoreException
341     {
342         Product product = getProduct(inProductID);
343         
344         String JavaDoc count = product.get("seriescount");
345
346         if ( count == null)
347         {
348             int i = 0;
349             String JavaDoc 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 JavaDoc makeCacheFile(Product inProduct, String JavaDoc inPostfix)
371     {
372         File JavaDoc tmp = new File JavaDoc( getArchiveDirectory() , "/temp/downloads/" + inPostfix );
373         return tmp;
374     }
375     public String JavaDoc getCatalogId()
376     {
377         return getStore().getCatalogId();
378     }
379     public String JavaDoc getCatalogHome()
380     {
381         return getStore().getStoreHome();
382     }
383
384 // public HistoryArchive getHistoryArchive()
385
// {
386
// return fieldHistoryArchive;
387
// }
388
//
389
// public void setHistoryArchive(HistoryArchive inHistoryArchive)
390
// {
391
// fieldHistoryArchive = inHistoryArchive;
392
// }
393

394     public void zipCollectionDocuments(Collection inSelectedCollection, OutputStream JavaDoc 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 JavaDoc inOriginalDocumentFile, File JavaDoc inConvertFile) throws OpenEditException
404     {
405         //do conversion
406
ImageMagickResizer resizer = new ImageMagickResizer();
407         
408         resizer.setCommandName(getStore().getConfigValue("convertpath") );
409         resizer.setGhostScriptCommand(getStore().getConfigValue("ghostscriptpath") );
410         
411 // Configuration child = getSettings().getChild("convertpath");
412
// if( child != null)
413
// {
414
// resize.setCommandName(child.getValue());
415
// }
416
// child = getSettings().getChild("ghostscriptpath");
417
// if( child != null)
418
// {
419
// resize.setGhostScriptCommand(child.getValue());
420
// }
421

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 JavaDoc getDataProperties(String JavaDoc inType ) throws Exception JavaDoc
451     {
452         String JavaDoc level = (String JavaDoc)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 JavaDoc details = new ArrayList JavaDoc();
465             for (Iterator JavaDoc iter = types.keys().iterator(); iter.hasNext();)
466             {
467                 String JavaDoc key = (String JavaDoc) 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