/*
 *  eXist Open Source Native XML Database
 *  Copyright (C) 2013 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
 */
package org.exist.mongodb.xquery.gridfs;

import com.mongodb.DB;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import org.exist.dom.QName;
import org.exist.messaging.xquery.*;
import org.exist.mongodb.shared.Constants;
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.*;

/**
 * Implementation gridfs:connect() functions
 * 
 * @author Dannes Wessels
 */

public class Connect extends BasicFunction {
    
    public final static FunctionSignature signatures[] = {

        new FunctionSignature(
        new QName("connect", MessagingModule.NAMESPACE_URI, MessagingModule.PREFIX),
        "Connect to GridFS server",
        new SequenceType[]{
            new FunctionParameterSequenceType("url", Type.STRING, Cardinality.ONE, "URI to server")
//                        ,
//            new FunctionParameterSequenceType("jmsMessageProperties", Type.MAP, Cardinality.ZERO_OR_ONE, "Application-defined property values"),
//            new FunctionParameterSequenceType("jmsConfiguration", Type.MAP, Cardinality.ONE, "JMS configuration settings")
            },
            new FunctionReturnSequenceType(Type.NODE, Cardinality.ONE, "Confirmation message")
        ),
        
    };

    public Connect(XQueryContext context, FunctionSignature signature) {
        super(context, signature);
    }

    @Override
    public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {

        // User must either be DBA or in the JMS group
        if (!context.getSubject().hasDbaRole() && !context.getSubject().hasGroup(Constants.MONGODB_GROUP)) {
            String txt = String.format("Permission denied, user '%s' must be a DBA or be in group '%s'",
                    context.getSubject().getName(), Constants.MONGODB_GROUP);
            XPathException ex = new XPathException(this, txt);
            LOG.error(txt, ex);
            throw ex;
        }

        // Get content
        String url = args[0].itemAt(0).getStringValue();
        String database = args[1].itemAt(0).getStringValue();


        try {
            MongoClientURI uri = new MongoClientURI(url);
            MongoClient client = new MongoClient(uri);
            DB db = client.getDB(database);
            //client.


        } catch (Throwable t) {
            LOG.error(t.getMessage());
            XPathException ex = new XPathException(this, t);
            throw ex;
        }

        return null;
    }    
}

@@ -1038,25 +1038,31 @@
 st.m
-essaging.xquery.*
+ongodb.shared.Constants
 ;%0Aim
@@ -1084,32 +1084,35 @@
 mongodb.
-shared.Constants
+xquery.GridfsModule
 ;%0Aimport
@@ -1591,25 +1591,22 @@
 nnect%22, 
-Messaging
+Gridfs
 Module.N
@@ -1623,17 +1623,14 @@
 RI, 
-Messaging
+Gridfs
 Modu

make it build again