View Javadoc
1   package org.kuali.ole.deliver.util;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.apache.log4j.Logger;
5   import org.kuali.ole.OLEConstants;
6   import org.kuali.ole.deliver.bo.OleDeliverRequestBo;
7   import org.kuali.ole.deliver.bo.OlePatronDocument;
8   import org.kuali.ole.deliver.controller.checkout.CircUtilController;
9   import org.kuali.ole.deliver.drools.DroolsEngineLogger;
10  import org.kuali.ole.describe.keyvalue.LocationValuesBuilder;
11  import org.kuali.ole.docstore.common.document.content.instance.CallNumber;
12  import org.kuali.ole.docstore.common.document.content.instance.OleHoldings;
13  import org.kuali.ole.docstore.common.document.content.instance.xstream.HoldingOlemlRecordProcessor;
14  import org.kuali.rice.core.api.config.property.ConfigContext;
15  import org.kuali.rice.kim.api.permission.PermissionService;
16  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
17  import org.kuali.rice.krad.service.BusinessObjectService;
18  import org.kuali.rice.krad.service.KRADServiceLocator;
19  import org.kuali.rice.krad.util.GlobalVariables;
20  
21  import java.util.ArrayList;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  
26  /**
27   * Created by pvsubrah on 6/3/15.
28   */
29  public class OlePatronRecordUtil extends CircUtilController {
30      private static final Logger LOG = Logger.getLogger(OlePatronRecordUtil.class);
31      private BusinessObjectService businessObjectService;
32  
33      public OlePatronDocument getPatronRecordByBarcode(String barcode) throws Exception {
34          LOG.debug("Inside the getPatronBarcodeRecord method");
35          StringBuffer values_StringBuffer = new StringBuffer();
36  
37          try {
38              Map barMap = new HashMap();
39              barMap.put(OLEConstants.OlePatron.BARCODE, barcode);
40              List<OlePatronDocument> matching = (List<OlePatronDocument>) getBusinessObjectService().findMatching(OlePatronDocument.class, barMap);
41  
42              if (matching != null && matching.size() > 0) {
43                  OlePatronDocument olePatronDocument = matching.get(0);
44                  if (GlobalVariables.getUserSession() != null) {
45                      olePatronDocument.setPatronRecordURL(patronNameURL(GlobalVariables.getUserSession().getPrincipalId(), olePatronDocument.getOlePatronId()));
46                  }
47                  return olePatronDocument;
48              } else {
49                  LOG.error(OLEConstants.PTRN_BARCD_NOT_EXT);
50                  throw new Exception(OLEConstants.PTRN_BARCD_NOT_EXT);
51              }
52          } catch (Exception e) {
53              LOG.error(OLEConstants.PTRN_BARCD_NOT_EXT + e, e);
54              values_StringBuffer.append(OLEConstants.PTRN_BARCD_NOT_EXT + "  " + OLEConstants.PTRN_START_LINK + ConfigContext.getCurrentContextConfig().getProperty("ole.fs.url.base") + OLEConstants.PTRN_END_LINK);
55              throw new Exception(values_StringBuffer.toString());
56          }
57  
58      }
59  
60      public String patronNameURL(String loginUser, String patronId) {
61          boolean canEdit = canEdit(loginUser);
62          String patronNameURL = "";
63          if (canEdit) {
64              patronNameURL = OLEConstants.ASSIGN_EDIT_PATRON_ID + patronId + OLEConstants.ASSIGN_PATRON_MAINTENANCE_EDIT;
65          } else {
66              patronNameURL = OLEConstants.ASSIGN_INQUIRY_PATRON_ID + patronId + OLEConstants.ASSIGN_PATRON_INQUIRY;
67          }
68          return patronNameURL;
69      }
70  
71      public boolean canEdit(String principalId) {
72          PermissionService service = KimApiServiceLocator.getPermissionService();
73          return service.hasPermission(principalId, OLEConstants.OlePatron.PATRON_NAMESPACE, OLEConstants.EDIT_PATRON);
74      }
75  
76      public DroolsResponse fireRules(OlePatronDocument olePatronDocument, String[] expectedRules) {
77          List facts = new ArrayList();
78          facts.add(olePatronDocument);
79          DroolsResponse droolsResponse = new DroolsResponse();
80          facts.add(droolsResponse);
81  
82          fireRules(facts, expectedRules, "general-checks");
83  
84          return droolsResponse;
85      }
86  
87  
88      public BusinessObjectService getBusinessObjectService() {
89          if (null == businessObjectService) {
90              businessObjectService = KRADServiceLocator.getBusinessObjectService();
91          }
92          return businessObjectService;
93      }
94  
95      public void setBusinessObjectService(BusinessObjectService businessObjectService) {
96          this.businessObjectService = businessObjectService;
97      }
98  
99  
100     /**
101      * This method will return the request records along with item information
102       * @param olePatronDocument
103      * @return
104      * @throws Exception
105      */
106     public List<OleDeliverRequestBo> getPatronRequestRecords(OlePatronDocument olePatronDocument) throws Exception {
107         LOG.debug("Inside the getPatronRequestRecords method");
108         List<OleDeliverRequestBo> matchingRequest = null;
109         if(olePatronDocument.getOleDeliverRequestBos()!=null && olePatronDocument.getOleDeliverRequestBos().size()>0){
110             matchingRequest = olePatronDocument.getOleDeliverRequestBos();
111         }
112         else{
113             Map barMap = new HashMap();
114             barMap.put(OLEConstants.OleDeliverRequest.BORROWER_ID, olePatronDocument.getOlePatronId());
115             matchingRequest = (List<OleDeliverRequestBo>) getBusinessObjectService().findMatching(OleDeliverRequestBo.class, barMap);
116         }
117         for (int itemid = 0; itemid < matchingRequest.size(); itemid++) {
118             String itemUuid = matchingRequest.get(itemid).getItemUuid();
119             org.kuali.ole.docstore.common.document.Item item = getDocstoreClientLocator().getDocstoreClient().retrieveItem(itemUuid);
120             org.kuali.ole.docstore.common.document.content.instance.Item itemContent = getItemOlemlRecordProcessor().fromXML(item.getContent());
121             HoldingOlemlRecordProcessor holdingOlemlRecordProcessor = new HoldingOlemlRecordProcessor();
122             OleHoldings oleHoldings = holdingOlemlRecordProcessor.fromXML(item.getHolding().getContent());
123             OleDeliverRequestBo oleDeliverRequestBo = matchingRequest.get(itemid);
124             if (oleDeliverRequestBo.getItemUuid().equals(item.getId())) {
125                 oleDeliverRequestBo.setTitle(item.getHolding().getBib().getTitle());
126                 oleDeliverRequestBo.setAuthor(item.getHolding().getBib().getAuthor());
127                 oleDeliverRequestBo.setCallNumber(getItemCallNumber(itemContent.getCallNumber(),oleHoldings.getCallNumber()));
128                 oleDeliverRequestBo.setCopyNumber(itemContent.getCopyNumber());
129                 oleDeliverRequestBo.setVolumeNumber(itemContent.getVolumeNumber());
130                 oleDeliverRequestBo.setItemStatus(itemContent.getItemStatus().getCodeValue());
131                 oleDeliverRequestBo.setItemType(itemContent.getItemType().getCodeValue());
132                 oleDeliverRequestBo.setChronology(itemContent.getChronology());
133                 oleDeliverRequestBo.setEnumeration(itemContent.getEnumeration());
134                 if (oleDeliverRequestBo.getCirculationLocationCode() != null && !oleDeliverRequestBo.getCirculationLocationCode().equals("")) {
135                     LocationValuesBuilder locationValuesBuilder = new LocationValuesBuilder();
136                     locationValuesBuilder.getLocation(itemContent, oleDeliverRequestBo, item.getHolding().getId());
137                 }
138 
139             }
140         }
141         return matchingRequest;
142     }
143 
144     private String getItemCallNumber(CallNumber itemCallNumber,CallNumber holdingCallNumber) throws Exception {
145         LOG.debug("Inside the getItemCallNumber method");
146         String callNumber = "";
147 
148         if (itemCallNumber != null && StringUtils.isNotBlank(itemCallNumber.getType())) {
149             callNumber += itemCallNumber.getType() + OLEConstants.DELIMITER_DASH;
150         }else if(holdingCallNumber != null && StringUtils.isNotBlank(holdingCallNumber.getType())){
151             callNumber += holdingCallNumber.getType() + OLEConstants.DELIMITER_DASH;
152         }
153         if (itemCallNumber != null && StringUtils.isNotBlank(itemCallNumber.getPrefix())) {
154             callNumber += itemCallNumber.getPrefix() + OLEConstants.DELIMITER_DASH;
155         }else if(holdingCallNumber != null && StringUtils.isNotBlank(holdingCallNumber.getPrefix())){
156             callNumber += holdingCallNumber.getPrefix() + OLEConstants.DELIMITER_DASH;
157         }
158         if (itemCallNumber != null && StringUtils.isNotBlank(itemCallNumber.getNumber())) {
159             callNumber += itemCallNumber.getNumber();
160         }else if(holdingCallNumber != null && StringUtils.isNotBlank(holdingCallNumber.getNumber())){
161             callNumber += holdingCallNumber.getNumber();
162         }
163 
164         return callNumber;
165     }
166     }
167 
168