001package org.apache.maven.scm.provider.integrity.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 com.mks.api.response.APIException;
023import org.apache.maven.scm.ScmBranch;
024import org.apache.maven.scm.ScmException;
025import org.apache.maven.scm.ScmFileSet;
026import org.apache.maven.scm.ScmResult;
027import org.apache.maven.scm.command.changelog.AbstractChangeLogCommand;
028import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
029import org.apache.maven.scm.provider.ScmProviderRepository;
030import org.apache.maven.scm.provider.integrity.ExceptionHandler;
031import org.apache.maven.scm.provider.integrity.Sandbox;
032import org.apache.maven.scm.provider.integrity.repository.IntegrityScmProviderRepository;
033
034import java.util.Date;
035
036/**
037 * MKS Integrity implementation for Maven's AbstractChangeLogCommand
038 * <br>Currently there is a limitation in the 'si rlog' command where changes
039 * can't be limited to a normal/variant/build configuration.  In other
040 * words all revisions modified within the date range will be picked up
041 * for the Change Log report.  By default the Change Log is grouped by
042 * Change Package ID.  However, if no Change Package is found or Change
043 * Packages are not in use, then all the changes are grouped in one big
044 * Change Log Set
045 *
046 * @author <a href="mailto:cletus@mks.com">Cletus D'Souza</a>
047 * @since 1.6
048 */
049public class IntegrityChangeLogCommand
050    extends AbstractChangeLogCommand
051{
052    /**
053     * {@inheritDoc}
054     */
055    @Override
056    public ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repository, ScmFileSet fileSet,
057                                                       Date startDate, Date endDate, ScmBranch branch,
058                                                       String datePattern )
059        throws ScmException
060    {
061        // First lets validate the date range provided
062        if ( null == startDate || null == endDate )
063        {
064            throw new ScmException( "Both 'startDate' and 'endDate' must be specified!" );
065        }
066        if ( startDate.after( endDate ) )
067        {
068            throw new ScmException( "'stateDate' is not allowed to occur after 'endDate'!" );
069        }
070        getLogger().info(
071            "Attempting to obtain change log for date range: '" + Sandbox.RLOG_DATEFORMAT.format( startDate ) + "' to '"
072                + Sandbox.RLOG_DATEFORMAT.format( endDate ) + "'" );
073        ChangeLogScmResult result;
074        IntegrityScmProviderRepository iRepo = (IntegrityScmProviderRepository) repository;
075        try
076        {
077            result = new ChangeLogScmResult( iRepo.getSandbox().getChangeLog( startDate, endDate ),
078                                             new ScmResult( "si rlog", "", "", true ) );
079        }
080        catch ( APIException aex )
081        {
082            ExceptionHandler eh = new ExceptionHandler( aex );
083            getLogger().error( "MKS API Exception: " + eh.getMessage() );
084            getLogger().info( eh.getCommand() + " exited with return code " + eh.getExitCode() );
085            result =
086                new ChangeLogScmResult( eh.getCommand(), eh.getMessage(), "Exit Code: " + eh.getExitCode(), false );
087        }
088
089        return result;
090    }
091
092}