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