KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > store > edit > AutoImageCreator


1 /*
2  * Created on Jun 8, 2005
3  *
4  * TODO To change the template for this generated file go to
5  * Window - Preferences - Java - Code Style - Code Templates
6  */

7 package com.openedit.store.edit;
8
9 import java.awt.Dimension JavaDoc;
10 import java.awt.image.BufferedImage JavaDoc;
11 import java.io.File JavaDoc;
12 import java.util.Iterator JavaDoc;
13
14 import javax.imageio.ImageIO JavaDoc;
15
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
18 import org.apache.lucene.document.Document;
19
20 import com.openedit.modules.search.LuceneHitTracker;
21 import com.openedit.store.Category;
22 import com.openedit.store.Product;
23 import com.openedit.store.Store;
24 import com.openedit.store.images.ImageMagickResizer;
25
26 /**
27  * @author admin
28  *
29  * TODO To change the template for this generated type comment go to
30  * Window - Preferences - Java - Code Style - Code Templates
31  */

32 public class AutoImageCreator {
33
34     private static final Log log = LogFactory.getLog(AutoImageCreator.class);
35     
36     protected Store fieldStore;
37     protected boolean fieldUse100s;
38     
39     public void run() throws Exception JavaDoc
40     {
41         //TODO: Make there configurable
42
ImageMagickResizer resizer = new ImageMagickResizer();
43         resizer.setMaxScaledSize(new Dimension JavaDoc(350,400));
44         resizer.setCommandName(getStore().getConfigValue("convertpath") );
45         resizer.setGhostScriptCommand(getStore().getConfigValue("ghostscriptpath") );
46
47         ImageMagickResizer tresizer = new ImageMagickResizer();
48         tresizer.setMaxScaledSize(new Dimension JavaDoc(150,200));
49         tresizer.setCommandName(getStore().getConfigValue("convertpath") );
50         tresizer.setGhostScriptCommand(getStore().getConfigValue("ghostscriptpath") );
51         //resizer.setUseImageMagik(true);
52
log.info("Checking for Medium sized images");
53         //loop over all the hits
54
//TODO: This should be optional
55
Category catalog = getStore().getCatalog("index");
56         String JavaDoc catalogs = "index";
57         for ( Iterator JavaDoc iter = catalog.getChildren().iterator(); iter.hasNext(); )
58         {
59             Category childCatalog = (Category) iter.next();
60             catalogs = catalogs + " OR " + childCatalog.getId();
61         }
62         String JavaDoc query = "category:( " + catalogs + ")";
63         
64         LuceneHitTracker hits = getStore().search(query);
65         log.info("Checking " + hits.getTotal() + " images");
66         for (int i = 0; i < hits.getTotal(); i++)
67         {
68             Document hit = (Document)hits.get(i);
69             //check for medium size. If not then convert to jpg and render
70
String JavaDoc productid = hit.get("id");
71             String JavaDoc path = getStore().getProductPathFinder().idToPath(productid);
72             File JavaDoc med = new File JavaDoc( getStore().getStoreDirectory(),"/products/images/medium/" + path + ext() );
73             if ( !med.exists() )
74             {
75                 log.info("Creating medium image:" + med.getName());
76                 Product prod = getStore().getProduct(productid);
77                 log.info("Loading original");
78                 File JavaDoc orig = getOriginalDocument(prod);
79                 if ( orig != null && orig.exists() )
80                 {
81                     log.info("Start resize " + med.getName());
82                     resizer.resizeImage(orig,med);
83                     log.info("Resize complete " + med.getName());
84                 }
85                 else
86                 {
87                     log.error("No such original: " + orig + " =>" + productid );
88                 }
89             }
90             File JavaDoc thumb = new File JavaDoc( getStore().getStoreDirectory(),"/products/images/thumb/" + path + ext() );
91             if( !thumb.exists() && med.exists() )
92             {
93                 tresizer.resizeImage(med,thumb);
94             }
95             else if (med.exists() && thumb.exists() && thumb.length() > 6000)
96             {
97                 BufferedImage JavaDoc image = ImageIO.read(thumb);
98                 log.info("large image " + productid + " " + image.getHeight());
99                 if ( image.getHeight() > 200 || image.getWidth() > 150 )
100                 {
101                     tresizer.resizeImage(med,thumb);
102                 }
103             }
104             
105         }
106         log.info("Completed image processing");
107     }
108     protected String JavaDoc ext()
109     {
110         if ( isUse100s())
111         {
112             return "-100.jpg";
113         }
114         return ".jpg";
115     }
116     protected File JavaDoc getOriginalDocument(Product inProd)
117     {
118         File JavaDoc image = new File JavaDoc( getStore().getStoreDirectory(),"/products/images/original/" +
119             getStore().getProductPathFinder().idToPath(inProd.getId()) + ext() );
120         return image;
121     }
122     public Store getStore()
123     {
124         return fieldStore;
125     }
126     public void setStore(Store inStore)
127     {
128         fieldStore = inStore;
129     }
130     public boolean isUse100s()
131     {
132         return fieldUse100s;
133     }
134     public void setUse100s(boolean inUse100s)
135     {
136         fieldUse100s = inUse100s;
137     }
138     
139     }
140
141
Popular Tags