package fi.nls.oskari.control.layer;

import org.oskari.service.util.ServiceFactory;

import fi.nls.oskari.annotation.OskariActionRoute;
import fi.nls.oskari.control.ActionDeniedException;
import fi.nls.oskari.control.ActionException;
import fi.nls.oskari.control.ActionHandler;
import fi.nls.oskari.control.ActionParameters;
import fi.nls.oskari.log.LogFactory;
import fi.nls.oskari.log.Logger;
import fi.nls.oskari.map.layer.DataProviderService;
import fi.nls.oskari.util.ConversionHelper;

/**
 * Admin WMS organization layer delete
 * 
 * 
 */
@OskariActionRoute("DeleteOrganization")
public class DeleteOrganizationHandler extends ActionHandler {

    private DataProviderService groupService = ServiceFactory.getDataProviderService();
    private static final Logger log = LogFactory.getLogger(DeleteOrganizationHandler.class);
    private static final String PARAM_GROUP_ID = "id";

    @Override
    public void handleAction(ActionParameters params) throws ActionException {

        final int groupId = ConversionHelper.getInt(params.getRequiredParam(PARAM_GROUP_ID), -1);
        
        if(!groupService.hasPermissionToUpdate(params.getUser(), groupId)) {
            throw new ActionDeniedException("Unauthorized user tried to remove layer group and its map layers - group id=" + groupId);
        }
       
        try {
            // cascade in db will handle that layers are deleted
            groupService.delete(groupId);
        } catch (Exception e) {
            throw new ActionException("Couldn't delete layer group and its map layers - id:" + groupId,
                    e);
        }
    }

}

@@ -42,19 +42,22 @@
 ort 
-org
+fi.nls
 .oskari.
 serv
@@ -56,139 +56,105 @@
 ari.
-service.util.ServiceFactory;%0A%0Aimport fi.nls.oskari.annotation.OskariActionRoute;%0Aimport fi.nls.oskari.control.ActionDeniedException
+control.*;%0Aimport fi.nls.oskari.domain.map.DataProvider;%0Aimport fi.nls.oskari.util.ResponseHelper
 ;%0Aim
@@ -154,38 +154,35 @@
 per;%0Aimport 
-fi.nls
+org
 .oskari.
 control.Acti
@@ -173,341 +173,295 @@
 ari.
-control.ActionException;%0Aimport fi.nls.oskari.control.ActionHandler;%0Aimport fi.nls.oskari.control.ActionParameters;%0Aimport fi.nls.oskari.log.LogFactory;%0Aimport fi.nls.oskari.log.Logger;%0Aimport fi.nls.oskari.map.layer.DataProviderService;%0Aimport fi.nls.oskari.util.ConversionHelper;%0A%0A/**%0A * Admin WMS organization layer delete%0A * %0A * 
+service.util.ServiceFactory;%0A%0Aimport fi.nls.oskari.annotation.OskariActionRoute;%0Aimport fi.nls.oskari.map.layer.DataProviderService;%0Aimport fi.nls.oskari.util.ConversionHelper;%0Aimport static fi.nls.oskari.control.ActionConstants.PARAM_ID;%0A%0A/**%0A * For deleting a dataprovider/organization
 %0A */
@@ -657,156 +657,8 @@
 e();
-%0A    private static final Logger log = LogFactory.getLogger(DeleteOrganizationHandler.class);%0A    private static final String PARAM_GROUP_ID = %22id%22;
 %0A%0A  
@@ -781,32 +781,8 @@
 d = 
-ConversionHelper.getInt(
 para
@@ -804,28 +804,20 @@
 aram
+Int
 (PARAM_
-GROUP_ID), -1
+ID
 );%0A 
@@ -1046,23 +1046,16 @@
       %7D%0A
-       
 %0A       
@@ -1077,100 +1077,446 @@
     
-// cascade in db will handle that layers are deleted%0A            groupService.delete(groupId
+DataProvider organization = groupService.find(groupId);%0A            if(organization == null) %7B%0A                throw new ActionParamsException(%22Dataprovider not found with id %22 + groupId);%0A            %7D%0A            // cascade in db will handle that layers are deleted%0A            groupService.delete(groupId);%0A%0A            // write deleted organization as response%0A            ResponseHelper.writeResponse(params, organization.getAsJSON()
 );%0A 
@@ -1653,28 +1653,8 @@
 pId,
-%0A                   
  e);

Delete to respond with deleted dataprovider