<commit_before>/*
 * Kuali Coeus, a comprehensive research administration system for higher education.
 * 
 * Copyright 2005-2015 Kuali, Inc.
 * 
 * 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.kuali.coeus.propdev.impl.notification;

import org.kuali.coeus.common.notification.impl.NotificationRendererBase;
import org.kuali.coeus.propdev.impl.core.ProposalDevelopmentService;
import org.kuali.coeus.propdev.impl.person.ProposalPerson;
import org.kuali.kra.infrastructure.Constants;
import org.kuali.kra.infrastructure.RoleConstants;
import org.kuali.kra.institutionalproposal.home.InstitutionalProposal;
import org.kuali.rice.coreservice.framework.parameter.ParameterService;
import org.kuali.coeus.propdev.impl.core.DevelopmentProposal;
import org.kuali.coeus.propdev.impl.docperm.ProposalUserRoles;
import org.kuali.coeus.propdev.impl.attachment.Narrative;
import org.kuali.coeus.propdev.impl.editable.ProposalChangedData;
import org.kuali.coeus.sys.framework.service.KcServiceLocator;
import org.springframework.stereotype.Component;
import org.springframework.context.annotation.Scope;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * Renders fields for the Proposal Development notifications.
 */

@Component("proposalDevelopmentNotificationRenderer")
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class ProposalDevelopmentNotificationRenderer extends NotificationRendererBase {

    private static final long serialVersionUID = 1143944858168503090L;

    private DevelopmentProposal developmentProposal;
    private ProposalChangedData proposalChangedData;
    private Narrative modifiedNarrative;
    private ProposalPerson proposalPerson;

    @Autowired
    @Qualifier("proposalDevelopmentService")
    private transient ProposalDevelopmentService proposalDevelopmentService;
    
    @Autowired
    @Qualifier("parameterService")
    private transient ParameterService parameterService;
    

	public ProposalDevelopmentNotificationRenderer() {
        
    }
    
    /**
     * Constructs a Proposal Development notification renderer.
     * @param developmentProposal
     */
    public ProposalDevelopmentNotificationRenderer(DevelopmentProposal developmentProposal) {
        this.developmentProposal = developmentProposal;
    }

    @Override
    public Map<String, String> getDefaultReplacementParameters() {
        SimpleDateFormat dateFormatter = new SimpleDateFormat("MM/dd/yyyy");
        Map<String, String> result = super.getDefaultReplacementParameters();
        result.put("{DOCUMENT_NUMBER}",developmentProposal.getProposalDocument().getDocumentNumber());
        result.put("{PROPOSAL_NUMBER}", developmentProposal.getProposalNumber());
        result.put("{PROPOSAL_TITLE}", developmentProposal.getTitle());
        result.put("{PRINCIPAL INVESTIGATOR}",developmentProposal.getPrincipalInvestigatorName());
        result.put("{SPONSOR_CODE}", developmentProposal.getSponsorCode());
        result.put("{SPONSOR_NAME}", developmentProposal.getSponsorName());
        result.put("{START_DATE}",developmentProposal.getRequestedStartDateInitial().toString());
        result.put("{END_DATE}",developmentProposal.getRequestedEndDateInitial().toString());
        result.put("{PROGRAM_ANNOUNCEMENT_NUMBER}", developmentProposal.getProgramAnnouncementNumber());
        result.put("{PROGRAM_ANNOUNCEMENT_TITLE}", developmentProposal.getProgramAnnouncementTitle());
        result.put("{CFDA_NUMBER}", developmentProposal.getCfdaNumber());
        List<ProposalUserRoles> proposalUserRoles =  developmentProposal.getWorkingUserRoles();
        StringBuilder aggregators = new StringBuilder();
        int proposalUserCount = 0;
        if(proposalUserRoles!=null){
        	for(ProposalUserRoles proposalUserRole : proposalUserRoles){

        		if(proposalUserRole.getRoleNames().contains(RoleConstants.AGGREGATOR_DOCUMENT_LEVEL)){
        			proposalUserCount = proposalUserCount ++;
        			aggregators.append(proposalUserRole.getUsername());
        			if(proposalUserCount!=proposalUserRoles.size()-1){
        				aggregators.append(",");
        			}
        		}
        	}
        }
        result.put("{AGGREGATOR}", aggregators.toString());
        String coiLink =  getParameterService().
        		getParameterValueAsString(Constants.KC_GENERIC_PARAMETER_NAMESPACE,Constants.KC_ALL_PARAMETER_DETAIL_TYPE_CODE, "LINK_TO_COI");
        result.put("{LINK_TO_COI}", coiLink);
        if (proposalPerson != null) {
        String certificatioPage =result.get("{APP_LINK_PREFIX}")+"/kc-pd-krad/proposalDevelopment?methodToCall=viewUtility&" +
        		"viewId=PropDev-CertificationView&docId="+developmentProposal.getProposalDocument().getDocumentNumber()+"&userName="+proposalPerson.getUserName();
        result.put("{CERT_PAGE}", certificatioPage);
        }
        
        if (developmentProposal.getDeadlineDate() != null) {
            result.put("{DEADLINE_DATE}", dateFormatter.format(developmentProposal.getDeadlineDate()));
        } else {
            result.put("{DEADLINE_DATE}", "");
        }
        result.put("{PI_NAME}", developmentProposal.getPrincipalInvestigatorName());
        result.put("{LEAD_UNIT}", developmentProposal.getUnitNumber());
        result.put("{LEAD_UNIT_NAME}", developmentProposal.getUnit().getUnitName());
        result.put("{PRIME_SPONSOR_CODE}", developmentProposal.getPrimeSponsorCode());
        result.put("{PRIME_SPONSOR_NAME}", developmentProposal.getPrimeSponsor() != null ? developmentProposal.getPrimeSponsor().getSponsorName() : "");
        InstitutionalProposal instProp = getProposalDevelopmentService().getInstitutionalProposal(developmentProposal.getProposalNumber());
        result.put("{INSTITUTIONAL_PROPOSAL_NUMBER}", instProp != null ? instProp.getProposalNumber() : "");
        result.put("{INSTITUTIONAL_PROPOSAL_DOC_NBR}", instProp != null ? instProp.getInstitutionalProposalDocument().getDocumentNumber() : "");
        if (proposalChangedData != null) {
            result.put("{OVERRIDE_FIELD_NAME}", proposalChangedData.getEditableColumn().getColumnLabel());
            result.put("{OVERRIDE_FIELD_VALUE}", proposalChangedData.getDisplayValue());
        }
        if (modifiedNarrative != null && modifiedNarrative.getModuleNumber()!=null) {
            result.put("{NARRATIVE_MODULE_NUM}", modifiedNarrative.getModuleNumber().toString());
            result.put("{NARRATIVE_TYPE}", modifiedNarrative.getNarrativeType().getDescription());
            result.put("{NARRATIVE_MODULE_DESCRIPTION}", modifiedNarrative.getModuleTitle() == null ? "" : modifiedNarrative.getModuleTitle());
        }
        if (proposalPerson != null) {
            result.put("{USER_NAME}",proposalPerson.getUserName());
            result.put("{PROPOSAL_CERTIFY_USER}",proposalPerson.getCertifiedPersonName()); 
            result.put("{PROPOSAL_CERTIFY_TIME_STAMP}", proposalPerson.getCertifiedTimeStamp());        
        }
        if(developmentProposal.getContactAdministrator()!=null){
        	result.put("{CA_FIRST_NAME}",developmentProposal.getContactAdministrator().getFirstName()); 
        	result.put("{CA_LAST_NAME}",developmentProposal.getContactAdministrator().getLastName()); 
        	result.put("{CA_FIRST_NAME}",developmentProposal.getContactAdministrator().getFirstName()); 
        	result.put("{CA_EMAIL}",developmentProposal.getContactAdministrator().getEmailAddress()); 
        	result.put("{CA_PHONE}",developmentProposal.getContactAdministrator().getPhoneNumber()); 
            String wlFunfact =  getParameterService().
            		getParameterValueAsString(Constants.KC_GENERIC_PARAMETER_NAMESPACE,Constants.KC_ALL_PARAMETER_DETAIL_TYPE_CODE, "WL_FUN_FACTS");
        	result.put("{WL_FUN_FACTS}",wlFunfact);<commit_msg>Added null checking notification renderer<commit_after>/*
 * Kuali Coeus, a comprehensive research administration system for higher education.
 * 
 * Copyright 2005-2015 Kuali, Inc.
 * 
 * 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.kuali.coeus.propdev.impl.notification;

import org.kuali.coeus.common.notification.impl.NotificationRendererBase;
import org.kuali.coeus.propdev.impl.core.ProposalDevelopmentService;
import org.kuali.coeus.propdev.impl.person.ProposalPerson;
import org.kuali.kra.infrastructure.Constants;
import org.kuali.kra.infrastructure.RoleConstants;
import org.kuali.kra.institutionalproposal.home.InstitutionalProposal;
import org.kuali.rice.coreservice.framework.parameter.ParameterService;
import org.kuali.coeus.propdev.impl.core.DevelopmentProposal;
import org.kuali.coeus.propdev.impl.docperm.ProposalUserRoles;
import org.kuali.coeus.propdev.impl.attachment.Narrative;
import org.kuali.coeus.propdev.impl.editable.ProposalChangedData;
import org.kuali.coeus.sys.framework.service.KcServiceLocator;
import org.springframework.stereotype.Component;
import org.springframework.context.annotation.Scope;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * Renders fields for the Proposal Development notifications.
 */

@Component("proposalDevelopmentNotificationRenderer")
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class ProposalDevelopmentNotificationRenderer extends NotificationRendererBase {

    private static final long serialVersionUID = 1143944858168503090L;

    private DevelopmentProposal developmentProposal;
    private ProposalChangedData proposalChangedData;
    private Narrative modifiedNarrative;
    private ProposalPerson proposalPerson;

    @Autowired
    @Qualifier("proposalDevelopmentService")
    private transient ProposalDevelopmentService proposalDevelopmentService;
    
    @Autowired
    @Qualifier("parameterService")
    private transient ParameterService parameterService;
    

	public ProposalDevelopmentNotificationRenderer() {
        
    }
    
    /**
     * Constructs a Proposal Development notification renderer.
     * @param developmentProposal
     */
    public ProposalDevelopmentNotificationRenderer(DevelopmentProposal developmentProposal) {
        this.developmentProposal = developmentProposal;
    }

    @Override
    public Map<String, String> getDefaultReplacementParameters() {
        SimpleDateFormat dateFormatter = new SimpleDateFormat("MM/dd/yyyy");
        Map<String, String> result = super.getDefaultReplacementParameters();
        result.put("{DOCUMENT_NUMBER}",developmentProposal.getProposalDocument().getDocumentNumber());
        result.put("{PROPOSAL_NUMBER}", developmentProposal.getProposalNumber());
        result.put("{PROPOSAL_TITLE}", developmentProposal.getTitle());
        result.put("{PRINCIPAL INVESTIGATOR}",developmentProposal.getPrincipalInvestigatorName());
        result.put("{SPONSOR_CODE}", developmentProposal.getSponsorCode());
        result.put("{SPONSOR_NAME}", developmentProposal.getSponsorName());
        result.put("{START_DATE}",developmentProposal.getRequestedStartDateInitial().toString());
        result.put("{END_DATE}",developmentProposal.getRequestedEndDateInitial().toString());
        result.put("{PROGRAM_ANNOUNCEMENT_NUMBER}", developmentProposal.getProgramAnnouncementNumber()== null ? "" : developmentProposal.getProgramAnnouncementNumber());
        result.put("{PROGRAM_ANNOUNCEMENT_TITLE}", developmentProposal.getProgramAnnouncementTitle()== null ? "" :developmentProposal.getProgramAnnouncementTitle());
        result.put("{CFDA_NUMBER}", developmentProposal.getCfdaNumber()== null ? "" :developmentProposal.getCfdaNumber());
        List<ProposalUserRoles> proposalUserRoles =  developmentProposal.getWorkingUserRoles();
        StringBuilder aggregators = new StringBuilder();
        int proposalUserCount = 0;
        if(proposalUserRoles!=null){
        	for(ProposalUserRoles proposalUserRole : proposalUserRoles){

        		if(proposalUserRole.getRoleNames().contains(RoleConstants.AGGREGATOR_DOCUMENT_LEVEL)){
        			proposalUserCount = proposalUserCount ++;
        			aggregators.append(proposalUserRole.getUsername());
        			if(proposalUserCount!=proposalUserRoles.size()-1){
        				aggregators.append(",");
        			}
        		}
        	}
        }
        result.put("{AGGREGATOR}", aggregators.toString());
        String coiLink =  getParameterService().
        		getParameterValueAsString(Constants.KC_GENERIC_PARAMETER_NAMESPACE,Constants.KC_ALL_PARAMETER_DETAIL_TYPE_CODE, "LINK_TO_COI");
        result.put("{LINK_TO_COI}", coiLink);
        if (proposalPerson != null) {
        String certificatioPage =result.get("{APP_LINK_PREFIX}")+"/kc-pd-krad/proposalDevelopment?methodToCall=viewUtility&" +
        		"viewId=PropDev-CertificationView&docId="+developmentProposal.getProposalDocument().getDocumentNumber()+"&userName="+proposalPerson.getUserName();
        result.put("{CERT_PAGE}", certificatioPage);
        }
        
        if (developmentProposal.getDeadlineDate() != null) {
            result.put("{DEADLINE_DATE}", dateFormatter.format(developmentProposal.getDeadlineDate()));
        } else {
            result.put("{DEADLINE_DATE}", "");
        }
        result.put("{PI_NAME}", developmentProposal.getPrincipalInvestigatorName());
        result.put("{LEAD_UNIT}", developmentProposal.getUnitNumber());
        result.put("{LEAD_UNIT_NAME}", developmentProposal.getUnit().getUnitName());
        result.put("{PRIME_SPONSOR_CODE}", developmentProposal.getPrimeSponsorCode()!= null ? developmentProposal.getPrimeSponsorCode() : "");
        result.put("{PRIME_SPONSOR_NAME}", developmentProposal.getPrimeSponsor() != null ? developmentProposal.getPrimeSponsor().getSponsorName() : "");
        InstitutionalProposal instProp = getProposalDevelopmentService().getInstitutionalProposal(developmentProposal.getProposalNumber());
        result.put("{INSTITUTIONAL_PROPOSAL_NUMBER}", instProp != null ? instProp.getProposalNumber() : "");
        result.put("{INSTITUTIONAL_PROPOSAL_DOC_NBR}", instProp != null ? instProp.getInstitutionalProposalDocument().getDocumentNumber() : "");
        if (proposalChangedData != null) {
            result.put("{OVERRIDE_FIELD_NAME}", proposalChangedData.getEditableColumn().getColumnLabel());
            result.put("{OVERRIDE_FIELD_VALUE}", proposalChangedData.getDisplayValue());
        }
        if (modifiedNarrative != null && modifiedNarrative.getModuleNumber()!=null) {
            result.put("{NARRATIVE_MODULE_NUM}", modifiedNarrative.getModuleNumber().toString());
            result.put("{NARRATIVE_TYPE}", modifiedNarrative.getNarrativeType().getDescription());
            result.put("{NARRATIVE_MODULE_DESCRIPTION}", modifiedNarrative.getModuleTitle() == null ? "" : modifiedNarrative.getModuleTitle());
        }
        if (proposalPerson != null) {
            result.put("{USER_NAME}",proposalPerson.getUserName());
            result.put("{PROPOSAL_CERTIFY_USER}",proposalPerson.getCertifiedPersonName()!= null ? proposalPerson.getCertifiedPersonName() :""); 
            result.put("{PROPOSAL_CERTIFY_TIME_STAMP}", proposalPerson.getCertifiedTimeStamp()!= null ? proposalPerson.getCertifiedTimeStamp() :"");        
        }
        if(developmentProposal.getContactAdministrator()!=null){
        	result.put("{CA_FIRST_NAME}",developmentProposal.getContactAdministrator().getFirstName() != null ? developmentProposal.getContactAdministrator().getFirstName() :""); 
        	result.put("{CA_LAST_NAME}",developmentProposal.getContactAdministrator().getLastName() != null ? developmentProposal.getContactAdministrator().getLastName() :""); 
        	result.put("{CA_FIRST_NAME}",developmentProposal.getContactAdministrator().getFirstName() != null ? developmentProposal.getContactAdministrator().getFirstName() :""); 
        	result.put("{CA_EMAIL}",developmentProposal.getContactAdministrator().getEmailAddress() != null ? developmentProposal.getContactAdministrator().getEmailAddress() :""); 
        	result.put("{CA_PHONE}",developmentProposal.getContactAdministrator().getPhoneNumber() != null ? developmentProposal.getContactAdministrator().getPhoneNumber() :""); 
            String wlFunfact =  getParameterService().
            		getParameterValueAsString(Constants.KC_GENERIC_PARAMETER_NAMESPACE,Constants.KC_ALL_PARAMETER_DETAIL_TYPE_CODE, "WL_FUN_FACTS");
        	result.put("{WL_FUN_FACTS}",wlFunfact != null ? wlFunfact  :"");
{'fraction_non_alphanumeric': 0.11224489795918367, 'fraction_numerical': 0.0033274179236912156, 'mean_word_length': 4.794665809768637, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 30, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6005673', 'n_tokens_mistral': 5248, 'n_tokens_neox': 4970, 'n_words': 739}