<commit_before>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see http://www.gnu.org/licenses/
 */
package org.phenotips.matchingnotification.script;

import org.phenotips.matchingnotification.MatchingNotificationManager;
import org.phenotips.matchingnotification.export.PatientMatchExport;
import org.phenotips.matchingnotification.match.PatientMatch;
import org.phenotips.matchingnotification.notification.PatientMatchNotificationResponse;
import org.phenotips.matchingnotification.storage.MatchStorageManager;
import org.phenotips.security.authorization.AuthorizationService;

import org.xwiki.component.annotation.Component;
import org.xwiki.model.EntityType;
import org.xwiki.model.reference.EntityReferenceResolver;
import org.xwiki.script.service.ScriptService;
import org.xwiki.security.authorization.Right;
import org.xwiki.users.UserManager;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @version $Id$
 */
@Component
@Named("matchingNotification")
@Singleton
public class MatchingNotificationScriptService implements ScriptService
{
    /** key for matches ids array in JSON. */
    public static final String IDS_STRING = "ids";

    @Inject
    private PatientMatchExport patientMatchExport;

    @Inject
    private MatchingNotificationManager matchingNotificationManager;

    @Inject
    private MatchStorageManager matchStorageManager;

    @Inject
    private AuthorizationService auth;

    @Inject
    private UserManager users;

    @Inject
    private EntityReferenceResolver<String> resolver;

    private Logger logger = LoggerFactory.getLogger(MatchingNotificationScriptService.class);

    /**
     * Find patient matches and saves only those with score higher or equals to {@code score}.
     *
     * @param score only matches with score higher or equal to this value are saved
     * @return JSON with the saved matches (after filtering)
     */
    public String findAndSaveMatches(double score)
    {
        if (!isAdmin()) {
            return "";
        }
        List<PatientMatch> matches = this.matchingNotificationManager.findAndSaveMatches(score);
        JSONObject json = this.patientMatchExport.toJSON(matches);
        return json.toString();
    }

    /**
     * Returns a JSON object containing all matches from database filtered by parameters.
     *
     * @param score only matches with score higher or equal to this value are returned
     * @param notified whether the matches were notified of
     * @return a JSON object with a list of matches
     */
    public String getMatches(double score, boolean notified)
    {
        if (!isAdmin()) {
            return "";
        }
        List<PatientMatch> matches = this.matchStorageManager.loadMatches(score, notified);
        filterPatientsFromMatches(matches);
        JSONObject json = this.patientMatchExport.toJSON(matches);
        return json.toString();
    }

    /**
     * Marks matches, with ids given in parameter, as saved, rejected or uncategorized. Example:
     *
     * <pre>
     * Input: ["1", "2", "3"], "saved"
     * Output: {"results": [{"id": "1", "success": true}, {"id": "2", "success": false}, {"id": "3", "success": true}]}
     * </pre>
     *
     * @param ids JSON with list of ids of matches to set status
     * @param status whether the matches should be set as saved, rejected or uncategorized
     * @return result JSON
     */
    public String setStatus(String ids, String status)
    {
        if (!isAdmin()) {
            return "";
        }
        List<Long> idsList = this.jsonToIdsList(ids);
        this.matchingNotificationManager.setStatus(idsList, status);
        return this.successfulIdsToJSON(idsList, idsList).toString();
    }<commit_msg>PN-161: Notifications does not handle contacts for remote matches correctly Don't ignore the status of the wrapped method call when reporting the outcome of $services.matchingNotification.setStatus<commit_after>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see http://www.gnu.org/licenses/
 */
package org.phenotips.matchingnotification.script;

import org.phenotips.matchingnotification.MatchingNotificationManager;
import org.phenotips.matchingnotification.export.PatientMatchExport;
import org.phenotips.matchingnotification.match.PatientMatch;
import org.phenotips.matchingnotification.notification.PatientMatchNotificationResponse;
import org.phenotips.matchingnotification.storage.MatchStorageManager;
import org.phenotips.security.authorization.AuthorizationService;

import org.xwiki.component.annotation.Component;
import org.xwiki.model.EntityType;
import org.xwiki.model.reference.EntityReferenceResolver;
import org.xwiki.script.service.ScriptService;
import org.xwiki.security.authorization.Right;
import org.xwiki.users.UserManager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @version $Id$
 */
@Component
@Named("matchingNotification")
@Singleton
public class MatchingNotificationScriptService implements ScriptService
{
    /** key for matches ids array in JSON. */
    public static final String IDS_STRING = "ids";

    @Inject
    private PatientMatchExport patientMatchExport;

    @Inject
    private MatchingNotificationManager matchingNotificationManager;

    @Inject
    private MatchStorageManager matchStorageManager;

    @Inject
    private AuthorizationService auth;

    @Inject
    private UserManager users;

    @Inject
    private EntityReferenceResolver<String> resolver;

    private Logger logger = LoggerFactory.getLogger(MatchingNotificationScriptService.class);

    /**
     * Find patient matches and saves only those with score higher or equals to {@code score}.
     *
     * @param score only matches with score higher or equal to this value are saved
     * @return JSON with the saved matches (after filtering)
     */
    public String findAndSaveMatches(double score)
    {
        if (!isAdmin()) {
            return "";
        }
        List<PatientMatch> matches = this.matchingNotificationManager.findAndSaveMatches(score);
        JSONObject json = this.patientMatchExport.toJSON(matches);
        return json.toString();
    }

    /**
     * Returns a JSON object containing all matches from database filtered by parameters.
     *
     * @param score only matches with score higher or equal to this value are returned
     * @param notified whether the matches were notified of
     * @return a JSON object with a list of matches
     */
    public String getMatches(double score, boolean notified)
    {
        if (!isAdmin()) {
            return "";
        }
        List<PatientMatch> matches = this.matchStorageManager.loadMatches(score, notified);
        filterPatientsFromMatches(matches);
        JSONObject json = this.patientMatchExport.toJSON(matches);
        return json.toString();
    }

    /**
     * Marks matches, with ids given in parameter, as saved, rejected or uncategorized. Example:
     *
     * <pre>
     * Input: ["1", "2", "3"], "saved"
     * Output: {"results": [{"id": "1", "success": true}, {"id": "2", "success": false}, {"id": "3", "success": true}]}
     * </pre>
     *
     * @param ids JSON with list of ids of matches to set status
     * @param status whether the matches should be set as saved, rejected or uncategorized
     * @return result JSON
     */
    public String setStatus(String ids, String status)
    {
        if (!isAdmin()) {
            return "";
        }
        List<Long> idsList = this.jsonToIdsList(ids);
        boolean success = this.matchingNotificationManager.setStatus(idsList, status);
        return this.successfulIdsToJSON(idsList, success ? idsList : Collections.<Long>emptyList()).toString();
    }
{'fraction_non_alphanumeric': 0.08648593971272507, 'fraction_numerical': 0.0025288286465709083, 'mean_word_length': 3.721585482330468, 'pattern_counts': {'":': 14, '<': 16, '<?xml version=': 0, '>': 16, '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': '1572275', 'n_tokens_mistral': 2595, 'n_tokens_neox': 2531, 'n_words': 860}