View Javadoc

1   package org.kuali.ole.ncip.service.impl;
2   
3   import org.apache.log4j.Logger;
4   import org.kuali.ole.BibliographicRecordHandler;
5   import org.kuali.ole.OLEConstants;
6   import org.kuali.ole.OLEParameterConstants;
7   import org.kuali.ole.deliver.bo.OleCirculationDesk;
8   import org.kuali.ole.deliver.bo.OleCirculationDeskDetail;
9   import org.kuali.ole.deliver.bo.OleLoanDocument;
10  import org.kuali.ole.deliver.bo.OlePatronDocument;
11  import org.kuali.ole.deliver.processor.LoanProcessor;
12  import org.kuali.ole.describe.service.DocstoreHelperService;
13  import org.kuali.ole.docstore.discovery.service.QueryServiceImpl;
14  import org.kuali.ole.docstore.model.xmlpojo.ingest.*;
15  import org.kuali.ole.docstore.model.xmlpojo.work.bib.marc.DataField;
16  import org.kuali.ole.docstore.model.xmlpojo.work.bib.marc.SubField;
17  import org.kuali.ole.docstore.model.xmlpojo.work.instance.oleml.*;
18  import org.kuali.ole.docstore.model.xstream.ingest.RequestHandler;
19  import org.kuali.ole.docstore.model.xstream.ingest.ResponseHandler;
20  import org.kuali.ole.docstore.model.xstream.work.instance.oleml.WorkInstanceOlemlRecordProcessor;
21  import org.kuali.ole.ncip.bo.*;
22  import org.kuali.ole.ncip.converter.OLECheckInItemConverter;
23  import org.kuali.ole.ncip.converter.OLECheckOutItemConverter;
24  import org.kuali.ole.pojo.bib.BibliographicRecord;
25  import org.kuali.rice.core.api.config.property.ConfigContext;
26  import org.kuali.rice.kim.impl.identity.address.EntityAddressBo;
27  import org.kuali.rice.kim.impl.identity.email.EntityEmailBo;
28  import org.kuali.rice.kim.impl.identity.entity.EntityBo;
29  import org.kuali.rice.kim.impl.identity.name.EntityNameBo;
30  import org.kuali.rice.krad.service.BusinessObjectService;
31  import org.kuali.ole.docstore.model.xmlpojo.work.instance.oleml.Location;
32  import org.kuali.rice.krad.service.KRADServiceLocator;
33  
34  import java.net.URLEncoder;
35  import java.sql.Timestamp;
36  import java.util.*;
37  
38  /**
39   * Created with IntelliJ IDEA.
40   * User: maheswarang
41   * Date: 7/21/13
42   * Time: 3:47 PM
43   * To change this template use File | Settings | File Templates.
44   */
45  class OLECirculationHelperServiceImpl {
46      private static final Logger LOG = Logger.getLogger(OLECirculationHelperServiceImpl.class);
47      private static final String DOCSTORE_URL = "docstore.url";
48      private final String CREATE_NEW_DOCSTORE_RECORD_QUERY_STRING = "docAction=ingestContent&stringContent=";
49      private BusinessObjectService businessObjectService=KRADServiceLocator.getBusinessObjectService();
50      private LoanProcessor loanProcessor = new LoanProcessor();
51      private OLECheckInItemConverter oleCheckInItemConverter = new OLECheckInItemConverter();
52      private OLECheckOutItemConverter oleCheckOutItemConverter=new OLECheckOutItemConverter();
53      public BusinessObjectService getBusinessObjectService() {
54          return businessObjectService;
55      }
56  
57      public void setBusinessObjectService(BusinessObjectService businessObjectService) {
58          this.businessObjectService = businessObjectService;
59      }
60  
61      public OLELookupUser initialiseLookupUser(OlePatronDocument olePatronDocument){
62          OLELookupUser oleLookupUser = new OLELookupUser();
63          oleLookupUser.setPatronId(olePatronDocument.getOlePatronId());
64          EntityEmailBo entityEmailBo = getDefaultEmailBo(olePatronDocument.getOlePatronId());
65          if(entityEmailBo!=null){
66              oleLookupUser.setPatronEmail(entityEmailBo);
67          }
68          EntityAddressBo entityAddressBo =getDefaultAddressBo(olePatronDocument.getOlePatronId());
69          if(entityAddressBo!=null){
70              oleLookupUser.setPatronAddress(entityAddressBo);
71          }
72          if(getPatronPrivilege(olePatronDocument)!=null){
73              oleLookupUser.setOleUserPrivileges(getPatronPrivilege(olePatronDocument));
74          }
75           EntityNameBo entityNameBo = getEntityNameBo(olePatronDocument.getOlePatronId());
76          if(entityNameBo!=null){
77              oleLookupUser.setPatronName(entityNameBo);
78          }
79       return oleLookupUser;
80      }
81  
82      public EntityBo getEntity(String entityId){
83         Map<String,String> entityMap = new HashMap<>();
84          entityMap.put("id", entityId);
85          List<EntityBo> entityBoList = (List<EntityBo>)businessObjectService.findMatching(EntityBo.class,entityMap);
86          if(entityBoList.size()>0)
87              return entityBoList.get(0);
88              return null;
89      }
90  
91      public EntityEmailBo getDefaultEmailBo(String entityId){
92          EntityBo entityBo = getEntity(entityId);
93          if(entityBo!=null){
94                      if(entityBo.getEntityTypeContactInfos()!=null && entityBo.getEntityTypeContactInfos().size()>0)
95                          if(entityBo.getEntityTypeContactInfos().get(0).getEmailAddresses()!=null && entityBo.getEntityTypeContactInfos().get(0).getEmailAddresses().size()>0){
96                              for(EntityEmailBo entityEmailBo : entityBo.getEntityTypeContactInfos().get(0).getEmailAddresses()){
97                                  if(entityEmailBo.getDefaultValue()){
98                                      return entityEmailBo;
99                                  }
100                             }
101                         }
102                    }
103         return null;
104     }
105 
106     public EntityAddressBo getDefaultAddressBo(String entityId){
107         EntityBo entityBo = getEntity(entityId);
108         if(entityBo!=null){
109             if(entityBo.getEntityTypeContactInfos()!=null && entityBo.getEntityTypeContactInfos().size()>0)
110                 if(entityBo.getEntityTypeContactInfos().get(0).getDefaultAddress()!=null){
111                         return  entityBo.getEntityTypeContactInfos().get(0).getDefaultAddress();
112                     }
113         }
114         return null;
115     }
116 
117     public EntityNameBo getEntityNameBo(String entityId){
118         EntityBo entityBo = getEntity(entityId);
119         if(entityBo!=null){
120             if(entityBo.getNames()!=null && entityBo.getNames().size()>0){
121                return entityBo.getNames().get(0);
122             }
123         }
124         return null;
125     }
126 
127     public List<OLEUserPrivilege> getPatronPrivilege(OlePatronDocument olePatronDocument){
128         List<OLEUserPrivilege> userPrivilegeList;
129 
130         String pdfLocationSystemParam = loanProcessor.getParameter(OLEParameterConstants.PDF_LOCATION);
131         if (LOG.isInfoEnabled()) {
132             LOG.info("System Parameter for PDF_Location --> " + pdfLocationSystemParam);
133         }
134         if (pdfLocationSystemParam == null || pdfLocationSystemParam.trim().isEmpty()) {
135             pdfLocationSystemParam = ConfigContext.getCurrentContextConfig().getProperty("staging.directory") + "/";
136             if (LOG.isInfoEnabled()) {
137                 LOG.info("System Parameter for PDF_Location staging dir--> " + pdfLocationSystemParam);
138             }
139         } else{
140             pdfLocationSystemParam = ConfigContext.getCurrentContextConfig().getProperty("homeDirectory")+ "/" + pdfLocationSystemParam +"/";
141         }
142         if(olePatronDocument!=null){
143             userPrivilegeList=new ArrayList<OLEUserPrivilege>();
144                      OLEUserPrivilege courtesyPrivilege = new OLEUserPrivilege();
145             courtesyPrivilege.setUserPrivilegeType("Courtesy Notice");
146             courtesyPrivilege.setUserPrivilegeDescription("Eligible to receive the courtesy Notices");
147                      if(olePatronDocument.isCourtesyNotice())
148                          courtesyPrivilege.setUserPrivilegeStatus("true");
149             courtesyPrivilege.setUserPrivilegeStatus("false");
150                       userPrivilegeList.add(courtesyPrivilege);
151             OLEUserPrivilege deliverPrivilege = new OLEUserPrivilege();
152             deliverPrivilege.setUserPrivilegeType("Delivery");
153             deliverPrivilege.setUserPrivilegeDescription("Eligible to receive item");
154             if(olePatronDocument.isDeliveryPrivilege())
155                 deliverPrivilege.setUserPrivilegeStatus("true");
156             deliverPrivilege.setUserPrivilegeStatus("false");
157             userPrivilegeList.add(deliverPrivilege);
158             OLEUserPrivilege pagingPrivilege = new OLEUserPrivilege();
159             pagingPrivilege.setUserPrivilegeType("Paging");
160             pagingPrivilege.setUserPrivilegeDescription("Eligible to Page item");
161             if(olePatronDocument.isPagingPrivilege())
162                 pagingPrivilege.setUserPrivilegeStatus("true");
163             pagingPrivilege.setUserPrivilegeStatus("false");
164             userPrivilegeList.add(pagingPrivilege);
165             return userPrivilegeList;
166         }
167         return null;
168     }
169 
170     public String checkOutItem(String patronId,String operatorId,String itemBarcode){
171         OlePatronDocument olePatronDocument =null;
172         Map<String,String> patronMap = new HashMap<String,String>();
173         patronMap.put("olePatronId",patronId);
174         List<OlePatronDocument> olePatronDocumentList = (List<OlePatronDocument>)businessObjectService.findMatching(OlePatronDocument.class,patronMap);
175         if(olePatronDocumentList.size()>0){
176           olePatronDocument=olePatronDocumentList.get(0);
177         }else{
178             OLECheckOutItem oleCheckOutItem = new OLECheckOutItem();
179             oleCheckOutItem.setErrorMessage(OLEConstants.NO_PATRON_INFO);
180             return oleCheckOutItemConverter.generateCheckOutItemXml(oleCheckOutItem);
181         }
182         Map<String,String> circulationDeskDetailMap = new HashMap<String,String>();
183         circulationDeskDetailMap.put("operatorId",operatorId);
184         List<OleCirculationDeskDetail> oleCirculationDeskDetailList = (List<OleCirculationDeskDetail>)businessObjectService.findMatching(OleCirculationDeskDetail.class,circulationDeskDetailMap);
185         OleLoanDocument oleLoanDocument = new OleLoanDocument();
186         if(oleCirculationDeskDetailList!=null && oleCirculationDeskDetailList.size()>0){
187             for(OleCirculationDeskDetail oleCirculationDeskDetail : oleCirculationDeskDetailList){
188                 if(oleCirculationDeskDetail.isDefaultLocation()){
189             String circulationDeskId=oleCirculationDeskDetail.getCirculationDeskId();
190             oleLoanDocument.setCirculationLocationId(circulationDeskId);
191                     Map<String,String> circulationMap = new HashMap<String,String>();
192                      circulationMap.put("circulationDeskId",circulationDeskId);
193                    List<OleCirculationDesk> oleCirculationDeskList = (List<OleCirculationDesk>)businessObjectService.findMatching(OleCirculationDesk.class,circulationMap);
194                     if(oleCirculationDeskList.size()>0)
195                     oleLoanDocument.setOleCirculationDesk(oleCirculationDeskList.get(0));
196                     break;
197                 }
198             }
199         }else{
200             OLECheckOutItem oleCheckOutItem = new OLECheckOutItem();
201             oleCheckOutItem.setErrorMessage(OLEConstants.CIRCULATION_DESK_NOT_MAPPED_OPERATOR);
202             return oleCheckOutItemConverter.generateCheckOutItemXml(oleCheckOutItem);
203         }
204         try {
205             oleLoanDocument = loanProcessor.getLoanDocument(olePatronDocument.getBarcode(),null,true);
206             Map<String,String> circulationDeskDetailMaps = new HashMap<String,String>();
207             circulationDeskDetailMaps.put("operatorId",operatorId);
208             List<OleCirculationDeskDetail> oleCirculationDeskDetailLists = (List<OleCirculationDeskDetail>)businessObjectService.findMatching(OleCirculationDeskDetail.class,circulationDeskDetailMaps);
209             if(oleCirculationDeskDetailList!=null && oleCirculationDeskDetailList.size()>0){
210                 for(OleCirculationDeskDetail oleCirculationDeskDetail : oleCirculationDeskDetailList){
211                     if(oleCirculationDeskDetail.isDefaultLocation()){
212                         String circulationDeskId=oleCirculationDeskDetail.getCirculationDeskId();
213                         oleLoanDocument.setCirculationLocationId(circulationDeskId);
214                         Map<String,String> circulationMap = new HashMap<String,String>();
215                         circulationMap.put("circulationDeskId",circulationDeskId);
216                         List<OleCirculationDesk> oleCirculationDeskList = (List<OleCirculationDesk>)businessObjectService.findMatching(OleCirculationDesk.class,circulationMap);
217                         if(oleCirculationDeskList.size()>0)
218                             oleLoanDocument.setOleCirculationDesk(oleCirculationDeskList.get(0));
219                         break;
220                     }
221                 }
222             }
223             if(oleLoanDocument.getErrorMessage()==null || (oleLoanDocument.getErrorMessage()!=null && oleLoanDocument.getErrorMessage().isEmpty())){
224                          if(olePatronDocument!=null){
225 
226                              Map itemUuid = QueryServiceImpl.getInstance().getItemDetails(itemBarcode,null);
227                              Item oleItem = null;
228                              String itemXml = null;
229                              Date pastDueDate=oleLoanDocument.getLoanDueDate();
230                              oleLoanDocument.setInstanceUuid((String) itemUuid.get(OLEConstants.INSTANCE_UUID));
231                              oleLoanDocument.setItemUuid((String) itemUuid.get(OLEConstants.ITEM_UUID));
232                              oleLoanDocument.setBibUuid((String) itemUuid.get(OLEConstants.BIB_UUID));
233                              itemXml = loanProcessor.getItemXML(oleLoanDocument.getItemUuid());
234                              oleItem = loanProcessor.getItemPojo(itemXml);
235                              if(!oleItem.getItemStatus().getCodeValue().equalsIgnoreCase("LOANED")){
236                                  oleLoanDocument = loanProcessor.addLoan(olePatronDocument.getBarcode(),itemBarcode,oleLoanDocument);
237                              }else {
238                                 String checkInItemResponse= this.checkInItem(patronId,operatorId,itemBarcode,"N");
239                                  OLECheckInItem oleCheckInItem = (OLECheckInItem)oleCheckInItemConverter.generateCheckInItemObject(checkInItemResponse);
240                                  if(!oleCheckInItem.getErrorMessage().equals(OLEConstants.SUCCESSFULLEY_CHECKED_IN)){
241                                      OLECheckOutItem oleCheckOutItem = new OLECheckOutItem();
242                                      oleCheckOutItem.setErrorMessage(oleCheckInItem.getErrorMessage());
243                                      return oleCheckOutItemConverter.generateCheckOutItemXml(oleCheckOutItem);
244                                  }
245                                  oleLoanDocument = loanProcessor.addLoan(olePatronDocument.getBarcode(),itemBarcode,oleLoanDocument);
246                              }
247 
248                 if(oleLoanDocument.getErrorMessage()==null ||  (oleLoanDocument.getErrorMessage()!=null&&oleLoanDocument.getErrorMessage().isEmpty())){
249                    OLECheckOutItem oleCheckOutItem = new OLECheckOutItem();
250                     oleCheckOutItem.setDueDate(oleLoanDocument.getLoanDueDate().toString());
251                     oleCheckOutItem.setRenewalCount(oleLoanDocument.getNumberOfRenewals());
252                     oleCheckOutItem.setUserType(oleLoanDocument.getBorrowerTypeName());
253                     oleCheckOutItem.setBarcode(oleLoanDocument.getItemId());
254                     oleCheckOutItem.setPatronId(oleLoanDocument.getPatronId());
255                     if(oleLoanDocument.getOleItem()!=null && oleLoanDocument.getOleItem().getItemType()!=null){
256                         oleCheckOutItem.setItemType(oleLoanDocument.getOleItem().getItemType().getCodeValue());
257                     }
258                     oleCheckOutItem.setErrorMessage(OLEConstants.SUCCESSFULLEY_LOANED);
259                     return oleCheckOutItemConverter.generateCheckOutItemXml(oleCheckOutItem);
260 
261                 }else{
262                     OLECheckOutItem oleCheckOutItem = new OLECheckOutItem();
263                     oleCheckOutItem.setErrorMessage(oleLoanDocument.getErrorMessage());
264                     return oleCheckOutItemConverter.generateCheckOutItemXml(oleCheckOutItem);
265                 }
266                 }
267             }
268             else{
269                 OLECheckOutItem oleCheckOutItem = new OLECheckOutItem();
270                 oleCheckOutItem.setErrorMessage(oleLoanDocument.getErrorMessage());
271                 return oleCheckOutItemConverter.generateCheckOutItemXml(oleCheckOutItem);
272             }
273         }catch (Exception e) {
274             OLECheckOutItem oleCheckOutItem = new OLECheckOutItem();
275             oleCheckOutItem.setErrorMessage(OLEConstants.ITEM_BARCODE_DOESNOT_EXISTS);
276             return oleCheckOutItemConverter.generateCheckOutItemXml(oleCheckOutItem);
277         }
278        return null;
279     }
280 
281     public String checkInItem(String patronId, String operatorId, String itemBarcode,String deleteIndicator){
282         OleLoanDocument oleLoanDocument = new OleLoanDocument();
283 
284         try{
285            // oleLoanDocument= loanProcessor.returnLoan(oleLoanDocument);
286 
287                 Map<String,String> loanMap = new HashMap<String,String>();
288                 loanMap.put("itemId",itemBarcode);
289                 List<OleLoanDocument> oleLoanList = (List<OleLoanDocument>)businessObjectService.findMatching(OleLoanDocument.class,loanMap);
290                 if(oleLoanList.size()>0){
291                        oleLoanDocument=oleLoanList.get(0);
292                   String olePatronId = oleLoanList.get(0).getPatronId();
293                        Map<String,String> patronMap= new HashMap<String,String>();
294                        patronMap.put("olePatronId",olePatronId);
295                     List<OlePatronDocument>  olePatronDocumentList =(List<OlePatronDocument>) businessObjectService.findMatching(OlePatronDocument.class,patronMap);
296                        if(olePatronDocumentList.size()>0){
297                            oleLoanDocument.setOlePatron(olePatronDocumentList.get(0));
298                            oleLoanDocument.setBorrowerTypeCode(olePatronDocumentList.get(0).getOleBorrowerType().getBorrowerTypeCode());
299                        }
300                 }
301             Map<String,String> circulationDeskDetailMap = new HashMap<String,String>();
302             circulationDeskDetailMap.put("operatorId",operatorId);
303             List<OleCirculationDeskDetail> oleCirculationDeskDetailList = (List<OleCirculationDeskDetail>)businessObjectService.findMatching(OleCirculationDeskDetail.class,circulationDeskDetailMap);
304             if(oleCirculationDeskDetailList!=null && oleCirculationDeskDetailList.size()>0){
305                 for(OleCirculationDeskDetail oleCirculationDeskDetail : oleCirculationDeskDetailList){
306                     if(oleCirculationDeskDetail.isDefaultLocation()){
307                         String circulationDeskId=oleCirculationDeskDetail.getCirculationDeskId();
308                         oleLoanDocument.setCirculationLocationId(circulationDeskId);
309                         Map<String,String> circulationMap = new HashMap<String,String>();
310                         circulationMap.put("circulationDeskId",circulationDeskId);
311                         List<OleCirculationDesk> oleCirculationDeskList = (List<OleCirculationDesk>)businessObjectService.findMatching(OleCirculationDesk.class,circulationMap);
312                         if(oleCirculationDeskList.size()>0)
313                             oleLoanDocument.setOleCirculationDesk(oleCirculationDeskList.get(0));
314                         break;
315                     }
316                 }
317                 oleLoanDocument = loanProcessor.returnLoan(itemBarcode,oleLoanDocument);
318                 OLECheckInItem  oleCheckInItem = new OLECheckInItem();
319                 oleCheckInItem.setAuthor(oleLoanDocument.getAuthor());
320                 oleCheckInItem.setTitle(oleLoanDocument.getTitle());
321                 oleCheckInItem.setCallNumber(oleLoanDocument.getItemCallNumber());
322                 oleCheckInItem.setUserId(oleLoanDocument.getPatronId());
323                 oleCheckInItem.setUserType(oleLoanDocument.getBorrowerTypeName());
324                 oleCheckInItem.setBarcode(oleLoanDocument.getItemId());
325                 if(oleLoanDocument.getOleItem()!=null && oleLoanDocument.getOleItem().getItemType()!=null){
326                     oleCheckInItem.setItemType(oleLoanDocument.getOleItem().getItemType().getCodeValue());
327                 }
328                if(oleLoanDocument.getErrorMessage()==null || (oleLoanDocument.getErrorMessage()!=null && oleLoanDocument.getErrorMessage().isEmpty())){
329                    oleCheckInItem.setErrorMessage(OLEConstants.SUCCESSFULLEY_CHECKED_IN);
330                    DocstoreHelperService docstoreHelperService = new DocstoreHelperService();
331                    if(deleteIndicator.equalsIgnoreCase("y")){
332                        docstoreHelperService.performRestFulOperation("work","item","oleml",oleLoanDocument.getItemUuid());
333                    }
334                    return oleCheckInItemConverter.generateCheckInItemXml(oleCheckInItem);
335                                                                                                           } else{
336                    oleCheckInItem.setErrorMessage(oleLoanDocument.getErrorMessage());
337                    return oleCheckInItemConverter.generateCheckInItemXml(oleCheckInItem);
338                }
339             }else{
340                 OLECheckInItem oleCheckInItem = new OLECheckInItem();
341                 oleCheckInItem.setErrorMessage(OLEConstants.CHECK_IN_FAILED);
342                 return oleCheckInItemConverter.generateCheckInItemXml(oleCheckInItem);
343 
344             }
345 
346         }
347         catch(Exception e){
348             OLECheckInItem oleCheckInItem = new OLECheckInItem();
349             oleCheckInItem.setErrorMessage(OLEConstants.CHECK_IN_FAILED);
350             return oleCheckInItemConverter.generateCheckInItemXml(oleCheckInItem);
351         }
352 
353     }
354 
355     public String acceptItem(String patronId, String operatorId, String itemBarcode, String callNumber, String title, String author, String itemType, String itemLocation, String dateExpires) throws Exception {
356         Request request = buildRecord(author, title, itemType, itemBarcode, itemLocation,callNumber);
357         RequestHandler requestHandler = new RequestHandler();
358         String requestxml = requestHandler.toXML(request);
359         LOG.info("Request :"+requestxml);
360         String docstoreURL = ConfigContext.getCurrentContextConfig().getProperty(DOCSTORE_URL);
361         String queryString = CREATE_NEW_DOCSTORE_RECORD_QUERY_STRING + URLEncoder.encode(requestxml, "UTF-8");
362         String responseXml = DocstoreHelperService.postData(docstoreURL,queryString);
363         LOG.info("Response :"+responseXml);
364         ResponseHandler responseHandler = new ResponseHandler();
365         Response response = responseHandler.toObject(responseXml);
366         String identifier = "";
367         if(response.getDocuments() !=null && response.getDocuments().size()>0){
368             identifier = response.getDocuments().get(0).getLinkedDocuments().get(0).getLinkedDocuments().get(1).getUuid();
369         }else{
370             return null;
371         }
372         return identifier;
373     }
374 
375     private Request buildRecord(String author,String title,String itemType,String barcode,String location,String callNumber){
376         Request request =  new Request();
377         request.setUser("checkinUser");
378         request.setOperation(OLEConstants.INGEST_OPERATION);
379         ArrayList<RequestDocument> requestDocuments = new ArrayList<RequestDocument>();
380         RequestDocument requestDocument = new RequestDocument();
381         requestDocument.setCategory(OLEConstants.WORK_CATEGORY);
382         requestDocument.setType(OLEConstants.BIB_DOC_TYPE);
383         requestDocument.setFormat(OLEConstants.MARC_FORMAT);
384         String bibRecord = buildBibRecord(author,title);
385         Content content = new Content();
386         content.setContent(bibRecord);
387         requestDocument.setContent(content);
388         RequestDocument linkedRequestDocument = new RequestDocument();
389         linkedRequestDocument.setCategory(OLEConstants.WORK_CATEGORY);
390         linkedRequestDocument.setType(OLEConstants.INSTANCE_DOC_TYPE);
391         linkedRequestDocument.setFormat(OLEConstants.OLEML_FORMAT);
392         Content instanceContent = new Content();
393         String instanceRecord = buildInstanceRecord(itemType, barcode, location,callNumber);
394         instanceContent.setContent(instanceRecord);
395         linkedRequestDocument.setContent(instanceContent);
396         List<RequestDocument> linkedRequestDocuments = new ArrayList<RequestDocument>();
397         linkedRequestDocuments.add(linkedRequestDocument);
398         requestDocument.setLinkedRequestDocuments(linkedRequestDocuments);
399         AdditionalAttributes additionalAttributes = new AdditionalAttributes();
400         additionalAttributes.setAttribute(AdditionalAttributes.STAFFONLYFLAG,"true");
401         requestDocument.setAdditionalAttributes(additionalAttributes);
402         requestDocuments.add(requestDocument);
403         request.setRequestDocuments(requestDocuments);
404         return request;
405     }
406 
407     private String buildInstanceRecord(String itemType,String barcode,String location,String callNumber){
408         InstanceCollection instanceCollection = new InstanceCollection();
409         Instance instance = new Instance();
410         OleHoldings oleHoldings = new OleHoldings();
411         LocationLevel locationLevel = new LocationLevel();
412         locationLevel = loanProcessor.createLocationLevel(location,locationLevel);
413         Location  holdingsLocation=new Location();
414         holdingsLocation.setPrimary(OLEConstants.TRUE);
415         holdingsLocation.setStatus(OLEConstants.PERMANENT);
416         holdingsLocation.setLocationLevel(locationLevel);
417         oleHoldings.setLocation(holdingsLocation);
418         AdditionalAttributes additionalAttributes = new AdditionalAttributes();
419         additionalAttributes.setAttribute(AdditionalAttributes.STAFFONLYFLAG,"true");
420         Extension extension = new Extension();
421         List<Object> additionalAttributesList = new ArrayList<>();
422         additionalAttributesList.add(additionalAttributes);
423         extension.setContent(additionalAttributesList);
424         oleHoldings.setExtension(extension);
425         instance.setOleHoldings(oleHoldings);
426         Items items = new Items();
427         Item item = new Item();
428         AccessInformation accessInformation = new AccessInformation();
429         accessInformation.setBarcode(barcode);
430         item.setAccessInformation(accessInformation);
431         ItemStatus itemStatus = new ItemStatus();
432         itemStatus.setCodeValue("AVAILABLE");
433         item.setItemStatus(itemStatus);
434         ItemType type = new ItemType();
435         type.setCodeValue(itemType);
436         item.setItemType(type);
437         CallNumber itemCallNumber = new CallNumber();
438         itemCallNumber.setNumber(callNumber);
439         item.setCallNumber(itemCallNumber);
440         ShelvingScheme shelvingScheme = new ShelvingScheme();
441         shelvingScheme.setCodeValue("LCC");
442         itemCallNumber.setShelvingScheme(shelvingScheme);
443         item.setExtension(extension);
444         List<Item> itemList = new ArrayList<Item>();
445         itemList.add(item);
446         items.setItem(itemList);
447         instance.setItems(items);
448         List<Instance> instances = new ArrayList<Instance>();
449         instances.add(instance);
450         instanceCollection.setInstance(instances);
451         WorkInstanceOlemlRecordProcessor workInstanceOlemlRecordProcessor = new WorkInstanceOlemlRecordProcessor();
452         return workInstanceOlemlRecordProcessor.toXML(instanceCollection);
453     }
454 
455     private String buildBibRecord(String author,String title){
456         BibliographicRecord bibliographicRecord = new BibliographicRecord();
457         List<DataField> dataFields = buildBibliographicDataFields(author,title);
458         bibliographicRecord.setDatafields(dataFields);
459         return new BibliographicRecordHandler().generateXML(bibliographicRecord);
460     }
461 
462     private List<DataField> buildBibliographicDataFields(String author,String title) {
463         List<DataField> dataFieldList = new ArrayList<DataField>();
464         DataField authorField = new DataField();
465         authorField.setTag("245");
466         SubField authorSubField = new SubField();
467         authorSubField.setCode("a");
468         authorSubField.setValue(title);
469         authorField.addSubField(authorSubField);
470         dataFieldList.add(authorField);
471         DataField titleField = new DataField();
472         SubField titleSubField = new SubField();
473         titleField.setTag("100");
474         titleSubField.setCode("a");
475         titleSubField.setValue(author);
476         titleField.addSubField(titleSubField);
477         dataFieldList.add(titleField);
478         return dataFieldList;
479     }
480 
481     /*private Location getLocationDetails(String locationName) {
482         Location location = new Location();
483         LocationLevel locationLevel = createLocationLevel(locationName);
484         location.setLocationLevel(locationLevel);
485         return location;
486     }
487 
488     private LocationLevel createLocationLevel(String locationName) {
489         LocationLevel locationLevel = null;
490         if (locationName != null) {
491             String[] locations = locationName.split("/");
492             String locName = locations[0];
493             if (locationName.contains("/")) {
494                 locationName = locationName.replace(locations[0] + "/", "");
495             } else {
496                 locationName = locationName.replace(locations[0], "");
497             }
498             if (locName != null && locName.length() > 0 && locations.length != 0) {
499                 locationLevel = new LocationLevel();
500                 locationLevel.setName(locName);
501                 locationLevel.setLocationLevel(createLocationLevel(locationName));
502             }
503         }
504         return locationLevel;
505     }*/
506 
507     public HashMap<String,String> getAgencyPropertyMap(String agencyId){
508         HashMap<String,String> agencyPropertyMap = null;
509         if(OLENCIPConstants.agencyPropertyMap.containsKey(agencyId)){
510             agencyPropertyMap = OLENCIPConstants.agencyPropertyMap.get(agencyId);
511         }
512         return agencyPropertyMap;
513     }
514     public String renewItem(String patronId,String operatorId,String itemBarcode){
515 
516         Map<String,String> loanMap= new HashMap<String,String>();
517         loanMap.put("patronId",patronId);
518         loanMap.put("itemId",itemBarcode);
519 
520         List<OleLoanDocument> loanDocuments = (List<OleLoanDocument>)businessObjectService.findMatching(OleLoanDocument.class,loanMap);
521         if(loanDocuments.size()>0){
522             OleLoanDocument oleLoanDocument= loanDocuments.get(0);
523             Map<String,String> patronMap= new HashMap<String,String>();
524             patronMap.put("olePatronId",patronId);
525             List<OlePatronDocument> patronDocuments = (List<OlePatronDocument>)businessObjectService.findMatching(OlePatronDocument.class,patronMap);
526             if(patronDocuments.size()>0){
527                 oleLoanDocument.setOlePatron(patronDocuments.get(0));
528                 oleLoanDocument.setBorrowerTypeCode(patronDocuments.get(0).getBorrowerTypeCode());
529                 oleLoanDocument.setBorrowerTypeId(patronDocuments.get(0).getBorrowerType());
530                 oleLoanDocument.setOleBorrowerType(patronDocuments.get(0).getOleBorrowerType());
531                 oleLoanDocument.setBorrowerTypeName(patronDocuments.get(0).getBorrowerTypeName());
532             }
533 
534             oleLoanDocument.setRenewalItemFlag(true);
535             oleLoanDocument.setErrorMessage(null);
536             if(loanProcessor.canOverrideLoan(operatorId)){
537                 if(!loanProcessor.checkPendingRequestforItem(oleLoanDocument.getItemUuid())){
538                     Timestamp currentDate = new Timestamp(System.currentTimeMillis());
539                     try {
540                         oleLoanDocument = loanProcessor.addLoan(oleLoanDocument.getPatronBarcode(), oleLoanDocument.getItemId(), oleLoanDocument);
541                         if(oleLoanDocument.getErrorMessage()==null || (oleLoanDocument.getErrorMessage()!=null && oleLoanDocument.getErrorMessage().trim().isEmpty()))
542                             return "Item SuccessFully renewed";
543                         else
544                             return oleLoanDocument.getErrorMessage();
545                     } catch (Exception e) {
546                         return "Exception occured while renewing an item";
547                     }
548 
549                 }   else
550                     return "There is an pending request for that item";
551             }   else
552                 return "The Operator in not authorized to renew an item";
553         }
554         else
555             return "Item is not loaned by the patron";
556     }
557 }
558 
559 
560 
561