View Javadoc

1   /**
2    * ComponentTypeActions.java
3    *
4    * Created on 6 oct. 2006, 12:30:38
5    */
6   package org.figure8.join.control.action;
7   
8   import org.figure8.join.control.JoinAction;
9   import org.figure8.join.control.form.ComponentTypeForm;
10  import org.figure8.join.core.DuplicateEntityException;
11  import org.figure8.join.businessobjects.artifact.ComponentType;
12  import org.figure8.join.businessfacades.artifact.ArtifactManager;
13  import org.figure8.join.util.LogUtil;
14  
15  import org.apache.commons.logging.Log;
16  import org.apache.commons.beanutils.PropertyUtils;
17  import org.apache.struts.action.ActionForm;
18  import org.apache.struts.action.ActionForward;
19  import org.apache.struts.action.ActionMapping;
20  
21  import javax.servlet.http.HttpSession;
22  import javax.servlet.http.HttpServletRequest;
23  import javax.servlet.http.HttpServletResponse;
24  /**
25   * Struts action used for managing Component categories (types) within Join application
26   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
27   * @version $Revision: 1.1 $
28   *
29   * @struts.action path="/componentType" name="componentTypeForm"
30   *                scope="request" parameter="op" validate="true"
31   *                input="/pages/mainpage.jsp?body=/jsp/componenttypes.jsp"
32   * @struts.action-forward name="ComponentTypes" path="/jsp/componenttypes.jsp"
33   */
34  public class ComponentTypeActions extends JoinAction{
35  
36     // Static -------------------------------------------------------------------
37  
38     /** Get a commons logger. */
39     private static final Log log = LogUtil.getLog(ComponentTypeActions.class);
40  
41     /** Operation code for loading a specific ComponentType */
42     public static final String LOAD_OP = "load";
43     /** Operation code for saving a ComponentType (create or update) */
44     public static final String SAVE_OP = "save";
45  
46     /**
47      * The session scope attribute under which the ComponentType object currently
48      * selected by our logged-in User is stored
49      */
50     public static final String COMPONENT_TYPE_KEY = "componentType";
51     /**
52      * The request scope attribute under which the ComponentTypes is stored
53      */
54     public static final String COMPONENT_TYPES_KEY = "componentTypes";
55     /**
56      * The request scope attribute under which is stored the ComponentType that
57      * has raised a DuplicateEntityException during save of another one.
58      */
59     public static final String DUPLICATE_COMPONENT_TYPE_KEY = "duplicate";
60  
61  
62     // Attributes ---------------------------------------------------------------
63  
64     /** Join artifact manager instance. */
65     protected ArtifactManager artifactManager;
66  
67  
68     // Constructors -------------------------------------------------------------
69  
70     /** Creates a new instance of ComponentTypeActions */
71     public ComponentTypeActions(){
72     }
73  
74  
75     // Public -------------------------------------------------------------------
76  
77     /** @param manager <code>ArtifactManager</code> implementation instance */
78     public void setArtifactManager(ArtifactManager manager){
79        this.artifactManager = manager;
80     }
81  
82  
83     // Implementation of JoinAction ---------------------------------------------
84  
85     /**
86      *
87      * @param operation String representing the operation to invoke on Action
88      * @param mapping Mapping between forwards name and path for this action
89      * @param form The form object containing request parameters
90      * @param request The servlet container request wrapper
91      * @param response The servlet container response wrapper
92      * @return A forward to the next view to render and display
93      * @throws Exception such as InfraStructureExceptions ...
94      */
95     public ActionForward doExecute(String operation, ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{
96        // Trace operation to execute.
97        log.debug("doExecute() called for '" + operation + "' operation");
98  
99        if (LOAD_OP.equals(operation)){
100          return loadComponentType(mapping, form, request, response);
101       }
102       else if (SAVE_OP.equals(operation)){
103          return saveComponentType(mapping, form, request, response);
104       }
105       else{
106          // Default : empty component types management page.
107          request.setAttribute(COMPONENT_TYPES_KEY, artifactManager.getComponentTypes());
108          request.getSession().removeAttribute(COMPONENT_TYPE_KEY);
109          return mapping.findForward("ComponentTypes");
110       }
111    }
112 
113 
114    // Protected ----------------------------------------------------------------
115 
116    /**
117     * Load a specified component type from datastore and fill form with it.
118     * @return A forward to the next view to render and display
119     */
120    protected ActionForward loadComponentType(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
121          throws Exception{
122 
123       if (form instanceof ComponentTypeForm){
124          // Get the request component type from manager.
125          ComponentTypeForm tForm = (ComponentTypeForm)form;
126          ComponentType type = artifactManager.getComponentType(tForm.getKey());
127 
128          // Store type within session.
129          HttpSession session = request.getSession();
130          session.setAttribute(COMPONENT_TYPE_KEY, type);
131          // Copy into form the type properties.
132          PropertyUtils.copyProperties(tForm, type);
133 
134          // Forward after having refreshed component types.
135          request.setAttribute(COMPONENT_TYPES_KEY, artifactManager.getComponentTypes());
136          return mapping.findForward("ComponentTypes");
137       }
138       // This should not happen...
139       return null;
140    }
141 
142    /**
143     * Save a component type into datastore. The type may be an already existing one or a
144     * new one (this is indeed a create or update method).
145     * @return A forward to the next view to render and display
146     */
147    protected ActionForward saveComponentType(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
148          throws Exception{
149 
150       if (form instanceof ComponentTypeForm){
151          // Get the component type form.
152          ComponentTypeForm tForm = (ComponentTypeForm)form;
153 
154          // Is there's a type to update in session ?
155          HttpSession session = request.getSession();
156          ComponentType type = (ComponentType)session.getAttribute(COMPONENT_TYPE_KEY);
157 
158          if (type == null){
159             log.info("Creation of a new ComponentType with key: " + tForm.getKey());
160             type = new ComponentType(tForm.getKey(), tForm.getLabel(), tForm.getKeyTemplate(), tForm.getDescription());
161          }
162          else{
163             log.info("Update of existing ComponentType having key: " + type.getKey());
164             PropertyUtils.copyProperties(type, tForm);
165          }
166          try {artifactManager.saveComponentType(type);}
167          catch (DuplicateEntityException dee){
168             // Store exception within request.
169             request.setAttribute(COMPONENT_TYPES_KEY, artifactManager.getComponentTypes());
170             request.setAttribute(DUPLICATE_COMPONENT_TYPE_KEY, dee.getOriginalEntity());
171             return mapping.findForward("ComponentTypes");
172          }
173          // Remove form and session attribute if present.
174          session.removeAttribute(COMPONENT_TYPE_KEY);
175          removeObsoleteForm(mapping, request);
176 
177          // Refresh component type list and forward.
178          request.setAttribute(COMPONENT_TYPES_KEY, artifactManager.getComponentTypes());
179          return mapping.findForward("ComponentTypes");
180       }
181       // This should not happen...
182       return null;
183    }
184 }
185