/**
 * Copyright 2019 interactive instruments GmbH
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package de.ii.ldproxy.ogcapi.filter;

import com.google.common.collect.ImmutableSet;
import de.ii.ldproxy.ogcapi.domain.OgcApiDatasetData;
import de.ii.ldproxy.ogcapi.domain.OgcApiParameterExtension;
import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Instantiate;
import org.apache.felix.ipojo.annotations.Provides;

import java.util.Set;

@Component
@Provides
@Instantiate
public class OgcApiParameterFilter implements OgcApiParameterExtension {

    @Override
    public boolean isEnabledForApi(OgcApiDatasetData apiData) {
        return isExtensionEnabled(apiData, FilterConfiguration.class);
    }

    @Override
    public ImmutableSet<String> getParameters(OgcApiDatasetData apiData, String subPath) {
        if (!isEnabledForApi(apiData))
            return ImmutableSet.of();

        if (subPath.matches("^/[\\w\\-]+/items/?$")) {
            // Features
            return ImmutableSet.of("filter");
        }
        return ImmutableSet.of();
    }

    @Override
    public Set<String> getFilterParameters(Set<String> filterParameters, OgcApiDatasetData apiData) {
        if (!isEnabledForApi(apiData))
            return filterParameters;

        return new ImmutableSet.Builder<String>()
                .addAll(filterParameters)
                .add("filter")
                .build();
    }

    /* TODO
    @Override
    public ImmutableFeatureQuery.Builder transformQuery(FeatureTypeConfigurationOgcApi featureTypeConfiguration,
                                                        ImmutableFeatureQuery.Builder queryBuilder,
                                                        Map<String, String> parameters, OgcApiDatasetData datasetData) {

        if (!isEnabledForApi(datasetData)) {
            return queryBuilder;
        }

        if (parameters.containsKey("filter")) {
            queryBuilder.filter(parameters.get("filter"));
        }

        return queryBuilder;
    }
     */


}

@@ -338,16 +338,83 @@
 bleSet;%0A
+import de.ii.ldproxy.ogcapi.domain.FeatureTypeConfigurationOgcApi;%0A
 import d
@@ -520,16 +520,86 @@
 ension;%0A
+import de.ii.xtraplatform.feature.provider.api.ImmutableFeatureQuery;%0A
 import o
@@ -751,16 +751,78 @@
 vides;%0A%0A
+import javax.ws.rs.BadRequestException;%0Aimport java.util.Map;%0A
 import j
@@ -940,24 +940,87 @@
 xtension %7B%0A%0A
+    private static final String FILTER_LANG_CQL = %22cql-text%22;%0A%0A
     @Overrid
@@ -1468,16 +1468,31 @@
 %22filter%22
+, %22filter-lang%22
 );%0A     
@@ -1890,20 +1890,8 @@
  %7D%0A%0A
-    /* TODO%0A
     
@@ -2251,73 +2251,149 @@
 if (
-!isEnabledForApi(datasetData)) %7B%0A            return queryBuilder;
+parameters.containsKey(%22filter-lang%22) && !FILTER_LANG_CQL.equals(parameters.get(%22filter-lang%22))) %7B%0A            throw new BadRequestException(
 %0A   
@@ -2397,27 +2397,28 @@
         
-%7D%0A%0A
+    
         
 if (para
@@ -2413,103 +2413,197 @@
     
-if (parameters.containsKey(%22filter%22)) %7B%0A            queryBuilder.filter(parameters.get(%22filter%22
+String.format(%22The following value for query parameter filter-lang is rejected: %25s. Valid parameter value is: %25s%22,%0A                            parameters.get(%22filter-lang%22), FILTER_LANG_CQL
 ));%0A
@@ -2652,16 +2652,8 @@
   %7D%0A
-     */%0A
 %0A%0A%7D%0A

Add filter-lang query parameter