001package org.apache.maven.scm.provider.synergy.command.checkout;
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.ScmVersion;
033import org.apache.maven.scm.command.checkout.AbstractCheckOutCommand;
034import org.apache.maven.scm.command.checkout.CheckOutScmResult;
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 SynergyCheckOutCommand
046    extends AbstractCheckOutCommand
047    implements SynergyCommand
048{
049
050    /** {@inheritDoc} */
051    protected CheckOutScmResult executeCheckOutCommand( ScmProviderRepository repository, ScmFileSet fileSet,
052                                                       ScmVersion version, boolean recursive, boolean shallow )
053        throws ScmException
054    {
055        if ( fileSet.getFileList().size() != 0 )
056        {
057            throw new ScmException( "This provider doesn't support checking out subsets of a project" );
058        }
059
060        if ( getLogger().isDebugEnabled() )
061        {
062            getLogger().debug( "executing checkout command..." );
063        }
064
065        SynergyScmProviderRepository repo = (SynergyScmProviderRepository) repository;
066
067        if ( getLogger().isDebugEnabled() )
068        {
069            getLogger().debug( fileSet.toString() );
070        }
071
072        String ccmAddr = SynergyUtil.start( getLogger(), repo.getUser(), repo.getPassword(), null );
073
074        File waPath;
075        try
076        {
077            String projectSpec =
078                SynergyUtil.getWorkingProject( getLogger(), repo.getProjectSpec(), repo.getUser(), ccmAddr );
079            if ( projectSpec != null )
080            {
081                if ( getLogger().isInfoEnabled() )
082                {
083                    getLogger().info( "A working project already exists [" + projectSpec + "]." );
084                }
085                SynergyUtil.synchronize( getLogger(), projectSpec, ccmAddr );
086            }
087            else
088            {
089                SynergyUtil.checkoutProject( getLogger(), null, repo.getProjectSpec(), version,
090                                             repo.getProjectPurpose(), repo.getProjectRelease(), ccmAddr );
091                projectSpec =
092                    SynergyUtil.getWorkingProject( getLogger(), repo.getProjectSpec(), repo.getUser(), ccmAddr );
093                if ( getLogger().isInfoEnabled() )
094                {
095                    getLogger().info( "A new working project [" + projectSpec + "] was created." );
096                }
097            }
098            SynergyUtil.reconfigure( getLogger(), projectSpec, ccmAddr );
099            waPath = SynergyUtil.getWorkArea( getLogger(), projectSpec, ccmAddr );
100
101        }
102        finally
103        {
104            SynergyUtil.stop( getLogger(), ccmAddr );
105        }
106
107        File source = new File( waPath, repo.getProjectName() );
108
109        if ( getLogger().isInfoEnabled() )
110        {
111            getLogger().info(
112                              "We will now copy files from Synergy Work Area [" + source
113                                  + "] to expected folder [" + fileSet.getBasedir() + "]" );
114        }
115
116        // Move files to the expected folder
117        try
118        {
119            FileUtils.copyDirectoryStructure( source, fileSet.getBasedir() );
120        }
121        catch ( IOException e1 )
122        {
123            throw new ScmException( "Unable to copy directory structure", e1 );
124        }
125
126        if ( getLogger().isDebugEnabled() )
127        {
128            getLogger().debug( "We will list content of checkout directory." );
129        }
130
131        // We need to list files in the directory
132        List<ScmFile> files = new ArrayList<ScmFile>();
133        try
134        {
135            @SuppressWarnings( "unchecked" )
136            List<File> realFiles = FileUtils.getFiles( fileSet.getBasedir(), null, "_ccmwaid.inf" );
137            for ( File f : realFiles )
138            {
139                files.add( new ScmFile( f.getPath(), ScmFileStatus.CHECKED_OUT ) );
140            }
141        }
142        catch ( IOException e )
143        {
144            throw new ScmException( "Unable to list files in checkout directory", e );
145        }
146
147        if ( getLogger().isDebugEnabled() )
148        {
149            getLogger().debug( "checkout command end successfully ..." );
150        }
151
152        return new CheckOutScmResult( files, new ScmResult( "multiple commandline", "OK", "OK", true ) );
153    }
154
155}