001/*
002 * Copyright 2007 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.pdp.document;
017
018import java.util.List;
019import java.util.Map;
020
021import org.apache.commons.lang.StringUtils;
022import org.kuali.ole.pdp.PdpPropertyConstants;
023import org.kuali.ole.pdp.businessobject.CustomerProfile;
024import org.kuali.ole.sys.document.FinancialSystemMaintainable;
025import org.kuali.rice.kns.document.MaintenanceDocument;
026import org.kuali.rice.kns.maintenance.Maintainable;
027import org.kuali.rice.kns.web.ui.Field;
028import org.kuali.rice.kns.web.ui.Row;
029import org.kuali.rice.kns.web.ui.Section;
030import org.kuali.rice.krad.util.ObjectUtils;
031
032/**
033 * This class is a special implementation of Maintainable specifically for Account Delegates. It was created to correctly update the
034 * default Start Date on edits and copies, ala JIRA #KULRNE-62.
035 */
036public class CustomerProfileMaintenanceDocumentMaintainableImpl extends FinancialSystemMaintainable {
037    private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(CustomerProfileMaintenanceDocumentMaintainableImpl.class);
038
039    /**
040     * This method will reset AccountDelegate's Start Date to the current timestamp on edits and copies
041     * 
042     * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#processAfterRetrieve()
043     */
044    @Override
045    public void processAfterCopy( MaintenanceDocument document, Map<String,String[]> parameters ) {
046        
047        super.processAfterCopy( document, parameters );
048        CustomerProfile customerProfile = (CustomerProfile) document.getNewMaintainableObject().getBusinessObject();
049        customerProfile.setChartCode(null);
050        customerProfile.setUnitCode(null); 
051        customerProfile.setSubUnitCode(null);
052    }
053    
054    /**
055     * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#refresh(java.lang.String, java.util.Map,
056     *      org.kuali.rice.kns.document.MaintenanceDocument)
057     * 
058     * KRAD Conversion: Performs customization of the sections making fields read only.
059     * 
060     * No use of data dictionary.
061     */
062    @Override
063    public List getSections(MaintenanceDocument document, Maintainable oldMaintainable) {
064        List<Section> sections = super.getSections(document, oldMaintainable);
065        //If oldMaintainable is null, it means we are trying to get sections for the old part
066        //If oldMaintainable is not null, it means we are trying to get sections for the new part
067        //Refer to KualiMaintenanceForm lines 288-294
068        CustomerProfile customerProfile = (CustomerProfile) document.getNewMaintainableObject().getBusinessObject();
069        if(oldMaintainable==null) {
070            return sections;
071        }
072        if (shouldReviewTypesFieldBeReadOnly(document) == false) {
073            return sections;
074        }
075
076        for (Section section : sections) {
077            for (Row row : section.getRows()) {
078                for (Field field : row.getFields()) {
079                    if (PdpPropertyConstants.CustomerProfile.CUSTOMER_PROFILE_CHART_CODE.equals(field.getPropertyName())) {
080                        field.setReadOnly(true);
081                     }
082                    if (PdpPropertyConstants.CustomerProfile.CUSTOMER_PROFILE_UNIT_CODE.equals(field.getPropertyName())) {
083                         field.setReadOnly(true);
084                    }
085                    if (PdpPropertyConstants.CustomerProfile.CUSTOMER_PROFILE_SUB_UNIT_CODE.equals(field.getPropertyName())) {
086                        field.setReadOnly(true);
087                    }
088                }
089            }
090        }
091        return sections;
092    }
093
094
095    protected boolean shouldReviewTypesFieldBeReadOnly(MaintenanceDocument document){
096        CustomerProfile  customerProfile = (CustomerProfile)document.getNewMaintainableObject().getBusinessObject();
097        if(StringUtils.isEmpty(customerProfile.getChartCode())) {
098            return false;
099        }
100        if(StringUtils.isEmpty(customerProfile.getSubUnitCode())) {
101            return false;
102        }
103        if(StringUtils.isEmpty(customerProfile.getUnitCode())) {
104            return false;
105        }
106        return true;
107    }
108
109    /**
110     * @see org.kuali.ole.sys.document.FinancialSystemMaintainable#processAfterPost(org.kuali.rice.kns.document.MaintenanceDocument, java.util.Map)
111     */
112    @Override
113    public void processAfterPost(MaintenanceDocument document, Map<String, String[]> parameters) {
114        CustomerProfile  customerProfile = (CustomerProfile)document.getNewMaintainableObject().getBusinessObject();
115
116       if (ObjectUtils.isNull(customerProfile.getDefaultSubAccountNumber())) {
117        customerProfile.setDefaultSubAccountNumber(PdpPropertyConstants.CustomerProfile.CUSTOMER_DEFAULT_SUB_ACCOUNT_NUMBER);
118    }
119       if (ObjectUtils.isNull(customerProfile.getDefaultSubObjectCode())) {
120        customerProfile.setDefaultSubObjectCode(PdpPropertyConstants.CustomerProfile.CUSTOMER_DEFAULT_SUB_OBJECT_CODE);
121    }
122        super.processAfterPost(document, parameters);
123    }
124}