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  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
29  
30  import java.util.ArrayList;
31  import java.util.HashMap;
32  import java.util.List;
33  import java.util.Map;
34  
35  public class OlePersonRequestorLookupableServiceImpl extends AbstractLookupableHelperServiceImpl {
36  
37      private transient OleUrlResolver oleUrlResolver;
38      private OlePatronRecordHandler olePatronRecordHandler;
39  
40  
41      public OlePatronRecordHandler getOlePatronRecordHandler() {
42          if (null == olePatronRecordHandler) {
43              olePatronRecordHandler = new OlePatronRecordHandler();
44          }
45          return olePatronRecordHandler;
46      }
47  
48      public void setOlePatronRecordHandler(OlePatronRecordHandler olePatronRecordHandler) {
49          this.olePatronRecordHandler = olePatronRecordHandler;
50      }
51  
52      @Override
53      public List<? extends BusinessObject> getSearchResults(Map<String, String> fieldValues) {
54          Map<String, String> personMap = new HashMap<String, String>();
55          Map<String, String> requestorMap = new HashMap<String, String>();
56          String personRequestorFirstName = fieldValues.get(OLEConstants.OlePersonRequestorLookupable.FIRST_NAME);
57          String personRequestorLastName = fieldValues.get(OLEConstants.OlePersonRequestorLookupable.LAST_NAME);
58          String personRequestorEmail = fieldValues.get(OLEConstants.OlePersonRequestorLookupable.EMAIL);
59          String personRequestorPhoneNumber = fieldValues.get(OLEConstants.OlePersonRequestorLookupable.PHONE_NUMBER);
60          String personRequestorId = fieldValues.get(OLEConstants.OlePersonRequestorLookupable.ID);
61          String personRequestorTypeId = fieldValues.get(OLEConstants.OlePersonRequestorLookupable.REQUESTOR_TYPE_ID);
62          String personRequestorRefKrimId = fieldValues.get(OLEConstants.OlePersonRequestorLookupable.REF_KRIM_ID);
63  
64          personMap.put(OLEConstants.OlePersonRequestorLookupable.FIRST_NAME, personRequestorFirstName);
65          personMap.put(OLEConstants.OlePersonRequestorLookupable.LAST_NAME, personRequestorLastName);
66          personMap.put(OLEConstants.OlePersonRequestorLookupable.EMAIL_ADDRESS, personRequestorEmail);
67          personMap.put(OLEConstants.OlePersonRequestorLookupable.PHONE_NUMBER, personRequestorPhoneNumber);
68          personMap.put(OLEConstants.OlePersonRequestorLookupable.PRINCIPAL_ID, personRequestorId);
69          List<OlePersonRequestor> results = new ArrayList<OlePersonRequestor>(0);
70          if ("".equals(personRequestorRefKrimId) && "".equals(personRequestorTypeId)) {
71              List<Person> people = SpringContext.getBean(PersonService.class).findPeople(personMap, true);
72  
73              for (int i = 0; i < people.size(); i++) {
74                  OlePersonRequestor olePersonRequestor = new OlePersonRequestor();
75                  olePersonRequestor.setId(people.get(i).getPrincipalId());
76                  olePersonRequestor.setInternalRequestorId(people.get(i).getPrincipalId());
77                  olePersonRequestor.setName(people.get(i).getPrincipalName());
78                  olePersonRequestor.setFirstName(people.get(i).getFirstName());
79                  olePersonRequestor.setLastName(people.get(i).getLastName());
80                  olePersonRequestor.setEmail(people.get(i).getEmailAddress());
81                  olePersonRequestor.setPhoneNumber(people.get(i).getPhoneNumber());
82                  results.add(olePersonRequestor);
83              }
84          }
85  
86          String requestorTypeId = null;
87          Map<String, String> requestorTypeMap = getRequestorTypes();
88  
89          if (!(personRequestorFirstName.equals(OLEConstants.OlePersonRequestorLookupable.NULLSTRING))
90                  && !(personRequestorFirstName.equals(OLEConstants.OlePersonRequestorLookupable.EMPTY))) {
91              requestorMap.put(OLEConstants.OlePersonRequestorLookupable.REQUESTOR_FIRST_NAME, personRequestorFirstName);
92          }
93          if (!(personRequestorLastName.equals(OLEConstants.OlePersonRequestorLookupable.NULLSTRING))
94                  && !(personRequestorLastName.equals(OLEConstants.OlePersonRequestorLookupable.EMPTY))) {
95              requestorMap.put(OLEConstants.OlePersonRequestorLookupable.REQUESTOR_LAST_NAME, personRequestorLastName);
96          }
97          if (!(personRequestorEmail.equals(OLEConstants.OlePersonRequestorLookupable.NULLSTRING))
98                  && !(personRequestorEmail.equals(OLEConstants.OlePersonRequestorLookupable.EMPTY))) {
99              requestorMap.put(OLEConstants.OlePersonRequestorLookupable.REQUESTOR_EMAIL, personRequestorEmail);
100         }
101         if (!(personRequestorPhoneNumber.equals(OLEConstants.OlePersonRequestorLookupable.NULLSTRING))
102                 && !(personRequestorPhoneNumber.equals(OLEConstants.OlePersonRequestorLookupable.EMPTY))) {
103             requestorMap.put(OLEConstants.OlePersonRequestorLookupable.REQUESTOR_PHONE_NUMBER,
104                     personRequestorPhoneNumber);
105         }
106         if (!(personRequestorId.equals(OLEConstants.OlePersonRequestorLookupable.NULLSTRING))
107                 && !(personRequestorId.equals(OLEConstants.OlePersonRequestorLookupable.EMPTY))) {
108             requestorMap.put(OLEConstants.OlePersonRequestorLookupable.REQUESTOR_ID, personRequestorId);
109         }
110         if (!(personRequestorRefKrimId.equals(OLEConstants.OlePersonRequestorLookupable.NULLSTRING))
111                 && !(personRequestorRefKrimId.equals(OLEConstants.OlePersonRequestorLookupable.EMPTY))) {
112             requestorMap.put(OLEConstants.OlePersonRequestorLookupable.REF_KRIM_ID, personRequestorRefKrimId);
113         }
114         if (!(OLEConstants.OlePersonRequestorLookupable.NULLSTRING.equals(personRequestorTypeId))
115                 && !(OLEConstants.OlePersonRequestorLookupable.EMPTY.equals(personRequestorTypeId))) {
116             requestorMap.put(OLEConstants.OlePersonRequestorLookupable.REQUESTOR_TYPE_ID, personRequestorTypeId);
117         }
118 
119         /*
120          * List<OleRequestor> requestors = getRequestorResults(requestorMap); for(int j=0;j<requestors.size();j++){
121          * OlePersonRequestor olePersonRequestor = new OlePersonRequestor();
122          * olePersonRequestor.setId(requestors.get(j).getRequestorId());
123          * olePersonRequestor.setFirstName(requestors.get(j).getRequestorFirstName());
124          * olePersonRequestor.setLastName(requestors.get(j).getRequestorLastName());
125          * olePersonRequestor.setName(requestors.get(j).getRequestorName());
126          * olePersonRequestor.setEmail(requestors.get(j).getRequestorEmail());
127          * olePersonRequestor.setPhoneNumber(requestors.get(j).getRequestorPhoneNumber());
128          * olePersonRequestor.setRefKrimId(requestors.get(j).getRefKrimId()); for(Entry<String,String> requestorTypeKey :
129          * requestorTypeMap.entrySet()){ if(requestors.get(j).getRequestorTypeId().equals(requestorTypeKey.getKey())){ String
130          * requestorTypeName = requestorTypeKey.getValue(); olePersonRequestor.setRequestorTypeName(requestorTypeName); } }
131          * results.add(olePersonRequestor); }
132          */
133 
134         List<OLERequestorPatronDocument> olePatronDocumentList = new ArrayList<OLERequestorPatronDocument>();
135         OlePatronDocuments olePatronDocuments = new OlePatronDocuments();
136         OlePatronWebServiceImpl olePatronWebService = new OlePatronWebServiceImpl();
137         String patronRecords = olePatronWebService.getPatronRecords();
138         olePatronDocuments = getOlePatronRecordHandler().retrievePatronFromXML(patronRecords);
139         olePatronDocumentList = olePatronDocuments.getOlePatronDocuments();
140         String requestorId = fieldValues.get(OLEConstants.OlePersonRequestorLookupable.ID);
141         String firstName = fieldValues.get(OLEConstants.OlePersonRequestorLookupable.FIRST_NAME);
142         String lastName = fieldValues.get(OLEConstants.OlePersonRequestorLookupable.LAST_NAME);
143 
144         for (int patronCount = 0; patronCount < olePatronDocumentList.size(); patronCount++) {
145             OlePersonRequestor olePersonRequestor = new OlePersonRequestor();
146             if (StringUtils.isEmpty(firstName) && StringUtils.isEmpty(lastName) && StringUtils.isEmpty(requestorId)) {
147                 olePersonRequestor.setId(olePatronDocumentList.get(patronCount).getOlePatronId());
148                 olePersonRequestor.setExternalRequestorId(olePatronDocumentList.get(patronCount).getOlePatronId());
149                 olePersonRequestor.setFirstName(olePatronDocumentList.get(patronCount).getFirstName());
150                 olePersonRequestor.setLastName(olePatronDocumentList.get(patronCount).getLastName());
151                 olePersonRequestor.setName(olePatronDocumentList.get(patronCount).getLastName() + ", "
152                         + olePatronDocumentList.get(patronCount).getFirstName());
153             } else {
154                 if (olePatronDocumentList.get(patronCount).getFirstName().equalsIgnoreCase(firstName)
155                         || olePatronDocumentList.get(patronCount).getLastName().equalsIgnoreCase(lastName)
156                         || olePatronDocumentList.get(patronCount).getOlePatronId().equalsIgnoreCase(requestorId)) {
157                     olePersonRequestor.setId(olePatronDocumentList.get(patronCount).getOlePatronId());
158                     olePersonRequestor.setExternalRequestorId(olePatronDocumentList.get(patronCount).getOlePatronId());
159                     olePersonRequestor.setFirstName(olePatronDocumentList.get(patronCount).getFirstName());
160                     olePersonRequestor.setLastName(olePatronDocumentList.get(patronCount).getLastName());
161                     olePersonRequestor.setName(olePatronDocumentList.get(patronCount).getLastName() + ", "
162                             + olePatronDocumentList.get(patronCount).getFirstName());
163                 }
164             }
165             /*
166              * olePersonRequestor.setEmail(olePatronDocumentList.get(patronCount).getRequestorEmail());
167              * olePersonRequestor.setPhoneNumber(requestors.get(j).getRequestorPhoneNumber());
168              * olePersonRequestor.setRefKrimId(requestors.get(j).getRefKrimId());
169              */
170             if (olePersonRequestor.getExternalRequestorId() != null) {
171                 results.add(olePersonRequestor);
172             }
173         }
174         return results;
175     }
176 
177     public List<OleRequestor> getRequestorResults(Map requestorMap) {
178         List<OleRequestor> requestorsList;
179         org.kuali.rice.krad.service.BusinessObjectService
180                 businessObjectService = SpringContext.getBean(org.kuali.rice.krad.service.BusinessObjectService.class);
181         if (requestorMap.isEmpty()) {
182             requestorsList = (List) KRADServiceLocatorWeb.getLegacyDataAdapter().findAll(OleRequestor.class);
183         } else {
184             requestorsList = (List) KRADServiceLocatorWeb.getLegacyDataAdapter().findMatching(OleRequestor.class, requestorMap);
185         }
186         return requestorsList;
187     }
188 
189     public Map<String, String> getRequestorTypes() {
190 
191         Map<String, String> requestorTypeMap = new HashMap<String, String>();
192         org.kuali.rice.krad.service.BusinessObjectService
193                 businessObjectService = SpringContext.getBean(org.kuali.rice.krad.service.BusinessObjectService.class);
194         List<OleRequestorType> oleRequestorTypeList = (List) KRADServiceLocatorWeb.getLegacyDataAdapter().findAll(OleRequestorType.class);
195         for (int i = 0; i < oleRequestorTypeList.size(); i++) {
196             requestorTypeMap.put(oleRequestorTypeList.get(i).getRequestorTypeId().toString(), oleRequestorTypeList.get(i).getRequestorType());
197         }
198         return requestorTypeMap;
199     }
200 
201 }