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