View Javadoc

1   /*
2    *  DISCLAIMER
3    */
4   package org.flowfuse.base.frontend;
5   
6   import org.apache.commons.logging.Log;
7   import org.apache.commons.logging.LogFactory;
8   import org.flowfuse.base.services.filter.FilterManagementService;
9   import org.springframework.beans.propertyeditors.CustomDateEditor;
10  import org.springframework.validation.BindException;
11  import org.springframework.web.bind.ServletRequestDataBinder;
12  import org.springframework.web.servlet.ModelAndView;
13  import org.springframework.web.servlet.view.RedirectView;
14  
15  import javax.servlet.http.HttpServletRequest;
16  import javax.servlet.http.HttpServletResponse;
17  import java.text.SimpleDateFormat;
18  import java.util.Date;
19  import java.util.List;
20  import java.util.Map;
21  
22  /***
23   * <p>The <code>WorkListController</code> is the controller for the work
24   * list view. It loads all work items for the current user and for the
25   * current filter.</p>
26   *
27   * @author <a href="stefan@flowfuse.org">Stefan Kleineikenscheidt</a>,
28   *         Flowfuse.org
29   * @version $Id: WorkListController.java,v 1.3 2006/01/15 19:56:09 skleinei Exp $
30   */
31  public class WorkListController extends BaseFormController {
32  
33    /***
34     * RCS ID
35     */
36    public final static String rcsid = "$Id: WorkListController.java,v 1.3 2006/01/15 19:56:09 skleinei Exp $";
37  
38    /***
39     * Logger
40     */
41    protected final Log logger = LogFactory.getLog(this.getClass());
42  
43    private FilterManagementService filterManagementService = null;
44  
45    private static final String WORKLIST_FILTER_KEY = "ff.worklistfilter";
46    private static final String WORKITEMS_SESSION_KEY = "ff.workitems";
47  
48  
49    public static final String FILTER_ACTION_KEY = "filter.action";
50    public static final String FILTER_ACTION_RESET = "Reset";
51    public static final String FILTER_ACTION_FILTER = "Filter";
52  
53    public WorkListController() {
54      setFormView("list");
55      setCommandClass(FrontendCommand.class);
56    }
57  
58    protected boolean isFormSubmission(HttpServletRequest request) {
59      if (request.getParameter(FILTER_ACTION_KEY) != null) {
60        return true;
61      } else {
62        return false;
63      }
64    }
65  
66    protected Object formBackingObject(HttpServletRequest request)
67            throws Exception {
68      if (logger.isDebugEnabled()) {
69        logger.debug("Forming backing object.");
70      }
71      FrontendCommand frontendCommand = (FrontendCommand) super.formBackingObject(
72              request);
73  
74      // trying to get filters from session
75      Map filters = (Map) request.getSession().getAttribute(
76              WORKLIST_FILTER_KEY);
77      if (filters == null) {
78        filters = this.filterManagementService.getConfiguredFilters();
79      }
80  
81      frontendCommand.setWorklistFilter(filters);
82  
83      // trying to get worklist from session...
84      List workList = (List) request.getSession().getAttribute(
85              WORKITEMS_SESSION_KEY);
86  
87      // ...if null, query workitem manager
88      if (workList == null) {
89        workList = super.workItemManagementService.queryWorkItems(filters);
90      }
91      frontendCommand.setWorkList(workList);
92  
93      return frontendCommand;
94    }
95  
96    protected void initBinder(HttpServletRequest request,
97            ServletRequestDataBinder binder)
98            throws Exception {
99      SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
100     sdf.setLenient(true);
101     binder.registerCustomEditor(Date.class,
102             new CustomDateEditor(sdf, true));
103   }
104 
105   /*protected Map referenceData(HttpServletRequest request,
106           Object command,
107           Errors errors) throws Exception {
108     FrontendCommand frontendCommand = (FrontendCommand) command;
109     Map worklistFilters = frontendCommand.getWorklistFilter();
110 
111     Map referenceData = new HashMap();
112     List configuredFilters = this.filterManagementService.getConfiguredFilters();
113 
114     for (int i = 0; i < configuredFilters.size(); i++) {
115       WorklistFilter filter = (WorklistFilter) configuredFilters.get(i);
116 
117       if((filter != null) && (filter instanceof MultipleSelectFilter)) {
118         MultipleSelectFilter multipleSelectFilter = (MultipleSelectFilter) filter;
119         List options = multipleSelectFilter.getOptions();
120         List selectedOptions = (List) worklistFilters.get(filter.getKey());
121 
122         if(selectedOptions != null) {
123           // TODO this has to be done better!!
124           List filterOptionsClone = new ArrayList();
125 
126           for (int j = 0; j < options.size(); j++) {
127             SelectFilterOption filterOption = (SelectFilterOption) options.get(j);
128             SelectFilterOption filterOptionClone = (SelectFilterOption) filterOption.clone();
129             String optionName = filterOption.getText();
130             if (selectedOptions.contains(optionName)) {
131               filterOptionClone.setSelected(true);
132               filterOption.setSelected(true);
133             }
134             filterOptionsClone.add(filterOptionClone);
135           }
136 
137           referenceData.put(filter.getKey(), filterOptionsClone);
138         }
139       }
140     }
141 
142     referenceData.put("filters", configuredFilters);
143 
144     return referenceData;
145   }*/
146 
147   protected ModelAndView onSubmit(HttpServletRequest request,
148           HttpServletResponse response,
149           Object command,
150           BindException errors) throws Exception {
151 
152     FrontendCommand frontendCommand = (FrontendCommand) command;
153     List workList = frontendCommand.getWorkList();
154     Map workListFilter = frontendCommand.getWorklistFilter();
155     String filterAction = request.getParameter(FILTER_ACTION_KEY);
156 
157     // Reset Filter
158     if ((filterAction != null) &&
159             FILTER_ACTION_RESET.equals(filterAction)) {
160       logger.debug("Resetting worklist ilter.");
161       workListFilter = null;
162     }
163 
164     // If filter has changed, get new worklist.
165     if (workListFilter !=
166             request.getSession().getAttribute(WORKLIST_FILTER_KEY)) {
167       logger.debug("New worklist filter, therefore getting new worklist.");
168     }
169 
170       workList = super.workItemManagementService.queryWorkItems(
171               workListFilter);
172 
173     // Write filter and workitems back to session
174     request.getSession().setAttribute(WORKITEMS_SESSION_KEY, workList);
175     request.getSession().setAttribute(WORKLIST_FILTER_KEY, workListFilter);
176 
177     return new ModelAndView(new RedirectView("list"));
178   }
179 
180   public FilterManagementService getFilterManagementService() {
181     return filterManagementService;
182   }
183 
184   public void setFilterManagementService(
185           FilterManagementService filterManagementService) {
186     this.filterManagementService = filterManagementService;
187   }
188 
189 }
190 
191 /* EOF */