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.businessobject;
17  
18  import java.sql.Date;
19  
20  import org.apache.commons.lang.StringUtils;
21  import org.kuali.ole.sys.OLEConstants;
22  import org.kuali.ole.sys.context.SpringContext;
23  import org.kuali.rice.core.api.util.type.KualiDecimal;
24  import org.kuali.rice.kew.api.WorkflowDocument;
25  import org.kuali.rice.kew.api.exception.WorkflowException;
26  import org.kuali.rice.krad.bo.DocumentHeader;
27  import org.kuali.rice.krad.util.GlobalVariables;
28  import org.kuali.rice.krad.workflow.service.WorkflowDocumentService;
29  
30  /**
31   * This class is a custom {@link DocumentHeader} class used by OLE to facilitate custom data fields and a few UI fields
32   */
33  public class FinancialSystemDocumentHeader extends DocumentHeader {
34  
35      protected KualiDecimal financialDocumentTotalAmount;
36      protected String correctedByDocumentId;
37      protected String financialDocumentInErrorNumber;
38      protected String financialDocumentStatusCode;
39  
40      /**
41       * Constructor - creates empty instances of dependent objects
42       *
43       */
44      public FinancialSystemDocumentHeader() {
45          super();
46          financialDocumentStatusCode = OLEConstants.DocumentStatusCodes.INITIATED;
47      }
48  
49      /**
50       * Gets the financialDocumentTotalAmount attribute.
51       * @return Returns the financialDocumentTotalAmount.
52       */
53      public KualiDecimal getFinancialDocumentTotalAmount() {
54          return financialDocumentTotalAmount;
55      }
56  
57      /**
58       * Sets the financialDocumentTotalAmount attribute value.
59       * @param financialDocumentTotalAmount The financialDocumentTotalAmount to set.
60       */
61      public void setFinancialDocumentTotalAmount(KualiDecimal financialDocumentTotalAmount) {
62          this.financialDocumentTotalAmount = financialDocumentTotalAmount;
63      }
64  
65      /**
66       * Gets the correctedByDocumentId attribute.
67       * @return Returns the correctedByDocumentId.
68       */
69      public String getCorrectedByDocumentId() {
70          return correctedByDocumentId;
71      }
72  
73      /**
74       * Sets the correctedByDocumentId attribute value.
75       * @param correctedByDocumentId The correctedByDocumentId to set.
76       */
77      public void setCorrectedByDocumentId(String correctedByDocumentId) {
78          this.correctedByDocumentId = correctedByDocumentId;
79      }
80  
81      /**
82       * Gets the financialDocumentInErrorNumber attribute.
83       * @return Returns the financialDocumentInErrorNumber.
84       */
85      public String getFinancialDocumentInErrorNumber() {
86          return financialDocumentInErrorNumber;
87      }
88  
89      /**
90       * Sets the financialDocumentInErrorNumber attribute value.
91       * @param financialDocumentInErrorNumber The financialDocumentInErrorNumber to set.
92       */
93      public void setFinancialDocumentInErrorNumber(String financialDocumentInErrorNumber) {
94          this.financialDocumentInErrorNumber = financialDocumentInErrorNumber;
95      }
96  
97      /**
98       * Gets the financialDocumentStatusCode attribute.
99       * @return Returns the financialDocumentStatusCode.
100      */
101     public String getFinancialDocumentStatusCode() {
102         return financialDocumentStatusCode;
103     }
104 
105     /**
106      * Sets the financialDocumentStatusCode attribute value.
107      * @param financialDocumentStatusCode The financialDocumentStatusCode to set.
108      */
109     public void setFinancialDocumentStatusCode(String financialDocumentStatusCode) {
110         this.financialDocumentStatusCode = financialDocumentStatusCode;
111     }
112 
113     /**
114      * Gets the documentFinalDate attribute.
115      * @return Returns the documentFinalDate.
116      */
117     public Date getDocumentFinalDate() {
118         WorkflowDocument workflowDoc = this.getWorkflowDocument();
119         if (workflowDoc == null || (workflowDoc.getDateFinalized() == null)) {
120             return null;
121         }
122         return  new java.sql.Date(workflowDoc.getDateFinalized().getMillis());
123     }
124 
125     /**
126      * Gets the applicationDocumentStatus attribute.
127      *
128      * @return Returns the applicationDocumentStatus
129      */
130 
131     public String getApplicationDocumentStatus() {
132         WorkflowDocument document = getWorkflowDocument();
133         return StringUtils.trimToEmpty(document.getApplicationDocumentStatus());
134     }
135 
136     /**
137      * Sets the applicationDocumentStatus attribute.
138      *
139      * @param applicationDocumentStatus The applicationDocumentStatus to set.
140      */
141     public void setApplicationDocumentStatus(String applicationDocumentStatus) {
142         WorkflowDocument document = getWorkflowDocument();
143 
144         document.setApplicationDocumentStatus(applicationDocumentStatus);
145     }
146 
147     /**
148      * method to retrieve the workflow document for the given documentHeader.
149      *
150      * @return workflowDocument
151      */
152     @Override
153     public WorkflowDocument getWorkflowDocument() {
154 
155         WorkflowDocument workflowDocument = null;
156 
157         if (hasWorkflowDocument()) {
158             workflowDocument = super.getWorkflowDocument();
159         }
160 
161         try {
162             if (workflowDocument != null) {
163                 return workflowDocument;
164             }
165             if ( StringUtils.isNotBlank(getDocumentNumber()) ) {
166                 workflowDocument = SpringContext.getBean(WorkflowDocumentService.class).loadWorkflowDocument(getDocumentNumber(), GlobalVariables.getUserSession().getPerson());
167             } else {
168                 throw new RuntimeException("Document number is blank/null.  Unable to load a WorkflowDocument" );
169             }
170         }
171         catch (WorkflowException we) {
172             throw new RuntimeException("Unable to load a WorkflowDocument object for " + getDocumentNumber(), we);
173         }
174 
175         return workflowDocument;
176     }
177 
178     /**
179      * Updates status of this document and saves the workflow data
180      *
181      * @param applicationDocumentStatus is the app doc status to save
182      * @throws WorkflowException
183      */
184     public void updateAndSaveAppDocStatus(String applicationDocumentStatus) throws WorkflowException {
185        setApplicationDocumentStatus(applicationDocumentStatus);
186        // if the document is Initiated, then it has not been saved yet
187        // So, we don't need to save the routing data since the document
188        // has not been submitted to the workflow engine yet
189        if ( !getWorkflowDocument().isInitiated() ) {
190            SpringContext.getBean(WorkflowDocumentService.class).saveRoutingData(getWorkflowDocument());
191        }
192     }
193 
194 }