package org.oskari.service.mvt.wfs;

import java.util.Map;

import org.geotools.data.simple.SimpleFeatureCollection;

import fi.nls.oskari.cache.ComputeOnceCache;
import fi.nls.oskari.util.IOHelper;

/**
 * Caching version of {@link org.oskari.service.mvt.wfs.OskariWFS110Client} WFSClient
 * Caches the parsed SimpleFeatureCollections
 */
public class CachingWFSClient extends OskariWFS110Client {

    private static final int DEFAULT_LIMIT = 100;

    private final ComputeOnceCache<SimpleFeatureCollection> cache;

    public CachingWFSClient() {
        this(DEFAULT_LIMIT);
    }

    public CachingWFSClient(int cacheSize) {
        cache = new ComputeOnceCache<>(cacheSize);
    }

    @Override
    public SimpleFeatureCollection tryGetFeatures(String endPoint, String user, String pass,
            String typeName, double[] bbox, String srsName, Integer maxFeatures) {
        String key = getCacheKey(endPoint, user, pass, typeName, bbox, srsName, maxFeatures);
        return cache.get(key, __ -> super.tryGetFeatures(endPoint, user, pass, typeName, bbox, srsName, maxFeatures));
    }

    private String getCacheKey(String endPoint, String user, String pass, String typeName, double[] bbox, String srsName, Integer maxFeatures) {
        // Use the request as the cache key
        Map<String, String> getFeatureKVP = getQueryParams(typeName, bbox, srsName, maxFeatures);
        return IOHelper.constructUrl(endPoint, getFeatureKVP);
    }

}

@@ -34,31 +34,8 @@
 s;%0A%0A
-import java.util.Map;%0A%0A
 impo
@@ -136,44 +136,8 @@
 che;
-%0Aimport fi.nls.oskari.util.IOHelper;
 %0A%0A/*
@@ -852,36 +852,24 @@
 ey(endPoint,
- user, pass,
  typeName, b
@@ -888,32 +888,32 @@
 , maxFeatures);%0A
+
         return c
@@ -1073,34 +1073,8 @@
 int,
- String user, String pass,
  Str
@@ -1153,43 +1153,72 @@
     
-// Use the request as the cache key
+String bboxStr = bbox != null ? getBBOX(bbox, srsName) : %22null%22;
 %0A   
@@ -1226,158 +1226,162 @@
     
-Map%3C
 String
-, String%3E getFeatureKVP = getQueryParams(typeName, bbox, srsName, maxFeatures);%0A        return IOHelper.constructUrl(endPoint, get
+ maxFeaturesStr = maxFeatures != null ? maxFeatures.toString() : %22null%22;%0A        return String.join(%22,%22, endPoint, typeName, bboxStr, max
 Feature
-KVP
+sStr
 );%0A 

Change the cache key structure