<commit_before>/**
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations under
* the License.
*
* The Original Code is OpenELIS code.
*
* Copyright (C) CIRG, University of Washington, Seattle WA.  All Rights Reserved.
*
*/
package us.mn.state.health.lims.reports.action.implementation;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JasperRunManager;
import org.apache.commons.validator.GenericValidator;
import us.mn.state.health.lims.common.exception.LIMSRuntimeException;
import us.mn.state.health.lims.common.util.ConfigurationProperties;
import us.mn.state.health.lims.common.util.ConfigurationProperties.Property;
import us.mn.state.health.lims.common.util.DateUtil;
import us.mn.state.health.lims.common.util.StringUtil;
import us.mn.state.health.lims.common.util.SystemConfiguration;
import us.mn.state.health.lims.image.dao.ImageDAO;
import us.mn.state.health.lims.image.daoimpl.ImageDAOImpl;
import us.mn.state.health.lims.organization.dao.OrganizationDAO;
import us.mn.state.health.lims.organization.daoimpl.OrganizationDAOImpl;
import us.mn.state.health.lims.organization.valueholder.Organization;
import us.mn.state.health.lims.reports.action.implementation.reportBeans.ErrorMessages;
import us.mn.state.health.lims.siteinformation.dao.SiteInformationDAO;
import us.mn.state.health.lims.siteinformation.daoimpl.SiteInformationDAOImpl;
import us.mn.state.health.lims.siteinformation.valueholder.SiteInformation;

import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.apache.commons.validator.GenericValidator.isBlankOrNull;


public abstract class Report implements IReportCreator {

    public static ImageDAO imageDAO = new ImageDAOImpl();
    public static SiteInformationDAO siteInformationDAO = new SiteInformationDAOImpl();
    public static final String ERROR_REPORT = "NoticeOfReportError";
    
	protected static final String CSV = "csv";

	protected boolean initialized = false;
    protected boolean errorFound = false;
    protected List<ErrorMessages> errorMsgs = new ArrayList<ErrorMessages>();
    protected HashMap<String, Object> reportParameters = null;
    protected String requestedReport;
    private String fullReportFilename;

    @Override
    public void setRequestedReport( String report){
        requestedReport = report;
    }
	protected void initializeReport() {
		initialized = true;
	}

	public String getResponseHeaderName(){
		return null;
	}
	public String getResponseHeaderContent(){
		return null;
	}



    /**
     * @see us.mn.state.health.lims.reports.action.implementation.IReportCreator#getContentType()
     */
    public String getContentType() {
        return "application/pdf; charset=UTF-8";
    }

    /**
     * Make sure we have a reportParameters map and make sure there is lab director in that map (for any possible error report).
     * All reports need a director name either in their header including or on their error report page."
     */
    protected void createReportParameters() {
        reportParameters = (reportParameters != null) ? reportParameters : new HashMap<String, Object>();
        reportParameters.put("directorName", ConfigurationProperties.getInstance().getPropertyValue(Property.labDirectorName));
        reportParameters.put("siteName", ConfigurationProperties.getInstance().getPropertyValue(Property.SiteName));
        reportParameters.put("additionalSiteInfo", ConfigurationProperties.getInstance().getPropertyValue(Property.ADDITIONAL_SITE_INFO));
        reportParameters.put("usePageNumbers", ConfigurationProperties.getInstance().getPropertyValue(Property.USE_PAGE_NUMBERS_ON_REPORTS));
        reportParameters.put("localization", createLocalizationMap());
        reportParameters.put("leftHeaderImage", getImage("headerLeftImage"));
        reportParameters.put("rightHeaderImage", getImage("headerRightImage"));
        reportParameters.put("REPORT_LOCALE", SystemConfiguration.getInstance().getDefaultLocale());
    }

    private Object getImage(String siteName){
        SiteInformation siteInformation = siteInformationDAO.getSiteInformationByName( siteName );
        return GenericValidator.isBlankOrNull( siteInformation.getValue() ) ? null: imageDAO.retrieveImageInputStream( siteInformation.getValue() );
    }

    protected Map<String, String> createLocalizationMap(){
        HashMap<String,String> localizationMap = new HashMap<String, String>(  );
        localizationMap.put( "requestOrderNumber", StringUtil.getMessageForKey( "report.requestOrderNumber" ) );
        localizationMap.put( "confirmationOrderNumber", StringUtil.getMessageForKey( "report.confirmationOrderNumber" ) );
        localizationMap.put( "sampleType", StringUtil.getMessageForKey( "report.sampleType" ) );
        localizationMap.put( "reception", StringUtil.getMessageForKey( "report.reception" ) );
        localizationMap.put( "initialResults", StringUtil.getMessageForKey( "report.initialResults" ) );
        localizationMap.put( "confirmationResults", StringUtil.getMessageForKey( "report.confirmationResult" ) );
        localizationMap.put( "requesterContact", StringUtil.getMessageForKey( "report.requesterContact" ) );
        localizationMap.put( "telephoneAbv", StringUtil.getMessageForKey( "report.telephoneAbv" ) );
        localizationMap.put( "completionDate", StringUtil.getMessageForKey( "report.completionDate" ) );
        localizationMap.put( "site", StringUtil.getMessageForKey( "report.site" ) );
        localizationMap.put( "fax", StringUtil.getMessageForKey( "report.fax" ) );
        localizationMap.put( "email", StringUtil.getMessageForKey( "report.email" ) );
        localizationMap.put( "test", StringUtil.getMessageForKey( "report.test" ) );
        localizationMap.put( "result", StringUtil.getMessageForKey( "report.result" ) );
        localizationMap.put( "note", StringUtil.getMessageForKey( "report.note" ) );
        localizationMap.put( "pageNumberOf", StringUtil.getMessageForKey( "report.pageNumberOf" ) );
        localizationMap.put( "collectionDate", StringUtil.getMessageForKey( "report.collectionDate" ));
         /* For patient report CDI*/
        localizationMap.put("patientCode", StringUtil.getMessageForKey( "report.patientCode" ));
        localizationMap.put("prescriber", StringUtil.getMessageForKey( "report.prescriber" ));
        localizationMap.put("districtFacility", StringUtil.getMessageForKey( "report.districtFacility" ));
        localizationMap.put("regionFacility", StringUtil.getMessageForKey( "report.regionFacility" ));
        localizationMap.put("referringSite", StringUtil.getMessageForKey( "report.referringSite" ));<commit_msg>Deprecated localization method which used the wrong model for jasperReports<commit_after>/**
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations under
* the License.
*
* The Original Code is OpenELIS code.
*
* Copyright (C) CIRG, University of Washington, Seattle WA.  All Rights Reserved.
*
*/
package us.mn.state.health.lims.reports.action.implementation;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JasperRunManager;
import org.apache.commons.validator.GenericValidator;
import us.mn.state.health.lims.common.exception.LIMSRuntimeException;
import us.mn.state.health.lims.common.util.ConfigurationProperties;
import us.mn.state.health.lims.common.util.ConfigurationProperties.Property;
import us.mn.state.health.lims.common.util.DateUtil;
import us.mn.state.health.lims.common.util.StringUtil;
import us.mn.state.health.lims.common.util.SystemConfiguration;
import us.mn.state.health.lims.image.dao.ImageDAO;
import us.mn.state.health.lims.image.daoimpl.ImageDAOImpl;
import us.mn.state.health.lims.organization.dao.OrganizationDAO;
import us.mn.state.health.lims.organization.daoimpl.OrganizationDAOImpl;
import us.mn.state.health.lims.organization.valueholder.Organization;
import us.mn.state.health.lims.reports.action.implementation.reportBeans.ErrorMessages;
import us.mn.state.health.lims.siteinformation.dao.SiteInformationDAO;
import us.mn.state.health.lims.siteinformation.daoimpl.SiteInformationDAOImpl;
import us.mn.state.health.lims.siteinformation.valueholder.SiteInformation;

import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.apache.commons.validator.GenericValidator.isBlankOrNull;


public abstract class Report implements IReportCreator {

    public static ImageDAO imageDAO = new ImageDAOImpl();
    public static SiteInformationDAO siteInformationDAO = new SiteInformationDAOImpl();
    public static final String ERROR_REPORT = "NoticeOfReportError";
    
	protected static final String CSV = "csv";

	protected boolean initialized = false;
    protected boolean errorFound = false;
    protected List<ErrorMessages> errorMsgs = new ArrayList<ErrorMessages>();
    protected HashMap<String, Object> reportParameters = null;
    protected String requestedReport;
    private String fullReportFilename;

    @Override
    public void setRequestedReport( String report){
        requestedReport = report;
    }
	protected void initializeReport() {
		initialized = true;
	}

	public String getResponseHeaderName(){
		return null;
	}
	public String getResponseHeaderContent(){
		return null;
	}



    /**
     * @see us.mn.state.health.lims.reports.action.implementation.IReportCreator#getContentType()
     */
    public String getContentType() {
        return "application/pdf; charset=UTF-8";
    }

    /**
     * Make sure we have a reportParameters map and make sure there is lab director in that map (for any possible error report).
     * All reports need a director name either in their header including or on their error report page."
     */
    protected void createReportParameters() {
        reportParameters = (reportParameters != null) ? reportParameters : new HashMap<String, Object>();
        reportParameters.put("directorName", ConfigurationProperties.getInstance().getPropertyValue(Property.labDirectorName));
        reportParameters.put("siteName", ConfigurationProperties.getInstance().getPropertyValue(Property.SiteName));
        reportParameters.put("additionalSiteInfo", ConfigurationProperties.getInstance().getPropertyValue(Property.ADDITIONAL_SITE_INFO));
        reportParameters.put("usePageNumbers", ConfigurationProperties.getInstance().getPropertyValue(Property.USE_PAGE_NUMBERS_ON_REPORTS));
        reportParameters.put("localization", createLocalizationMap());
        reportParameters.put("leftHeaderImage", getImage("headerLeftImage"));
        reportParameters.put("rightHeaderImage", getImage("headerRightImage"));
        reportParameters.put("REPORT_LOCALE", SystemConfiguration.getInstance().getDefaultLocale());
    }

    private Object getImage(String siteName){
        SiteInformation siteInformation = siteInformationDAO.getSiteInformationByName( siteName );
        return GenericValidator.isBlankOrNull( siteInformation.getValue() ) ? null: imageDAO.retrieveImageInputStream( siteInformation.getValue() );
    }

    /**
     *
     * @return map
     * @deprecated The correct way to localize JasperReports is to us $R{key}.  This was put in before the correct way
     * was understood.  Do not add to this list.  It will eventually be moved to the correct way.
     */
    @Deprecated
    protected Map<String, String> createLocalizationMap(){
        HashMap<String,String> localizationMap = new HashMap<String, String>(  );
        localizationMap.put( "requestOrderNumber", StringUtil.getMessageForKey( "report.requestOrderNumber" ) );
        localizationMap.put( "confirmationOrderNumber", StringUtil.getMessageForKey( "report.confirmationOrderNumber" ) );
        localizationMap.put( "sampleType", StringUtil.getMessageForKey( "report.sampleType" ) );
        localizationMap.put( "reception", StringUtil.getMessageForKey( "report.reception" ) );
        localizationMap.put( "initialResults", StringUtil.getMessageForKey( "report.initialResults" ) );
        localizationMap.put( "confirmationResults", StringUtil.getMessageForKey( "report.confirmationResult" ) );
        localizationMap.put( "requesterContact", StringUtil.getMessageForKey( "report.requesterContact" ) );
        localizationMap.put( "telephoneAbv", StringUtil.getMessageForKey( "report.telephoneAbv" ) );
        localizationMap.put( "completionDate", StringUtil.getMessageForKey( "report.completionDate" ) );
        localizationMap.put( "site", StringUtil.getMessageForKey( "report.site" ) );
        localizationMap.put( "fax", StringUtil.getMessageForKey( "report.fax" ) );
        localizationMap.put( "email", StringUtil.getMessageForKey( "report.email" ) );
        localizationMap.put( "test", StringUtil.getMessageForKey( "report.test" ) );
        localizationMap.put( "result", StringUtil.getMessageForKey( "report.result" ) );
        localizationMap.put( "note", StringUtil.getMessageForKey( "report.note" ) );
        localizationMap.put( "pageNumberOf", StringUtil.getMessageForKey( "report.pageNumberOf" ) );
        localizationMap.put( "collectionDate", StringUtil.getMessageForKey( "report.collectionDate" ));
         /* For patient report CDI*/
        localizationMap.put("patientCode", StringUtil.getMessageForKey( "report.patientCode" ));
        localizationMap.put("prescriber", StringUtil.getMessageForKey( "report.prescriber" ));
        localizationMap.put("districtFacility", StringUtil.getMessageForKey( "report.districtFacility" ));
        localizationMap.put("regionFacility", StringUtil.getMessageForKey( "report.regionFacility" ));
        localizationMap.put("referringSite", StringUtil.getMessageForKey( "report.referringSite" ));
{'fraction_non_alphanumeric': 0.10595981071257116, 'fraction_numerical': 0.0004114944105342569, 'mean_word_length': 4.839807769323188, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '3230090', 'n_tokens_mistral': 3960, 'n_tokens_neox': 3949, 'n_words': 852}