View Javadoc

1   package org.apache.maven.scm.provider.synergy.command.unedit;
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.List;
26  
27  import org.apache.maven.scm.ScmException;
28  import org.apache.maven.scm.ScmFile;
29  import org.apache.maven.scm.ScmFileSet;
30  import org.apache.maven.scm.ScmFileStatus;
31  import org.apache.maven.scm.ScmResult;
32  import org.apache.maven.scm.command.unedit.AbstractUnEditCommand;
33  import org.apache.maven.scm.command.unedit.UnEditScmResult;
34  import org.apache.maven.scm.provider.ScmProviderRepository;
35  import org.apache.maven.scm.provider.synergy.command.SynergyCommand;
36  import org.apache.maven.scm.provider.synergy.repository.SynergyScmProviderRepository;
37  import org.apache.maven.scm.provider.synergy.util.SynergyUtil;
38  import org.codehaus.plexus.util.FileUtils;
39  
40  /**
41   * @author <a href="mailto:julien.henry@capgemini.com">Julien Henry</a>
42   *
43   */
44  public class SynergyUnEditCommand
45      extends AbstractUnEditCommand
46      implements SynergyCommand
47  {
48      /** {@inheritDoc} */
49      protected ScmResult executeUnEditCommand( ScmProviderRepository repository, ScmFileSet fileSet )
50          throws ScmException
51      {
52          if ( getLogger().isDebugEnabled() )
53          {
54              getLogger().debug( "executing unedit command..." );
55          }
56  
57          SynergyScmProviderRepository repo = (SynergyScmProviderRepository) repository;
58  
59          if ( getLogger().isDebugEnabled() )
60          {
61              getLogger().debug( "basedir: " + fileSet.getBasedir() );
62          }
63  
64          String ccmAddr = SynergyUtil.start( getLogger(), repo.getUser(), repo.getPassword(), null );
65  
66          try
67          {
68              String projectSpec =
69                  SynergyUtil.getWorkingProject( getLogger(), repo.getProjectSpec(), repo.getUser(), ccmAddr );
70              if ( projectSpec == null )
71              {
72                  throw new ScmException( "You should checkout a working project first" );
73              }
74              File waPath = SynergyUtil.getWorkArea( getLogger(), projectSpec, ccmAddr );
75              File destPath = new File( waPath, repo.getProjectName() );
76              for ( File f : fileSet.getFileList() )
77              {
78                  File source = new File( fileSet.getBasedir(), f.getPath() );
79                  File dest = new File( destPath, f.getPath() );
80                  SynergyUtil.delete( getLogger(), dest, ccmAddr, true );
81                  if ( !source.equals( dest ) )
82                  {
83                      if ( getLogger().isDebugEnabled() )
84                      {
85                          getLogger().debug( "Copy file [" + dest + "] to [" + source + "]." );
86                      }
87                      try
88                      {
89                          FileUtils.copyFile( dest, source );
90                      }
91                      catch ( IOException e )
92                      {
93                          throw new ScmException( "Unable to restore file in output folder", e );
94                      }
95                  }
96              }
97          }
98          finally
99          {
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 }