View Javadoc
1   /*
2    * Copyright 2008 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.sys.document;
17  
18  import java.util.Map;
19  
20  import org.apache.log4j.Logger;
21  import org.kuali.ole.sys.OLEConstants;
22  import org.kuali.ole.sys.businessobject.FinancialSystemDocumentHeader;
23  import org.kuali.ole.sys.context.SpringContext;
24  import org.kuali.ole.sys.document.dataaccess.FinancialSystemDocumentHeaderDao;
25  import org.kuali.rice.kew.api.WorkflowRuntimeException;
26  import org.kuali.rice.kew.framework.postprocessor.DocumentRouteStatusChange;
27  import org.kuali.rice.kim.api.identity.Person;
28  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
29  import org.kuali.rice.kns.document.MaintenanceDocumentBase;
30  import org.kuali.rice.krad.bo.DocumentHeader;
31  
32  /**
33   * This class is used by the system to use financial specific objects and data for maintenance documents
34   */
35  public class FinancialSystemMaintenanceDocument extends MaintenanceDocumentBase implements FinancialSystemDocument {
36      private static final Logger LOG = Logger.getLogger(FinancialSystemMaintenanceDocument.class);
37  
38      private transient Map<String,Boolean> canEditCache;
39  
40      /**
41       * Constructs a FinancialSystemMaintenanceDocument.java.
42       */
43      public FinancialSystemMaintenanceDocument() {
44          super();
45      }
46  
47      /**
48       * Constructs a FinancialSystemMaintenanceDocument.java.
49       * @param documentTypeName
50       */
51      public FinancialSystemMaintenanceDocument(String documentTypeName) {
52          super(documentTypeName);
53      }
54  
55      /**
56       * @see org.kuali.rice.krad.document.DocumentBase#setDocumentHeader(org.kuali.rice.krad.bo.DocumentHeader)
57       */
58      @Override
59      public void setDocumentHeader(DocumentHeader documentHeader) {
60          if ((documentHeader != null) && (!FinancialSystemDocumentHeader.class.isAssignableFrom(documentHeader.getClass()))) {
61              throw new IllegalArgumentException("document header of class '" + documentHeader.getClass() + "' is not assignable from financial document header class '" + FinancialSystemDocumentHeader.class + "'");
62          }
63          this.documentHeader = documentHeader;
64      }
65  
66      /**
67       * This is the default implementation which ensures that document note attachment references are loaded.
68       * 
69       * @see org.kuali.rice.krad.document.Document#processAfterRetrieve()
70       */
71      @Override
72      public void processAfterRetrieve() {
73          // set correctedByDocumentId manually, since OJB doesn't maintain that relationship
74          try {
75              DocumentHeader correctingDocumentHeader = SpringContext.getBean(FinancialSystemDocumentHeaderDao.class).getCorrectingDocumentHeader(getDocumentHeader().getWorkflowDocument().getDocumentId());
76              if (correctingDocumentHeader != null) {
77                  getFinancialSystemDocumentHeader().setCorrectedByDocumentId(correctingDocumentHeader.getDocumentNumber());
78              }
79          } catch (RuntimeException e) {
80              LOG.error("Received WorkflowException trying to get route header id from workflow document");
81              throw new WorkflowRuntimeException(e);
82          }
83          // set the ad hoc route recipients too, since OJB doesn't maintain that relationship
84          // TODO - see KULNRVSYS-1054
85  
86          super.processAfterRetrieve();
87      }
88  
89      /**
90       * This is the default implementation which checks for a different workflow statuses, and updates the Kuali status accordingly.
91       * 
92       * @see org.kuali.rice.krad.document.Document#doRouteStatusChange()
93       */
94      @Override
95      public void doRouteStatusChange(DocumentRouteStatusChange statusChangeEvent) {
96          if (getDocumentHeader().getWorkflowDocument().isCanceled()) {
97              getFinancialSystemDocumentHeader().setFinancialDocumentStatusCode(OLEConstants.DocumentStatusCodes.CANCELLED);
98          }
99          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