/*
 * The MIT License
 *
 * Copyright (c) 2016 Marcelo "Ataxexe" Guimarães <ataxexe@devnull.tools>
 *
 * Permission  is hereby granted, free of charge, to any person obtaining
 * a  copy  of  this  software  and  associated  documentation files (the
 * "Software"),  to  deal  in the Software without restriction, including
 * without  limitation  the  rights to use, copy, modify, merge, publish,
 * distribute,  sublicense,  and/or  sell  copies of the Software, and to
 * permit  persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * The  above  copyright  notice  and  this  permission  notice  shall be
 * included  in  all  copies  or  substantial  portions  of the Software.
 *
 * THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT  WARRANTY OF ANY KIND,
 * EXPRESS  OR  IMPLIED,  INCLUDING  BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN  NO  EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM,  DAMAGES  OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT  OR  OTHERWISE,  ARISING  FROM,  OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE   OR   THE   USE   OR   OTHER   DEALINGS  IN  THE  SOFTWARE.
 */

package tools.devnull.boteco.persistence.request;

import com.google.gson.Gson;
import com.mongodb.BasicDBObject;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.IndexOptions;
import org.bson.Document;
import tools.devnull.boteco.plugins.request.RequestRepository;
import tools.devnull.boteco.request.Request;

import java.util.Date;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * A repository for requests that persists information in a MongoDB
 */
public class MongoRequestRepository implements RequestRepository {

  private final MongoCollection<Document> requests;
  private final Gson gson;

  public MongoRequestRepository(MongoDatabase database, Long minutesToExpire) {
    this.requests = database.getCollection("requests");
    this.requests.createIndex(new Document("createdAt", 1),
        new IndexOptions().expireAfter(minutesToExpire, TimeUnit.MINUTES));
    this.gson = new Gson();
  }

  @Override
  public String create(Object target, String type) {
    String token = UUID.randomUUID().toString();
    Document document = new Document("_id", token)
        .append("createdAt", new Date())
        .append("type", type)
        .append("object", Document.parse(this.gson.toJson(target)));
    this.requests.insertOne(document);
    return token;
  }

  @Override
  public <T> Request<T> pull(String token) {
    Document document = this.requests.findOneAndDelete(new BasicDBObject("_id", token));
    if (document != null) {
      return new BotecoRequest<>(document);
    }
    return null;
  }

}

@@ -1355,16 +1355,58 @@
 Object;%0A
+import com.mongodb.MongoCommandException;%0A
 import c
@@ -1436,24 +1436,24 @@
 Collection;%0A
-
 import com.m
@@ -1553,16 +1553,73 @@
 cument;%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
 import t
@@ -1948,16 +1948,111 @@
 tory %7B%0A%0A
+  private static final Logger logger = LoggerFactory.getLogger(MongoRequestRepository.class);%0A%0A
   privat
@@ -2199,24 +2199,24 @@
 ToExpire) %7B%0A
-
     this.req
@@ -2259,16 +2259,262 @@
 ests%22);%0A
+    try %7B%0A      this.requests.dropIndex(%22createdAt_1%22);%0A      logger.info(%22Dropped index 'createdAt', creating a new one.%22);%0A    %7D catch (MongoCommandException e) %7B%0A      logger.info(%22Index for 'createdAt' doesn't exist, creating index.%22);%0A    %7D%0A
     this

delete de index before creating the new one