KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > archive > generators > PreviewGenerator


1 package com.openedit.archive.generators;
2
3 import java.awt.Dimension JavaDoc;
4 import java.io.File JavaDoc;
5
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8
9 import com.openedit.OpenEditException;
10 import com.openedit.WebPageRequest;
11 import com.openedit.archive.Archive;
12 import com.openedit.config.Configuration;
13 import com.openedit.generators.Output;
14 import com.openedit.modules.archive.ArchiveModule;
15 import com.openedit.page.Page;
16 import com.openedit.store.Product;
17 import com.openedit.store.images.ImageMagickResizer;
18 import com.openedit.util.PathUtilities;
19
20 /**
21  * This generator generates original product documents from an archive based on
22  * paths of the form <tt>.../<var>productid</var>/<var>filename.ext</var></tt>.
23  *
24  * @author Eric Galluzzo
25  */

26 public class PreviewGenerator extends TempFileGenerator
27 {
28     private static final Log log = LogFactory.getLog(PreviewGenerator.class);
29
30     public void generate( WebPageRequest inReq, Page inPage, Output inOut )
31         throws OpenEditException
32     {
33         Archive archive = (Archive) inReq.getPageValue("archive");
34         String JavaDoc productId = PathUtilities.extractPageName( inPage.getPath() );
35         Product product = archive.getProduct( productId );
36         if ( product == null )
37         {
38             throw new OpenEditException( "No product with ID " + productId );
39         }
40         //TODO: look in preview directory for a pre-created original
41
File JavaDoc previewFile = archive.makeCacheFile(product, "/preview/" + product.getId() + ".jpg");
42         if ( previewFile.exists() && previewFile.length() > 0)
43         {
44             super.generate(inReq,previewFile, inOut);
45             return;
46         }
47         try
48         {
49             previewFile.getParentFile().mkdirs();
50             previewFile.createNewFile(); //keep two people from converting the same one
51

52             //convert it
53
File JavaDoc originalDocumentFile = archive.getOriginalDocument( product );
54             if ( originalDocumentFile == null )
55             {
56                 log.error( "Could not determine original document filename for " + product.getName() );
57                 inReq.redirect(archive.getCatalogHome() + "/missingfile.html" );
58                 return;
59             }
60             if ( !originalDocumentFile.exists() )
61             {
62                 log.error( "Original document not found:" + originalDocumentFile );
63                 inReq.redirect(archive.getCatalogHome() + "/missingfile.jpg");
64                 return;
65             }
66         
67             ImageMagickResizer resizer = new ImageMagickResizer();
68             
69             String JavaDoc w = inReq.getRequestParameter("prefwidth");
70             String JavaDoc h = inReq.getRequestParameter("prefheight");
71             if( w == null)
72             {
73                 w = "800";
74             }
75             if( h == null)
76             {
77                 h = "600";
78             }
79             resizer.setMaxScaledSize(new Dimension JavaDoc(Integer.parseInt(w),Integer.parseInt(h)));
80             Configuration child = archive.getSettings().getChild("convertpath");
81             if( child != null)
82             {
83                 resizer.setCommandName(child.getValue());
84             }
85             child = archive.getSettings().getChild("ghostscriptpath");
86             if( child != null)
87             {
88                 resizer.setGhostScriptCommand(child.getValue());
89             }
90             resizer.resizeImage(originalDocumentFile,previewFile);
91             super.generate(inReq,previewFile, inOut);
92
93         }
94         catch ( Exception JavaDoc ioe )
95         {
96             if( ignoreError(ioe))
97             {
98                 //ignored
99
return;
100             }
101             throw new OpenEditException(ioe);
102         }
103         finally
104         {
105             if ( previewFile.length() == 0)
106             {
107                 previewFile.delete();
108                 //404?
109
}
110         }
111     }
112 }
113
Popular Tags