<commit_before>/*******************************************************************************
 * Copyright SemanticBits, Northwestern University and Akaza Research
 * 
 * Distributed under the OSI-approved BSD 3-Clause License.
 * See http://ncip.github.com/caaers/LICENSE.txt for details.
 ******************************************************************************/
package gov.nih.nci.cabig.report2caaers;

import gov.nih.nci.cabig.caaers2adeers.Caaers2AdeersRouteBuilder;

import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang.math.RandomUtils;

import static gov.nih.nci.cabig.caaers2adeers.exchnage.ExchangePreProcessor.*;
import static gov.nih.nci.cabig.caaers2adeers.track.IntegrationLog.Stage.*;
import static gov.nih.nci.cabig.caaers2adeers.track.Tracker.track;

public class ToCaaersReportWSRouteBuilder {

	private static String caAERSSafetyReportJBIURL = "jbi:service:http://schema.integration.caaers.cabig.nci.nih.gov/aereport/SafetyReportManagementService?operation={http://schema.integration.caaers.cabig.nci.nih.gov/aereport}";
	
	private static String requestXSLBase = "xslt/e2b/request/";
	private static String responseXSLBase = "xslt/e2b/response/";
	
	private static String[] msgComboIdPaths = { "//safetyreportid", "//messagedate"};
	
	private String inputEDIDir;
	private String outputEDIDir;	
		
	private Caaers2AdeersRouteBuilder routeBuilder;

	public void configure(Caaers2AdeersRouteBuilder rb){
        this.routeBuilder = rb;
        
        Map<String, String> nss = new HashMap<String, String>();
        nss.put("svrl", "http://purl.oclc.org/dsdl/svrl");
        
        routeBuilder.from("file://"+inputEDIDir+"?preMove=inprogress&move=done&moveFailed=movefailed")
            .streamCaching()
            .setProperty(CORRELATION_ID, rb.constant(String.valueOf(System.currentTimeMillis()) + RandomUtils.randomAlphaNumberic(3)))
            .setProperty(SYNC_HEADER, rb.constant("sync"))
            .setProperty(ENTITY_NAME, rb.constant("SafetyReport"))
            .setProperty(OPERATION_NAME, rb.constant("submitSafetyReport"))
            .processRef("headerGeneratorProcessor")
            .process(track(REQUEST_RECEIVED))
                .to(rb.getFileTracker().fileURI(REQUEST_RECEIVED))
        	.to("log:gov.nih.nci.cabig.report2caaers.caaers-ws-request?showHeaders=true&level=TRACE")
            .processRef("removeEDIHeadersAndFootersProcessor")
            .process(track(E2B_SUBMISSION_REQUEST_RECEIVED, msgComboIdPaths))
                .to(routeBuilder.getFileTracker().fileURI(E2B_SUBMISSION_REQUEST_RECEIVED))
            .processRef("eDIMessagePreProcessor")
            .process(track(PRE_PROCESS_EDI_MSG))
                .to(routeBuilder.getFileTracker().fileURI(PRE_PROCESS_EDI_MSG))
			.to("direct:performSchematronValidation");
        
        //perform schematron validation
        routeBuilder.from("direct:performSchematronValidation")                
			.to("xslt:" + requestXSLBase + "safetyreport_e2b_schematron.xsl?transformerFactoryClass=net.sf.saxon.TransformerFactoryImpl") //for XSLT2.0 support
            .processRef("headerGeneratorProcessor")
            .process(track(E2B_SCHEMATRON_VALIDATION))
			    .to(routeBuilder.getFileTracker().fileURI(E2B_SCHEMATRON_VALIDATION))
			.choice()
                .when().xpath("//svrl:failed-assert", nss) 
                	.to("xslt:" + responseXSLBase + "extract-failures.xsl")
                	.to("xslt:" + responseXSLBase + "E2BSchematronErrors2ACK.xsl")
                	.to("direct:sendE2BAckSink")
                .otherwise()
                	.to("direct:processE2B");

        routeBuilder.from("direct:processE2B")
        	.to("log:gov.nih.nci.cabig.report2caaers.post-validation?showHeaders=true&level=TRACE")
        	.processRef("resetOriginalMessageProcessor")
            .processRef("headerGeneratorProcessor")
            .process(track(ROUTED_TO_CAAERS_WS_INVOCATION_CHANNEL))
            .to("direct:caaers-reportSubmit-sync");


        //caAERS - submitsafety route
        configureWSCallRoute("direct:caaers-reportSubmit-sync", "safetyreport_e2b_sync.xsl", caAERSSafetyReportJBIURL + "submitSafetyReport" );


        nss = new HashMap<String, String>();
        nss.put("soap", "http://schemas.xmlsoap.org/soap/envelope/");
        nss.put("ns1", "http://schema.integration.caaers.cabig.nci.nih.gov/aereport");
        nss.put("ns3", "http://schema.integration.caaers.cabig.nci.nih.gov/common");

        //content based router
        //if it is saveSafetyReportResponse, then E2B ack will not be sent
        //also, if submit safety report is processed successfully or successfully submitted to AdEERS, then E2B ack will not be sent
        routeBuilder.from("direct:processedE2BMessageSink")
			.to("log:gov.nih.nci.cabig.report2caaers.caaers-ws-request?showHeaders=true&level=WARN")
			.choice()
                .when().xpath("/ichicsrack/acknowledgment/messageacknowledgment/parsingerrormessage", nss)
                	.to("direct:sendE2BAckSink");

        routeBuilder.from("direct:sendE2BAckSink")
			.processRef("addEDIHeadersAndFootersProcessor")
			.process(track(REQUST_PROCESSING_ERROR))
			.to(routeBuilder.getFileTracker().fileURI(REQUST_PROCESSING_ERROR))
			.to("file://"+outputEDIDir);
	}
	

	private void configureWSCallRoute(String fromSink, String xslFileName, String serviceURI){
		this.routeBuilder.configureWSCallRoute(fromSink, 
				requestXSLBase + xslFileName, 
				serviceURI, 
				responseXSLBase + xslFileName, 
				"direct:processedE2BMessageSink", 
				CAAERS_WS_IN_TRANSFORMATION, CAAERS_WS_INVOCATION_INITIATED, CAAERS_WS_INVOCATION_COMPLETED, CAAERS_WS_OUT_TRANSFORMATION, ROUTED_TO_CAAERS_RESPONSE_SINK);
	}


	public String getInputEDIDir() {
		return inputEDIDir;
	}


	public void setInputEDIDir(String inputEDIDir) {
		this.inputEDIDir = inputEDIDir;
	}


	public String getOutputEDIDir() {
		return outputEDIDir;
	}


	public void setOutputEDIDir(String outputEDIDir) {
		this.outputEDIDir = outputEDIDir;
	}
	
	
	
}<commit_msg>CAAERS-7103, add function to generate padding data.<commit_after>/*******************************************************************************
 * Copyright SemanticBits, Northwestern University and Akaza Research
 * 
 * Distributed under the OSI-approved BSD 3-Clause License.
 * See http://ncip.github.com/caaers/LICENSE.txt for details.
 ******************************************************************************/
package gov.nih.nci.cabig.report2caaers;

import gov.nih.nci.cabig.caaers2adeers.Caaers2AdeersRouteBuilder;

import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang.math.RandomUtils;

import static gov.nih.nci.cabig.caaers2adeers.exchnage.ExchangePreProcessor.*;
import static gov.nih.nci.cabig.caaers2adeers.track.IntegrationLog.Stage.*;
import static gov.nih.nci.cabig.caaers2adeers.track.Tracker.track;

public class ToCaaersReportWSRouteBuilder {

	private static String caAERSSafetyReportJBIURL = "jbi:service:http://schema.integration.caaers.cabig.nci.nih.gov/aereport/SafetyReportManagementService?operation={http://schema.integration.caaers.cabig.nci.nih.gov/aereport}";
	
	private static String requestXSLBase = "xslt/e2b/request/";
	private static String responseXSLBase = "xslt/e2b/response/";
	
	private static String[] msgComboIdPaths = { "//safetyreportid", "//messagedate"};
	
	private String inputEDIDir;
	private String outputEDIDir;	
		
	private Caaers2AdeersRouteBuilder routeBuilder;

	public void configure(Caaers2AdeersRouteBuilder rb){
        this.routeBuilder = rb;
        
        Map<String, String> nss = new HashMap<String, String>();
        nss.put("svrl", "http://purl.oclc.org/dsdl/svrl");
        
        routeBuilder.from("file://"+inputEDIDir+"?preMove=inprogress&move=done&moveFailed=movefailed")
            .streamCaching()
            .setProperty(CORRELATION_ID, rb.constant(String.valueOf(System.currentTimeMillis()) + randomAlphaNumberic(3)))
            .setProperty(SYNC_HEADER, rb.constant("sync"))
            .setProperty(ENTITY_NAME, rb.constant("SafetyReport"))
            .setProperty(OPERATION_NAME, rb.constant("submitSafetyReport"))
            .processRef("headerGeneratorProcessor")
            .process(track(REQUEST_RECEIVED))
                .to(rb.getFileTracker().fileURI(REQUEST_RECEIVED))
        	.to("log:gov.nih.nci.cabig.report2caaers.caaers-ws-request?showHeaders=true&level=TRACE")
            .processRef("removeEDIHeadersAndFootersProcessor")
            .process(track(E2B_SUBMISSION_REQUEST_RECEIVED, msgComboIdPaths))
                .to(routeBuilder.getFileTracker().fileURI(E2B_SUBMISSION_REQUEST_RECEIVED))
            .processRef("eDIMessagePreProcessor")
            .process(track(PRE_PROCESS_EDI_MSG))
                .to(routeBuilder.getFileTracker().fileURI(PRE_PROCESS_EDI_MSG))
			.to("direct:performSchematronValidation");
        
        //perform schematron validation
        routeBuilder.from("direct:performSchematronValidation")                
			.to("xslt:" + requestXSLBase + "safetyreport_e2b_schematron.xsl?transformerFactoryClass=net.sf.saxon.TransformerFactoryImpl") //for XSLT2.0 support
            .processRef("headerGeneratorProcessor")
            .process(track(E2B_SCHEMATRON_VALIDATION))
			    .to(routeBuilder.getFileTracker().fileURI(E2B_SCHEMATRON_VALIDATION))
			.choice()
                .when().xpath("//svrl:failed-assert", nss) 
                	.to("xslt:" + responseXSLBase + "extract-failures.xsl")
                	.to("xslt:" + responseXSLBase + "E2BSchematronErrors2ACK.xsl")
                	.to("direct:sendE2BAckSink")
                .otherwise()
                	.to("direct:processE2B");

        routeBuilder.from("direct:processE2B")
        	.to("log:gov.nih.nci.cabig.report2caaers.post-validation?showHeaders=true&level=TRACE")
        	.processRef("resetOriginalMessageProcessor")
            .processRef("headerGeneratorProcessor")
            .process(track(ROUTED_TO_CAAERS_WS_INVOCATION_CHANNEL))
            .to("direct:caaers-reportSubmit-sync");


        //caAERS - submitsafety route
        configureWSCallRoute("direct:caaers-reportSubmit-sync", "safetyreport_e2b_sync.xsl", caAERSSafetyReportJBIURL + "submitSafetyReport" );


        nss = new HashMap<String, String>();
        nss.put("soap", "http://schemas.xmlsoap.org/soap/envelope/");
        nss.put("ns1", "http://schema.integration.caaers.cabig.nci.nih.gov/aereport");
        nss.put("ns3", "http://schema.integration.caaers.cabig.nci.nih.gov/common");

        //content based router
        //if it is saveSafetyReportResponse, then E2B ack will not be sent
        //also, if submit safety report is processed successfully or successfully submitted to AdEERS, then E2B ack will not be sent
        routeBuilder.from("direct:processedE2BMessageSink")
			.to("log:gov.nih.nci.cabig.report2caaers.caaers-ws-request?showHeaders=true&level=WARN")
			.choice()
                .when().xpath("/ichicsrack/acknowledgment/messageacknowledgment/parsingerrormessage", nss)
                	.to("direct:sendE2BAckSink");

        routeBuilder.from("direct:sendE2BAckSink")
			.processRef("addEDIHeadersAndFootersProcessor")
			.process(track(REQUST_PROCESSING_ERROR))
			.to(routeBuilder.getFileTracker().fileURI(REQUST_PROCESSING_ERROR))
			.to("file://"+outputEDIDir);
	}
	

	private void configureWSCallRoute(String fromSink, String xslFileName, String serviceURI){
		this.routeBuilder.configureWSCallRoute(fromSink, 
				requestXSLBase + xslFileName, 
				serviceURI, 
				responseXSLBase + xslFileName, 
				"direct:processedE2BMessageSink", 
				CAAERS_WS_IN_TRANSFORMATION, CAAERS_WS_INVOCATION_INITIATED, CAAERS_WS_INVOCATION_COMPLETED, CAAERS_WS_OUT_TRANSFORMATION, ROUTED_TO_CAAERS_RESPONSE_SINK);
	}


	public String getInputEDIDir() {
		return inputEDIDir;
	}


	public void setInputEDIDir(String inputEDIDir) {
		this.inputEDIDir = inputEDIDir;
	}


	public String getOutputEDIDir() {
		return outputEDIDir;
	}


	public void setOutputEDIDir(String outputEDIDir) {
		this.outputEDIDir = outputEDIDir;
	}
	
	private String randomAlphaNumberic(int length) {
		String str ="";
		for (int i =0; i < length; i++) {
			str += Character.toChars(RandomUtils.nextInt(25) + 65);
		}
		return str;
	}
	
}
{'fraction_non_alphanumeric': 0.13926829268292684, 'fraction_numerical': 0.006585365853658536, 'mean_word_length': 3.8448207955888143, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '1122955', 'n_tokens_mistral': 4195, 'n_tokens_neox': 3953, 'n_words': 510}