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.update;
20  
21  import java.util.ArrayList;
22  import java.util.Date;
23  import java.util.List;
24  
25  import org.apache.maven.scm.ChangeSet;
26  import org.apache.maven.scm.CommandParameter;
27  import org.apache.maven.scm.CommandParameters;
28  import org.apache.maven.scm.ScmException;
29  import org.apache.maven.scm.ScmFile;
30  import org.apache.maven.scm.ScmFileSet;
31  import org.apache.maven.scm.ScmResult;
32  import org.apache.maven.scm.ScmVersion;
33  import org.apache.maven.scm.command.AbstractCommand;
34  import org.apache.maven.scm.command.changelog.ChangeLogCommand;
35  import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
36  import org.apache.maven.scm.command.changelog.ChangeLogSet;
37  import org.apache.maven.scm.provider.ScmProviderRepository;
38  
39  /**
40   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse </a>
41   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
42   *
43   */
44  public abstract class AbstractUpdateCommand extends AbstractCommand {
45      protected abstract UpdateScmResult executeUpdateCommand(
46              ScmProviderRepository repository, ScmFileSet fileSet, ScmVersion scmVersion) throws ScmException;
47  
48      /** {@inheritDoc} */
49      public ScmResult executeCommand(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
50              throws ScmException {
51          ScmVersion scmVersion = parameters.getScmVersion(CommandParameter.SCM_VERSION, null);
52  
53          boolean runChangelog = Boolean.valueOf(parameters.getString(CommandParameter.RUN_CHANGELOG_WITH_UPDATE, "true"))
54                  .booleanValue();
55  
56          UpdateScmResult updateScmResult = executeUpdateCommand(repository, fileSet, scmVersion);
57  
58          List<ScmFile> filesList = updateScmResult.getUpdatedFiles();
59  
60          if (!runChangelog) {
61              return updateScmResult;
62          }
63  
64          ChangeLogCommand changeLogCmd = getChangeLogCommand();
65  
66          if (filesList != null && filesList.size() > 0 && changeLogCmd != null) {
67              ChangeLogScmResult changeLogScmResult =
68                      (ChangeLogScmResult) changeLogCmd.executeCommand(repository, fileSet, parameters);
69  
70              List<ChangeSet> changes = new ArrayList<>();
71  
72              ChangeLogSet changeLogSet = changeLogScmResult.getChangeLog();
73  
74              if (changeLogSet != null) {
75                  Date startDate = null;
76  
77                  try {
78                      startDate = parameters.getDate(CommandParameter.START_DATE);
79                  } catch (ScmException e) {
80                      // Do nothing, startDate isn't define.
81                  }
82  
83                  for (ChangeSet change : changeLogSet.getChangeSets()) {
84                      if (startDate != null && change.getDate() != null) {
85                          if (startDate.after(change.getDate())) {
86                              continue;
87                          }
88                      }
89  
90                      for (ScmFile currentFile : filesList) {
91                          if (change.containsFilename(currentFile.getPath())) {
92                              changes.add(change);
93  
94                              break;
95                          }
96                      }
97                  }
98              }
99  
100             updateScmResult.setChanges(changes);
101         }
102 
103         return updateScmResult;
104     }
105 
106     protected abstract ChangeLogCommand getChangeLogCommand();
107 }