001package org.apache.maven.scm.provider.synergy.command.edit;
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.io.File;
023import java.io.IOException;
024import java.util.ArrayList;
025import java.util.LinkedList;
026import java.util.List;
027
028import org.apache.maven.scm.ScmException;
029import org.apache.maven.scm.ScmFile;
030import org.apache.maven.scm.ScmFileSet;
031import org.apache.maven.scm.ScmFileStatus;
032import org.apache.maven.scm.ScmResult;
033import org.apache.maven.scm.command.edit.AbstractEditCommand;
034import org.apache.maven.scm.command.edit.EditScmResult;
035import org.apache.maven.scm.provider.ScmProviderRepository;
036import org.apache.maven.scm.provider.synergy.command.SynergyCommand;
037import org.apache.maven.scm.provider.synergy.repository.SynergyScmProviderRepository;
038import org.apache.maven.scm.provider.synergy.util.SynergyUtil;
039import org.codehaus.plexus.util.FileUtils;
040
041/**
042 * @author <a href="mailto:julien.henry@capgemini.com">Julien Henry</a>
043 *
044 */
045public class SynergyEditCommand
046    extends AbstractEditCommand
047    implements SynergyCommand
048{
049    /** {@inheritDoc} */
050    protected ScmResult executeEditCommand( ScmProviderRepository repository, ScmFileSet fileSet )
051        throws ScmException
052    {
053        if ( getLogger().isDebugEnabled() )
054        {
055            getLogger().debug( "executing edit command..." );
056        }
057
058        SynergyScmProviderRepository repo = (SynergyScmProviderRepository) repository;
059
060        if ( getLogger().isDebugEnabled() )
061        {
062            getLogger().debug( fileSet.toString() );
063        }
064
065        String ccmAddr = SynergyUtil.start( getLogger(), repo.getUser(), repo.getPassword(), null );
066
067        try
068        {
069            String projectSpec =
070                SynergyUtil.getWorkingProject( getLogger(), repo.getProjectSpec(), repo.getUser(), ccmAddr );
071            File waPath = SynergyUtil.getWorkArea( getLogger(), projectSpec, ccmAddr );
072            File sourcePath = new File( waPath, repo.getProjectName() );
073            if ( projectSpec == null )
074            {
075                throw new ScmException( "You should checkout project first" );
076            }
077            int taskNum = SynergyUtil.createTask( getLogger(), "Maven SCM Synergy provider: edit command for project "
078                + repo.getProjectSpec(), repo.getProjectRelease(), true, ccmAddr );
079            if ( getLogger().isInfoEnabled() )
080            {
081                getLogger().info( "Task " + taskNum + " was created to perform checkout." );
082            }
083            for ( File f : fileSet.getFileList() )
084            {
085                File dest = f;
086                File source = new File( sourcePath, SynergyUtil.removePrefix( fileSet.getBasedir(), f ) );
087                List<File> list = new LinkedList<File>();
088                list.add( source );
089                SynergyUtil.checkoutFiles( getLogger(), list, ccmAddr );
090                if ( !source.equals( dest ) )
091                {
092                    if ( getLogger().isDebugEnabled() )
093                    {
094                        getLogger().debug( "Copy file [" + source + "] to expected folder [" + dest + "]." );
095                    }
096                    try
097                    {
098                        FileUtils.copyFile( source, dest );
099                    }
100                    catch ( IOException e )
101                    {
102                        throw new ScmException( "Unable to copy file from Work Area", e );
103                    }
104                }
105            }
106        }
107        finally
108        {
109            SynergyUtil.stop( getLogger(), ccmAddr );
110        }
111        List<ScmFile> scmFiles = new ArrayList<ScmFile>( fileSet.getFileList().size() );
112        for ( File f : fileSet.getFileList() )
113        {
114            scmFiles.add( new ScmFile( f.getPath(), ScmFileStatus.EDITED ) );
115        }
116        return new EditScmResult( "", scmFiles );
117    }
118
119}