/*
 *  eXist Open Source Native XML Database
 *  Copyright (C) 2010 The eXist Project
 *  http://exist-db.org
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * $Id$
 */
package org.exist.versioning.svn.xquery;

import org.exist.dom.QName;
import org.exist.versioning.svn.Resource;
import org.exist.versioning.svn.WorkingCopy;
import org.exist.xquery.BasicFunction;
import org.exist.xquery.Cardinality;
import org.exist.xquery.FunctionSignature;
import org.exist.xquery.XPathException;
import org.exist.xquery.XQueryContext;
import org.exist.xquery.value.FunctionParameterSequenceType;
import org.exist.xquery.value.FunctionReturnSequenceType;
import org.exist.xquery.value.IntegerValue;
import org.exist.xquery.value.Sequence;
import org.exist.xquery.value.SequenceType;
import org.exist.xquery.value.Type;
import org.tmatesoft.svn.core.SVNCommitInfo;
import org.tmatesoft.svn.core.SVNException;

/**
 * Commits files or directories into repository.
 * 
 * @author <a href="mailto:amir.akhmedov@gmail.com">Amir Akhmedov</a>
 * @author <a href="mailto:shabanovd@gmail.com">Dmitriy Shabanov</a>
 */
public class SVNCommit extends BasicFunction {

    public final static FunctionSignature signature =
		new FunctionSignature(
			new QName("commit", SVNModule.NAMESPACE_URI, SVNModule.PREFIX),
			"Commits files or directories into repository.",
			new SequenceType[] {
                new FunctionParameterSequenceType("login", Type.STRING, Cardinality.ZERO_OR_ONE, "The SVN user login."),
                new FunctionParameterSequenceType("password", Type.STRING, Cardinality.ZERO_OR_ONE, "The SVN user password."),
                new FunctionParameterSequenceType("resource", Type.ANY_URI, Cardinality.EXACTLY_ONE, "The path to the resource to be stored."),
                new FunctionParameterSequenceType("message", Type.STRING, Cardinality.ZERO_OR_ONE, "The SVN commit message.")
            },
			new FunctionReturnSequenceType(Type.LONG, Cardinality.EXACTLY_ONE, "the revision number the repository was committed to"));

    /**
     *
     * @param context
     */
    public SVNCommit(XQueryContext context) {
        super(context, signature);
    }
    
    /**
     * Process the function. All arguments are passed in the array args. The number of
     * arguments, their type and cardinality have already been checked to match
     * the function signature.
     *
     * @param args
     * @param contextSequence
     */
    public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {

        String user = args[0].getStringValue();
        String password = args[1].getStringValue();
        String wcDir = args[2].getStringValue();
        String comment = args[3].getStringValue();
        
        SVNCommitInfo info = null;
        
        try {
        	WorkingCopy wc = new WorkingCopy(user, password);

        	info = wc.commit(new Resource(wcDir), false, comment);
		} catch (SVNException svne) {
			svne.printStackTrace();
			throw new XPathException(this,
					"error while commiting a working copy to the repository '"
                    + wcDir + "'", svne);
		}

		if (info == null)
			return new IntegerValue(-1);
		
		return new IntegerValue(info.getNewRevision());
    }
}

@@ -2120,21 +2120,24 @@
 pe(%22
-login
+resource
 %22, Type.
 STRI
@@ -2124,38 +2124,39 @@
 resource%22, Type.
-STRING
+ANY_URI
 , Cardinality.ZE
@@ -2145,39 +2145,39 @@
 RI, Cardinality.
-ZERO_OR
+EXACTLY
 _ONE, %22The SVN u
@@ -2175,22 +2175,41 @@
 The 
-SVN user login
+path to the resource to be stored
 .%22),
@@ -2260,24 +2260,23 @@
 ceType(%22
-password
+message
 %22, Type.
@@ -2321,21 +2321,22 @@
 SVN 
-user password
+commit message
 .%22),
@@ -2387,32 +2387,29 @@
 ceType(%22
-resource
+login
 %22, Type.
 ANY_URI,
@@ -2392,39 +2392,38 @@
 e(%22login%22, Type.
-ANY_URI
+STRING
 , Cardinality.EX
@@ -2412,39 +2412,39 @@
 NG, Cardinality.
-EXACTLY
+ZERO_OR
 _ONE, %22The path 
@@ -2442,41 +2442,22 @@
 The 
-path to the resource to be stored
+SVN user login
 .%22),
@@ -2500,39 +2500,40 @@
 erSequenceType(%22
-message
+password
 %22, Type.STRING, 
@@ -2566,30 +2566,29 @@
 The SVN 
-commit message
+user password
 .%22)%0A    
@@ -3241,19 +3241,20 @@
  String 
-use
+wcDi
 r = args
@@ -3294,24 +3294,23 @@
 ing 
-password
+comment
  = args%5B
 1%5D.g
@@ -3305,17 +3305,17 @@
  = args%5B
-1
+2
 %5D.getStr
@@ -3333,36 +3333,35 @@
 %0A        String 
-wcDi
+use
 r = args%5B2%5D.getS
@@ -3345,33 +3345,33 @@
 ing user = args%5B
-2
+3
 %5D.getStringValue
@@ -3389,31 +3389,32 @@
  String 
-comment
+password
  = args%5B
 3%5D.getSt
@@ -3397,33 +3397,33 @@
 password = args%5B
-3
+4
 %5D.getStringValue

change order of parameters for 'commit' (xquery function)