<commit_before>/*
 * Copyright 2005-2010 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.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.kuali.kra.award.awardhierarchy.AwardHierarchy;
import org.kuali.kra.award.awardhierarchy.AwardHierarchyService;
import org.kuali.kra.award.home.Award;
import org.kuali.kra.infrastructure.Constants;
import org.kuali.kra.service.AwardHierarchyUIService;
import org.kuali.kra.timeandmoney.AwardHierarchyNode;
import org.kuali.kra.timeandmoney.document.TimeAndMoneyDocument;
import org.kuali.kra.timeandmoney.service.ActivePendingTransactionsService;
import org.kuali.rice.core.api.util.type.KualiDecimal;
import org.kuali.rice.core.web.format.Formatter;
import org.kuali.rice.krad.service.BusinessObjectService;
import org.kuali.rice.krad.util.GlobalVariables;
import org.kuali.rice.krad.util.KRADConstants;
import org.springframework.util.StringUtils;

public class AwardHierarchyUIServiceImpl implements AwardHierarchyUIService {
    
    private static final String FIELD_NAME_PARENT_AWARD_NUMBER = "parentAwardNumber";
    private static final String FIELD_NAME_AWARD_NUMBER = "awardNumber";
    private static final String DATE_FORMAT_MM_DD_YYYY = "MM/dd/yyyy";
    private static final String TAG_H3_END = "</h3>";
    private static final String TAG_H3_START = "<h3>";
    private static final String LAST_5_CHARS_OF_ROOT = "00001";
    private static final String COLUMN_CODE = "%3A";
    private static final String DOC_FINAL_STATUS_CODE = "F";
    private static final String DOC_NON_FINAL_STATUS_CODE = "N"; 
    
    private BusinessObjectService businessObjectService;    
    private ActivePendingTransactionsService activePendingTransactionsService;
    private AwardHierarchyService awardHierarchyService;
    private Map<String, AwardHierarchyNode> awardHierarchyNodes;
    
    AwardHierarchyUIServiceImpl(){
        awardHierarchyNodes = new HashMap<String, AwardHierarchyNode>();    
    }
    
     /**
     * 
     * @see org.kuali.kra.service.AwardHierarchyUIService#getRootAwardNode(java.lang.String)
     */
    public String getRootAwardNode(String awardNumber, String currentAwardNumber, String currentSequenceNumber) throws ParseException{
        AwardHierarchyNode awardNode;
        if(canUseExistingTMSessionObject(awardNumber)){ 
            awardHierarchyNodes = ((TimeAndMoneyDocument)GlobalVariables.getUserSession().retrieveObject(
                    GlobalVariables.getUserSession().getKualiSessionId() + Constants.TIME_AND_MONEY_DOCUMENT_STRING_FOR_SESSION)).getAwardHierarchyNodes();
            awardNode = awardHierarchyNodes.get(awardNumber);
        }else{
            AwardHierarchy hierarchy = awardHierarchyService.loadAwardHierarchy(awardNumber);
            awardNode = awardHierarchyService.createAwardHierarchyNode(hierarchy, currentAwardNumber, currentSequenceNumber); 
        }
        return "[" + buildJavascriptRecord(awardNumber, awardNode) + "]"; 
    }

    public AwardHierarchyNode getRootAwardNode(Award award) {
        String awardNumber = award.getAwardNumber();
        AwardHierarchy hierarchy = awardHierarchyService.loadAwardHierarchy(awardNumber);
        AwardHierarchyNode awardNode = awardHierarchyService.createAwardHierarchyNode(hierarchy, null, null);
        return awardNode;
    }
    
    protected String buildJavascriptRecord(String awardNumber, AwardHierarchyNode aNode) {
        StringBuilder sb = new StringBuilder();
        SimpleDateFormat df = new SimpleDateFormat(DATE_FORMAT_MM_DD_YYYY);
        Formatter formatter = Formatter.getFormatter(KualiDecimal.class);
        sb.append("{");
        appendJson(sb, "awardNumber", awardNumber);
        sb.append(",");
        appendJson(sb, "accountNumber", aNode.getAccountNumber());
        sb.append(",");
        appendJson(sb, "principalInvestigatorName", aNode.getPrincipalInvestigatorName());
        sb.append(",");
        appendJson(sb, "leadUnitName", aNode.getLeadUnitName());
        sb.append(","); 
        //remove any and all carriage returns
        String titleCleaned = aNode.getTitle().replaceAll("[\\r\\n]", " ");   
        appendJson(sb, "title", titleCleaned);
        sb.append(",");
        appendJson(sb, "awardId", aNode.getAwardId().toString());
        sb.append(",");
        appendJson(sb, "awardDocumentNumber", aNode.getAwardDocumentNumber());
        sb.append(",");
        appendJson(sb, "awardDocumentFinalStatus", aNode.isAwardDocumentFinalStatus());
        sb.append(",");            
        appendJson(sb, "projectStartDate", aNode.getProjectStartDate(), df);
        sb.append(",");
        appendJson(sb, "currentFundEffectiveDate", aNode.getCurrentFundEffectiveDate(), df);
        sb.append(",");
        appendJson(sb, "obligationExpirationDate", aNode.getObligationExpirationDate(), df);
        sb.append(",");
        appendJson(sb, "finalExpirationDate", aNode.getFinalExpirationDate(), df);
        sb.append(",");
        appendJson(sb, "amountObligatedToDate", (String)formatter.format(aNode.getAmountObligatedToDate()));
        sb.append(",");<commit_msg>KRACOEUS-5673<commit_after>/*
 * Copyright 2005-2010 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.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.kuali.kra.award.awardhierarchy.AwardHierarchy;
import org.kuali.kra.award.awardhierarchy.AwardHierarchyService;
import org.kuali.kra.award.home.Award;
import org.kuali.kra.infrastructure.Constants;
import org.kuali.kra.service.AwardHierarchyUIService;
import org.kuali.kra.timeandmoney.AwardHierarchyNode;
import org.kuali.kra.timeandmoney.document.TimeAndMoneyDocument;
import org.kuali.kra.timeandmoney.service.ActivePendingTransactionsService;
import org.kuali.rice.core.api.util.type.KualiDecimal;
import org.kuali.rice.core.web.format.Formatter;
import org.kuali.rice.krad.service.BusinessObjectService;
import org.kuali.rice.krad.util.GlobalVariables;
import org.kuali.rice.krad.util.KRADConstants;
import org.springframework.util.StringUtils;

public class AwardHierarchyUIServiceImpl implements AwardHierarchyUIService {
    
    private static final String FIELD_NAME_PARENT_AWARD_NUMBER = "parentAwardNumber";
    private static final String FIELD_NAME_AWARD_NUMBER = "awardNumber";
    private static final String DATE_FORMAT_MM_DD_YYYY = "MM/dd/yyyy";
    private static final String TAG_H3_END = "</h3>";
    private static final String TAG_H3_START = "<h3>";
    private static final String LAST_5_CHARS_OF_ROOT = "00001";
    private static final String COLUMN_CODE = "%3A";
    private static final String DOC_FINAL_STATUS_CODE = "F";
    private static final String DOC_NON_FINAL_STATUS_CODE = "N"; 
    
    private BusinessObjectService businessObjectService;    
    private ActivePendingTransactionsService activePendingTransactionsService;
    private AwardHierarchyService awardHierarchyService;
    private Map<String, AwardHierarchyNode> awardHierarchyNodes;
    
    AwardHierarchyUIServiceImpl(){
        awardHierarchyNodes = new HashMap<String, AwardHierarchyNode>();    
    }
    
     /**
     * 
     * @see org.kuali.kra.service.AwardHierarchyUIService#getRootAwardNode(java.lang.String)
     */
    public String getRootAwardNode(String awardNumber, String currentAwardNumber, String currentSequenceNumber) throws ParseException{
        AwardHierarchyNode awardNode;
        if(canUseExistingTMSessionObject(awardNumber)){ 
            awardHierarchyNodes = ((TimeAndMoneyDocument)GlobalVariables.getUserSession().retrieveObject(
                    GlobalVariables.getUserSession().getKualiSessionId() + Constants.TIME_AND_MONEY_DOCUMENT_STRING_FOR_SESSION)).getAwardHierarchyNodes();
            awardNode = awardHierarchyNodes.get(awardNumber);
        }else{
            AwardHierarchy hierarchy = awardHierarchyService.loadAwardHierarchy(awardNumber);
            awardNode = awardHierarchyService.createAwardHierarchyNode(hierarchy, currentAwardNumber, currentSequenceNumber); 
        }
        return "[" + buildJavascriptRecord(awardNumber, awardNode) + "]"; 
    }

    public AwardHierarchyNode getRootAwardNode(Award award) {
        String awardNumber = award.getAwardNumber();
        AwardHierarchy hierarchy = awardHierarchyService.loadAwardHierarchy(awardNumber);
        AwardHierarchyNode awardNode = awardHierarchyService.createAwardHierarchyNode(hierarchy, null, null);
        return awardNode;
    }
    
    protected String buildJavascriptRecord(String awardNumber, AwardHierarchyNode aNode) {
        StringBuilder sb = new StringBuilder();
        SimpleDateFormat df = new SimpleDateFormat(DATE_FORMAT_MM_DD_YYYY);
        Formatter formatter = Formatter.getFormatter(KualiDecimal.class);
        sb.append("{");
        appendJson(sb, "awardNumber", awardNumber);
        sb.append(",");
        appendJson(sb, "accountNumber", aNode.getAccountNumber());
        sb.append(",");
        appendJson(sb, "principalInvestigatorName", aNode.getPrincipalInvestigatorName());
        sb.append(",");
        appendJson(sb, "leadUnitName", aNode.getLeadUnitName());
        sb.append(","); 
        
        //remove any and all carriage returns, and add leading and trailing spaces to '<' & '>'
        String titleCleaned = aNode.getTitle().replaceAll("[\\r\\n]", " ");   
        titleCleaned = titleCleaned.replaceAll(">", " > ");
        titleCleaned = titleCleaned.replaceAll("<", " < ");
        appendJson(sb, "title", titleCleaned);
        
        sb.append(",");
        appendJson(sb, "awardId", aNode.getAwardId().toString());
        sb.append(",");
        appendJson(sb, "awardDocumentNumber", aNode.getAwardDocumentNumber());
        sb.append(",");
        appendJson(sb, "awardDocumentFinalStatus", aNode.isAwardDocumentFinalStatus());
        sb.append(",");            
        appendJson(sb, "projectStartDate", aNode.getProjectStartDate(), df);
        sb.append(",");
        appendJson(sb, "currentFundEffectiveDate", aNode.getCurrentFundEffectiveDate(), df);
        sb.append(",");
        appendJson(sb, "obligationExpirationDate", aNode.getObligationExpirationDate(), df);
        sb.append(",");
        appendJson(sb, "finalExpirationDate", aNode.getFinalExpirationDate(), df);
        sb.append(",");
        appendJson(sb, "amountObligatedToDate", (String)formatter.format(aNode.getAmountObligatedToDate()));
        sb.append(",");
{'fraction_non_alphanumeric': 0.10302979578172079, 'fraction_numerical': 0.004017408771342484, 'mean_word_length': 4.128326180257511, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, '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': '5014168', 'n_tokens_mistral': 3572, 'n_tokens_neox': 3520, 'n_words': 690}