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