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.external.kc.document;
017    
018    import java.util.ArrayList;
019    import java.util.List;
020    import java.util.Map;
021    
022    import org.kuali.kfs.integration.cg.service.AccountCreationService;
023    import org.kuali.kfs.module.external.kc.KcConstants;
024    import org.kuali.kfs.module.external.kc.businessobject.AccountAutoCreateDefaults;
025    import org.kuali.kfs.sys.context.SpringContext;
026    import org.kuali.kfs.sys.document.FinancialSystemMaintainable;
027    import org.kuali.rice.kns.document.MaintenanceDocument;
028    import org.kuali.rice.kns.maintenance.Maintainable;
029    import org.kuali.rice.kns.web.ui.Field;
030    import org.kuali.rice.kns.web.ui.Row;
031    import org.kuali.rice.kns.web.ui.Section;
032    
033    public class AccountAutoCreateDefaultsMaintainableImpl extends FinancialSystemMaintainable {
034    
035        /**
036         * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#getSections(org.kuali.rice.kns.document.MaintenanceDocument,
037         *      org.kuali.rice.kns.maintenance.Maintainable)
038         */
039        @Override
040        public List getSections(MaintenanceDocument document, Maintainable oldMaintainable) {
041            List<Section> sections = super.getSections(document, oldMaintainable);
042            boolean isNew = document.isNew();
043    
044            for (Section section : sections) {
045                for (Row row : section.getRows()) {
046                    List<Field> updatedFields = new ArrayList<Field>();
047                    for (Field field : row.getFields()) {
048                        if (isReadOnly(field, isNew)) 
049                            field.setReadOnly(true);
050                        if (shouldIncludeField(field)) {
051                            updatedFields.add(field);
052                        }
053                        
054                    }
055    
056                    row.setFields(updatedFields);
057                }
058            }
059            return sections;
060        }
061    
062        private boolean isReadOnly(Field field, boolean isNew) {
063            if (KcConstants.AccountCreationDefaults.KcUnit.equals(field.getPropertyName())) {
064                if (!isNew)
065                    return true;
066            }
067            return false;
068        }
069    
070        private boolean shouldIncludeField(Field field) {
071            return true;
072        }
073    
074        /**
075         * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#processAfterCopy(org.kuali.rice.kns.document.MaintenanceDocument,
076         *      java.util.Map)
077         */
078        @Override
079        public void processAfterCopy(MaintenanceDocument document, Map<String, String[]> parameters) {
080            super.processAfterCopy(document, parameters);
081            // clear the pre-tag details when coyping pre-tag information
082            AccountAutoCreateDefaults newAcctAuto = (AccountAutoCreateDefaults) document.getNewMaintainableObject().getBusinessObject();
083            newAcctAuto.setKcUnit("");
084            newAcctAuto.setKcUnitName("");
085        }
086    
087    }