/**
 * Copyright (C) 2015 The Gravitee team (http://gravitee.io)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.gravitee.repository.mongodb.ratelimit;

import com.mongodb.BasicDBObjectBuilder;
import com.mongodb.DBObject;
import io.gravitee.repository.ratelimit.api.RateLimitRepository;
import io.gravitee.repository.ratelimit.model.RateLimit;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.index.IndexDefinition;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.Iterator;

/**
 * @author David BRASSELY (david.brassely at graviteesource.com)
 * @author GraviteeSource Team
 */
@Component
public class MongoRateLimitRepository implements RateLimitRepository {

    @Autowired
    @Qualifier("rateLimitMongoTemplate")
    private MongoOperations mongoOperations;

    private final static String RATE_LIMIT_COLLECTION = "ratelimit";

    private final static String FIELD_KEY = "_id";
    private final static String FIELD_COUNTER = "counter";
    private final static String FIELD_RESET_TIME = "reset_time";
    private final static String FIELD_LAST_REQUEST = "last_request";
    private final static String FIELD_UPDATED_AT = "updated_at";
    private final static String FIELD_CREATED_AT = "created_at";
    private final static String FIELD_ASYNC = "async";

    @PostConstruct
    public void ensureTTLIndex() {
        mongoOperations.indexOps(RATE_LIMIT_COLLECTION).ensureIndex(new IndexDefinition() {
            @Override
            public Document getIndexKeys() {
                return new Document(FIELD_RESET_TIME, 1L);
            }

            @Override
            public Document getIndexOptions() {
                // To expire Documents at a Specific Clock Time we have to specify an expireAfterSeconds value of 0.
                return new Document("expireAfterSeconds", 0L);
            }
        });
    }

    @Override
    public RateLimit get(String rateLimitKey) {
        //because RateLimit has no default constructor, we have to manually map values
        Document result = mongoOperations.findById(rateLimitKey, Document.class, RATE_LIMIT_COLLECTION);

        RateLimit rateLimit = new RateLimit(rateLimitKey);

        if (result != null) {
            rateLimit.setCounter((long) result.get(FIELD_COUNTER));
            rateLimit.setLastRequest((long) result.get(FIELD_LAST_REQUEST));
            rateLimit.setResetTime(((Date) result.get(FIELD_RESET_TIME)).getTime());
            rateLimit.setUpdatedAt((long) result.get(FIELD_UPDATED_AT));
            rateLimit.setCreatedAt((long) result.get(FIELD_CREATED_AT));
            rateLimit.setAsync((boolean) result.get(FIELD_ASYNC));
        }
        return rateLimit;
    }

    @Override
    public void save(RateLimit rateLimit) {
        final DBObject doc = BasicDBObjectBuilder.start()
                .add(FIELD_KEY, rateLimit.getKey())
                .add(FIELD_COUNTER, rateLimit.getCounter())
                .add(FIELD_LAST_REQUEST, rateLimit.getLastRequest())
                .add(FIELD_RESET_TIME, new Date(rateLimit.getResetTime()))
                .add(FIELD_UPDATED_AT, rateLimit.getUpdatedAt())
                .add(FIELD_CREATED_AT, rateLimit.getCreatedAt())
                .add(FIELD_ASYNC, rateLimit.isAsync())
                .get();

        mongoOperations.save(doc, RATE_LIMIT_COLLECTION);
    }

    @Override
    public Iterator<RateLimit> findAsyncAfter(long timestamp) {
        final Query query = Query.query(Criteria.where(FIELD_ASYNC).is(true).and(FIELD_UPDATED_AT).gte(timestamp));
        return mongoOperations.find(query, RateLimit.class, RATE_LIMIT_COLLECTION).iterator();
    }
}

@@ -3021,25 +3021,24 @@
 CTION);%0A
-%0A
         
 RateLimi
@@ -3033,567 +3033,30 @@
     
-RateLimit rateLimit = new RateLimit(rateLimitKey);%0A%0A        if (result != null) %7B%0A            rateLimit.setCounter((long) result.get(FIELD_COUNTER));%0A            rateLimit.setLastRequest((long) result.get(FIELD_LAST_REQUEST));%0A            rateLimit.setResetTime(((Date) result.get(FIELD_RESET_TIME)).getTime());%0A            rateLimit.setUpdatedAt((long) result.get(FIELD_UPDATED_AT));%0A            rateLimit.setCreatedAt((long) result.get(FIELD_CREATED_AT));%0A            rateLimit.setAsync((boolean) result.get(FIELD_ASYNC));%0A        %7D%0A        return rateLimit
+return convert(result)
 ;%0A  
@@ -3944,24 +3944,23 @@
 (query, 
-RateLimi
+Documen
 t.class,
@@ -3986,19 +3986,760 @@
 ION)
-.iterator()
+%0A                .stream()%0A                .map(this::convert)%0A                .iterator();%0A    %7D%0A%0A    private RateLimit convert(Document document) %7B%0A        if (document == null) %7B%0A            return null;%0A        %7D%0A%0A        RateLimit rateLimit = new RateLimit(document.getString((FIELD_KEY)));%0A        rateLimit.setCounter((long) document.get(FIELD_COUNTER));%0A        rateLimit.setLastRequest((long) document.get(FIELD_LAST_REQUEST));%0A        rateLimit.setResetTime(((Date) document.get(FIELD_RESET_TIME)).getTime());%0A        rateLimit.setUpdatedAt((long) document.get(FIELD_UPDATED_AT));%0A        rateLimit.setCreatedAt((long) document.get(FIELD_CREATED_AT));%0A        rateLimit.setAsync((boolean) document.get(FIELD_ASYNC));%0A%0A        return rateLimit
 ;%0A  

Handle empty constructor after latest Spring upgrade