001    /*
002     * Copyright 2011 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     */
016    package org.kuali.kfs.module.endow.document;
017    
018    import java.util.HashMap;
019    import java.util.Map;
020    import java.sql.Date;
021    
022    import org.apache.cxf.common.util.StringUtils;
023    import org.kuali.kfs.module.endow.EndowConstants;
024    import org.kuali.kfs.module.endow.businessobject.EndowmentTransactionSourceType;
025    import org.kuali.kfs.module.endow.businessobject.EndowmentTransactionSubType;
026    import org.kuali.kfs.module.endow.businessobject.PendingTransactionDocumentEntry;
027    import org.kuali.kfs.module.endow.document.service.KEMService;
028    import org.kuali.kfs.sys.context.SpringContext;
029    import org.kuali.kfs.sys.document.FinancialSystemTransactionalDocumentBase;
030    import org.kuali.rice.kew.dto.DocumentRouteStatusChangeDTO;
031    import org.kuali.rice.kew.exception.WorkflowException;
032    import org.kuali.rice.kns.service.BusinessObjectService;
033    import org.kuali.rice.kns.service.DateTimeService;
034    import org.kuali.rice.kns.util.KNSPropertyConstants;
035    import org.kuali.rice.kns.util.ObjectUtils;
036    
037    
038    public abstract class EndowmentTransactionalDocumentBase extends FinancialSystemTransactionalDocumentBase implements EndowmentTransactionalDocument {
039        protected static final String CHECK_IF_ROUTE_SPLIT = "CheckIfNoRoute";
040        protected String transactionSubTypeCode;
041        protected String transactionSourceTypeCode;
042        protected boolean transactionPosted;
043    
044        protected EndowmentTransactionSubType transactionSubType;
045        protected EndowmentTransactionSourceType transactionSourceType;
046        
047        protected static transient BusinessObjectService businessObjectService;
048        protected static transient DateTimeService dateTimeService;
049    
050        protected boolean noRouteIndicator;
051    
052        /**
053         * Constructs a EndowmentTransactionalDocumentBase.java.
054         */
055        public EndowmentTransactionalDocumentBase() {
056            super();
057            this.transactionPosted = false;
058            
059            //set noRouteIndicator = false by default to make sure when a user initiates 
060            //the eDoc manually through UI, it goes through the routing path defined in 
061            //the workflow xml file.
062            this.noRouteIndicator= false;
063            this.setTransactionSourceTypeCode(EndowConstants.TransactionSourceTypeCode.MANUAL);
064            initializeSourceTypeObj();        
065        }
066    
067        /**
068         * Constructs used by creating a recurring or automated eDoc
069         */
070        public EndowmentTransactionalDocumentBase(String transactionSourceTypeCode) {
071            super();
072            this.transactionPosted = false;
073            this.setTransactionSourceTypeCode(transactionSourceTypeCode);
074            initializeSourceTypeObj();
075        }
076    
077        /**
078         * This method fills source type code for UI on Initial request.
079         */
080        protected void initializeSourceTypeObj() {
081            EndowmentTransactionSourceType endowmentTransactionSourceType = getBusinessObjectService().findBySinglePrimaryKey(EndowmentTransactionSourceType.class, this.getTransactionSourceTypeCode());
082            this.setTransactionSourceType(endowmentTransactionSourceType);
083        }
084    
085        /**
086         * This method fills sub type code for UI on Initial request.
087         */
088        protected void initializeSubType() {
089            EndowmentTransactionSubType endowmentTransactionSubType = getBusinessObjectService().findBySinglePrimaryKey(EndowmentTransactionSubType.class, this.getTransactionSubTypeCode());
090            setTransactionSubType(endowmentTransactionSubType);
091        }
092    
093        /**
094         * @see org.kuali.kfs.module.endow.document.EndowmentTransactionalDocument#getTransactionSubTypeCode()
095         */
096        public String getTransactionSubTypeCode() {
097            return transactionSubTypeCode;
098        }
099    
100        /**
101         * @see org.kuali.kfs.module.endow.document.EndowmentTransactionalDocument#setTransactionSubTypeCode(java.lang.String)
102         */
103        public void setTransactionSubTypeCode(String transactionSubTypeCode) {
104            this.transactionSubTypeCode = transactionSubTypeCode;
105        }
106    
107        /**
108         * @see org.kuali.kfs.module.endow.document.EndowmentTransactionalDocument#getTransactionSourceTypeCode()
109         */
110        public String getTransactionSourceTypeCode() {
111            return transactionSourceTypeCode;
112        }
113    
114        /**
115         * @see org.kuali.kfs.module.endow.document.EndowmentTransactionalDocument#setTransactionSourceTypeCode(java.lang.String)
116         */
117        public void setTransactionSourceTypeCode(String transactionSourceTypeCode) {
118            this.transactionSourceTypeCode = transactionSourceTypeCode;
119        }
120    
121        /**
122         * @see org.kuali.kfs.module.endow.document.EndowmentTransactionalDocument#isTransactionPosted()
123         */
124        public boolean isTransactionPosted() {
125            return transactionPosted;
126        }
127    
128        /**
129         * @see org.kuali.kfs.module.endow.document.EndowmentTransactionalDocument#setTransactionPosted(boolean)
130         */
131        public void setTransactionPosted(boolean transactionPosted) {
132            this.transactionPosted = transactionPosted;
133        }
134    
135        /**
136         * Gets the transactionSubType.
137         * 
138         * @return transactionSubType
139         */
140        public EndowmentTransactionSubType getTransactionSubType() {
141            return transactionSubType;
142        }
143    
144        /**
145         * Sets the transactionSubType.
146         * 
147         * @param transactionSubType
148         */
149        public void setTransactionSubType(EndowmentTransactionSubType transactionSubType) {
150            this.transactionSubType = transactionSubType;
151        }
152    
153        /**
154         * Gets the transactionSourceType.
155         * 
156         * @return transactionSourceType
157         */
158        public EndowmentTransactionSourceType getTransactionSourceType() {
159            return transactionSourceType;
160        }
161    
162        /**
163         * Sets the transactionSourceType.
164         * 
165         * @param transactionSourceType
166         */
167        public void setTransactionSourceType(EndowmentTransactionSourceType transactionSourceType) {
168            this.transactionSourceType = transactionSourceType;
169        }
170        
171        /**
172         * Gets the noRouteIndicator.
173         * 
174         * @return noRouteIndicator
175         */
176        public boolean getNoRouteIndicator(){
177            return noRouteIndicator;
178        }
179        
180        /**
181         * Sets the noRouteIndicator.
182         * 
183         * @param noRouteIndicator
184         */
185        public void setNoRouteIndicator(boolean noRouteIndicator){
186            this.noRouteIndicator = noRouteIndicator;
187        }        
188    
189        /**
190         * @see org.kuali.kfs.sys.document.Correctable#toErrorCorrection()
191         */
192        public void toErrorCorrection() throws WorkflowException, IllegalStateException {
193            super.toErrorCorrection();
194    
195            // Reset All the Version numbers to 1
196            try {
197                ObjectUtils.setObjectPropertyDeep(this, KNSPropertyConstants.VERSION_NUMBER, versionNumber.getClass(), 0L);
198            }
199            catch (Exception e) {
200                LOG.error("Unable to set version number property in copied document " + e.getMessage());
201                throw new RuntimeException("Unable to set version number property in copied document " + e.getMessage());
202            }
203        }
204    
205        /**
206         * @see org.kuali.kfs.module.endow.document.EndowmentTransactionalDocument#isErrorCorrectedDocument()
207         */
208        public boolean isErrorCorrectedDocument() {
209            if (StringUtils.isEmpty(getDocumentHeader().getFinancialDocumentInErrorNumber()))
210                return false;
211            else
212                return true;
213        }
214        
215        /**
216         * When document is processed or in the final status, create an PendingTransactionDocumentEntry object
217         * and persist documentId, documentType and the approved date to END_PENDING_TRAN_DOC_T.
218         *
219         * @see org.kuali.kfs.sys.document.FinancialSystemTransactionalDocumentBase#doRouteStatusChange()
220         */
221        @Override
222        public void doRouteStatusChange(DocumentRouteStatusChangeDTO statusChangeEvent){
223            super.doRouteStatusChange(statusChangeEvent);        
224              
225            if (getDocumentHeader().getWorkflowDocument().stateIsProcessed()) {
226                
227                String documentId = getDocumentHeader().getDocumentNumber();
228                String documentType = getDocumentHeader().getWorkflowDocument().getDocumentType();
229                Date approvedDate =  getDateTimeService().getCurrentSqlDate();
230            
231                //persist documentId, documentType and the approved date to END_PENDING_TRAN_DOC_T 
232                PendingTransactionDocumentEntry entry = new PendingTransactionDocumentEntry();
233                entry.setDocumentNumber(documentId);
234                entry.setDocumentType(documentType);
235                entry.setApprovedDate(approvedDate);
236                
237                getBusinessObjectService().save(entry);    
238            }
239        }       
240       
241        public boolean answerSplitNodeQuestion(String nodeName) throws UnsupportedOperationException {
242            if (nodeName.equals(this.CHECK_IF_ROUTE_SPLIT))
243                return this.noRouteIndicator;
244            throw new UnsupportedOperationException("Cannot answer split question for this node you call \""+nodeName+"\"");
245        }
246    
247        protected BusinessObjectService getBusinessObjectService() {
248            if ( businessObjectService == null ) {
249                businessObjectService = SpringContext.getBean(BusinessObjectService.class);
250            }
251            return businessObjectService;
252        }
253    
254        public DateTimeService getDateTimeService() {
255            if ( dateTimeService == null ) {
256                dateTimeService = SpringContext.getBean(DateTimeService.class);
257            }
258            return dateTimeService;
259        }
260        
261    
262    }