1 /**
2 * Copyright 2005-2006 the original author or authors.
3 *
4 * Licensed under the Gnu General Pubic License, Version 2.0 (the
5 * "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.opensource.org/licenses/gpl-license.php
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the Gnu General Public License for more details.
14 */
15 package org.figure8.join.control.action;
16
17 import org.figure8.join.control.JoinAction;
18 import org.figure8.join.control.form.TargetForm;
19 import org.figure8.join.core.DuplicateEntityException;
20 import org.figure8.join.businessobjects.commons.Target;
21 import org.figure8.join.businessfacades.commons.IntegrationProcessManager;
22 import org.figure8.join.util.LogUtil;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.beanutils.PropertyUtils;
26 import org.apache.struts.action.ActionForm;
27 import org.apache.struts.action.ActionForward;
28 import org.apache.struts.action.ActionMapping;
29
30 import javax.servlet.http.HttpSession;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
33 /**
34 * Struts action used for managing targets within Join application.
35 * @author <a href="mailto:jerome.evrard@gmail.com">Jerome Evrard</a>
36 * @version $Revision: 1.1 $
37 *
38 * @struts.action path="/target" name="targetForm"
39 * scope="request" parameter="op" validate="true"
40 * input="/pages/mainpage.jsp?body=/jsp/targets.jsp"
41 * @struts.action-forward name="Targets" path="/jsp/targets.jsp"
42 */
43 public class TargetActions extends JoinAction{
44
45
46
47 /** Get a commons logger. */
48 private static final Log log = LogUtil.getLog(TargetActions.class);
49
50 /** Operation code for loading a specific Target */
51 public static final String LOAD_OP = "load";
52 /** Operation code for saving a Target (create or update) */
53 public static final String SAVE_OP = "save";
54
55 /**
56 * The session scope attribute under which the Target object currently
57 * selected by our logged-in User is stored.
58 */
59 public static final String TARGET_KEY = "target";
60 /**
61 * The request scope attribute under which the retrieved Targets
62 * list is stored.
63 */
64 public static final String TARGETS_KEY = "targets";
65 /**
66 * The request scope attribute under which is stored the Target that
67 * has raised a DuplicateEntityException during save of another one.
68 */
69 public static final String DUPLICATE_TARGET_KEY = "duplicate";
70
71
72
73
74 /** Join integration process manager. */
75 protected IntegrationProcessManager processManager;
76
77
78
79
80 /** Creates a new instance of TargetActions */
81 public TargetActions(){
82 }
83
84
85
86
87 /** @param manager <code>IntegrationProcessManager</code> implementation instance */
88 public void setIntegrationProcessManager(IntegrationProcessManager manager){
89 this.processManager = manager;
90 }
91
92
93
94
95 /**
96 *
97 * @param operation String representing the operation to invoke on Action
98 * @param mapping Mapping between forwards name and path for this action
99 * @param form The form object containing request parameters
100 * @param request The servlet container request wrapper
101 * @param response The servlet container response wrapper
102 * @return A forward to the next view to render and display
103 * @throws Exception such as InfraStructureExceptions ...
104 */
105 public ActionForward doExecute(String operation, ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{
106
107 log.debug("doExecute() called for '" + operation + "' operation");
108
109 if (LOAD_OP.equals(operation)){
110 return loadTarget(mapping, form, request, response);
111 }
112 else if (SAVE_OP.equals(operation)){
113 return saveTarget(mapping, form, request, response);
114 }
115 else{
116
117 request.getSession().removeAttribute(TARGET_KEY);
118
119 request.setAttribute(TARGETS_KEY, processManager.getTargets());
120 return mapping.findForward("Targets");
121 }
122 }
123
124
125
126
127 /**
128 * Load a specified target from datastore and fill form with it.
129 * @return A forward to the next view to render and display
130 */
131 protected ActionForward loadTarget(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
132 throws Exception{
133
134 if (form instanceof TargetForm){
135
136 TargetForm tForm = (TargetForm)form;
137 Target target = processManager.getTarget(tForm.getName());
138
139
140 HttpSession session = request.getSession();
141 session.setAttribute(TARGET_KEY, target);
142
143 PropertyUtils.copyProperties(tForm, target);
144
145
146 request.setAttribute(TARGETS_KEY, processManager.getTargets());
147 return mapping.findForward("Targets");
148 }
149
150 return null;
151 }
152
153 /**
154 * Save a target into datastore. The Target may be an already existing one or a
155 * new one (this is indeed a create or update method).
156 * @return A forward to the next view to render and display
157 */
158 protected ActionForward saveTarget(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
159 throws Exception{
160
161 if (form instanceof TargetForm){
162
163 TargetForm tForm = (TargetForm)form;
164
165
166 HttpSession session = request.getSession();
167 Target target = (Target)session.getAttribute(TARGET_KEY);
168
169
170 if (target == null){
171 log.info("Creation of a new Target with name: " + tForm.getName());
172 target = new Target(tForm.getName(), tForm.getDescription());
173 }
174 else{
175 log.info("Update of existing Target having name: " + target.getName());
176 PropertyUtils.copyProperties(target, tForm);
177 }
178
179 try {processManager.saveTarget(target);}
180 catch (DuplicateEntityException dee){
181
182 request.setAttribute(DUPLICATE_TARGET_KEY, dee.getOriginalEntity());
183 request.setAttribute(TARGETS_KEY, processManager.getTargets());
184 return mapping.findForward("Targets");
185 }
186
187
188 session.removeAttribute(TARGET_KEY);
189 removeObsoleteForm(mapping, request);
190
191
192 request.setAttribute(TARGETS_KEY, processManager.getTargets());
193 return mapping.findForward("Targets");
194 }
195
196 return null;
197 }
198 }