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