<commit_before>/*
 * Copyright 2017 Crown Copyright
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package uk.gov.gchq.gaffer.operation.export.graph.handler;

import uk.gov.gchq.gaffer.graph.Graph;
import uk.gov.gchq.gaffer.operation.export.graph.ExportToOtherAuthorisedGraph;
import uk.gov.gchq.gaffer.operation.export.graph.OtherGraphExporter;
import uk.gov.gchq.gaffer.store.Context;
import uk.gov.gchq.gaffer.store.Store;
import uk.gov.gchq.gaffer.store.StoreProperties;
import uk.gov.gchq.gaffer.store.library.GraphLibrary;
import uk.gov.gchq.gaffer.store.operation.handler.export.ExportToHandler;
import uk.gov.gchq.gaffer.store.schema.Schema;
import uk.gov.gchq.gaffer.user.User;
import uk.gov.gchq.koryphe.ValidationResult;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ExportToOtherAuthorisedGraphHandler extends ExportToHandler<ExportToOtherAuthorisedGraph, OtherGraphExporter> {

    private Map<String, List<String>> idAuths = new HashMap<>();

    public Map<String, List<String>> getIdAuths() {
        return idAuths;
    }

    public void setIdAuths(Map<String, List<String>> idAuths) {
        if (idAuths == null) {
            this.idAuths = new HashMap<>();
        } else {
            this.idAuths = idAuths;
        }
    }

    @Override
    protected Class<OtherGraphExporter> getExporterClass() {
        return OtherGraphExporter.class;
    }

    @Override
    protected OtherGraphExporter createExporter(final ExportToOtherAuthorisedGraph export, final Context context, final Store store) {
        return new OtherGraphExporter(context.getUser(), context.getJobId(), createGraph(export, context, store));
    }

    protected Graph createGraph(final ExportToOtherAuthorisedGraph export, final Context context, final Store store) {
        final String exportGraphId = export.getGraphId();
        final String exportParentSchemaId = export.getParentSchemaId();
        final String exportParentStorePropertiesId = export.getParentStorePropertiesId();
        final GraphLibrary graphLibrary = store.getGraphLibrary();

        ValidationResult validationResult = validate(exportGraphId, exportParentSchemaId, exportParentStorePropertiesId, graphLibrary, store);
        if (!validationResult.isValid()) {
            throw new IllegalArgumentException(validationResult.getErrorString());
        }

        if (!isAuthorised(context.getUser(), idAuths.get(exportGraphId))) {
            throw new IllegalArgumentException("User is not authorised to export using graph id: " + exportGraphId);
        }


        if (export.getGraphId() != null && exportParentSchemaId == null && exportParentStorePropertiesId == null) {
            return buildGraph(graphLibrary, exportGraphId, null, null, store);
        } else if (exportGraphId != null && exportParentSchemaId != null && exportParentStorePropertiesId != null) {
            if (isAuthorised(context.getUser(), idAuths.get(exportParentSchemaId))) {
                if (isAuthorised(context.getUser(), idAuths.get(exportParentStorePropertiesId))) {
                    return buildGraph(graphLibrary, exportGraphId, exportParentSchemaId, exportParentStorePropertiesId, store);
                } else {
                    throw new IllegalArgumentException("User is not authorised to export using parentStorePropertiesId: " + exportParentStorePropertiesId);
                }
            } else {
                throw new IllegalArgumentException("User is not authorised to export using parentSchemaId(s): " + exportParentSchemaId);
            }
        }
        throw new IllegalArgumentException("Export failed with input:\n graphId:" + exportGraphId);
    }

    private boolean isAuthorised(User user, List<String> auths) {
        if (auths != null && !auths.isEmpty()) {
            for (String auth : auths) {
                if (user.getOpAuths().contains(auth)) {
                    return true;
                }
            }
        }
        return false;
    }

    private Graph buildGraph(final GraphLibrary graphLibrary, final String graphId,
                             final String parentSchemaId, final String parentStorePropertiesId,
                             final Store store) {
        if (graphLibrary == null) {
            throw new IllegalArgumentException("graphLibrary is null");
        }
        if (graphLibrary.exists(graphId)) {
            return new Graph.Builder()
                    .graphId(graphId)
                    .library(graphLibrary)<commit_msg>gh-1085 - checkstyle fixes<commit_after>/*
 * Copyright 2017 Crown Copyright
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package uk.gov.gchq.gaffer.operation.export.graph.handler;

import uk.gov.gchq.gaffer.graph.Graph;
import uk.gov.gchq.gaffer.operation.export.graph.ExportToOtherAuthorisedGraph;
import uk.gov.gchq.gaffer.operation.export.graph.OtherGraphExporter;
import uk.gov.gchq.gaffer.store.Context;
import uk.gov.gchq.gaffer.store.Store;
import uk.gov.gchq.gaffer.store.StoreProperties;
import uk.gov.gchq.gaffer.store.library.GraphLibrary;
import uk.gov.gchq.gaffer.store.operation.handler.export.ExportToHandler;
import uk.gov.gchq.gaffer.store.schema.Schema;
import uk.gov.gchq.gaffer.user.User;
import uk.gov.gchq.koryphe.ValidationResult;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ExportToOtherAuthorisedGraphHandler extends ExportToHandler<ExportToOtherAuthorisedGraph, OtherGraphExporter> {

    private Map<String, List<String>> idAuths = new HashMap<>();

    public Map<String, List<String>> getIdAuths() {
        return idAuths;
    }

    public void setIdAuths(final Map<String, List<String>> idAuths) {
        if (idAuths == null) {
            this.idAuths = new HashMap<>();
        } else {
            this.idAuths = idAuths;
        }
    }

    @Override
    protected Class<OtherGraphExporter> getExporterClass() {
        return OtherGraphExporter.class;
    }

    @Override
    protected OtherGraphExporter createExporter(final ExportToOtherAuthorisedGraph export, final Context context, final Store store) {
        return new OtherGraphExporter(context.getUser(), context.getJobId(), createGraph(export, context, store));
    }

    protected Graph createGraph(final ExportToOtherAuthorisedGraph export, final Context context, final Store store) {
        final String exportGraphId = export.getGraphId();
        final String exportParentSchemaId = export.getParentSchemaId();
        final String exportParentStorePropertiesId = export.getParentStorePropertiesId();
        final GraphLibrary graphLibrary = store.getGraphLibrary();

        ValidationResult validationResult = validate(exportGraphId, exportParentSchemaId, exportParentStorePropertiesId, graphLibrary, store);
        if (!validationResult.isValid()) {
            throw new IllegalArgumentException(validationResult.getErrorString());
        }

        if (!isAuthorised(context.getUser(), idAuths.get(exportGraphId))) {
            throw new IllegalArgumentException("User is not authorised to export using graph id: " + exportGraphId);
        }


        if (export.getGraphId() != null && exportParentSchemaId == null && exportParentStorePropertiesId == null) {
            return buildGraph(graphLibrary, exportGraphId, null, null, store);
        } else if (exportGraphId != null && exportParentSchemaId != null && exportParentStorePropertiesId != null) {
            if (isAuthorised(context.getUser(), idAuths.get(exportParentSchemaId))) {
                if (isAuthorised(context.getUser(), idAuths.get(exportParentStorePropertiesId))) {
                    return buildGraph(graphLibrary, exportGraphId, exportParentSchemaId, exportParentStorePropertiesId, store);
                } else {
                    throw new IllegalArgumentException("User is not authorised to export using parentStorePropertiesId: " + exportParentStorePropertiesId);
                }
            } else {
                throw new IllegalArgumentException("User is not authorised to export using parentSchemaId(s): " + exportParentSchemaId);
            }
        }
        throw new IllegalArgumentException("Export failed with input:\n graphId:" + exportGraphId);
    }

    private boolean isAuthorised(final User user, final List<String> auths) {
        if (auths != null && !auths.isEmpty()) {
            for (final String auth : auths) {
                if (user.getOpAuths().contains(auth)) {
                    return true;
                }
            }
        }
        return false;
    }

    private Graph buildGraph(final GraphLibrary graphLibrary, final String graphId,
                             final String parentSchemaId, final String parentStorePropertiesId,
                             final Store store) {
        if (graphLibrary == null) {
            throw new IllegalArgumentException("graphLibrary is null");
        }
        if (graphLibrary.exists(graphId)) {
            return new Graph.Builder()
                    .graphId(graphId)
                    .library(graphLibrary)
{'fraction_non_alphanumeric': 0.09032513094179266, 'fraction_numerical': 0.001976479889317126, 'mean_word_length': 3.0707964601769913, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5716753', 'n_tokens_mistral': 2677, 'n_tokens_neox': 2586, 'n_words': 687}