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.coa.document;
017    
018    import java.util.HashMap;
019    import java.util.Iterator;
020    import java.util.List;
021    import java.util.Map;
022    
023    import org.kuali.kfs.sys.KFSPropertyConstants;
024    import org.kuali.kfs.sys.document.FinancialSystemMaintainable;
025    import org.kuali.rice.kns.document.MaintenanceDocument;
026    import org.kuali.rice.kns.maintenance.Maintainable;
027    import org.kuali.rice.kns.web.ui.Field;
028    import org.kuali.rice.kns.web.ui.Row;
029    import org.kuali.rice.kns.web.ui.Section;
030    
031    /**
032     * This class overrides the getCoreSections method to provide specific field conversions for the postal code
033     */
034    public class KualiOrgMaintainable extends FinancialSystemMaintainable {
035    
036        private static final long serialVersionUID = -3182120468758958991L;
037    
038        public static final String KUALI_ORG_SECTION = "Edit Organization Code";
039    
040        /**
041         * Provides special field conversions for the Org.organizationZipCode
042         * 
043         * @see org.kuali.rice.kns.maintenance.Maintainable#getCoreSections(org.kuali.rice.kns.maintenance.Maintainable)
044         */
045        @Override
046        public List getCoreSections(MaintenanceDocument document, Maintainable oldMaintainable) {
047    
048            boolean fieldFound = false;
049            boolean sectionFound = false;
050    
051            String orgPostalCodeFieldName = KFSPropertyConstants.ORGANIZATION_ZIP_CODE;
052    
053            // walk the sections
054            List sections = super.getCoreSections(document, oldMaintainable);
055            for (Iterator sectionIterator = sections.iterator(); sectionIterator.hasNext();) {
056                Section section = (Section) sectionIterator.next();
057    
058                // if this is the section we're looking for
059                if (section.getSectionTitle().equalsIgnoreCase(KUALI_ORG_SECTION)) {
060    
061                    // mark that we found the section
062                    sectionFound = true;
063    
064                    // walk the rows
065                    List rows = section.getRows();
066                    for (Iterator rowIterator = rows.iterator(); rowIterator.hasNext();) {
067                        Row row = (Row) rowIterator.next();
068    
069                        // walk the fields
070                        List fields = row.getFields();
071                        for (Iterator fieldIterator = fields.iterator(); fieldIterator.hasNext();) {
072                            Field field = (Field) fieldIterator.next();
073    
074                            // if this is the field we're looking for ...
075                            if (field.getPropertyName().equalsIgnoreCase(orgPostalCodeFieldName)) {
076    
077                                // mark that we've found the field
078                                fieldFound = true;
079    
080                                // build the fieldConversions for the UserID field lookup
081                                Map fieldConversions = new HashMap();
082                                fieldConversions.put(KFSPropertyConstants.POSTAL_CODE, KFSPropertyConstants.ORGANIZATION_ZIP_CODE);
083                                fieldConversions.put(KFSPropertyConstants.POSTAL_STATE_CODE, KFSPropertyConstants.ORGANIZATION_STATE_CODE);
084                                fieldConversions.put(KFSPropertyConstants.POSTAL_CITY_NAME, KFSPropertyConstants.ORGANIZATION_CITY_NAME);
085    
086    
087                                // add the fieldConversions, lookupParameters and the lookup class
088                                field.setFieldConversions(fieldConversions);
089                                // field.setLookupParameters(lookupParameters);
090                                // field.setQuickFinderClassNameImpl(Person.class.getName());
091                            }
092                        }
093                    }
094                }
095    
096            }
097    
098            // if the section no longer exists, fail loudly
099            if (!sectionFound) {
100                throw new RuntimeException("There is no longer a section titled '" + KUALI_ORG_SECTION + "'. " + "As a result, the lookup setup will not work as expected and the maintenance document " + "will be broken.  The correct name needs to be set in the Constant in this class.");
101            }
102            // if the field was not found, fail loudly
103            else if (!fieldFound) {
104                throw new RuntimeException("There is no longer a field titled '" + KFSPropertyConstants.ORGANIZATION_ZIP_CODE + "'. " + "As a result, the lookup setup will not work as expected and the maintenance document " + "will be broken.  The correct name needs to be set in the KFSPropertyConstants class.");
105            }
106    
107            return sections;
108        }
109    }
110