View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.scm.command.changelog;
20  
21  import java.util.Date;
22  
23  import org.apache.maven.scm.CommandParameter;
24  import org.apache.maven.scm.CommandParameters;
25  import org.apache.maven.scm.ScmBranch;
26  import org.apache.maven.scm.ScmException;
27  import org.apache.maven.scm.ScmFileSet;
28  import org.apache.maven.scm.ScmResult;
29  import org.apache.maven.scm.ScmVersion;
30  import org.apache.maven.scm.command.AbstractCommand;
31  import org.apache.maven.scm.provider.ScmProviderRepository;
32  
33  /**
34   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
35   * @author Olivier Lamy
36   */
37  public abstract class AbstractChangeLogCommand extends AbstractCommand implements ChangeLogCommand {
38      @Deprecated
39      protected abstract ChangeLogScmResult executeChangeLogCommand(
40              ScmProviderRepository repository,
41              ScmFileSet fileSet,
42              Date startDate,
43              Date endDate,
44              ScmBranch branch,
45              String datePattern)
46              throws ScmException;
47  
48      @Deprecated
49      protected ChangeLogScmResult executeChangeLogCommand(
50              ScmProviderRepository repository,
51              ScmFileSet fileSet,
52              ScmVersion startVersion,
53              ScmVersion endVersion,
54              String datePattern)
55              throws ScmException {
56          throw new ScmException("Unsupported method for this provider.");
57      }
58  
59      @Deprecated
60      protected ChangeLogScmResult executeChangeLogCommand(
61              ScmProviderRepository repository, ScmFileSet fileSet, ScmVersion version, String datePattern)
62              throws ScmException {
63          throw new ScmException("Unsupported method for this provider.");
64      }
65  
66      /**
67       * {@inheritDoc}
68       */
69      public ScmResult executeCommand(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
70              throws ScmException {
71          Date startDate = parameters.getDate(CommandParameter.START_DATE, null);
72  
73          Date endDate = parameters.getDate(CommandParameter.END_DATE, null);
74  
75          int numDays = parameters.getInt(CommandParameter.NUM_DAYS, 0);
76  
77          Integer limit = parameters.getInt(CommandParameter.LIMIT, -1);
78          if (limit < 1) {
79              limit = null;
80          }
81  
82          ScmBranch branch = (ScmBranch) parameters.getScmVersion(CommandParameter.BRANCH, null);
83  
84          ScmVersion version = parameters.getScmVersion(CommandParameter.SCM_VERSION, null);
85  
86          ScmVersion startVersion = parameters.getScmVersion(CommandParameter.START_SCM_VERSION, null);
87  
88          ScmVersion endVersion = parameters.getScmVersion(CommandParameter.END_SCM_VERSION, null);
89  
90          String datePattern = parameters.getString(CommandParameter.CHANGELOG_DATE_PATTERN, null);
91  
92          boolean versionOnly = startVersion == null && endVersion == null && version != null;
93  
94          if (versionOnly) {
95              return executeChangeLogCommand(repository, fileSet, version, datePattern);
96          } else if (startVersion != null || endVersion != null) {
97              return executeChangeLogCommand(repository, fileSet, startVersion, endVersion, datePattern);
98          } else {
99              if (numDays != 0 && (startDate != null || endDate != null)) {
100                 throw new ScmException("Start or end date cannot be set if num days is set.");
101             }
102 
103             if (endDate != null && startDate == null) {
104                 throw new ScmException("The end date is set but the start date isn't.");
105             }
106 
107             if (numDays > 0) {
108                 @SuppressWarnings("checkstyle:magicnumber")
109                 int day = 24 * 60 * 60 * 1000;
110                 startDate = new Date(System.currentTimeMillis() - (long) numDays * day);
111 
112                 endDate = new Date(System.currentTimeMillis() + (long) day);
113             } else if (endDate == null) {
114                 endDate = new Date();
115             }
116 
117             return executeChangeLogCommand(repository, fileSet, startDate, endDate, branch, datePattern);
118         }
119     }
120 
121     protected ChangeLogScmResult executeChangeLogCommand(ChangeLogScmRequest request) throws ScmException {
122         throw new ScmException("Unsupported method for this provider.");
123     }
124 }