View Javadoc
1   package org.kuali.ole.deliver.inquiry;
2   
3   import org.kuali.ole.OLEConstants;
4   import org.kuali.ole.deliver.bo.OleDeliverRequestBo;
5   import org.kuali.ole.deliver.service.OleDeliverRequestDocumentHelperServiceImpl;
6   import org.kuali.ole.sys.context.SpringContext;
7   import org.kuali.ole.util.DocstoreUtil;
8   import org.kuali.rice.kim.impl.identity.entity.EntityBo;
9   import org.kuali.rice.kim.impl.identity.name.EntityNameBo;
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.KRADServiceLocator;
15  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
16  import org.kuali.rice.krad.service.ModuleService;
17  
18  import java.security.GeneralSecurityException;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  /**
24   * Created with IntelliJ IDEA.
25   * User: ?
26   * Date: 10/24/12
27   * Time: 5:19 PM
28   * To change this template use File | Settings | File Templates.
29   */
30  public class OleDeliverRequestInquirableImpl extends InquirableImpl {
31      public OleDeliverRequestDocumentHelperServiceImpl oleDeliverRequestDocumentHelperService = new OleDeliverRequestDocumentHelperServiceImpl();
32      private DocstoreUtil docstoreUtil;
33      public DocstoreUtil getDocstoreUtil() {
34  
35          if (docstoreUtil == null) {
36              docstoreUtil = SpringContext.getBean(DocstoreUtil.class);
37  
38          }
39          return docstoreUtil;
40      }
41  
42  
43  
44      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OleDeliverRequestInquirableImpl.class);
45  
46      @Override
47      public Object retrieveDataObject(Map<String, String> parameters) {
48          if (dataObjectClass == null) {
49              LOG.error("Data object class must be set in inquirable before retrieving the object");
50              throw new RuntimeException("Data object class must be set in inquirable before retrieving the object");
51          }
52  
53          // build list of key values from the map parameters
54          List<String> pkPropertyNames = getDataObjectMetaDataService().listPrimaryKeyFieldNames(dataObjectClass);
55  
56          // some classes might have alternate keys defined for retrieving
57          List<List<String>> alternateKeyNames = this.getAlternateKeysForClass(dataObjectClass);
58  
59          // add pk set as beginning so it will be checked first for match
60          alternateKeyNames.add(0, pkPropertyNames);
61  
62          List<String> dataObjectKeySet = retrieveKeySetFromMap(alternateKeyNames, parameters);
63          if ((dataObjectKeySet == null) || dataObjectKeySet.isEmpty()) {
64              LOG.warn("Matching key set not found in request for class: " + getDataObjectClass());
65  
66              return null;
67          }
68  
69          // found key set, now build map of key values pairs we can use to retrieve the object
70          Map<String, Object> keyPropertyValues = new HashMap<String, Object>();
71          for (String keyPropertyName : dataObjectKeySet) {
72              String keyPropertyValue = parameters.get(keyPropertyName);
73  
74              // uppercase value if needed
75              Boolean forceUppercase = Boolean.FALSE;
76              try {
77                  forceUppercase = getDataDictionaryService().getAttributeForceUppercase(dataObjectClass,
78                          keyPropertyName);
79              } catch (UnknownBusinessClassAttributeException ex) {
80                  // swallowing exception because this check for ForceUppercase would
81                  // require a DD entry for the attribute, and we will just set force uppercase to false
82                  LOG.warn("Data object class "
83                          + dataObjectClass
84                          + " property "
85                          + keyPropertyName
86                          + " should probably have a DD definition.", ex);
87              }
88  
89              if (forceUppercase.booleanValue() && (keyPropertyValue != null)) {
90                  keyPropertyValue = keyPropertyValue.toUpperCase();
91              }
92  
93              // check security on key field
94              if (getDataObjectAuthorizationService().attributeValueNeedsToBeEncryptedOnFormsAndLinks(dataObjectClass,
95                      keyPropertyName)) {
96                  try {
97                      keyPropertyValue = getEncryptionService().decrypt(keyPropertyValue);
98                  } catch (GeneralSecurityException e) {
99                      LOG.error("Data object class "
100                             + dataObjectClass
101                             + " property "
102                             + keyPropertyName
103                             + " should have been encrypted, but there was a problem decrypting it.", e);
104                     throw new RuntimeException("Data object class "
105                             + dataObjectClass
106                             + " property "
107                             + keyPropertyName
108                             + " should have been encrypted, but there was a problem decrypting it.", e);
109                 }
110             }
111 
112             keyPropertyValues.put(keyPropertyName, keyPropertyValue);
113         }
114 
115         // now retrieve the object based on the key set
116         Object dataObject = null;
117 
118         ModuleService moduleService = KRADServiceLocatorWeb.getKualiModuleService().getResponsibleModuleService(
119                 getDataObjectClass());
120         if (moduleService != null && moduleService.isExternalizable(getDataObjectClass())) {
121             dataObject = moduleService.getExternalizableBusinessObject(getDataObjectClass().asSubclass(
122                     ExternalizableBusinessObject.class), keyPropertyValues);
123         } else if (BusinessObject.class.isAssignableFrom(getDataObjectClass())) {
124             dataObject = getBusinessObjectService().findByPrimaryKey(getDataObjectClass().asSubclass(
125                     BusinessObject.class), keyPropertyValues);
126         }
127         OleDeliverRequestBo oleDeliverRequestBo = (OleDeliverRequestBo) dataObject;
128         if (oleDeliverRequestBo.getOlePatron() != null && oleDeliverRequestBo.getOlePatron().getEntity() != null && oleDeliverRequestBo.getOlePatron().getEntity().getNames() != null && oleDeliverRequestBo.getOlePatron().getEntity().getNames().size() > 0) {
129             EntityNameBo nameBo = oleDeliverRequestBo.getOlePatron().getEntity().getNames().get(0);
130             oleDeliverRequestBo.getOlePatron().setPatronName(oleDeliverRequestBo.getOlePatron().getEntity().getNames().get(0).getFirstName() + " " + oleDeliverRequestBo.getOlePatron().getEntity().getNames().get(0).getLastName());
131         }
132         EntityNameBo entityNameBo = getEntityNameBo(oleDeliverRequestBo.getBorrowerId());
133         if (oleDeliverRequestBo.getBorrowerId() != null && entityNameBo != null) {
134             oleDeliverRequestBo.setFirstName(entityNameBo.getFirstName());
135             oleDeliverRequestBo.setLastName(entityNameBo.getLastName());
136         }
137         EntityNameBo entityNameBo1 = getEntityNameBo(oleDeliverRequestBo.getProxyBorrowerId());
138         if (oleDeliverRequestBo.getProxyBorrowerId() != null && entityNameBo1 != null) {
139             oleDeliverRequestBo.setProxyBorrowerName(entityNameBo1.getFirstName() + entityNameBo1.getLastName());
140             oleDeliverRequestBo.setProxyBorrowerFirstName(entityNameBo1.getFirstName());
141             oleDeliverRequestBo.setProxyBorrowerLastName(entityNameBo1.getLastName());
142         }
143         if (oleDeliverRequestBo.getOperatorCreateId() != null && !oleDeliverRequestBo.getOperatorCreateId().isEmpty()) {
144             oleDeliverRequestBo.setRequestCreator(OLEConstants.OleDeliverRequest.REQUESTER_OPERATOR);
145         } else if (oleDeliverRequestBo.getProxyBorrowerId() != null && !oleDeliverRequestBo.getProxyBorrowerId().isEmpty()) {
146             oleDeliverRequestBo.setRequestCreator(OLEConstants.OleDeliverRequest.REQUESTER_PROXY_PATRON);
147         } else {
148             oleDeliverRequestBo.setRequestCreator(OLEConstants.OleDeliverRequest.REQUESTER_PATRON);
149         }
150         // oleDeliverRequestBo =  oleDeliverRequestDocumentHelperService.processItem(oleDeliverRequestBo);
151         getDocstoreUtil().isItemAvailableInDocStore(oleDeliverRequestBo);
152         return oleDeliverRequestBo;
153     }
154 
155     public EntityNameBo getEntityNameBo(String entityId) {
156         EntityBo entityBo = getEntity(entityId);
157         if (entityBo != null) {
158             if (entityBo.getNames() != null && entityBo.getNames().size() > 0) {
159                 return entityBo.getNames().get(0);
160             }
161         }
162         return null;
163     }
164 
165     public EntityBo getEntity(String entityId) {
166         Map<String, String> entityMap = new HashMap<>();
167         entityMap.put("id", entityId);
168         List<EntityBo> entityBoList = (List<EntityBo>) KRADServiceLocator.getBusinessObjectService().findMatching(EntityBo.class, entityMap);
169         if (entityBoList.size() > 0)
170             return entityBoList.get(0);
171         return null;
172     }
173 
174 }