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.service.ItemService;
24 import org.bejug.javacareers.common.view.jsf.constants.JSFConstants;
25 import org.bejug.javacareers.common.view.jsf.utils.ItemFileRemover;
26 import org.bejug.javacareers.jobs.model.*;
27 import org.bejug.javacareers.jobs.view.jsf.model.CurrentItem;
28 import org.bejug.javacareers.jobs.view.jsf.model.ItemData;
29 import org.bejug.javacareers.jobs.view.jsf.util.Utils;
30 import org.bejug.javacareers.project.properties.ItemValueBinding;
31 import org.bejug.javacareers.project.properties.ItemValueBindingList;
32 import org.bejug.javacareers.project.properties.JavaCareersConfig;
33
34 import javax.faces.component.UIData;
35 import javax.faces.context.ExternalContext;
36 import javax.faces.event.ActionEvent;
37 import javax.faces.event.ValueChangeEvent;
38 import javax.faces.model.SelectItem;
39 import javax.servlet.http.HttpServletRequest;
40 import javax.servlet.http.HttpSession;
41 import java.util.ArrayList;
42 import java.util.Iterator;
43 import java.util.List;
44
45 /***
46 * The Job Action class.
47 *
48 * @author Peter Symoens (last modified by $Author: shally $)
49 * @version $Revision: 1.18 $ - $Date: 2005/12/21 11:38:42 $
50 */
51 public class ItemAction extends BaseAction {
52
53 /***
54 * The Job Action logger.
55 */
56 private static final Log LOG = LogFactory.getLog(ItemAction.class);
57
58 /***
59 * The itemSpinnerList List.
60 */
61 private List itemSpinnerList;
62
63 /***
64 * The itemFileRemover ItemFileRemover.
65 */
66 private ItemFileRemover itemFileRemover;
67
68 /***
69 *
70 */
71 public static final String LINES_PER_PAGE = "LINES_PER_PAGE";
72
73 /***
74 * The deletePressed boolean.
75 */
76 private boolean deletePressed;
77
78 /***
79 * The to delete item.
80 */
81 private Item itemToDelete;
82 /***
83 *
84 */
85 public static final String SHOW_ALL_ITEMS = "label_show_all_item";
86
87 /***
88 * The unApprovedItems boolean.
89 */
90 private boolean unApprovedItems;
91
92 /***
93 * The Job Service reference, injected by Springs container.
94 */
95 private ItemService itemService;
96
97 /***
98 * The Job Service reference, injected by Springs container.
99 */
100 private JavaCareersConfig config;
101
102 /***
103 * The list of current items
104 */
105 private List items;
106
107 /***
108 * Set Job Service via Springs IoC injection.
109 *
110 * @param itemService ItemService
111 */
112 public void setItemService(ItemService itemService) {
113 this.itemService = itemService;
114 }
115
116 /***
117 * The currentFilter String.
118 */
119 private String currentFilter;
120
121 /***
122 * The injected ItemValueBindingList
123 */
124 private ItemValueBindingList bindingList;
125
126 /***
127 * @param bindingList ItemValueBindingList
128 */
129 public void setBindingList(ItemValueBindingList bindingList) {
130 this.bindingList = bindingList;
131 }
132
133 /***
134 * @param actionEvent actionEvent
135 */
136 public void confirmDelete(ActionEvent actionEvent) {
137 itemToDelete = (Item) itemTable.getRowData();
138 saveToken();
139 deletePressed = true;
140 }
141
142 /***
143 * @param actionEvent actionEvent
144 */
145 public void confirmDeleteCurrentItem(ActionEvent actionEvent) {
146 itemToDelete = getCurrentItem().getItem();
147 saveToken();
148 deletePressed = true;
149 }
150
151 /***
152 * The table listing items.
153 */
154 private UIData itemTable;
155
156 /***
157 * Store the item requested to be displayed on session scope so it can be
158 * picked up by the next page.
159 *
160 * @return next page to display.
161 */
162 public String showItem() {
163 Item item = (Item) itemTable.getRowData();
164 Utils.createValueBinding(item);
165 return "go_jobdetails";
166 }
167
168 /***
169 * @param itemTable The itemTable to set.
170 */
171 public void setItemTable(UIData itemTable) {
172 this.itemTable = itemTable;
173 }
174
175 /***
176 * @return Returns the itemTable.
177 */
178 public UIData getItemTable() {
179 return itemTable;
180 }
181
182 /***
183 * @return Returns true if the item is a offer.
184 */
185 public boolean isOffer() {
186 Item item = (Item) itemTable.getRowData();
187 boolean bool = item instanceof Offer;
188 return bool;
189 }
190
191 /***
192 * @return Returns true if the item is a offer.
193 */
194 public boolean isJobOffer() {
195 Item item = (Item) itemTable.getRowData();
196 boolean bool = item instanceof JobOffer;
197 return bool;
198 }
199
200 /***
201 * @return Returns the link to the gif.
202 */
203 public String getImage() {
204 Item item = (Item) itemTable.getRowData();
205 if (item instanceof JobOffer) {
206 return Utils.getMessage("image_joboffer");
207 }
208 if (item instanceof Resume) {
209 return Utils.getMessage("image_resume");
210 }
211 if (item instanceof CommercialEducationOffer) {
212 return Utils.getMessage("image_commercial_education_offer");
213 }
214 return Utils.getMessage("image_interview");
215 }
216
217
218 /***
219 * List all open Items
220 *
221 * @param actionEvent ActionEvent
222 */
223 public void listItems(ActionEvent actionEvent) {
224 unApprovedItems = false;
225 LOG.info("Debug: executing listAllItems");
226
227 if (currentFilter == null || currentFilter.equalsIgnoreCase(SHOW_ALL_ITEMS)) {
228 ItemData.setItems(itemService.getItems());
229 } else {
230 List valueBindingList = bindingList.getBindingList();
231 for (Iterator iterator = valueBindingList.iterator(); iterator.hasNext();) {
232 ItemValueBinding binding = (ItemValueBinding) iterator.next();
233 if (currentFilter.equalsIgnoreCase(binding.getMessageKey())) {
234 ItemData.setItems(itemService.getItems(binding.getClazz()));
235 }
236 }
237 }
238
239 }
240
241 /***
242 * @param actionEvent ActionEvent
243 */
244 public void showPreviousItem(ActionEvent actionEvent) {
245 items = ItemData.getItemsList();
246 LOG.info("Debug: excecuting showPreviousItem");
247 CurrentItem currentItem = getCurrentItem();
248 if (currentItem != null) {
249 int index = items.indexOf(currentItem.getItem());
250 if (index > 0) {
251 Utils.createValueBinding((Item) items.get(index - 1));
252 }
253 }
254 }
255
256 /***
257 * @param actionEvent ActionEvent
258 */
259 public void showNextItem(ActionEvent actionEvent) {
260 items = ItemData.getItemsList();
261 LOG.info("Debug: Excecuting showNextItem");
262 CurrentItem currentItem = getCurrentItem();
263 if (currentItem != null) {
264 int index = items.indexOf(currentItem.getItem());
265 if (index != -1 && index < items.size() - 1) {
266 Utils.createValueBinding((Item) items.get(index + 1));
267 }
268 }
269 }
270
271 /***
272 * @return first Returns true if currentItem is the first offer in the list
273 */
274 public boolean isFirst() {
275
276
277 items = ItemData.getItemsList();
278
279 CurrentItem currentItem = getCurrentItem();
280 if (currentItem != null) {
281 int index = items.indexOf(currentItem.getItem());
282 if (index == 0) {
283 return true;
284 }
285 }
286 return false;
287 }
288
289 /***
290 * @return Returns true if currentItem is the last offer in the list
291 */
292 public boolean isLast() {
293 items = ItemData.getItemsList();
294 CurrentItem currentItem = getCurrentItem();
295 if (currentItem != null) {
296 int index = items.indexOf(currentItem.getItem());
297 if (index == items.size() - 1) {
298 return true;
299 }
300 }
301 return false;
302 }
303
304 /***
305 * @return Returns the current Item
306 */
307 private CurrentItem getCurrentItem() {
308 CurrentItem currentItem = null;
309 try {
310 currentItem = (CurrentItem) Utils
311 .resolveVariable(JSFConstants.CURRENT_ITEM);
312 } catch (NullPointerException n) {
313 LOG.error("could not resolve variable : "
314 + JSFConstants.CURRENT_ITEM);
315 } catch (ClassCastException c) {
316 LOG.error("could not resolve variable : "
317 + JSFConstants.CURRENT_ITEM);
318 }
319 return currentItem;
320 }
321
322 /***
323 * @return config
324 */
325 public JavaCareersConfig getConfig() {
326 return config;
327 }
328
329 /***
330 * @param config String
331 */
332 public void setConfig(JavaCareersConfig config) {
333 this.config = config;
334 }
335
336 /***
337 * Gets the number of items
338 *
339 * @return number of items
340 */
341 public String getItemCount() {
342 int count = itemService.getItemCount();
343 LOG.info("Item count: " + count);
344 return "(" + count + ")";
345 }
346
347 /***
348 * A getter method to be able to acces this variable from within jsf
349 *
350 * @return The static final token key
351 */
352 public String getTokenKey() {
353 String tokenKey = TOKEN_KEY;
354 return tokenKey;
355 }
356
357 /***
358 * @return the list of Javacareers items do display.
359 */
360 public List getItemSpinnerList() {
361
362 if (itemSpinnerList == null) {
363 itemSpinnerList = new ArrayList();
364 List list = bindingList.getBindingList();
365 ItemValueBinding vb = new ItemValueBinding();
366 vb.setMessageKey("label_show_all_item");
367 itemSpinnerList.add(new SelectItem(Utils.getMessage(vb.getMessageKey())));
368 for (Iterator iterator = list.iterator(); iterator.hasNext();) {
369 ItemValueBinding binding = (ItemValueBinding) iterator.next();
370 itemSpinnerList.add(new SelectItem(Utils.getMessage(binding.getMessageKey())));
371 }
372 }
373
374 return itemSpinnerList;
375 }
376
377
378 /***
379 * @param event ValueChangeEvent
380 */
381 public void setDisplayFilter(ValueChangeEvent event) {
382 String filterValue = event.getNewValue().toString();
383 LOG.info("Debug: filterValue = " + filterValue);
384 if (filterValue.equalsIgnoreCase(Utils.getMessage(SHOW_ALL_ITEMS))) {
385 if (isUnApprovedItems()) {
386 ItemData.setItems(itemService.getUnApprovedItems());
387 } else {
388 ItemData.setItems(itemService.getItems());
389 }
390 currentFilter = SHOW_ALL_ITEMS;
391 }
392 List valueBindingList = bindingList.getBindingList();
393 for (Iterator iterator = valueBindingList.iterator(); iterator.hasNext();) {
394 ItemValueBinding binding = (ItemValueBinding) iterator.next();
395 if (filterValue.equalsIgnoreCase(Utils.getMessage(binding.getMessageKey()))) {
396 if (isUnApprovedItems()) {
397 ItemData.setItems(itemService.getUnApprovedItems(binding.getClazz()));
398 } else {
399 ItemData.setItems(itemService.getItems(binding.getClazz()));
400 }
401 currentFilter = binding.getMessageKey();
402 }
403 }
404 }
405
406
407 /***
408 * @return the lines per page
409 */
410 public int getLinesPerPage() {
411 HttpSession session = Utils.getSession();
412 Integer linesPerPage = (Integer) session.getAttribute(LINES_PER_PAGE);
413 if (linesPerPage != null) {
414 return linesPerPage.intValue();
415 }
416 return Integer.parseInt(config.getLinesPerPage());
417 }
418
419 /***
420 * @param event The ValueChangeEvent.
421 */
422 public void setLinesPerPage(ValueChangeEvent event) {
423 HttpSession session = Utils.getSession();
424 LOG.info("Debug: newValue = " + event.getNewValue().toString());
425 int newLinesPerPage = ((Integer) event.getNewValue()).intValue();
426 session.setAttribute(LINES_PER_PAGE, new Integer(newLinesPerPage));
427 }
428
429 /***
430 * @return message
431 */
432 public String getCurrentFilter() {
433 if (currentFilter == null) {
434 currentFilter = SHOW_ALL_ITEMS;
435 }
436 return Utils.getMessage(currentFilter);
437 }
438
439
440 /***
441 * @return unapproved itemcount
442 */
443 public String getUnApprovedItemCount() {
444 int count = itemService.getUnApprovedItemCount();
445
446 return "(" + count + ")";
447 }
448
449
450 /***
451 * List all open Items
452 *
453 * @param actionEvent ActionEvent
454 */
455 public void listUnApprovedItems(ActionEvent actionEvent) {
456 unApprovedItems = true;
457 LOG.info("Debug: executing listAllItems");
458 if (currentFilter == null || currentFilter.equalsIgnoreCase(SHOW_ALL_ITEMS)) {
459 ItemData.setItems(itemService.getUnApprovedItems());
460 } else {
461 List valueBindingList = bindingList.getBindingList();
462 for (Iterator iterator = valueBindingList.iterator(); iterator.hasNext();) {
463 ItemValueBinding binding = (ItemValueBinding) iterator.next();
464 if (currentFilter.equalsIgnoreCase(binding.getMessageKey())) {
465 ItemData.setItems(itemService.getUnApprovedItems(binding.getClazz()));
466 }
467 }
468 }
469
470 }
471
472 /***
473 * @return Returns the unApprovedItems.
474 */
475 public boolean isUnApprovedItems() {
476 return unApprovedItems;
477 }
478
479 /***
480 * @param approvedItems The unApprovedItems to set.
481 */
482 public void setUnApprovedItems(boolean approvedItems) {
483 this.unApprovedItems = approvedItems;
484 }
485
486 /***
487 * @param actionEvent is the generated event
488 *
489 */
490 public void approveItem(ActionEvent actionEvent) {
491 Item item = (Item) itemTable.getRowData();
492 LOG.info("executing approve item");
493 item.setAdminApproved(true);
494 itemService.store(item);
495 ItemData.getItemsList().remove(item);
496 items = ItemData.getItemsList();
497 outcome = "go_unapproved_items";
498 }
499
500 /***
501 * @param actionEvent is the generated event
502 *
503 */
504 public void approveCurrentItem(ActionEvent actionEvent) {
505 Item item = getCurrentItem().getItem();
506 item.setAdminApproved(true);
507 itemService.store(item);
508 ItemData.getItemsList().remove(item);
509 items = ItemData.getItemsList();
510 outcome = "go_unapproved_items";
511 }
512
513 /***
514 * Delete the current Item
515 * @param actionEvent is the generated event
516 */
517 public void deleteItem(ActionEvent actionEvent) {
518 LOG.info("Debug: executing deleteCurrentJobOffer");
519 if (isTokenValid()) {
520 itemFileRemover.deleteFiles(itemToDelete);
521 itemService.delete(itemToDelete);
522 ItemData.getItemsList().remove(itemToDelete);
523 items = ItemData.getItemsList();
524
525 saveToken();
526
527 if (unApprovedItems) {
528 outcome = "go_unapproved_items";
529 listUnApprovedItems(actionEvent);
530 } else {
531 outcome = "go_items";
532 listItems(actionEvent);
533 }
534 } else {
535 ExternalContext externalContext = Utils.getExternalContext();
536 HttpServletRequest request = (HttpServletRequest)
537 externalContext.getRequest();
538 request.setAttribute(JSFConstants.ERROR_CONTROL_FLOW,
539 Utils.getMessage("error_invalid_control_flow"));
540 outcome = JSFConstants.GO_ERROR;
541 }
542 deletePressed = false;
543 }
544
545 /***
546 * cancel the Delete
547 * @return outcome, null to stay on the same page
548 */
549 public String cancelDelete() {
550 LOG.info("Debug: cancel delete item");
551 itemToDelete = null;
552
553 deletePressed = false;
554 return null;
555 }
556
557 /***
558 * @return Returns the deletePressed.
559 */
560 public boolean isDeletePressed() {
561 return deletePressed;
562 }
563
564 /***
565 * @param deletePressed The deletePressed to set.
566 */
567 public void setDeletePressed(boolean deletePressed) {
568 this.deletePressed = deletePressed;
569 }
570
571 /***
572 * @return Returns the itemToDelete.
573 */
574 public Item getItemToDelete() {
575 return itemToDelete;
576 }
577
578 /***
579 * @param itemToDelete The itemToDelete to set.
580 */
581 public void setItemToDelete(Item itemToDelete) {
582 this.itemToDelete = itemToDelete;
583 }
584
585 /***
586 * @return Returns the itemFileRemover.
587 */
588 public ItemFileRemover getItemFileRemover() {
589 return itemFileRemover;
590 }
591
592 /***
593 * @param itemFileRemover The itemFileRemover to set.
594 */
595 public void setItemFileRemover(ItemFileRemover itemFileRemover) {
596 this.itemFileRemover = itemFileRemover;
597 }
598 }
599 /***
600 * $Log: ItemAction.java,v $
601 * Revision 1.18 2005/12/21 11:38:42 shally
602 * *** empty log message ***
603 *
604 * Revision 1.17 2005/12/20 15:36:47 shally
605 * CheckStyle and PMD changes.
606 *
607 * Revision 1.16 2005/12/08 14:53:46 shally
608 * Opkuis voor checkstyle.
609 *
610 * Revision 1.15 2005/10/11 11:43:40 stephan_janssen
611 * Code cleanup.
612 *
613 * Revision 1.14 2005/09/30 14:38:07 bavo_jcs
614 * Fixed URL
615 *
616 * Revision 1.13 2005/09/24 17:30:15 schauwvliege
617 * delete content, refactory of resume
618 *
619 * Revision 1.12 2005/09/23 07:29:48 schauwvliege
620 * delete content refactory for interview
621 *
622 * Revision 1.11 2005/09/22 08:06:46 schauwvliege
623 * Admin delete content
624 *
625 * Revision 1.10 2005/09/21 14:54:54 schauwvliege
626 * Admin delete content
627 *
628 * Revision 1.9 2005/09/20 15:09:29 schauwvliege
629 * Admin approved content V1
630 *
631 * Revision 1.8 2005/09/20 11:04:20 schauwvliege
632 * Admin approved
633 *
634 * Revision 1.7 2005/09/19 16:15:36 schauwvliege
635 * Introduction of Approve items
636 *
637 * Revision 1.6 2005/09/05 16:39:58 schauwvliege
638 * Fixed forward to a friend
639 *
640 * Revision 1.5 2005/09/05 09:20:06 schauwvliege
641 * Removed CurrentJobOffer is now CurrentItem
642 *
643 * Revision 1.4 2005/08/30 13:05:35 psong09
644 * added filter items functionality
645 *
646 * Revision 1.3 2005/08/29 12:45:02 psong09
647 * moved spinner actionListener to ItemAction
648 *
649 * Revision 1.2 2005/08/25 11:36:57 schauwvliege
650 * introduction of mixed list of all items
651 * Revision 1.1 2005/08/24 16:29:59 schauwvliege
652 * introduction of mixed list of all items
653 *
654 */