View Javadoc

1   package org.kuali.ole.deliver.inquiry;
2   
3   import org.kuali.ole.OLEConstants;
4   import org.kuali.ole.deliver.bo.*;
5   import org.kuali.ole.deliver.processor.LoanProcessor;
6   import org.kuali.ole.deliver.service.OleDeliverRequestDocumentHelperServiceImpl;
7   import org.kuali.rice.core.api.config.property.ConfigContext;
8   import org.kuali.rice.kim.impl.identity.address.EntityAddressBo;
9   import org.kuali.rice.kim.impl.identity.affiliation.EntityAffiliationBo;
10  import org.kuali.rice.kim.impl.identity.employment.EntityEmploymentBo;
11  import org.kuali.rice.kim.impl.identity.entity.EntityBo;
12  import org.kuali.rice.krad.bo.BusinessObject;
13  import org.kuali.rice.krad.bo.ExternalizableBusinessObject;
14  import org.kuali.rice.krad.datadictionary.exception.UnknownBusinessClassAttributeException;
15  import org.kuali.rice.krad.inquiry.InquirableImpl;
16  import org.kuali.rice.krad.service.KRADServiceLocator;
17  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
18  import org.kuali.rice.krad.service.ModuleService;
19  
20  import java.security.GeneralSecurityException;
21  import java.util.ArrayList;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  
26  /**
27   * OlePatronInquirableImpl supports to get the data object and patron document.
28   */
29  public class OlePatronInquirableImpl extends InquirableImpl {
30      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OlePatronInquirableImpl.class);
31  
32      /**
33       * This method will retrieve the patron document based on the olePatronId
34       *
35       * @param parameters
36       * @return patronDocument(Object)
37       */
38      String baseUrl = ConfigContext.getCurrentContextConfig().getProperty("ole.rice2.url.base");
39  
40      public Object retrieveDataObject(Map<String, String> parameters) {
41          if (dataObjectClass == null) {
42              LOG.error("Data object class must be set in inquirable before retrieving the object");
43              throw new RuntimeException("Data object class must be set in inquirable before retrieving the object");
44          }
45  
46          // build list of key values from the map parameters
47          List<String> pkPropertyNames = getDataObjectMetaDataService().listPrimaryKeyFieldNames(dataObjectClass);
48  
49          // some classes might have alternate keys defined for retrieving
50          List<List<String>> alternateKeyNames = this.getAlternateKeysForClass(dataObjectClass);
51  
52          // add pk set as beginning so it will be checked first for match
53          alternateKeyNames.add(0, pkPropertyNames);
54  
55          List<String> dataObjectKeySet = retrieveKeySetFromMap(alternateKeyNames, parameters);
56          if ((dataObjectKeySet == null) || dataObjectKeySet.isEmpty()) {
57              LOG.warn("Matching key set not found in request for class: " + getDataObjectClass());
58  
59              return null;
60          }
61          String patronHome = parameters.get("showHistory");
62  
63          // found key set, now build map of key values pairs we can use to retrieve the object
64          Map<String, Object> keyPropertyValues = new HashMap<String, Object>();
65          for (String keyPropertyName : dataObjectKeySet) {
66              String keyPropertyValue = parameters.get(keyPropertyName);
67  
68              // uppercase value if needed
69              Boolean forceUppercase = Boolean.FALSE;
70              try {
71                  forceUppercase = getDataDictionaryService().getAttributeForceUppercase(dataObjectClass,
72                          keyPropertyName);
73              } catch (UnknownBusinessClassAttributeException ex) {
74                  // swallowing exception because this check for ForceUppercase would
75                  // require a DD entry for the attribute, and we will just set force uppercase to false
76                  LOG.warn("Data object class "
77                          + dataObjectClass
78                          + " property "
79                          + keyPropertyName
80                          + " should probably have a DD definition.", ex);
81              }
82  
83              if (forceUppercase.booleanValue() && (keyPropertyValue != null)) {
84                  keyPropertyValue = keyPropertyValue.toUpperCase();
85              }
86  
87              // check security on key field
88              if (getDataObjectAuthorizationService().attributeValueNeedsToBeEncryptedOnFormsAndLinks(dataObjectClass,
89                      keyPropertyName)) {
90                  try {
91                      keyPropertyValue = getEncryptionService().decrypt(keyPropertyValue);
92                  } catch (GeneralSecurityException e) {
93                      LOG.error("Data object class "
94                              + dataObjectClass
95                              + " property "
96                              + keyPropertyName
97                              + " should have been encrypted, but there was a problem decrypting it.", e);
98                      throw new RuntimeException("Data object class "
99                              + dataObjectClass
100                             + " property "
101                             + keyPropertyName
102                             + " should have been encrypted, but there was a problem decrypting it.", e);
103                 }
104             }
105 
106             keyPropertyValues.put(keyPropertyName, keyPropertyValue);
107         }
108 
109         // now retrieve the object based on the key set
110         Object dataObject = null;
111 
112         ModuleService moduleService = KRADServiceLocatorWeb.getKualiModuleService().getResponsibleModuleService(
113                 getDataObjectClass());
114         if (moduleService != null && moduleService.isExternalizable(getDataObjectClass())) {
115             dataObject = moduleService.getExternalizableBusinessObject(getDataObjectClass().asSubclass(
116                     ExternalizableBusinessObject.class), keyPropertyValues);
117         } else if (BusinessObject.class.isAssignableFrom(getDataObjectClass())) {
118             dataObject = getBusinessObjectService().findByPrimaryKey(getDataObjectClass().asSubclass(
119                     BusinessObject.class), keyPropertyValues);
120         }
121         //OleEntityAddressBo entityAddressBo = new OleEntityAddressBo();
122         EntityAddressBo entityAddress = new EntityAddressBo();
123         List<OleEntityAddressBo> oleEntityAddressList = new ArrayList<OleEntityAddressBo>();
124         OlePatronDocument patronDocument = (OlePatronDocument) dataObject;
125         if (patronDocument != null) {
126             EntityBo kimEnity = patronDocument.getEntity();
127             patronDocument.setName(kimEnity.getNames().get(0));
128             List<EntityAddressBo> entityAddressList = kimEnity.getEntityTypeContactInfos().get(0).getAddresses();
129             for (EntityAddressBo entityAdd : entityAddressList) {
130                 OleEntityAddressBo entityAddressBo = new OleEntityAddressBo();
131                 entityAddressBo.setEntityAddressBo(entityAdd);
132                 Map addMap = new HashMap();
133                 addMap.put(OLEConstants.OlePatron.OLE_ADDRESS_ID, entityAdd.getId());
134                 OleAddressBo oleAddressBo = getBusinessObjectService().findByPrimaryKey(OleAddressBo.class, addMap);
135                 entityAddressBo.setOleAddressBo(oleAddressBo);
136                 oleEntityAddressList.add(entityAddressBo);
137             }
138             patronDocument.setOleEntityAddressBo(oleEntityAddressList);
139             patronDocument.setPhones(kimEnity.getEntityTypeContactInfos().get(0).getPhoneNumbers());
140             patronDocument.setEmails(kimEnity.getEntityTypeContactInfos().get(0).getEmailAddresses());
141             patronDocument.setPatronAffiliations(getPatronAffiliationFromEntity(kimEnity.getAffiliations(), kimEnity.getEmploymentInformation()));
142 
143 
144             if(patronHome.equalsIgnoreCase("true"))
145                 patronDocument.setPatronHomePage(true);
146             else
147                 patronDocument.setPatronHomePage(false);
148 
149 
150             List<OleProxyPatronDocument> oleProxyPatronDocuments = patronDocument.getOleProxyPatronDocuments();
151             List<OleProxyPatronDocument> proxyPatronDocumentList = new ArrayList<OleProxyPatronDocument>();
152             if (oleProxyPatronDocuments.size() > 0) {
153                 for (OleProxyPatronDocument oleProxyPatronDocument : oleProxyPatronDocuments) {
154                     Map<String, String> proxyMap = new HashMap<String, String>();
155                     proxyMap.put(OLEConstants.OlePatron.PATRON_ID, oleProxyPatronDocument.getProxyPatronId());
156                     OlePatronDocument tempDocument = (OlePatronDocument) KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OlePatronDocument.class, proxyMap);
157                     if (tempDocument != null && tempDocument.isActiveIndicator()) {
158                         oleProxyPatronDocument.setProxyPatronBarcode(tempDocument.getBarcode());
159                         oleProxyPatronDocument.setProxyPatronFirstName(tempDocument.getEntity().getNames().get(0).getFirstName());
160                         oleProxyPatronDocument.setProxyPatronLastName(tempDocument.getEntity().getNames().get(0).getLastName());
161                         proxyPatronDocumentList.add(oleProxyPatronDocument);
162                     }
163                 }
164                 patronDocument.setOleProxyPatronDocuments(proxyPatronDocumentList);
165             }
166             OlePatronDocument olePatronDocument;
167             OleProxyPatronDocument proxyPatronDocument = null;
168             List<OleProxyPatronDocument> proxyPatronDocuments = new ArrayList<OleProxyPatronDocument>();
169             Map proxyMap = new HashMap();
170             proxyMap.put(OLEConstants.OlePatron.PROXY_PATRON_ID, patronDocument.getOlePatronId());
171             List<OleProxyPatronDocument> oleProxyPatronDocumentList = (List<OleProxyPatronDocument>) getBusinessObjectService().findMatching(OleProxyPatronDocument.class, proxyMap);
172             if (oleProxyPatronDocumentList.size() > 0) {
173                 proxyMap.remove(OLEConstants.OlePatron.PROXY_PATRON_ID);
174                 for (OleProxyPatronDocument oleProxyPatronDocument : oleProxyPatronDocumentList) {
175                     proxyMap.put(OLEConstants.OlePatron.PATRON_ID, oleProxyPatronDocument.getOlePatronId());
176                     List<OlePatronDocument> olePatronDocumentList = (List<OlePatronDocument>) getBusinessObjectService().findMatching(OlePatronDocument.class, proxyMap);
177                     if (olePatronDocumentList.size() > 0) {
178                         olePatronDocument = olePatronDocumentList.get(0);
179                         proxyPatronDocument = new OleProxyPatronDocument();
180                         if (olePatronDocument.isActiveIndicator()) {
181                             proxyPatronDocument.setOlePatronId(olePatronDocument.getOlePatronId());
182                             proxyPatronDocument.setProxyForPatronFirstName(olePatronDocument.getEntity().getNames().get(0).getFirstName());
183                             proxyPatronDocument.setProxyForPatronLastName(olePatronDocument.getEntity().getNames().get(0).getLastName());
184                             List<OleProxyPatronDocument> proxyPatronList = (List<OleProxyPatronDocument>) getBusinessObjectService().findMatching(OleProxyPatronDocument.class, proxyMap);
185                             if (proxyPatronList.size() > 0) {
186                                 for (OleProxyPatronDocument proxyPatron : proxyPatronList) {
187                                     if (proxyPatron.getOlePatronId().equals(olePatronDocument.getOlePatronId())) {
188                                         proxyPatronDocument.setProxyPatronActivationDate(proxyPatron.getProxyPatronActivationDate());
189                                         proxyPatronDocument.setProxyPatronExpirationDate(proxyPatron.getProxyPatronExpirationDate());
190                                     }
191                                 }
192                             }
193                         }
194                         proxyPatronDocuments.add(proxyPatronDocument);
195                     }
196                 }
197                 patronDocument.setOleProxyPatronDocumentList(proxyPatronDocuments);
198             }
199             LoanProcessor loanProcessor = new LoanProcessor();
200             OleDeliverRequestDocumentHelperServiceImpl requestService = new OleDeliverRequestDocumentHelperServiceImpl();
201             List<OleDeliverRequestBo> oleDeliverRequestBoList = patronDocument.getOleDeliverRequestBos();
202             if (oleDeliverRequestBoList.size() > 0) {
203                 for (int i = 0; i < oleDeliverRequestBoList.size(); i++) {
204             /*        OleItemSearch oleItemSearch= requestService.getItemDetailsForPatron(oleDeliverRequestBoList.get(i).getItemUuid());
205                     if(oleItemSearch!=null && oleItemSearch.getItemBarCode()!=null)
206                     {
207                     oleDeliverRequestBoList.get(i).setTitle(oleItemSearch.getTitle());
208                     oleDeliverRequestBoList.get(i).setCallNumber(oleItemSearch.getCallNumber());
209                     }*/
210                     requestService.processItem(oleDeliverRequestBoList.get(i));
211                 }
212             }
213 
214             try {
215                 patronDocument.setOleLoanDocuments(loanProcessor.getPatronLoanedItem(patronDocument.getOlePatronId()));
216                 patronDocument.setOleTemporaryCirculationHistoryRecords(loanProcessor.getPatronTemporaryCirculationHistoryRecords(patronDocument.getOlePatronId()));
217                 patronDocument.setOleDeliverRequestBos(loanProcessor.getPatronRequestRecords(patronDocument.getOlePatronId()));
218             } catch (Exception e) {
219                 LOG.error("Cannot fetch the patron loaned items.", e);
220 
221             }
222         }
223 
224         return patronDocument;
225     }
226 
227     private List<OlePatronAffiliation> getPatronAffiliationFromEntity(List<EntityAffiliationBo> affiliations,
228                                                                       List<EntityEmploymentBo> employeeDetails) {
229         List<OlePatronAffiliation> patronAffiliations = new ArrayList<OlePatronAffiliation>();
230         for (EntityAffiliationBo entityAffiliationBo : affiliations) {
231             OlePatronAffiliation patronAffiliation = new OlePatronAffiliation(entityAffiliationBo);
232             List<EntityEmploymentBo> employmentBos = new ArrayList<EntityEmploymentBo>();
233             for (EntityEmploymentBo entityEmploymentBo : employeeDetails) {
234                 if (patronAffiliation.getEntityAffiliationId().equalsIgnoreCase(entityEmploymentBo.getEntityAffiliationId())) {
235                     employmentBos.add(entityEmploymentBo);
236                 }
237                 patronAffiliation.setEmployments(employmentBos);
238             }
239             patronAffiliations.add(patronAffiliation);
240         }
241         return patronAffiliations;
242     }
243 
244     public String getTempCircRecords(String olePatronId) {
245         String url = baseUrl + "/portal.do?channelTitle=Patron&channelUrl=" + baseUrl + "/ole-kr-krad/temporaryCirculationRecord?viewId=OleTemporaryCirculationHistoryRecordView&amp;methodToCall=viewTempCircRecords&amp;patronId=" + olePatronId;
246         return url;
247     }
248 
249     public String getLoanedRecords(String olePatronId) {
250 
251         String url = baseUrl + "/portal.do?channelTitle=Patron&channelUrl=" + baseUrl + "/ole-kr-krad/patronLoanedRecord?viewId=OlePatronLoanedRecordView&amp;methodToCall=viewLoanedRecords&amp;patronId=" + olePatronId;
252         return url;
253     }
254 
255     public String getRequestedRecords(String olePatronId) {
256         String url = baseUrl + "/portal.do?channelTitle=Patron&channelUrl=" + baseUrl + "/ole-kr-krad/patronRequestedRecord?viewId=OlePatronRequestedRecordView&amp;methodToCall=viewRequestedRecords&amp;patronId=" + olePatronId;
257         return url;
258     }
259 
260     public String getHrefLink(String item,String instance,String bib){
261        String inquiryUrl= ConfigContext.getCurrentContextConfig().getProperty("ole.rice2.url.base")+"/ole-kr-krad/editorcontroller?viewId=EditorView&amp;methodToCall=load&amp;docCategory=work&amp;docType=item&amp;editable=false&amp;docFormat=oleml&amp;docId="+item+"&amp;instanceId="+instance+"&amp;bibId="+bib+"";
262         return inquiryUrl;
263     }
264 }