001/* 002 * Copyright 2012 The Kuali Foundation. 003 * 004 * Licensed under the Educational Community License, Version 1.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/ecl1.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 */ 016package org.kuali.ole.select.service.impl; 017 018import org.apache.commons.lang.StringUtils; 019import org.kuali.ole.select.businessobject.*; 020import org.kuali.ole.select.service.OleUrlResolver; 021import org.kuali.ole.service.impl.OlePatronWebServiceImpl; 022import org.kuali.ole.sys.OLEConstants; 023import org.kuali.ole.sys.context.SpringContext; 024import org.kuali.rice.kim.api.identity.Person; 025import org.kuali.rice.kim.api.identity.PersonService; 026import org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl; 027import org.kuali.rice.krad.bo.BusinessObject; 028 029import java.util.ArrayList; 030import java.util.HashMap; 031import java.util.List; 032import java.util.Map; 033 034public class OlePersonRequestorLookupableServiceImpl extends AbstractLookupableHelperServiceImpl { 035 036 private transient OleUrlResolver oleUrlResolver; 037 private OlePatronRecordHandler olePatronRecordHandler; 038 039 040 public OlePatronRecordHandler getOlePatronRecordHandler() { 041 if (null == olePatronRecordHandler) { 042 olePatronRecordHandler = new OlePatronRecordHandler(); 043 } 044 return olePatronRecordHandler; 045 } 046 047 public void setOlePatronRecordHandler(OlePatronRecordHandler olePatronRecordHandler) { 048 this.olePatronRecordHandler = olePatronRecordHandler; 049 } 050 051 @Override 052 public List<? extends BusinessObject> getSearchResults(Map<String, String> fieldValues) { 053 Map<String, String> personMap = new HashMap<String, String>(); 054 Map<String, String> requestorMap = new HashMap<String, String>(); 055 String personRequestorFirstName = fieldValues.get(OLEConstants.OlePersonRequestorLookupable.FIRST_NAME); 056 String personRequestorLastName = fieldValues.get(OLEConstants.OlePersonRequestorLookupable.LAST_NAME); 057 String personRequestorEmail = fieldValues.get(OLEConstants.OlePersonRequestorLookupable.EMAIL); 058 String personRequestorPhoneNumber = fieldValues.get(OLEConstants.OlePersonRequestorLookupable.PHONE_NUMBER); 059 String personRequestorId = fieldValues.get(OLEConstants.OlePersonRequestorLookupable.ID); 060 String personRequestorTypeId = fieldValues.get(OLEConstants.OlePersonRequestorLookupable.REQUESTOR_TYPE_ID); 061 String personRequestorRefKrimId = fieldValues.get(OLEConstants.OlePersonRequestorLookupable.REF_KRIM_ID); 062 063 personMap.put(OLEConstants.OlePersonRequestorLookupable.FIRST_NAME, personRequestorFirstName); 064 personMap.put(OLEConstants.OlePersonRequestorLookupable.LAST_NAME, personRequestorLastName); 065 personMap.put(OLEConstants.OlePersonRequestorLookupable.EMAIL_ADDRESS, personRequestorEmail); 066 personMap.put(OLEConstants.OlePersonRequestorLookupable.PHONE_NUMBER, personRequestorPhoneNumber); 067 personMap.put(OLEConstants.OlePersonRequestorLookupable.PRINCIPAL_ID, personRequestorId); 068 List<OlePersonRequestor> results = new ArrayList<OlePersonRequestor>(0); 069 if ("".equals(personRequestorRefKrimId) && "".equals(personRequestorTypeId)) { 070 List<Person> people = SpringContext.getBean(PersonService.class).findPeople(personMap, true); 071 072 for (int i = 0; i < people.size(); i++) { 073 OlePersonRequestor olePersonRequestor = new OlePersonRequestor(); 074 olePersonRequestor.setId(people.get(i).getPrincipalId()); 075 olePersonRequestor.setInternalRequestorId(people.get(i).getPrincipalId()); 076 olePersonRequestor.setName(people.get(i).getPrincipalName()); 077 olePersonRequestor.setFirstName(people.get(i).getFirstName()); 078 olePersonRequestor.setLastName(people.get(i).getLastName()); 079 olePersonRequestor.setEmail(people.get(i).getEmailAddress()); 080 olePersonRequestor.setPhoneNumber(people.get(i).getPhoneNumber()); 081 results.add(olePersonRequestor); 082 } 083 } 084 085 String requestorTypeId = null; 086 Map<String, String> requestorTypeMap = getRequestorTypes(); 087 088 if (!(personRequestorFirstName.equals(OLEConstants.OlePersonRequestorLookupable.NULLSTRING)) 089 && !(personRequestorFirstName.equals(OLEConstants.OlePersonRequestorLookupable.EMPTY))) { 090 requestorMap.put(OLEConstants.OlePersonRequestorLookupable.REQUESTOR_FIRST_NAME, personRequestorFirstName); 091 } 092 if (!(personRequestorLastName.equals(OLEConstants.OlePersonRequestorLookupable.NULLSTRING)) 093 && !(personRequestorLastName.equals(OLEConstants.OlePersonRequestorLookupable.EMPTY))) { 094 requestorMap.put(OLEConstants.OlePersonRequestorLookupable.REQUESTOR_LAST_NAME, personRequestorLastName); 095 } 096 if (!(personRequestorEmail.equals(OLEConstants.OlePersonRequestorLookupable.NULLSTRING)) 097 && !(personRequestorEmail.equals(OLEConstants.OlePersonRequestorLookupable.EMPTY))) { 098 requestorMap.put(OLEConstants.OlePersonRequestorLookupable.REQUESTOR_EMAIL, personRequestorEmail); 099 } 100 if (!(personRequestorPhoneNumber.equals(OLEConstants.OlePersonRequestorLookupable.NULLSTRING)) 101 && !(personRequestorPhoneNumber.equals(OLEConstants.OlePersonRequestorLookupable.EMPTY))) { 102 requestorMap.put(OLEConstants.OlePersonRequestorLookupable.REQUESTOR_PHONE_NUMBER, 103 personRequestorPhoneNumber); 104 } 105 if (!(personRequestorId.equals(OLEConstants.OlePersonRequestorLookupable.NULLSTRING)) 106 && !(personRequestorId.equals(OLEConstants.OlePersonRequestorLookupable.EMPTY))) { 107 requestorMap.put(OLEConstants.OlePersonRequestorLookupable.REQUESTOR_ID, personRequestorId); 108 } 109 if (!(personRequestorRefKrimId.equals(OLEConstants.OlePersonRequestorLookupable.NULLSTRING)) 110 && !(personRequestorRefKrimId.equals(OLEConstants.OlePersonRequestorLookupable.EMPTY))) { 111 requestorMap.put(OLEConstants.OlePersonRequestorLookupable.REF_KRIM_ID, personRequestorRefKrimId); 112 } 113 if (!(OLEConstants.OlePersonRequestorLookupable.NULLSTRING.equals(personRequestorTypeId)) 114 && !(OLEConstants.OlePersonRequestorLookupable.EMPTY.equals(personRequestorTypeId))) { 115 requestorMap.put(OLEConstants.OlePersonRequestorLookupable.REQUESTOR_TYPE_ID, personRequestorTypeId); 116 } 117 118 /* 119 * List<OleRequestor> requestors = getRequestorResults(requestorMap); for(int j=0;j<requestors.size();j++){ 120 * OlePersonRequestor olePersonRequestor = new OlePersonRequestor(); 121 * olePersonRequestor.setId(requestors.get(j).getRequestorId()); 122 * olePersonRequestor.setFirstName(requestors.get(j).getRequestorFirstName()); 123 * olePersonRequestor.setLastName(requestors.get(j).getRequestorLastName()); 124 * olePersonRequestor.setName(requestors.get(j).getRequestorName()); 125 * olePersonRequestor.setEmail(requestors.get(j).getRequestorEmail()); 126 * olePersonRequestor.setPhoneNumber(requestors.get(j).getRequestorPhoneNumber()); 127 * olePersonRequestor.setRefKrimId(requestors.get(j).getRefKrimId()); for(Entry<String,String> requestorTypeKey : 128 * requestorTypeMap.entrySet()){ if(requestors.get(j).getRequestorTypeId().equals(requestorTypeKey.getKey())){ String 129 * requestorTypeName = requestorTypeKey.getValue(); olePersonRequestor.setRequestorTypeName(requestorTypeName); } } 130 * results.add(olePersonRequestor); } 131 */ 132 133 List<OLERequestorPatronDocument> olePatronDocumentList = new ArrayList<OLERequestorPatronDocument>(); 134 OlePatronDocuments olePatronDocuments = new OlePatronDocuments(); 135 OlePatronWebServiceImpl olePatronWebService = new OlePatronWebServiceImpl(); 136 String patronRecords = olePatronWebService.getPatronRecords(); 137 olePatronDocuments = getOlePatronRecordHandler().retrievePatronFromXML(patronRecords); 138 olePatronDocumentList = olePatronDocuments.getOlePatronDocuments(); 139 String requestorId = fieldValues.get(OLEConstants.OlePersonRequestorLookupable.ID); 140 String firstName = fieldValues.get(OLEConstants.OlePersonRequestorLookupable.FIRST_NAME); 141 String lastName = fieldValues.get(OLEConstants.OlePersonRequestorLookupable.LAST_NAME); 142 143 for (int patronCount = 0; patronCount < olePatronDocumentList.size(); patronCount++) { 144 OlePersonRequestor olePersonRequestor = new OlePersonRequestor(); 145 if (StringUtils.isEmpty(firstName) && StringUtils.isEmpty(lastName) && StringUtils.isEmpty(requestorId)) { 146 olePersonRequestor.setId(olePatronDocumentList.get(patronCount).getOlePatronId()); 147 olePersonRequestor.setExternalRequestorId(olePatronDocumentList.get(patronCount).getOlePatronId()); 148 olePersonRequestor.setFirstName(olePatronDocumentList.get(patronCount).getFirstName()); 149 olePersonRequestor.setLastName(olePatronDocumentList.get(patronCount).getLastName()); 150 olePersonRequestor.setName(olePatronDocumentList.get(patronCount).getLastName() + ", " 151 + olePatronDocumentList.get(patronCount).getFirstName()); 152 } else { 153 if (olePatronDocumentList.get(patronCount).getFirstName().equalsIgnoreCase(firstName) 154 || olePatronDocumentList.get(patronCount).getLastName().equalsIgnoreCase(lastName) 155 || olePatronDocumentList.get(patronCount).getOlePatronId().equalsIgnoreCase(requestorId)) { 156 olePersonRequestor.setId(olePatronDocumentList.get(patronCount).getOlePatronId()); 157 olePersonRequestor.setExternalRequestorId(olePatronDocumentList.get(patronCount).getOlePatronId()); 158 olePersonRequestor.setFirstName(olePatronDocumentList.get(patronCount).getFirstName()); 159 olePersonRequestor.setLastName(olePatronDocumentList.get(patronCount).getLastName()); 160 olePersonRequestor.setName(olePatronDocumentList.get(patronCount).getLastName() + ", " 161 + olePatronDocumentList.get(patronCount).getFirstName()); 162 } 163 } 164 /* 165 * olePersonRequestor.setEmail(olePatronDocumentList.get(patronCount).getRequestorEmail()); 166 * olePersonRequestor.setPhoneNumber(requestors.get(j).getRequestorPhoneNumber()); 167 * olePersonRequestor.setRefKrimId(requestors.get(j).getRefKrimId()); 168 */ 169 if (olePersonRequestor.getExternalRequestorId() != null) { 170 results.add(olePersonRequestor); 171 } 172 } 173 return results; 174 } 175 176 public List<OleRequestor> getRequestorResults(Map requestorMap) { 177 List<OleRequestor> requestorsList; 178 org.kuali.rice.krad.service.BusinessObjectService 179 businessObjectService = SpringContext.getBean(org.kuali.rice.krad.service.BusinessObjectService.class); 180 if (requestorMap.isEmpty()) { 181 requestorsList = (List) businessObjectService.findAll(OleRequestor.class); 182 } else { 183 requestorsList = (List) businessObjectService.findMatching(OleRequestor.class, requestorMap); 184 } 185 return requestorsList; 186 } 187 188 public Map<String, String> getRequestorTypes() { 189 190 Map<String, String> requestorTypeMap = new HashMap<String, String>(); 191 org.kuali.rice.krad.service.BusinessObjectService 192 businessObjectService = SpringContext.getBean(org.kuali.rice.krad.service.BusinessObjectService.class); 193 List<OleRequestorType> oleRequestorTypeList = (List) businessObjectService.findAll(OleRequestorType.class); 194 for (int i = 0; i < oleRequestorTypeList.size(); i++) { 195 requestorTypeMap.put(oleRequestorTypeList.get(i).getRequestorTypeId().toString(), oleRequestorTypeList.get(i).getRequestorType()); 196 } 197 return requestorTypeMap; 198 } 199 200}