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 */
043public abstract class AbstractUpdateCommand extends AbstractCommand {
044    protected abstract UpdateScmResult executeUpdateCommand(
045            ScmProviderRepository repository, ScmFileSet fileSet, ScmVersion scmVersion) throws ScmException;
046
047    /**
048     * {@inheritDoc}
049     */
050    public ScmResult executeCommand(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
051            throws ScmException {
052        ScmVersion scmVersion = parameters.getScmVersion(CommandParameter.SCM_VERSION, null);
053
054        boolean runChangelog = Boolean.valueOf(parameters.getString(CommandParameter.RUN_CHANGELOG_WITH_UPDATE, "true"))
055                .booleanValue();
056
057        UpdateScmResult updateScmResult = executeUpdateCommand(repository, fileSet, scmVersion);
058
059        List<ScmFile> filesList = updateScmResult.getUpdatedFiles();
060
061        if (!runChangelog) {
062            return updateScmResult;
063        }
064
065        ChangeLogCommand changeLogCmd = getChangeLogCommand();
066
067        if (filesList != null && filesList.size() > 0 && changeLogCmd != null) {
068            ChangeLogScmResult changeLogScmResult =
069                    (ChangeLogScmResult) changeLogCmd.executeCommand(repository, fileSet, parameters);
070
071            List<ChangeSet> changes = new ArrayList<>();
072
073            ChangeLogSet changeLogSet = changeLogScmResult.getChangeLog();
074
075            if (changeLogSet != null) {
076                Date startDate = null;
077
078                try {
079                    startDate = parameters.getDate(CommandParameter.START_DATE);
080                } catch (ScmException e) {
081                    // Do nothing, startDate isn't define.
082                }
083
084                for (ChangeSet change : changeLogSet.getChangeSets()) {
085                    if (startDate != null && change.getDate() != null) {
086                        if (startDate.after(change.getDate())) {
087                            continue;
088                        }
089                    }
090
091                    for (ScmFile currentFile : filesList) {
092                        if (change.containsFilename(currentFile.getPath())) {
093                            changes.add(change);
094
095                            break;
096                        }
097                    }
098                }
099            }
100
101            updateScmResult.setChanges(changes);
102        }
103
104        return updateScmResult;
105    }
106
107    protected abstract ChangeLogCommand getChangeLogCommand();
108}