package fi.nls.oskari.control.statistics.db;

import fi.nls.oskari.util.JSONHelper;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * This is the value object for the layer url and other metadata in the database.
 * MyBatis Type for the SQL table oskari_maplayers
 */
public class RegionSet {

    private long oskariLayerId; // oskari_maplayer.id
    private String oskariLayerName; // oskari_maplayer.name
    private String url; // oskari_maplayer.url
    private String srs; // oskari_maplayer.srs_name
    private String attributes; // oskari_maplayer.attributes

    private JSONObject stats; // Lazily populated by getStatsJSON()

    public long getOskariLayerId() {
        return oskariLayerId;
    }

    public void setOskariLayerId(long oskariLayerId) {
        this.oskariLayerId = oskariLayerId;
    }

    public String getOskariLayerName() {
        return oskariLayerName;
    }

    public void setOskariLayerName(String oskariLayerName) {
        this.oskariLayerName = oskariLayerName;
    }

    public String getAttributes() {
        return attributes;
    }

    public void setAttributes(String attributes) {
        this.attributes = attributes;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getSrs() {
        return srs;
    }

    public void setSrs(String srs) {
        this.srs = srs;
    }

    public JSONObject asJSON() {
        JSONObject tags = new JSONObject();
        try {
            // only regionset id and regionIdTag is used in frontend, and regionIdTag should propably come from datasource...
            //tags.put("nameIdTag", getNameProperty());
            tags.put("regionIdTag", getIdProperty());
            //tags.put("url", getUrl());
            //tags.put("featuresUrl", getFeaturesUrl());
        } catch (JSONException e) {
            throw new RuntimeException("Something went wrong serializing the region set", e);
        }
        return tags;
    }

    public String getNameProperty() {
        return getStatsJSON().optString("nameIdTag");
    }

    public String getIdProperty() {
        return getStatsJSON().optString("regionIdTag");
    }

    public String getFeaturesUrl() {
        return getStatsJSON().optString("featuresUrl");
    }

    private JSONObject getStatsJSON() {
        if (stats == null) {
            JSONObject json = JSONHelper.createJSONObject(attributes);
            if (json != null) {
                stats = json.optJSONObject("statistics");
            }
            if (stats == null) {
                stats = new JSONObject();
            }
        }
        return stats;
    }

}

@@ -81,39 +81,8 @@
 er;%0A
-import org.json.JSONException;%0A
 impo
@@ -1452,551 +1452,74 @@
     
-JSONObject tags = new JSONObject();%0A        try %7B%0A            // only regionset id and regionIdTag is used in frontend, and regionIdTag should propably come from datasource...%0A            //tags.put(%22nameIdTag%22, getNameProperty());%0A            tags.put(%22regionIdTag%22, getIdProperty());%0A            //tags.put(%22url%22, getUrl());%0A            //tags.put(%22featuresUrl%22, getFeaturesUrl());%0A        %7D catch (JSONException e) %7B%0A            throw new RuntimeException(%22Something went wrong serializing the region set%22, e);%0A        %7D%0A        return tags
+return JSONHelper.createJSONObject(%22regionIdTag%22, getIdProperty())
 ;%0A  

Remove out-commented code from RegionSet#asJSON()