View Javadoc
1   package org.kuali.ole.deliver.inquiry;
2   
3   import org.apache.commons.collections.CollectionUtils;
4   import org.kuali.ole.OLEConstants;
5   import org.kuali.ole.OLEPropertyConstants;
6   import org.kuali.ole.deliver.bo.*;
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.email.EntityEmailBo;
11  import org.kuali.rice.kim.impl.identity.employment.EntityEmploymentBo;
12  import org.kuali.rice.kim.impl.identity.entity.EntityBo;
13  import org.kuali.rice.kim.impl.identity.phone.EntityPhoneBo;
14  import org.kuali.rice.krad.bo.BusinessObject;
15  import org.kuali.rice.krad.bo.ExternalizableBusinessObject;
16  import org.kuali.rice.krad.datadictionary.exception.UnknownBusinessClassAttributeException;
17  import org.kuali.rice.krad.inquiry.InquirableImpl;
18  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
19  import org.kuali.rice.krad.service.ModuleService;
20  
21  import java.security.GeneralSecurityException;
22  import java.util.ArrayList;
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  
27  /**
28   * OlePatronInquirableImpl supports to get the data object and patron document  .
29   */
30  public class OlePatronInquirableImpl extends InquirableImpl {
31      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OlePatronInquirableImpl.class);
32  
33      /**
34       * This method will retrieve the patron document based on the olePatronId
35       *
36       * @param parameters
37       * @return patronDocument(Object)
38       */
39      String baseUrl = ConfigContext.getCurrentContextConfig().getProperty(OLEPropertyConstants.OLE_URL_BASE);
40  
41      public Object retrieveDataObject(Map<String, String> parameters) {
42          if (dataObjectClass == null) {
43              LOG.error("Data object class must be set in inquirable before retrieving the object");
44              throw new RuntimeException("Data object class must be set in inquirable before retrieving the object");
45          }
46  
47          // build list of key values from the map parameters
48          List<String> pkPropertyNames = getDataObjectMetaDataService().listPrimaryKeyFieldNames(dataObjectClass);
49  
50          // some classes might have alternate keys defined for retrieving
51          List<List<String>> alternateKeyNames = this.getAlternateKeysForClass(dataObjectClass);
52  
53          // add pk set as beginning so it will be checked first for match
54          alternateKeyNames.add(0, pkPropertyNames);
55  
56          List<String> dataObjectKeySet = retrieveKeySetFromMap(alternateKeyNames, parameters);
57          if ((dataObjectKeySet == null) || dataObjectKeySet.isEmpty()) {
58              LOG.warn("Matching key set not found in request for class: " + getDataObjectClass());
59  
60              return null;
61          }
62          String patronHome = parameters.get("flow");
63  
64          // found key set, now build map of key values pairs we can use to retrieve the object
65          Map<String, Object> keyPropertyValues = new HashMap<String, Object>();
66          for (String keyPropertyName : dataObjectKeySet) {
67              String keyPropertyValue = parameters.get(keyPropertyName);
68  
69              // uppercase value if needed
70              Boolean forceUppercase = Boolean.FALSE;
71              try {
72                  forceUppercase = getDataDictionaryService().getAttributeForceUppercase(dataObjectClass,
73                          keyPropertyName);
74              } catch (UnknownBusinessClassAttributeException ex) {
75                  // swallowing exception because this check for ForceUppercase would
76                  // require a DD entry for the attribute, and we will just set force uppercase to false
77                  LOG.warn("Data object class "
78                          + dataObjectClass
79                          + " property "
80                          + keyPropertyName
81                          + " should probably have a DD definition.", ex);
82              }
83  
84              if (forceUppercase.booleanValue() && (keyPropertyValue != null)) {
85                  keyPropertyValue = keyPropertyValue.toUpperCase();
86              }
87  
88              // check security on key field
89              if (getDataObjectAuthorizationService().attributeValueNeedsToBeEncryptedOnFormsAndLinks(dataObjectClass,
90                      keyPropertyName)) {
91                  try {
92                      keyPropertyValue = getEncryptionService().decrypt(keyPropertyValue);
93                  } catch (GeneralSecurityException e) {
94                      LOG.error("Data object class "
95                              + dataObjectClass
96                              + " property "
97                              + keyPropertyName
98                              + " should have been encrypted, but there was a problem decrypting it.", e);
99                      throw new RuntimeException("Data object class "
100                             + dataObjectClass
101                             + " property "
102                             + keyPropertyName
103                             + " should have been encrypted, but there was a problem decrypting it.", e);
104                 }
105             }
106 
107             keyPropertyValues.put(keyPropertyName, keyPropertyValue);
108         }
109 
110         // now retrieve the object based on the key set
111         Object dataObject = null;
112 
113         ModuleService moduleService = KRADServiceLocatorWeb.getKualiModuleService().getResponsibleModuleService(
114                 getDataObjectClass());
115         if (moduleService != null && moduleService.isExternalizable(getDataObjectClass())) {
116             dataObject = moduleService.getExternalizableBusinessObject(getDataObjectClass().asSubclass(
117                     ExternalizableBusinessObject.class), keyPropertyValues);
118         } else if (BusinessObject.class.isAssignableFrom(getDataObjectClass())) {
119             dataObject = getBusinessObjectService().findByPrimaryKey(getDataObjectClass().asSubclass(
120                     BusinessObject.class), keyPropertyValues);
121         }
122         //OleEntityAddressBo entityAddressBo = new OleEntityAddressBo();
123         EntityAddressBo entityAddress = new EntityAddressBo();
124         List<OleEntityAddressBo> oleEntityAddressList = new ArrayList<OleEntityAddressBo>();
125         List<OleEntityPhoneBo> oleEntityPhoneBoList = new ArrayList<>();
126         List<OleEntityEmailBo> oleEntityEmailBoList = new ArrayList<>();
127         OlePatronDocument patronDocument = (OlePatronDocument) dataObject;
128         if (patronDocument != null) {
129             EntityBo kimEnity = patronDocument.getEntity();
130             if (kimEnity.getNames().size() > 0) {
131                 patronDocument.setName(kimEnity.getNames().get(0));
132             }
133             if(kimEnity.getEntityTypeContactInfos().size()>0){
134                 List<EntityAddressBo> entityAddressList = kimEnity.getEntityTypeContactInfos().get(0).getAddresses();
135                 for (EntityAddressBo entityAdd : entityAddressList) {
136                     OleEntityAddressBo entityAddressBo = new OleEntityAddressBo();
137                     entityAddressBo.setEntityAddressBo(entityAdd);
138                     Map addMap = new HashMap();
139                     //addMap.put(OLEConstants.OlePatron.OLE_ADDRESS_ID, entityAdd.getId());
140                     addMap.put(OLEConstants.OlePatron.ENTITY_BO_ID, entityAdd.getId());
141                     addMap.put(OLEConstants.OlePatron.PATRON_ID, patronDocument.getId());
142                     List<OleAddressBo> oleAddressBos = (List<OleAddressBo>)getBusinessObjectService().findMatching(OleAddressBo.class, addMap);
143                     if(CollectionUtils.isNotEmpty(oleAddressBos)) {
144                         entityAddressBo.setOleAddressBo(oleAddressBos.get(0));
145                     }
146                     oleEntityAddressList.add(entityAddressBo);
147                 }
148                 patronDocument.setOleEntityAddressBo(oleEntityAddressList);
149                 List<EntityPhoneBo> entityPhoneBoList = kimEnity.getEntityTypeContactInfos().get(0).getPhoneNumbers();
150                 for(EntityPhoneBo entityPhoneBo : entityPhoneBoList) {
151                     OleEntityPhoneBo oleEntityPhoneBo = new OleEntityPhoneBo();
152                     oleEntityPhoneBo.setEntityPhoneBo(entityPhoneBo);
153                     Map map = new HashMap();
154                     map.put(OLEConstants.OlePatron.ENTITY_BO_ID, entityPhoneBo.getId());
155                     map.put(OLEConstants.OlePatron.PATRON_ID, patronDocument.getId());
156                     List<OlePhoneBo> olePhoneBos = (List<OlePhoneBo>)getBusinessObjectService().findMatching(OlePhoneBo.class, map);
157                     if(CollectionUtils.isNotEmpty(olePhoneBos)) {
158                         oleEntityPhoneBo.setOlePhoneBo(olePhoneBos.get(0));
159                     }
160                     oleEntityPhoneBoList.add(oleEntityPhoneBo);
161                 }
162                 patronDocument.setOleEntityPhoneBo(oleEntityPhoneBoList);
163                 List<EntityEmailBo> entityEmailBoList = kimEnity.getEntityTypeContactInfos().get(0).getEmailAddresses();
164                 for(EntityEmailBo entityEmailBo : entityEmailBoList) {
165                     OleEntityEmailBo oleEntityEmailBo = new OleEntityEmailBo();
166                     oleEntityEmailBo.setEntityEmailBo(entityEmailBo);
167                     Map map = new HashMap();
168                     map.put(OLEConstants.OlePatron.ENTITY_BO_ID, entityEmailBo.getId());
169                     map.put(OLEConstants.OlePatron.PATRON_ID, patronDocument.getId());
170                     List<OleEmailBo> oleEmailBos = (List<OleEmailBo>)getBusinessObjectService().findMatching(OleEmailBo.class, map);
171                     if(CollectionUtils.isNotEmpty(oleEmailBos)) {
172                         oleEntityEmailBo.setOleEmailBo(oleEmailBos.get(0));
173                     }
174                     oleEntityEmailBoList.add(oleEntityEmailBo);
175                 }
176                 patronDocument.setOleEntityEmailBo(oleEntityEmailBoList);
177                 patronDocument.setPatronAffiliations(getPatronAffiliationFromEntity(kimEnity.getAffiliations(), kimEnity.getEmploymentInformation()));
178                 if (patronHome != null) {
179                     if (patronHome.equalsIgnoreCase("start"))
180                         patronDocument.setPatronHomePage(false);
181                     else
182                         patronDocument.setPatronHomePage(true);
183                 }
184             }
185             if (null != patronDocument.getName()) {
186                 String createBillUrl = OLEConstants.OlePatron.PATRON_CREATE_BILL_URL + patronDocument.getOlePatronId() + "&firstName=" + patronDocument.getName().getFirstName() + "&lastName=" + patronDocument.getName().getLastName();
187                 patronDocument.setCreateBillUrl(createBillUrl);
188             }
189             if (CollectionUtils.isNotEmpty(patronDocument.getPatronBillPayments())) {
190                 patronDocument.setPatronBillFileName(OLEConstants.OlePatron.PATRON_VIEW_BILLS);
191                 patronDocument.setViewBillUrl(OLEConstants.OlePatron.PATRON_VIEW_BILL_URL + patronDocument.getOlePatronId());
192             }
193             patronDocument.setShowBillUrlsFlag(true);
194 
195 
196         }
197 
198         if (CollectionUtils.isNotEmpty(patronDocument.getOleProxyPatronDocuments())) {
199             OlePatronDocument olePatronDocument;
200             for (OleProxyPatronDocument oleProxyPatronDocument : patronDocument.getOleProxyPatronDocuments()) {
201                 olePatronDocument = oleProxyPatronDocument.getOlePatronDocument();
202                 oleProxyPatronDocument.setProxyForPatronFirstName(olePatronDocument.getEntity().getNames().get(0).getFirstName());
203                 oleProxyPatronDocument.setProxyForPatronLastName(olePatronDocument.getEntity().getNames().get(0).getLastName());
204             }
205         }
206         if (CollectionUtils.isNotEmpty(patronDocument.getOleProxyPatronDocumentList())) {
207             OlePatronDocument olePatronDocument;
208             for (OleProxyPatronDocument oleProxyPatronDocument : patronDocument.getOleProxyPatronDocumentList()) {
209                 olePatronDocument = oleProxyPatronDocument.getOlePatronDocument();
210                 oleProxyPatronDocument.setProxyForPatronFirstName(olePatronDocument.getEntity().getNames().get(0).getFirstName());
211                 oleProxyPatronDocument.setProxyForPatronLastName(olePatronDocument.getEntity().getNames().get(0).getLastName());
212             }
213         }
214 
215         if(patronDocument.getOleLoanDocuments()!=null){
216             patronDocument.setLoanCount(patronDocument.getOleLoanDocuments().size());
217         }
218         if(patronDocument.getOleTemporaryCirculationHistoryRecords()!=null){
219             patronDocument.setTempCirculationHistoryCount(patronDocument.getOleTemporaryCirculationHistoryRecords().size());
220         }
221         if(patronDocument.getOleDeliverRequestBos()!=null){
222             patronDocument.setRequestedItemRecordsCount(patronDocument.getOleDeliverRequestBos().size());
223         }
224 
225 
226 
227         return patronDocument;
228     }
229 
230     private List<OlePatronAffiliation> getPatronAffiliationFromEntity(List<EntityAffiliationBo> affiliations,
231                                                                       List<EntityEmploymentBo> employeeDetails) {
232         List<OlePatronAffiliation> patronAffiliations = new ArrayList<OlePatronAffiliation>();
233         for (EntityAffiliationBo entityAffiliationBo : affiliations) {
234             OlePatronAffiliation patronAffiliation = new OlePatronAffiliation(entityAffiliationBo);
235             List<EntityEmploymentBo> employmentBos = new ArrayList<EntityEmploymentBo>();
236             for (EntityEmploymentBo entityEmploymentBo : employeeDetails) {
237                 if (patronAffiliation.getEntityAffiliationId().equalsIgnoreCase(entityEmploymentBo.getEntityAffiliationId())) {
238                     employmentBos.add(entityEmploymentBo);
239                 }
240                 patronAffiliation.setEmployments(employmentBos);
241             }
242             patronAffiliations.add(patronAffiliation);
243         }
244         return patronAffiliations;
245     }
246 
247     public String getTempCircRecords(String olePatronId) {
248         String url = baseUrl + "/portal.do?channelTitle=Patron&channelUrl=" + baseUrl + "/ole-kr-krad/temporaryCirculationRecord?viewId=OleTemporaryCirculationHistoryRecordView&amp;methodToCall=viewTempCircRecords&amp;patronId=" + olePatronId;
249         return url;
250     }
251 
252     public String getLoanedRecords(String olePatronId) {
253 
254         String url = baseUrl + "/portal.do?channelTitle=Patron&channelUrl=" + baseUrl + "/ole-kr-krad/patronLoanedRecord?viewId=OlePatronLoanedRecordView&amp;methodToCall=viewLoanedRecords&amp;patronId=" + olePatronId;
255         return url;
256     }
257 
258     public String getRequestedRecords(String olePatronId) {
259         String url = baseUrl + "/portal.do?channelTitle=Patron&channelUrl=" + baseUrl + "/ole-kr-krad/patronRequestedRecord?viewId=OlePatronRequestedRecordView&amp;methodToCall=viewRequestedRecords&amp;patronId=" + olePatronId;
260         return url;
261     }
262 
263     public String getCountOfPendingRequests(String itemId){
264         Map itemMap = new HashMap();
265         itemMap.put(OLEConstants.OleDeliverRequest.ITEM_ID, itemId);
266         List<OleDeliverRequestBo> oleDeliverRequestBoList = (List<OleDeliverRequestBo>) getBusinessObjectService().findMatching(OleDeliverRequestBo.class, itemMap);
267         if (oleDeliverRequestBoList!=null && oleDeliverRequestBoList.size()>0) {
268 
269             return "View all requests";
270         }
271         else
272             return " ";
273     }
274 
275     public String getHrefLink(String item,String instance,String bib){
276        String inquiryUrl= ConfigContext.getCurrentContextConfig().getProperty(OLEPropertyConstants.OLE_URL_BASE)+
277                "/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+"";
278         return inquiryUrl;
279     }
280 
281 }