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