/*
 * Copyright 2015 Telefonica Investigación y Desarrollo, S.A.U
 *
 * This file is part of perseo-fe
 *
 * perseo-fe is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 *
 * perseo-fe 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public
 * License along with perseo-fe.
 * If not, see http://www.gnu.org/licenses/.
 *
 * For those usages not covered by the GNU Affero General Public License
 * please contact with::[contacto@tid.es]
 */
'use strict';

var async = require('async'),
    appContext = require('../appContext'),
    logger = require('logops'),
    execCollectionName = require('../../config').collections.executions,
    myutils = require('../myutils');

module.exports = {
    LastTime: function LastTime(task, callback) {
        var db = appContext.Db(),
            service = task.event.service,
            subservice = task.event.subservice,
            ruleName = task.event.ruleName,
            id = task.event.id,
            index = task.action.index;

        db.collection(execCollectionName, { strict: true }, function(err, col) {
            if (err) {
                myutils.logErrorIf(err);
                return callback(err, null);
            }
            col.find(
                {
                    name: ruleName, subservice: subservice, service: service, id: id, index: index
                },
                {
                    sort: { lastTime: -1 },
                    limit: 1
                },
                function(err, data) {
                    myutils.logErrorIf(err);
                    if (err) {
                        return callback(err, null);
                    }
                    return callback(null, (data && data.lastTime && data.lastTime.getTime()) || 0);
                }
            );
        });
    },
    AlreadyDone: function AlreadyDone(task, callback) {
        var db = appContext.Db(),
            service = task.event.service,
            subservice = task.event.subservice,
            ruleName = task.event.ruleName,
            id = task.event.id,
            index = task.action.index,
            noticeId = task.event.noticeId;
        db.collection(execCollectionName, { strict: true }, function(err, col) {
            if (err) {
                myutils.logErrorIf(err);
                return callback(err, null);
            }
            col.findOne(
                {
                    name: ruleName,
                    subservice: subservice,
                    service: service,
                    id: id,
                    notice: noticeId,
                    index: index
                },
                function(err, data) {
                    myutils.logErrorIf(err);
                    if (err) {
                        return callback(err, null);
                    }
                    return callback(null, data);
                }
            );
        });
    },
    Update: function Update(task, callback) {
        var db = appContext.Db(),
            service = task.event.service,
            subservice = task.event.subservice,
            ruleName = task.event.ruleName,
            id = task.event.id,
            index = task.action.index,
            noticeId = task.event.noticeId;
        async.waterfall(
            [
                db.collection.bind(db, execCollectionName, { strict: true }),
                function(col, cb) {
                    col.update(
                        {
                            name: ruleName,
                            subservice: subservice,
                            service: service,
                            id: id,
                            notice: noticeId,
                            index: index
                        },
                        { $currentDate: { lastTime: true } },
                        { upsert: true },
                        cb
                    );
                }
            ],
            function(err, result) {
                myutils.logErrorIf(err);
                logger.info('executionsStore.Update %j', result);
                return callback(err, result);
            }
        );
    }
};

@@ -1696,16 +1696,36 @@
 uleName,
+%0A                   
  subserv
@@ -1744,34 +1744,94 @@
 ice,
- service: service, id: id,
+%0A                    service: service,%0A                    id: id,%0A                   
  ind
@@ -1851,32 +1851,52 @@
              %7D,%0A
+                %7B%7D,%0A
                 
@@ -2011,39 +2011,113 @@
   function(err, 
-data) %7B
+result) %7B%0A                    var data = result.hasNext() ? result.next() : null;
 %0A               

use find cursor