View Javadoc

1   package com.panogenesis.webapp.action;
2   
3   import java.util.Date;
4   import java.util.Locale;
5   
6   import javax.servlet.http.HttpServletRequest;
7   import javax.servlet.http.HttpServletResponse;
8   
9   import org.apache.commons.lang.StringUtils;
10  import com.panogenesis.model.Component;
11  import com.panogenesis.service.ComponentManager;
12  import org.springframework.validation.BindException;
13  import org.springframework.web.servlet.ModelAndView;
14  import org.springframework.web.servlet.view.RedirectView;
15  
16  public class ComponentFormController extends BaseFormController {
17      private ComponentManager componentManager = null;
18  
19      public void setComponentManager(ComponentManager componentManager) {
20          this.componentManager = componentManager;
21      }
22   
23      /***
24       * Default behavior for FormControllers - redirect to the successView
25       * when the cancel button has been pressed.
26       */
27      public ModelAndView processFormSubmission(HttpServletRequest request,
28                                                HttpServletResponse response,
29                                                Object command,
30                                                BindException errors)
31      throws Exception {
32          if (request.getParameter("cancel") != null) {
33              return new ModelAndView(new RedirectView("editProject.html?id=" + request.getSession().getAttribute("current_project_id")));
34          }
35          return super.processFormSubmission(request, response, command, errors);
36      }
37  
38      protected Object formBackingObject(HttpServletRequest request)
39      throws Exception {
40          String id = request.getParameter("id");
41  /*        
42          if (!StringUtils.isEmpty(id)){
43          	request.getSession().setAttribute("current_component_id", id);
44          }
45  */
46          log.debug("ComponentFormController  - entering 'formBackingObject' method - comp id:" + id);
47          Component component = null;
48  //        String hold_id = (String)request.getSession().getAttribute("current_component_id");
49          if (!StringUtils.isEmpty(id)) {
50          	 log.debug("ComponentFormController  - entering 'formBackingObject' method - hold id:" + id);
51              component = componentManager.getComponent(id);
52          } else {
53          	 log.debug("ComponentFormController  NEW COMPONENT- entering 'formBackingObject' method - hold id:" + id);
54              component = new Component();
55          }
56  
57          return component;
58      }
59  
60      public ModelAndView onSubmit(HttpServletRequest request,
61                                   HttpServletResponse response, Object command,
62                                   BindException errors)
63      throws Exception {
64          if (log.isDebugEnabled()) {
65              log.debug("ComponentFormController - entering 'onSubmit' method...");
66          }
67  
68          Component component = (Component) command;
69          String current_component_id = (String)request.getParameter("id");
70          boolean isNew = ( component.getId()== null);
71          String success = getSuccessView();
72          Locale locale = request.getLocale();
73          String project_id = (String)request.getSession().getAttribute("current_project_id");
74          log.debug("ComponentFormController - 'onSubmit' method - comp id: " + current_component_id);
75          log.debug("ComponentFormController - 'onSubmit' method - comp id from comp: " + component.getId());
76          if (request.getParameter("delete") != null) {
77          	
78              componentManager.removeComponent(component.getId().toString());
79  
80              saveMessage(request, getText("component.deleted", locale));
81          } 
82          else {  
83              log.debug("current project id is " + project_id);
84              log.debug("component project id is " + component.getProjectId());
85              log.debug("isNew ? :  " + isNew);
86          	if (isNew){
87          		log.debug("setting new component information (proj id): " + project_id);
88          		component.setDateCreated(new Date());
89          		component.setProjectId(new Integer(project_id));
90          	}
91          	component.setLastModified(new Date());
92          	componentManager.saveComponent(component);
93  
94              String key = (isNew) ? "component.added" : "component.updated";
95              saveMessage(request, getText(key, locale));
96  
97              if (!isNew) {
98                  success = "editComponent.html?id=" + component.getId();
99              }
100         }     
101         return new ModelAndView(new RedirectView("editProject.html?id=" + project_id));
102     }
103 }