KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > archive > cumulus > CumulusConverter


1 /*
2  * Created on Sep 15, 2004
3  */

4 package com.openedit.archive.cumulus;
5
6 import java.io.File JavaDoc;
7 import java.io.FileOutputStream JavaDoc;
8 import java.io.FileReader JavaDoc;
9 import java.io.FilenameFilter JavaDoc;
10 import java.util.HashSet JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.List JavaDoc;
13 import java.util.Set JavaDoc;
14
15 import org.apache.commons.logging.Log;
16 import org.apache.commons.logging.LogFactory;
17 import org.apache.lucene.document.Document;
18
19 import com.openedit.archive.Archive;
20 import com.openedit.archive.ImageMaker;
21 import com.openedit.modules.search.LuceneHitTracker;
22 import com.openedit.store.Category;
23 import com.openedit.store.Product;
24 import com.openedit.store.ProductPathFinder;
25 import com.openedit.store.StoreException;
26 import com.openedit.store.Store;
27 import com.openedit.store.products.Detail;
28 import com.openedit.util.FileUtils;
29 import com.openedit.util.OutputFiller;
30 import com.openedit.util.PathUtilities;
31
32 /**
33  * A catalog converter that converts Cumulus tab separated values and images into products
34  * @author cburkey
35  */

36 public class CumulusConverter extends BaseCumulusConvert
37 {
38     protected OutputFiller fieldOutputFiller;
39     private static final Log log = LogFactory.getLog(CumulusConverter.class);
40
41     public CumulusConverter()
42     {
43     }
44     public synchronized boolean convert(Store inStore, List JavaDoc inErrorLog) throws Exception JavaDoc
45     {
46         //Set the read
47
File JavaDoc inputdir = new File JavaDoc(inStore.getStoreDirectory(), "/upload/" );
48         File JavaDoc[] inputs = inputdir.listFiles(new FilenameFilter JavaDoc() {
49             public boolean accept(File JavaDoc dir, String JavaDoc name)
50             {
51                 return name.toLowerCase().endsWith(".cre");
52             }
53         });
54         
55         if (inputs == null || inputs.length == 0)
56         {
57             log.debug("input does not exist, skipping conversion");
58             return false; //do nothing
59

60         }
61         inErrorLog.add("Starting Cumulus inventory import");
62
63         Archive fieldArchive = new Archive();
64         fieldArchive.setStore( inStore );
65         fieldArchive.setPageManager(getPageManager());
66         
67         for (int i = 0; i < inputs.length; i++)
68         {
69             processFile(inputs[i],inErrorLog, fieldArchive);
70         }
71         
72         
73         String JavaDoc cache = fieldArchive.getSettings().getChildValue("cacheoriginals");
74         if ( Boolean.parseBoolean(cache))
75         {
76             ImageMaker maker = new ImageMaker();
77             maker.setArchive(fieldArchive);
78             try
79             {
80                 maker.backUpOriginals();
81             }
82             catch ( Exception JavaDoc ex)
83             {
84                 inErrorLog.add("ERROR: Could not create originals cache " + ex);
85                 log.error(ex);
86             }
87         }
88         return true;
89     }
90
91
92     private void processFile(File JavaDoc input, List JavaDoc inLog, Archive inArchive) throws Exception JavaDoc
93     {
94         inLog.add("Loading " + input.getName());
95         log.info("processing... " + input);
96         
97         Set JavaDoc toDelete = listProductsToDelete(input.getName(), inArchive);
98
99         ImportFile inputf = new ImportFile();
100         inputf.load(new FileReader JavaDoc(input));
101         try
102         {
103             Row row = inputf.getNextRow();
104             int count = 0;
105             int total = 0;
106             while ( row != null)
107             {
108                 Product prod = createProduct(row, inArchive);
109                 toDelete.remove(prod.getId());
110                 row = inputf.getNextRow();
111                 count++;
112                 if ( count > 1000)
113                 {
114                     total += count;
115                     log.info("Exported " + total);
116                     count = 0;
117                     inArchive.getStore().getCatalogArchive().saveCatalogs();
118                 }
119             }
120         } finally
121         {
122             inputf.close();
123         }
124         inArchive.getStore().getCatalogArchive().saveCatalogs();
125         File JavaDoc backup = new File JavaDoc(inArchive.getStore().getRootDirectory(),"upload/completed/" + input.getName() );
126         backup.getParentFile().mkdir();
127         input.renameTo(backup);
128         
129         deleteProductsById(toDelete, inArchive);
130     }
131
132     protected void deleteProductsById(Set JavaDoc inToDelete, Archive inArchive) throws Exception JavaDoc
133     {
134         for (Iterator JavaDoc iter = inToDelete.iterator(); iter.hasNext();)
135         {
136             String JavaDoc id = (String JavaDoc) iter.next();
137             Product prod = inArchive.getStore().getProduct(id);
138             if ( prod != null)
139             {
140                 inArchive.getStore().getProductArchive().deleteProduct(prod);
141             }
142         }
143     }
144     protected Set JavaDoc listProductsToDelete(String JavaDoc inName, Archive inArchive) throws Exception JavaDoc
145     {
146         String JavaDoc catid = extractId( PathUtilities.extractPageName( inName ) );
147         LuceneHitTracker hits = inArchive.getStore().getStoreSearcher().search("catalogs:" + catid, null); //this would be the top level catalog
148
HashSet JavaDoc set = new HashSet JavaDoc();
149         for (int i = 0; i < set.size(); i++)
150         {
151             Document doc = (Document)hits.get(i);
152             String JavaDoc pid = doc.get("id");
153             set.add(pid);
154         }
155         return set;
156     }
157     protected Product createProduct(Row row, Archive inArchive) throws Exception JavaDoc
158     {
159         String JavaDoc name = row.getData("Asset Name");
160         if ( name.equalsIgnoreCase("thumbs.db") || name.equalsIgnoreCase("desktop.ini"))
161         {
162             return null;
163         }
164         Product newproduct = new Product();//inArchive.getStore().getProduct(id);
165

166         String JavaDoc assid = row.getData("Asset Identifier");
167
168         //need to get the second to top catalog
169
String JavaDoc id = extractProductId(name , assid );
170         newproduct.setId(id);
171         newproduct.addProperty("assetidentifier", assid);
172
173         convertOriginalLink(newproduct,row);
174         //newproduct.setOrdering(i); //natual ordering
175

176         newproduct.setName(name);
177         convertCatalogs(row, newproduct, inArchive);
178
179         //misc fields
180
for (Iterator JavaDoc iter = inArchive.getStore().getProductArchive().getPropertyDetails().getDetails().iterator(); iter.hasNext();)
181         {
182             Detail detail = (Detail ) iter.next();
183             if( detail.isKeyword() || detail.isStored() )
184             {
185                 String JavaDoc val = row.getData(detail.getId());
186                 if ( val != null)
187                 {
188 // if ( detail.isDate() && val.length() > 5)
189
// {
190
// Date date = fieldCumulusSyncFormat.parse(val);
191
// val = detail.getDateFormat().format( date );
192
// }
193
if( detail.isStored() )
194                     {
195                         newproduct.addProperty(detail.getId(),val);
196                     }
197                 }
198             }
199         }
200         
201         createImages(row, newproduct, inArchive);
202         
203         inArchive.getStore().getProductArchive().saveProduct( newproduct );
204
205         inArchive.getStore().getProductArchive().clearProduct(newproduct);
206         return newproduct;
207     }
208
209     private void convertOriginalLink(Product inNewproduct, Row inRow)
210     {
211         String JavaDoc folderName = inRow.getData("Windows Folder Names" );
212         if ( folderName == null)
213         {
214             folderName = inRow.getData("Folder Name" );
215         }
216         String JavaDoc server = inRow.getData("Windows File Server Name");
217         if ( server == null)
218         {
219             server = inRow.getData("Server Name");
220         }
221         
222         String JavaDoc volume = inRow.getData("Windows Volume Name");
223         if ( volume == null)
224         {
225             volume = inRow.getData("Volume Name");
226         }
227         if ( server != null && server.length() > 0) //we do not support local G:\ imports
228
{
229             folderName = "\\\\" + server + "\\" + volume + "\\" + folderName;
230         }
231         /* if ( File.separatorChar != '\\') //only change in Linux
232          {
233             folderName = folderName.replaceAll( "\\\\", File.separator );
234          }
235         */

236         String JavaDoc fileName = inRow.getData("Asset Name");
237         //inNewproduct.addKeyword(fileName);
238
String JavaDoc displaypath = folderName + "\\" + fileName;
239         inNewproduct.putAttribute("originalpath",displaypath );
240         
241     }
242     private void createImages(Row row, Product newproduct, Archive inArchive) throws Exception JavaDoc
243     {
244         //thumbnails
245
Store store = inArchive.getStore();
246         ProductPathFinder finder = store.getProductPathFinder();
247         String JavaDoc loc = finder.idToPath(newproduct.getId());
248         String JavaDoc path = inArchive.getStore().getStoreHome() + "/products/images/medium/" + loc + ".jpg";
249         File JavaDoc out = new File JavaDoc( inArchive.getStore().getRootDirectory(),path);
250         if ( !out.exists() )
251         {
252             String JavaDoc code = row.getData("Thumbnail");
253             String JavaDoc junk = "5444617442797465"; //some hex junk at the begining of the files
254
int cut = code.indexOf(junk);
255             code = code.substring(cut+junk.length());
256             if ( code.startsWith("FFD8FFE0")) //jpg only
257
{ //other formats are unknown
258
byte[] bytes = new HexToBinaryConverter().hexToBinary( code );
259                 out.getParentFile().mkdirs();
260                 FileOutputStream JavaDoc fout = new FileOutputStream JavaDoc(out.getAbsoluteFile());
261                 try
262                 {
263                     fout.write(bytes);
264                 }
265                 finally
266                 {
267                     FileUtils.safeClose(fout);
268                 }
269             }
270             File JavaDoc copy = new File JavaDoc( inArchive.getStore().getRootDirectory(),inArchive.getStore().getStoreHome() + "/products/images/thumb/" + loc + ".jpg");
271             if (out.exists() && !copy.exists() )
272             {
273                 copy.getParentFile().mkdirs();
274                 new FileUtils( ).fileCopy(out,copy);
275             }
276         }
277     }
278
279     protected void convertCatalogs(Row row, Product newproduct, Archive inArchive) throws StoreException
280     {
281         //take everything off the end and update the catalog XML tree
282
String JavaDoc[] categories = row.getRemainder();
283         newproduct.clearCatalogs();
284         for (int j = 0; j < categories.length; j++)
285         {
286             String JavaDoc[] pairs = categories[j].split(":");
287             Category parent = null;
288
289             Category oldParent = null;
290             for (int i = 0; i < pairs.length; i++)
291             {
292                 String JavaDoc catalogParent = pairs[i].trim();
293                 
294                 if ( catalogParent.equalsIgnoreCase("$Keywords") )
295                 {
296                     i++; //SKIP to next
297
newproduct.addKeyword(pairs[i] );
298                     continue;
299                 }
300                 if ( catalogParent.startsWith("$")) //we can include $Keywords tho
301
{
302                     catalogParent = catalogParent.substring(1);
303                 }
304                 if (catalogParent.length() == 0 ||
305                     catalogParent.equalsIgnoreCase("Sources") ||
306                     catalogParent.equalsIgnoreCase("PDRKOPCML01") || //TODO: Remove these checks
307
catalogParent.equalsIgnoreCase("Pdrmakopcml01") ||
308                     catalogParent.equalsIgnoreCase("YB Image Library")
309                 )
310                 {
311                     continue; //lets start a little lower and skip some large groups
312
}
313                 //TODO: Try out http://www.koders.com/java/fidF83F5AC43A0CF80F83664D3FB590701A62EEC550.aspx for performance
314
//vs http://www.regular-expressions.info/java.html
315

316                 newproduct.addKeyword(catalogParent );
317
318                 //name must be lucene and url friendly
319
String JavaDoc catalogParentId = extractId(catalogParent);
320                 
321                 String JavaDoc pid = null;
322                 if ( oldParent != null )
323                 {
324                     pid = oldParent.getId() + catalogParentId; //give us a unique id
325
}
326                 else
327                 {
328                     pid = catalogParentId; //give us a unique id
329
}
330                 //pid = pid.replaceAll("__","_"); //undo having so many __ in there
331

332                 parent = inArchive.getStore().getCatalog(pid);
333                 if ( parent == null)
334                 {
335                     parent = new Category();
336                     parent.setId(pid);
337                     parent.setName(catalogParent);
338                 }
339                 if ( oldParent == null)
340                 {
341                     oldParent = inArchive.getStore().getCatalogArchive().getRootCatalog();
342                 }
343                 oldParent.addChild(parent);
344                 newproduct.addCatalog(parent);
345                 newproduct.setDefaultCatalog(parent); //the last one wins
346
inArchive.getStore().getCatalogArchive().addCatalog(parent); //put it into the cache
347
oldParent = parent;
348             }
349         }
350     }
351 }
Popular Tags