KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > modules > archive > ArchiveModule


1 package com.openedit.modules.archive;
2
3 import java.util.List JavaDoc;
4 import java.util.Map JavaDoc;
5
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8
9 import com.openedit.OpenEditException;
10 import com.openedit.WebPageRequest;
11 import com.openedit.archive.Archive;
12 import com.openedit.archive.ImageMaker;
13 import com.openedit.archive.authenticate.WindowsAuthentication;
14 import com.openedit.error.EmailErrorHandler;
15 import com.openedit.modules.admin.AdminModule;
16 import com.openedit.modules.admin.filemanager.FileUpload;
17 import com.openedit.modules.cart.StoreDataReader;
18 import com.openedit.modules.search.LuceneHitTracker;
19 import com.openedit.modules.store.BaseStoreModule;
20 import com.openedit.page.Page;
21 import com.openedit.store.Product;
22 import com.openedit.store.Store;
23 import com.openedit.store.StoreException;
24 import com.openedit.users.User;
25 import com.openedit.util.PathUtilities;
26 public class ArchiveModule extends BaseStoreModule
27 {
28     private static final Log log = LogFactory.getLog(ArchiveModule.class);
29     private EmailErrorHandler fieldEmailErrorHandler;
30     public ArchiveModule()
31     {
32     }
33
34     /**
35      * Loads the archive into the session, if it is not already there, and
36      * returns it.
37      *
38      * @param inReq The page request
39      *
40      * @return The archive
41      *
42      * @throws OpenEditException
43      */

44     public Archive getArchive( WebPageRequest inReq ) throws OpenEditException
45     {
46         Store store = getStore(inReq);
47         String JavaDoc id = Archive.ARCHIVE_PARAM + store.getCatalogId() + inReq.getUserName();
48         Archive archive = (Archive) inReq.getSessionValue( id );
49         
50         if ( archive == null )
51         {
52             archive = createArchive( inReq );
53             inReq.putSessionValue( id, archive );
54         }
55         inReq.putPageValue("cataloghome", archive.getCatalogHome() );
56         inReq.putPageValue("store",archive.getStore());
57         inReq.putPageValue("archive",archive);
58         return archive;
59     }
60     /**
61      * Create a new archive and configures it from
62      * <tt>/archive/configuration/archive.xml</tt>, and with other appropriate
63      * information such as the store and the archive root directory.
64      *
65      * @param inRequest The web page request
66      *
67      * @throws OpenEditException
68      */

69     protected Archive createArchive( WebPageRequest inRequest ) throws OpenEditException
70     {
71         Archive archive = new Archive();
72         archive.setUser(inRequest.getUser());
73         archive.setStore( getStore( inRequest ) );
74         archive.setPageManager(getPageManager());
75         archive.setEmailErrorHandler(fieldEmailErrorHandler);
76         archive.reladSettings();
77         return archive;
78     }
79
80     /**
81      * Updates JPEG and TIFF image headers with metadata information
82      *
83      * @param inRequest The web page request
84      */

85     public void updateHeaders( WebPageRequest inRequest )
86         throws OpenEditException
87     {
88         Archive archive = getArchive( inRequest );
89         archive.updateHeaders();
90     }
91 // protected Cart getCart( WebPageRequest inRequest ) throws OpenEditException
92
// {
93
// CartModule cartModule = (CartModule) getModuleManager().getModule(
94
// "CartModule" );
95
// return cartModule.getCart( inRequest );
96
// }
97
public void login(WebPageRequest inReq) throws OpenEditException
98     {
99         String JavaDoc account = inReq.getRequestParameter("accountname");
100
101         if ( account != null )
102         {
103             User user = getUserManager().getUser( account );
104             String JavaDoc password = inReq.getRequestParameter("password");
105
106             if( user == null || !getUserManager().authenticate(user, password) )
107             {
108                 String JavaDoc domain = inReq.getRequestParameter("domain");
109                 //check for NT password
110
WindowsAuthentication nt = new WindowsAuthentication();
111                 
112                 if( !nt.login(account,password, domain) )
113                 {
114                     inReq.putPageValue("oe-exception","Could not log into Windows domain as user " + account);
115                     return;
116                 }
117                 if( user == null)
118                 {
119                     
120                     String JavaDoc groupname = inReq.getPage().get("autologingroup");
121                     if( groupname != null)
122                     {
123                         log.info("Creating " + groupname + " user " + account);
124                         user = getUserManager().createGuestUser(account, password, groupname);
125                     }
126                     else
127                     {
128                         inReq.putPageValue("oe-exception","No autologingroup group configured");
129                         return;
130                     }
131                 }
132             }
133             inReq.putSessionValue( "user", user );
134
135             AdminModule umodule = (AdminModule)getModule("Admin");
136             umodule.savePasswordAsCookie(user,inReq);
137             String JavaDoc sendTo = inReq.getRequestParameter("loginokpage");
138
139             if( sendTo == null)
140             {
141                 String JavaDoc sendToOld = (String JavaDoc)inReq.getSessionValue("originalEntryPage");
142                 String JavaDoc referrer = inReq.getRequest().getHeader("REFERER");
143                 if ( sendToOld != null && !sendToOld.equals(referrer)) //Make sure no loops
144
{
145                     sendTo = sendToOld;
146                 }
147                 inReq.removeSessionValue( "originalEntryPage" );
148             }
149             
150             inReq.redirect(sendTo);
151         }
152         
153     }
154     public void createLogin(WebPageRequest inReq) throws OpenEditException
155     {
156         //sales people get nothing
157
//production people get everything
158
String JavaDoc username = inReq.getCurrentAction().getChildValue("username");
159         if ( username == null)
160         {
161             username = "production";
162         }
163         
164         User user = getUserManager().getUser(username);
165         
166         AdminModule umodule = (AdminModule)getModule("Admin");
167         umodule.savePasswordAsCookie(user,inReq);
168         inReq.putSessionValue("user",user);
169         inReq.removeSessionValue("catalogTree"); //to reload it
170
}
171     public void createThumbAndMedium(WebPageRequest inReq) throws Exception JavaDoc
172     {
173         ImageMaker maker = new ImageMaker();
174         maker.setArchive(getArchive(inReq));
175         maker.run();
176         
177     }
178     
179     public void voteUp(WebPageRequest inReq) throws Exception JavaDoc
180     {
181         String JavaDoc id = inReq.getRequestParameter("productid");
182         if ( id != null)
183         {
184             Archive archive = getArchive(inReq);
185             Product product = archive.getProduct(id);
186             String JavaDoc vote = product.get("votecount");
187             int val = 1;
188             if ( vote != null)
189             {
190                 val = Integer.parseInt(vote);
191                 val++;
192             }
193             product.putAttribute("votecount",String.valueOf( val) );
194             archive.getStore().saveProduct(product);
195             reloadSearch(inReq, archive);
196             
197         }
198     }
199
200     public void voteDown(WebPageRequest inReq) throws Exception JavaDoc
201     {
202         String JavaDoc id = inReq.getRequestParameter("productid");
203         if ( id != null)
204         {
205             Archive archive = getArchive(inReq);
206             Product product = archive.getProduct(id);
207             String JavaDoc vote = product.get("votecount");
208             int val = 0;
209             if ( vote != null)
210             {
211                 val = Integer.parseInt(vote);
212                 val--;
213             }
214             if ( val < 0)
215             {
216                 val = 0;
217             }
218             product.putAttribute("votecount",String.valueOf( val) );
219             archive.getStore().saveProduct(product);
220             
221             reloadSearch(inReq, archive);
222         }
223     }
224
225     protected void reloadSearch(WebPageRequest inReq, Archive archive) throws StoreException
226     {
227         LuceneHitTracker tracker = (LuceneHitTracker)inReq.getPageValue("hits");
228         if( tracker != null)
229         {
230             int oldNum = tracker.getPage();
231             LuceneHitTracker tracker2 = archive.getStore().search( tracker.getQuery(), tracker.getOrdering() );
232             tracker2.setPage(oldNum);
233             inReq.putSessionValue("hits",tracker2);
234             inReq.putPageValue("hits",tracker2);
235         }
236     }
237     
238     public void toggleSlideshow(WebPageRequest inReq) throws Exception JavaDoc
239     {
240         Archive archive = getArchive(inReq);
241         archive.setSlideshow(archive.isSlideshow());
242     }
243     public void uploadNewRecords(WebPageRequest inReq) throws Exception JavaDoc
244     {
245         
246         FileUpload command = new FileUpload();
247         command.setPageManager(getPageManager());
248         Map JavaDoc properties = command.parseArguments(inReq);
249         if ( properties == null)
250         {
251             return;
252         }
253         String JavaDoc name = (String JavaDoc)properties.get("uploadedfilename");
254         //prefix the name with the catalog id
255
name = PathUtilities.extractFileName(name);
256         Archive archive = getArchive(inReq);
257         
258         //handle the upload
259
//figure out path
260
String JavaDoc saveas = archive.getCatalogHome() + "/uploads/" + name;
261         
262         command.saveFile(properties,saveas,inReq);
263
264         String JavaDoc notes = inReq.getRequestParameter("notes");
265         
266         
267         archive.recordUploaded(saveas, notes);
268     }
269     public void backUpOriginals( WebPageRequest inReq) throws Exception JavaDoc
270     {
271         ImageMaker maker = new ImageMaker();
272         maker.setArchive(getArchive(inReq));
273         maker.backUpOriginals();
274     }
275
276     public Store getStore(WebPageRequest inPageRequest) throws OpenEditException
277     {
278         Page page = inPageRequest.getPage();
279         String JavaDoc readername = null;
280         if( page != null)
281         {
282             readername = page.get("storereadername");
283         }
284         if( readername == null)
285         {
286             readername = "AssetStoreDataReader";
287         }
288         StoreDataReader reader = (StoreDataReader)getModuleManager().getBean(readername);
289         Store store = reader.getStore(page);
290         return store;
291     }
292     
293     public void convertData(WebPageRequest inPageRequest) throws Exception JavaDoc
294     {
295         Store store = getStore(inPageRequest);
296     
297         List JavaDoc errorlog = store.convertCatalog();
298         if (inPageRequest != null)
299         {
300             inPageRequest.removeSessionValue("store");
301             inPageRequest.putPageValue("exception-report", errorlog);
302         }
303     }
304     public void reIndexStore(WebPageRequest inPageRequest) throws Exception JavaDoc
305     {
306         Store store = getStore(inPageRequest);
307
308         store.reindexAll();
309         store.getCatalogArchive().reloadCatalogs();
310     }
311     public void clearSearchIndex(WebPageRequest inPageRequest) throws Exception JavaDoc
312     {
313         Store store = getStore(inPageRequest);
314
315         store.getStoreSearcher().clearIndex();
316     }
317
318     public EmailErrorHandler getEmailErrorHandler() {
319         return fieldEmailErrorHandler;
320     }
321
322     public void setEmailErrorHandler(EmailErrorHandler fieldEmailErrorHandler) {
323         this.fieldEmailErrorHandler = fieldEmailErrorHandler;
324     }
325     public void toggleProperty(WebPageRequest inReq) throws Exception JavaDoc
326     {
327         User user = inReq.getUser();
328         if( user != null)
329         {
330             String JavaDoc id = inReq.getRequestParameter("id");
331             if( id != null)
332             {
333                 boolean has = user.hasProperty(id);
334                 if( has)
335                 {
336                     user.remove(id);
337                 }
338                 else
339                 {
340                     user.put(id, String.valueOf(has));
341                 }
342                 getUserManager().saveUser(user);
343             }
344         }
345     }
346     public void changeHitsPerPage(WebPageRequest inReq) throws Exception JavaDoc
347     {
348         String JavaDoc count = inReq.getRequestParameter("count");
349         if( count != null)
350         {
351             Archive archive = getArchive(inReq);
352             archive.setHitsPerPage(Integer.parseInt(count));
353         }
354     }
355 }
356
Free Books   Free Magazines  
Popular Tags