001package org.apache.maven.scm.provider.bazaar.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 java.util.ArrayList;
023import java.util.Arrays;
024import java.util.Date;
025import java.util.List;
026
027import org.apache.maven.scm.ChangeSet;
028import org.apache.maven.scm.ScmBranch;
029import org.apache.maven.scm.ScmException;
030import org.apache.maven.scm.ScmFileSet;
031import org.apache.maven.scm.ScmResult;
032import org.apache.maven.scm.ScmVersion;
033import org.apache.maven.scm.command.Command;
034import org.apache.maven.scm.command.changelog.AbstractChangeLogCommand;
035import org.apache.maven.scm.command.changelog.ChangeLogScmRequest;
036import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
037import org.apache.maven.scm.command.changelog.ChangeLogSet;
038import org.apache.maven.scm.provider.ScmProviderRepository;
039import org.apache.maven.scm.provider.bazaar.BazaarUtils;
040import org.apache.maven.scm.provider.bazaar.command.BazaarConstants;
041
042/**
043 * @author <a href="mailto:torbjorn@smorgrav.org">Torbjorn Eikli Smorgrav</a>
044 * @author Olivier Lamy
045 *
046 */
047public class BazaarChangeLogCommand
048    extends AbstractChangeLogCommand
049    implements Command
050{
051    /**
052     * {@inheritDoc}
053     */
054    @Override
055    protected ChangeLogScmResult executeChangeLogCommand( ChangeLogScmRequest request )
056        throws ScmException
057    {
058        final ScmVersion startVersion = request.getStartRevision();
059        final ScmVersion endVersion = request.getEndRevision();
060        final ScmFileSet fileSet = request.getScmFileSet();
061        final String datePattern = request.getDatePattern();
062        if ( startVersion != null || endVersion != null )
063        {
064            final ScmProviderRepository scmProviderRepository = request.getScmRepository().getProviderRepository();
065            return executeChangeLogCommand( scmProviderRepository, fileSet, startVersion, endVersion, datePattern );
066        }
067        return executeChangeLogCommand( fileSet, request.getStartDate(), request.getEndDate(),
068            datePattern, request.getLimit() );
069    }
070
071    /** {@inheritDoc} */
072    protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
073                                                          Date startDate, Date endDate, ScmBranch branch,
074                                                          String datePattern )
075        throws ScmException
076    {
077        return executeChangeLogCommand( fileSet, startDate, endDate, datePattern, null );
078    }
079
080    private ChangeLogScmResult executeChangeLogCommand( ScmFileSet fileSet,
081                                                        Date startDate, Date endDate,
082                                                        String datePattern, Integer limit )
083        throws ScmException
084    {
085        List<String> cmd = new ArrayList<String>();
086        cmd.addAll( Arrays.asList( BazaarConstants.LOG_CMD, BazaarConstants.VERBOSE_OPTION ) );
087        if ( limit != null && limit > 0 )
088        {
089            cmd.add( BazaarConstants.LIMIT_OPTION );
090            cmd.add( Integer.toString( limit ) );
091        }
092
093        BazaarChangeLogConsumer consumer = new BazaarChangeLogConsumer( getLogger(), datePattern );
094        ScmResult result = BazaarUtils.execute( consumer, getLogger(), fileSet.getBasedir(),
095            cmd.toArray( new String[cmd.size()] ) );
096
097        List<ChangeSet> logEntries = consumer.getModifications();
098        List<ChangeSet> inRangeAndValid = new ArrayList<ChangeSet>();
099        startDate = startDate == null ? new Date( 0 ) : startDate; //From 1. Jan 1970
100        endDate = endDate == null ? new Date() : endDate; //Upto now
101
102        for ( ChangeSet change : logEntries )
103        {
104            if ( change.getFiles().size() > 0 )
105            {
106                if ( !change.getDate().before( startDate ) && !change.getDate().after( endDate ) )
107                {
108                    inRangeAndValid.add( change );
109                }
110            }
111        }
112
113        ChangeLogSet changeLogSet = new ChangeLogSet( inRangeAndValid, startDate, endDate );
114        return new ChangeLogScmResult( changeLogSet, result );
115    }
116}