<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 stroom.receive.rules.impl;

import stroom.docref.DocRef;
import stroom.docstore.api.DocumentResourceHelper;
import stroom.importexport.api.DocumentData;
import stroom.importexport.api.ImportExportActionHandler;
import stroom.importexport.shared.Base64EncodedDocumentData;
import stroom.importexport.shared.ImportState;
import stroom.importexport.shared.ImportState.ImportMode;
import stroom.receive.rules.shared.ReceiveDataRuleSetResource;
import stroom.receive.rules.shared.ReceiveDataRules;
import stroom.util.shared.EntityServiceException;

import java.util.ArrayList;
import java.util.Map;
import java.util.Set;
import javax.inject.Inject;
import javax.inject.Provider;

public class ReceiveDataRuleSetResourceImpl implements ReceiveDataRuleSetResource {

    private final Provider<ReceiveDataRuleSetService> ruleSetServiceProvider;
    private final Provider<DocumentResourceHelper> documentResourceHelperProvider;

    @Inject
    ReceiveDataRuleSetResourceImpl(final Provider<ReceiveDataRuleSetService> ruleSetServiceProvider,
                                   final Provider<DocumentResourceHelper> documentResourceHelperProvider) {
        this.ruleSetServiceProvider = ruleSetServiceProvider;
        this.documentResourceHelperProvider = documentResourceHelperProvider;
    }

    @Override
    public ReceiveDataRules fetch(final String uuid) {
        return documentResourceHelperProvider.get().read(ruleSetServiceProvider.get(), getDocRef(uuid));
    }

    @Override
    public ReceiveDataRules update(final String uuid, final ReceiveDataRules doc) {
        if (doc.getUuid() == null || !doc.getUuid().equals(uuid)) {
            throw new EntityServiceException("The document UUID must match the update UUID");
        }
        return documentResourceHelperProvider.get().update(ruleSetServiceProvider.get(), doc);
    }

    private DocRef getDocRef(final String uuid) {
        return DocRef.builder()
                .uuid(uuid)
                .type(ReceiveDataRules.DOCUMENT_TYPE)
                .build();
    }

    @Override
    public Set<DocRef> listDocuments() {
        return ruleSetServiceProvider.get().listDocuments();
    }

    @Override
    public DocRef importDocument(final Base64EncodedDocumentData encodedDocumentData) {
        final DocumentData documentData = DocumentData.fromBase64EncodedDocumentData(encodedDocumentData);
        final ImportState importState = new ImportState(documentData.getDocRef(),
                documentData.getDocRef().getName());
        final ImportExportActionHandler.ImpexDetails result = ruleSetServiceProvider.get().importDocument(
                documentData.getDocRef(),
                documentData.getDataMap(),
                importState,
                ImportMode.IGNORE_CONFIRMATION);
        if (result != null) {
            return result.getDocRef();
        } else {
            return null;
        }
    }

    @Override
    public Base64EncodedDocumentData exportDocument(final DocRef docRef) {
        final Map<String, byte[]> map = ruleSetServiceProvider.get().exportDocument(
                docRef,
                true,
                new ArrayList<>());
        return DocumentData.toBase64EncodedDocumentData(new DocumentData(docRef, map));
    }
}<commit_msg>Applying Stroom standards for REST to ReceiveDataRuleSetResourceImpl<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 stroom.receive.rules.impl;

import stroom.docref.DocRef;
import stroom.docstore.api.DocumentResourceHelper;
import stroom.event.logging.rs.api.AutoLogged;
import stroom.event.logging.rs.api.AutoLogged.OperationType;
import stroom.importexport.api.DocumentData;
import stroom.importexport.api.ImportExportActionHandler;
import stroom.importexport.shared.Base64EncodedDocumentData;
import stroom.importexport.shared.ImportState;
import stroom.importexport.shared.ImportState.ImportMode;
import stroom.receive.rules.shared.ReceiveDataRuleSetResource;
import stroom.receive.rules.shared.ReceiveDataRules;
import stroom.util.shared.EntityServiceException;

import java.util.ArrayList;
import java.util.Map;
import java.util.Set;
import javax.inject.Inject;
import javax.inject.Provider;

@AutoLogged
public class ReceiveDataRuleSetResourceImpl implements ReceiveDataRuleSetResource {

    private final Provider<ReceiveDataRuleSetService> ruleSetServiceProvider;
    private final Provider<DocumentResourceHelper> documentResourceHelperProvider;

    @Inject
    ReceiveDataRuleSetResourceImpl(final Provider<ReceiveDataRuleSetService> ruleSetServiceProvider,
                                   final Provider<DocumentResourceHelper> documentResourceHelperProvider) {
        this.ruleSetServiceProvider = ruleSetServiceProvider;
        this.documentResourceHelperProvider = documentResourceHelperProvider;
    }

    @Override
    public ReceiveDataRules fetch(final String uuid) {
        return documentResourceHelperProvider.get().read(ruleSetServiceProvider.get(), getDocRef(uuid));
    }

    @Override
    public ReceiveDataRules update(final String uuid, final ReceiveDataRules doc) {
        if (doc.getUuid() == null || !doc.getUuid().equals(uuid)) {
            throw new EntityServiceException("The document UUID must match the update UUID");
        }
        return documentResourceHelperProvider.get().update(ruleSetServiceProvider.get(), doc);
    }

    private DocRef getDocRef(final String uuid) {
        return DocRef.builder()
                .uuid(uuid)
                .type(ReceiveDataRules.DOCUMENT_TYPE)
                .build();
    }

    @Override
    @AutoLogged(OperationType.VIEW)
    public Set<DocRef> listDocuments() {
        return ruleSetServiceProvider.get().listDocuments();
    }

    @Override
    @AutoLogged(value = OperationType.IMPORT, verb = "Importing data for ruleset")
    public DocRef importDocument(final Base64EncodedDocumentData encodedDocumentData) {
        final DocumentData documentData = DocumentData.fromBase64EncodedDocumentData(encodedDocumentData);
        final ImportState importState = new ImportState(documentData.getDocRef(),
                documentData.getDocRef().getName());
        final ImportExportActionHandler.ImpexDetails result = ruleSetServiceProvider.get().importDocument(
                documentData.getDocRef(),
                documentData.getDataMap(),
                importState,
                ImportMode.IGNORE_CONFIRMATION);
        if (result != null) {
            return result.getDocRef();
        } else {
            return null;
        }
    }

    @Override
    @AutoLogged(value = OperationType.EXPORT, verb = "Exporting data for ruleset")
    public Base64EncodedDocumentData exportDocument(final DocRef docRef) {
        final Map<String, byte[]> map = ruleSetServiceProvider.get().exportDocument(
                docRef,
                true,
                new ArrayList<>());
        return DocumentData.toBase64EncodedDocumentData(new DocumentData(docRef, map));
    }
}
{'fraction_non_alphanumeric': 0.07942149773256527, 'fraction_numerical': 0.004412305429586959, 'mean_word_length': 3.5459610027855155, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2377108', 'n_tokens_mistral': 2121, 'n_tokens_neox': 2034, 'n_words': 508}