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