001    package org.apache.maven.scm.provider.synergy.command.unedit;
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.io.IOException;
024    import java.util.ArrayList;
025    import java.util.List;
026    
027    import org.apache.maven.scm.ScmException;
028    import org.apache.maven.scm.ScmFile;
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.command.unedit.AbstractUnEditCommand;
033    import org.apache.maven.scm.command.unedit.UnEditScmResult;
034    import org.apache.maven.scm.provider.ScmProviderRepository;
035    import org.apache.maven.scm.provider.synergy.command.SynergyCommand;
036    import org.apache.maven.scm.provider.synergy.repository.SynergyScmProviderRepository;
037    import org.apache.maven.scm.provider.synergy.util.SynergyUtil;
038    import org.codehaus.plexus.util.FileUtils;
039    
040    /**
041     * @author <a href="mailto:julien.henry@capgemini.com">Julien Henry</a>
042     * @version $Id: SynergyUnEditCommand.java 1333668 2012-05-03 22:37:08Z hboutemy $
043     */
044    public class SynergyUnEditCommand
045        extends AbstractUnEditCommand
046        implements SynergyCommand
047    {
048        /** {@inheritDoc} */
049        protected ScmResult executeUnEditCommand( ScmProviderRepository repository, ScmFileSet fileSet )
050            throws ScmException
051        {
052            if ( getLogger().isDebugEnabled() )
053            {
054                getLogger().debug( "executing unedit command..." );
055            }
056    
057            SynergyScmProviderRepository repo = (SynergyScmProviderRepository) repository;
058    
059            if ( getLogger().isDebugEnabled() )
060            {
061                getLogger().debug( "basedir: " + fileSet.getBasedir() );
062            }
063    
064            String ccmAddr = SynergyUtil.start( getLogger(), repo.getUser(), repo.getPassword(), null );
065    
066            try
067            {
068                String projectSpec =
069                    SynergyUtil.getWorkingProject( getLogger(), repo.getProjectSpec(), repo.getUser(), ccmAddr );
070                if ( projectSpec == null )
071                {
072                    throw new ScmException( "You should checkout a working project first" );
073                }
074                File waPath = SynergyUtil.getWorkArea( getLogger(), projectSpec, ccmAddr );
075                File destPath = new File( waPath, repo.getProjectName() );
076                for ( File f : fileSet.getFileList() )
077                {
078                    File source = new File( fileSet.getBasedir(), f.getPath() );
079                    File dest = new File( destPath, f.getPath() );
080                    SynergyUtil.delete( getLogger(), dest, ccmAddr, true );
081                    if ( !source.equals( dest ) )
082                    {
083                        if ( getLogger().isDebugEnabled() )
084                        {
085                            getLogger().debug( "Copy file [" + dest + "] to [" + source + "]." );
086                        }
087                        try
088                        {
089                            FileUtils.copyFile( dest, source );
090                        }
091                        catch ( IOException e )
092                        {
093                            throw new ScmException( "Unable to restore file in output folder", e );
094                        }
095                    }
096                }
097            }
098            finally
099            {
100                SynergyUtil.stop( getLogger(), ccmAddr );
101            }
102            List<ScmFile> files = new ArrayList<ScmFile>();
103            for ( File f : fileSet.getFileList() )
104            {
105                files.add( new ScmFile( f.getPath(), ScmFileStatus.UNKNOWN ) );
106            }
107            return new UnEditScmResult( "", files );
108        }
109    
110    }