
classdef MongoDB < handle
    properties
        hostname;
        username;
        password;
        database='poc';
        port=27017;
        cardCollection, wellDataCollection, gaugeOffCollection, ...
            wellTestCollection, fluidShotsCollection, ...
            runStatusCollection, wellConfigCollection, setpointCollection;
    end
    
    methods
        function obj = Database(hostname, username, password)
            javaaddpath 'mongo-java-driver-3.4.2.jar';
            import com.mongodb.*;
            import com.mongodb.client.model.*;
            import java.util.Arrays;
            
            obj.hostname = hostname;
            obj.username = username;
            obj.password = password;
            credential = MongoCredential.createCredential(obj.username, obj.database, obj.password);
            mongo = MongoClient(Arrays.asList(ServerAddress(obj.hostname, obj.port)), Arrays.asList(credential));
            
            db = mongo.getDatabase("poc");
            obj.cardCollection = db.getCollection('cards');
%             obj.cardCollection.createIndex(Indexes.ascending("timestamp", "strokeNumber"));

            obj.wellDataCollection = db.getCollection("measurements");
%             obj.wellDataCollection.createIndex(Indexes.ascending("dateStored", "tagName"));

            obj.gaugeOffCollection = db.getCollection("gaugeOff");
%             obj.gaugeOffCollection.createIndex(Indexes.ascending("timestamp", "tagName"));

            obj.wellTestCollection = db.getCollection("wellTests");
%             obj.wellTestCollection.createIndex(Indexes.ascending("testStartTime"));

            obj.fluidShotsCollection = db.getCollection("fluidShots");
%             obj.fluidShotsCollection.createIndex(Indexes.ascending("timestamp"));

            obj.runStatusCollection = db.getCollection("runStatus");
%             obj.runStatusCollection.createIndex(Indexes.ascending("timestamp"));

            obj.wellConfigCollection = db.getCollection("wellConfiguration");
%             obj.wellConfigCollection.createIndex(Indexes.ascending("timestamp"));

            obj.setpointCollection = db.getCollection("setpoints");
%             obj.setpointCollection.createIndex(Indexes.ascending("name"));
        end
        
        function lastStroke = getLastStrokeNum(obj)
            javaaddpath 'mongo-java-driver-3.4.2.jar';
            import com.mongodb.client.model.*;
            import java.util.Arrays;
            import java.util.List;
            
            lastStroke = -1;
            last = Accumulators.last("lastStroke", "$strokeNumber");
            lastList = java.util.ArrayList;
            lastList.add(last);
            group = Aggregates.group("strokeNumber", lastList);
            groupList = java.util.ArrayList;
            groupList.add(group);
            groupListArray = Arrays.asList(groupList);
            
            cursor = obj.cardCollection.aggregate(groupListArray).iterator();
            while (cursor.hasNext())
                docStroke = cursor.next().getLong("lastStroke");
                if (docStroke  > lastStroke)
                    lastStroke = docStroke;
                end
            end
            cursor.close();
        end
    end
    
    methods(Static)
        function test
            db = Database('localhost', 'poc_java', 'HenryPump@1903');
            lastStroke = db.getLastStrokeNum()
        end
    end
    
    
end