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.ole.deliver.processor.LoanProcessor;
8   import org.kuali.ole.deliver.service.OleDeliverRequestDocumentHelperServiceImpl;
9   import org.kuali.rice.core.api.config.property.ConfigContext;
10  import org.kuali.rice.kim.impl.identity.address.EntityAddressBo;
11  import org.kuali.rice.kim.impl.identity.affiliation.EntityAffiliationBo;
12  import org.kuali.rice.kim.impl.identity.employment.EntityEmploymentBo;
13  import org.kuali.rice.kim.impl.identity.entity.EntityBo;
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.KRADServiceLocator;
19  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
20  import org.kuali.rice.krad.service.ModuleService;
21  
22  import java.security.GeneralSecurityException;
23  import java.util.ArrayList;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  
28  /**
29   * OlePatronInquirableImpl supports to get the data object and patron document.
30   */
31  public class OlePatronInquirableImpl extends InquirableImpl {
32      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OlePatronInquirableImpl.class);
33  
34      /**
35       * This method will retrieve the patron document based on the olePatronId
36       *
37       * @param parameters
38       * @return patronDocument(Object)
39       */
40      String baseUrl = ConfigContext.getCurrentContextConfig().getProperty(OLEPropertyConstants.OLE_URL_BASE);
41  
42      public Object retrieveDataObject(Map<String, String> parameters) {
43          if (dataObjectClass == null) {
44              LOG.error("Data object class must be set in inquirable before retrieving the object");
45              throw new RuntimeException("Data object class must be set in inquirable before retrieving the object");
46          }
47  
48          // build list of key values from the map parameters
49          List<String> pkPropertyNames = getDataObjectMetaDataService().listPrimaryKeyFieldNames(dataObjectClass);
50  
51          // some classes might have alternate keys defined for retrieving
52          List<List<String>> alternateKeyNames = this.getAlternateKeysForClass(dataObjectClass);
53  
54          // add pk set as beginning so it will be checked first for match
55          alternateKeyNames.add(0, pkPropertyNames);
56  
57          List<String> dataObjectKeySet = retrieveKeySetFromMap(alternateKeyNames, parameters);
58          if ((dataObjectKeySet == null) || dataObjectKeySet.isEmpty()) {
59              LOG.warn("Matching key set not found in request for class: " + getDataObjectClass());
60  
61              return null;
62          }
63          String patronHome = parameters.get("flow");
64  
65          // found key set, now build map of key values pairs we can use to retrieve the object
66          Map<String, Object> keyPropertyValues = new HashMap<String, Object>();
67          for (String keyPropertyName : dataObjectKeySet) {
68              String keyPropertyValue = parameters.get(keyPropertyName);
69  
70              // uppercase value if needed
71              Boolean forceUppercase = Boolean.FALSE;
72              try {
73                  forceUppercase = getDataDictionaryService().getAttributeForceUppercase(dataObjectClass,
74                          keyPropertyName);
75              } catch (UnknownBusinessClassAttributeException ex) {
76                  // swallowing exception because this check for ForceUppercase would
77                  // require a DD entry for the attribute, and we will just set force uppercase to false
78                  LOG.warn("Data object class "
79                          + dataObjectClass
80                          + " property "
81                          + keyPropertyName
82                          + " should probably have a DD definition.", ex);
83              }
84  
85              if (forceUppercase.booleanValue() && (keyPropertyValue != null)) {
86                  keyPropertyValue = keyPropertyValue.toUpperCase();
87              }
88  
89              // check security on key field
90              if (getDataObjectAuthorizationService().attributeValueNeedsToBeEncryptedOnFormsAndLinks(dataObjectClass,
91                      keyPropertyName)) {
92                  try {
93                      keyPropertyValue = getEncryptionService().decrypt(keyPropertyValue);
94                  } catch (GeneralSecurityException e) {
95                      LOG.error("Data object class "
96                              + dataObjectClass
97                              + " property "
98                              + keyPropertyName
99                              + " should have been encrypted, but there was a problem decrypting it.", e);
100                     throw new RuntimeException("Data object class "
101                             + dataObjectClass
102                             + " property "
103                             + keyPropertyName
104                             + " should have been encrypted, but there was a problem decrypting it.", e);
105                 }
106             }
107 
108             keyPropertyValues.put(keyPropertyName, keyPropertyValue);
109         }
110 
111         // now retrieve the object based on the key set
112         Object dataObject = null;
113 
114         ModuleService moduleService = KRADServiceLocatorWeb.getKualiModuleService().getResponsibleModuleService(
115                 getDataObjectClass());
116         if (moduleService != null && moduleService.isExternalizable(getDataObjectClass())) {
117             dataObject = moduleService.getExternalizableBusinessObject(getDataObjectClass().asSubclass(
118                     ExternalizableBusinessObject.class), keyPropertyValues);
119         } else if (BusinessObject.class.isAssignableFrom(getDataObjectClass())) {
120             dataObject = getBusinessObjectService().findByPrimaryKey(getDataObjectClass().asSubclass(
121                     BusinessObject.class), keyPropertyValues);
122         }
123         //OleEntityAddressBo entityAddressBo = new OleEntityAddressBo();
124         EntityAddressBo entityAddress = new EntityAddressBo();
125         List<OleEntityAddressBo> oleEntityAddressList = new ArrayList<OleEntityAddressBo>();
126         OlePatronDocument patronDocument = (OlePatronDocument) dataObject;
127         if (patronDocument != null) {
128             EntityBo kimEnity = patronDocument.getEntity();
129             if (kimEnity.getNames().size() > 0) {
130                 patronDocument.setName(kimEnity.getNames().get(0));
131             }
132             if(kimEnity.getEntityTypeContactInfos().size()>0){
133                 List<EntityAddressBo> entityAddressList = kimEnity.getEntityTypeContactInfos().get(0).getAddresses();
134                 for (EntityAddressBo entityAdd : entityAddressList) {
135                     OleEntityAddressBo entityAddressBo = new OleEntityAddressBo();
136                     entityAddressBo.setEntityAddressBo(entityAdd);
137                     Map addMap = new HashMap();
138                     //addMap.put(OLEConstants.OlePatron.OLE_ADDRESS_ID, entityAdd.getId());
139                     addMap.put("id", entityAdd.getId());
140                     OleAddressBo oleAddressBo = getBusinessObjectService().findByPrimaryKey(OleAddressBo.class, addMap);
141                     entityAddressBo.setOleAddressBo(oleAddressBo);
142                     oleEntityAddressList.add(entityAddressBo);
143                 }
144                 patronDocument.setOleEntityAddressBo(oleEntityAddressList);
145                 patronDocument.setPhones(kimEnity.getEntityTypeContactInfos().get(0).getPhoneNumbers());
146                 patronDocument.setEmails(kimEnity.getEntityTypeContactInfos().get(0).getEmailAddresses());
147                 patronDocument.setPatronAffiliations(getPatronAffiliationFromEntity(kimEnity.getAffiliations(), kimEnity.getEmploymentInformation()));
148                 if (patronHome != null) {
149                     if (patronHome.equalsIgnoreCase("start"))
150                         patronDocument.setPatronHomePage(false);
151                     else
152                         patronDocument.setPatronHomePage(true);
153                 }
154             }
155 
156 
157         }
158 
159         if(patronDocument.getOleLoanDocuments()!=null){
160             patronDocument.setLoanCount(patronDocument.getOleLoanDocuments().size());
161         }
162         if(patronDocument.getOleTemporaryCirculationHistoryRecords()!=null){
163             patronDocument.setTempCirculationHistoryCount(patronDocument.getOleTemporaryCirculationHistoryRecords().size());
164         }
165         if(patronDocument.getOleDeliverRequestBos()!=null){
166             patronDocument.setRequestedItemRecordsCount(patronDocument.getOleDeliverRequestBos().size());
167         }
168 
169 
170 
171         return patronDocument;
172     }
173 
174     private List<OlePatronAffiliation> getPatronAffiliationFromEntity(List<EntityAffiliationBo> affiliations,
175                                                                       List<EntityEmploymentBo> employeeDetails) {
176         List<OlePatronAffiliation> patronAffiliations = new ArrayList<OlePatronAffiliation>();
177         for (EntityAffiliationBo entityAffiliationBo : affiliations) {
178             OlePatronAffiliation patronAffiliation = new OlePatronAffiliation(entityAffiliationBo);
179             List<EntityEmploymentBo> employmentBos = new ArrayList<EntityEmploymentBo>();
180             for (EntityEmploymentBo entityEmploymentBo : employeeDetails) {
181                 if (patronAffiliation.getEntityAffiliationId().equalsIgnoreCase(entityEmploymentBo.getEntityAffiliationId())) {
182                     employmentBos.add(entityEmploymentBo);
183                 }
184                 patronAffiliation.setEmployments(employmentBos);
185             }
186             patronAffiliations.add(patronAffiliation);
187         }
188         return patronAffiliations;
189     }
190 
191     public String getTempCircRecords(String olePatronId) {
192         String url = baseUrl + "/portal.do?channelTitle=Patron&channelUrl=" + baseUrl + "/ole-kr-krad/temporaryCirculationRecord?viewId=OleTemporaryCirculationHistoryRecordView&amp;methodToCall=viewTempCircRecords&amp;patronId=" + olePatronId;
193         return url;
194     }
195 
196     public String getLoanedRecords(String olePatronId) {
197 
198         String url = baseUrl + "/portal.do?channelTitle=Patron&channelUrl=" + baseUrl + "/ole-kr-krad/patronLoanedRecord?viewId=OlePatronLoanedRecordView&amp;methodToCall=viewLoanedRecords&amp;patronId=" + olePatronId;
199         return url;
200     }
201 
202     public String getRequestedRecords(String olePatronId) {
203         String url = baseUrl + "/portal.do?channelTitle=Patron&channelUrl=" + baseUrl + "/ole-kr-krad/patronRequestedRecord?viewId=OlePatronRequestedRecordView&amp;methodToCall=viewRequestedRecords&amp;patronId=" + olePatronId;
204         return url;
205     }
206 
207     public String getCountOfPendingRequests(String itemId){
208         Map itemMap = new HashMap();
209         itemMap.put(OLEConstants.OleDeliverRequest.ITEM_ID, itemId);
210         List<OleDeliverRequestBo> oleDeliverRequestBoList = (List<OleDeliverRequestBo>) getBusinessObjectService().findMatching(OleDeliverRequestBo.class, itemMap);
211         if (oleDeliverRequestBoList!=null && oleDeliverRequestBoList.size()>0) {
212 
213             return "View all requests";
214         }
215         else
216             return " ";
217     }
218 
219     public String getHrefLink(String item,String instance,String bib){
220        String inquiryUrl= ConfigContext.getCurrentContextConfig().getProperty(OLEPropertyConstants.OLE_URL_BASE)+
221                "/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+"";
222         return inquiryUrl;
223     }
224 
225 }