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.authorization;
017    
018    import java.util.List;
019    import java.util.Set;
020    
021    import org.apache.commons.lang.StringUtils;
022    import org.kuali.kfs.module.endow.EndowConstants;
023    import org.kuali.kfs.module.endow.EndowPropertyConstants;
024    import org.kuali.kfs.module.endow.businessobject.FeeClassCode;
025    import org.kuali.kfs.module.endow.businessobject.FeeEndowmentTransactionCode;
026    import org.kuali.kfs.module.endow.businessobject.FeeMethod;
027    import org.kuali.kfs.module.endow.businessobject.FeePaymentType;
028    import org.kuali.kfs.module.endow.businessobject.FeeSecurity;
029    import org.kuali.kfs.module.endow.businessobject.FeeTransaction;
030    import org.kuali.kfs.module.endow.document.service.impl.FrequencyCodeServiceImpl;
031    import org.kuali.kfs.sys.context.SpringContext;
032    import org.kuali.kfs.sys.document.authorization.FinancialSystemMaintenanceDocumentPresentationControllerBase;
033    import org.kuali.rice.kns.datadictionary.MaintainableSectionDefinition;
034    import org.kuali.rice.kns.document.MaintenanceDocument;
035    import org.kuali.rice.kns.service.MaintenanceDocumentDictionaryService;
036    
037    public class FeeMethodDocumentPresentationControllerForTabShowOrHide extends FinancialSystemMaintenanceDocumentPresentationControllerBase {
038    
039        @Override
040        public Set<String> getConditionallyReadOnlyPropertyNames(MaintenanceDocument document) {
041            Set<String> readOnlyPropertyNames = super.getConditionallyReadOnlyPropertyNames(document);
042            
043            FeeMethod feeMethod = (FeeMethod) document.getNewMaintainableObject().getBusinessObject();
044            
045            String frequencyCode = feeMethod.getFeeFrequencyCode();
046            if (StringUtils.isNotEmpty(frequencyCode)) {
047                FrequencyCodeServiceImpl frequencyCodeServiceImpl = (FrequencyCodeServiceImpl) SpringContext.getBean(FrequencyCodeServiceImpl.class);
048                feeMethod.setFeeNextProcessDate(frequencyCodeServiceImpl.calculateProcessDate(frequencyCode));
049            }
050            
051            String feeTypeCode = feeMethod.getFeeTypeCode();
052            
053            if (EndowConstants.FeeMethod.FEE_TYPE_CODE_VALUE_FOR_PAYMENTS.equalsIgnoreCase(feeTypeCode)) {
054                // rule# 8:  If fee type code is "P" then fee base code should be "I"
055                feeMethod.setFeeBaseCode(EndowConstants.FeeMethod.FEE_BASE_CD_VALUE);
056                readOnlyPropertyNames.add(EndowPropertyConstants.FEE_BASE_CD);
057            }
058            
059            return readOnlyPropertyNames;
060        }
061        
062        /**
063         * @see org.kuali.rice.kns.document.authorization.MaintenanceDocumentPresentationControllerBase#getConditionallyReadOnlyPropertyNames(org.kuali.rice.kns.document.MaintenanceDocument)
064         */
065        @Override
066        public Set<String> getConditionallyReadOnlySectionIds(MaintenanceDocument document) {
067            Set<String> readOnlySectionIds = super.getConditionallyReadOnlySectionIds(document);
068        
069            // make all the tabs read only to begin with
070            readOnlySectionIds.add(EndowConstants.FeeMethod.CLASS_CODES_TAB_ID);
071            readOnlySectionIds.add(EndowConstants.FeeMethod.SECURITY_TAB_ID);
072            readOnlySectionIds.add(EndowConstants.FeeMethod.PAYMENT_TYPES_TAB_ID);
073            readOnlySectionIds.add(EndowConstants.FeeMethod.TRANSACTION_TYPES_TAB_ID);
074            readOnlySectionIds.add(EndowConstants.FeeMethod.ENDOWMENT_TRANSACTION_CODES_TAB_ID);
075    
076            FeeMethod feeMethod = (FeeMethod) document.getNewMaintainableObject().getBusinessObject();
077            FeeMethod oldFeeMethod = (FeeMethod) document.getOldMaintainableObject().getBusinessObject();
078            
079            String documentTypeName = SpringContext.getBean(MaintenanceDocumentDictionaryService.class).getDocumentTypeName(feeMethod.getClass());
080            List<MaintainableSectionDefinition> sectionDefinitions = getMaintenanceDocumentDictionaryService().getMaintainableSections(documentTypeName);
081    
082            for (MaintainableSectionDefinition sectionDefinition : sectionDefinitions) {
083                String sectionId = sectionDefinition.getId();
084                if (EndowConstants.FeeMethod.FEE_METHOD_TAB_ID.equalsIgnoreCase(sectionId)) {
085                    sectionDefinition.setDefaultOpen(true); 
086                }
087                else {
088                    sectionDefinition.setDefaultOpen(false);
089                }
090            }
091            
092            String feeTypeCode = feeMethod.getFeeTypeCode();
093            
094            if (StringUtils.isEmpty(feeTypeCode)) {
095                return readOnlySectionIds;
096            }
097            
098            // if Fee Type Code is T then valid tabs are Transaction Type and Endowment Transaction Type
099            //  read only the rest of the tabs. Also clear the old collection records
100            // also clear the read only tables collection records as they are no longer valid...
101            if (EndowConstants.FeeMethod.FEE_TYPE_CODE_VALUE_FOR_TRANSACTIONS.equalsIgnoreCase(feeTypeCode)) {
102                readOnlySectionIds.remove(EndowConstants.FeeMethod.TRANSACTION_TYPES_TAB_ID);
103                readOnlySectionIds.remove(EndowConstants.FeeMethod.ENDOWMENT_TRANSACTION_CODES_TAB_ID);
104                
105                List<FeeClassCode> feeClassCodes = (List<FeeClassCode>) feeMethod.getFeeClassCodes();
106                feeClassCodes.clear();
107                List<FeeClassCode> oldFeeClassCodes = (List<FeeClassCode>) oldFeeMethod.getFeeClassCodes();
108                oldFeeClassCodes.clear();
109    
110                
111                List<FeeSecurity> feeSecurity = (List<FeeSecurity>) feeMethod.getFeeSecurity();
112                feeSecurity.clear();
113                List<FeeSecurity> olFeeSecurity = (List<FeeSecurity>) oldFeeMethod.getFeeSecurity();
114                olFeeSecurity.clear();
115                
116                List<FeePaymentType> feePaymentTypes = (List<FeePaymentType>) feeMethod.getFeePaymentTypes();
117                feePaymentTypes.clear();
118                List<FeePaymentType> oldFeePaymentTypes = (List<FeePaymentType>) oldFeeMethod.getFeePaymentTypes();
119                oldFeePaymentTypes.clear();
120                
121                for (MaintainableSectionDefinition sectionDefinition : sectionDefinitions) {
122                    String sectionId = sectionDefinition.getId();
123                    if (EndowConstants.FeeMethod.CLASS_CODES_TAB_ID.equalsIgnoreCase(sectionId) ||
124                        EndowConstants.FeeMethod.SECURITY_TAB_ID.equalsIgnoreCase(sectionId) ||
125                        EndowConstants.FeeMethod.PAYMENT_TYPES_TAB_ID.equalsIgnoreCase(sectionId)) {
126                        sectionDefinition.setDefaultOpen(false); 
127                    }
128                    else {
129                        sectionDefinition.setDefaultOpen(true);                    
130                    }
131                }
132    
133                return readOnlySectionIds;            
134            }
135            
136            // if Fee Type Code is B then tabs Class Codes and Security are valid. Read only the rest of the tabs...
137            // Clear the old collection records and also clear the read only tables collection records as they are not longer valid...
138            if (EndowConstants.FeeMethod.FEE_TYPE_CODE_VALUE_FOR_BALANCES.equalsIgnoreCase(feeTypeCode)) {
139                readOnlySectionIds.remove(EndowConstants.FeeMethod.CLASS_CODES_TAB_ID);
140                readOnlySectionIds.remove(EndowConstants.FeeMethod.SECURITY_TAB_ID);
141                
142                List<FeePaymentType> feePaymentTypes = (List<FeePaymentType>) feeMethod.getFeePaymentTypes();
143                feePaymentTypes.clear();
144                List<FeePaymentType> oldFeePaymentTypes = (List<FeePaymentType>) oldFeeMethod.getFeePaymentTypes();
145                oldFeePaymentTypes.clear();
146                
147                List<FeeTransaction> feeTransactions = (List<FeeTransaction>) feeMethod.getFeeTransactions();
148                feeTransactions.clear();
149                List<FeeTransaction> oldFeeTransactions = (List<FeeTransaction>) oldFeeMethod.getFeeTransactions();
150                oldFeeTransactions.clear();
151                
152                List<FeeEndowmentTransactionCode> feeEndowmentTransactionCodes = (List<FeeEndowmentTransactionCode>) feeMethod.getFeeEndowmentTransactionCodes();
153                feeEndowmentTransactionCodes.clear();
154                List<FeeEndowmentTransactionCode> oldFeeEndowmentTransactionCodes = (List<FeeEndowmentTransactionCode>) oldFeeMethod.getFeeEndowmentTransactionCodes();
155                oldFeeEndowmentTransactionCodes.clear();
156                
157                for (MaintainableSectionDefinition sectionDefinition : sectionDefinitions) {
158                    String sectionId = sectionDefinition.getId();
159                    if (EndowConstants.FeeMethod.TRANSACTION_TYPES_TAB_ID.equalsIgnoreCase(sectionId) ||
160                        EndowConstants.FeeMethod.PAYMENT_TYPES_TAB_ID.equalsIgnoreCase(sectionId) ||
161                        EndowConstants.FeeMethod.ENDOWMENT_TRANSACTION_CODES_TAB_ID.equalsIgnoreCase(sectionId)) {
162                        sectionDefinition.setDefaultOpen(false); 
163                    }
164                    else {
165                        sectionDefinition.setDefaultOpen(true);                    
166                    }
167                }
168                
169                return readOnlySectionIds;            
170            }
171            
172            // if Fee Type Code is P then only Payment Type tab is valid
173            // also clear the read only tables collection records as they are not longer valid...
174            // also clear the old collection records
175            //  read only the rest of the tabs...
176            if (EndowConstants.FeeMethod.FEE_TYPE_CODE_VALUE_FOR_PAYMENTS.equalsIgnoreCase(feeTypeCode)) {
177                readOnlySectionIds.remove(EndowConstants.FeeMethod.PAYMENT_TYPES_TAB_ID);
178                
179                List<FeeClassCode> feeClassCodes = (List<FeeClassCode>) feeMethod.getFeeClassCodes();
180                feeClassCodes.clear();
181                List<FeeClassCode> oldFeeClassCodes = (List<FeeClassCode>) oldFeeMethod.getFeeClassCodes();
182                oldFeeClassCodes.clear();
183    
184                List<FeeSecurity> feeSecurity = (List<FeeSecurity>) feeMethod.getFeeSecurity();
185                feeSecurity.clear();
186                List<FeeSecurity> olFeeSecurity = (List<FeeSecurity>) oldFeeMethod.getFeeSecurity();
187                olFeeSecurity.clear();
188                
189                List<FeeTransaction> feeTransactions = (List<FeeTransaction>) feeMethod.getFeeTransactions();
190                feeTransactions.clear();
191                List<FeeTransaction> oldFeeTransactions = (List<FeeTransaction>) oldFeeMethod.getFeeTransactions();
192                oldFeeTransactions.clear();
193                   
194                List<FeeEndowmentTransactionCode> feeEndowmentTransactionCodes = (List<FeeEndowmentTransactionCode>) feeMethod.getFeeEndowmentTransactionCodes();
195                feeEndowmentTransactionCodes.clear();
196                List<FeeEndowmentTransactionCode> oldFeeEndowmentTransactionCodes = (List<FeeEndowmentTransactionCode>) oldFeeMethod.getFeeEndowmentTransactionCodes();
197                oldFeeEndowmentTransactionCodes.clear();
198                
199                for (MaintainableSectionDefinition sectionDefinition : sectionDefinitions) {
200                    String sectionId = sectionDefinition.getId();
201                    if (EndowConstants.FeeMethod.CLASS_CODES_TAB_ID.equalsIgnoreCase(sectionId) ||
202                        EndowConstants.FeeMethod.SECURITY_TAB_ID.equalsIgnoreCase(sectionId) ||
203                        EndowConstants.FeeMethod.TRANSACTION_TYPES_TAB_ID.equalsIgnoreCase(sectionId) || 
204                        EndowConstants.FeeMethod.ENDOWMENT_TRANSACTION_CODES_TAB_ID.equalsIgnoreCase(sectionId)) {
205                        sectionDefinition.setDefaultOpen(false);                    
206                    }
207                    else {
208                        sectionDefinition.setDefaultOpen(true);                    
209                    }
210                }
211                
212                return readOnlySectionIds;            
213            }
214    
215            return readOnlySectionIds;
216        }
217    }