<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.data.internal;

import org.phenotips.Constants;
import org.phenotips.data.Patient;
import org.phenotips.data.events.PatientChangingEvent;

import org.xwiki.component.annotation.Component;
import org.xwiki.container.Container;
import org.xwiki.container.servlet.ServletRequest;
import org.xwiki.context.Execution;
import org.xwiki.model.EntityType;
import org.xwiki.model.reference.EntityReference;
import org.xwiki.observation.AbstractEventListener;
import org.xwiki.observation.event.Event;

import java.text.MessageFormat;
import java.util.Arrays;
import java.util.List;

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

import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.doc.XWikiDocument;
import com.xpn.xwiki.objects.BaseObject;
import com.xpn.xwiki.objects.classes.BaseClass;

/**
 * Store the target category specified for free text phenotypes.
 *
 * @version $Id$
 */
@Component
@Named("phenotype-category-updater")
@Singleton
public class NonStandardFeatureCategoryUpdater extends AbstractEventListener
{
    /** The name of the class where the mapping between phenotypes and categories is stored. */
    private static final EntityReference CATEGORY_CLASS_REFERENCE = new EntityReference("PhenotypeCategoryClass",
        EntityType.DOCUMENT, Constants.CODE_SPACE_REFERENCE);

    /** The name of the mapping class property where the phenotype property name is stored. */
    private static final String NAME_PROPETY_NAME = "target_property_name";

    /** The name of the mapping class property where the phenotype value is stored. */
    private static final String VALUE_PROPETY_NAME = "target_property_value";

    /** The name of the mapping class property where the target category is stored. */
    private static final String CATEGORY_PROPETY_NAME = "target_property_category";

    @Inject
    private Execution execution;

    /** Needed for getting access to the request. */
    @Inject
    private Container container;

    /** Default constructor, sets up the listener name and the list of events to subscribe to. */
    public NonStandardFeatureCategoryUpdater()
    {
        super("phenotype-category-updater", new PatientChangingEvent());
    }

    @Override
    public void onEvent(Event event, Object source, Object data)
    {
        if (this.container.getRequest() == null) {
            // Not a browser request, no custom categories, nothing to do
            return;
        }
        XWikiContext context = (XWikiContext) this.execution.getContext().getProperty("xwikicontext");
        XWikiDocument doc = (XWikiDocument) source;

        BaseObject patientRecordObj = doc.getXObject(Patient.CLASS_REFERENCE);
        if (patientRecordObj == null) {
            return;
        }
        BaseClass patientRecordClass = patientRecordObj.getXClass(context);

        for (String targetPropertyName : patientRecordClass.getPropertyList()) {
            if (!targetPropertyName.contains("phenotype")) {
                continue;
            }
            @SuppressWarnings("unchecked")
            List<String> phenotypes = patientRecordObj.getListValue(targetPropertyName);
            for (String phenotype : phenotypes) {
                if (!phenotype.matches("HP:[0-9]+")) {
                    List<String> category =
                        getParameter(targetPropertyName + "__" + phenotype.replaceAll("[^a-zA-Z0-9_]+", "_")
                            + "__category", patientRecordObj.getNumber());
                    if (category != null && !category.isEmpty()) {
                        storeCategory(phenotype, category, targetPropertyName, doc, context);
                    }
                }
            }
        }
    }<commit_msg>PT-2530: Free-text phenotype categories are not saved/created Fixed<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.data.internal;

import org.phenotips.Constants;
import org.phenotips.data.Patient;
import org.phenotips.data.events.PatientChangingEvent;

import org.xwiki.component.annotation.Component;
import org.xwiki.container.Container;
import org.xwiki.container.servlet.ServletRequest;
import org.xwiki.context.Execution;
import org.xwiki.model.EntityType;
import org.xwiki.model.reference.EntityReference;
import org.xwiki.observation.AbstractEventListener;
import org.xwiki.observation.event.Event;

import java.text.MessageFormat;
import java.util.Arrays;
import java.util.List;

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

import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.doc.XWikiDocument;
import com.xpn.xwiki.objects.BaseObject;
import com.xpn.xwiki.objects.classes.BaseClass;

/**
 * Store the target category specified for free text phenotypes.
 *
 * @version $Id$
 */
@Component
@Named("phenotype-category-updater")
@Singleton
public class NonStandardFeatureCategoryUpdater extends AbstractEventListener
{
    /** The name of the class where the mapping between phenotypes and categories is stored. */
    private static final EntityReference CATEGORY_CLASS_REFERENCE = new EntityReference("PhenotypeCategoryClass",
        EntityType.DOCUMENT, Constants.CODE_SPACE_REFERENCE);

    /** The name of the mapping class property where the phenotype property name is stored. */
    private static final String NAME_PROPETY_NAME = "target_property_name";

    /** The name of the mapping class property where the phenotype value is stored. */
    private static final String VALUE_PROPETY_NAME = "target_property_value";

    /** The name of the mapping class property where the target category is stored. */
    private static final String CATEGORY_PROPETY_NAME = "target_property_category";

    @Inject
    private Execution execution;

    /** Needed for getting access to the request. */
    @Inject
    private Container container;

    /** Default constructor, sets up the listener name and the list of events to subscribe to. */
    public NonStandardFeatureCategoryUpdater()
    {
        super("phenotype-category-updater", new PatientChangingEvent());
    }

    @Override
    public void onEvent(Event event, Object source, Object data)
    {
        if (this.container.getRequest() == null) {
            // Not a browser request, no custom categories, nothing to do
            return;
        }
        XWikiContext context = (XWikiContext) this.execution.getContext().getProperty("xwikicontext");
        XWikiDocument doc = (XWikiDocument) source;

        BaseObject patientRecordObj = doc.getXObject(Patient.CLASS_REFERENCE);
        if (patientRecordObj == null) {
            return;
        }
        BaseClass patientRecordClass = patientRecordObj.getXClass(context);

        for (String targetPropertyName : patientRecordClass.getPropertyList()) {
            if (!targetPropertyName.contains("phenotype")) {
                continue;
            }
            @SuppressWarnings("unchecked")
            List<String> phenotypes = patientRecordObj.getListValue(targetPropertyName);
            for (String phenotype : phenotypes) {
                if (!phenotype.matches("HP:[0-9]+")) {
                    List<String> category =
                        getParameter(targetPropertyName + "__suggested__" + phenotype.replaceAll("[^a-zA-Z0-9_]+", "_")
                            + "__category", patientRecordObj.getNumber());
                    if (category != null && !category.isEmpty()) {
                        storeCategory(phenotype, category, targetPropertyName, doc, context);
                    }
                }
            }
        }
    }
{'fraction_non_alphanumeric': 0.08595864458157411, 'fraction_numerical': 0.0015156436072317851, 'mean_word_length': 3.2317911131470454, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '7397520', 'n_tokens_mistral': 2450, 'n_tokens_neox': 2377, 'n_words': 750}