KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Created on Mar 28, 2006
3  */

4 package com.openedit.archive.cumulus;
5
6 import java.io.BufferedReader JavaDoc;
7 import java.io.File JavaDoc;
8 import java.io.FileNotFoundException JavaDoc;
9 import java.io.FileReader JavaDoc;
10 import java.io.FilenameFilter JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.util.List JavaDoc;
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 JavaDoc inStatus) throws Exception JavaDoc
29     {
30         boolean reindex = deleteFromLogs(inStore, inStatus);
31         return reindex;
32     }
33     protected boolean deleteFromLogs(Store inStore, List JavaDoc inStatus) throws Exception JavaDoc
34     {
35         Archive fieldArchive = new Archive();
36         fieldArchive.setPageManager(getPageManager());
37         fieldArchive.setStore( inStore );
38
39         //fieldArchive.setSetting(null); ///causes a reload
40

41         //list all the files in the logs dir
42
String JavaDoc dir = fieldArchive.getSettings().getChildValue("deletelogs-directory");
43         boolean reindex = false;
44         if ( dir != null)
45         {
46             File JavaDoc[] all = new File JavaDoc( dir ).listFiles(new FilenameFilter JavaDoc()
47                 {
48                 public boolean accept(File JavaDoc inDir, String JavaDoc 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 JavaDoc filelog = all[i];
61                 
62                 reindex = processLog(reindex, filelog, fieldArchive, inStatus);
63                 
64                 File JavaDoc copy = new File JavaDoc( filelog.getParentFile(), "old/" + filelog.getName() );
65                 
66                 new FileUtils().copyFiles(filelog, copy);
67                 
68                 filelog.delete(); //this may not delete it if it is still open for writing
69
}
70         }
71         return reindex;
72     }
73     protected boolean processLog(boolean reindex, File JavaDoc inLog, Archive inArchive, List JavaDoc inStatus) throws FileNotFoundException JavaDoc, IOException JavaDoc, Exception JavaDoc, StoreException
74     {
75         BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new FileReader JavaDoc(inLog));
76         try
77         {
78             String JavaDoc line = reader.readLine();
79             int count = 0;
80             while ( line != null)
81             {
82                 if( count > 5)
83                 {
84                     reindex = true;
85                 }
86                 String JavaDoc[] tabs = line.split("\t");
87                 if ( tabs.length > 3)
88                 {
89                     if ( "Record deleted".equals( tabs[3] ) )
90                     {
91                         String JavaDoc catName = inLog.getName();
92                         catName = catName.substring(0,catName.indexOf('-'));
93                         //catName = extractId(catName);
94
//Category root = inArchive.getStore().getCatalogArchive().getRootCatalog().getChildByName(catName);
95
String JavaDoc 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 JavaDoc id = hit.get("id");
102                             //String id = findProductId(tabs[5],tabs[4],catName);
103
Product product = inArchive.getStore().getProduct(id);
104                             if ( product != null)
105                             {
106                                 String JavaDoc loc = inArchive.getStore().getProductPathFinder().idToPath(product.getId());
107                                 
108                                 File JavaDoc thumb = new File JavaDoc( inArchive.getStore().getRootDirectory(),inArchive.getStore().getStoreHome() + "/products/images/thumb/" + loc + ".jpg");
109                                 thumb.delete();
110                                 File JavaDoc med = new File JavaDoc( 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     /*
146     protected String findProductId(String inName, String inRecordId, String inRootCatName) throws Exception
147     {
148         ServerCatalog cat = getServerCatalogs().getServerCatalog(inRootCatName);
149         CatalogCollection col = cat.open();
150         Record record = col.getRecords().getRecordByID(Integer.parseInt( inRecordId ) );
151         Fields field = record.getFields();
152         String name = getValue(field.getField("Asset Name") );
153         String assid = getValue(field.getField("Asset Identifier"));
154
155         
156         return extractProductId(name, assid);
157     }
158 */

159     
160
161 }
162
Popular Tags