KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > archive > history > HistoryModule


1 /*
2  * Created on Jul 1, 2006
3  */

4 package com.openedit.archive.history;
5
6 import java.text.SimpleDateFormat JavaDoc;
7 import java.util.Calendar JavaDoc;
8 import java.util.Date JavaDoc;
9 import java.util.GregorianCalendar JavaDoc;
10 import java.util.Iterator JavaDoc;
11
12 import com.openedit.OpenEditException;
13 import com.openedit.WebPageRequest;
14 import com.openedit.archive.Archive;
15 import com.openedit.archive.collection.Collection;
16 import com.openedit.archive.collection.CollectionItem;
17 import com.openedit.modules.BaseModule;
18 import com.openedit.modules.archive.ArchiveModule;
19 import com.openedit.store.Product;
20 import com.openedit.store.StoreException;
21
22 public class HistoryModule extends BaseModule
23 {
24     public Archive getArchive( WebPageRequest inReq ) throws OpenEditException
25     {
26         Archive archive = (Archive) inReq.getPageValue( Archive.ARCHIVE_PARAM );
27         if( archive == null)
28         {
29             ArchiveModule mod = (ArchiveModule)getModule("ArchiveModule");
30             archive = mod.getArchive(inReq);
31         }
32         return archive;
33     }
34     
35     public HistoryArchive getHistoryArchive(WebPageRequest inReq) throws OpenEditException
36     {
37         String JavaDoc name = inReq.getPage().get("HistoryArchiveName");
38         if( name == null)
39         {
40             name = "historyArchive";
41         }
42         HistoryArchive history = (HistoryArchive)getBeanFactory().getBean(name);
43         return history;
44     }
45
46     public UsageHistory getUsageHistory( WebPageRequest inReq) throws Exception JavaDoc
47     {
48         String JavaDoc produid = inReq.getRequestParameter("productid");
49         if (produid == null)
50         {
51             Product product = (Product)inReq.getPageValue("product");
52             produid = product.getId();
53         }
54         Archive archive = getArchive(inReq);
55         UsageHistory history = getHistoryArchive(inReq).getUsageHistory(archive, produid);
56         inReq.putPageValue("usagehistory", history);
57         return history;
58     }
59         
60     public void saveUsageHistoryForCollection(WebPageRequest inReq) throws Exception JavaDoc
61     {
62         
63         //String catIdRecId = inReq.getRequestParameter("cIiI");
64
String JavaDoc isfpos = inReq.getRequestParameter("isfpo");
65         String JavaDoc username = inReq.getRequestParameter("username");
66         //String access = inReq.getRequestParameter("access");
67
String JavaDoc use = inReq.getRequestParameter("use");
68         String JavaDoc use2 = inReq.getRequestParameter("use2");
69         //String proj = inReq.getRequestParameter("proj");
70
//String nm = inReq.getRequestParameter("nm");
71
String JavaDoc formID = inReq.getRequestParameter("formID");
72         String JavaDoc comments = inReq.getRequestParameter("comments");
73         //String colorCor = inReq.getRequestParameter("colorCor");
74
String JavaDoc restrictdays = inReq.getRequestParameter("rdays");
75     // String m_sQuery = "";
76

77         boolean isfpo = Boolean.parseBoolean(isfpos);
78         
79         saveRecord(inReq, username, use, use2, formID, comments, restrictdays, isfpo);
80         
81         inReq.putPageValue("isfpo", String.valueOf(isfpo));
82         
83         //TODO: If its final then add a restriction on this record. Needs to save the flag and date into the index?
84
//TODO: Make sync by-directional?
85
//Store data outside of the product data? Open a connection and save it both places?
86

87         /*
88         com.canto.cumulus.web.pub.RecordView view = (com.canto.cumulus.web.pub.RecordView) session.getAttribute("CollBasket_DetailsView");
89
90         com.canto.cumulus.web.pub.RecordCollection logcoll = view.getCursor().getCollection();
91         int pics = logcoll.countRecords();
92         FileOutputStream logfile = new FileOutputStream(path + "/" + "download.log", true);
93         String logstr = theDate + ","+ ac + "," + String.valueOf(ac)+"basket"+basket+".txt" + "," + String.valueOf(pics) + "," + DLtype + "\r\n";
94         for (int a=0; a < logstr.length(); a++) {
95             logfile.write(logstr.charAt(a));
96         }
97         logfile.close();
98
99                 if(String.valueOf(DLtype).equals("###Final")){
100                     FileOutputStream ffo = new FileOutputStream(path + "/" + ac + "basket" + basket + ".txt");
101 // log("Finals: " + path + "/" + ac + "basket" + basket + ".txt");
102
103                     String str = "";
104                     for (int a=0;a<str.length();a++) {
105                        ffo.write(str.charAt(a));
106                     }
107                 }
108
109                 if(view != null){
110                     com.canto.cumulus.web.pub.RecordCollection coll = view.getCursor().getCollection();
111                     int cnt = coll.countRecords();
112                         out.println(String.valueOf(cnt));
113 // log ("No. of records#1: " + String.valueOf(cnt));
114                         int arrRec[] = new int[cnt];
115
116             
117
118                         Record m_currentRecord1;
119                         Fields fldsTest1;
120                         Field fldTest1;
121                         //Field fldTest2;
122                         for(int ci = 0; ci<arrRec.length;ci++){
123                             m_currentRecord1 = coll.getRecord(ci);
124                             fldsTest1 = m_currentRecord1.getFields();
125                             fldTest1 = fldsTest1.getField("Series");
126                             //fldTest2 = fldsTest1.getField("Record Name");
127                             //System.out.println(fldTest2);
128                             try{
129                                 if(ci == 0 && arrRec.length == 1){
130                                     m_sQuery += "Series,2,"+fldTest1.getValue()+",18";
131                                     out.println(m_sQuery);
132                                 }else if(ci == arrRec.length - 1){
133                                     m_sQuery += "Series,2,"+fldTest1.getValue()+",18";
134                                     out.println(m_sQuery);
135                                 }else{
136                                     m_sQuery += "Series,2,"+fldTest1.getValue()+",18,";
137                                     out.println(m_sQuery);
138                                 }
139                             }catch (Exception e){
140                             }
141
142                             if(String.valueOf(DLtype).equals("###Final")){
143                                 coll.removeRecord(0, arrRec[ci]);
144                             }
145                         } // end for
146
147                 } // end if(view != null)
148                 
149                 com.canto.cumulus.web.pub.RecordView view2 = (com.canto.cumulus.web.pub.RecordView) session.getAttribute("SearchResult_DetailsView");
150                 com.canto.cumulus.web.pub.RecordCollection coll2 = view2.getCursor().getCollection();
151                 int cnt2 = coll2.countRecords();
152                 out.println(String.valueOf(cnt2)+"<br>");
153 // log ("Count#2 = " + String.valueOf(cnt2));
154                 Locale m_locale = Locale.getDefault();
155                 String m_sQueryItems[] = m_sQuery.split(",");
156
157                 int combine = 0;
158                 coll2.search(m_sQueryItems, combine, m_locale);
159                 //response.sendRedirect("frameset.jsp?ac="+ac+"&arr="+arr+"&flag=1");
160                 cnt2 = coll2.countRecords();
161 // log ("2ndCount#2 = " + String.valueOf(cnt2));
162                 Record m_currentRecord;
163                 Fields fldsTest;
164                 Field fldTest;
165                 String strFieldVal;
166                 Date rdDate = new Date();
167                 Date restrictrelease = new Date();
168                 long oneday;
169                 long offset;
170                 long iRestrictDays = Long.parseLong(restrictdays);
171
172                 if(String.valueOf(DLtype).equals("Final")){
173                     oneday = 24 * 60 * 60 * 1000;
174                     offset = oneday * iRestrictDays;
175                     restrictrelease = new Date(rdDate.getTime() + offset);
176                 } // end if(String.valueOf(DLtype).equals("Final"))
177
178                 for(int ci2 = 0; ci2 < cnt2; ci2++){
179                     m_currentRecord = coll2.getRecord(ci2);
180                     fldsTest = m_currentRecord.getFields();
181                     
182                     fldTest = fldsTest.getField("UsageHistory");
183                     //strFieldVal = fldTest.getValue();
184                     //out.println("|"+fldTest.getValue()+"|");
185                     try{
186                         fldTest.setValue(fldTest.getValue() + vHistory);
187                     }catch (Exception e){
188                         fldTest.setValue(vHistory);
189                     }
190                     
191                     fldTest = fldsTest.getField("Restriction Release Date");
192                     try{
193                         fldTest.setValue(restrictrelease);
194                     }catch (Exception e){
195                         fldTest.setValue(restrictrelease);
196                     }
197                     
198                     m_currentRecord.save();
199                 } // end for loop
200                 
201             } // End else
202
203             com.canto.cumulus.web.pub.RecordView view3 = (com.canto.cumulus.web.pub.RecordView) session.getAttribute("CollBasket_DetailsView");
204                 if(view3 != null){
205                     com.canto.cumulus.web.pub.RecordCollection coll3 = view3.getCursor().getCollection();
206                     int cnt3 = coll3.countRecords();
207                         out.println(String.valueOf(cnt3));
208 // log ("count3 = " + String.valueOf(cnt3));
209                         int arrRec3[] = new int[cnt3];
210                         for(int ci3 = 0; ci3 < cnt3; ci3++){
211                             Record m_currentRecord3 = coll3.getRecord(ci3);
212                             int thaID3 = m_currentRecord3.getID();
213                             arrRec3[ci3] = thaID3;
214                         } // end for loop
215                         for(int ci = 0; ci < arrRec3.length; ci++){
216                             if(String.valueOf(DLtype).equals("###Final")){
217                                 coll3.removeRecord(7, arrRec3[ci]);
218                                 out.println("removed ** frame.jsp<br>");
219                             }
220                         } // end for loop
221                 } // end if (view3 != null)
222 */

223     }
224
225     protected void saveRecord(WebPageRequest inReq, String JavaDoc username, String JavaDoc use, String JavaDoc use2,
226         String JavaDoc formID, String JavaDoc comments, String JavaDoc restrictdays, boolean isfpo)
227         throws OpenEditException, StoreException
228     {
229         SimpleDateFormat JavaDoc format = new SimpleDateFormat JavaDoc("dd/MM/yyyy");
230         Date JavaDoc nowDate = new Date JavaDoc();
231         
232         String JavaDoc nowString = format.format(nowDate);//(String.valueOf(theDate.getMonth()).length() == 1 ? "0" : "") + (theDate.getMonth() + 1) + "/" + (String.valueOf(theDate.getDate()).length() == 1 ? "0" : "") + theDate.getDate() + "/" + (theDate.getYear() + 1900);
233
//String vHistory = use + "\t" + use2 + "\t" + comments + "\t" + nm + "\t" + dt + "\t" + formID + "\t" + DLtype + "\r";
234

235         Calendar JavaDoc releaseCalendar = new GregorianCalendar JavaDoc();
236         releaseCalendar.add(Calendar.DAY_OF_YEAR, Integer.parseInt(restrictdays));
237         String JavaDoc releaseString = format.format(releaseCalendar.getTime());
238         
239         Archive archive = getArchive(inReq);
240
241         Collection col = archive.getUserCollections().getSelectedCollection();
242         
243         for (Iterator JavaDoc iter = col.getCollectionItems().iterator(); iter.hasNext();)
244         {
245             CollectionItem item = (CollectionItem) iter.next();
246             Product product = item.getProduct();
247             
248             UsageHistory history = getHistoryArchive(inReq).getUsageHistory(archive, product.getId());
249             HistoryRecord record = history.createNewRecord();
250             //String vHistory = use + "\t" + use2 + "\t" + comments + "\t" + nm + "\t" + dt + "\t" + formID + "\t" + DLtype + "\r";
251
record.putData("date", nowString);
252             record.putData("description", comments);
253             record.putData("audience", use);
254             record.putData("typeofuse", use2);
255             record.putData("username", username);
256             record.putData("formnum", formID);
257             record.putData("isfpo", String.valueOf(isfpo));
258             history.addRecord(record);
259             getHistoryArchive(inReq).save(archive, history);
260             
261         
262             product.putAttribute("Restricted", "true");
263             product.putAttribute("Restriction Release Date", releaseString);
264
265             StringBuffer JavaDoc recordlog = new StringBuffer JavaDoc();
266             for (Iterator JavaDoc iterator = history.getRecordData(); iterator.hasNext();) {
267                 HistoryRecord tolog = (HistoryRecord) iterator.next();
268                 recordlog.append(nowString);
269                 recordlog.append("\t");
270                 recordlog.append(comments);
271                 recordlog.append("\t");
272                 recordlog.append(use);
273                 recordlog.append("\t");
274                 recordlog.append(use2);
275                 recordlog.append("\t");
276                 recordlog.append(username);
277                 recordlog.append("\t");
278                 recordlog.append(formID);
279                 recordlog.append("\t");
280                 recordlog.append(String.valueOf(isfpo));
281                 recordlog.append("\r\n");
282                 
283             }
284             product.putAttribute("Version History", recordlog.toString());
285             getArchive(inReq).getStore().saveProduct(product);
286             
287         }
288     }
289 }
290
Popular Tags