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