View Javadoc
1   package org.apache.maven.scm.provider.synergy.command.add;
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.add.AbstractAddCommand;
33  import org.apache.maven.scm.command.add.AddScmResult;
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 SynergyAddCommand
45      extends AbstractAddCommand
46      implements SynergyCommand
47  {
48      /** {@inheritDoc} */
49      protected ScmResult executeAddCommand( ScmProviderRepository repository, ScmFileSet fileSet, String message,
50                                             boolean binary )
51          throws ScmException
52      {
53          if ( getLogger().isDebugEnabled() )
54          {
55              getLogger().debug( "executing add command..." );
56          }
57  
58          SynergyScmProviderRepository repo = (SynergyScmProviderRepository) repository;
59  
60          if ( getLogger().isDebugEnabled() )
61          {
62              getLogger().debug( "basedir: " + fileSet.getBasedir() );
63          }
64  
65          if ( message == null || message.equals( "" ) )
66          {
67              message = "Maven SCM Synergy provider: adding file(s) to project " + repo.getProjectSpec();
68          }
69  
70          String ccmAddr = SynergyUtil.start( getLogger(), repo.getUser(), repo.getPassword(), null );
71  
72          try
73          {
74              int taskNum = SynergyUtil.createTask( getLogger(), message, repo.getProjectRelease(), true, ccmAddr );
75              String projectSpec =
76                  SynergyUtil.getWorkingProject( getLogger(), repo.getProjectSpec(), repo.getUser(), ccmAddr );
77              if ( projectSpec == null )
78              {
79                  throw new ScmException( "You should checkout a working project first" );
80              }
81              File waPath = SynergyUtil.getWorkArea( getLogger(), projectSpec, ccmAddr );
82              File destPath = new File( waPath, repo.getProjectName() );
83              for ( File source : fileSet.getFileList() )
84              {
85                  File dest = new File( destPath, SynergyUtil.removePrefix( fileSet.getBasedir(), source ) );
86                  if ( !source.equals( dest ) )
87                  {
88                      if ( getLogger().isDebugEnabled() )
89                      {
90                          getLogger().debug( "Copy file [" + source + "] to Synergy Work Area [" + dest + "]." );
91                      }
92                      try
93                      {
94                          FileUtils.copyFile( source, dest );
95                      }
96                      catch ( IOException e )
97                      {
98                          throw new ScmException( "Unable to copy file in Work Area", e );
99                      }
100                 }
101                 SynergyUtil.create( getLogger(), dest, message, ccmAddr );
102             }
103             SynergyUtil.checkinTask( getLogger(), taskNum, message, ccmAddr );
104 
105         }
106         finally
107         {
108             SynergyUtil.stop( getLogger(), ccmAddr );
109         }
110         List<ScmFile> scmFiles = new ArrayList<ScmFile>( fileSet.getFileList().size() );
111         for ( File f : fileSet.getFileList() )
112         {
113             scmFiles.add( new ScmFile( f.getPath(), ScmFileStatus.ADDED ) );
114         }
115         return new AddScmResult( "", scmFiles );
116     }
117 
118 
119 }