001package org.apache.maven.scm.provider.jazz.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
022import java.io.File;
023import java.util.List;
024
025import org.apache.maven.scm.ScmException;
026import org.apache.maven.scm.ScmFileSet;
027import org.apache.maven.scm.ScmResult;
028import org.apache.maven.scm.command.unedit.AbstractUnEditCommand;
029import org.apache.maven.scm.command.unedit.UnEditScmResult;
030import org.apache.maven.scm.provider.ScmProviderRepository;
031import org.apache.maven.scm.provider.jazz.command.JazzConstants;
032import org.apache.maven.scm.provider.jazz.command.JazzScmCommand;
033import org.apache.maven.scm.provider.jazz.command.consumer.DebugLoggerConsumer;
034import org.apache.maven.scm.provider.jazz.command.consumer.ErrorConsumer;
035
036// In RTC the need to 'edit' or 'lock' a file is not required. It is actually encouraged to not 
037// lock 'text' based files and to only lock binary file types.
038//
039// The Maven SCM plugin "edit" goal has been implemented by using the RTC "lock acquire/release" commands. 
040//
041// See the following links for additional information on the RTC "lock acquire" command:
042// RTC 2.0.0.2:
043// http://publib.boulder.ibm.com/infocenter/rtc/v2r0m0/topic/com.ibm.team.scm.doc/topics/r_scm_cli_lock_acquire.html
044// RTC 3.0:
045// http://publib.boulder.ibm.com/infocenter/clmhelp/v3r0/topic/com.ibm.team.scm.doc/topics/r_scm_cli_lock_acquire.html
046// RTC 3.0.1:
047// http://publib.boulder.ibm.com/infocenter/clmhelp/v3r0m1/topic/com.ibm.team.scm.doc/topics/r_scm_cli_lock_acquire.html
048//
049// See the following links for additional information on the RTC "lock release" command:
050// RTC 2.0.0.2:
051// http://publib.boulder.ibm.com/infocenter/rtc/v2r0m0/topic/com.ibm.team.scm.doc/topics/r_scm_cli_lock_release.html
052// RTC 3.0:
053// http://publib.boulder.ibm.com/infocenter/clmhelp/v3r0/topic/com.ibm.team.scm.doc/topics/r_scm_cli_lock_release.html
054// RTC 3.0.1:
055// http://publib.boulder.ibm.com/infocenter/clmhelp/v3r0m1/topic/com.ibm.team.scm.doc/topics/r_scm_cli_lock_release.html
056//
057
058/**
059 * @author <a href="mailto:ChrisGWarp@gmail.com">Chris Graham</a>
060 */
061public class JazzUnEditCommand
062    extends AbstractUnEditCommand
063{
064    /**
065     * {@inheritDoc}
066     */
067    protected ScmResult executeUnEditCommand( ScmProviderRepository repo, ScmFileSet fileSet )
068        throws ScmException
069    {
070        if ( getLogger().isDebugEnabled() )
071        {
072            getLogger().debug( "Executing unedit command..." );
073        }
074
075        DebugLoggerConsumer uneditConsumer = new DebugLoggerConsumer( getLogger() );
076        ErrorConsumer errConsumer = new ErrorConsumer( getLogger() );
077
078        JazzScmCommand uneditCmd = createUneditCommand( repo, fileSet );
079        int status = uneditCmd.execute( uneditConsumer, errConsumer );
080
081        if ( status != 0 || errConsumer.hasBeenFed() )
082        {
083            return new UnEditScmResult( uneditCmd.getCommandString(),
084                                        "Error code for Jazz SCM unedit command - " + status, errConsumer.getOutput(),
085                                        false );
086        }
087
088        return new UnEditScmResult( uneditCmd.getCommandString(), "Successfully Completed.", uneditConsumer.getOutput(),
089                                    true );
090    }
091
092    public JazzScmCommand createUneditCommand( ScmProviderRepository repo, ScmFileSet fileSet )
093    {
094        JazzScmCommand command =
095            new JazzScmCommand( JazzConstants.CMD_LOCK, JazzConstants.CMD_SUB_RELEASE, repo, fileSet, getLogger() );
096
097        List<File> files = fileSet.getFileList();
098        if ( files != null && !files.isEmpty() )
099        {
100            for ( File file : files )
101            {
102                command.addArgument( file.getPath() ); // Un-Lock only the files specified
103            }
104        }
105        else
106        {
107            command.addArgument( "." ); // Un-Lock all files
108        }
109
110        return command;
111    }
112}