@@ -0,0 +1,3011 @@
+package fi.nls.oskari.control.admin;%0A%0Aimport com.fasterxml.jackson.core.JsonProcessingException;%0Aimport com.fasterxml.jackson.databind.ObjectMapper;%0Aimport fi.nls.oskari.annotation.OskariActionRoute;%0Aimport fi.nls.oskari.control.*;%0Aimport fi.nls.oskari.domain.map.OskariLayer;%0Aimport fi.nls.oskari.log.LogFactory;%0Aimport fi.nls.oskari.log.Logger;%0Aimport fi.nls.oskari.map.layer.OskariLayerService;%0Aimport fi.nls.oskari.service.OskariComponentManager;%0Aimport fi.nls.oskari.service.ServiceRuntimeException;%0Aimport fi.nls.oskari.util.ResponseHelper;%0Aimport org.json.JSONException;%0Aimport org.json.JSONObject;%0A%0Aimport java.util.*;%0A%0A@OskariActionRoute(%22LayerAdminUsageCheck%22)%0Apublic class LayerAdminUsageCheckHandler extends RestActionHandler %7B%0A    private static final Logger LOG = LogFactory.getLogger(LayerAdminUsageCheckHandler.class);%0A%0A    private static final String PARAM_LAYER_ID = %22id%22;%0A    private OskariLayerService mapLayerService;%0A    private ObjectMapper mapper = new ObjectMapper();%0A%0A    @Override%0A    public void init() %7B%0A        try %7B%0A            mapLayerService = OskariComponentManager.getComponentOfType(OskariLayerService.class);%0A        %7D catch (Exception e) %7B%0A            throw new ServiceRuntimeException(%22Exception occured while initializing map layer service%22, e);%0A        %7D%0A    %7D%0A%0A    @Override%0A    public void handleGet(ActionParameters params) throws ActionException %7B%0A        final int layerId = params.getRequiredParamInt(PARAM_LAYER_ID);%0A        LOG.info(%22Checking layer usage in other layers for layerId: %22, layerId);%0A        Map%3CString, Set%3CInteger%3E%3E layerUsages = new HashMap%3C%3E();%0A        Set%3CInteger%3E timeseriesLayerIds = new HashSet%3C%3E();%0A        for (OskariLayer layer : mapLayerService.findAll()) %7B%0A            JSONObject options = layer.getOptions();%0A            try %7B%0A                if (options != null && options.has(%22timeseries%22)) %7B%0A                    JSONObject timeseriesOptions = options.getJSONObject(%22timeseries%22);%0A                    Integer metadataLayerId = timeseriesOptions.getJSONObject(%22metadata%22).getInt(%22layer%22);%0A                    if (metadataLayerId == layerId) %7B%0A                        timeseriesLayerIds.add(layer.getId());%0A                    %7D%0A                %7D%0A            %7D catch (JSONException e) %7B%0A                throw new ActionException(%22Cannot parse layer metadata options for layer: %22 +%0A                        layer.getName() + %22, id: %22 + layer.getId());%0A            %7D%0A%0A        %7D%0A        layerUsages.put(%22timeseries%22, timeseriesLayerIds);%0A        writeResponse(params, layerUsages);%0A    %7D%0A%0A    private void writeResponse(ActionParameters params, Object output) %7B%0A        params.getResponse().setCharacterEncoding(%22UTF-8%22);%0A        params.getResponse().setContentType(%22application/json;charset=UTF-8%22);%0A        try %7B%0A            ResponseHelper.writeResponse(params, mapper.writeValueAsString(output));%0A        %7D catch (JsonProcessingException e) %7B%0A            ResponseHelper.writeError(params, %22Couldn't serialize JSON%22);%0A        %7D%0A    %7D%0A%0A%7D%0A

Add layer usage check