<commit_before>/*
 * Copyright 2006-2007 The Kuali Foundation.
 * 
 * Licensed under the Educational Community License, Version 1.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.module.gl.document;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;

import org.kuali.core.document.DocumentBase;
import org.kuali.core.document.TransactionalDocumentBase;
import org.kuali.core.service.DateTimeService;
import org.kuali.core.util.KualiDecimal;
import org.kuali.kfs.KFSPropertyConstants;
import org.kuali.kfs.util.SpringServiceLocator;
import org.kuali.module.gl.bo.CorrectionChangeGroup;
import org.kuali.module.gl.bo.OriginEntry;
import org.kuali.module.gl.bo.OriginEntryGroup;
import org.kuali.module.gl.bo.OriginEntrySource;
import org.kuali.module.gl.service.CorrectionDocumentService;
import org.kuali.module.gl.service.OriginEntryGroupService;
import org.kuali.module.gl.service.OriginEntryService;
import org.kuali.module.gl.service.ReportService;
import org.kuali.module.gl.service.ScrubberService;

/**
 * 
 */
public class CorrectionDocument extends TransactionalDocumentBase {
    private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(CorrectionDocument.class);

    private String correctionTypeCode; // CorrectionDocumentService.CORRECTION_TYPE_MANUAL or
                                        // CorrectionDocumentService.CORRECTION_TYPE_CRITERIA
    private boolean correctionSelection; // false if all input rows should be in the output, true if only selected rows should be
                                            // in the output
    private boolean correctionFileDelete; // false if the file should be processed by scrubber, true if the file should not be
                                            // processed by scrubber
    private Integer correctionRowCount; // Row count in output group
    private KualiDecimal correctionDebitTotalAmount; // Debit/Budget amount total in output group
    private KualiDecimal correctionCreditTotalAmount; // Credit amount total in output group
    private String correctionInputFileName; // File name if uploaded
    private String correctionOutputFileName; // Not used
    private String correctionScriptText; // Not used
    private Integer correctionInputGroupId; // Group ID that has input data
    private Integer correctionOutputGroupId; // Group ID that has output data
    private Integer correctionChangeGroupNextLineNumber;

    private List<CorrectionChangeGroup> correctionChangeGroup;

    public CorrectionDocument() {
        super();
        correctionChangeGroupNextLineNumber = new Integer(0);

        correctionChangeGroup = new ArrayList<CorrectionChangeGroup>();
    }

    /**
     * @see org.kuali.core.bo.BusinessObjectBase#toStringMapper()
     */
    @Override
    protected LinkedHashMap toStringMapper() {
        LinkedHashMap m = new LinkedHashMap();
        m.put(KFSPropertyConstants.DOCUMENT_NUMBER, this.documentNumber);
        return m;
    }

    public String getMethod() {
        if (CorrectionDocumentService.CORRECTION_TYPE_MANUAL.equals(correctionTypeCode)) {
            return "Manual Edit";
        }
        else {
            return "Using Criteria";
        }
    }

    public String getSystem() {
        if (correctionInputFileName != null) {
            return "File Upload";
        }
        else {
            return "Database";
        }
    }

    public void addCorrectionChangeGroup(CorrectionChangeGroup ccg) {
        ccg.setDocumentNumber(documentNumber);
        ccg.setCorrectionChangeGroupLineNumber(correctionChangeGroupNextLineNumber++);
        correctionChangeGroup.add(ccg);
    }

    public void removeCorrectionChangeGroup(int changeNumber) {
        for (Iterator iter = correctionChangeGroup.iterator(); iter.hasNext();) {
            CorrectionChangeGroup element = (CorrectionChangeGroup) iter.next();
            if (changeNumber == element.getCorrectionChangeGroupLineNumber().intValue()) {
                iter.remove();
            }
        }
    }

    public CorrectionChangeGroup getCorrectionChangeGroupItem(int groupNumber) {
        for (Iterator iter = correctionChangeGroup.iterator(); iter.hasNext();) {
            CorrectionChangeGroup element = (CorrectionChangeGroup) iter.next();
            if (groupNumber == element.getCorrectionChangeGroupLineNumber().intValue()) {
                return element;
            }
        }

        CorrectionChangeGroup ccg = new CorrectionChangeGroup(documentNumber, groupNumber);
        correctionChangeGroup.add(ccg);

        return ccg;
    }

    /**
     * If the document final, change the process flag on the output origin entry group (if necessary)
     * 
     * @see org.kuali.core.document.DocumentBase#handleRouteStatusChange()
     */
    @Override
    public void handleRouteStatusChange() {
        LOG.debug("handleRouteStatusChange() started");
        super.handleRouteStatusChange();

        CorrectionDocumentService correctionDocumentService = SpringServiceLocator.getCorrectionDocumentService();
        OriginEntryGroupService originEntryGroupService = SpringServiceLocator.getOriginEntryGroupService();

        String docId = getDocumentHeader().getDocumentNumber();
        CorrectionDocument doc = correctionDocumentService.findByCorrectionDocumentHeaderId(docId);

        if (getDocumentHeader().getWorkflowDocument().stateIsFinal()) {
            OriginEntryGroup outputGroup = originEntryGroupService.getExactMatchingEntryGroup(doc.getCorrectionOutputGroupId().intValue());
            if (!doc.getCorrectionFileDelete()) {
                LOG.debug("handleRouteStatusChange() Mark group as to be processed");
                outputGroup.setProcess(true);
                originEntryGroupService.save(outputGroup);
            }
        }
    }

    
    /**
     * Constant for the workgroup approval routing level
     */
    private static final Integer WORKGROUP_APPROVAL_ROUTE_LEVEL = new Integer(1);
    
    /**
     * @see org.kuali.core.document.DocumentBase#handleRouteLevelChange()
     */
    @Override
    public void handleRouteLevelChange() {
        super.handleRouteLevelChange();
        Integer routeLevel = getDocumentHeader().getWorkflowDocument().getDocRouteLevel();
        if (routeLevel == null) {
            LOG.error("Null routing level");
        }
        else if(WORKGROUP_APPROVAL_ROUTE_LEVEL.equals(routeLevel)) {
            String docId = getDocumentHeader().getDocumentNumber();
            // this code is performed asynchronously
            
            // First, save the origin entries to the origin entry table
            DateTimeService dateTimeService = SpringServiceLocator.getDateTimeService();
            OriginEntryService originEntryService = SpringServiceLocator.getOriginEntryService();
            CorrectionDocumentService correctionDocumentService = SpringServiceLocator.getCorrectionDocumentService();
            
            List<OriginEntry> outputEntries = correctionDocumentService.retrievePersistedOutputOriginEntries(this);
            System.out.println(outputEntries.size());
            
            // Create output group
            java.sql.Date today = dateTimeService.getCurrentSqlDate();
            // Scrub is set to false when the document is initiated. When the document is approved, it will be changed to true
            OriginEntryGroup oeg = originEntryService.copyEntries(today, OriginEntrySource.GL_CORRECTION_PROCESS_EDOC, true, false, true, outputEntries);
            
            // Now, run the reports
            ReportService reportService = SpringServiceLocator.getReportService();
            ScrubberService scrubberService = SpringServiceLocator.getScrubberService();
            
            setCorrectionOutputGroupId(oeg.getId());
            // not using the document service to save because it touches workflow, just save the doc BO as a regular BO
            SpringServiceLocator.getBusinessObjectService().save(this);
            
            LOG.debug("handleRouteStatusChange() Run reports");

            reportService.correctionOnlineReport(this, today);<commit_msg>KULRNE-4677: miscellaneous glcp bugs remove testing code<commit_after>/*
 * Copyright 2006-2007 The Kuali Foundation.
 * 
 * Licensed under the Educational Community License, Version 1.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.module.gl.document;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;

import org.kuali.core.document.DocumentBase;
import org.kuali.core.document.TransactionalDocumentBase;
import org.kuali.core.service.DateTimeService;
import org.kuali.core.util.KualiDecimal;
import org.kuali.kfs.KFSPropertyConstants;
import org.kuali.kfs.util.SpringServiceLocator;
import org.kuali.module.gl.bo.CorrectionChangeGroup;
import org.kuali.module.gl.bo.OriginEntry;
import org.kuali.module.gl.bo.OriginEntryGroup;
import org.kuali.module.gl.bo.OriginEntrySource;
import org.kuali.module.gl.service.CorrectionDocumentService;
import org.kuali.module.gl.service.OriginEntryGroupService;
import org.kuali.module.gl.service.OriginEntryService;
import org.kuali.module.gl.service.ReportService;
import org.kuali.module.gl.service.ScrubberService;

/**
 * 
 */
public class CorrectionDocument extends TransactionalDocumentBase {
    private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(CorrectionDocument.class);

    private String correctionTypeCode; // CorrectionDocumentService.CORRECTION_TYPE_MANUAL or
                                        // CorrectionDocumentService.CORRECTION_TYPE_CRITERIA
    private boolean correctionSelection; // false if all input rows should be in the output, true if only selected rows should be
                                            // in the output
    private boolean correctionFileDelete; // false if the file should be processed by scrubber, true if the file should not be
                                            // processed by scrubber
    private Integer correctionRowCount; // Row count in output group
    private KualiDecimal correctionDebitTotalAmount; // Debit/Budget amount total in output group
    private KualiDecimal correctionCreditTotalAmount; // Credit amount total in output group
    private String correctionInputFileName; // File name if uploaded
    private String correctionOutputFileName; // Not used
    private String correctionScriptText; // Not used
    private Integer correctionInputGroupId; // Group ID that has input data
    private Integer correctionOutputGroupId; // Group ID that has output data
    private Integer correctionChangeGroupNextLineNumber;

    private List<CorrectionChangeGroup> correctionChangeGroup;

    public CorrectionDocument() {
        super();
        correctionChangeGroupNextLineNumber = new Integer(0);

        correctionChangeGroup = new ArrayList<CorrectionChangeGroup>();
    }

    /**
     * @see org.kuali.core.bo.BusinessObjectBase#toStringMapper()
     */
    @Override
    protected LinkedHashMap toStringMapper() {
        LinkedHashMap m = new LinkedHashMap();
        m.put(KFSPropertyConstants.DOCUMENT_NUMBER, this.documentNumber);
        return m;
    }

    public String getMethod() {
        if (CorrectionDocumentService.CORRECTION_TYPE_MANUAL.equals(correctionTypeCode)) {
            return "Manual Edit";
        }
        else {
            return "Using Criteria";
        }
    }

    public String getSystem() {
        if (correctionInputFileName != null) {
            return "File Upload";
        }
        else {
            return "Database";
        }
    }

    public void addCorrectionChangeGroup(CorrectionChangeGroup ccg) {
        ccg.setDocumentNumber(documentNumber);
        ccg.setCorrectionChangeGroupLineNumber(correctionChangeGroupNextLineNumber++);
        correctionChangeGroup.add(ccg);
    }

    public void removeCorrectionChangeGroup(int changeNumber) {
        for (Iterator iter = correctionChangeGroup.iterator(); iter.hasNext();) {
            CorrectionChangeGroup element = (CorrectionChangeGroup) iter.next();
            if (changeNumber == element.getCorrectionChangeGroupLineNumber().intValue()) {
                iter.remove();
            }
        }
    }

    public CorrectionChangeGroup getCorrectionChangeGroupItem(int groupNumber) {
        for (Iterator iter = correctionChangeGroup.iterator(); iter.hasNext();) {
            CorrectionChangeGroup element = (CorrectionChangeGroup) iter.next();
            if (groupNumber == element.getCorrectionChangeGroupLineNumber().intValue()) {
                return element;
            }
        }

        CorrectionChangeGroup ccg = new CorrectionChangeGroup(documentNumber, groupNumber);
        correctionChangeGroup.add(ccg);

        return ccg;
    }

    /**
     * If the document final, change the process flag on the output origin entry group (if necessary)
     * 
     * @see org.kuali.core.document.DocumentBase#handleRouteStatusChange()
     */
    @Override
    public void handleRouteStatusChange() {
        LOG.debug("handleRouteStatusChange() started");
        super.handleRouteStatusChange();

        CorrectionDocumentService correctionDocumentService = SpringServiceLocator.getCorrectionDocumentService();
        OriginEntryGroupService originEntryGroupService = SpringServiceLocator.getOriginEntryGroupService();

        String docId = getDocumentHeader().getDocumentNumber();
        CorrectionDocument doc = correctionDocumentService.findByCorrectionDocumentHeaderId(docId);

        if (getDocumentHeader().getWorkflowDocument().stateIsFinal()) {
            OriginEntryGroup outputGroup = originEntryGroupService.getExactMatchingEntryGroup(doc.getCorrectionOutputGroupId().intValue());
            if (!doc.getCorrectionFileDelete()) {
                LOG.debug("handleRouteStatusChange() Mark group as to be processed");
                outputGroup.setProcess(true);
                originEntryGroupService.save(outputGroup);
            }
        }
    }

    
    /**
     * Constant for the workgroup approval routing level
     */
    private static final Integer WORKGROUP_APPROVAL_ROUTE_LEVEL = new Integer(1);
    
    /**
     * @see org.kuali.core.document.DocumentBase#handleRouteLevelChange()
     */
    @Override
    public void handleRouteLevelChange() {
        super.handleRouteLevelChange();
        Integer routeLevel = getDocumentHeader().getWorkflowDocument().getDocRouteLevel();
        if (routeLevel == null) {
            LOG.error("Null routing level");
        }
        else if(WORKGROUP_APPROVAL_ROUTE_LEVEL.equals(routeLevel)) {
            String docId = getDocumentHeader().getDocumentNumber();
            // this code is performed asynchronously
            
            // First, save the origin entries to the origin entry table
            DateTimeService dateTimeService = SpringServiceLocator.getDateTimeService();
            OriginEntryService originEntryService = SpringServiceLocator.getOriginEntryService();
            CorrectionDocumentService correctionDocumentService = SpringServiceLocator.getCorrectionDocumentService();
            
            List<OriginEntry> outputEntries = correctionDocumentService.retrievePersistedOutputOriginEntries(this);
            
            // Create output group
            java.sql.Date today = dateTimeService.getCurrentSqlDate();
            // Scrub is set to false when the document is initiated. When the document is approved, it will be changed to true
            OriginEntryGroup oeg = originEntryService.copyEntries(today, OriginEntrySource.GL_CORRECTION_PROCESS_EDOC, true, false, true, outputEntries);
            
            // Now, run the reports
            ReportService reportService = SpringServiceLocator.getReportService();
            ScrubberService scrubberService = SpringServiceLocator.getScrubberService();
            
            setCorrectionOutputGroupId(oeg.getId());
            // not using the document service to save because it touches workflow, just save the doc BO as a regular BO
            SpringServiceLocator.getBusinessObjectService().save(this);
            
            LOG.debug("handleRouteStatusChange() Run reports");

            reportService.correctionOnlineReport(this, today);
{'fraction_non_alphanumeric': 0.0777413604738083, 'fraction_numerical': 0.0019550342130987292, 'mean_word_length': 3.0711610486891385, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3106511', 'n_tokens_mistral': 4322, 'n_tokens_neox': 4224, 'n_words': 1084}