1 4 package com.openedit.archive.cumulus; 5 6 import java.io.File ; 7 import java.io.FileOutputStream ; 8 import java.io.FileReader ; 9 import java.io.FilenameFilter ; 10 import java.util.HashSet ; 11 import java.util.Iterator ; 12 import java.util.List ; 13 import java.util.Set ; 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 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 inErrorLog) throws Exception 45 { 46 File inputdir = new File (inStore.getStoreDirectory(), "/upload/" ); 48 File [] inputs = inputdir.listFiles(new FilenameFilter () { 49 public boolean accept(File dir, String 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; 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 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 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 input, List inLog, Archive inArchive) throws Exception 93 { 94 inLog.add("Loading " + input.getName()); 95 log.info("processing... " + input); 96 97 Set toDelete = listProductsToDelete(input.getName(), inArchive); 98 99 ImportFile inputf = new ImportFile(); 100 inputf.load(new FileReader (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 backup = new File (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 inToDelete, Archive inArchive) throws Exception 133 { 134 for (Iterator iter = inToDelete.iterator(); iter.hasNext();) 135 { 136 String id = (String ) 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 listProductsToDelete(String inName, Archive inArchive) throws Exception 145 { 146 String catid = extractId( PathUtilities.extractPageName( inName ) ); 147 LuceneHitTracker hits = inArchive.getStore().getStoreSearcher().search("catalogs:" + catid, null); HashSet set = new HashSet (); 149 for (int i = 0; i < set.size(); i++) 150 { 151 Document doc = (Document)hits.get(i); 152 String pid = doc.get("id"); 153 set.add(pid); 154 } 155 return set; 156 } 157 protected Product createProduct(Row row, Archive inArchive) throws Exception 158 { 159 String 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(); 166 String assid = row.getData("Asset Identifier"); 167 168 String id = extractProductId(name , assid ); 170 newproduct.setId(id); 171 newproduct.addProperty("assetidentifier", assid); 172 173 convertOriginalLink(newproduct,row); 174 176 newproduct.setName(name); 177 convertCatalogs(row, newproduct, inArchive); 178 179 for (Iterator 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 val = row.getData(detail.getId()); 186 if ( val != null) 187 { 188 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 folderName = inRow.getData("Windows Folder Names" ); 212 if ( folderName == null) 213 { 214 folderName = inRow.getData("Folder Name" ); 215 } 216 String server = inRow.getData("Windows File Server Name"); 217 if ( server == null) 218 { 219 server = inRow.getData("Server Name"); 220 } 221 222 String 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) { 229 folderName = "\\\\" + server + "\\" + volume + "\\" + folderName; 230 } 231 236 String fileName = inRow.getData("Asset Name"); 237 String displaypath = folderName + "\\" + fileName; 239 inNewproduct.putAttribute("originalpath",displaypath ); 240 241 } 242 private void createImages(Row row, Product newproduct, Archive inArchive) throws Exception 243 { 244 Store store = inArchive.getStore(); 246 ProductPathFinder finder = store.getProductPathFinder(); 247 String loc = finder.idToPath(newproduct.getId()); 248 String path = inArchive.getStore().getStoreHome() + "/products/images/medium/" + loc + ".jpg"; 249 File out = new File ( inArchive.getStore().getRootDirectory(),path); 250 if ( !out.exists() ) 251 { 252 String code = row.getData("Thumbnail"); 253 String junk = "5444617442797465"; int cut = code.indexOf(junk); 255 code = code.substring(cut+junk.length()); 256 if ( code.startsWith("FFD8FFE0")) { byte[] bytes = new HexToBinaryConverter().hexToBinary( code ); 259 out.getParentFile().mkdirs(); 260 FileOutputStream fout = new FileOutputStream (out.getAbsoluteFile()); 261 try 262 { 263 fout.write(bytes); 264 } 265 finally 266 { 267 FileUtils.safeClose(fout); 268 } 269 } 270 File copy = new File ( 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 String [] categories = row.getRemainder(); 283 newproduct.clearCatalogs(); 284 for (int j = 0; j < categories.length; j++) 285 { 286 String [] 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 catalogParent = pairs[i].trim(); 293 294 if ( catalogParent.equalsIgnoreCase("$Keywords") ) 295 { 296 i++; newproduct.addKeyword(pairs[i] ); 298 continue; 299 } 300 if ( catalogParent.startsWith("$")) { 302 catalogParent = catalogParent.substring(1); 303 } 304 if (catalogParent.length() == 0 || 305 catalogParent.equalsIgnoreCase("Sources") || 306 catalogParent.equalsIgnoreCase("PDRKOPCML01") || catalogParent.equalsIgnoreCase("Pdrmakopcml01") || 308 catalogParent.equalsIgnoreCase("YB Image Library") 309 ) 310 { 311 continue; } 313 316 newproduct.addKeyword(catalogParent ); 317 318 String catalogParentId = extractId(catalogParent); 320 321 String pid = null; 322 if ( oldParent != null ) 323 { 324 pid = oldParent.getId() + catalogParentId; } 326 else 327 { 328 pid = catalogParentId; } 330 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); inArchive.getStore().getCatalogArchive().addCatalog(parent); oldParent = parent; 348 } 349 } 350 } 351 } | Popular Tags |