View Javadoc

1   package org.apache.maven.scm.command.update;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.scm.ChangeSet;
23  import org.apache.maven.scm.CommandParameter;
24  import org.apache.maven.scm.CommandParameters;
25  import org.apache.maven.scm.ScmException;
26  import org.apache.maven.scm.ScmFile;
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.command.changelog.ChangeLogCommand;
32  import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
33  import org.apache.maven.scm.command.changelog.ChangeLogSet;
34  import org.apache.maven.scm.provider.ScmProviderRepository;
35  
36  import java.util.ArrayList;
37  import java.util.Date;
38  import java.util.List;
39  
40  /**
41   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse </a>
42   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
43   *
44   */
45  public abstract class AbstractUpdateCommand
46      extends AbstractCommand
47  {
48      protected abstract UpdateScmResult executeUpdateCommand( ScmProviderRepository repository, ScmFileSet fileSet,
49                                                               ScmVersion scmVersion )
50          throws ScmException;
51  
52      /** {@inheritDoc} */
53      public ScmResult executeCommand( ScmProviderRepository repository, ScmFileSet fileSet,
54                                       CommandParameters parameters )
55          throws ScmException
56      {
57          ScmVersion scmVersion = parameters.getScmVersion( CommandParameter.SCM_VERSION, null );
58  
59          boolean runChangelog = Boolean.valueOf(
60              parameters.getString( CommandParameter.RUN_CHANGELOG_WITH_UPDATE, "true" ) ).booleanValue();
61  
62          UpdateScmResult updateScmResult = executeUpdateCommand( repository, fileSet, scmVersion );
63  
64          List<ScmFile> filesList = updateScmResult.getUpdatedFiles();
65  
66          if ( !runChangelog )
67          {
68              return updateScmResult;
69          }
70  
71          ChangeLogCommand changeLogCmd = getChangeLogCommand();
72  
73          if ( filesList != null && filesList.size() > 0 && changeLogCmd != null )
74          {
75              ChangeLogScmResult changeLogScmResult =
76                  (ChangeLogScmResult) changeLogCmd.executeCommand( repository, fileSet, parameters );
77  
78              List<ChangeSet> changes = new ArrayList<ChangeSet>();
79  
80              ChangeLogSet changeLogSet = changeLogScmResult.getChangeLog();
81  
82              if ( changeLogSet != null )
83              {
84                  Date startDate = null;
85  
86                  try
87                  {
88                      startDate = parameters.getDate( CommandParameter.START_DATE );
89                  }
90                  catch ( ScmException e )
91                  {
92                      //Do nothing, startDate isn't define.
93                  }
94  
95                  for ( ChangeSet change : changeLogSet.getChangeSets() )
96                  {
97                      if ( startDate != null && change.getDate() != null )
98                      {
99                          if ( startDate.after( change.getDate() ) )
100                         {
101                             continue;
102                         }
103                     }
104 
105                     for ( ScmFile currentFile : filesList )
106                     {
107                         if ( change.containsFilename( currentFile.getPath() ) )
108                         {
109                             changes.add( change );
110 
111                             break;
112                         }
113                     }
114                 }
115             }
116 
117             updateScmResult.setChanges( changes );
118         }
119 
120         return updateScmResult;
121     }
122 
123     protected abstract ChangeLogCommand getChangeLogCommand();
124 }