<commit_before>package fi.mml.map.mapwindow.util;

import fi.mml.portti.domain.permissions.Permissions;
import fi.mml.portti.service.db.permissions.PermissionsService;
import fi.mml.portti.service.db.permissions.PermissionsServiceIbatisImpl;
import fi.nls.oskari.domain.Role;
import fi.nls.oskari.domain.User;
import fi.nls.oskari.domain.map.OskariLayer;
import fi.nls.oskari.log.LogFactory;
import fi.nls.oskari.log.Logger;
import fi.nls.oskari.map.layer.OskariLayerService;
import fi.nls.oskari.map.layer.OskariLayerServiceIbatisImpl;
import fi.nls.oskari.map.layer.formatters.LayerJSONFormatter;
import fi.nls.oskari.util.JSONHelper;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Worker class for rendering json objects from domain objects
 */
public class OskariLayerWorker {

    public static final String KEY_LAYERS = "layers";

    private static final String NO_PUBLICATION_PERMISSION = "no_publication_permission";
    private static final String PUBLICATION_PERMISSION_OK = "publication_permission_ok";


    private static Logger log = LogFactory.getLogger(OskariLayerWorker.class);

    private static OskariLayerService mapLayerService = new OskariLayerServiceIbatisImpl();
    private static PermissionsService permissionsService = new PermissionsServiceIbatisImpl();

    private final static LayerJSONFormatter FORMATTER = new LayerJSONFormatter();

    public static JSONObject getListOfAllMapLayers(final User user, final String lang) {
        long start = System.currentTimeMillis();
        final List<OskariLayer> layers = mapLayerService.findAll();
        log.debug("Layers loaded in", System.currentTimeMillis() - start, "ms");
        final boolean isSecure = false;
        final boolean isPublished = false;
        return getListOfMapLayers(layers, user, lang, isPublished, isSecure);
    }

    /**
     * Gets all the selected map layers
     *
     * @param layerIdList List of selected layer IDs
     * @param user        User
     * @param lang        Language
     * @param isPublished Determines the permission type used for the layers (view/published view)
     * @param isSecure    true to modify urls for easier proxy forwarding/false to keep as is
     * @return JSONObject containing the selected layers
     */
    public static JSONObject getListOfMapLayersById(final List<String> layerIdList, final User user,
                                                    final String lang, final boolean isPublished, final boolean isSecure) {
        final List<OskariLayer> layers = mapLayerService.find(layerIdList);
        return getListOfMapLayers(layers, user, lang, isPublished, isSecure);
    }

    /**
     * Gets all the map layers the user is permitted to view
     *
     * @param layers      list of layers to render
     * @param user        User
     * @param lang        Language
     * @param isPublished Determines the permission type used for the layers (view/published view)
     * @param isSecure    true to modify urls for easier proxy forwarding/false to keep as is
     * @return JSONObject of map layers
     */
    public static JSONObject getListOfMapLayers(final List<OskariLayer> layers, final User user,
                                                final String lang, final boolean isPublished, final boolean isSecure) {

        final String permissionType = getPermissionType(isPublished);
        long start = System.currentTimeMillis();
        final List<String> resources = permissionsService.getResourcesWithGrantedPermissions(
                Permissions.RESOURCE_TYPE_MAP_LAYER, user, permissionType);
        log.debug("View permissions loaded in", System.currentTimeMillis() - start, "ms");

        start = System.currentTimeMillis();
        final Set<String> permissionsList = permissionsService.getPublishPermissions();
        log.debug("Publish permissions loaded in", System.currentTimeMillis() - start, "ms");
        start = System.currentTimeMillis();
        final Set<String> editAccessList = permissionsService.getEditPermissions();
        log.debug("Edit permissions loaded in", System.currentTimeMillis() - start, "ms");

        final JSONArray layersList = new JSONArray();
        start = System.currentTimeMillis();
        for (OskariLayer layer : layers) {
            final String permissionKey = layer.getUrl() + "+" + layer.getName();
            if (!resources.contains(permissionKey)) {
                // not permitted!
                continue;
            }
            try {
                final JSONObject layerJson = FORMATTER.getJSON(layer, lang, isSecure);
                //log.debug("Generated JSON");
                if (layerJson != null) {
                    //log.debug("Generating permissions JSON");
                    JSONObject permissions = getPermissions(user, permissionKey, permissionsList, editAccessList);
                    JSONHelper.putValue(layerJson, "permissions", permissions);
                    if(permissions.optBoolean("edit")) {
                        // has edit rights, alter JSON/add info for admin bundle
                        modifyCommonFieldsForEditing(layerJson, layer);
                    }

                    //log.debug("Adding layer to list");
                    layersList.put(layerJson);<commit_msg>Permit sublayers to be created as JSON without checking permissions (permissions should be tagged on collection layer<commit_after>package fi.mml.map.mapwindow.util;

import fi.mml.portti.domain.permissions.Permissions;
import fi.mml.portti.service.db.permissions.PermissionsService;
import fi.mml.portti.service.db.permissions.PermissionsServiceIbatisImpl;
import fi.nls.oskari.domain.Role;
import fi.nls.oskari.domain.User;
import fi.nls.oskari.domain.map.OskariLayer;
import fi.nls.oskari.log.LogFactory;
import fi.nls.oskari.log.Logger;
import fi.nls.oskari.map.layer.OskariLayerService;
import fi.nls.oskari.map.layer.OskariLayerServiceIbatisImpl;
import fi.nls.oskari.map.layer.formatters.LayerJSONFormatter;
import fi.nls.oskari.util.JSONHelper;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Worker class for rendering json objects from domain objects
 */
public class OskariLayerWorker {

    public static final String KEY_LAYERS = "layers";

    private static final String NO_PUBLICATION_PERMISSION = "no_publication_permission";
    private static final String PUBLICATION_PERMISSION_OK = "publication_permission_ok";


    private static Logger log = LogFactory.getLogger(OskariLayerWorker.class);

    private static OskariLayerService mapLayerService = new OskariLayerServiceIbatisImpl();
    private static PermissionsService permissionsService = new PermissionsServiceIbatisImpl();

    private final static LayerJSONFormatter FORMATTER = new LayerJSONFormatter();

    public static JSONObject getListOfAllMapLayers(final User user, final String lang) {
        long start = System.currentTimeMillis();
        final List<OskariLayer> layers = mapLayerService.findAll();
        log.debug("Layers loaded in", System.currentTimeMillis() - start, "ms");
        final boolean isSecure = false;
        final boolean isPublished = false;
        return getListOfMapLayers(layers, user, lang, isPublished, isSecure);
    }

    /**
     * Gets all the selected map layers
     *
     * @param layerIdList List of selected layer IDs
     * @param user        User
     * @param lang        Language
     * @param isPublished Determines the permission type used for the layers (view/published view)
     * @param isSecure    true to modify urls for easier proxy forwarding/false to keep as is
     * @return JSONObject containing the selected layers
     */
    public static JSONObject getListOfMapLayersById(final List<String> layerIdList, final User user,
                                                    final String lang, final boolean isPublished, final boolean isSecure) {
        final List<OskariLayer> layers = mapLayerService.find(layerIdList);
        return getListOfMapLayers(layers, user, lang, isPublished, isSecure);
    }

    /**
     * Gets all the map layers the user is permitted to view
     *
     * @param layers      list of layers to render
     * @param user        User
     * @param lang        Language
     * @param isPublished Determines the permission type used for the layers (view/published view)
     * @param isSecure    true to modify urls for easier proxy forwarding/false to keep as is
     * @return JSONObject of map layers
     */
    public static JSONObject getListOfMapLayers(final List<OskariLayer> layers, final User user,
                                                final String lang, final boolean isPublished, final boolean isSecure) {

        final String permissionType = getPermissionType(isPublished);
        long start = System.currentTimeMillis();
        final List<String> resources = permissionsService.getResourcesWithGrantedPermissions(
                Permissions.RESOURCE_TYPE_MAP_LAYER, user, permissionType);
        log.debug("View permissions loaded in", System.currentTimeMillis() - start, "ms");

        start = System.currentTimeMillis();
        final Set<String> permissionsList = permissionsService.getPublishPermissions();
        log.debug("Publish permissions loaded in", System.currentTimeMillis() - start, "ms");
        start = System.currentTimeMillis();
        final Set<String> editAccessList = permissionsService.getEditPermissions();
        log.debug("Edit permissions loaded in", System.currentTimeMillis() - start, "ms");

        final JSONArray layersList = new JSONArray();
        start = System.currentTimeMillis();
        for (OskariLayer layer : layers) {
            final String permissionKey = layer.getUrl() + "+" + layer.getName();
            if (layer.getParentId() == -1 && !resources.contains(permissionKey)) {
                // not permitted if resource NOT found in permissions!
                // sublayers can pass through since their parentId != -1
                continue;
            }
            try {
                final JSONObject layerJson = FORMATTER.getJSON(layer, lang, isSecure);
                //log.debug("Generated JSON");
                if (layerJson != null) {
                    //log.debug("Generating permissions JSON");
                    JSONObject permissions = getPermissions(user, permissionKey, permissionsList, editAccessList);
                    JSONHelper.putValue(layerJson, "permissions", permissions);
                    if(permissions.optBoolean("edit")) {
                        // has edit rights, alter JSON/add info for admin bundle
                        modifyCommonFieldsForEditing(layerJson, layer);
                    }

                    //log.debug("Adding layer to list");
                    layersList.put(layerJson);
{'fraction_non_alphanumeric': 0.08303806668483692, 'fraction_numerical': 0.00018170255292086854, 'mean_word_length': 2.8733286418015482, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5630904', 'n_tokens_mistral': 2901, 'n_tokens_neox': 2822, 'n_words': 809}