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.artifact.versioning.ArtifactVersion;
19  import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
20  import org.apache.maven.project.MavenProject;
21  
22  import de.smartics.maven.bugzilla.AbstractIssueMojo.IssueServer;
23  import de.smartics.maven.issue.command.AddComponentCommand;
24  import de.smartics.maven.issue.command.AddMilestoneCommand;
25  import de.smartics.maven.issue.command.AddProductCommand;
26  import de.smartics.maven.issue.command.AddVersionCommand;
27  import de.smartics.maven.issue.command.ClassificationNavigationCommand;
28  import de.smartics.maven.issue.command.CommandArgument;
29  import de.smartics.maven.issue.command.CommandFactory;
30  import de.smartics.maven.issue.command.LoginCommand;
31  import de.smartics.maven.issue.command.LogoutCommand;
32  import de.smartics.maven.issue.command.ProductNavigationCommand;
33  import de.smartics.maven.issue.command.UpdateProductCommand;
34  
35  /**
36   * Factory to create commands within the Maven environment.
37   */
38  final class MavenCommandFactory
39  { // NOPMD
40    // ********************************* Fields *********************************
41  
42    // --- constants ------------------------------------------------------------
43  
44    // --- members --------------------------------------------------------------
45  
46    /**
47     * The Maven project.
48     */
49    private final MavenProject project;
50  
51    /**
52     * The command factory for issue management commands.
53     */
54    private final CommandFactory commandFactory;
55  
56    // ****************************** Initializer *******************************
57  
58    // ****************************** Constructors ******************************
59  
60    MavenCommandFactory(final MavenProject project,
61        final CommandFactory commandFactory)
62    {
63      this.project = project;
64      this.commandFactory = commandFactory;
65    }
66  
67    // ****************************** Inner Classes *****************************
68  
69    // ********************************* Methods ********************************
70  
71    // --- init -----------------------------------------------------------------
72  
73    // --- get&set --------------------------------------------------------------
74  
75    // --- business -------------------------------------------------------------
76  
77    String calculateNextVersion()
78    {
79      final String version = project.getVersion();
80      final ArtifactVersion artifactVersion = new DefaultArtifactVersion(version);
81      final int nextIncrement = artifactVersion.getIncrementalVersion();
82      final String nextVersion =
83          String.valueOf(artifactVersion.getMajorVersion()) + '.'
84              + artifactVersion.getMajorVersion() + '.' + nextIncrement;
85      return nextVersion;
86    }
87  
88    LoginCommand createLoginCommand(final IssueServer server)
89    {
90      final String loginString = server.getUsername();
91      final CommandArgument<LoginCommand> login =
92          CommandArgument.create(LoginCommand.Parameter.LOGIN, loginString);
93      final String passwordString = server.getPassword();
94      final CommandArgument<LoginCommand> password =
95          CommandArgument.create(LoginCommand.Parameter.PASSWORD, passwordString,
96              true);
97      final LoginCommand command =
98          commandFactory.createLoginCommand(login, password);
99      return command;
100   }
101 
102   AddVersionCommand createAddVersionCommand(final String productString,
103       final String nextVersionString, final String tokenString)
104   {
105     final CommandArgument<AddVersionCommand> product =
106         CommandArgument.create(AddVersionCommand.Parameter.PRODUCT,
107             productString);
108     final CommandArgument<AddVersionCommand> version =
109         CommandArgument.create(AddVersionCommand.Parameter.VERSION,
110             nextVersionString);
111     final CommandArgument<AddVersionCommand> token =
112         CommandArgument.create(AddVersionCommand.Parameter.TOKEN, tokenString);
113     final AddVersionCommand command =
114         commandFactory.createAddVersionCommand(product, version, token);
115     return command;
116   }
117 
118   AddMilestoneCommand createAddMilestoneCommand(final String productString,
119       final String nextMilestoneString, final String sortkeyString,
120       final String tokenString)
121   {
122     final CommandArgument<AddMilestoneCommand> product =
123         CommandArgument.create(AddMilestoneCommand.Parameter.PRODUCT,
124             productString);
125     final CommandArgument<AddMilestoneCommand> milestone =
126         CommandArgument.create(AddMilestoneCommand.Parameter.MILESTONE,
127             nextMilestoneString);
128     final CommandArgument<AddMilestoneCommand> sortkey =
129         CommandArgument.create(AddMilestoneCommand.Parameter.SORTKEY,
130             sortkeyString);
131     final CommandArgument<AddMilestoneCommand> token =
132         CommandArgument
133             .create(AddMilestoneCommand.Parameter.TOKEN, tokenString);
134     final AddMilestoneCommand command =
135         commandFactory.createAddMilestoneCommand(product, milestone, sortkey,
136             token);
137     return command;
138   }
139 
140   // CHECKSTYLE:OFF
141   AddProductCommand createAddProductCommand( // NOPMD
142       final String classificationString, final String productString,
143       final String descriptionString, final String defaultMilestoneString,
144       final String versionString, final String tokenString)
145   {
146     final CommandArgument<AddProductCommand> classification =
147         CommandArgument.create(AddProductCommand.Parameter.CLASSIFICATION,
148             classificationString);
149     final CommandArgument<AddProductCommand> product =
150         CommandArgument.create(AddProductCommand.Parameter.PRODUCT,
151             productString);
152     final CommandArgument<AddProductCommand> description =
153         CommandArgument.create(AddProductCommand.Parameter.DESCRIPTION,
154             descriptionString);
155     final CommandArgument<AddProductCommand> defaultMilestone =
156         CommandArgument.create(AddProductCommand.Parameter.DEFAULT_MILESTONE,
157             defaultMilestoneString);
158     final CommandArgument<AddProductCommand> version =
159         CommandArgument.create(AddProductCommand.Parameter.VERSION,
160             versionString);
161     final CommandArgument<AddProductCommand> token =
162         CommandArgument.create(AddProductCommand.Parameter.TOKEN, tokenString);
163     final AddProductCommand command =
164         commandFactory.createAddProductCommand(classification, product,
165             description, defaultMilestone, version, token);
166     return command;
167   }
168 
169   // CHECKSTYLE:ON
170 
171   // CHECKSTYLE:OFF
172   UpdateProductCommand createUpdateProductCommand( // NOPMD
173       final String productOldString, final String productString,
174       final String descriptionString, final String defaultMilestoneString,
175       final String tokenString)
176   {
177     final CommandArgument<UpdateProductCommand> oldProduct =
178         CommandArgument.create(UpdateProductCommand.Parameter.PRODUCT_OLD_NAME,
179             productOldString);
180     final CommandArgument<UpdateProductCommand> product =
181         CommandArgument.create(UpdateProductCommand.Parameter.PRODUCT,
182             productString);
183     final CommandArgument<UpdateProductCommand> description =
184         CommandArgument.create(UpdateProductCommand.Parameter.DESCRIPTION,
185             descriptionString);
186     final CommandArgument<UpdateProductCommand> defaultMilestone =
187         CommandArgument.create(
188             UpdateProductCommand.Parameter.DEFAULT_MILESTONE,
189             defaultMilestoneString);
190     final CommandArgument<UpdateProductCommand> token =
191         CommandArgument.create(UpdateProductCommand.Parameter.TOKEN,
192             tokenString);
193     final UpdateProductCommand command =
194         commandFactory.createUpdateProductCommand(oldProduct, product,
195             description, defaultMilestone, token);
196     return command;
197   }
198 
199   // CHECKSTYLE:ON
200 
201   AddComponentCommand createAddComponentCommand(final String productString,
202       final String componentString, final String descriptionString,
203       final String initialOwnerString, final String tokenString)
204   {
205     final CommandArgument<AddComponentCommand> product =
206         CommandArgument.create(AddComponentCommand.Parameter.PRODUCT,
207             productString);
208     final CommandArgument<AddComponentCommand> component =
209         CommandArgument.create(AddComponentCommand.Parameter.COMPONENT,
210             componentString);
211     final CommandArgument<AddComponentCommand> description =
212         CommandArgument.create(AddComponentCommand.Parameter.DESCRIPTION,
213             descriptionString);
214     final CommandArgument<AddComponentCommand> initialOwner =
215         CommandArgument.create(AddComponentCommand.Parameter.INITIAL_OWNER,
216             initialOwnerString);
217     final CommandArgument<AddComponentCommand> token =
218         CommandArgument
219             .create(AddComponentCommand.Parameter.TOKEN, tokenString);
220     final AddComponentCommand command =
221         commandFactory.createAddComponentCommand(product, component,
222             description, initialOwner, token);
223     return command;
224   }
225 
226   LogoutCommand createLogoutCommand()
227   {
228     final LogoutCommand command = commandFactory.createLogoutCommand();
229     return command;
230   }
231 
232   // TODO: Create a class for this command
233   ClassificationNavigationCommand createClassificationNavigationCommand(
234       final String classificationString)
235   {
236     final CommandArgument<ClassificationNavigationCommand> classification =
237         CommandArgument.create(
238             ClassificationNavigationCommand.Parameter.CLASSIFICATION,
239             classificationString);
240     final ClassificationNavigationCommand command =
241         commandFactory.createClassificationNavigationCommand(
242             "editproducts.cgi", classification);
243     return command;
244   }
245 
246   // TODO: Create a class for this command
247   ProductNavigationCommand createVersionProductNavigationCommand(
248       final String productString)
249   {
250     return createProductNavigationCommand("editversions.cgi", productString);
251   }
252 
253   // TODO: Create a class for this command
254   ProductNavigationCommand createMilestoneProductNavigationCommand(
255       final String productString)
256   {
257     return createProductNavigationCommand("editmilestones.cgi", productString);
258   }
259 
260   // TODO: Create a class for this command
261   ProductNavigationCommand createComponentProductNavigationCommand(
262       final String productString)
263   {
264     return createProductNavigationCommand("editcomponents.cgi", productString);
265   }
266 
267   // TODO: Create a class for this command
268   ProductNavigationCommand createProductNavigationCommand(
269       final String productString)
270   {
271     final CommandArgument<ProductNavigationCommand> action =
272         CommandArgument.create(ProductNavigationCommand.Parameter.ACTION,
273             "edit");
274     final CommandArgument<ProductNavigationCommand> product =
275         CommandArgument.create(ProductNavigationCommand.Parameter.PRODUCT,
276             productString);
277     final ProductNavigationCommand command =
278         commandFactory.createProductNavigationCommand("editproducts.cgi",
279             action, product);
280     return command;
281   }
282 
283   private ProductNavigationCommand createProductNavigationCommand(
284       final String service, final String productString)
285   {
286     final CommandArgument<ProductNavigationCommand> product =
287         CommandArgument.create(ProductNavigationCommand.Parameter.PRODUCT,
288             productString);
289     final ProductNavigationCommand command =
290         commandFactory.createProductNavigationCommand(service, product);
291     return command;
292   }
293 
294   // --- object basics --------------------------------------------------------
295 
296 }