View Javadoc

1   /*
2    *  DISCLAIMER
3    */
4   
5   package org.flowfuse.base.frontend;
6   
7   import org.apache.commons.logging.Log;
8   import org.apache.commons.logging.LogFactory;
9   import org.flowfuse.base.model.Comment;
10  import org.flowfuse.base.model.WorkItem;
11  import org.flowfuse.base.model.user.User;
12  import org.flowfuse.base.util.FrontendUtil;
13  import org.flowfuse.holiday.model.HolidayApplication;
14  import org.springframework.beans.propertyeditors.CustomDateEditor;
15  import org.springframework.web.bind.ServletRequestDataBinder;
16  import org.springframework.web.servlet.ModelAndView;
17  import org.springframework.web.servlet.view.RedirectView;
18  
19  import javax.servlet.http.HttpServletRequest;
20  import java.text.SimpleDateFormat;
21  import java.util.Date;
22  
23  /***
24   * The CreateNewWorkflowForm controls the creation of a new workflow.
25   *
26   * @author <a href="stefan@flowfuse.org">Stefan Kleineikenscheidt</a>,
27   *         Flowfuse.org
28   * @version $Id: CreateWorkItemForm.java,v 1.1 2005/11/20 15:02:04 skleinei Exp $
29   */
30  public class CreateWorkItemForm extends BaseFormController {
31  
32    /* RCS ID */
33    public final static String rcsid = "$Id: CreateWorkItemForm.java,v 1.1 2005/11/20 15:02:04 skleinei Exp $";
34  
35    /* Logger */
36    protected final Log logger = LogFactory.getLog(this.getClass());
37  
38    public CreateWorkItemForm() {
39      setFormView("create");
40      setCommandClass(FrontendCommand.class);
41    }
42  
43    protected Object formBackingObject(HttpServletRequest request)
44            throws Exception {
45      FrontendCommand frontendCommand = (FrontendCommand) super.formBackingObject(
46              request);
47  
48      HolidayApplication afl = new HolidayApplication();
49      WorkItem workItem = this.workItemManagementService.createWorkItem();
50  
51      User employee = frontendCommand.getUser();
52      afl.setEmployee(employee);
53  
54      workItem.setPayload(afl);
55      frontendCommand.setWorkItem(workItem);
56      return frontendCommand;
57    }
58  
59    protected void initBinder(HttpServletRequest request,
60            ServletRequestDataBinder binder)
61            throws Exception {
62      SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
63      sdf.setLenient(true);
64      binder.registerCustomEditor(Date.class,
65              new CustomDateEditor(sdf, true));
66    }
67  
68    protected ModelAndView onSubmit(Object command) throws Exception {
69      FrontendCommand frontendCommand = (FrontendCommand) command;
70      WorkItem workItem = frontendCommand.getWorkItem();
71      Comment comment = frontendCommand.getComment();
72  
73      if ((comment.getText() != null) && (comment.getText() != "")) {
74        String caller = FrontendUtil.getCaller();
75        comment.setAuthor(caller);
76        comment.setCreationDate(new Date());
77        workItem.addComment(comment);
78      }
79  
80      // initialize workflow
81      workItem.initializeWorkflow();
82  
83      return new ModelAndView(new RedirectView("view"));
84    }
85  
86  }
87  
88  /* EOF */