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.service.impl;
017    
018    import java.net.MalformedURLException;
019    import java.util.ArrayList;
020    import java.util.Collection;
021    import java.util.Collections;
022    import java.util.Comparator;
023    import java.util.HashMap;
024    import java.util.Iterator;
025    import java.util.List;
026    import java.util.Map;
027    
028    import javax.xml.ws.WebServiceException;
029    
030    import org.kuali.kfs.integration.cg.ContractsAndGrantsCfda;
031    import org.kuali.kfs.integration.cg.dto.HashMapElement;
032    import org.kuali.kfs.module.external.kc.KcConstants;
033    import org.kuali.kfs.module.external.kc.businessobject.AwardAccountDTO;
034    import org.kuali.kfs.module.external.kc.businessobject.Cfda;
035    import org.kuali.kfs.module.external.kc.service.ExternalizableBusinessObjectService;
036    import org.kuali.kfs.module.external.kc.service.KfsService;
037    import org.kuali.kfs.module.external.kc.util.GlobalVariablesExtractHelper;
038    import org.kuali.kfs.module.external.kc.webService.CfdaNumberService;
039    import org.kuali.kfs.module.external.kc.webService.CfdaNumberSoapService;
040    import org.kuali.rice.kns.bo.ExternalizableBusinessObject;
041    import org.kuali.rice.kns.util.ObjectUtils;
042    
043    public class CfdaServiceImpl implements ExternalizableBusinessObjectService {
044        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(CfdaServiceImpl.class);
045    
046        protected CfdaNumberService getWebService() {
047            CfdaNumberSoapService soapService = null;
048            try {
049                soapService = new CfdaNumberSoapService();
050            }
051            catch (MalformedURLException ex) {
052                LOG.error("Could not intialize CfdaSoapService: " + ex.getMessage());
053                throw new RuntimeException("Could not intialize CfdaSoapService: " + ex.getMessage());
054            }
055            CfdaNumberService port = soapService.getCfdaNumberServicePort();
056            return port;
057        }
058    
059        public ExternalizableBusinessObject findByPrimaryKey(Map primaryKeys) {
060    
061            Collection Cfda = findMatching(primaryKeys);
062    
063            if (Cfda != null && Cfda.iterator().hasNext())
064                return (ContractsAndGrantsCfda) Cfda.iterator().next();
065            else
066                return null;
067        }
068    
069        public Collection findMatching(Map fieldValues) {
070            List<Cfda> cfdas = null;
071            java.util.List<HashMapElement> hashMapList = new ArrayList<HashMapElement>();
072    
073            for (Iterator i = fieldValues.entrySet().iterator(); i.hasNext();) {
074                Map.Entry e = (Map.Entry) i.next();
075    
076                String key = (String) e.getKey();
077                String val = (String) e.getValue();
078    
079                if (KcConstants.Cfda.KC_ALLOWABLE_CRITERIA_PARAMETERS.contains(key) && (val.length() > 0)) {
080                    HashMapElement hashMapElement = new HashMapElement();
081                    hashMapElement.setKey(key);
082                    hashMapElement.setValue(val);
083                    hashMapList.add(hashMapElement);
084                }
085            }
086    
087            try {
088                cfdas = getWebService().lookupCfda(hashMapList);
089            }
090            catch (WebServiceException ex) {
091                LOG.error("Could not retrieve cfda: "+ ex.getMessage());
092                GlobalVariablesExtractHelper.insertError(KcConstants.WEBSERVICE_UNREACHABLE, KfsService.getWebServiceServerName());
093            }
094    
095            if (cfdas == null) cfdas = new ArrayList();
096            
097            return cfdas;
098        }
099    
100    }