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.BuildForm;
19 import org.figure8.join.core.DuplicateEntityException;
20 import org.figure8.join.businessobjects.commons.Release;
21 import org.figure8.join.businessobjects.artifact.Build;
22 import org.figure8.join.businessobjects.artifact.ComponentType;
23 import org.figure8.join.businessfacades.artifact.ArtifactManager;
24 import org.figure8.join.businessfacades.artifact.AssemblyManager;
25 import org.figure8.join.businessfacades.commons.IntegrationProcessManager;
26 import org.figure8.join.util.LogUtil;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.struts.action.ActionForm;
30 import org.apache.struts.action.ActionForward;
31 import org.apache.struts.action.ActionMapping;
32
33 import javax.servlet.http.HttpServletRequest;
34 import javax.servlet.http.HttpServletResponse;
35
36 import java.util.Map;
37 import java.util.List;
38 import java.util.HashMap;
39 import java.util.Iterator;
40 import java.util.ArrayList;
41 /**
42 * This is a Struts action for managing and viewing assemblies within Join system.
43 * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
44 * @version $Revision: 1.1 $
45 *
46 * @struts.action path="/build" name="buildForm"
47 * scope="request" parameter="op" validate="true"
48 * input="/pages/mainpage.jsp?body=/jsp/artifact/builds.jsp"
49 * @struts.action-forward name="Form" path="/jsp/artifact/buildform.jsp"
50 * @struts.action-forward name="Builds" path="/jsp/artifact/builds.jsp"
51 */
52 public class BuildActions extends JoinAction{
53
54
55
56 /** Get a commons logger. */
57 private static final Log log = LogUtil.getLog(AssemblyActions.class);
58
59 /** Operation code fo showing the build creation form */
60 public static final String FORM_OP = "form";
61 /** Operation code for creating a new Build */
62 public static final String CREATE_OP = "create";
63 /** Operation code for searching builds using release */
64 public static final String SEARCH_OP = "search";
65
66 /**
67 * The session scope attribute under which the Build object currently
68 * selected by our logged-in User is stored.
69 */
70 public static final String BUILD_KEY = "build";
71 /**
72 * The request scope attribute under which the retrivied builds
73 * list is stored.
74 */
75 public static final String BUILDS_KEY = "builds";
76 /**
77 * The request scope attribute under which the available components
78 * mapping is stored.
79 */
80 public static final String COMPONENTS_KEY = "componentsMap";
81
82 /**
83 * The request scope attribute under which is stored the Build that
84 * has raised a DuplicateEntityException during save of another one.
85 */
86 public static final String DUPLICATE_BUILD_KEY = "duplicate";
87
88
89
90
91 /** The artifact manager implementation to use. */
92 protected ArtifactManager artifactManager = null;
93 /** The assembly manager implementation to use. */
94 protected AssemblyManager assemblyManager = null;
95 /** The integration process manager implementation to use. */
96 protected IntegrationProcessManager processManager = null;
97
98
99
100
101 /** Creates a new instance of BuildActions */
102 public BuildActions(){
103 }
104
105
106
107
108 /** @param manager The ArtifactManager implementation to use */
109 public void setArtifactManager(ArtifactManager manager){
110 this.artifactManager = manager;
111 }
112 /** @param manager The AssemblyManager implementation to use */
113 public void setAssemblyManager(AssemblyManager manager){
114 this.assemblyManager = manager;
115 }
116 /** @param manager The integration process manager implementation to use */
117 public void setIntegrationProcessManager(IntegrationProcessManager manager){
118 this.processManager = manager;
119 }
120
121
122
123
124 /**
125 *
126 * @param operation String representing the operation to invoke on Action
127 * @param mapping Mapping between forwards name and path for this action
128 * @param form The form object containing request parameters
129 * @param request The servlet container request wrapper
130 * @param response The servlet container response wrapper
131 * @return A forward to the next view to render and display
132 * @throws Exception such as InfraStructureExceptions ...
133 */
134 public ActionForward doExecute(String operation, ActionMapping mapping, ActionForm form,
135 HttpServletRequest request, HttpServletResponse response) throws Exception{
136
137 log.debug("doExecute() called for '" + operation + "' operation");
138
139 if (FORM_OP.equals(operation)){
140 return showBuildForm(mapping, form, request, response);
141 }
142 else if (CREATE_OP.equals(operation)){
143 return createBuild(mapping, form, request, response);
144 }
145 else if (SEARCH_OP.equals(operation)){
146 return searchBuilds(mapping, form, request, response);
147 }
148 else{
149
150 request.getSession().removeAttribute(BUILD_KEY);
151 return mapping.findForward("Builds");
152 }
153 }
154
155
156
157
158 /**
159 * Prepare all we need for displaying a build creation form
160 * @return A forward to the next view to render and display
161 */
162 protected ActionForward showBuildForm(ActionMapping mapping, ActionForm form,
163 HttpServletRequest request, HttpServletResponse response) throws Exception{
164 if (form instanceof BuildForm){
165 BuildForm bForm = (BuildForm)form;
166
167 if (bForm.getReleaseName() != null){
168
169 HashMap componentsMap = new HashMap();
170 Release release = processManager.getRelease(bForm.getReleaseName());
171 List assemblies = assemblyManager.getAssemblies(release);
172 List types = artifactManager.getComponentTypes();
173
174 for (int i=0; i<types.size(); i++){
175 ComponentType type = (ComponentType)types.get(i);
176 componentsMap.put(type, artifactManager.getContainedComponents(type, release));
177 }
178
179
180 bForm.setReleaseName(release.getName());
181 request.setAttribute(COMPONENTS_KEY, componentsMap);
182 request.setAttribute(AssemblyActions.ASSEMBLIES_KEY, assemblies);
183 request.setAttribute(ReleaseActions.RELEASE_KEY, release);
184 }
185
186 return mapping.findForward("Form");
187 }
188
189 return null;
190 }
191
192 /**
193 * Create and register a new Build within Join application. No build for this
194 * release and having this version info should aready exists.
195 * @return A forward to the next view to render and display
196 */
197 protected ActionForward createBuild(ActionMapping mapping, ActionForm form,
198 HttpServletRequest request, HttpServletResponse response) throws Exception{
199 if (form instanceof BuildForm){
200 BuildForm bForm = (BuildForm)form;
201
202
203 List components = new ArrayList();
204 Iterator keys = bForm.getComponents().values().iterator();
205 while (keys.hasNext()){
206 String key = (String)keys.next();
207 if (key != null && key.length() > 0)
208 components.add(artifactManager.getComponent(key));
209 }
210
211 Map extractedComponents = new HashMap();
212 keys = bForm.getExtractedComponents().keySet().iterator();
213 while (keys.hasNext()){
214 String key = (String)keys.next();
215 String value = (String)bForm.getExtractedComponent(key);
216 if (key != null && value != null && key.length() > 0 && value.length() > 0)
217 extractedComponents.put(artifactManager.getComponentType(key), assemblyManager.getAssembly(value));
218 }
219 Release release = processManager.getRelease(bForm.getReleaseName());
220 String composerId = getUserContainer(request).getView().getUser().getLogin();
221
222
223 Build build = new Build(bForm.getVersionInfo(), bForm.getComments(), composerId, release);
224
225 try {assemblyManager.saveBuild(build, components, extractedComponents);}
226 catch (DuplicateEntityException dee){
227
228 request.setAttribute(DUPLICATE_BUILD_KEY, dee.getOriginalEntity());
229 return mapping.findForward("Form");
230 }
231
232 return searchBuilds(mapping, form, request, response);
233 }
234
235 return null;
236 }
237
238 /**
239 * Serch all the builds using the bound release criteria.
240 * @return A forward to the next view to render and display
241 */
242 protected ActionForward searchBuilds(ActionMapping mapping, ActionForm form,
243 HttpServletRequest request, HttpServletResponse response) throws Exception{
244 if (form instanceof BuildForm){
245
246 BuildForm bForm = (BuildForm)form;
247
248
249 Release release = processManager.getRelease(bForm.getReleaseName());
250
251
252 List builds = assemblyManager.getBuilds(release);
253 request.setAttribute(BUILDS_KEY, builds);
254 request.setAttribute(ReleaseActions.RELEASE_KEY, release);
255
256 return mapping.findForward("Builds");
257 }
258
259 return null;
260 }
261 }