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);
        }
    }

}

package fi.nls.oskari.control.layer;

import fi.nls.oskari.control.*;
import fi.nls.oskari.domain.map.DataProvider;
import fi.nls.oskari.util.ResponseHelper;
import org.oskari.service.util.ServiceFactory;

import fi.nls.oskari.annotation.OskariActionRoute;
import fi.nls.oskari.map.layer.DataProviderService;
import fi.nls.oskari.util.ConversionHelper;
import static fi.nls.oskari.control.ActionConstants.PARAM_ID;

/**
 * For deleting a dataprovider/organization
 */
@OskariActionRoute("DeleteOrganization")
public class DeleteOrganizationHandler extends ActionHandler {

    private DataProviderService groupService = ServiceFactory.getDataProviderService();

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

        final int groupId = params.getRequiredParamInt(PARAM_ID);
        
        if(!groupService.hasPermissionToUpdate(params.getUser(), groupId)) {
            throw new ActionDeniedException("Unauthorized user tried to remove layer group and its map layers - group id=" + groupId);
        }

        try {
            DataProvider organization = groupService.find(groupId);
            if(organization == null) {
                throw new ActionParamsException("Dataprovider not found with id " + groupId);
            }
            // cascade in db will handle that layers are deleted
            groupService.delete(groupId);

            // write deleted organization as response
            ResponseHelper.writeResponse(params, organization.getAsJSON());
        } catch (Exception e) {
            throw new ActionException("Couldn't delete layer group and its map layers - id:" + groupId, e);
        }
    }

}

Delete to respond with deleted dataprovider