package org.oskari.control.userlayer;

import org.oskari.map.userlayer.service.UserLayerDbService;

import fi.nls.oskari.control.ActionConstants;
import fi.nls.oskari.control.ActionDeniedException;
import fi.nls.oskari.control.ActionException;
import fi.nls.oskari.control.ActionParameters;
import fi.nls.oskari.control.ActionParamsException;
import fi.nls.oskari.domain.map.userlayer.UserLayer;

public class UserLayerHandlerHelper {

    public static UserLayer getUserLayer(UserLayerDbService service, ActionParameters params) throws ActionException {
        params.requireLoggedInUser();
        long id = params.getRequiredParamLong(ActionConstants.KEY_ID);
        UserLayer userLayer = service.getUserLayerById(id);
        if (userLayer == null) {
            throw new ActionParamsException("UserLayer doesn't exist: " + id);
        }
        if (!userLayer.isOwnedBy(params.getUser().getUuid())) {
            throw new ActionDeniedException("UserLayer belongs to another user");
        }
        return userLayer;
    }

}

@@ -32,16 +32,116 @@
 layer;%0A%0A
+import java.util.Map;%0Aimport java.util.Set;%0A%0Aimport org.json.JSONArray;%0Aimport org.json.JSONObject;%0A
 import o
@@ -192,16 +192,16 @@
 ervice;%0A
-
 %0Aimport 
@@ -488,16 +488,54 @@
 erLayer;
+%0Aimport fi.nls.oskari.util.JSONHelper;
 %0A%0Apublic
@@ -1165,11 +1165,1591 @@
 ;%0A    %7D%0A
+    public static JSONObject createErrorJSON (String error, String key, Set%3CString%3E set) %7B%0A        JSONObject obj = createErrorJSON (error);%0A        JSONHelper.put(obj, key, parseStringSet(set));%0A        return obj;%0A    %7D%0A    public static JSONObject createErrorJSON (String error, String key, String value) %7B%0A        JSONObject obj = JSONHelper.createJSONObject(%22error%22, error);%0A        JSONHelper.putValue(obj, key, value);%0A        return obj;%0A    %7D%0A    public static JSONObject createErrorJSON (String error) %7B%0A        return JSONHelper.createJSONObject(%22error%22, error);%0A    %7D%0A    public static void addSetToErrorJSON (JSONObject obj, String key, Set%3CString%3E set) %7B%0A        if (set.isEmpty())%7B%0A            return;%0A        %7D%0A        if (obj == null) %7B%0A            obj = new JSONObject();%0A        %7D%0A        JSONHelper.put(obj, key, parseStringSet(set));%0A    %7D%0A    public static void addMapToErrorJSON (JSONObject obj, String key, Map%3CString,String%3E map) %7B%0A        if(map.isEmpty())%7B%0A            return;%0A        %7D%0A        if (obj == null) %7B%0A            obj = new JSONObject();%0A        %7D%0A        JSONHelper.putValue(obj, key, new JSONObject(map));%0A    %7D%0A    public static JSONObject addStringToErrorJSON (JSONObject obj, String key, String str) %7B%0A        if (obj == null) %7B%0A            obj = new JSONObject();%0A        %7D%0A        JSONHelper.putValue(obj, key, str);%0A        return obj;%0A    %7D%0A    public static JSONArray parseStringSet (Set%3CString%3E set)%7B%0A        JSONArray json = new JSONArray();%0A        set.stream()%0A            .forEach(e -%3E json.put(e));%0A        return json;%0A    %7D
 %0A%7D%0A

add helpers for creating json errors