View Javadoc

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 org.apache.maven.plugin.MojoExecutionException;
19  import org.apache.maven.plugin.MojoFailureException;
20  import org.apache.maven.project.MavenProject;
21  
22  import de.smartics.maven.issue.command.AddMilestoneCommand;
23  import de.smartics.maven.issue.command.CommandResult;
24  import de.smartics.maven.issue.command.ProductNavigationCommand;
25  import de.smartics.maven.issue.util.VersionHelper;
26  
27  /**
28   * Helper to add milestones.
29   */
30  class MojoHelperMilestone extends AbstractMojoHelper
31  {
32  
33    // ********************************* Fields *********************************
34  
35    // --- constants ------------------------------------------------------------
36  
37    // --- members --------------------------------------------------------------
38  
39    // ****************************** Initializer *******************************
40  
41    // ****************************** Constructors ******************************
42  
43    /**
44     * Default constructor.
45     *
46     * @param project the Maven project.
47     * @param commandFactory the helper to create commands.
48     * @param console the console to execute commands.
49     * @see de.smartics.maven.bugzilla.AbstractMojoHelper#AbstractMojoHelper(org.apache.maven.project.MavenProject,
50     *      de.smartics.maven.bugzilla.MavenCommandFactory,
51     *      de.smartics.maven.bugzilla.Console)
52     */
53    protected MojoHelperMilestone(final MavenProject project,
54        final MavenCommandFactory commandFactory, final Console console)
55    {
56      super(project, commandFactory, console);
57    }
58  
59    // ****************************** Inner Classes *****************************
60  
61    // ********************************* Methods ********************************
62  
63    // --- init -----------------------------------------------------------------
64  
65    // --- get&set --------------------------------------------------------------
66  
67    // --- business -------------------------------------------------------------
68  
69    /**
70     * Executes the helpers commands.
71     *
72     * @param product the name of the product whose milestone is to be added.
73     * @param milestone the milestone to add.
74     * @param sortkey the sortkey to the milestone.
75     * @throws MojoExecutionException if an unexpected problem occurs. Throwing
76     *           this exception causes a "BUILD ERROR" message to be displayed.
77     * @throws MojoFailureException if an expected problem (such as a compilation
78     *           failure) occurs. Throwing this exception causes a "BUILD FAILURE"
79     *           message to be displayed.
80     */
81    public final void run(final String product, final String milestone,
82        final String sortkey) throws MojoExecutionException, MojoFailureException
83    {
84      final ProductNavigationCommand navigationCommand =
85          createNavigationCommand(product);
86      console.execute(navigationCommand);
87  
88      final CommandResult<?> navigationResult = navigationCommand.getResult();
89      final String token = navigationResult.getToken();
90      final AddMilestoneCommand addVersionCommand =
91          createAddMilestoneCommand(product, milestone, sortkey, token);
92      console.execute(addVersionCommand);
93    }
94  
95    private ProductNavigationCommand createNavigationCommand(final String product)
96    {
97      final ProductNavigationCommand command =
98          commandFactory.createMilestoneProductNavigationCommand(product);
99      return command;
100   }
101 
102   private AddMilestoneCommand createAddMilestoneCommand(final String product,
103       final String milestone, final String sortkey, final String token)
104   {
105     final VersionHelper helper = new VersionHelper(project);
106     final String nextMilestone = helper.calculateNextVersion(milestone);
107     if (nextMilestone == null)
108     {
109       return null;
110     }
111     final AddMilestoneCommand command =
112         commandFactory.createAddMilestoneCommand(product, nextMilestone,
113             sortkey, token);
114     return command;
115   }
116 
117   /**
118    * Executes the helpers commands.
119    *
120    * @param milestone the milestone to add.
121    * @throws MojoExecutionException if an unexpected problem occurs. Throwing
122    *           this exception causes a "BUILD ERROR" message to be displayed.
123    * @throws MojoFailureException if an expected problem (such as a compilation
124    *           failure) occurs. Throwing this exception causes a "BUILD FAILURE"
125    *           message to be displayed.
126    */
127   public void run(final String product, final String version)
128     throws MojoExecutionException, MojoFailureException
129   {
130     run(product, version, null);
131   }
132 
133   // --- object basics --------------------------------------------------------
134 
135 }