<commit_before>/* global sails */
"use strict";

var request = require('request-promise');
var _ = require('lodash');
var Promise = require("bluebird");

module.exports = {
    attributes: {},
    getDocuments: function (ResearchEntityModel, researchEntityId, query, skipCopiedCheck) {
        var self = this;
        var connector = query.where.connector;
        if (!connector)
            throw new Error('A Connector parameter is necessary');
        return ResearchEntityModel.findOneById(researchEntityId)
            .then(function (researchEntity) {
                var reqConfig;
                switch (connector) {
                    case'Publications':
                        reqConfig = self.getPublicationsConfig(researchEntity, query);
                        break;
                    case'ORCID':
                        reqConfig = self.getOrcidConfig(researchEntity, query);
                        break;
                    case'Scopus':
                        reqConfig = self.getScopusConfig(researchEntity, query);
                        break;
                    default:
                    //sTODO: error management
                }

                return self.makeRequest(reqConfig)
                    .then(res => {
                        /*if (!skipCopiedCheck)
                         return ResearchEntityModel.checkCopiedDocuments(ResearchEntityModel, researchEntityId, res.items)
                         .then(documents => ({items: documents, count: res.count}));*/

                        return res;
                    });
            });
    },
    makeRequest: function (reqConfig) {
        return request.get(reqConfig.reqParams)
            .then(res => {
                const extracted = reqConfig.fieldExtract(res);
                return Promise.all(_.map(extracted.documents, r => reqConfig.transform(r)))
                    .then(documents => ({
                        items: documents,
                        count: extracted.count
                    }));
            });
    },
    getPublicationsConfig: function (researchEntity, configQuery) {
        var researchEntityType = researchEntity.getType();
        var query;
        if (researchEntityType === 'user') {
            var opts = {
                'username': {'author-email': researchEntity.username},
                'surname': {author: researchEntity.surname}
            };

            if (configQuery.where.field in opts)
                query = opts[configQuery.where.field];
            else
                throw "ExternalDocument error: field not selected";
        }
        else {<commit_msg>Readded copied document check.<commit_after>/* global sails */
"use strict";

var request = require('request-promise');
var _ = require('lodash');
var Promise = require("bluebird");

module.exports = {
    attributes: {},
    getDocuments: function (ResearchEntityModel, researchEntityId, query, skipCopiedCheck) {
        var self = this;
        var connector = query.where.connector;
        if (!connector)
            throw new Error('A Connector parameter is necessary');
        return ResearchEntityModel.findOneById(researchEntityId)
            .then(function (researchEntity) {
                var reqConfig;
                switch (connector) {
                    case'Publications':
                        reqConfig = self.getPublicationsConfig(researchEntity, query);
                        break;
                    case'ORCID':
                        reqConfig = self.getOrcidConfig(researchEntity, query);
                        break;
                    case'Scopus':
                        reqConfig = self.getScopusConfig(researchEntity, query);
                        break;
                    default:
                    //sTODO: error management
                }

                return self.makeRequest(reqConfig)
                    .then(res => {
                        if (!skipCopiedCheck)
                         return ResearchEntityModel.checkCopiedDocuments(ResearchEntityModel, researchEntityId, res.items)
                         .then(documents => ({items: documents, count: res.count}));

                        return res;
                    });
            });
    },
    makeRequest: function (reqConfig) {
        return request.get(reqConfig.reqParams)
            .then(res => {
                const extracted = reqConfig.fieldExtract(res);
                return Promise.all(_.map(extracted.documents, r => reqConfig.transform(r)))
                    .then(documents => ({
                        items: documents,
                        count: extracted.count
                    }));
            });
    },
    getPublicationsConfig: function (researchEntity, configQuery) {
        var researchEntityType = researchEntity.getType();
        var query;
        if (researchEntityType === 'user') {
            var opts = {
                'username': {'author-email': researchEntity.username},
                'surname': {author: researchEntity.surname}
            };

            if (configQuery.where.field in opts)
                query = opts[configQuery.where.field];
            else
                throw "ExternalDocument error: field not selected";
        }
        else {
{'fraction_non_alphanumeric': 0.09596819988642817, 'fraction_numerical': 0.0, 'mean_word_length': 1.4912776991984913, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1041796', 'n_tokens_mistral': 1269, 'n_tokens_neox': 1162, 'n_words': 265}