001package org.apache.maven.scm.provider.synergy.command.remove;
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.util.ArrayList;
024import java.util.List;
025
026import org.apache.maven.scm.ScmException;
027import org.apache.maven.scm.ScmFile;
028import org.apache.maven.scm.ScmFileSet;
029import org.apache.maven.scm.ScmFileStatus;
030import org.apache.maven.scm.ScmResult;
031import org.apache.maven.scm.command.remove.AbstractRemoveCommand;
032import org.apache.maven.scm.command.status.StatusScmResult;
033import org.apache.maven.scm.provider.ScmProviderRepository;
034import org.apache.maven.scm.provider.synergy.command.SynergyCommand;
035import org.apache.maven.scm.provider.synergy.repository.SynergyScmProviderRepository;
036import org.apache.maven.scm.provider.synergy.util.SynergyUtil;
037
038/**
039 * @author <a href="mailto:julien.henry@capgemini.com">Julien Henry</a>
040 *
041 */
042public class SynergyRemoveCommand
043    extends AbstractRemoveCommand
044    implements SynergyCommand
045{
046    /** {@inheritDoc} */
047    protected ScmResult executeRemoveCommand( ScmProviderRepository repository, ScmFileSet fileSet, String message )
048        throws ScmException
049    {
050        if ( getLogger().isDebugEnabled() )
051        {
052            getLogger().debug( "executing remove command..." );
053        }
054
055        SynergyScmProviderRepository repo = (SynergyScmProviderRepository) repository;
056
057        if ( getLogger().isDebugEnabled() )
058        {
059            getLogger().debug( "basedir: " + fileSet.getBasedir() );
060        }
061
062        String ccmAddr = SynergyUtil.start( getLogger(), repo.getUser(), repo.getPassword(), null );
063
064        try
065        {
066            String projectSpec =
067                SynergyUtil.getWorkingProject( getLogger(), repo.getProjectSpec(), repo.getUser(), ccmAddr );
068            if ( projectSpec == null )
069            {
070                throw new ScmException( "You should checkout a working project first" );
071            }
072            File waPath = SynergyUtil.getWorkArea( getLogger(), projectSpec, ccmAddr );
073            File destPath = new File( waPath, repo.getProjectName() );
074            for ( File f : fileSet.getFileList() )
075            {
076                File source = new File( fileSet.getBasedir(), f.getPath() );
077                File dest = new File( destPath, f.getPath() );
078                SynergyUtil.delete( getLogger(), dest, ccmAddr, false );
079                if ( !source.equals( dest ) )
080                {
081                    if ( getLogger().isDebugEnabled() )
082                    {
083                        getLogger().debug( "Delete file [" + source + "]." );
084                    }
085                    dest.delete();
086                }
087            }
088        }
089        finally
090        {
091            SynergyUtil.stop( getLogger(), ccmAddr );
092        }
093        List<ScmFile> scmFiles = new ArrayList<ScmFile>();
094        for ( File file : fileSet.getFileList() )
095        {
096            scmFiles.add( new ScmFile( file.getPath(), ScmFileStatus.DELETED ) );
097        }
098        return new StatusScmResult( "", scmFiles );
099    }
100
101}