/**
 * 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.management.internal.key;

import io.gravitee.common.data.domain.Page;
import io.gravitee.repository.management.api.search.ApiKeyCriteria;
import io.gravitee.repository.mongodb.management.internal.model.ApiKeyMongo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.Date;
import java.util.List;

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

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Page<ApiKeyMongo> search(ApiKeyCriteria filter) {
        Query query = new Query();

        if (! filter.isIncludeRevoked()) {
            query.addCriteria(Criteria.where("revoked").is(false));
        }

        if (filter.getPlans() != null) {
            query.addCriteria(Criteria.where("plan").in(filter.getPlans()));
        }

        // set range query
        if (filter.getFrom() != 0 && filter.getTo() != 0) {
            query.addCriteria(Criteria.where("updatedAt").gte(new Date(filter.getFrom())).lt(new Date(filter.getTo())));
        }

        query.with(new Sort(Sort.Direction.DESC, "updatedAt"));

        List<ApiKeyMongo> events = mongoTemplate.find(query, ApiKeyMongo.class);
        long total = mongoTemplate.count(query, ApiKeyMongo.class);

        return new Page<>(events, 0, 0, total);
    }
}

@@ -1579,17 +1579,16 @@
    if (!
- 
 filter.i
@@ -1604,24 +1604,24 @@
 evoked()) %7B%0A
+
             
@@ -2020,24 +2020,24 @@
 getTo())));%0A
-
         %7D%0A%0A 
@@ -2027,32 +2027,745 @@
 )));%0A        %7D%0A%0A
+        if (filter.getExpireAfter() %3E 0 %7C%7C filter.getExpireBefore() %3E 0) %7B%0A            // Need to mutualize the instantiation of this filter otherwise mongo driver is throwing an error, when%0A            // using multiple %60Criteria.where(%22expireAt%22).xxx%60 with the same query%0A            Criteria expireAtCriteria = Criteria.where(%22expireAt%22);%0A%0A            if (filter.getExpireAfter() %3E 0) %7B%0A                expireAtCriteria = expireAtCriteria.gte(new Date(filter.getExpireAfter()));%0A            %7D%0A            if (filter.getExpireBefore() %3E 0) %7B%0A                expireAtCriteria = expireAtCriteria.lte(new Date(filter.getExpireBefore()));%0A            %7D%0A%0A            query.addCriteria(expireAtCriteria);%0A        %7D%0A%0A%0A
         query.wi

handle fields to filter api key by expiration date