View Javadoc
1   package org.kuali.ole.serviceimpl;
2   
3   import org.apache.commons.lang.StringEscapeUtils;
4   import org.apache.solr.client.solrj.SolrServerException;
5   import org.kuali.ole.OLESruItemHandler;
6   import org.kuali.ole.OleSRUConstants;
7   import org.kuali.ole.bo.diagnostics.OleSRUDiagnostics;
8   import org.kuali.ole.bo.serachRetrieve.*;
9   import org.kuali.ole.docstore.common.client.DocstoreClient;
10  import org.kuali.ole.docstore.common.document.Bib;
11  import org.kuali.ole.docstore.common.document.HoldingsTree;
12  import org.kuali.ole.docstore.common.document.content.bib.dc.BibDcRecord;
13  import org.kuali.ole.docstore.common.document.content.bib.dc.DCValue;
14  import org.kuali.ole.docstore.common.document.content.bib.dc.xstream.BibDcRecordProcessor;
15  import org.kuali.ole.docstore.common.document.content.bib.marc.BibMarcRecords;
16  import org.kuali.ole.docstore.common.document.content.bib.marc.xstream.BibMarcRecordProcessor;
17  import org.kuali.ole.docstore.common.document.content.instance.*;
18  import org.kuali.ole.docstore.common.document.content.instance.xstream.HoldingOlemlRecordProcessor;
19  import org.kuali.ole.docstore.common.document.content.instance.xstream.InstanceOlemlRecordProcessor;
20  import org.kuali.ole.docstore.common.document.content.instance.xstream.ItemOlemlRecordProcessor;
21  import org.kuali.ole.docstore.discovery.service.SRUCQLQueryService;
22  import org.kuali.ole.docstore.discovery.service.SRUCQLQueryServiceImpl;
23  import org.kuali.ole.docstore.engine.client.DocstoreLocalClient;
24  import org.kuali.ole.docstore.model.bo.OleDocument;
25  import org.kuali.ole.docstore.model.bo.WorkBibDocument;
26  import org.kuali.ole.docstore.model.bo.WorkInstanceDocument;
27  import org.kuali.ole.docstore.service.OleWebServiceProvider;
28  import org.kuali.ole.docstore.service.impl.OleWebServiceProviderImpl;
29  import org.kuali.ole.handler.OleSRUDublinRecordResponseHandler;
30  import org.kuali.ole.handler.OleSRUOpacXMLResponseHandler;
31  import org.kuali.ole.pojo.OLESruItem;
32  import org.kuali.ole.service.OleDiagnosticsService;
33  import org.kuali.ole.service.OleLoanDocumentWebService;
34  import org.kuali.ole.service.OleSRUDataService;
35  import org.kuali.rice.core.api.config.property.ConfigContext;
36  import org.kuali.rice.coreservice.impl.parameter.ParameterBo;
37  import org.kuali.rice.krad.service.KRADServiceLocator;
38  import org.slf4j.Logger;
39  import org.slf4j.LoggerFactory;
40  
41  import java.util.*;
42  
43  
44  /**
45   * Created with IntelliJ IDEA.
46   * User: ?
47   * Date: 7/9/12
48   * Time: 7:41 PM
49   * To change this template use File | Settings | File Templates.
50   */
51  public class OleSRUDataServiceImpl implements OleSRUDataService {
52  
53      private Logger LOG = LoggerFactory.getLogger(this.getClass());
54      public SRUCQLQueryService srucqlQueryService;
55      public OleDiagnosticsService oleDiagnosticsService;
56      private DocstoreClient docstoreClient;
57  
58      private HoldingOlemlRecordProcessor holdingOlemlRecordProcessor = new HoldingOlemlRecordProcessor();
59      private InstanceOlemlRecordProcessor instanceOlemlRecordProcessor = new InstanceOlemlRecordProcessor();
60      private ItemOlemlRecordProcessor itemOlemlRecordProcessor = new ItemOlemlRecordProcessor();
61      private OLESruItemHandler oleSruItemHandler;
62  
63      public OleSRUDataServiceImpl() {
64          srucqlQueryService = SRUCQLQueryServiceImpl.getInstance();
65          oleDiagnosticsService = new OleDiagnosticsServiceImpl();
66      }
67  
68      public OLESruItemHandler getOleSruItemHandler(){
69          if(oleSruItemHandler == null){
70              oleSruItemHandler = new OLESruItemHandler();
71          }
72          return oleSruItemHandler;
73      }
74  
75      /**
76       * this method will fetch the bib id list from the docstore
77       *
78       * @param reqParamMap
79       * @param solrQuery
80       * @return list of bib id
81       */
82      public List getBibRecordsIdList(Map reqParamMap, String solrQuery) {
83          LOG.info("Inside getBibRecordsIdList method");
84          List oleBibIDList = null;
85          List<OleDocument> oleDocumentsList = null;
86          try {
87              oleBibIDList = new ArrayList();
88              oleDocumentsList = (ArrayList<OleDocument>) srucqlQueryService.queryForBibDocs(reqParamMap, solrQuery);
89              if ((oleDocumentsList != null) && (oleDocumentsList.size() > 0)) {
90                  oleBibIDList = generateBibIDList(oleDocumentsList);
91              }
92          } catch (Exception e) {
93              LOG.error(e.getMessage());
94              if(e instanceof SolrServerException){
95                  if(solrQuery.contains("LocalId"))
96                      oleBibIDList.add("Invalid Local Id");
97                  else
98                      oleBibIDList.add("Exception Occured");
99  
100                 return  oleBibIDList;
101             } else
102                 return null;
103         }
104         return oleBibIDList;
105     }
106 
107     /**
108      * this method will fetch the bib details , holding details from the docstore data base , creates an object for bib and holdings , and generates OPAC xml
109      *
110      * @param oleBibIDList
111      * @param reqParamMap
112      * @return opac xml or diagnostics or oleSRUResponseDocuments as a string depends upon the record packing
113      */
114     public String getOPACXMLSearchRetrieveResponse(List oleBibIDList, Map reqParamMap) {
115         LOG.info("Inside getOPACXMLSearchRetrieveResponse method");
116         try {
117             OleSRUOpacXMLResponseHandler oleSRUOpacXMLResponseHandler = new OleSRUOpacXMLResponseHandler();
118             OleSRUSearchRetrieveResponse oleSRUSearchRetrieveResponse = new OleSRUSearchRetrieveResponse();
119             oleSRUSearchRetrieveResponse.setVersion((String) reqParamMap.get(OleSRUConstants.VERSION));
120             OleSRUResponseRecords oleSRUResponseRecords = new OleSRUResponseRecords();
121             List<OleSRUResponseRecord> oleSRUResponseRecordList = new ArrayList<OleSRUResponseRecord>();
122             if (oleBibIDList != null && oleBibIDList.size() > 0) {
123                 List<OleSRUResponseDocument> oleSRUResponseDocumentList = new ArrayList<OleSRUResponseDocument>();
124                 String bibRecordInfo = null;
125                 int startPosition = 0;
126                 if ((Integer) reqParamMap.get(OleSRUConstants.START_RECORD) != 0) {
127                     startPosition = (Integer) reqParamMap.get(OleSRUConstants.START_RECORD);
128                 }
129                 for (int i = 0; i < oleBibIDList.size(); i++) {
130                     OleSRUResponseRecord oleSRUResponseRecord = new OleSRUResponseRecord();
131                     oleSRUResponseRecord.setRecordPacking((String) reqParamMap.get(OleSRUConstants.RECORD_PACKING));
132                     if ((Integer) reqParamMap.get(OleSRUConstants.START_RECORD) == 0) {
133                         oleSRUResponseRecord.setRecordPosition(i + 1);
134                     } else {
135                         oleSRUResponseRecord.setRecordPosition(startPosition + 1);
136                         startPosition = startPosition + 1;
137                     }
138                     OleSRUResponseDocument oleSRUResponseDocument = new OleSRUResponseDocument();
139                     OleSRUData oleSRUData = (OleSRUData) oleBibIDList.get(i);
140                     bibRecordInfo = getBibliographicRecordInfo(oleSRUData.getBibId());
141                     BibMarcRecords bibMarcRecords=(BibMarcRecords)(new BibMarcRecordProcessor()).fromXML(bibRecordInfo);
142                     OleSRUResponseRecordData oleSRUResponseRecordData = new OleSRUResponseRecordData();
143                     String recordSchema= (String)reqParamMap.get(OleSRUConstants.RECORD_SCHEMA);
144                     if (recordSchema.equalsIgnoreCase(OleSRUConstants.OPAC_RECORD)) {
145                         List<OleSRUInstanceDocument> oleSRUInstanceDocumentList = new ArrayList<OleSRUInstanceDocument>();
146                         if (oleSRUData.getInstanceIds() != null && oleSRUData.getInstanceIds().size() > 0) {
147                             for (String instance : oleSRUData.getInstanceIds()) {
148                                 String holdingInfo = getInstanceInfoRecordInfo(instance);
149                                 OleSRUInstanceDocument oleSRUInstanceDocument = processInstanceCollectionXml(holdingInfo);
150                                 oleSRUInstanceDocumentList.add(oleSRUInstanceDocument);
151                             }
152                         }
153                         oleSRUResponseRecordData.setHoldings(oleSRUInstanceDocumentList);
154                     }
155 
156                     if (((String) reqParamMap.get(OleSRUConstants.RECORD_SCHEMA)).equalsIgnoreCase(OleSRUConstants.DUBLIN_RECORD_SCHEMA)) {
157                         String dublinRecordInfo = null;
158                         BibDcRecordProcessor qualifiedDublinRecordHandler = new BibDcRecordProcessor();
159                         BibDcRecord workBibDublinRecord = qualifiedDublinRecordHandler.fromXML(bibRecordInfo);
160                         OleSRUDublinRecord oleSRUDublinRecord = new OleSRUDublinRecord();
161                         for (DCValue dcValue : workBibDublinRecord.getDcValues()) {
162                             oleSRUDublinRecord.put(dcValue.getElement(), dcValue.getValue());
163                         }
164                         OleSRUDublinRecordResponseHandler oleSRUDublinRecordResponseHandler = new OleSRUDublinRecordResponseHandler();
165                         String dublinInfo = oleSRUDublinRecordResponseHandler.toXML(oleSRUDublinRecord);
166                         //dublinInfo = dublinInfo.replaceAll("&amp;","&");
167                         //bibRecordInfo=StringEscapeUtils.unescapeXml(bibRecordInfo);
168                         oleSRUResponseRecordData.setBibliographicRecord(dublinInfo);
169                         oleSRUResponseDocument.setOleSRUResponseRecordData(oleSRUResponseRecordData);
170                     } else {
171                         //bibRecordInfo = bibRecordInfo.replaceAll("&amp;","&");
172                         //bibRecordInfo=StringEscapeUtils.unescapeXml(bibRecordInfo);
173                         oleSRUResponseRecordData.setBibliographicRecord(bibRecordInfo);
174                         oleSRUResponseRecordData.setBibMarcRecords(bibMarcRecords);
175                         oleSRUResponseDocument.setOleSRUResponseRecordData(oleSRUResponseRecordData);
176                     }
177                     oleSRUSearchRetrieveResponse.setNumberOfRecords((Long) (reqParamMap.get(OleSRUConstants.NUMBER_OF_REORDS)));
178                     if(recordSchema == null ||(recordSchema!=null && (recordSchema.equalsIgnoreCase(OleSRUConstants.MARC) || recordSchema.equalsIgnoreCase(OleSRUConstants.MARC_SCHEMA)))){
179                         oleSRUResponseRecord.setRecordSchema(OleSRUConstants.MARC_RECORD_RESPONSE_SCHEMA);
180                     }else if(recordSchema!=null && recordSchema!=null && recordSchema.equalsIgnoreCase(OleSRUConstants.DC_SCHEMA)){
181                         oleSRUResponseRecord.setRecordSchema(OleSRUConstants.DC_RECORD_RESPONSE_SCHEMA);
182                     }else if(recordSchema!=null && recordSchema.equalsIgnoreCase(OleSRUConstants.OPAC_RECORD)){
183                         oleSRUResponseRecord.setRecordSchema(OleSRUConstants.OPAC_RECORD_RESPONSE_SCHEMA);
184                     }
185                     if (reqParamMap.containsKey(OleSRUConstants.EXTRA_REQ_DATA_KEY))
186                         oleSRUResponseRecordData.setExtraRequestData(getExtraReqDataInfo(reqParamMap));
187                     oleSRUResponseRecord.setOleSRUResponseDocument(oleSRUResponseDocument);
188                     oleSRUResponseRecordList.add(oleSRUResponseRecord);
189                 }
190                 oleSRUResponseRecords.setOleSRUResponseRecordList(oleSRUResponseRecordList);
191                 oleSRUSearchRetrieveResponse.setOleSRUResponseRecords(oleSRUResponseRecords);
192                 int maxRecords = (Integer) reqParamMap.get(OleSRUConstants.MAXIMUM_RECORDS);
193                 if (maxRecords == 0) {
194                     oleSRUSearchRetrieveResponse = new OleSRUSearchRetrieveResponse();
195                     oleSRUSearchRetrieveResponse.setVersion((String) reqParamMap.get(OleSRUConstants.VERSION));
196                     oleSRUSearchRetrieveResponse.setNumberOfRecords((Long) (reqParamMap.get(OleSRUConstants.NUMBER_OF_REORDS)));
197                     String xml = oleSRUOpacXMLResponseHandler.toXML(oleSRUSearchRetrieveResponse,(String)reqParamMap.get(OleSRUConstants.RECORD_SCHEMA));
198                     return xml;
199                 }
200                 if (OleSRUConstants.RECORD_PACK_XML.equalsIgnoreCase((String) reqParamMap.get(OleSRUConstants.RECORD_PACKING))) {
201                     String opacXML = oleSRUOpacXMLResponseHandler.toXML(oleSRUSearchRetrieveResponse,(String)reqParamMap.get(OleSRUConstants.RECORD_SCHEMA));
202                     if (null != opacXML)
203                         //opacXML=StringEscapeUtils.unescapeXml(opacXML);
204                         //opacXML = replaceStringWithSymbols(opacXML);
205                     return opacXML;
206                 } else if (OleSRUConstants.RECORD_PACK_STRING.equalsIgnoreCase((String) reqParamMap.get(OleSRUConstants.RECORD_PACKING))) {
207                     return oleSRUSearchRetrieveResponse.toString();
208                 }
209             }
210             Long numberOfRecords = (Long) (reqParamMap.get(OleSRUConstants.NUMBER_OF_REORDS));
211             oleSRUSearchRetrieveResponse.setNumberOfRecords(numberOfRecords);
212             OleSRUDiagnostics oleSRUDiagnostics = null;
213             if (numberOfRecords > 0) {
214                 oleSRUDiagnostics = oleDiagnosticsService.getDiagnosticResponse(ConfigContext.getCurrentContextConfig().getProperty(OleSRUConstants.START_RECORD_UNMATCH));
215             } else {
216                 oleSRUDiagnostics = oleDiagnosticsService.getDiagnosticResponse(ConfigContext.getCurrentContextConfig().getProperty(OleSRUConstants.NORECORDS_DIAGNOSTIC_MSG));
217             }
218             int start = (Integer) reqParamMap.get(OleSRUConstants.START_RECORD);
219             if(start > numberOfRecords){
220                 oleSRUDiagnostics = oleDiagnosticsService.getDiagnosticResponse(ConfigContext.getCurrentContextConfig().getProperty(OleSRUConstants.START_RECORD_UNMATCH));
221             }
222             oleSRUSearchRetrieveResponse.setOleSRUDiagnostics(oleSRUDiagnostics);
223             return oleSRUOpacXMLResponseHandler.toXML(oleSRUSearchRetrieveResponse,(String)reqParamMap.get(OleSRUConstants.RECORD_SCHEMA));
224         } catch (Exception e) {
225             LOG.error(e.getMessage() , e );
226         }
227         return null;
228     }
229 
230     /**
231      * this will generate the list of document from docstore
232      *
233      * @param oleDocumentsList (list of OleDocument)
234      * @return bib id list
235      */
236     public List generateBibIDList(List<OleDocument> oleDocumentsList) {
237         LOG.info("Inside generateBibIDList method");
238         List oleBibIDList = new ArrayList();
239         WorkBibDocument workBibDocument = null;
240         List<OleSRUData> oleSRUDataList = new ArrayList<OleSRUData>();
241         OleSRUData oleSRUData = null;
242         for (int i = 0; i < oleDocumentsList.size(); i++) {
243             workBibDocument = (WorkBibDocument) oleDocumentsList.get(i);
244             oleSRUData = new OleSRUData();
245             oleSRUData.setBibId(workBibDocument.getId());
246             List<String> instanceList = new ArrayList<String>();
247             if (workBibDocument.getWorkInstanceDocumentList() != null) {
248                 for (WorkInstanceDocument workInstanceDocument : workBibDocument.getWorkInstanceDocumentList()) {
249                     instanceList.add(workInstanceDocument.getInstanceIdentifier());
250                 }
251             }
252             oleSRUData.setInstanceIds(instanceList);
253             oleSRUDataList.add(oleSRUData);
254         }
255         return oleSRUDataList;
256     }
257 
258     /**
259      * this method will do checkout operation of docstore which gives the bib info details
260      *
261      * @param uuid
262      * @return bib info xml as a string
263      */
264     public String getBibliographicRecordInfo(String uuid) {
265         LOG.info("Inside getBibliographicRecordInfo method");
266         String bibRecordInfo = null;
267 
268         Bib bib =  getDocstoreClient().retrieveBib(uuid);
269         bibRecordInfo = bib.getContent();
270 //        CheckoutManager checkoutManager = new CheckoutManager();
271       /*  try {
272             Request req = new Request();
273             if (DocumentUniqueIDPrefix.hasPrefix(uuid)) {
274                 Map<String, String> categoryTypeFormat = DocumentUniqueIDPrefix.getCategoryTypeFormat(uuid);
275                 String category = categoryTypeFormat.get("category");
276                 String type = categoryTypeFormat.get("type");
277                 String format = categoryTypeFormat.get("format");
278                 RequestDocument requestDocument = new RequestDocument();
279                 requestDocument.setCategory(category);
280                 requestDocument.setType(type);
281                 requestDocument.setFormat(format);
282                 requestDocument.setUuid(uuid);
283                 List<RequestDocument> requestDocuments = new ArrayList<RequestDocument>();
284                 requestDocuments.add(requestDocument);
285                 req.setOperation("checkOut");
286                 req.setUser("checkOutUser");
287                 req.setRequestDocuments(requestDocuments);
288                 Response response = BeanLocator.getDocstoreFactory().getDocumentService().process(req);
289                 if(response.getDocuments()!=null && response.getDocuments().size()>0)
290                 bibRecordInfo = response.getDocuments().get(0).getContent().getContent();
291             } else {
292                 bibRecordInfo = checkoutManager.checkOut(uuid, "defaultUser", "checkOut");
293             }
294             LOG.info("BibRecordInfo:" + bibRecordInfo);
295             if (bibRecordInfo != null) {
296                 bibRecordInfo = bibRecordInfo.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "");
297                 bibRecordInfo = bibRecordInfo.replace("<collection>","");
298                 bibRecordInfo = bibRecordInfo.replace("<collection xmlns=\"http://www.loc.gov/MARC21/slim\">", "");
299                 bibRecordInfo = bibRecordInfo.replace("</collection>", "");
300                 bibRecordInfo = bibRecordInfo.replace("<record>", "<record xmlns=\"http://www.loc.gov/MARC21/slim\">");
301             }
302         } catch (Exception e) {
303             LOG.error(e.getMessage() , e );
304         }*/
305         return bibRecordInfo;
306     }
307 
308 
309     public String getInstanceInfoRecordInfo(String uuid) {
310         String instanceXml = "";
311         InstanceCollection instanceCollection = new InstanceCollection();
312         HoldingsTree holdingsTree = getDocstoreClient().retrieveHoldingsTree(uuid);
313         OleHoldings oleHoldings = holdingOlemlRecordProcessor.fromXML(holdingsTree.getHoldings().getContent());
314         Items items = new Items();
315         for(org.kuali.ole.docstore.common.document.Item itemDoc : holdingsTree.getItems()) {
316             items.getItem().add(itemOlemlRecordProcessor.fromXML(itemDoc.getContent()));
317         }
318         Instance instance = new Instance();
319         instance.setOleHoldings(oleHoldings);
320         instance.setItems(items);
321         instanceCollection.getInstance().add(instance);
322         instanceXml = instanceOlemlRecordProcessor.toXML(instanceCollection);
323         return instanceXml;
324     }
325 
326     /**
327      * to replace the encoded value from the string to original special character to display as an xml
328      *
329      * @param opacXML
330      * @return opac xml response
331      */
332    /* private String replaceStringWithSymbols(String opacXML) {
333         LOG.info("Inside replaceStringWithSymbols method");
334         opacXML = opacXML.replaceAll("&lt;", "<");
335         opacXML = opacXML.replaceAll("&gt;", ">");
336         opacXML = opacXML.replaceAll("&quot;", "\"");
337         opacXML = opacXML.replaceAll("&apos;", "\'");
338         //opacXML =opacXML.replaceAll("&amp;","&");
339         //opacXML = opacXML.replaceAll("&","&amp;");
340         //opacXML = StringEscapeUtils.unescapeHtml(opacXML).replaceAll("[^\\x20-\\x7e]", "");
341         return opacXML;
342     }*/
343 
344     public List<OleSRUInstanceDocument> getOleSRUInstanceDocument() {
345         LOG.info("Inside getOleSRUInstanceDocument method");
346         OleSRUInstanceDocument oleSRUInstanceDocument = new OleSRUInstanceDocument();
347         List<OleSRUInstanceDocument> oleSRUInstanceDocumentList = new ArrayList<OleSRUInstanceDocument>();
348         List<OleSRUCirculationDocument> oleSRUCirculationDocumentList = getOleSRUCirculationDocument();
349         List<OleSRUInstanceVolume> oleSRUInstanceVolumeList = getOleSRUInstanceVolumes();
350         oleSRUInstanceDocument.setCallNumber("mockCallNumber");
351         oleSRUInstanceDocument.setCompleteness("mockCompleteness");
352         oleSRUInstanceDocument.setCopyNumber("mockCopyNumber");
353         oleSRUInstanceDocument.setDateOfReport("mockDateOfReport");
354         oleSRUInstanceDocument.setEncodingLevel("mockEncodingLevel");
355         oleSRUInstanceDocument.setEnumAndChron("mockEnumAndChorn");
356         oleSRUInstanceDocument.setFormat("mockFormat");
357         oleSRUInstanceDocument.setGeneralRetention("mockGeneralRentention");
358         oleSRUInstanceDocument.setLocalLocation("mockLocalLocation");
359         oleSRUInstanceDocument.setTypeOfRecord("mockTypeOfRecord");
360         oleSRUInstanceDocument.setShelvingLocation("mockShelvingLocation");
361         oleSRUInstanceDocument.setShelvingData("mockShelvingData");
362         oleSRUInstanceDocument.setReproductionNote("mockReproductionNote");
363         oleSRUInstanceDocument.setReceiptAcqStatus("mockReceiptAcqStatus");
364         oleSRUInstanceDocument.setNucCode("mockNucCode");
365         oleSRUInstanceDocument.setPublicNote("mockPublicNote");
366         oleSRUInstanceDocument.setCirculations(oleSRUCirculationDocumentList);
367         oleSRUInstanceDocument.setVolumes(oleSRUInstanceVolumeList);
368         oleSRUInstanceDocumentList.add(oleSRUInstanceDocument);
369         return oleSRUInstanceDocumentList;
370     }
371 
372     public List<OleSRUCirculationDocument> getOleSRUCirculationDocument() {
373         LOG.info("Inside getOleSRUCirculationDocument method");
374         OleSRUCirculationDocument oleSRUCirculationDocument = new OleSRUCirculationDocument();
375         List<OleSRUCirculationDocument> oleSRUCirculationDocumentList = new ArrayList<OleSRUCirculationDocument>();
376         oleSRUCirculationDocument.setAvailabilityDate("mockAvailabilityDate");
377         oleSRUCirculationDocument.setAvailableNow("mockAvailableNow");
378         oleSRUCirculationDocument.setAvailableThru("mockAvailableThru");
379         oleSRUCirculationDocument.setEnumAndChron("mockEnumAndChron");
380         oleSRUCirculationDocument.setItemId("mockItemId");
381         oleSRUCirculationDocument.setMidspine("mockMidspine");
382         oleSRUCirculationDocument.setOnHold("mockOnHold");
383         //oleSRUCirculationDocument.setRenewable("mockRenewable");
384         oleSRUCirculationDocument.setRestrictions("mockRestrictions");
385         oleSRUCirculationDocument.setTemporaryLocation("mockTemporaryLocation");
386         oleSRUCirculationDocumentList.add(oleSRUCirculationDocument);
387         return oleSRUCirculationDocumentList;
388     }
389 
390     public List<OleSRUInstanceVolume> getOleSRUInstanceVolumes() {
391         LOG.info("Inside getOleSRUInstanceVolumes method");
392         OleSRUInstanceVolume oleSRUInstanceVolume = new OleSRUInstanceVolume();
393         List<OleSRUInstanceVolume> oleSRUInstanceVolumeList = new ArrayList<OleSRUInstanceVolume>();
394         oleSRUInstanceVolume.setChronology("mockChronology");
395         oleSRUInstanceVolume.setEnumAndChron("mockEnumAndChron");
396         oleSRUInstanceVolume.setEnumeration("mockEnumeration");
397         oleSRUInstanceVolumeList.add(oleSRUInstanceVolume);
398         return oleSRUInstanceVolumeList;
399     }
400 
401     public String getExtraReqDataInfo(Map reqParamMap) {
402         LOG.info("Inside getExtraReqDataInfo method");
403         return "<theo:" + reqParamMap.get(OleSRUConstants.EXTRA_REQ_DATA_KEY) + " xmlns:theo=\"" + ConfigContext.getCurrentContextConfig().getProperty(OleSRUConstants.EXTRA_REQ_DATA_XML_NAMESPACE) + "\">\n" +
404                 reqParamMap.get(OleSRUConstants.EXTRA_REQ_DATA_VALUE) + "\n" +
405                 "</theo:" + reqParamMap.get(OleSRUConstants.EXTRA_REQ_DATA_KEY) + ">";
406     }
407 
408     public OleSRUInstanceDocument processInstanceCollectionXml(String instanceXml) {
409         LOG.info("Inside processInstanceCollectionXml method");
410         String sruTrueValue = getParameter(OleSRUConstants.BOOLEAN_FIELD_TRUE_FORMAT);
411         String sruFalseValue = getParameter(OleSRUConstants.BOOLEAN_FIELD_FALSE_FORMAT);
412         InstanceOlemlRecordProcessor instanceOlemlRecordProcessor = new InstanceOlemlRecordProcessor();
413         InstanceCollection instanceCollection = instanceOlemlRecordProcessor.fromXML(instanceXml);
414         OleSRUInstanceDocument oleSRUInstanceDocument = null;
415         OleWebServiceProvider oleWebServiceProvider = new OleWebServiceProviderImpl();
416         String url = ConfigContext.getCurrentContextConfig().getProperty("oleLoanWebService.url");
417         OleLoanDocumentWebService oleLoanDocumentService = (OleLoanDocumentWebService) oleWebServiceProvider.getService("org.kuali.ole.service.OleLoanDocumentWebService", "oleLoanWebService", url);
418         String sruItemContent;
419         OLESruItem oleSruItem;
420         if (instanceCollection != null) {
421             List<Instance> instances = instanceCollection.getInstance();
422             if (instances != null && instances.size() > 0) {
423                 for (Instance instance : instances) {
424                     oleSRUInstanceDocument = new OleSRUInstanceDocument();
425                     if (instance.getOleHoldings() != null && instance.getOleHoldings().getCallNumber() != null) {
426                         oleSRUInstanceDocument.setCallNumber(instance.getOleHoldings().getCallNumber().getNumber());
427                         oleSRUInstanceDocument.setShelvingData(instance.getOleHoldings().getCallNumber().getPrefix());
428                     }
429                     if (instance.getOleHoldings() != null) {
430                         oleSRUInstanceDocument.setReceiptAcqStatus(instance.getOleHoldings().getReceiptStatus());
431                     }
432                     if (instance.getOleHoldings().getLocation() != null) {
433                         sruItemContent = oleLoanDocumentService.getItemInformation(null,null,null, getLocations(instance.getOleHoldings().getLocation(), OleSRUConstants.SHELVING_LOCATION), getLocations(instance.getOleHoldings().getLocation(), OleSRUConstants.LOCAL_LOCATION));
434                         oleSruItem = (OLESruItem) getOleSruItemHandler().getObjectFromXml(sruItemContent, (Object) new OLESruItem());
435                         if (oleSruItem != null) {
436                             oleSRUInstanceDocument.setLocalLocation(oleSruItem.getLocalLocation());
437                             oleSRUInstanceDocument.setShelvingLocation(oleSruItem.getShelvingLocation());
438                         }
439                     }
440                     Items items = instance.getItems();
441                     List<OleSRUInstanceVolume> oleSRUInstanceVolumeList = new ArrayList<OleSRUInstanceVolume>();
442                     List<OleSRUCirculationDocument> oleSRUCirculationDocumentList = new ArrayList<OleSRUCirculationDocument>();
443                     if (items != null) {
444                         List<Item> itemList = items.getItem();
445 
446                         for (int i = 0; i < itemList.size(); i++) {
447                             Item item = itemList.get(i);
448                             if (item != null) {
449                                 oleSRUInstanceDocument.setCopyNumber(item.getCopyNumber());
450                             }
451                             OleSRUInstanceVolume oleSRUInstanceVolume = new OleSRUInstanceVolume();
452                             oleSRUInstanceVolume.setChronology(item.getChronology());
453                             oleSRUInstanceVolume.setEnumeration(item.getEnumeration());
454                             if (item.getEnumeration() != null && item.getChronology() != null) {
455                                 oleSRUInstanceVolume.setEnumAndChron(item.getEnumeration() + "," + item.getChronology());
456                             }
457                             oleSRUInstanceVolumeList.add(oleSRUInstanceVolume);
458                             OleSRUCirculationDocument oleSRUCirculationDocument = new OleSRUCirculationDocument();
459                             oleSRUCirculationDocument.setItemId(item.getBarcodeARSL());
460                             StringBuffer locationName = new StringBuffer();
461                             if (item.getItemStatus() != null && item.getItemStatus().getCodeValue().equalsIgnoreCase(OleSRUConstants.ITEM_STATUS_AVAILABLE)) {
462                                 oleSRUCirculationDocument.setAvailableNow(sruTrueValue);
463                             } else {
464                                 oleSRUCirculationDocument.setAvailableNow(sruFalseValue);
465                             }
466                             if (item.getItemStatus() != null && item.getItemStatus().getCodeValue().equalsIgnoreCase(OleSRUConstants.ITEM_STATUS_ONHOLD)) {
467                                 oleSRUCirculationDocument.setOnHold(sruTrueValue);
468                             } else {
469                                 oleSRUCirculationDocument.setOnHold(sruFalseValue);
470                             }
471                             String shelvingLocationCode = "";
472                             if (item.getLocation() != null) {
473                                 shelvingLocationCode = getShelvingLocation(item.getLocation());
474                             }
475                             String itemTypeCode = "";
476                             if (item.getItemType() != null) {
477                                 itemTypeCode = item.getItemType().getCodeValue();
478                             }
479 
480                             if (item.getLocation() != null) {
481                                 sruItemContent = oleLoanDocumentService.getItemInformation(item.getItemIdentifier(), itemTypeCode, shelvingLocationCode, getLocations(item.getLocation(), OleSRUConstants.SHELVING_LOCATION), getLocations(item.getLocation(), OleSRUConstants.LOCAL_LOCATION));
482                                 oleSruItem = (OLESruItem) getOleSruItemHandler().getObjectFromXml(sruItemContent, (Object) new OLESruItem());
483                                 if (oleSruItem != null) {
484                                     String tempLocation = oleSruItem.getShelvingLocation() != null ? oleSruItem.getShelvingLocation() : "";
485                                     if (!"".equals(tempLocation)) {
486                                         oleSRUCirculationDocument.setTemporaryLocation(tempLocation);
487                                     }
488                                 }
489                             }
490 
491                             if(item.getDueDateTime()!=null){
492                                 oleSRUCirculationDocument.setAvailabilityDate(item.getDueDateTime());
493                             }
494                             if (item.getEnumeration() != null && item.getChronology() != null) {
495                                 oleSRUCirculationDocument.setEnumAndChron(item.getEnumeration() + "," + item.getChronology());
496                             }
497                             if (item.getAccessInformation() != null && item.getAccessInformation().getBarcode() != null) {
498                                 oleSRUCirculationDocument.setItemId(item.getAccessInformation().getBarcode());
499                             }
500                             if(item.getTemporaryItemType() !=null && item.getTemporaryItemType().getFullValue()!=null){
501                                 oleSRUCirculationDocument.setRestrictions(item.getTemporaryItemType().getFullValue());
502                                 oleSRUCirculationDocument.setAvailableThru(item.getTemporaryItemType().getFullValue());
503                             }else if(item.getItemType() !=null && item.getItemType().getFullValue()!=null){
504                                 oleSRUCirculationDocument.setRestrictions(item.getItemType().getFullValue());
505                                 oleSRUCirculationDocument.setAvailableThru(item.getItemType().getFullValue());
506                             }
507                             oleSRUCirculationDocumentList.add(oleSRUCirculationDocument);
508                         }
509                     }
510                     oleSRUInstanceDocument.setVolumes(oleSRUInstanceVolumeList);
511                     oleSRUInstanceDocument.setCirculations(oleSRUCirculationDocumentList);
512                 }
513             }
514         }
515         return oleSRUInstanceDocument;
516     }
517 
518 
519     public String getParameter(String name) {
520         String parameter = "";
521         try {
522             Map<String, String> criteriaMap = new HashMap<String, String>();
523             criteriaMap.put("namespaceCode", "OLE-DESC");
524             criteriaMap.put("componentCode", "Describe");
525             criteriaMap.put("name", name);
526             List<ParameterBo> parametersList = (List<ParameterBo>) KRADServiceLocator.getBusinessObjectService().findMatching(ParameterBo.class, criteriaMap);
527             for (ParameterBo parameterBo : parametersList) {
528                 parameter = parameterBo.getValue();
529             }
530         } catch (Exception e) {
531             LOG.error("Exception while getting parameter value", e);
532         }
533         return parameter;
534     }
535     private String getShelvingLocation(Location location) {
536         String locationName = "";
537         if (location.getLocationLevel() != null) {
538             locationName = location.getLocationLevel().getName();
539 
540             if (location.getLocationLevel().getLocationLevel() != null) {
541                 locationName = location.getLocationLevel().getLocationLevel().getName();
542 
543                 if (location.getLocationLevel().getLocationLevel().getLocationLevel() != null) {
544                     locationName = location.getLocationLevel().getLocationLevel().getLocationLevel().getName();
545 
546                     if (location.getLocationLevel().getLocationLevel().getLocationLevel().getLocationLevel() != null) {
547                         locationName = location.getLocationLevel().getLocationLevel().getLocationLevel().getLocationLevel().getName();
548 
549                         if (location.getLocationLevel().getLocationLevel().getLocationLevel().getLocationLevel().getLocationLevel() != null) {
550                             locationName = location.getLocationLevel().getLocationLevel().getLocationLevel().getLocationLevel().getLocationLevel().getName();
551                         }
552                     }
553                 }
554             }
555         }
556         return locationName;
557     }
558     private String getLocations(Location location,String paramName) {
559         String locationName = "";
560         String level =getParameter(paramName);
561         int i = 0;
562         LocationLevel locationLevel = location.getLocationLevel();
563         if (locationLevel != null && level!=null) {
564             while (locationLevel!=null) {
565                 if (locationLevel.getLevel()!=null && locationLevel.getLevel().equalsIgnoreCase(level)) {
566                     locationName = locationLevel.getName();
567                     break;
568                 } else {
569                     locationLevel = locationLevel.getLocationLevel();
570                 }
571 
572             }
573         }
574         return locationName;
575     }
576 
577     private DocstoreClient getDocstoreClient(){
578 
579         if (docstoreClient == null) {
580             docstoreClient = new DocstoreLocalClient();
581         }
582         return docstoreClient;
583     }
584 
585 }