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  public abstract class AbstractUpdateCommand extends AbstractCommand {
44      protected abstract UpdateScmResult executeUpdateCommand(
45              ScmProviderRepository repository, ScmFileSet fileSet, ScmVersion scmVersion) throws ScmException;
46  
47      /**
48       * {@inheritDoc}
49       */
50      public ScmResult executeCommand(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
51              throws ScmException {
52          ScmVersion scmVersion = parameters.getScmVersion(CommandParameter.SCM_VERSION, null);
53  
54          boolean runChangelog = Boolean.valueOf(parameters.getString(CommandParameter.RUN_CHANGELOG_WITH_UPDATE, "true"))
55                  .booleanValue();
56  
57          UpdateScmResult updateScmResult = executeUpdateCommand(repository, fileSet, scmVersion);
58  
59          List<ScmFile> filesList = updateScmResult.getUpdatedFiles();
60  
61          if (!runChangelog) {
62              return updateScmResult;
63          }
64  
65          ChangeLogCommand changeLogCmd = getChangeLogCommand();
66  
67          if (filesList != null && filesList.size() > 0 && changeLogCmd != null) {
68              ChangeLogScmResult changeLogScmResult =
69                      (ChangeLogScmResult) changeLogCmd.executeCommand(repository, fileSet, parameters);
70  
71              List<ChangeSet> changes = new ArrayList<>();
72  
73              ChangeLogSet changeLogSet = changeLogScmResult.getChangeLog();
74  
75              if (changeLogSet != null) {
76                  Date startDate = null;
77  
78                  try {
79                      startDate = parameters.getDate(CommandParameter.START_DATE);
80                  } catch (ScmException e) {
81                      // Do nothing, startDate isn't define.
82                  }
83  
84                  for (ChangeSet change : changeLogSet.getChangeSets()) {
85                      if (startDate != null && change.getDate() != null) {
86                          if (startDate.after(change.getDate())) {
87                              continue;
88                          }
89                      }
90  
91                      for (ScmFile currentFile : filesList) {
92                          if (change.containsFilename(currentFile.getPath())) {
93                              changes.add(change);
94  
95                              break;
96                          }
97                      }
98                  }
99              }
100 
101             updateScmResult.setChanges(changes);
102         }
103 
104         return updateScmResult;
105     }
106 
107     protected abstract ChangeLogCommand getChangeLogCommand();
108 }