001    package org.apache.maven.scm.provider.accurev.command.update;
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    
022    import java.io.File;
023    import java.util.Date;
024    import java.util.List;
025    
026    import org.apache.maven.scm.CommandParameter;
027    import org.apache.maven.scm.CommandParameters;
028    import org.apache.maven.scm.ScmException;
029    import org.apache.maven.scm.ScmFileSet;
030    import org.apache.maven.scm.ScmFileStatus;
031    import org.apache.maven.scm.ScmResult;
032    import org.apache.maven.scm.ScmVersion;
033    import org.apache.maven.scm.command.update.UpdateScmResult;
034    import org.apache.maven.scm.log.ScmLogger;
035    import org.apache.maven.scm.provider.ScmProviderRepository;
036    import org.apache.maven.scm.provider.accurev.AccuRev;
037    import org.apache.maven.scm.provider.accurev.AccuRevException;
038    import org.apache.maven.scm.provider.accurev.AccuRevInfo;
039    import org.apache.maven.scm.provider.accurev.AccuRevScmProviderRepository;
040    import org.apache.maven.scm.provider.accurev.AccuRevVersion;
041    import org.apache.maven.scm.provider.accurev.command.AbstractAccuRevCommand;
042    
043    public class AccuRevUpdateCommand
044        extends AbstractAccuRevCommand
045    {
046    
047        public AccuRevUpdateCommand( ScmLogger logger )
048        {
049            super( logger );
050        }
051    
052        @Override
053        protected ScmResult executeAccurevCommand( AccuRevScmProviderRepository repository, ScmFileSet fileSet,
054                                                   CommandParameters parameters )
055            throws ScmException, AccuRevException
056        {
057    
058            AccuRev accuRev = repository.getAccuRev();
059    
060            File basedir = fileSet.getBasedir();
061    
062            AccuRevInfo info = accuRev.info( basedir );
063    
064            if ( !info.isWorkSpace() )
065            {
066                throw new AccuRevException( "No workspace at " + basedir.getAbsolutePath() );
067            }
068    
069            String startRevision = getStartRevision( repository, parameters, info );
070    
071            ScmVersion scmVersion = parameters.getScmVersion( CommandParameter.SCM_VERSION, null );
072    
073            String updateTransactionId = null;
074    
075            if ( scmVersion != null )
076            {
077                AccuRevVersion updateVersion = repository.getAccuRevVersion( scmVersion );
078    
079                // Reparent if necessary
080                String newBasisStream = updateVersion.getBasisStream();
081                if ( newBasisStream != null
082                    && ( !( newBasisStream.equals( info.getWorkSpace() ) || newBasisStream.equals( info.getBasis() ) ) ) )
083                {
084                    getLogger().info( "Reparenting " + info.getWorkSpace() + " to " + newBasisStream );
085                    accuRev.chws( basedir, info.getWorkSpace(), newBasisStream );
086                }
087    
088                if ( !updateVersion.isNow() )
089                {
090                    updateTransactionId = updateVersion.getTimeSpec();
091                }
092            }
093    
094            if ( updateTransactionId == null )
095            {
096                updateTransactionId = repository.getDepotTransactionId( info.getWorkSpace(), "now" );
097            }
098    
099            String endRevision = repository.getRevision( info.getWorkSpace(), updateTransactionId );
100    
101            List<File> updatedFiles = accuRev.update( basedir, updateTransactionId );
102    
103            if ( updatedFiles != null )
104            {
105                return new AccuRevUpdateScmResult( accuRev.getCommandLines(), getScmFiles( updatedFiles,
106                                                                                           ScmFileStatus.UPDATED ),
107                                                   startRevision, endRevision );
108            }
109            else
110            {
111                return new AccuRevUpdateScmResult( accuRev.getCommandLines(), "AccuRev error", accuRev.getErrorOutput(),
112                                                   null, null, false );
113            }
114        }
115    
116        /*
117         * If we are not capturing info for a changelog then we don't need a start revision. Start date is used if supplied
118         * otherwise get the current high water mark for the workspace as the start version.
119         */
120        private String getStartRevision( AccuRevScmProviderRepository repository, CommandParameters parameters,
121                                         AccuRevInfo info )
122            throws ScmException, AccuRevException
123        {
124    
125            boolean runChangeLog = parameters.getBoolean( CommandParameter.RUN_CHANGELOG_WITH_UPDATE );
126            Date startDate = parameters.getDate( CommandParameter.START_DATE, null );
127            String workspace = info.getWorkSpace();
128    
129            if ( !runChangeLog )
130            {
131                return null;
132            }
133            else
134            {
135                return startDate == null ? repository.getWorkSpaceRevision( workspace )
136                                : repository.getRevision( workspace, startDate );
137            }
138    
139        }
140    
141        public UpdateScmResult update( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters )
142            throws ScmException
143        {
144            return (UpdateScmResult) execute( repository, fileSet, parameters );
145        }
146    
147    }