View Javadoc

1   /*
2    * Copyright 2012-2013 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      * Returns the initial owner of bug entries for all components of a product.
121      *
122      * @return the initial owner of bug entries for all components of a product.
123      */
124     String getInitialOwner()
125     {
126       return initialOwner;
127     }
128 
129   }
130 
131   /**
132    * Provides information to load components descriptions and to control what is
133    * to be added.
134    */
135   static final class ComponentsInfo
136   {
137     /**
138      * The loader of component descriptions.
139      */
140     private final ComponentDescriptionLoader loader;
141 
142     /**
143      * The flag that signals to add the default component descriptions for multi
144      * module projects. The default descriptions are added in addition to the
145      * names of the sub modules. This has no effect on single modules where the
146      * default component descriptions are always added.
147      */
148     private final boolean addDefaultComponentsForMultiModuleProjects;
149 
150     /**
151      * The flag that signals that in the case of a multi module project the sub
152      * module names should not be added as components.
153      */
154     private final boolean suppressSubModuleNamesAsComponents;
155 
156     /**
157      * Default constructor.
158      *
159      * @param loader the loader of component descriptions.
160      * @param addDefaultComponentsForMultiModuleProjects the flag that signals
161      *          to add the default component descriptions for multi module
162      *          projects.
163      * @param suppressSubModuleNamesAsComponents the flag that signals that in
164      *          the case of a multi module project the sub module names should
165      *          not be added as components.
166      */
167     public ComponentsInfo(final ComponentDescriptionLoader loader,
168         final boolean addDefaultComponentsForMultiModuleProjects,
169         final boolean suppressSubModuleNamesAsComponents)
170     {
171       this.loader = loader;
172 
173       this.addDefaultComponentsForMultiModuleProjects =
174           addDefaultComponentsForMultiModuleProjects;
175       this.suppressSubModuleNamesAsComponents =
176           suppressSubModuleNamesAsComponents;
177     }
178   }
179 
180   // ********************************* Methods ********************************
181 
182   // --- init -----------------------------------------------------------------
183 
184   // --- get&set --------------------------------------------------------------
185 
186   /**
187    * Returns the name of the Bugzilla product.
188    *
189    * @return the name of the Bugzilla product.
190    */
191   protected final String getProduct()
192   {
193     return productInfo.product;
194   }
195 
196   /**
197    * Returns the default milestone to the product.
198    *
199    * @return the default milestone to the product.
200    */
201   protected final String getDefaultMilestone()
202   {
203     return productInfo.defaultMilestone;
204   }
205 
206   // --- business -------------------------------------------------------------
207 
208   /**
209    * Updates component information of the product.
210    *
211    * @throws MojoFailureException if the result is not as expected.
212    * @throws MojoExecutionException if the default components configuration
213    *           cannot be found.
214    */
215   protected final void updateComponents() throws MojoFailureException,
216     MojoExecutionException
217   {
218     final List<Component> components = getComponents();
219     for (final Component component : components)
220     {
221       final ProductNavigationCommand componentNavigationCommand =
222           createComponentNavigationCommand(productInfo.product);
223       console.execute(componentNavigationCommand);
224       final CommandResult<?> componentNavigationResult =
225           componentNavigationCommand.getResult();
226       final String componentToken = componentNavigationResult.getToken();
227       final AddComponentCommand addComponentCommand =
228           createAddComponentCommand(productInfo.product, component,
229               componentToken);
230       console.execute(addComponentCommand);
231     }
232   }
233 
234   /**
235    * Returns the component information for a product from the project.
236    *
237    * @return the component information for a product from the project.
238    * @throws MojoExecutionException if the default components configuration
239    *           cannot be found.
240    */
241   @SuppressWarnings("unchecked")
242   protected final List<Component> getComponents() throws MojoExecutionException
243   {
244     final List<Component> components;
245 
246     final ComponentDescriptionLoader loader = productInfo.components.loader;
247 
248     final List<MavenProject> modules = project.getCollectedProjects();
249     if (modules.isEmpty())
250     {
251       components = loader.loadDefaultComponents();
252     }
253     else
254     {
255       components =
256           (productInfo.components.addDefaultComponentsForMultiModuleProjects
257               ? loader.loadDefaultComponents() : new ArrayList<Component>());
258 
259       if (!productInfo.components.suppressSubModuleNamesAsComponents)
260       {
261         for (final MavenProject module : modules)
262         {
263           final String moduleName = module.getArtifactId();
264           final String moduleDescription = module.getDescription();
265           final Component component =
266               new Component(moduleName, moduleDescription);
267           components.add(component);
268         }
269       }
270     }
271     return components;
272   }
273 
274   /**
275    * Creates a command to the component form.
276    *
277    * @param product the product for which component information is to be
278    *          changed.
279    * @return the requested command instance.
280    */
281   protected final ProductNavigationCommand createComponentNavigationCommand(
282       final String product)
283   {
284     final ProductNavigationCommand command =
285         commandFactory.createComponentProductNavigationCommand(product);
286     return command;
287   }
288 
289   /**
290    * Creates a command to add a new component.
291    *
292    * @param product the product for which component information is to be
293    *          changed.
294    * @param component the component information to add.
295    * @param token the security token from the navigation command.
296    * @return the requested command instance.
297    */
298   protected final AddComponentCommand createAddComponentCommand(
299       final String product, final Component component, final String token)
300   {
301     final AddComponentCommand command =
302         commandFactory.createAddComponentCommand(product, component.getName(),
303             component.getDescription(), productInfo.initialOwner, token);
304     return command;
305   }
306 
307   // --- object basics --------------------------------------------------------
308 
309 }