001/*
002 * Copyright 2008 The Kuali Foundation
003 * 
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * 
008 * http://www.opensource.org/licenses/ecl2.php
009 * 
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.ole.sys.document;
017
018import java.util.Map;
019
020import org.apache.log4j.Logger;
021import org.kuali.ole.sys.OLEConstants;
022import org.kuali.ole.sys.businessobject.FinancialSystemDocumentHeader;
023import org.kuali.ole.sys.context.SpringContext;
024import org.kuali.ole.sys.document.dataaccess.FinancialSystemDocumentHeaderDao;
025import org.kuali.rice.kew.api.WorkflowRuntimeException;
026import org.kuali.rice.kew.framework.postprocessor.DocumentRouteStatusChange;
027import org.kuali.rice.kim.api.identity.Person;
028import org.kuali.rice.kim.api.services.KimApiServiceLocator;
029import org.kuali.rice.kns.document.MaintenanceDocumentBase;
030import org.kuali.rice.krad.bo.DocumentHeader;
031
032/**
033 * This class is used by the system to use financial specific objects and data for maintenance documents
034 */
035public class FinancialSystemMaintenanceDocument extends MaintenanceDocumentBase implements FinancialSystemDocument {
036    private static final Logger LOG = Logger.getLogger(FinancialSystemMaintenanceDocument.class);
037
038    private transient Map<String,Boolean> canEditCache;
039
040    /**
041     * Constructs a FinancialSystemMaintenanceDocument.java.
042     */
043    public FinancialSystemMaintenanceDocument() {
044        super();
045    }
046
047    /**
048     * Constructs a FinancialSystemMaintenanceDocument.java.
049     * @param documentTypeName
050     */
051    public FinancialSystemMaintenanceDocument(String documentTypeName) {
052        super(documentTypeName);
053    }
054
055    /**
056     * @see org.kuali.rice.krad.document.DocumentBase#setDocumentHeader(org.kuali.rice.krad.bo.DocumentHeader)
057     */
058    @Override
059    public void setDocumentHeader(DocumentHeader documentHeader) {
060        if ((documentHeader != null) && (!FinancialSystemDocumentHeader.class.isAssignableFrom(documentHeader.getClass()))) {
061            throw new IllegalArgumentException("document header of class '" + documentHeader.getClass() + "' is not assignable from financial document header class '" + FinancialSystemDocumentHeader.class + "'");
062        }
063        this.documentHeader = documentHeader;
064    }
065
066    /**
067     * This is the default implementation which ensures that document note attachment references are loaded.
068     * 
069     * @see org.kuali.rice.krad.document.Document#processAfterRetrieve()
070     */
071    @Override
072    public void processAfterRetrieve() {
073        // set correctedByDocumentId manually, since OJB doesn't maintain that relationship
074        try {
075            DocumentHeader correctingDocumentHeader = SpringContext.getBean(FinancialSystemDocumentHeaderDao.class).getCorrectingDocumentHeader(getDocumentHeader().getWorkflowDocument().getDocumentId());
076            if (correctingDocumentHeader != null) {
077                getFinancialSystemDocumentHeader().setCorrectedByDocumentId(correctingDocumentHeader.getDocumentNumber());
078            }
079        } catch (RuntimeException e) {
080            LOG.error("Received WorkflowException trying to get route header id from workflow document");
081            throw new WorkflowRuntimeException(e);
082        }
083        // set the ad hoc route recipients too, since OJB doesn't maintain that relationship
084        // TODO - see KULNRVSYS-1054
085
086        super.processAfterRetrieve();
087    }
088
089    /**
090     * This is the default implementation which checks for a different workflow statuses, and updates the Kuali status accordingly.
091     * 
092     * @see org.kuali.rice.krad.document.Document#doRouteStatusChange()
093     */
094    @Override
095    public void doRouteStatusChange(DocumentRouteStatusChange statusChangeEvent) {
096        if (getDocumentHeader().getWorkflowDocument().isCanceled()) {
097            getFinancialSystemDocumentHeader().setFinancialDocumentStatusCode(OLEConstants.DocumentStatusCodes.CANCELLED);
098        }
099        else if (getDocumentHeader().getWorkflowDocument().isEnroute()) {
100            getFinancialSystemDocumentHeader().setFinancialDocumentStatusCode(OLEConstants.DocumentStatusCodes.ENROUTE);
101        }
102        if (getDocumentHeader().getWorkflowDocument().isDisapproved()) {
103            getFinancialSystemDocumentHeader().setFinancialDocumentStatusCode(OLEConstants.DocumentStatusCodes.DISAPPROVED);
104        }
105        if (getDocumentHeader().getWorkflowDocument().isProcessed()) {
106            getFinancialSystemDocumentHeader().setFinancialDocumentStatusCode(OLEConstants.DocumentStatusCodes.APPROVED);
107        }
108        if ( LOG.isInfoEnabled() ) {
109            LOG.info("Status is: " + getFinancialSystemDocumentHeader().getFinancialDocumentStatusCode());
110        }
111        
112        super.doRouteStatusChange(statusChangeEvent);
113    }
114    
115    @Override
116    public boolean answerSplitNodeQuestion(String nodeName) {
117        if (getNewMaintainableObject() == null) {
118                throw new UnsupportedOperationException("Cannot access Maintainable class to answer split node question");
119            }
120        if (getNewMaintainableObject() instanceof FinancialSystemMaintainable) {
121            return ((FinancialSystemMaintainable)getNewMaintainableObject()).answerSplitNodeQuestion(nodeName);
122        } else if (getNewMaintainableObject() instanceof FinancialSystemGlobalMaintainable) {
123            return ((FinancialSystemGlobalMaintainable)getNewMaintainableObject()).answerSplitNodeQuestion(nodeName);
124        } else {
125            throw new UnsupportedOperationException("Maintainable for "+getNewMaintainableObject().getBoClass().getName()+" does not extend org.kuali.ole.sys.document.FinancialSystemMaintainable nor org.kuali.ole.sys.document.FinancialSystemGlobalMaintainable and therefore cannot answer split node question");
126        }
127    }
128    
129    /**
130     * This method is used for routing and simply returns the initiator's Chart code.
131     * @return The Chart code of the document initiator
132     */
133    public String getInitiatorChartOfAccountsCode() {
134        String[] chartOrg = getInitiatorPrimaryDepartmentCode();
135        return chartOrg[0];
136    }
137    
138    /**
139     * This method is used for routing and simply returns the initiator's Organization code.
140     * @return The Organization code of the document initiator
141     */
142    public String getInitiatorOrganizationCode() {
143        String[] chartOrg = getInitiatorPrimaryDepartmentCode();
144        return chartOrg[1];
145    }
146    
147    /**
148     * 
149     * This method is a utility method that returns a String array containing the document initiator's
150     * ChartCode in the first index and the OrganizationCode in the second.
151     * @return a String array.
152     */
153    protected String[] getInitiatorPrimaryDepartmentCode() {
154        
155        String netID = documentHeader.getWorkflowDocument().getInitiatorPrincipalId();
156        Person person =  KimApiServiceLocator.getPersonService().getPerson(netID);
157       
158        String deptCode = person.getPrimaryDepartmentCode();
159        String[] chartOrg = deptCode.split("-");
160        return chartOrg;
161        
162    }
163    
164    @Override
165    public FinancialSystemDocumentHeader getFinancialSystemDocumentHeader() {
166        return (FinancialSystemDocumentHeader)documentHeader;
167    }
168
169}
170