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.web;
017    
018    import java.util.HashMap;
019    import java.util.Map;
020    
021    import javax.servlet.http.HttpServletRequest;
022    
023    import org.apache.commons.lang.StringUtils;
024    import org.kuali.kfs.coa.businessobject.Organization;
025    import org.kuali.kfs.sys.KFSPropertyConstants;
026    import org.kuali.kfs.sys.context.SpringContext;
027    import org.kuali.rice.kns.bo.PostalCode;
028    import org.kuali.rice.kns.document.MaintenanceDocument;
029    import org.kuali.rice.kns.document.MaintenanceDocumentBase;
030    import org.kuali.rice.kns.service.PostalCodeService;
031    import org.kuali.rice.kns.util.KNSConstants;
032    import org.kuali.rice.kns.util.ObjectUtils;
033    import org.kuali.rice.kns.web.derviedvaluesetter.DerivedValuesSetter;
034    import org.kuali.rice.kns.web.struts.form.KualiForm;
035    import org.kuali.rice.kns.web.struts.form.KualiMaintenanceForm;
036    
037    /**
038     * This is a description of what this class does - wliang don't forget to fill this in. 
039     * 
040     * @author Kuali Rice Team (kuali-rice@googlegroups.com)
041     *
042     */
043    public class OrgDerivedValuesSetter implements DerivedValuesSetter {
044            public void setDerivedValues(KualiForm form, HttpServletRequest request){
045                KualiMaintenanceForm maintenanceForm = (KualiMaintenanceForm) form;
046                MaintenanceDocumentBase maintenanceDocument = (MaintenanceDocumentBase) maintenanceForm.getDocument();
047                Organization newOrg = (Organization) maintenanceDocument.getNewMaintainableObject().getBusinessObject();
048                String organizationZipCode = newOrg.getOrganizationZipCode();
049                String organizationCountryCode = newOrg.getOrganizationCountryCode();
050                if (StringUtils.isNotBlank(organizationZipCode) && StringUtils.isNotBlank(organizationCountryCode)) {
051                    PostalCode postalZipCode = SpringContext.getBean(PostalCodeService.class).getByPrimaryId(organizationCountryCode, organizationZipCode);
052                    if (ObjectUtils.isNotNull(postalZipCode)) {
053                        newOrg.setOrganizationCityName(postalZipCode.getPostalCityName());
054                        newOrg.setOrganizationStateCode(postalZipCode.getPostalStateCode());
055                    }
056                    else {
057                        newOrg.setOrganizationCityName(KNSConstants.EMPTY_STRING);
058                    newOrg.setOrganizationStateCode(KNSConstants.EMPTY_STRING);
059                    }
060                }
061                else {
062                newOrg.setOrganizationCityName(KNSConstants.EMPTY_STRING);
063                newOrg.setOrganizationStateCode(KNSConstants.EMPTY_STRING);
064                }
065            }
066    }