<commit_before>/*
 * This code is subject to the HIEOS License, Version 1.0
 *
 * Copyright(c) 2010 Vangent, Inc.  All rights reserved.
 *
 * 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 com.vangent.hieos.services.xcpd.gateway.controller;

import com.vangent.hieos.hl7v3util.model.message.PRPA_IN201305UV02_Message;
import com.vangent.hieos.hl7v3util.model.message.PRPA_IN201305UV02_Message_Builder;
import com.vangent.hieos.hl7v3util.model.message.PRPA_IN201306UV02_Message;
import com.vangent.hieos.hl7v3util.model.subject.Custodian;
import com.vangent.hieos.hl7v3util.model.subject.DeviceInfo;
import com.vangent.hieos.hl7v3util.model.subject.Subject;
import com.vangent.hieos.hl7v3util.model.subject.SubjectBuilder;
import com.vangent.hieos.hl7v3util.model.subject.SubjectIdentifier;
import com.vangent.hieos.hl7v3util.model.subject.SubjectIdentifierDomain;
import com.vangent.hieos.hl7v3util.model.subject.SubjectSearchCriteria;
import com.vangent.hieos.hl7v3util.model.subject.SubjectSearchResponse;
import com.vangent.hieos.hl7v3util.model.exception.ModelBuilderException;

import com.vangent.hieos.services.xcpd.gateway.framework.XCPDGatewayRequestHandler;
import com.vangent.hieos.services.xcpd.gateway.exception.XCPDException;

import com.vangent.hieos.services.xcpd.patientcorrelationcache.exception.PatientCorrelationCacheException;
import com.vangent.hieos.services.xcpd.patientcorrelationcache.model.PatientCorrelationCacheEntry;
import com.vangent.hieos.services.xcpd.patientcorrelationcache.service.PatientCorrelationCacheService;

import com.vangent.hieos.xutil.xconfig.XConfig;
import com.vangent.hieos.xutil.xconfig.XConfigActor;
import com.vangent.hieos.xutil.xconfig.XConfigObject;
import com.vangent.hieos.xutil.xlog.client.XLogMessage;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.apache.axis2.AxisFault;
import org.apache.log4j.Logger;

/**
 *
 * @author <NAME>
 */
public class XCPDGatewayRequestController {

    private final static Logger logger = Logger.getLogger(XCPDGatewayRequestController.class);

    // Result mode.
    public enum SearchResultMode {

        Identifiers,
        Demographics  // Used to get demographics from external gateways (since this are not cached).
    };
    // Statics.
    private static List<XConfigActor> _xcpdRespondingGateways = null;
    private static ExecutorService _executor = null;  // Only one of these shared across all web requests.
    private SearchResultMode searchResultMode = SearchResultMode.Identifiers;
    private XLogMessage logMessage;
    private XCPDGatewayRequestHandler requestHandler;
    private SubjectIdentifier communitySubjectIdentifier = null;
    private PatientCorrelationCacheService cacheService = null;
    private List<XConfigActor> targetRespondingGateways = new ArrayList<XConfigActor>();
    private List<PatientCorrelationCacheEntry> activeCacheEntries = new ArrayList<PatientCorrelationCacheEntry>();

    /**
     * 
     * @param logMessage
     */
    public XCPDGatewayRequestController(SearchResultMode searchResultMode, XCPDGatewayRequestHandler requestHandler, XLogMessage logMessage) {
        this.searchResultMode = searchResultMode;
        this.requestHandler = requestHandler;
        this.logMessage = logMessage;
        this.initCacheService();
    }

    /**
     *
     */
    private void initCacheService() {

        // Get configuration.
        logger.info("Setting MatchCacheExpirationDays from xconfig");
        String matchCacheExpirationDaysText = requestHandler.getGatewayConfigProperty("MatchCacheExpirationDays");
        int matchCacheExpirationDays = PatientCorrelationCacheService.DEFAULT_MATCH_EXPIRATION_DAYS;
        if (matchCacheExpirationDaysText != null) {
            matchCacheExpirationDays = new Integer(matchCacheExpirationDaysText);
        }
        logger.info("Setting MoMatchCacheExpirationDays from xconfig");
        String noMatchCacheExpirationDaysText = requestHandler.getGatewayConfigProperty("NoMatchCacheExpirationDays");
        int noMatchCacheExpirationDays = PatientCorrelationCacheService.DEFAULT_NO_MATCH_EXPIRATION_DAYS;
        if (noMatchCacheExpirationDaysText != null) {
            matchCacheExpirationDays = new Integer(noMatchCacheExpirationDaysText);
        }
        // Create cache service instance.
        this.cacheService = new PatientCorrelationCacheService(matchCacheExpirationDays, noMatchCacheExpirationDays);
    }

    /**
     *
     * @param subjectSearchCriteria
     * @return
     */
    public void init(SubjectSearchCriteria subjectSearchCriteria) throws XCPDException {
        try {
            // Get subject identifier for the search subject (as it is known in this community).
            communitySubjectIdentifier = this.getCommunitySubjectIdentifier(subjectSearchCriteria);

            // Vitals.
            String localPatientId = communitySubjectIdentifier.getCXFormatted();
            String localHomeCommunityId = requestHandler.getGatewayConfig().getUniqueId();

            // First, get rid of any expired correlations for the localpid.
            cacheService.deleteExpired(localPatientId, localHomeCommunityId);<commit_msg>Added xconfig support for MatchCacheExpirationDays and NoMatchExpirationDays.<commit_after>/*
 * This code is subject to the HIEOS License, Version 1.0
 *
 * Copyright(c) 2010 Vangent, Inc.  All rights reserved.
 *
 * 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 com.vangent.hieos.services.xcpd.gateway.controller;

import com.vangent.hieos.hl7v3util.model.message.PRPA_IN201305UV02_Message;
import com.vangent.hieos.hl7v3util.model.message.PRPA_IN201305UV02_Message_Builder;
import com.vangent.hieos.hl7v3util.model.message.PRPA_IN201306UV02_Message;
import com.vangent.hieos.hl7v3util.model.subject.Custodian;
import com.vangent.hieos.hl7v3util.model.subject.DeviceInfo;
import com.vangent.hieos.hl7v3util.model.subject.Subject;
import com.vangent.hieos.hl7v3util.model.subject.SubjectBuilder;
import com.vangent.hieos.hl7v3util.model.subject.SubjectIdentifier;
import com.vangent.hieos.hl7v3util.model.subject.SubjectIdentifierDomain;
import com.vangent.hieos.hl7v3util.model.subject.SubjectSearchCriteria;
import com.vangent.hieos.hl7v3util.model.subject.SubjectSearchResponse;
import com.vangent.hieos.hl7v3util.model.exception.ModelBuilderException;

import com.vangent.hieos.services.xcpd.gateway.framework.XCPDGatewayRequestHandler;
import com.vangent.hieos.services.xcpd.gateway.exception.XCPDException;

import com.vangent.hieos.services.xcpd.patientcorrelationcache.exception.PatientCorrelationCacheException;
import com.vangent.hieos.services.xcpd.patientcorrelationcache.model.PatientCorrelationCacheEntry;
import com.vangent.hieos.services.xcpd.patientcorrelationcache.service.PatientCorrelationCacheService;

import com.vangent.hieos.xutil.xconfig.XConfig;
import com.vangent.hieos.xutil.xconfig.XConfigActor;
import com.vangent.hieos.xutil.xconfig.XConfigObject;
import com.vangent.hieos.xutil.xlog.client.XLogMessage;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.apache.axis2.AxisFault;
import org.apache.log4j.Logger;

/**
 *
 * @author <NAME>
 */
public class XCPDGatewayRequestController {

    private final static Logger logger = Logger.getLogger(XCPDGatewayRequestController.class);

    // Result mode.
    public enum SearchResultMode {

        Identifiers,
        Demographics  // Used to get demographics from external gateways (since this are not cached).
    };
    // Statics.
    private static List<XConfigActor> _xcpdRespondingGateways = null;
    private static ExecutorService _executor = null;  // Only one of these shared across all web requests.
    private SearchResultMode searchResultMode = SearchResultMode.Identifiers;
    private XLogMessage logMessage;
    private XCPDGatewayRequestHandler requestHandler;
    private SubjectIdentifier communitySubjectIdentifier = null;
    private PatientCorrelationCacheService cacheService = null;
    private List<XConfigActor> targetRespondingGateways = new ArrayList<XConfigActor>();
    private List<PatientCorrelationCacheEntry> activeCacheEntries = new ArrayList<PatientCorrelationCacheEntry>();

    /**
     * 
     * @param logMessage
     */
    public XCPDGatewayRequestController(SearchResultMode searchResultMode, XCPDGatewayRequestHandler requestHandler, XLogMessage logMessage) {
        this.searchResultMode = searchResultMode;
        this.requestHandler = requestHandler;
        this.logMessage = logMessage;
        this.initCacheService();
    }

    /**
     *
     */
    private void initCacheService() {

        // Get configuration.
        logger.info("Setting MatchCacheExpirationDays from xconfig");
        String matchCacheExpirationDaysText = requestHandler.getGatewayConfigProperty("MatchCacheExpirationDays");
        int matchCacheExpirationDays = PatientCorrelationCacheService.DEFAULT_MATCH_EXPIRATION_DAYS;
        if (matchCacheExpirationDaysText != null) {
            matchCacheExpirationDays = new Integer(matchCacheExpirationDaysText);
        }
        logger.info("Setting MoMatchCacheExpirationDays from xconfig");
        String noMatchCacheExpirationDaysText = requestHandler.getGatewayConfigProperty("NoMatchCacheExpirationDays");
        int noMatchCacheExpirationDays = PatientCorrelationCacheService.DEFAULT_NO_MATCH_EXPIRATION_DAYS;
        if (noMatchCacheExpirationDaysText != null) {
            noMatchCacheExpirationDays = new Integer(noMatchCacheExpirationDaysText);
        }
        // Create cache service instance.
        this.cacheService = new PatientCorrelationCacheService(matchCacheExpirationDays, noMatchCacheExpirationDays);
    }

    /**
     *
     * @param subjectSearchCriteria
     * @return
     */
    public void init(SubjectSearchCriteria subjectSearchCriteria) throws XCPDException {
        try {
            // Get subject identifier for the search subject (as it is known in this community).
            communitySubjectIdentifier = this.getCommunitySubjectIdentifier(subjectSearchCriteria);

            // Vitals.
            String localPatientId = communitySubjectIdentifier.getCXFormatted();
            String localHomeCommunityId = requestHandler.getGatewayConfig().getUniqueId();

            // First, get rid of any expired correlations for the localpid.
            cacheService.deleteExpired(localPatientId, localHomeCommunityId);
{'fraction_non_alphanumeric': 0.07664427157001415, 'fraction_numerical': 0.009900990099009901, 'mean_word_length': 4.459942084942085, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '7255130', 'n_tokens_mistral': 3299, 'n_tokens_neox': 3166, 'n_words': 623}