View Javadoc

1   package org.kuali.ole.patron.inquiry;
2   
3   import org.kuali.ole.deliver.loan.LoanProcessor;
4   import org.kuali.ole.deliver.request.service.OleDeliverRequestDocumentHelperServiceImpl;
5   import org.kuali.ole.patron.bo.OleAddressBo;
6   import org.kuali.ole.patron.bo.OleEntityAddressBo;
7   import org.kuali.ole.patron.bo.OlePatronDocument;
8   import org.kuali.rice.kim.impl.identity.address.EntityAddressBo;
9   import org.kuali.rice.kim.impl.identity.entity.EntityBo;
10  import org.kuali.rice.krad.bo.BusinessObject;
11  import org.kuali.rice.krad.bo.ExternalizableBusinessObject;
12  import org.kuali.rice.krad.datadictionary.exception.UnknownBusinessClassAttributeException;
13  import org.kuali.rice.krad.inquiry.InquirableImpl;
14  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
15  import org.kuali.rice.krad.service.ModuleService;
16  
17  import java.security.GeneralSecurityException;
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  /**
24   * OlePatronInquirableImpl supports to get the data object and patron document.
25   */
26  public class OlePatronInquirableImpl extends InquirableImpl {
27      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OlePatronInquirableImpl.class);
28  
29      /**
30       * This method will retrieve the patron document based on the olePatronId
31       * @param parameters
32       * @return patronDocument(Object)
33       */
34      public Object retrieveDataObject(Map<String, String> parameters) {
35          if (dataObjectClass == null) {
36              LOG.error("Data object class must be set in inquirable before retrieving the object");
37              throw new RuntimeException("Data object class must be set in inquirable before retrieving the object");
38          }
39  
40          // build list of key values from the map parameters
41          List<String> pkPropertyNames = getDataObjectMetaDataService().listPrimaryKeyFieldNames(dataObjectClass);
42  
43          // some classes might have alternate keys defined for retrieving
44          List<List<String>> alternateKeyNames = this.getAlternateKeysForClass(dataObjectClass);
45  
46          // add pk set as beginning so it will be checked first for match
47          alternateKeyNames.add(0, pkPropertyNames);
48  
49          List<String> dataObjectKeySet = retrieveKeySetFromMap(alternateKeyNames, parameters);
50          if ((dataObjectKeySet == null) || dataObjectKeySet.isEmpty()) {
51              LOG.warn("Matching key set not found in request for class: " + getDataObjectClass());
52  
53              return null;
54          }
55  
56          // found key set, now build map of key values pairs we can use to retrieve the object
57          Map<String, Object> keyPropertyValues = new HashMap<String, Object>();
58          for (String keyPropertyName : dataObjectKeySet) {
59              String keyPropertyValue = parameters.get(keyPropertyName);
60  
61              // uppercase value if needed
62              Boolean forceUppercase = Boolean.FALSE;
63              try {
64                  forceUppercase = getDataDictionaryService().getAttributeForceUppercase(dataObjectClass,
65                          keyPropertyName);
66              } catch (UnknownBusinessClassAttributeException ex) {
67                  // swallowing exception because this check for ForceUppercase would
68                  // require a DD entry for the attribute, and we will just set force uppercase to false
69                  LOG.warn("Data object class "
70                          + dataObjectClass
71                          + " property "
72                          + keyPropertyName
73                          + " should probably have a DD definition.", ex);
74              }
75  
76              if (forceUppercase.booleanValue() && (keyPropertyValue != null)) {
77                  keyPropertyValue = keyPropertyValue.toUpperCase();
78              }
79  
80              // check security on key field
81              if (getDataObjectAuthorizationService().attributeValueNeedsToBeEncryptedOnFormsAndLinks(dataObjectClass,
82                      keyPropertyName)) {
83                  try {
84                      keyPropertyValue = getEncryptionService().decrypt(keyPropertyValue);
85                  } catch (GeneralSecurityException e) {
86                      LOG.error("Data object class "
87                              + dataObjectClass
88                              + " property "
89                              + keyPropertyName
90                              + " should have been encrypted, but there was a problem decrypting it.", e);
91                      throw new RuntimeException("Data object class "
92                              + dataObjectClass
93                              + " property "
94                              + keyPropertyName
95                              + " should have been encrypted, but there was a problem decrypting it.", e);
96                  }
97              }
98  
99              keyPropertyValues.put(keyPropertyName, keyPropertyValue);
100         }
101 
102         // now retrieve the object based on the key set
103         Object dataObject = null;
104 
105         ModuleService moduleService = KRADServiceLocatorWeb.getKualiModuleService().getResponsibleModuleService(
106                 getDataObjectClass());
107         if (moduleService != null && moduleService.isExternalizable(getDataObjectClass())) {
108             dataObject = moduleService.getExternalizableBusinessObject(getDataObjectClass().asSubclass(
109                     ExternalizableBusinessObject.class), keyPropertyValues);
110         } else if (BusinessObject.class.isAssignableFrom(getDataObjectClass())) {
111             dataObject = getBusinessObjectService().findByPrimaryKey(getDataObjectClass().asSubclass(
112                     BusinessObject.class), keyPropertyValues);
113         }
114         OleEntityAddressBo entityAddressBo = new OleEntityAddressBo();
115         EntityAddressBo entityAddress = new EntityAddressBo();
116         List<OleEntityAddressBo> oleEntityAddressList = new ArrayList<OleEntityAddressBo>();
117         OlePatronDocument patronDocument = (OlePatronDocument)dataObject;
118         if (patronDocument != null) {
119             EntityBo kimEnity = patronDocument.getEntity();
120             patronDocument.setName(kimEnity.getNames().get(0));
121             List<EntityAddressBo> entityAddressList = kimEnity.getEntityTypeContactInfos().get(0).getAddresses();
122             for (EntityAddressBo entityAdd : entityAddressList) {
123                 entityAddressBo.setEntityAddressBo(entityAdd);
124                 Map addMap = new HashMap();
125                 addMap.put("oleAddressId",entityAdd.getId());
126                 OleAddressBo oleAddressBo=  getBusinessObjectService().findByPrimaryKey(OleAddressBo.class, addMap);
127                 entityAddressBo.setOleAddressBo(oleAddressBo);
128                 oleEntityAddressList.add(entityAddressBo);
129             }
130             patronDocument.setOleEntityAddressBo(oleEntityAddressList);
131             patronDocument.setPhones(kimEnity.getEntityTypeContactInfos().get(0).getPhoneNumbers());
132             patronDocument.setEmails(kimEnity.getEntityTypeContactInfos().get(0).getEmailAddresses());
133             LoanProcessor loanProcessor = new LoanProcessor();
134             OleDeliverRequestDocumentHelperServiceImpl requestService = new OleDeliverRequestDocumentHelperServiceImpl();
135             try{
136                 patronDocument.setOleLoanDocuments(loanProcessor.getPatronLoanedItem(patronDocument.getOlePatronId()));
137                 patronDocument.setOleTemporaryCirculationHistoryRecords(loanProcessor.getPatronTemporaryCirculationHistoryRecords(patronDocument.getOlePatronId()));
138             }catch (Exception e){
139                 LOG.error("Cannot fetch the patron loaned items.");
140             }
141         }
142 
143         return patronDocument;
144     }
145 
146 }