KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.openedit.archive;
2
3 import java.io.File JavaDoc;
4 import java.io.FileInputStream JavaDoc;
5 import java.io.FileOutputStream JavaDoc;
6 import java.io.IOException JavaDoc;
7 import java.io.OutputStream JavaDoc;
8 import java.util.ArrayList JavaDoc;
9 import java.util.Iterator JavaDoc;
10 import java.util.List JavaDoc;
11 import java.util.zip.ZipEntry JavaDoc;
12 import java.util.zip.ZipOutputStream JavaDoc;
13
14 import org.apache.commons.logging.Log;
15 import org.apache.commons.logging.LogFactory;
16
17 import com.openedit.OpenEditException;
18 import com.openedit.archive.collection.Collection;
19 import com.openedit.archive.collection.CollectionItem;
20 import com.openedit.store.Order;
21 import com.openedit.store.Store;
22 import com.openedit.store.images.ImageMagickResizer;
23 import com.openedit.util.FileUtils;
24 import com.openedit.util.OutputFiller;
25 import com.openedit.util.PathUtilities;
26 import com.openedit.modules.email.*;
27
28 public class ZipCollection
29 {
30     private static final Log log = LogFactory.getLog(ZipCollection.class);
31     protected Archive fieldArchive;
32     private PostMail fieldPostMail;
33     
34     /**
35      * Creates a zip file as a temp file in <tt>/archive/downloads/cart</tt>,
36      * containing the original documents for all the items in the cart. The
37      * file will be deleted if/when the JVM exits normally.
38      *
39      * @param cart The cart
40      *
41      * @return The path to the temp file
42      */

43     public void zipCollectionDocuments( Collection inCol, OutputStream JavaDoc inStream , boolean inForPositionOnly )
44         throws OpenEditException
45     {
46         List JavaDoc missingDocumentItems = new ArrayList JavaDoc();
47         ZipOutputStream JavaDoc zos = new ZipOutputStream JavaDoc( inStream );
48         zos.setLevel(1); //for speed since these are jpegs
49
WatermarkMaker marker = null;;
50         if( inForPositionOnly )
51         {
52             File JavaDoc water = new File JavaDoc(getArchive().getArchiveDirectory(),"/layout/media/watermark.png");
53             if( water.exists() )
54             {
55                 marker = new WatermarkMaker();
56                 marker.setWaterMark(water);
57             }
58         }
59         try
60         {
61             OutputFiller filler = new OutputFiller();
62             for ( Iterator JavaDoc iter = inCol.getCollectionItems().iterator(); iter.hasNext(); )
63             {
64                 CollectionItem item = (CollectionItem) iter.next();
65                 File JavaDoc documentFile = getArchive().getOriginalDocument( item.getProduct() );
66                 if ( documentFile == null || !documentFile.exists() )
67                 {
68                     if ( documentFile != null)
69                     {
70                         log.info("Image missing:" + documentFile.getAbsolutePath());
71                     }
72                     missingDocumentItems.add( item );
73                 }
74                 else
75                 {
76                     if( inForPositionOnly )
77                     {
78                         //take this file and convert it. Then use it
79
String JavaDoc dir = getArchive().getStore().getProductPathFinder().idToPath(item.getProduct().getId());
80                         File JavaDoc convertFile = getArchive().makeCacheFile(item.getProduct(), "/convert/" + PathUtilities.extractPagePath(dir) + ".jpg" );
81                         if( !getArchive().convert(documentFile, convertFile) ) //Make a JPG file
82
{
83                             log.error("Could not create document " + convertFile.getAbsolutePath() + " from " + documentFile.getAbsolutePath() );
84                             missingDocumentItems.add( item );
85                         }
86                         else
87                         {
88                             //write watermark
89
if( marker != null)
90                             {
91                                 try
92                                 {
93                                     File JavaDoc temp = File.createTempFile(item.getProduct().getName(), "FPO.jpg");
94                                     FileOutputStream JavaDoc done = new FileOutputStream JavaDoc(temp);
95                                     log.info("adding watermarked file: " + temp);
96                                     marker.addWaterMark(new FileInputStream JavaDoc(convertFile), done);
97                                     FileUtils.safeClose(done);
98                                     writeFileToZip( zos, filler, temp );
99                                     temp.delete();
100                                 }
101                                 catch (IOException JavaDoc ex)
102                                 {
103                                     throw new OpenEditException(ex);
104                                 }
105                             }
106                             else
107                             {
108                                 log.info("No watermark found adding size:" + convertFile.length() + " path:" + convertFile.getAbsolutePath() );
109                                 writeFileToZip( zos, filler, convertFile );
110                             }
111                         }
112                     }
113                     else
114                     {
115                         writeFileToZip( zos, filler, documentFile );
116                     }
117                 }
118             }
119
120             if ( !missingDocumentItems.isEmpty() )
121             {
122                 String JavaDoc missingProducts = buildMissingDocumentsText( missingDocumentItems );
123                 
124                 
125                 writeStringToZip( zos, missingProducts, "missing.txt" );
126                 getArchive().getEmailErrorHandler().sendNotification("Missing File Report", missingProducts);
127                 
128             }
129         }
130         finally
131         {
132             try
133             {
134                 FileUtils.safeClose( zos ); //This will fail if there was any error
135
}
136             catch ( Exception JavaDoc ex2)
137             {
138                 //nothing
139
}
140         }
141     }
142
143     protected void writeFileToZip( ZipOutputStream JavaDoc inZipOutputStream,
144         OutputFiller inOutputFiller, File JavaDoc inFile )
145     {
146         ZipEntry JavaDoc entry = new ZipEntry JavaDoc( inFile.getName() );
147         entry.setSize( inFile.length() );
148         entry.setTime( inFile.lastModified() );
149         try
150         {
151             FileInputStream JavaDoc fis = new FileInputStream JavaDoc( inFile );
152             inZipOutputStream.putNextEntry( entry );
153             try
154             {
155                 inOutputFiller.fill( fis, inZipOutputStream );
156             }
157             finally
158             {
159                 fis.close();
160             }
161             inZipOutputStream.closeEntry();
162         }
163         catch ( Exception JavaDoc ex )
164         {
165             log.error( ex );
166         }
167     }
168
169     protected void writeStringToZip( ZipOutputStream JavaDoc inZipOutputStream,
170         String JavaDoc inText, String JavaDoc inFileName )
171     {
172         try
173         {
174             byte[] bytes = inText.getBytes( "UTF-8" );
175             ZipEntry JavaDoc entry = new ZipEntry JavaDoc( inFileName );
176             entry.setSize( bytes.length );
177             inZipOutputStream.putNextEntry( entry );
178             inZipOutputStream.write( bytes );
179             inZipOutputStream.closeEntry();
180         }
181         catch ( IOException JavaDoc ex)
182         {
183             log.error( ex );
184         }
185     }
186
187     protected String JavaDoc buildMissingDocumentsText( List JavaDoc inMissingDocumentItems )
188     {
189         StringBuffer JavaDoc missingProductsStr = new StringBuffer JavaDoc(
190             "Original documents for the following could not be retrieved:\n\n" );
191         for ( Iterator JavaDoc iter = inMissingDocumentItems.iterator(); iter
192             .hasNext(); )
193         {
194             CollectionItem item = (CollectionItem) iter.next();
195             missingProductsStr.append( " - " );
196             missingProductsStr.append( item.getProduct().getName());
197             missingProductsStr.append( " (" );
198             File JavaDoc path = getArchive().getOriginalDocument( item.getProduct() );
199             if ( path == null )
200             {
201                 missingProductsStr.append( "no file name specified" );
202             }
203             else
204             {
205                 missingProductsStr.append( path.getAbsolutePath() );
206             }
207             missingProductsStr.append( ")\n" );
208         }
209         return missingProductsStr.toString();
210     }
211     
212     
213     public Archive getArchive()
214     {
215         return fieldArchive;
216     }
217
218     public void setArchive(Archive inArchive)
219     {
220         fieldArchive = inArchive;
221     }
222     
223 }
224
Popular Tags