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.changelog;
020
021import java.util.Date;
022
023import org.apache.maven.scm.CommandParameter;
024import org.apache.maven.scm.CommandParameters;
025import org.apache.maven.scm.ScmBranch;
026import org.apache.maven.scm.ScmException;
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.provider.ScmProviderRepository;
032
033/**
034 * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
035 * @author Olivier Lamy
036 *
037 */
038public abstract class AbstractChangeLogCommand extends AbstractCommand implements ChangeLogCommand {
039    @Deprecated
040    protected abstract ChangeLogScmResult executeChangeLogCommand(
041            ScmProviderRepository repository,
042            ScmFileSet fileSet,
043            Date startDate,
044            Date endDate,
045            ScmBranch branch,
046            String datePattern)
047            throws ScmException;
048
049    @Deprecated
050    protected ChangeLogScmResult executeChangeLogCommand(
051            ScmProviderRepository repository,
052            ScmFileSet fileSet,
053            ScmVersion startVersion,
054            ScmVersion endVersion,
055            String datePattern)
056            throws ScmException {
057        throw new ScmException("Unsupported method for this provider.");
058    }
059
060    @Deprecated
061    protected ChangeLogScmResult executeChangeLogCommand(
062            ScmProviderRepository repository, ScmFileSet fileSet, ScmVersion version, String datePattern)
063            throws ScmException {
064        throw new ScmException("Unsupported method for this provider.");
065    }
066
067    /**
068     * {@inheritDoc}
069     */
070    public ScmResult executeCommand(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
071            throws ScmException {
072        Date startDate = parameters.getDate(CommandParameter.START_DATE, null);
073
074        Date endDate = parameters.getDate(CommandParameter.END_DATE, null);
075
076        int numDays = parameters.getInt(CommandParameter.NUM_DAYS, 0);
077
078        Integer limit = parameters.getInt(CommandParameter.LIMIT, -1);
079        if (limit < 1) {
080            limit = null;
081        }
082
083        ScmBranch branch = (ScmBranch) parameters.getScmVersion(CommandParameter.BRANCH, null);
084
085        ScmVersion version = parameters.getScmVersion(CommandParameter.SCM_VERSION, null);
086
087        ScmVersion startVersion = parameters.getScmVersion(CommandParameter.START_SCM_VERSION, null);
088
089        ScmVersion endVersion = parameters.getScmVersion(CommandParameter.END_SCM_VERSION, null);
090
091        String datePattern = parameters.getString(CommandParameter.CHANGELOG_DATE_PATTERN, null);
092
093        boolean versionOnly = startVersion == null && endVersion == null && version != null;
094
095        if (versionOnly) {
096            return executeChangeLogCommand(repository, fileSet, version, datePattern);
097        } else if (startVersion != null || endVersion != null) {
098            return executeChangeLogCommand(repository, fileSet, startVersion, endVersion, datePattern);
099        } else {
100            if (numDays != 0 && (startDate != null || endDate != null)) {
101                throw new ScmException("Start or end date cannot be set if num days is set.");
102            }
103
104            if (endDate != null && startDate == null) {
105                throw new ScmException("The end date is set but the start date isn't.");
106            }
107
108            if (numDays > 0) {
109                @SuppressWarnings("checkstyle:magicnumber")
110                int day = 24 * 60 * 60 * 1000;
111                startDate = new Date(System.currentTimeMillis() - (long) numDays * day);
112
113                endDate = new Date(System.currentTimeMillis() + (long) day);
114            } else if (endDate == null) {
115                endDate = new Date();
116            }
117
118            return executeChangeLogCommand(repository, fileSet, startDate, endDate, branch, datePattern);
119        }
120    }
121
122    protected ChangeLogScmResult executeChangeLogCommand(ChangeLogScmRequest request) throws ScmException {
123        throw new ScmException("Unsupported method for this provider.");
124    }
125}