1 /*
2 * Copyright 2012 smartics, Kronseder & Reiner GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package de.smartics.maven.bugzilla;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import org.apache.maven.plugin.MojoExecutionException;
22 import org.apache.maven.plugin.MojoFailureException;
23 import org.apache.maven.project.MavenProject;
24
25 import de.smartics.maven.issue.command.AddComponentCommand;
26 import de.smartics.maven.issue.command.CommandResult;
27 import de.smartics.maven.issue.command.ProductNavigationCommand;
28 import de.smartics.maven.issue.config.Component;
29
30 /**
31 * Helper to add and update product information.
32 */
33 abstract class AbstractMojoHelperProduct extends AbstractMojoHelper
34 {
35
36 // ********************************* Fields *********************************
37
38 // --- constants ------------------------------------------------------------
39
40 // --- members --------------------------------------------------------------
41 /**
42 * The core product information.
43 */
44 protected final ProductInfo productInfo;
45
46 // ****************************** Initializer *******************************
47
48 // ****************************** Constructors ******************************
49
50 // CHECKSTYLE:OFF
51 /**
52 * Default constructor.
53 *
54 * @param project the Maven project.
55 * @param commandFactory the helper to create commands.
56 * @param console the console to execute commands.
57 * @param loader the loader of component descriptions.
58 * @param addDefaultComponentsForMultiModuleProjects the flag that signals to
59 * add the default component descriptions for multi module projects.
60 * @param productInfo the core product information.
61 */
62 protected AbstractMojoHelperProduct(
63 final MavenProject project, // NOPMD
64 final MavenCommandFactory commandFactory, final Console console,
65 final ProductInfo productInfo)
66 {
67 super(project, commandFactory, console);
68
69 this.productInfo = productInfo;
70 }
71
72 // CHECKSTYLE:ON
73
74 // ****************************** Inner Classes *****************************
75
76 /**
77 * Encapsulates the core product information.
78 */
79 static final class ProductInfo
80 {
81 /**
82 * The name of the Bugzilla product.
83 */
84 private final String product;
85
86 /**
87 * The default milestone to the product.
88 */
89 private final String defaultMilestone;
90
91 /**
92 * The initial owner of bug entries for all components of a product.
93 */
94 private final String initialOwner;
95
96 /**
97 * The information about the components.
98 */
99 private final ComponentsInfo components;
100
101 /**
102 * Default construtor.
103 *
104 * @param product the name of the Bugzilla product.
105 * @param defaultMilestone the default milestone to the product.
106 * @param initialOwner the initial owner of bug entries for all components
107 * of a product.
108 * @param components the information about the components.
109 */
110 ProductInfo(final String product, final String defaultMilestone,
111 final String initialOwner, final ComponentsInfo components)
112 {
113 this.product = product;
114 this.defaultMilestone = defaultMilestone;
115 this.initialOwner = initialOwner;
116 this.components = components;
117 }
118 }
119
120 /**
121 * Provides information to load components descriptions and to control what is
122 * to be added.
123 */
124 static final class ComponentsInfo
125 {
126 /**
127 * The loader of component descriptions.
128 */
129 private final ComponentDescriptionLoader loader;
130
131 /**
132 * The flag that signals to add the default component descriptions for multi
133 * module projects. The default descriptions are added in addition to the
134 * names of the sub modules. This has no effect on single modules where the
135 * default component descriptions are always added.
136 */
137 private final boolean addDefaultComponentsForMultiModuleProjects;
138
139 /**
140 * The flag that signals that in the case of a multi module project the sub
141 * module names should not be added as components.
142 */
143 private final boolean suppressSubModuleNamesAsComponents;
144
145 /**
146 * Default constructor.
147 *
148 * @param loader the loader of component descriptions.
149 * @param addDefaultComponentsForMultiModuleProjects the flag that signals
150 * to add the default component descriptions for multi module
151 * projects.
152 * @param suppressSubModuleNamesAsComponents the flag that signals that in
153 * the case of a multi module project the sub module names should
154 * not be added as components.
155 */
156 public ComponentsInfo(final ComponentDescriptionLoader loader,
157 final boolean addDefaultComponentsForMultiModuleProjects,
158 final boolean suppressSubModuleNamesAsComponents)
159 {
160 this.loader = loader;
161
162 this.addDefaultComponentsForMultiModuleProjects =
163 addDefaultComponentsForMultiModuleProjects;
164 this.suppressSubModuleNamesAsComponents =
165 suppressSubModuleNamesAsComponents;
166 }
167 }
168
169 // ********************************* Methods ********************************
170
171 // --- init -----------------------------------------------------------------
172
173 // --- get&set --------------------------------------------------------------
174
175 /**
176 * Returns the name of the Bugzilla product.
177 *
178 * @return the name of the Bugzilla product.
179 */
180 protected final String getProduct()
181 {
182 return productInfo.product;
183 }
184
185 /**
186 * Returns the default milestone to the product.
187 *
188 * @return the default milestone to the product.
189 */
190 protected final String getDefaultMilestone()
191 {
192 return productInfo.defaultMilestone;
193 }
194
195 // --- business -------------------------------------------------------------
196
197 /**
198 * Updates component information of the product.
199 *
200 * @throws MojoFailureException if the result is not as expected.
201 * @throws MojoExecutionException if the default components configuration
202 * cannot be found.
203 */
204 protected final void updateComponents() throws MojoFailureException,
205 MojoExecutionException
206 {
207 final List<Component> components = getComponents();
208 for (final Component component : components)
209 {
210 final ProductNavigationCommand componentNavigationCommand =
211 createComponentNavigationCommand(productInfo.product);
212 console.execute(componentNavigationCommand);
213 final CommandResult<?> componentNavigationResult =
214 componentNavigationCommand.getResult();
215 final String componentToken = componentNavigationResult.getToken();
216 final AddComponentCommand addComponentCommand =
217 createAddComponentCommand(productInfo.product, component,
218 componentToken);
219 console.execute(addComponentCommand);
220 }
221 }
222
223 /**
224 * Returns the component information for a product from the project.
225 *
226 * @return the component information for a product from the project.
227 * @throws MojoExecutionException if the default components configuration
228 * cannot be found.
229 */
230 @SuppressWarnings("unchecked")
231 protected final List<Component> getComponents() throws MojoExecutionException
232 {
233 final List<Component> components;
234
235 final ComponentDescriptionLoader loader = productInfo.components.loader;
236
237 final List<MavenProject> modules = project.getCollectedProjects();
238 if (modules.isEmpty())
239 {
240 components = loader.loadDefaultComponents();
241 }
242 else
243 {
244 components =
245 (productInfo.components.addDefaultComponentsForMultiModuleProjects
246 ? loader.loadDefaultComponents() : new ArrayList<Component>());
247
248 if (!productInfo.components.suppressSubModuleNamesAsComponents)
249 {
250 for (final MavenProject module : modules)
251 {
252 final String moduleName = module.getArtifactId();
253 final String moduleDescription = module.getDescription();
254 final Component component =
255 new Component(moduleName, moduleDescription);
256 components.add(component);
257 }
258 }
259 }
260 return components;
261 }
262
263 /**
264 * Creates a command to the component form.
265 *
266 * @param product the product for which component information is to be
267 * changed.
268 * @return the requested command instance.
269 */
270 protected final ProductNavigationCommand createComponentNavigationCommand(
271 final String product)
272 {
273 final ProductNavigationCommand command =
274 commandFactory.createComponentProductNavigationCommand(product);
275 return command;
276 }
277
278 /**
279 * Creates a command to add a new component.
280 *
281 * @param product the product for which component information is to be
282 * changed.
283 * @param component the component information to add.
284 * @param token the security token from the navigation command.
285 * @return the requested command instance.
286 */
287 protected final AddComponentCommand createAddComponentCommand(
288 final String product, final Component component, final String token)
289 {
290 final AddComponentCommand command =
291 commandFactory.createAddComponentCommand(product, component.getName(),
292 component.getDescription(), productInfo.initialOwner, token);
293 return command;
294 }
295
296 // --- object basics --------------------------------------------------------
297
298 }