View Javadoc
1   /*
2    * Copyright 2012 The Kuali Foundation.
3    *
4    * Licensed under the Educational Community License, Version 1.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl1.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.select.service.impl;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.ole.select.businessobject.*;
20  import org.kuali.ole.select.service.OleUrlResolver;
21  import org.kuali.ole.service.impl.OlePatronWebServiceImpl;
22  import org.kuali.ole.sys.OLEConstants;
23  import org.kuali.ole.sys.context.SpringContext;
24  import org.kuali.rice.kim.api.identity.Person;
25  import org.kuali.rice.kim.api.identity.PersonService;
26  import org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl;
27  import org.kuali.rice.krad.bo.BusinessObject;
28  
29  import java.util.ArrayList;
30  import java.util.HashMap;
31  import java.util.List;
32  import java.util.Map;
33  
34  public class OlePersonRequestorLookupableServiceImpl extends AbstractLookupableHelperServiceImpl {
35  
36      private transient OleUrlResolver oleUrlResolver;
37      private OlePatronRecordHandler olePatronRecordHandler;
38  
39  
40      public OlePatronRecordHandler getOlePatronRecordHandler() {
41          if (null == olePatronRecordHandler) {
42              olePatronRecordHandler = new OlePatronRecordHandler();
43          }
44          return olePatronRecordHandler;
45      }
46  
47      public void setOlePatronRecordHandler(OlePatronRecordHandler olePatronRecordHandler) {
48          this.olePatronRecordHandler = olePatronRecordHandler;
49      }
50  
51      @Override
52      public List<? extends BusinessObject> getSearchResults(Map<String, String> fieldValues) {
53          Map<String, String> personMap = new HashMap<String, String>();
54          Map<String, String> requestorMap = new HashMap<String, String>();
55          String personRequestorFirstName = fieldValues.get(OLEConstants.OlePersonRequestorLookupable.FIRST_NAME);
56          String personRequestorLastName = fieldValues.get(OLEConstants.OlePersonRequestorLookupable.LAST_NAME);
57          String personRequestorEmail = fieldValues.get(OLEConstants.OlePersonRequestorLookupable.EMAIL);
58          String personRequestorPhoneNumber = fieldValues.get(OLEConstants.OlePersonRequestorLookupable.PHONE_NUMBER);
59          String personRequestorId = fieldValues.get(OLEConstants.OlePersonRequestorLookupable.ID);
60          String personRequestorTypeId = fieldValues.get(OLEConstants.OlePersonRequestorLookupable.REQUESTOR_TYPE_ID);
61          String personRequestorRefKrimId = fieldValues.get(OLEConstants.OlePersonRequestorLookupable.REF_KRIM_ID);
62  
63          personMap.put(OLEConstants.OlePersonRequestorLookupable.FIRST_NAME, personRequestorFirstName);
64          personMap.put(OLEConstants.OlePersonRequestorLookupable.LAST_NAME, personRequestorLastName);
65          personMap.put(OLEConstants.OlePersonRequestorLookupable.EMAIL_ADDRESS, personRequestorEmail);
66          personMap.put(OLEConstants.OlePersonRequestorLookupable.PHONE_NUMBER, personRequestorPhoneNumber);
67          personMap.put(OLEConstants.OlePersonRequestorLookupable.PRINCIPAL_ID, personRequestorId);
68          List<OlePersonRequestor> results = new ArrayList<OlePersonRequestor>(0);
69          if ("".equals(personRequestorRefKrimId) && "".equals(personRequestorTypeId)) {
70              List<Person> people = SpringContext.getBean(PersonService.class).findPeople(personMap, true);
71  
72              for (int i = 0; i < people.size(); i++) {
73                  OlePersonRequestor olePersonRequestor = new OlePersonRequestor();
74                  olePersonRequestor.setId(people.get(i).getPrincipalId());
75                  olePersonRequestor.setInternalRequestorId(people.get(i).getPrincipalId());
76                  olePersonRequestor.setName(people.get(i).getPrincipalName());
77                  olePersonRequestor.setFirstName(people.get(i).getFirstName());
78                  olePersonRequestor.setLastName(people.get(i).getLastName());
79                  olePersonRequestor.setEmail(people.get(i).getEmailAddress());
80                  olePersonRequestor.setPhoneNumber(people.get(i).getPhoneNumber());
81                  results.add(olePersonRequestor);
82              }
83          }
84  
85          String requestorTypeId = null;
86          Map<String, String> requestorTypeMap = getRequestorTypes();
87  
88          if (!(personRequestorFirstName.equals(OLEConstants.OlePersonRequestorLookupable.NULLSTRING))
89                  && !(personRequestorFirstName.equals(OLEConstants.OlePersonRequestorLookupable.EMPTY))) {
90              requestorMap.put(OLEConstants.OlePersonRequestorLookupable.REQUESTOR_FIRST_NAME, personRequestorFirstName);
91          }
92          if (!(personRequestorLastName.equals(OLEConstants.OlePersonRequestorLookupable.NULLSTRING))
93                  && !(personRequestorLastName.equals(OLEConstants.OlePersonRequestorLookupable.EMPTY))) {
94              requestorMap.put(OLEConstants.OlePersonRequestorLookupable.REQUESTOR_LAST_NAME, personRequestorLastName);
95          }
96          if (!(personRequestorEmail.equals(OLEConstants.OlePersonRequestorLookupable.NULLSTRING))
97                  && !(personRequestorEmail.equals(OLEConstants.OlePersonRequestorLookupable.EMPTY))) {
98              requestorMap.put(OLEConstants.OlePersonRequestorLookupable.REQUESTOR_EMAIL, personRequestorEmail);
99          }
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 }