<commit_before>/*
 * Copyright 2006-2008 The Kuali Foundation
 * 
 * Licensed under the Educational Community License, Version 2.0 (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.opensource.org/licenses/ecl1.php
 * 
 * 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 org.kuali.kra.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.codehaus.plexus.util.StringUtils;
import org.kuali.kra.award.AwardAmountInfoService;
import org.kuali.kra.award.contacts.AwardPerson;
import org.kuali.kra.award.contacts.AwardPersonUnit;
import org.kuali.kra.award.home.Award;
import org.kuali.kra.award.home.AwardAmountInfo;
import org.kuali.kra.award.home.fundingproposal.AwardFundingProposal;
import org.kuali.kra.institutionalproposal.home.InstitutionalProposal;
import org.kuali.kra.institutionalproposal.proposaladmindetails.ProposalAdminDetails;
import org.kuali.kra.proposaldevelopment.bo.DevelopmentProposal;
import org.kuali.kra.proposaldevelopment.bo.ProposalPerson;
import org.kuali.kra.proposaldevelopment.bo.ProposalPersonUnit;
import org.kuali.kra.service.AwardHierarchyUIService;
import org.kuali.kra.service.MedusaService;
import org.kuali.rice.kns.service.BusinessObjectService;

public class MedusaServiceImpl implements MedusaService {
    
    BusinessObjectService businessObjectService;
    AwardHierarchyUIService awardHierarchyUIService;
    AwardAmountInfoService awardAmountInfoService;
    

    public String getMedusaByProposal(String moduleName, Long moduleIdentifier) {
        StringBuilder sb = new StringBuilder();
        
        Collection<InstitutionalProposal> proposals = getProposals(moduleName, moduleIdentifier);
        if (proposals != null) {
            if (proposals.size() > 0) {
                sb.append("<h3>");
                sb.append("P %2A ");
            }
            for (InstitutionalProposal proposal : proposals) {
                sb.append("Institutional Proposal ").append(proposal.getProposalNumber()).append("%3A");
                sb.append(" %31 ");
                    appendInstitutionalProposalDetails(sb, proposal);
                sb.append(" %32 ");
                sb.append("%5A");
                Collection<DevelopmentProposal> devProposals = getDevelopmentProposals(proposal);
                for (DevelopmentProposal devProposal : devProposals) {
                    sb.append("%5C1 ");
                    sb.append("Development Proposal  ").append(devProposal.getProposalNumber()).append("%3A");
                    sb.append(" %31 ");
                        appendDevelopmentProposalDetails(sb, devProposal);
                    sb.append(" %32 ");
                    sb.append(" %5C2");
                }   
                
                Collection<Award> awards = getAwards(proposal);
                for (Award award : awards) {
                    AwardAmountInfo awardAmountInfo = awardAmountInfoService.fetchAwardAmountInfoWithHighestTransactionId(award.getAwardAmountInfos());
                    sb.append("%5C1 ");
                    sb.append("Award ").append(award.getAwardNumber()).append("%3A");
                    sb.append(" %31 ");
                    appendAwardDetails(sb, award, awardAmountInfo);
                    sb.append(" %32 ");
                    sb.append(" %5C2");
                }   
                sb.append("%5B");
            }
            if (proposals.size() > 0) {
                sb.append("</h3>");
            }
        }
        
       
        return sb.toString();
    }

    public String getMedusaByAward(String moduleName, Long moduleIdentifier) {
        StringBuilder sb = new StringBuilder();
        
        Collection<Award> awards = getAwards(moduleName, moduleIdentifier);
        
        if (awards != null) {
            if (awards.size() > 0) {
                sb.append("<h3>");
                sb.append("A %2A ");
            }
            
            for(Award award : awards){
                AwardAmountInfo awardAmountInfo = awardAmountInfoService.fetchAwardAmountInfoWithHighestTransactionId(award.getAwardAmountInfos());
                sb.append("Award " + award.getAwardNumber() + "%3A" );
                sb.append(" %31 ");
                    appendAwardDetails(sb, award, awardAmountInfo);
                sb.append(" %32 ");
                sb.append("%5A");
                Collection<InstitutionalProposal> proposals = getProposals(award);
                for (InstitutionalProposal proposal : proposals) {
                    sb.append("%5C1 ");
                    sb.append("Institutional Proposal ").append(proposal.getProposalNumber()).append("%3A");
                    sb.append(" %31 ");
                        appendInstitutionalProposalDetails(sb, proposal);
                    sb.append(" %32 ");
                    sb.append(" %5C2");
                    Collection<DevelopmentProposal> devProposals = getDevelopmentProposals(proposal);
                    sb.append(" %6A ");
                    for (DevelopmentProposal devProposal : devProposals) {
                        sb.append(" %6C1 ");
                        sb.append("Development Proposal  ").append(devProposal.getProposalNumber()).append("%3A");
                        sb.append(" %31 ");
                            appendDevelopmentProposalDetails(sb, devProposal);
                        sb.append(" %32 ");
                        sb.append(" %6C2");
                    }
                    sb.append(" %6B ");
                }
                sb.append("%5B");
            }
            if (awards.size() > 0) {
                sb.append("</h3>");
            }
        }    
        return sb.toString();
    }
    
    private void appendDevelopmentProposalDetails(StringBuilder sb, DevelopmentProposal dp) {
        sb.append("Development Proposal ").append(dp.getProposalNumber()).append(":").append(dp.getProposalNumber()).append(":").append("status").append(":");
        sb.append(dp.getOwnedByUnitNumber()).append(" ; ").append(dp.getOwnedByUnit().getUnitName()).append(":").append(dp.getRequestedStartDateInitial());
        sb.append(":").append(dp.getRequestedEndDateInitial()).append(":").append(dp.getTitle()).append(":").append(dp.getProposalType().getDescription());
        sb.append(":").append(dp.getNsfCode()).append(":").append(dp.getSponsorCode()).append(dp.getPrimeSponsorCode()).append(":").append(dp.getSponsorProposalNumber());
        sb.append(":").append(dp.getActivityType().getDescription()).append(":").append(dp.getProgramAnnouncementTitle()).append(":");
        sb.append(dp.getNoticeOfOpportunityCode()).append(":").append(dp.getProgramAnnouncementNumber()).append(":").append("Narrative").append(":");
        sb.append(dp.getBudgetStatus()).append(":");
        StringBuilder investigators = new StringBuilder();
        StringBuilder units = new StringBuilder();
        for(ProposalPerson proposalPerson :dp.getInvestigators()){
            investigators.append(proposalPerson.getFullName());
            for(ProposalPersonUnit unit: proposalPerson.getUnits()){
                units.append(unit.getUnit().getUnitName());
                units.append(" ; ");
            }
            investigators.append(" ; ");
        }
        sb.append(investigators).append(":").append(units);
    }    

    private void appendInstitutionalProposalDetails(StringBuilder sb, InstitutionalProposal proposal) {
        proposal.refreshReferenceObject("primeSponsor");
        sb.append("Institutinoal Proposal ").append(proposal.getProposalNumber()).append(":").append(proposal.getProposalId()).append(":");
        sb.append(proposal.getProposalNumber()).append(":");
        sb.append(proposal.getTitle()).append(":").append(proposal.getStatusCode()).append(":").append(proposal.getProposalTypeCode()).append(":");
        sb.append(proposal.getSponsorProposalNumber()).append(":").append("a/c no").append(":").append(proposal.getActivityTypeCode()).append(":");
        sb.append(proposal.getNsfCode()).append(":").append(proposal.getNoticeOfOpportunityCode()).append(":").append(proposal.getSponsorCode());
        
        String primeSponsorCode = " ";
        String primeSponsorName = " ";
        if(proposal.getPrimeSponsorCode()!=null){
            primeSponsorCode = proposal.getPrimeSponsor().getSponsorCode();
            primeSponsorName = proposal.getPrimeSponsor().getSponsorName();
        }
        
        sb.append(" ").append(proposal.getSponsorName()).append(":").append(primeSponsorCode).append(" ");
        sb.append(primeSponsorName).append(":").append(proposal.getRequestedStartDateInitial()).append(":");
        sb.append(proposal.getRequestedStartDateTotal()).append(":").append(proposal.getRequestedEndDateInitial()).append(":").append(proposal.getRequestedEndDateTotal());
        sb.append(":").append(proposal.getTotalDirectCostInitial()).append(":").append(proposal.getTotalDirectCostTotal()).append(":").append(proposal.getTotalIndirectCostInitial());
        sb.append(":").append(proposal.getTotalIndirectCostTotal()).append(":").append(proposal.getTotalInitialCost()).append(":");
        sb.append(proposal.getTotalCost()).append(":");
        sb.append(proposal.getPrincipalInvestigator().getFullName()).append(":");
        sb.append(proposal.getLeadUnit().getUnitNumber()).append(" ; ").append(proposal.getLeadUnit().getUnitName()).append(":");
    }

    /**
     * This method...
     * @param sb
     * @param award
     * @param awardAmountInfo
     */
    private void appendAwardDetails(StringBuilder sb, Award award, AwardAmountInfo awardAmountInfo) {
        sb.append("Award ").append(award.getAwardNumber()).append(":").append(award.getAwardId()).append(":");
        sb.append(award.getAwardId()).append(":").append(award.getAwardTypeCode()).append(":");
        sb.append(award.getSponsorAwardNumber()).append(":").append(award.getActivityType().getDescription()).append(":");
        sb.append(award.getAwardStatus().getDescription()).append(":").append(award.getTitle()).append(":");
        sb.append(award.getSponsorCode()).append(" - ").append(award.getSponsorName()).append(":").append(award.getBeginDate()).append(":");
        sb.append(awardAmountInfo.getCurrentFundEffectiveDate()).append(":").append(awardAmountInfo.getFinalExpirationDate()).append(":");
        sb.append(awardAmountInfo.getObligationExpirationDate()).append(":").append(awardAmountInfo.getAnticipatedTotalAmount()).append(":");
        sb.append(awardAmountInfo.getAmountObligatedToDate()).append(":").append(award.getAwardApprovedSubawards().size()==0?"N":"Y").append(":").append(award.getPaymentScheduleItems().size()==0?"N":"Y");
        sb.append(":").append(award.getApprovedEquipmentItems().size()==0?"N":"Y").append(":").append(award.getAwardTransferringSponsors().size()==0?"N":"Y").append(":").append(award.getApprovedForeignTravelTrips().size()==0?"N":"Y");
        sb.append(":").append(award.getCostSharingIndicator()).append(":").append(award.getAwardFandaRate().size()==0?"N":"Y").append(":");
        
        StringBuilder units = new StringBuilder();
        for(AwardPerson person: award.getProjectPersons()){
            sb.append(person.getFullName());
            if(person.isPrincipalInvestigator()){
                sb.append(" (Principal Investigator)");
            }
            sb.append(":");
            units = new StringBuilder();<commit_msg>KRACOEUS-3359: Resolving NPE when PI on Inst Proposal was null.<commit_after>/*
 * Copyright 2006-2008 The Kuali Foundation
 * 
 * Licensed under the Educational Community License, Version 2.0 (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.opensource.org/licenses/ecl1.php
 * 
 * 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 org.kuali.kra.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.codehaus.plexus.util.StringUtils;
import org.kuali.kra.award.AwardAmountInfoService;
import org.kuali.kra.award.contacts.AwardPerson;
import org.kuali.kra.award.contacts.AwardPersonUnit;
import org.kuali.kra.award.home.Award;
import org.kuali.kra.award.home.AwardAmountInfo;
import org.kuali.kra.award.home.fundingproposal.AwardFundingProposal;
import org.kuali.kra.institutionalproposal.home.InstitutionalProposal;
import org.kuali.kra.institutionalproposal.proposaladmindetails.ProposalAdminDetails;
import org.kuali.kra.proposaldevelopment.bo.DevelopmentProposal;
import org.kuali.kra.proposaldevelopment.bo.ProposalPerson;
import org.kuali.kra.proposaldevelopment.bo.ProposalPersonUnit;
import org.kuali.kra.service.AwardHierarchyUIService;
import org.kuali.kra.service.MedusaService;
import org.kuali.rice.kns.service.BusinessObjectService;
import org.kuali.rice.kns.util.ObjectUtils;

public class MedusaServiceImpl implements MedusaService {
    
    BusinessObjectService businessObjectService;
    AwardHierarchyUIService awardHierarchyUIService;
    AwardAmountInfoService awardAmountInfoService;
    

    public String getMedusaByProposal(String moduleName, Long moduleIdentifier) {
        StringBuilder sb = new StringBuilder();
        
        Collection<InstitutionalProposal> proposals = getProposals(moduleName, moduleIdentifier);
        if (proposals != null) {
            if (proposals.size() > 0) {
                sb.append("<h3>");
                sb.append("P %2A ");
            }
            for (InstitutionalProposal proposal : proposals) {
                sb.append("Institutional Proposal ").append(proposal.getProposalNumber()).append("%3A");
                sb.append(" %31 ");
                    appendInstitutionalProposalDetails(sb, proposal);
                sb.append(" %32 ");
                sb.append("%5A");
                Collection<DevelopmentProposal> devProposals = getDevelopmentProposals(proposal);
                for (DevelopmentProposal devProposal : devProposals) {
                    sb.append("%5C1 ");
                    sb.append("Development Proposal  ").append(devProposal.getProposalNumber()).append("%3A");
                    sb.append(" %31 ");
                        appendDevelopmentProposalDetails(sb, devProposal);
                    sb.append(" %32 ");
                    sb.append(" %5C2");
                }   
                
                Collection<Award> awards = getAwards(proposal);
                for (Award award : awards) {
                    AwardAmountInfo awardAmountInfo = awardAmountInfoService.fetchAwardAmountInfoWithHighestTransactionId(award.getAwardAmountInfos());
                    sb.append("%5C1 ");
                    sb.append("Award ").append(award.getAwardNumber()).append("%3A");
                    sb.append(" %31 ");
                    appendAwardDetails(sb, award, awardAmountInfo);
                    sb.append(" %32 ");
                    sb.append(" %5C2");
                }   
                sb.append("%5B");
            }
            if (proposals.size() > 0) {
                sb.append("</h3>");
            }
        }
        
       
        return sb.toString();
    }

    public String getMedusaByAward(String moduleName, Long moduleIdentifier) {
        StringBuilder sb = new StringBuilder();
        
        Collection<Award> awards = getAwards(moduleName, moduleIdentifier);
        
        if (awards != null) {
            if (awards.size() > 0) {
                sb.append("<h3>");
                sb.append("A %2A ");
            }
            
            for(Award award : awards){
                AwardAmountInfo awardAmountInfo = awardAmountInfoService.fetchAwardAmountInfoWithHighestTransactionId(award.getAwardAmountInfos());
                sb.append("Award " + award.getAwardNumber() + "%3A" );
                sb.append(" %31 ");
                    appendAwardDetails(sb, award, awardAmountInfo);
                sb.append(" %32 ");
                sb.append("%5A");
                Collection<InstitutionalProposal> proposals = getProposals(award);
                for (InstitutionalProposal proposal : proposals) {
                    sb.append("%5C1 ");
                    sb.append("Institutional Proposal ").append(proposal.getProposalNumber()).append("%3A");
                    sb.append(" %31 ");
                        appendInstitutionalProposalDetails(sb, proposal);
                    sb.append(" %32 ");
                    sb.append(" %5C2");
                    Collection<DevelopmentProposal> devProposals = getDevelopmentProposals(proposal);
                    sb.append(" %6A ");
                    for (DevelopmentProposal devProposal : devProposals) {
                        sb.append(" %6C1 ");
                        sb.append("Development Proposal  ").append(devProposal.getProposalNumber()).append("%3A");
                        sb.append(" %31 ");
                            appendDevelopmentProposalDetails(sb, devProposal);
                        sb.append(" %32 ");
                        sb.append(" %6C2");
                    }
                    sb.append(" %6B ");
                }
                sb.append("%5B");
            }
            if (awards.size() > 0) {
                sb.append("</h3>");
            }
        }    
        return sb.toString();
    }
    
    private void appendDevelopmentProposalDetails(StringBuilder sb, DevelopmentProposal dp) {
        sb.append("Development Proposal ").append(dp.getProposalNumber()).append(":").append(dp.getProposalNumber()).append(":").append("status").append(":");
        sb.append(dp.getOwnedByUnitNumber()).append(" ; ").append(dp.getOwnedByUnit().getUnitName()).append(":").append(dp.getRequestedStartDateInitial());
        sb.append(":").append(dp.getRequestedEndDateInitial()).append(":").append(dp.getTitle()).append(":").append(dp.getProposalType().getDescription());
        sb.append(":").append(dp.getNsfCode()).append(":").append(dp.getSponsorCode()).append(dp.getPrimeSponsorCode()).append(":").append(dp.getSponsorProposalNumber());
        sb.append(":").append(dp.getActivityType().getDescription()).append(":").append(dp.getProgramAnnouncementTitle()).append(":");
        sb.append(dp.getNoticeOfOpportunityCode()).append(":").append(dp.getProgramAnnouncementNumber()).append(":").append("Narrative").append(":");
        sb.append(dp.getBudgetStatus()).append(":");
        StringBuilder investigators = new StringBuilder();
        StringBuilder units = new StringBuilder();
        for(ProposalPerson proposalPerson :dp.getInvestigators()){
            investigators.append(proposalPerson.getFullName());
            for(ProposalPersonUnit unit: proposalPerson.getUnits()){
                units.append(unit.getUnit().getUnitName());
                units.append(" ; ");
            }
            investigators.append(" ; ");
        }
        sb.append(investigators).append(":").append(units);
    }    

    private void appendInstitutionalProposalDetails(StringBuilder sb, InstitutionalProposal proposal) {
        proposal.refreshReferenceObject("primeSponsor");
        sb.append("Institutinoal Proposal ").append(proposal.getProposalNumber()).append(":").append(proposal.getProposalId()).append(":");
        sb.append(proposal.getProposalNumber()).append(":");
        sb.append(proposal.getTitle()).append(":").append(proposal.getStatusCode()).append(":").append(proposal.getProposalTypeCode()).append(":");
        sb.append(proposal.getSponsorProposalNumber()).append(":").append("a/c no").append(":").append(proposal.getActivityTypeCode()).append(":");
        sb.append(proposal.getNsfCode()).append(":").append(proposal.getNoticeOfOpportunityCode()).append(":").append(proposal.getSponsorCode());
        
        String primeSponsorCode = " ";
        String primeSponsorName = " ";
        if(proposal.getPrimeSponsorCode()!=null){
            primeSponsorCode = proposal.getPrimeSponsor().getSponsorCode();
            primeSponsorName = proposal.getPrimeSponsor().getSponsorName();
        }
        
        sb.append(" ").append(proposal.getSponsorName()).append(":").append(primeSponsorCode).append(" ");
        sb.append(primeSponsorName).append(":").append(proposal.getRequestedStartDateInitial()).append(":");
        sb.append(proposal.getRequestedStartDateTotal()).append(":").append(proposal.getRequestedEndDateInitial()).append(":").append(proposal.getRequestedEndDateTotal());
        sb.append(":").append(proposal.getTotalDirectCostInitial()).append(":").append(proposal.getTotalDirectCostTotal()).append(":").append(proposal.getTotalIndirectCostInitial());
        sb.append(":").append(proposal.getTotalIndirectCostTotal()).append(":").append(proposal.getTotalInitialCost()).append(":");
        sb.append(proposal.getTotalCost()).append(":");
        sb.append(ObjectUtils.isNull(proposal.getPrincipalInvestigator()) ? " " : proposal.getPrincipalInvestigator().getFullName()).append(":");
        sb.append(proposal.getLeadUnit().getUnitNumber()).append(" ; ").append(proposal.getLeadUnit().getUnitName()).append(":");
    }

    /**
     * This method...
     * @param sb
     * @param award
     * @param awardAmountInfo
     */
    private void appendAwardDetails(StringBuilder sb, Award award, AwardAmountInfo awardAmountInfo) {
        sb.append("Award ").append(award.getAwardNumber()).append(":").append(award.getAwardId()).append(":");
        sb.append(award.getAwardId()).append(":").append(award.getAwardTypeCode()).append(":");
        sb.append(award.getSponsorAwardNumber()).append(":").append(award.getActivityType().getDescription()).append(":");
        sb.append(award.getAwardStatus().getDescription()).append(":").append(award.getTitle()).append(":");
        sb.append(award.getSponsorCode()).append(" - ").append(award.getSponsorName()).append(":").append(award.getBeginDate()).append(":");
        sb.append(awardAmountInfo.getCurrentFundEffectiveDate()).append(":").append(awardAmountInfo.getFinalExpirationDate()).append(":");
        sb.append(awardAmountInfo.getObligationExpirationDate()).append(":").append(awardAmountInfo.getAnticipatedTotalAmount()).append(":");
        sb.append(awardAmountInfo.getAmountObligatedToDate()).append(":").append(award.getAwardApprovedSubawards().size()==0?"N":"Y").append(":").append(award.getPaymentScheduleItems().size()==0?"N":"Y");
        sb.append(":").append(award.getApprovedEquipmentItems().size()==0?"N":"Y").append(":").append(award.getAwardTransferringSponsors().size()==0?"N":"Y").append(":").append(award.getApprovedForeignTravelTrips().size()==0?"N":"Y");
        sb.append(":").append(award.getCostSharingIndicator()).append(":").append(award.getAwardFandaRate().size()==0?"N":"Y").append(":");
        
        StringBuilder units = new StringBuilder();
        for(AwardPerson person: award.getProjectPersons()){
            sb.append(person.getFullName());
            if(person.isPrincipalInvestigator()){
                sb.append(" (Principal Investigator)");
            }
            sb.append(":");
            units = new StringBuilder();
{'fraction_non_alphanumeric': 0.1522814963932226, 'fraction_numerical': 0.0067941620533467535, 'mean_word_length': 2.921875, 'pattern_counts': {'":': 144, '<': 23, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7249080', 'n_tokens_mistral': 6893, 'n_tokens_neox': 6594, 'n_words': 859}