KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mvnforum > admin > CompanyWebHandler


1 /*
2  * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/admin/CompanyWebHandler.java,v 1.46 2006/04/14 17:05:25 minhnn Exp $
3  * $Author: minhnn $
4  * $Revision: 1.46 $
5  * $Date: 2006/04/14 17:05:25 $
6  *
7  * ====================================================================
8  *
9  * Copyright (C) 2002-2006 by MyVietnam.net
10  *
11  * All copyright notices regarding mvnForum MUST remain
12  * intact in the scripts and in the outputted HTML.
13  * The "powered by" text/logo with a link back to
14  * http://www.mvnForum.com and http://www.MyVietnam.net in
15  * the footer of the pages MUST remain visible when the pages
16  * are viewed on the internet or intranet.
17  *
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation; either version 2 of the License, or
21  * any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31  *
32  * Support can be obtained from support forums at:
33  * http://www.mvnForum.com/mvnforum/index
34  *
35  * Correspondence and Marketing Questions can be sent to:
36  * info at MyVietnam net
37  *
38  * @author: Tran Van Giang
39  * @author: Minh Nguyen
40  */

41 package com.mvnforum.admin;
42
43 import java.io.*;
44 import java.sql.Timestamp JavaDoc;
45 import java.util.*;
46
47 import javax.servlet.ServletContext JavaDoc;
48
49 import com.mvnforum.*;
50 import com.mvnforum.auth.*;
51 import com.mvnforum.db.CompanyBean;
52 import com.mvnforum.db.DAOFactory;
53 import com.mvnforum.search.company.CompanyIndexer;
54 import com.mvnforum.search.company.CompanySearchQuery;
55 import net.myvietnam.mvncore.exception.*;
56 import net.myvietnam.mvncore.util.*;
57 import net.myvietnam.mvncore.web.*;
58 import net.myvietnam.mvncore.web.fileupload.FileItem;
59 import net.myvietnam.mvncore.web.fileupload.FileUploadException;
60 import org.apache.commons.logging.Log;
61 import org.apache.commons.logging.LogFactory;
62
63 public class CompanyWebHandler {
64
65     private static Log log = LogFactory.getLog(CompanyWebHandler.class);
66
67     private OnlineUserManager onlineUserManager = OnlineUserManager.getInstance();
68
69     public CompanyWebHandler() {
70     }
71
72     public void processAdd(GenericRequest request)
73         throws BadInputException, ObjectNotFoundException, CreateException, DatabaseException, DuplicateKeyException,
74         ForeignKeyNotFoundException, AuthenticationException, AssertionException, IOException {
75
76         OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
77         MVNForumPermission permission = onlineUser.getPermission();
78         permission.ensureCanAdminSystem();
79
80         Timestamp JavaDoc now = DateUtil.getCurrentGMTTimestamp();
81
82         String JavaDoc companyName = GenericParamUtil.getParameterSafe(request, "CompanyName", true);
83         String JavaDoc companyAddress = GenericParamUtil.getParameterSafe(request, "CompanyAddress", false);
84         String JavaDoc companyCity = GenericParamUtil.getParameterSafe(request, "CompanyCity", false);
85         String JavaDoc companyCAP = GenericParamUtil.getParameterSafe(request, "CompanyCAP", false);
86         String JavaDoc companyProvince = GenericParamUtil.getParameterSafe(request, "CompanyProvince", false);
87         String JavaDoc companyRegion = GenericParamUtil.getParameterSafe(request, "CompanyRegion", false);
88         String JavaDoc companyPhone = GenericParamUtil.getParameterSafe(request, "CompanyPhone", false);
89         String JavaDoc companyFax = GenericParamUtil.getParameterSafe(request, "CompanyFax", false);
90         String JavaDoc companyWebsite = GenericParamUtil.getParameterUrl(request, "CompanyWebsite");
91         String JavaDoc companyEmail = GenericParamUtil.getParameterEmail(request, "CompanyEmail");
92         String JavaDoc companySpaceName = GenericParamUtil.getParameterSafe(request, "CompanySpaceName", true);
93         String JavaDoc companySpaceHeader = GenericParamUtil.getParameterSafe(request, "CompanySpaceHeader", false);
94         String JavaDoc companySpaceFooter = GenericParamUtil.getParameterSafe(request, "CompanySpaceFooter", false);
95         String JavaDoc companyVATNumber = GenericParamUtil.getParameterSafe(request, "CompanyVATNumber", true);
96         String JavaDoc companyLogo = "";
97         String JavaDoc companyCss = "";
98         Timestamp JavaDoc companyCreationDate = now;
99         Timestamp JavaDoc companyModifiedDate = now;
100
101         // First, check the alternate key of Company, ensure things is okie before adding Group
102
try {
103             //Check if alternate key already exists
104
DAOFactory.getCompanyDAO().findByAlternateKey_CompanyName(companyName);
105             //If so, then we have to throw an exception
106
throw new DuplicateKeyException("Alternate key already exists. Cannot create new Company with the same <CompanyName> (" + companyName + ").");
107         } catch(ObjectNotFoundException e) {
108             //Otherwise we can go ahead
109
}
110
111         try {
112             //Check if alternate key already exists
113
DAOFactory.getCompanyDAO().findByAlternateKey_CompanyEmail(companyEmail);
114             //If so, then we have to throw an exception
115
throw new DuplicateKeyException("Alternate key already exists. Cannot create new Company with the same <CompanyEmail> (" + companyEmail + ").");
116         } catch(ObjectNotFoundException e) {
117             //Otherwise we can go ahead
118
}
119
120         try {
121             //Check if alternate key already exists
122
DAOFactory.getCompanyDAO().findByAlternateKey_CompanySpaceName(companySpaceName);
123             //If so, then we have to throw an exception
124
throw new DuplicateKeyException("Alternate key already exists. Cannot create new Company with the same <CompanySpaceName> (" + companySpaceName + ").");
125         } catch(ObjectNotFoundException e) {
126             //Otherwise we can go ahead
127
}
128
129         // Next, create group for this company
130
String JavaDoc groupName = MVNForumGlobal.COMPANY_GROUP_FREFIX + companyName;
131         String JavaDoc groupDesc = "This is the default generated group for company " + companyName;
132         int groupOption = 0;//@todo review it later
133

134         DAOFactory.getGroupsDAO().create(""/*groupOwnerName*/, groupName, groupDesc,
135                                groupOption, now/*groupCreationDate*/, now/*groupModifiedDate*/);
136
137         int groupID = DAOFactory.getGroupsDAO().getGroupIDFromGroupName(groupName);
138
139         // Finally, create the company now
140
DAOFactory.getCompanyDAO().create(groupID, companyName, companyAddress,
141                                 companyCity, companyCAP, companyProvince,
142                                 companyRegion, companyPhone, companyFax,
143                                 companyWebsite, companyEmail, companySpaceName,
144                                 companySpaceHeader, companySpaceFooter, companyVATNumber,
145                                 companyLogo, companyCss, companyCreationDate,
146                                 companyModifiedDate);
147
148         // create new folder for the company by companyID
149
int companyID = DAOFactory.getCompanyDAO().getCompanyIDFromCompanyEmail(companyEmail);
150
151         StringBuffer JavaDoc bufferCompanyFolder = new StringBuffer JavaDoc(128);
152         bufferCompanyFolder.append(request.getRealPath(MVNForumGlobal.UPLOADED_COMPANY_DIR));
153         bufferCompanyFolder.append(File.separatorChar).append(companyID);
154         String JavaDoc companyFolder = bufferCompanyFolder.toString();
155         FileUtil.createDir(companyFolder, true);
156
157         // now, add to the Search Index
158
CompanyBean justAddedCompanyBean = null;
159         try {
160             justAddedCompanyBean = DAOFactory.getCompanyDAO().getCompany(companyID);
161         } catch (ObjectNotFoundException e) {
162             Locale locale = I18nUtil.getLocaleInRequest(request);
163             String JavaDoc localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.ObjectNotFoundException.companyid_not_exists", new Object JavaDoc[] {new Integer JavaDoc(companyID)});
164             throw new ObjectNotFoundException(localizedMessage);
165         }
166
167         CompanyIndexer.scheduleAddCompanyTask(justAddedCompanyBean);
168     }
169
170     public void prepareView(GenericRequest request)
171         throws BadInputException, ObjectNotFoundException, DatabaseException,
172         AssertionException, AuthenticationException {
173
174         OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
175         MVNForumPermission permission = onlineUser.getPermission();
176         permission.ensureCanAdminSystem();
177
178         String JavaDoc strCompanyID = GenericParamUtil.getParameter(request, "companyid", false);
179         String JavaDoc strCompanyEmail = GenericParamUtil.getParameter(request, "companyemail", false);
180         String JavaDoc strCompanyCreationDate = GenericParamUtil.getParameter(request, "companycreationdate", false);
181
182         int companyID = 0;
183
184         Locale locale = I18nUtil.getLocaleInRequest(request);
185
186         if (strCompanyID.length() > 0) {
187             companyID = GenericParamUtil.getParameterInt(request, "companyid");
188         } else if (strCompanyEmail.length() > 0) {
189             String JavaDoc companyEmail = GenericParamUtil.getParameterEmail(request, "companyemail"); // check for better security
190
try {
191                 companyID = DAOFactory.getCompanyDAO().getCompanyIDFromCompanyEmail(companyEmail);
192             } catch (ObjectNotFoundException e) {
193                 String JavaDoc localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.ObjectNotFoundException.company_not_exists.with_email", new Object JavaDoc[] {companyEmail});
194                 throw new ObjectNotFoundException(localizedMessage);
195             }
196         } else if (strCompanyCreationDate.length() > 0) {
197             java.util.Date JavaDoc companyCreationDate = GenericParamUtil.getParameterDateUtil(request, "companycreationdate");
198             try {
199                 companyID = DAOFactory.getCompanyDAO().getCompanyIDFromCompanyCreationDate(companyCreationDate);
200             } catch (ObjectNotFoundException e) {
201                 String JavaDoc localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.ObjectNotFoundException.company_not_exists.with_creationdate", new Object JavaDoc[] {strCompanyCreationDate});
202                 throw new ObjectNotFoundException(localizedMessage);
203             }
204         } else {
205             String JavaDoc localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_get_info_to_view_company");
206             throw new BadInputException(localizedMessage);
207             //throw new BadInputException("Cannot get the information to view company.");
208
}
209         CompanyBean companyBean = null;
210         try {
211             companyBean = DAOFactory.getCompanyDAO().getCompany(companyID);
212         } catch (ObjectNotFoundException e) {
213             String JavaDoc localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.ObjectNotFoundException.companyid_not_exists", new Object JavaDoc[] {new Integer JavaDoc(companyID)});
214             throw new ObjectNotFoundException(localizedMessage);
215         }
216
217         request.setAttribute("CompanyBean", companyBean);
218     }
219
220     public void processUpdateCompanyInfo(GenericRequest request)
221         throws BadInputException, DatabaseException, ObjectNotFoundException,
222         DuplicateKeyException, AuthenticationException, AssertionException {
223
224         OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
225         MVNForumPermission permission = onlineUser.getPermission();
226         permission.ensureCanAdminSystem();
227
228         Timestamp JavaDoc now = DateUtil.getCurrentGMTTimestamp();
229
230         int companyID = GenericParamUtil.getParameterInt(request,"companyid");
231         String JavaDoc companyName = GenericParamUtil.getParameterSafe(request, "CompanyName", true);
232         String JavaDoc companyAddress = GenericParamUtil.getParameterSafe(request, "CompanyAddress", false);
233         String JavaDoc companyCity = GenericParamUtil.getParameterSafe(request, "CompanyCity", false);
234         String JavaDoc companyCAP = GenericParamUtil.getParameterSafe(request, "CompanyCAP", false);
235         String JavaDoc companyProvince = GenericParamUtil.getParameterSafe(request, "CompanyProvince", false);
236         String JavaDoc companyRegion = GenericParamUtil.getParameterSafe(request, "CompanyRegion", false);
237         String JavaDoc companyPhone = GenericParamUtil.getParameterSafe(request, "CompanyPhone", false);
238         String JavaDoc companyFax = GenericParamUtil.getParameterSafe(request, "CompanyFax", false);
239         String JavaDoc companyWebsite = GenericParamUtil.getParameterUrl(request, "CompanyWebsite");
240         String JavaDoc companyEmail = GenericParamUtil.getParameterEmail(request, "CompanyEmail");
241         String JavaDoc companySpaceName = GenericParamUtil.getParameterSafe(request, "CompanySpaceName", true);
242         String JavaDoc companySpaceHeader = GenericParamUtil.getParameterSafe(request,"CompanySpaceHeader", false);
243         String JavaDoc companySpaceFooter = GenericParamUtil.getParameterSafe(request, "CompanySpaceFooter", false);
244         String JavaDoc companyVATNumber = GenericParamUtil.getParameterSafe(request, "CompanyVATNumber", true);
245
246         DAOFactory.getCompanyDAO().updateCompanyInfo(companyID,
247                                                      companyName,
248                                                      companyAddress,
249                                                      companyCity,
250                                                      companyCAP,
251                                                      companyProvince,
252                                                      companyRegion,
253                                                      companyPhone,
254                                                      companyFax,
255                                                      companyWebsite,
256                                                      companyEmail,
257                                                      companySpaceName,
258                                                      companySpaceHeader,
259                                                      companySpaceFooter,
260                                                      companyVATNumber,
261                                                      now/*companyModifiedDate*/);
262         CompanyBean companyBean = null;
263         try {
264             companyBean = DAOFactory.getCompanyDAO().getCompany(companyID);
265         } catch (ObjectNotFoundException e) {
266             Locale locale = I18nUtil.getLocaleInRequest(request);
267             String JavaDoc localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.ObjectNotFoundException.companyid_not_exists", new Object JavaDoc[] {new Integer JavaDoc(companyID)});
268             throw new ObjectNotFoundException(localizedMessage);
269         }
270         request.setAttribute("CompanyBean", companyBean);
271
272         // Next, create group for this company
273
int groupID = companyBean.getGroupID();
274         String JavaDoc groupName = MVNForumGlobal.COMPANY_GROUP_FREFIX + companyName;
275         String JavaDoc groupDesc = "This is the default generated group for company " + companyName;
276
277         DAOFactory.getGroupsDAO().update(groupID, groupName, groupDesc, now/*groupModifiedDate*/);
278
279         // now update the search index
280
CompanyIndexer.scheduleUpdateCompanyTask(companyBean);
281     }
282
283     public void processChangeLogo(ServletContext JavaDoc context, GenericRequest request)
284         throws BadInputException, AuthenticationException, IOException,
285         AssertionException, ObjectNotFoundException, DatabaseException {
286
287         OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
288         MVNForumPermission permission = onlineUser.getPermission();
289         permission.ensureCanAdminSystem();
290
291         Locale locale = I18nUtil.getLocaleInRequest(request);
292
293         // primary key column(s)
294
int companyID = GenericParamUtil.getParameterInt(request, "companyid");
295
296         int sizeMax = 70000; // 70KB
297
int sizeThreshold = 100000;// max memory used = 100K (more than needed)
298
List fileItems;
299         try {
300             FileUploadParser uploadParser = FileUploadParserFactory.getFileUploadParser();
301             fileItems = uploadParser.parseRequest(request, sizeMax, sizeThreshold, null, "UTF-8");
302         } catch (FileUploadException ex) {
303             log.error("Cannot upload", ex);
304             String JavaDoc localizedMessage = MVNForumResourceBundle.getString(locale, "java.io.IOException.cannot_upload", new Object JavaDoc[] {ex.getMessage()});
305             throw new IOException(localizedMessage);
306             //throw new IOException("Cannot upload. Detailed reason: " + ex.getMessage());
307
}
308
309         // make sure only one file upload
310
if (fileItems.size() != 1) {
311             String JavaDoc localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.AssertionException.cannot_upload_more_than_one", new Object JavaDoc[] {"logo"});
312             throw new AssertionException(localizedMessage);
313             //throw new AssertionException("Assertion: Cannot upload more than 1 file while processing upload logo file for company.");
314
}
315
316         //get the first and only file
317
FileItem myFile = (FileItem)fileItems.get(0);
318         if (myFile.isFormField() == true) {
319             String JavaDoc localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.AssertionException.cannot_process_uploaded_file_with_a_form_field", new Object JavaDoc[] {"logo"});
320             throw new AssertionException(localizedMessage);
321             //throw new AssertionException("Cannot process uploaded company logo file with a form field.");
322
}
323
324         // now everything all right, go ahead and create logo
325
InputStream inputStream = myFile.getInputStream();
326
327         StringBuffer JavaDoc bufferPicFile = new StringBuffer JavaDoc(128);
328         bufferPicFile.append(request.getRealPath(MVNForumGlobal.UPLOADED_COMPANY_DIR));
329         bufferPicFile.append(File.separatorChar).append(companyID);
330
331         String JavaDoc companyDir = bufferPicFile.toString();
332         FileUtil.createDirs(companyDir, true/*ignore if exist*/);
333
334         bufferPicFile.append("/").append("logo.jpg");
335         String JavaDoc logoFile = bufferPicFile.toString();
336
337         log.trace("uploaded file = " + logoFile);
338
339         //The below method closes the inputStream after it have done its work.
340
ImageUtil.createThumbnail(inputStream, logoFile, 500/*maxWidth*/, 500/*maxHeight*/);// can throw BadInputException
341

342         // now the image has been save, go ahead and update database
343
StringBuffer JavaDoc bufferVirtualFile = new StringBuffer JavaDoc(128);
344         bufferVirtualFile.append(MVNForumGlobal.UPLOADED_COMPANY_DIR);
345         bufferVirtualFile.append("/").append(companyID).append("/").append("logo.jpg");
346         String JavaDoc virtualFile = bufferVirtualFile.toString();
347
348         try {
349             DAOFactory.getCompanyDAO().updateCompanyLogo(companyID, virtualFile);
350         } catch (DatabaseException ex) {// we dont need to catch ObjectNotFoundException since the companyID is already exits
351
log.fatal("Assertion in CompanyWebHandler.processChangeLogo");// we dont want it to be here
352
// need to delete the file if the above database task failed
353
FileUtil.deleteFile(logoFile);
354             throw ex;
355         }
356         CompanyBean companyBean = null;
357         try {
358             companyBean = DAOFactory.getCompanyDAO().getCompany(companyID);
359         } catch (ObjectNotFoundException e) {
360             String JavaDoc localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.ObjectNotFoundException.companyid_not_exists", new Object JavaDoc[] {new Integer JavaDoc(companyID)});
361             throw new ObjectNotFoundException(localizedMessage);
362         }
363         request.setAttribute("CompanyBean", companyBean);
364     }
365
366     public void processChangeCss(ServletContext JavaDoc context, GenericRequest request)
367         throws BadInputException, AuthenticationException, IOException,
368         AssertionException, ObjectNotFoundException, DatabaseException {
369
370         OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
371         MVNForumPermission permission = onlineUser.getPermission();
372         permission.ensureCanAdminSystem();
373
374         Locale locale = I18nUtil.getLocaleInRequest(request);
375
376         // primary key column(s)
377
int companyID = GenericParamUtil.getParameterInt(request, "companyid");
378
379         int sizeMax = 20000; // 20KB
380
int sizeThreshold = 100000;// max memory used = 100K (more than needed)
381
List fileItems;
382         try {
383             FileUploadParser uploadParser = FileUploadParserFactory.getFileUploadParser();
384             fileItems = uploadParser.parseRequest(request, sizeMax, sizeThreshold, null, "UTF-8");
385         } catch (FileUploadException ex) {
386             log.error("Cannot upload", ex);
387             String JavaDoc localizedMessage = MVNForumResourceBundle.getString(locale, "java.io.IOException.cannot_upload", new Object JavaDoc[] {ex.getMessage()});
388             throw new IOException(localizedMessage);
389             //throw new IOException("Cannot upload. Detailed reason: " + ex.getMessage());
390
}
391
392         // make sure only one file upload
393
if (fileItems.size() != 1) {
394             String JavaDoc localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.AssertionException.cannot_upload_more_than_one", new Object JavaDoc[] {"css"});
395             throw new AssertionException(localizedMessage);
396             //throw new AssertionException("Assertion: Cannot upload more than 1 file while processing upload css file for company.");
397
}
398
399         //get the first and only file
400
FileItem myFile = (FileItem)fileItems.get(0);
401         if (myFile.isFormField() == true) {
402             String JavaDoc localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.AssertionException.cannot_process_uploaded_file_with_a_form_field", new Object JavaDoc[] {"css"});
403             throw new AssertionException(localizedMessage);
404             //throw new AssertionException("Cannot process uploaded company css file with a form field.");
405
}
406
407         // now everything all right, go ahead and create logo
408
InputStream inputStream = myFile.getInputStream();
409         StringBuffer JavaDoc bufferCssFile = new StringBuffer JavaDoc(128);
410         bufferCssFile.append(context.getRealPath(MVNForumGlobal.UPLOADED_COMPANY_DIR));
411         bufferCssFile.append(File.separatorChar).append(companyID);
412
413         String JavaDoc companyDir = bufferCssFile.toString();
414         FileUtil.createDirs(companyDir, true/*ignore if exist*/);
415
416         bufferCssFile.append("/").append("style.css");
417         String JavaDoc cssFile = bufferCssFile.toString();
418
419         log.trace("uploaded file = " + cssFile);
420
421         //The below method closes the inputStream after it have done its work.
422
FileUtil.createTextFile(inputStream, cssFile);// can throw BadInputException
423

424         // now the image has been save, go ahead and update database
425
StringBuffer JavaDoc bufferVirtualFile = new StringBuffer JavaDoc(128);
426         bufferVirtualFile.append(MVNForumGlobal.UPLOADED_COMPANY_DIR);
427         bufferVirtualFile.append("/").append(companyID).append("/").append("style.css");
428         String JavaDoc virtualFile = bufferVirtualFile.toString();
429
430         try {
431             DAOFactory.getCompanyDAO().updateCompanyCss(companyID, virtualFile);
432         } catch (DatabaseException ex) {// we dont need to catch ObjectNotFoundException since the companyID is already exits
433
log.fatal("Assertion in CompanyWebHandler.processChangeCss");// we dont want it to be here
434
// need to delete the file if the above database task failed
435
FileUtil.deleteFile(cssFile);
436             throw ex;
437         }
438         CompanyBean companyBean = null;
439         try {
440             companyBean = DAOFactory.getCompanyDAO().getCompany(companyID);
441         } catch (ObjectNotFoundException e) {
442             String JavaDoc localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.ObjectNotFoundException.companyid_not_exists", new Object JavaDoc[] {new Integer JavaDoc(companyID)});
443             throw new ObjectNotFoundException(localizedMessage);
444         }
445         request.setAttribute("CompanyBean", companyBean);
446     }
447
448     public void prepare(GenericRequest request)
449         throws ObjectNotFoundException, BadInputException, DatabaseException, AuthenticationException, AssertionException {
450
451         OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
452         MVNForumPermission permission = onlineUser.getPermission();
453         permission.ensureCanAdminSystem();
454
455         int companyID = GenericParamUtil.getParameterInt(request, "companyid");
456
457         CompanyBean companyBean = null;
458         try {
459             companyBean = DAOFactory.getCompanyDAO().getCompany(companyID);
460         } catch (ObjectNotFoundException e) {
461             Locale locale = I18nUtil.getLocaleInRequest(request);
462             String JavaDoc localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.ObjectNotFoundException.companyid_not_exists", new Object JavaDoc[] {new Integer JavaDoc(companyID)});
463             throw new ObjectNotFoundException(localizedMessage);
464         }
465         request.setAttribute("CompanyBean", companyBean);
466     }
467
468     public void processDelete(ServletContext JavaDoc context, GenericRequest request)
469         throws BadInputException, ObjectNotFoundException, DatabaseException, AuthenticationException, AssertionException {
470
471         OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
472         MVNForumPermission permission = onlineUser.getPermission();
473         permission.ensureCanAdminSystem();
474
475         // primary key column(s)
476
int companyID = GenericParamUtil.getParameterInt(request, "companyid");
477
478         // now check the password
479
MyUtil.ensureCorrectCurrentPassword(request);
480
481         try {
482             DAOFactory.getCompanyDAO().delete(companyID);
483         } catch (ObjectNotFoundException e ) {
484             Locale locale = I18nUtil.getLocaleInRequest(request);
485             String JavaDoc localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.ObjectNotFoundException.companyid_not_exists", new Object JavaDoc[] {new Integer JavaDoc(companyID)});
486             throw new ObjectNotFoundException(localizedMessage);
487         }
488         DAOFactory.getMemberCompanyDAO().delete_inCompany(companyID);
489
490         // now delete the folder for this company
491
StringBuffer JavaDoc bufferCompanyFolder = new StringBuffer JavaDoc(128);
492         bufferCompanyFolder.append(context.getRealPath(MVNForumGlobal.UPLOADED_COMPANY_DIR));
493         bufferCompanyFolder.append(File.separatorChar).append(companyID);
494         String JavaDoc companyFolder = bufferCompanyFolder.toString();
495         try {
496             FileUtil.deleteDir(new File(companyFolder));
497         } catch (IOException ioe) {
498         }
499
500         // now update the search index
501
CompanyIndexer.scheduleDeleteCompanyTask(companyID);
502     }
503
504     /**
505      * This method supports sorting base on many criteria
506      */

507     public void prepareShowCompanyManagement(GenericRequest request)
508         throws DatabaseException, AssertionException, BadInputException, AuthenticationException {
509
510         OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
511         MVNForumPermission permission = onlineUser.getPermission();
512         permission.ensureCanAdminSystem();
513
514         // for sort and order stuff
515
String JavaDoc sort = GenericParamUtil.getParameter(request, "sort");
516         String JavaDoc order = GenericParamUtil.getParameter(request, "order");
517         if (sort.length() == 0) sort = "CompanyCreationDate";
518         if (order.length()== 0) order = "DESC";
519
520         // we continue
521
int postsPerPage = onlineUser.getPostsPerPage();
522         int offset = 0;
523         try {
524             offset = GenericParamUtil.getParameterInt(request, "offset");
525         } catch (BadInputException e) {
526             // do nothing
527
}
528
529         Locale locale = I18nUtil.getLocaleInRequest(request);
530
531         int totalCompanies = DAOFactory.getCompanyDAO().getNumberOfCompanies();
532         if (offset > totalCompanies) {
533             String JavaDoc localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.BadInputException.offset_greater_than_total_rows");
534             throw new BadInputException(localizedMessage);
535             //throw new BadInputException("The offset is not allowed to be greater than total companies.");
536
}
537
538         Collection companyBeans = DAOFactory.getCompanyDAO().getCompanies_withSortSupport_limit(offset, postsPerPage, sort, order);
539
540         request.setAttribute("CompanyBeans", companyBeans);
541         request.setAttribute("TotalCompanies", new Integer JavaDoc(totalCompanies));
542         request.setAttribute("offset", new Integer JavaDoc(offset));
543     }
544
545     public void processSearch(GenericRequest request)
546         throws BadInputException, IOException, DatabaseException, ObjectNotFoundException,
547         AuthenticationException, AssertionException {
548
549         OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
550         MVNForumPermission permission = onlineUser.getPermission();
551         permission.ensureCanAdminSystem();
552
553         String JavaDoc companyNameKey = GenericParamUtil.getParameter(request, "companyname", false);
554         String JavaDoc companyAddressKey = GenericParamUtil.getParameter(request, "companyaddress", false);
555         String JavaDoc companyDateKey = GenericParamUtil.getParameter(request, "companycreationdate", false);
556
557         Locale locale = I18nUtil.getLocaleInRequest(request);
558
559         int defaultRows = onlineUser.getPostsPerPage();
560
561         int offset = GenericParamUtil.getParameterUnsignedInt(request, "offset", 0);
562         int rows = GenericParamUtil.getParameterUnsignedInt(request, "rows", defaultRows);
563         if (rows == 0) {
564             rows = defaultRows;// fix NullPointerException when rows = 0
565
}
566
567         // offset should be even when divide with rowsToReturn
568
offset = (offset / rows) * rows;
569
570         CompanySearchQuery query = new CompanySearchQuery();
571
572         if (companyNameKey.length() > 0) {
573             query.setCompanyNameKey(companyNameKey);
574         } else if (companyAddressKey.length() > 0){
575             query.setCompanyAddressKey(companyAddressKey);
576         } else {
577             String JavaDoc localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_search.key_is_null");
578             throw new BadInputException(localizedMessage);
579         }
580         // dd/MM/yyyy :: default date string
581
/*if ( companyDateKey.equals("dd/MM/yyyy")) {
582             // do nothing
583         } else if (companyDateKey.length() > 0){
584             query.setCompanyDateKey(new Timestamp(GenericParamUtil.getParameterDate(request,"companycreationdate").getTime()));
585         }*/

586
587         int searchDate = GenericParamUtil.getParameterUnsignedInt(request, "date", CompanySearchQuery.SEARCH_ANY_DATE);
588         int searchBeforeAfter = GenericParamUtil.getParameterInt(request, "beforeafter", CompanySearchQuery.SEARCH_NEWER);
589
590         if ((searchDate != CompanySearchQuery.SEARCH_ANY_DATE) && (searchDate < 365 * 10)) { // 10 years
591
long deltaTime = DateUtil.DAY * searchDate;
592
593             Timestamp JavaDoc now = DateUtil.getCurrentGMTTimestamp();
594             Timestamp JavaDoc from = null;
595             Timestamp JavaDoc to = null;
596
597             long currentTime = now.getTime();
598
599             if (searchBeforeAfter == CompanySearchQuery.SEARCH_NEWER) {
600                 from = new Timestamp JavaDoc(currentTime - deltaTime);
601             } else {// older
602
to = new Timestamp JavaDoc(currentTime - deltaTime);
603             }
604
605             query.setFromCompanyDateKey(from);
606             query.setToCompanyDateKey(to);
607         }
608
609
610         query.searchDocuments(offset, rows);
611         int hitCount = query.getHitCount();
612         Collection result = query.getCompanyResult();
613
614         if (offset > hitCount) {
615             String JavaDoc localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.BadInputException.offset_greater_than_total_rows");
616             throw new BadInputException(localizedMessage);
617         }
618
619         request.setAttribute("offset", new Integer JavaDoc(offset));
620         request.setAttribute("rows", new Integer JavaDoc(rows));
621         request.setAttribute("TotalCompanies", new Integer JavaDoc(hitCount));
622         request.setAttribute("CompanyBeans", result);
623         request.setAttribute("CompanyName", companyNameKey);
624         request.setAttribute("CompanyAddress", companyAddressKey); //strCompanyAddress);
625
request.setAttribute("CompanyCreationDate",companyDateKey); //strCompanyCreationDate);
626
request.setAttribute("CompanyModifiedDate", ""); //strCompanyModifiedDate);
627
request.setAttribute("FromCompanyCreationDate", ""); // strFromCompanyCreationDate);
628
request.setAttribute("ToCompanyCreationDate", ""); // strToCompanyCreationDate);
629
}
630 }
631
Popular Tags