View Javadoc

1   /***
2    Copyright (C) 2005 The Java Community
3   
4    This program is free software; you can redistribute it and/or modify  it under
5    the terms of the GNU General Public License as published by  the Free Software
6    Foundation; either version 2 of the License, or  (at your option) any later
7    version.
8   
9    This program is distributed in the hope that it will be useful,  but WITHOUT
10   ANY WARRANTY; without even the implied warranty of  MERCHANTABILITY or FITNESS
11   FOR A PARTICULAR PURPOSE. See the  GNU General Public License for more details.
12  
13   You should have received a copy of the GNU General Public License  along with
14   this program; if not, write to the Free Software  Foundation, Inc., 59 Temple
15   Place, Suite 330, Boston, MA 02111-1307 USA.
16  
17   */
18  
19  package org.bejug.javacareers.jobs.view.jsf.action;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.bejug.javacareers.common.ajax.CompletionResult;
24  import org.bejug.javacareers.common.search.Criterium;
25  import org.bejug.javacareers.common.search.SearchCriteria;
26  import org.bejug.javacareers.common.view.jsf.constants.JSFConstants;
27  import org.bejug.javacareers.jobs.model.JobOffer;
28  import org.bejug.javacareers.jobs.model.User;
29  import org.bejug.javacareers.jobs.service.JobService;
30  import org.bejug.javacareers.jobs.view.jsf.forms.PostJobForm;
31  import org.bejug.javacareers.jobs.view.jsf.forms.SearchJobForm;
32  import org.bejug.javacareers.jobs.view.jsf.model.JobOfferData;
33  import org.bejug.javacareers.jobs.view.jsf.model.MailBean;
34  import org.bejug.javacareers.jobs.view.jsf.model.UserTracker;
35  import org.bejug.javacareers.jobs.view.jsf.preferences.UserPreferences;
36  import org.bejug.javacareers.jobs.view.jsf.util.Utils;
37  import org.bejug.javacareers.project.properties.JavaCareersConfig;
38  import org.springframework.dao.DataAccessException;
39  
40  import javax.faces.context.FacesContext;
41  import javax.faces.event.ActionEvent;
42  import java.util.Date;
43  import java.util.HashSet;
44  import java.util.List;
45  import java.util.Set;
46  
47  /***
48   * The Job CommentAction class.
49   *
50   * @author Peter Symoens (last modified by $Author: stephan_janssen $)
51   * @version $Revision: 1.42 $ - $Date: 2005/10/11 11:59:21 $
52   */
53  public class JobAction extends BaseAction implements JSFConstants {
54  	
55      /***
56       * The Job CommentAction logger.
57       */
58      private static final Log LOG = LogFactory.getLog(JobAction.class);
59  
60      /***
61       * The Job Service reference, injected by Springs container.
62       */
63      private JobService jobService;
64  
65      /***
66       * The Job Service reference, injected by Springs container.
67       */
68      private JavaCareersConfig config;
69  
70      /***
71       * The user tracker, injected.
72       */
73      private UserTracker userTracker;
74  
75      /***
76       * The mail bean, injected.
77       */
78      private MailBean mailBean;
79  
80      /***
81       * job searchcriteria ordered by publicationDate
82       */
83      private SearchCriteria jobsSearchcriteria = null;
84      
85      /***
86       *
87       */
88      private boolean deletePressed;
89  
90      /***
91       * Set Job Service via Springs IoC injection.
92       *
93       * @param jobService JobService
94       */
95      public void setJobService(JobService jobService) {
96          this.jobService = jobService;
97      }
98  
99      /***
100      * @param userTracker The userTracker to set.
101      */
102     public void setUserTracker(UserTracker userTracker) {
103         this.userTracker = userTracker;
104     }
105 
106     /***
107      * gets the searchCriteria for the jobs.
108      * @return the wanted SearchCriteria.
109      */
110     private SearchCriteria getJobSearchCriteria(){
111     	if (jobsSearchcriteria == null) {
112     		jobsSearchcriteria = searchCriteriaFactory.createSearchCriteria(
113                                                             JobOffer.class);
114         	jobsSearchcriteria.addDescendingOrder("publicationDate");
115     	}
116     	return jobsSearchcriteria;
117     }
118     
119     /***
120      * List all open JobOffers
121      *
122      * @param actionEvent ActionEvent
123      */
124     public void listAllJobs(ActionEvent actionEvent) {
125         LOG.info("Debug: executing listAllJobs");
126         JobOfferData.setJobOffers(jobService.getJobOffers(getJobSearchCriteria()));
127     }
128  
129     /***
130      * Process the job search action.
131      *
132      * @param actionEvent the JSF action event.
133      */
134     public void processJobSearch(ActionEvent actionEvent) {
135         LOG.info("Debug: executing processJobSearch");
136 
137         SearchJobForm form = (SearchJobForm) Utils.getVariableResolver().
138                 resolveVariable(FacesContext.getCurrentInstance(),
139                                                             "searchJobForm");
140 
141         String[] selectedProfiles = form.getSelectedProfiles();
142         for (int i = 0; i < selectedProfiles.length; i++) {
143             LOG.info("Debug: profile =  " + i + " = " + selectedProfiles[i]);
144 
145         }
146         LOG.info("Debug:  input keywords = " + form.getKeywords());
147 
148         String[] selectedLocations = form.getLocations();
149         for (int i = 0; i < selectedLocations.length; i++) {
150             LOG.info("Debug: location  " + i + " = " + selectedLocations[i]);
151 
152             LOG.info("Debug:  SearchPeriod = " + form.getSearchPeriod());
153             form.setRenderInfoMessage(true);
154             if (jobService.getJobOffers(getJobSearchCriteria()).size() > 0) {
155                 JobOfferData.setJobOffers(
156                         jobService.getJobOffers(getJobSearchCriteria()));
157                 form.setRenderJobSearchResult(true);
158                 form.setInfoMessage(Utils.getMessage("label_foundData"));
159             } else {
160                 form.setInfoMessage(Utils.getMessage("label_noDataFound"));
161             }
162         }
163     }
164     
165     /***
166      * do an ajac-search.
167      * @param context the FacesContext to use.
168      * @param prefix a String containing the prefix to use.
169      * @param result the result of the completion.
170      */
171     public void processAjaxSearch(FacesContext context, String prefix,
172                                   CompletionResult result) {
173         LOG.info("Debug: Searching for: " + prefix);
174         SearchCriteria searchCriteria =
175                 searchCriteriaFactory.createSearchCriteria(JobOffer.class);
176         //searchCriteria.addEqualsCriterium("description", "Test");
177         searchCriteria.addLikeCriterium("description", "%" + prefix + "%");
178         searchCriteria.addLikeCriterium("title", new StringBuffer(100).
179                                                                  append("%").
180                                                                  append(prefix).
181                                                                  append("%").
182                                             toString(), Criterium.LOGICAL_OR);
183         List searchList = null;
184         try {
185             searchList = jobService.getJobOffers(searchCriteria);
186         } catch (IllegalArgumentException e1) {
187             LOG.info("Debug: Illegal argument:" + e1);
188         }
189         result.addItem("- Results preview:");
190         LOG.info(new StringBuffer(100).append("Debug: Search on ").append(prefix).
191                 append(" returned hits: ").append(searchList.size()).toString());
192 
193         for (int i = 0; i < searchList.size(); i++) {
194             JobOffer offer = (JobOffer) searchList.get(i);
195             String contextMatch = offer.getTitle();
196             if (contextMatch.length() > 40) {
197                 contextMatch = contextMatch.substring(0, 40);
198             }
199             //contextMatch = contextMatch.replace(',', ';');
200 
201             String jobPage = config.getJobPageUrl();
202             String host = config.getAppname();
203 
204             String page = new StringBuffer(100).append(config.getUrl()).
205                     append(host).append("/").append(jobPage).append("?id=").
206                     append(offer.getId()).toString();
207             LOG.info("Debug: Page: " + page);
208             contextMatch = contextMatch + "," + page;
209             //contextMatch = contextMatch.replace(',', ';');
210 
211             LOG.info("Debug: - Added hit: " + contextMatch);
212             result.addItem(contextMatch);
213         }
214         if (result.getItems().size() == 1) {
215             result.addItem("- None");
216         }
217 
218         LOG.info("Debug: Send back: " + result);
219         LOG.info("Debug: Using" + result.getItems());
220     }
221 
222     /***
223      * Process the job insert action.
224      *
225      * @param actionEvent the JSF action event.
226      */
227     public void processJobInsert(ActionEvent actionEvent) {
228         LOG.info("Debug: executing processJobInsert");
229         PostJobForm form = (PostJobForm) Utils.getVariableResolver().
230                 resolveVariable(FacesContext.getCurrentInstance(),
231                             "postJobForm");
232 
233         try {
234             JobOffer jobOffer = new JobOffer();
235             jobOffer.setPublicationDate(new Date());
236             jobOffer.setTitle(form.getTitle());
237             jobOffer.setDescription(form.getDescription());
238             jobOffer.setLocation(form.getLocation());
239             if (form.getExternalURL() != null) {
240                 jobOffer.setUrl(form.getExternalURL());
241             } else {
242                 jobOffer.setUrl("n/a");
243 
244             }
245             Set profiles = new HashSet();
246             profiles.add(form.getModelProfile());
247             jobOffer.setProfiles(profiles);
248             jobOffer.setUser(userTracker.getCurrentUser());
249 
250             jobOffer.setStatus(1); // StatusConstants.ACTIVE);
251 
252             jobService.storeJobOffer(jobOffer);
253             jobOffer.setUrl(new StringBuffer(150).append(getConfig().
254                     getWebPathUrl()).append("/jobs/employer/job.jsf?").
255                     append(jobOffer.getId()).toString());
256 
257             User user = userTracker.getCurrentUser();
258             mailBean.sendPostJobMailNotification(user, jobOffer);
259 
260             LOG.info("Debug: jobOffer id = " + jobOffer.getId());
261         } catch (DataAccessException e) {
262             LOG.fatal(e.getMessage());
263         }
264 
265         listCurrentJobOfferPosts();
266         outcome = "go_employer_home";
267     }
268 
269     /***
270      * Lists the employers currently open JobOffers
271      */
272     public void listCurrentJobOfferPosts() {
273         UserPreferences.setRenderOpenJobs(true);
274         JobOfferData.setJobOffers(getCurrentJobOfferPosts());
275     }
276 
277     /***
278      * @return nrOfCurrentJobOfferPosts
279      */
280     public String getNrOfCurrentJobOfferPosts() {
281         return Integer.toString(jobService.getCurrentUserJobOfferCount(
282                 userTracker.getCurrentUser()));
283     }
284 
285     /***
286      * @return The current user's open job posts
287      */
288     public List getCurrentJobOfferPosts() {
289         User user = userTracker.getCurrentUser();
290         Class cl = null;
291         SearchCriteria criteria;
292         try {
293             cl = Class.forName("org.bejug.javacareers.jobs.model.JobOffer");
294         } catch (ClassNotFoundException e) {
295             LOG.error(e);
296         }
297         criteria = searchCriteriaFactory.createSearchCriteria(cl);
298         criteria.addEqualsCriterium("user", user);
299         UserPreferences.setRenderOpenJobs(true);
300         List openOffers = searchCriteriaService.executeQuery(criteria);
301         return openOffers;
302     }
303 
304     /***
305      *
306      * @param actionEvent actionEvent
307      */
308     public void confirmDelete(ActionEvent actionEvent) {
309         saveToken();
310         deletePressed = true;
311     }
312 
313 	/***
314      *
315      * @return deletePressed
316      */
317     public boolean isDeletePressed(){
318         return deletePressed;
319     }
320 
321     /***
322      * Send job digest mail to interested users.
323      *
324      * @return null to redisplay current page.
325      */
326     public String sendJobDigest() {
327         mailBean.sendJobDigestMailToUsers();
328         return null;
329     }
330 
331     /***
332      * @param jobDigest The mailBean to set.
333      */
334     public void setMailBean(MailBean jobDigest) {
335         this.mailBean = jobDigest;
336     }
337 
338     /***
339      * @return config
340      */
341     public JavaCareersConfig getConfig() {
342         return config;
343     }
344 
345     /***
346      * @param config String
347      */
348     public void setConfig(JavaCareersConfig config) {
349         this.config = config;
350     }
351 
352     /***
353      * Gets the number of JobOffers
354      *
355      * @return number of joboffers
356      */
357     public String getJobOfferCount() {
358         int count = jobService.getJobOfferCount();
359         // LOG.info("Debug: JobOffer count: "+count);
360         return new StringBuffer().append("(").append(count).
361                                   append(")").toString();
362     }
363 
364     /***
365      * Use a getter method to be able to acces this variable from within jsf
366      *
367      * @return The static final token key
368      */
369     public String getTokenKey() {
370         String tokenKey = TOKEN_KEY;
371         return tokenKey;
372     }
373 }
374 /***
375  * $Log: JobAction.java,v $
376  * Revision 1.42  2005/10/11 11:59:21  stephan_janssen
377  * Code cleanup.
378  *
379  * Revision 1.41  2005/09/30 14:38:07  bavo_jcs
380  * Fixed URL
381  *
382  * Revision 1.40  2005/09/06 13:22:50  schauwvliege
383  * Intro of interview
384  *
385  * Revision 1.39  2005/09/05 16:39:58  schauwvliege
386  * Fixed forward to a friend
387  *
388  * Revision 1.38  2005/09/05 09:20:06  schauwvliege
389  * Removed CurrentJobOffer is now CurrentItem
390  *
391  * Revision 1.37  2005/08/30 13:07:47  psong09
392  * renamed author: psong09
393  *
394  * Revision 1.36  2005/08/24 12:32:12  psong09
395  * Added spinner component and renamed commentAction
396  *
397  * Revision 1.35  2005/08/23 11:13:28  bme_jcs
398  * resolved checkstyle and pmd errors
399  *
400  * Revision 1.34  2005/08/23 10:18:47  bme_jcs
401  * resolved checkstyle and pmd errors
402  *
403  * Revision 1.33  2005/08/23 08:50:38  ge0ffrey
404  * JAVACAREERS-284 part 3: resolver
405  *
406  * Revision 1.32  2005/08/23 08:25:33  ge0ffrey
407  * JAVACAREERS-284 fix for NPE (shadowed field)
408  *
409  * Revision 1.31  2005/08/22 15:25:05  ge0ffrey
410  * JAVACAREERS-284
411  *
412  * Revision 1.30  2005/08/22 13:38:34  psong09
413  * Added error page for synchronisation error
414  *
415  * Revision 1.29  2005/08/19 15:04:41  schauwvliege
416  * update after merge
417  *
418  * Revision 1.28  2005/08/19 13:48:22  schauwvliege
419  * fix
420  *
421  * Revision 1.27  2005/08/19 13:40:42  bme_jcs
422  * checkstyle errors resolved
423  *
424  * Revision 1.26  2005/08/19 13:29:10  bme_jcs
425  * checkstyle errors resolved
426  *
427  * Revision 1.25  2005/08/19 13:13:53  psong09
428  * introduced synchroniser token
429  *
430  * Revision 1.24  2005/08/18 14:06:10  schauwvliege
431  * Sort by publicationDate
432  *
433  * Revision 1.23  2005/08/16 08:53:06  psong09
434  * Added delete job functionality
435  *
436  * Revision 1.22  2005/08/10 09:04:49  bavo_jcs
437  * Optimized imports according to checkstyle
438  *
439  * Revision 1.21  2005/08/09 12:59:56  bavo_jcs
440  * Optimized imports
441  *
442  * Revision 1.20  2005/08/08 09:38:22  bme_jcs
443  * resolved checkstyle errors
444  *
445  * Revision 1.19  2005/08/05 11:37:31  psong09
446  * added render check for previous and next buttons on jobContent.jsp
447  *
448  * Revision 1.18  2005/08/04 15:23:16  bme_jcs
449  * resolved checkstyle errors
450  *
451  * Revision 1.17  2005/08/04 14:43:07  psong09
452  * Provided back and forward navigation between jobdetail pages
453  *
454  * Revision 1.16  2005/08/03 08:18:40  bavo_jcs
455  * search ajax on both title and description
456  *
457  * Revision 1.15  2005/07/28 07:19:31  bavo_jcs
458  * URL path fix
459  *
460  * Revision 1.14  2005/07/18 15:42:31  bavo_jcs
461  * search fields alignment
462  *
463  * Revision 1.13  2005/07/13 11:16:59  bavo_jcs
464  * Moved Ajax files
465  *
466  * Revision 1.12  2005/07/12 14:49:22  bavo_jcs
467  * Ajax Job search
468  *
469  * Revision 1.11  2005/06/23 15:13:09  psong09
470  * comment component updated
471  *
472  * Revision 1.10  2005/06/21 12:55:51  bavo_jcs
473  * added number of joboffers
474  *
475  * Revision 1.9  2005/06/21 10:05:19  bavo_jcs
476  * added number of joboffers
477  *
478  * Revision 1.8  2005/06/17 12:01:17  schauwvliege
479  * CheckStyle/ PMD
480  *
481  * Revision 1.7  2005/06/17 11:42:46  schauwvliege
482  * CheckStyle/ PMD
483  *
484  * Revision 1.6  2005/06/17 09:22:21  psong09
485  * added subject field to comment
486  *
487  * Revision 1.5  2005/06/16 13:46:39  bavo_jcs
488  * made jobs accessible through GET method URL
489  * adapted RSS file to use this
490  *
491  * Revision 1.4  2005/06/14 13:40:36  schauwvliege
492  * Renamed add to store
493  *
494  * Revision 1.3  2005/06/14 12:05:53  schauwvliege
495  * CheckStyle and fixing tests
496  *
497  * Revision 1.2  2005/06/09 08:18:53  bejug_cc
498  * Fix initial import
499  *
500  * Revision 1.36  2005/06/03 12:45:36  PSONG09
501  * added check : externalURL != null
502  *
503  * Revision 1.35  2005/06/02 15:48:36  PSONG09
504  * added searchcriteria
505  *
506  * Revision 1.34  2005/06/02 07:43:43  PSONG09
507  * updated logging
508  *
509  * Revision 1.33  2005/06/01 08:54:15  kva
510  * added page to handle forgotten passwords
511  *
512  * Revision 1.32  2005/05/26 10:41:39  kva
513  * added job digest mail
514  *
515  * Revision 1.31  2005/05/25 15:41:15  ssc
516  * *** empty log message ***
517  *
518  * Revision 1.30  2005/05/25 12:25:21  kva
519  * comment
520  *
521  * Revision 1.29  2005/05/23 17:53:57  sja
522  * Introduced StatusConstants.ACTIVE.
523  *
524  * Revision 1.28  2005/05/23 12:30:27  kva
525  * when posting a job the poster will now receive an email with details of the new job offer
526  *
527  * Revision 1.27  2005/05/19 13:55:24  PSONG09
528  * update resume component + refactoring cv to resume
529  *
530  * Revision 1.26  2005/05/19 09:04:48  PSONG09
531  * modified jobsearch result table + CSS
532  *
533  * Revision 1.25  2005/05/18 14:59:33  PSONG09
534  * update searchJobAction
535  *
536  * Revision 1.24  2005/05/18 14:32:13  kva
537  * extracted methods in UserTracker
538  *
539  * Revision 1.23  2005/05/17 15:38:16  PSONG09
540  * update jobsearch functionality
541  *
542  * Revision 1.22  2005/05/17 13:36:39  kva
543  * posting jobs uses profiles as configured by admin and uses the user currently logged in
544  *
545  * Revision 1.21  2005/05/17 11:59:56  ssc
546  * Refactored User and Publisher class to User Class added cvAdded Boolean, added Address to user, Fixed test to work with this setup
547  *
548  * Revision 1.20  2005/05/12 08:23:55  ssc
549  * Checkstyle errors
550  *
551  * Revision 1.19  2005/05/11 20:14:48  kva
552  * fix duplicate JavaCareerException problem
553  *
554  * Revision 1.18  2005/05/11 16:27:41  sja
555  * Added some comments.
556  *
557  * Revision 1.17  2005/05/10 15:08:57  kva
558  * fix organisation constructor
559  *
560  * Revision 1.16  2005/05/10 14:38:06  ssc
561  * Deleted Salutation
562  *
563  * Revision 1.15  2005/05/10 13:23:34  kva
564  * handle duplicate username exception
565  *
566  * Revision 1.14  2005/05/10 12:00:10  kva
567  * change protected into private member
568  *
569  * Revision 1.13  2005/05/10 11:03:11  PSONG09
570  * update actions
571  *
572  * Revision 1.12  2005/05/09 14:52:32  ssc
573  * checkstyle
574  *
575  * Revision 1.10  2005/05/04 15:25:17  PSONG09
576  * addded action
577  *
578  * Revision 1.9  2005/05/03 14:59:18  PSONG09
579  * javadoc added
580  *
581  * Revision 1.8  2005/05/01 17:53:36  sja
582  * Checkstyle and PMD corrections.
583  *
584  * Revision 1.7  2005/05/01 11:16:49  sja
585  * Added getJobService method.
586  *
587  * Revision 1.6  2005/04/29 18:17:34  sja
588  * General cleanup (naming conv., javadoc and CVS tags)
589  *
590  */