1 4 package com.openedit.archive.cumulus; 5 6 import java.io.BufferedReader ; 7 import java.io.File ; 8 import java.io.FileNotFoundException ; 9 import java.io.FileReader ; 10 import java.io.FilenameFilter ; 11 import java.io.IOException ; 12 import java.util.List ; 13 14 import org.apache.commons.logging.Log; 15 import org.apache.commons.logging.LogFactory; 16 import org.apache.lucene.document.Document; 17 18 import com.openedit.archive.Archive; 19 import com.openedit.modules.search.LuceneHitTracker; 20 import com.openedit.store.Product; 21 import com.openedit.store.StoreException; 22 import com.openedit.store.Store; 23 import com.openedit.util.FileUtils; 24 25 public class CumulusLogConverter extends BaseCumulusConvert 26 { 27 private static final Log log = LogFactory.getLog(CumulusLogConverter.class); 28 public boolean convert(Store inStore, List inStatus) throws Exception 29 { 30 boolean reindex = deleteFromLogs(inStore, inStatus); 31 return reindex; 32 } 33 protected boolean deleteFromLogs(Store inStore, List inStatus) throws Exception 34 { 35 Archive fieldArchive = new Archive(); 36 fieldArchive.setPageManager(getPageManager()); 37 fieldArchive.setStore( inStore ); 38 39 41 String dir = fieldArchive.getSettings().getChildValue("deletelogs-directory"); 43 boolean reindex = false; 44 if ( dir != null) 45 { 46 File [] all = new File ( dir ).listFiles(new FilenameFilter () 47 { 48 public boolean accept(File inDir, String inName) 49 { 50 return inName.toLowerCase().endsWith(".log"); 51 } 52 }); 53 54 if ( all == null) 55 { 56 return false; 57 } 58 for (int i = 0; i < all.length; i++) 59 { 60 File filelog = all[i]; 61 62 reindex = processLog(reindex, filelog, fieldArchive, inStatus); 63 64 File copy = new File ( filelog.getParentFile(), "old/" + filelog.getName() ); 65 66 new FileUtils().copyFiles(filelog, copy); 67 68 filelog.delete(); } 70 } 71 return reindex; 72 } 73 protected boolean processLog(boolean reindex, File inLog, Archive inArchive, List inStatus) throws FileNotFoundException , IOException , Exception , StoreException 74 { 75 BufferedReader reader = new BufferedReader (new FileReader (inLog)); 76 try 77 { 78 String line = reader.readLine(); 79 int count = 0; 80 while ( line != null) 81 { 82 if( count > 5) 83 { 84 reindex = true; 85 } 86 String [] tabs = line.split("\t"); 87 if ( tabs.length > 3) 88 { 89 if ( "Record deleted".equals( tabs[3] ) ) 90 { 91 String catName = inLog.getName(); 92 catName = catName.substring(0,catName.indexOf('-')); 93 String recordId = tabs[4]; 96 log.info("Removing " + recordId ); 97 LuceneHitTracker hits = inArchive.getStore().search("cumulusid:" + catName + "_" + recordId); 98 if( hits.getTotal() > 0) 99 { 100 Document hit = (Document)hits.get(0); 101 String id = hit.get("id"); 102 Product product = inArchive.getStore().getProduct(id); 104 if ( product != null) 105 { 106 String loc = inArchive.getStore().getProductPathFinder().idToPath(product.getId()); 107 108 File thumb = new File ( inArchive.getStore().getRootDirectory(),inArchive.getStore().getStoreHome() + "/products/images/thumb/" + loc + ".jpg"); 109 thumb.delete(); 110 File med = new File ( inArchive.getStore().getRootDirectory(),inArchive.getStore().getStoreHome() + "/products/images/medium/" + loc + ".jpg"); 111 med.delete(); 112 113 if( reindex ) 114 { 115 inArchive.getStore().getProductArchive().deleteProduct(product); 116 } 117 else 118 { 119 inArchive.getStore().getStoreSearcher().deleteFromIndex(product); 120 inArchive.getStore().getProductArchive().deleteProduct(product); 121 } 122 count++; 123 } 124 } 125 else 126 { 127 log.debug("No record found " + catName + "dash" + recordId); 128 } 129 } 130 } 131 line = reader.readLine(); 132 } 133 if( count > 0) 134 { 135 inStatus.add("Removed " + count + " records"); 136 } 137 } 138 finally 139 { 140 FileUtils.safeClose( reader ); 141 } 142 return reindex; 143 } 144 145 159 160 161 } 162 | Popular Tags |