View Javadoc
1   /*
2    * The Kuali Financial System, a comprehensive financial management system for higher education.
3    *
4    * Copyright 2005-2014 The Kuali Foundation
5    *
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   *
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.kuali.kfs.module.external.kc.service.impl;
20  
21  import java.net.MalformedURLException;
22  import java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.Iterator;
25  import java.util.List;
26  import java.util.Map;
27  
28  import javax.xml.ws.WebServiceException;
29  
30  import org.kuali.kfs.integration.cg.ContractsAndGrantsCfda;
31  import org.kuali.kfs.integration.cg.dto.HashMapElement;
32  import org.kuali.kfs.module.external.kc.KcConstants;
33  import org.kuali.kfs.module.external.kc.businessobject.CfdaDTO;
34  import org.kuali.kfs.module.external.kc.service.ExternalizableLookupableBusinessObjectService;
35  import org.kuali.kfs.module.external.kc.util.GlobalVariablesExtractHelper;
36  import org.kuali.kfs.module.external.kc.webService.CfdaNumberSoapService;
37  import org.kuali.kfs.sys.KFSConstants;
38  import org.kuali.kra.external.Cfda.service.CfdaNumberService;
39  import org.kuali.rice.core.api.config.property.ConfigurationService;
40  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
41  import org.kuali.rice.krad.bo.BusinessObject;
42  import org.kuali.rice.krad.bo.ExternalizableBusinessObject;
43  
44  public class CfdaServiceImpl implements ExternalizableLookupableBusinessObjectService {
45      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(CfdaServiceImpl.class);
46  
47      protected ConfigurationService configurationService;
48  
49      protected CfdaNumberService getWebService() {
50          // first attempt to get the service from the KSB - works when KFS & KC share a Rice instance
51          CfdaNumberService cfdaNumberService = (CfdaNumberService) GlobalResourceLoader.getService(KcConstants.Cfda.SERVICE);
52  
53          // if we couldn't get the service from the KSB, get as web service - for when KFS & KC have separate Rice instances
54          if (cfdaNumberService == null) {
55              LOG.warn("Couldn't get CfdaNumberService from KSB, setting it up as SOAP web service - expected behavior for bundled Rice, but not when KFS & KC share a standalone Rice instance.");
56              CfdaNumberSoapService soapService = null;
57              try {
58                  soapService = new CfdaNumberSoapService();
59              }
60              catch (MalformedURLException ex) {
61                  LOG.error("Could not intialize CfdaNumberSoapService: " + ex.getMessage());
62                  throw new RuntimeException("Could not intialize CfdaNumberSoapService: " + ex.getMessage());
63              }
64              cfdaNumberService = soapService.getCfdaNumberServicePort();
65          }
66          return cfdaNumberService;
67      }
68  
69      @Override
70      public ExternalizableBusinessObject findByPrimaryKey(Map primaryKeys) {
71  
72          Collection cfda = findMatching(primaryKeys);
73  
74          if (cfda != null && cfda.iterator().hasNext()) {
75              return (ContractsAndGrantsCfda) cfda.iterator().next();
76          }
77          else {
78              return null;
79          }
80      }
81  
82      @Override
83      public Collection findMatching(Map fieldValues) {
84          List<CfdaDTO> cfdas = null;
85          java.util.List<HashMapElement> hashMapList = new ArrayList<HashMapElement>();
86  
87          for (Iterator i = fieldValues.entrySet().iterator(); i.hasNext();) {
88              Map.Entry e = (Map.Entry) i.next();
89  
90              String key = (String) e.getKey();
91              String val = (String) e.getValue();
92  
93              if (KcConstants.Cfda.KC_ALLOWABLE_CRITERIA_PARAMETERS.contains(key) && (val.length() > 0)) {
94                  HashMapElement hashMapElement = new HashMapElement();
95                  hashMapElement.setKey(key);
96                  hashMapElement.setValue(val);
97                  hashMapList.add(hashMapElement);
98              }
99          }
100 
101         try {
102             cfdas = getWebService().lookupCfda(hashMapList);
103         }
104         catch (WebServiceException ex) {
105             LOG.error("Could not retrieve cfda: "+ ex.getMessage());
106             GlobalVariablesExtractHelper.insertError(KcConstants.WEBSERVICE_UNREACHABLE, getConfigurationService().getPropertyValueAsString(KFSConstants.KC_APPLICATION_URL_KEY));
107         }
108 
109         if (cfdas == null) {
110             cfdas = new ArrayList();
111         }
112 
113         return cfdas;
114     }
115 
116     @Override
117     public List<? extends BusinessObject> getSearchResults(Map<String, String> fieldValues) {
118         return new ArrayList(findMatching(fieldValues));
119     }
120 
121     public ConfigurationService getConfigurationService() {
122         return configurationService;
123     }
124 
125     public void setConfigurationService(ConfigurationService configurationService) {
126         this.configurationService = configurationService;
127     }
128 
129 }