001package org.apache.maven.scm.command.update;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.maven.scm.ChangeSet;
023import org.apache.maven.scm.CommandParameter;
024import org.apache.maven.scm.CommandParameters;
025import org.apache.maven.scm.ScmException;
026import org.apache.maven.scm.ScmFile;
027import org.apache.maven.scm.ScmFileSet;
028import org.apache.maven.scm.ScmResult;
029import org.apache.maven.scm.ScmVersion;
030import org.apache.maven.scm.command.AbstractCommand;
031import org.apache.maven.scm.command.changelog.ChangeLogCommand;
032import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
033import org.apache.maven.scm.command.changelog.ChangeLogSet;
034import org.apache.maven.scm.provider.ScmProviderRepository;
035
036import java.util.ArrayList;
037import java.util.Date;
038import java.util.List;
039
040/**
041 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse </a>
042 * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
043 *
044 */
045public abstract class AbstractUpdateCommand
046    extends AbstractCommand
047{
048    protected abstract UpdateScmResult executeUpdateCommand( ScmProviderRepository repository, ScmFileSet fileSet,
049                                                             ScmVersion scmVersion )
050        throws ScmException;
051
052    /** {@inheritDoc} */
053    public ScmResult executeCommand( ScmProviderRepository repository, ScmFileSet fileSet,
054                                     CommandParameters parameters )
055        throws ScmException
056    {
057        ScmVersion scmVersion = parameters.getScmVersion( CommandParameter.SCM_VERSION, null );
058
059        boolean runChangelog = Boolean.valueOf(
060            parameters.getString( CommandParameter.RUN_CHANGELOG_WITH_UPDATE, "true" ) ).booleanValue();
061
062        UpdateScmResult updateScmResult = executeUpdateCommand( repository, fileSet, scmVersion );
063
064        List<ScmFile> filesList = updateScmResult.getUpdatedFiles();
065
066        if ( !runChangelog )
067        {
068            return updateScmResult;
069        }
070
071        ChangeLogCommand changeLogCmd = getChangeLogCommand();
072
073        if ( filesList != null && filesList.size() > 0 && changeLogCmd != null )
074        {
075            ChangeLogScmResult changeLogScmResult =
076                (ChangeLogScmResult) changeLogCmd.executeCommand( repository, fileSet, parameters );
077
078            List<ChangeSet> changes = new ArrayList<ChangeSet>();
079
080            ChangeLogSet changeLogSet = changeLogScmResult.getChangeLog();
081
082            if ( changeLogSet != null )
083            {
084                Date startDate = null;
085
086                try
087                {
088                    startDate = parameters.getDate( CommandParameter.START_DATE );
089                }
090                catch ( ScmException e )
091                {
092                    //Do nothing, startDate isn't define.
093                }
094
095                for ( ChangeSet change : changeLogSet.getChangeSets() )
096                {
097                    if ( startDate != null && change.getDate() != null )
098                    {
099                        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}