View Javadoc
1   package org.apache.maven.scm.provider.synergy.command.edit;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.util.ArrayList;
25  import java.util.LinkedList;
26  import java.util.List;
27  
28  import org.apache.maven.scm.ScmException;
29  import org.apache.maven.scm.ScmFile;
30  import org.apache.maven.scm.ScmFileSet;
31  import org.apache.maven.scm.ScmFileStatus;
32  import org.apache.maven.scm.ScmResult;
33  import org.apache.maven.scm.command.edit.AbstractEditCommand;
34  import org.apache.maven.scm.command.edit.EditScmResult;
35  import org.apache.maven.scm.provider.ScmProviderRepository;
36  import org.apache.maven.scm.provider.synergy.command.SynergyCommand;
37  import org.apache.maven.scm.provider.synergy.repository.SynergyScmProviderRepository;
38  import org.apache.maven.scm.provider.synergy.util.SynergyUtil;
39  import org.codehaus.plexus.util.FileUtils;
40  
41  /**
42   * @author <a href="mailto:julien.henry@capgemini.com">Julien Henry</a>
43   *
44   */
45  public class SynergyEditCommand
46      extends AbstractEditCommand
47      implements SynergyCommand
48  {
49      /** {@inheritDoc} */
50      protected ScmResult executeEditCommand( ScmProviderRepository repository, ScmFileSet fileSet )
51          throws ScmException
52      {
53          if ( getLogger().isDebugEnabled() )
54          {
55              getLogger().debug( "executing edit command..." );
56          }
57  
58          SynergyScmProviderRepository repo = (SynergyScmProviderRepository) repository;
59  
60          if ( getLogger().isDebugEnabled() )
61          {
62              getLogger().debug( fileSet.toString() );
63          }
64  
65          String ccmAddr = SynergyUtil.start( getLogger(), repo.getUser(), repo.getPassword(), null );
66  
67          try
68          {
69              String projectSpec =
70                  SynergyUtil.getWorkingProject( getLogger(), repo.getProjectSpec(), repo.getUser(), ccmAddr );
71              File waPath = SynergyUtil.getWorkArea( getLogger(), projectSpec, ccmAddr );
72              File sourcePath = new File( waPath, repo.getProjectName() );
73              if ( projectSpec == null )
74              {
75                  throw new ScmException( "You should checkout project first" );
76              }
77              int taskNum = SynergyUtil.createTask( getLogger(), "Maven SCM Synergy provider: edit command for project "
78                  + repo.getProjectSpec(), repo.getProjectRelease(), true, ccmAddr );
79              if ( getLogger().isInfoEnabled() )
80              {
81                  getLogger().info( "Task " + taskNum + " was created to perform checkout." );
82              }
83              for ( File f : fileSet.getFileList() )
84              {
85                  File dest = f;
86                  File source = new File( sourcePath, SynergyUtil.removePrefix( fileSet.getBasedir(), f ) );
87                  List<File> list = new LinkedList<File>();
88                  list.add( source );
89                  SynergyUtil.checkoutFiles( getLogger(), list, ccmAddr );
90                  if ( !source.equals( dest ) )
91                  {
92                      if ( getLogger().isDebugEnabled() )
93                      {
94                          getLogger().debug( "Copy file [" + source + "] to expected folder [" + dest + "]." );
95                      }
96                      try
97                      {
98                          FileUtils.copyFile( source, dest );
99                      }
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 }