1 package org.kuali.ole.systemintegration.rest.service;
2
3 import com.thoughtworks.xstream.XStream;
4 import org.apache.solr.client.solrj.SolrServer;
5 import org.apache.solr.client.solrj.SolrServerException;
6 import org.kuali.ole.OLEConstants;
7 import org.kuali.ole.describe.bo.OleLocation;
8 import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
9 import org.kuali.ole.docstore.common.document.BibTree;
10 import org.kuali.ole.docstore.common.document.BibTrees;
11 import org.kuali.ole.docstore.common.document.Holdings;
12 import org.kuali.ole.docstore.common.document.HoldingsTree;
13 import org.kuali.ole.docstore.common.document.content.instance.*;
14 import org.kuali.ole.docstore.common.document.content.instance.Item;
15 import org.kuali.ole.docstore.common.document.content.instance.Items;
16 import org.kuali.ole.docstore.common.document.content.instance.xstream.HoldingOlemlRecordProcessor;
17 import org.kuali.ole.docstore.common.document.content.instance.xstream.ItemOlemlRecordProcessor;
18 import org.kuali.ole.docstore.discovery.service.SolrServerManager;
19 import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
20 import org.kuali.ole.docstore.model.xmlpojo.work.einstance.oleml.EInstance;
21 import org.kuali.ole.select.bo.OLESerialReceivingDocument;
22 import org.kuali.ole.select.bo.OLESerialReceivingHistory;
23 import org.kuali.ole.service.impl.OLESerialReceivingServiceImpl;
24 import org.kuali.ole.sys.context.SpringContext;
25 import org.kuali.ole.systemintegration.rest.RestConstants;
26 import org.kuali.ole.systemintegration.rest.bo.*;
27 import org.kuali.ole.systemintegration.rest.circulation.XmlContentHandler;
28 import org.kuali.rice.krad.service.BusinessObjectService;
29 import org.kuali.rice.krad.service.KRADServiceLocator;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 import java.util.*;
34
35
36
37
38
39
40
41
42 public class DocstoreDataRetrieveService {
43 private static final Logger LOG = LoggerFactory.getLogger(DocstoreDataRetrieveService.class);
44 private DocstoreClientLocator docstoreClientLocator;
45 ItemOlemlRecordProcessor itemOlemlRecordProcessor = new ItemOlemlRecordProcessor();
46 HoldingOlemlRecordProcessor holdingOlemlRecordProcessor = new HoldingOlemlRecordProcessor();
47 private SolrServer solrServer;
48 private OLESerialReceivingServiceImpl oleSerialReceivingService = new OLESerialReceivingServiceImpl();
49
50 public DocstoreClientLocator getDocstoreClientLocator() {
51 if (null == docstoreClientLocator) {
52 return SpringContext.getBean(DocstoreClientLocator.class);
53 }
54 return docstoreClientLocator;
55 }
56
57
58
59
60
61
62 @Deprecated
63 public String getInstanceDetails(List<String> bibUUIDs, String contentType) {
64 InstanceCollection instanceCollection = new InstanceCollection();
65 org.kuali.ole.docstore.model.xmlpojo.work.einstance.oleml.InstanceCollection eInstanceCollection = new org.kuali.ole.docstore.model.xmlpojo.work.einstance.oleml.InstanceCollection();
66 org.kuali.ole.docstore.model.xmlpojo.work.einstance.oleml.InstanceCollection eInstanceCollectionOutput;
67 InstanceCollection instanceCollectionOutput = new InstanceCollection();
68 List<Instance> instances = new ArrayList<Instance>();
69 StringBuffer output = new StringBuffer();
70 boolean hasData = false;
71 List<HoldingsTree> holdingsTree;
72 BibTree bibTree;
73 BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
74
75 for (int i = 0; i < bibUUIDs.size(); i++) {
76 try {
77 bibTree = getDocstoreClientLocator().getDocstoreClient().retrieveBibTree(RestConstants.BIB_ID_PREFIX+bibUUIDs.get(i));
78 holdingsTree = bibTree.getHoldingsTrees();
79 for(HoldingsTree holdingsTree1 : holdingsTree){
80 hasData=true;
81 Holdings holdings = holdingsTree1.getHoldings();
82 Instance instance = new Instance();
83 OleHoldings oleHoldings = holdingOlemlRecordProcessor.fromXML(holdings.getContent());
84 instance.setOleHoldings(oleHoldings);
85 HoldingsSerialReceiving oleHoldingsSerialReceiving = new HoldingsSerialReceiving();
86 oleHoldingsSerialReceiving = oleHoldingsSerialReceiving.copyHoldingSerialReceiving(oleHoldings);
87 Map<String,String> requestMap = new HashMap<String, String>();
88 requestMap.put(RestConstants.INSTACE_ID,instance.getOleHoldings().getHoldingsIdentifier());
89 requestMap.put(RestConstants.BIB_ID,RestConstants.BIB_ID_PREFIX+bibUUIDs.get(i));
90 SerialReceiving oleSerialReceiving = new SerialReceiving();
91 List<OLESerialReceivingDocument> oleSerialReceivingDocuments = (List<OLESerialReceivingDocument>)businessObjectService.findMatching(OLESerialReceivingDocument.class,requestMap);
92 if(oleSerialReceivingDocuments!=null && oleSerialReceivingDocuments.size()>0){
93 if(oleSerialReceivingDocuments.get(0).isPublicDisplay()){
94 oleSerialReceiving = populateOleSerialReceiving(oleSerialReceivingDocuments.get(0));
95 HoldingsSerialHistory oleHoldingsSerialHistory = new HoldingsSerialHistory();
96 List<SerialReceivingMain> oleSerialReceivingMainList = new ArrayList<>();
97 List<SerialReceivingIndex> oleSerialReceivingIndexList = new ArrayList<>();
98 List<SerialReceivingSupplement> oleSerialReceivingSupplementList = new ArrayList<>();
99 for(OLESerialReceivingHistory oleSerialReceivingHistory:oleSerialReceivingDocuments.get(0).getOleSerialReceivingHistoryList()){
100 oleSerialReceivingService.setEnumerationAndChronologyValues(oleSerialReceivingHistory);
101 if(oleSerialReceivingHistory.getReceivingRecordType().equals(RestConstants.MAIN) && oleSerialReceivingHistory.isPublicDisplay()){
102 SerialReceivingMain oleSerialReceivingMain = new SerialReceivingMain();
103 oleSerialReceivingMain.setChronologyCaption(oleSerialReceivingHistory.getChronologyCaption());
104 oleSerialReceivingMain.setEnumerationCaption(oleSerialReceivingHistory.getEnumerationCaption());
105 oleSerialReceivingMain.setPublicReceiptNote(oleSerialReceivingHistory.getPublicReceipt());
106 oleSerialReceivingMainList.add(oleSerialReceivingMain);
107 }else if(oleSerialReceivingHistory.getReceivingRecordType().equals(RestConstants.INDEX) && oleSerialReceivingHistory.isPublicDisplay()){
108 oleSerialReceivingService.setEnumerationAndChronologyValues(oleSerialReceivingHistory);
109 SerialReceivingIndex oleSerialReceivingindex = new SerialReceivingIndex();
110 oleSerialReceivingindex.setChronologyCaption(oleSerialReceivingHistory.getChronologyCaption());
111 oleSerialReceivingindex.setEnumerationCaption(oleSerialReceivingHistory.getEnumerationCaption());
112 oleSerialReceivingindex.setPublicReceiptNote(oleSerialReceivingHistory.getPublicReceipt());
113 oleSerialReceivingIndexList.add(oleSerialReceivingindex);
114 }else if(oleSerialReceivingHistory.getReceivingRecordType().equals(RestConstants.SUPPLEMENTARY) && oleSerialReceivingHistory.isPublicDisplay()){
115 oleSerialReceivingService.setEnumerationAndChronologyValues(oleSerialReceivingHistory);
116 SerialReceivingSupplement oleSerialReceivingSupplement = new SerialReceivingSupplement();
117 oleSerialReceivingSupplement.setChronologyCaption(oleSerialReceivingHistory.getChronologyCaption());
118 oleSerialReceivingSupplement.setEnumerationCaption(oleSerialReceivingHistory.getEnumerationCaption());
119 oleSerialReceivingSupplement.setPublicReceiptNote(oleSerialReceivingHistory.getPublicReceipt());
120 oleSerialReceivingSupplementList.add(oleSerialReceivingSupplement);
121 }
122 }
123
124
125
126
127
128
129
130
131
132
133
134 }
135 }
136 instance.setOleHoldings(oleHoldingsSerialReceiving);
137 Items items = new Items();
138 List<Item> itemList = new ArrayList<Item>();
139 for(org.kuali.ole.docstore.common.document.Item item : holdingsTree1.getItems()){
140 Item item1 = itemOlemlRecordProcessor.fromXML(item.getContent());
141 itemList.add(item1);
142 }
143 items.setItem(itemList);
144 instance.setItems(items);
145 List<String> bibIdList = new ArrayList<String>();
146 bibIdList.add(bibUUIDs.get(i));
147 instance.setResourceIdentifier(bibIdList);
148 instances.add(instance);
149 }
150 }
151 catch(Exception e){
152 LOG.info("Exception occured while retrieving the instance details for the bibId .");
153 LOG.error(e.getMessage(),e);
154 }
155 }
156 instanceCollection.setInstance(instances);
157 if (hasData) {
158 String instanceXml = generateXmlInstanceContent(instanceCollection);
159 String eInstanceXml = getEInstanceContent(eInstanceCollection);
160
161
162
163
164
165
166
167
168
169
170
171
172 if (null == contentType) {
173 output.append(instanceXml);
174 } else if (contentType.equalsIgnoreCase(RestConstants.XML)) {
175 output.append(instanceXml);
176 } else if (contentType.equalsIgnoreCase(RestConstants.JSON)) {
177
178 }
179 }
180 LOG.info("Instance Output :" + output.toString());
181 return output.toString();
182 }
183
184 public String getHoldingsTree(List<String> bibUUIDs, String contentType) {
185 StringBuffer output = new StringBuffer();
186 boolean hasData = false;
187 List<HoldingsTree> holdingsTrees;
188 BibTrees bibTrees;
189 HoldingsTrees oleHoldingsTrees = new HoldingsTrees();
190 List<org.kuali.ole.docstore.common.document.content.instance.HoldingsTree> oleHoldingsTreeList = new ArrayList<>();
191 BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
192 StringBuffer instanceXml = new StringBuffer();
193
194 try {
195 bibTrees = (BibTrees) getDocstoreClientLocator().getDocstoreClient().retrieveBibTrees(bibUUIDs);
196 for (int i = 0; i < bibTrees.getBibTrees().size(); i++) {
197 BibTree bibTree = bibTrees.getBibTrees().get(i);
198 holdingsTrees = bibTree.getHoldingsTrees();
199 for(HoldingsTree holdingsTree : holdingsTrees){
200 org.kuali.ole.docstore.common.document.content.instance.HoldingsTree oleHoldingsTree = new org.kuali.ole.docstore.common.document.content.instance.HoldingsTree();
201 hasData=true;
202 Holdings holdings = holdingsTree.getHoldings();
203 OleHoldings oleHoldings = holdingOlemlRecordProcessor.fromXML(holdings.getContent());
204 oleHoldings = processLocationName(oleHoldings);
205 HoldingsSerialReceiving oleHoldingsSerialReceiving = new HoldingsSerialReceiving();
206 oleHoldingsSerialReceiving = oleHoldingsSerialReceiving.copyHoldingSerialReceiving(oleHoldings);
207 Map<String,Object> requestMap = new HashMap<String, Object>();
208 requestMap.put(RestConstants.INSTACE_ID, oleHoldings.getHoldingsIdentifier());
209 requestMap.put(RestConstants.BIB_ID,RestConstants.BIB_ID_PREFIX+bibUUIDs.get(i));
210 requestMap.put(RestConstants.ACTIVE,Boolean.TRUE);
211 SerialReceiving oleSerialReceiving = new SerialReceiving();
212 List<OLESerialReceivingDocument> oleSerialReceivingDocuments = (List<OLESerialReceivingDocument>)businessObjectService.findMatching(OLESerialReceivingDocument.class,requestMap);
213 if(oleSerialReceivingDocuments!=null && oleSerialReceivingDocuments.size()>0){
214
215 oleSerialReceiving = populateOleSerialReceiving(oleSerialReceivingDocuments.get(0));
216 HoldingsSerialHistory oleHoldingsSerialHistory = new HoldingsSerialHistory();
217 List<SerialReceivingMain> oleSerialReceivingMainList = new ArrayList<>();
218 List<SerialReceivingIndex> oleSerialReceivingIndexList = new ArrayList<>();
219 List<SerialReceivingSupplement> oleSerialReceivingSupplementList = new ArrayList<>();
220 for(OLESerialReceivingHistory oleSerialReceivingHistory:oleSerialReceivingDocuments.get(0).getOleSerialReceivingHistoryList()){
221 oleSerialReceivingService.setEnumerationAndChronologyValues(oleSerialReceivingHistory);
222 if(oleSerialReceivingHistory.getReceivingRecordType().equals(RestConstants.MAIN) && oleSerialReceivingHistory.isPublicDisplay()){
223 SerialReceivingMain oleSerialReceivingMain = new SerialReceivingMain();
224 oleSerialReceivingMain.setChronologyCaption(oleSerialReceivingHistory.getChronologyCaption());
225 oleSerialReceivingMain.setEnumerationCaption(oleSerialReceivingHistory.getEnumerationCaption());
226 oleSerialReceivingMain.setPublicReceiptNote(oleSerialReceivingHistory.getPublicReceipt());
227 oleSerialReceivingMainList.add(oleSerialReceivingMain);
228 }else if(oleSerialReceivingHistory.getReceivingRecordType().equals(RestConstants.INDEX) && oleSerialReceivingHistory.isPublicDisplay()){
229 oleSerialReceivingService.setEnumerationAndChronologyValues(oleSerialReceivingHistory);
230 SerialReceivingIndex oleSerialReceivingindex = new SerialReceivingIndex();
231 oleSerialReceivingindex.setChronologyCaption(oleSerialReceivingHistory.getChronologyCaption());
232 oleSerialReceivingindex.setEnumerationCaption(oleSerialReceivingHistory.getEnumerationCaption());
233 oleSerialReceivingindex.setPublicReceiptNote(oleSerialReceivingHistory.getPublicReceipt());
234 oleSerialReceivingIndexList.add(oleSerialReceivingindex);
235 }else if(oleSerialReceivingHistory.getReceivingRecordType().equals(RestConstants.SUPPLEMENTARY) && oleSerialReceivingHistory.isPublicDisplay()){
236 oleSerialReceivingService.setEnumerationAndChronologyValues(oleSerialReceivingHistory);
237 SerialReceivingSupplement oleSerialReceivingSupplement = new SerialReceivingSupplement();
238 oleSerialReceivingSupplement.setChronologyCaption(oleSerialReceivingHistory.getChronologyCaption());
239 oleSerialReceivingSupplement.setEnumerationCaption(oleSerialReceivingHistory.getEnumerationCaption());
240 oleSerialReceivingSupplement.setPublicReceiptNote(oleSerialReceivingHistory.getPublicReceipt());
241 oleSerialReceivingSupplementList.add(oleSerialReceivingSupplement);
242 }
243 }
244 Indexes serialReceivingIndexes = new Indexes();
245 serialReceivingIndexes.setIndex(oleSerialReceivingIndexList);
246 Mains serialReceivingMains = new Mains();
247 serialReceivingMains.setMain(oleSerialReceivingMainList);
248 Supplementaries serialReceivingSupplements = new Supplementaries();
249 serialReceivingSupplements.setSupplementary(oleSerialReceivingSupplementList);
250 oleHoldingsSerialHistory.setMains(serialReceivingMains);
251 oleHoldingsSerialHistory.setIndexes(serialReceivingIndexes);
252 oleHoldingsSerialHistory.setSupplementaries(serialReceivingSupplements);
253 oleSerialReceiving.setSerialReceivingHistory(oleHoldingsSerialHistory);
254 oleHoldingsSerialReceiving.setSerialReceiving(oleSerialReceiving);
255
256 }
257 oleHoldingsTree.setOleHoldings(oleHoldingsSerialReceiving);
258 Items items = new Items();
259 List<Item> itemList = new ArrayList<Item>();
260 for(org.kuali.ole.docstore.common.document.Item item : holdingsTree.getItems()){
261 Item item1 = itemOlemlRecordProcessor.fromXML(item.getContent());
262 itemList.add(item1);
263 }
264 items.getItem().addAll((List)itemList);
265 oleHoldingsTree.setItems(items);
266 List<String> bibIdList = new ArrayList<String>();
267 bibIdList.add(bibUUIDs.get(i));
268 oleHoldingsTreeList.add(oleHoldingsTree);
269 }
270 }
271 }
272 catch(Exception e){
273 LOG.info("Exception occured while retrieving the instance details for the bibId .");
274 LOG.error(e.getMessage(),e);
275 }
276
277 oleHoldingsTrees.setHoldingsTrees(oleHoldingsTreeList);
278 if (hasData) {
279 if(oleHoldingsTrees.getHoldingsTrees().size()>0){
280 instanceXml.append(oleHoldingsTrees.serialize(oleHoldingsTrees));
281 }
282 if (null == contentType) {
283 output.append(instanceXml);
284 } else if (contentType.equalsIgnoreCase(RestConstants.XML)) {
285 output.append(instanceXml);
286 } else if (contentType.equalsIgnoreCase(RestConstants.JSON)) {
287 output.append(generateJsonInstanceContent(oleHoldingsTrees));
288 }
289 }
290 LOG.info("Instance Output :" + output.toString());
291 return output.toString();
292 }
293
294
295
296
297
298
299 public OleHoldings processLocationName(OleHoldings oleHoldings) {
300 if (oleHoldings.getLocation() != null) {
301 Location location = oleHoldings.getLocation();
302 LocationLevel locationLevel = location.getLocationLevel();
303 while (locationLevel != null) {
304 Map<String, String> locationMap = new HashMap<String, String>();
305 locationMap.put("locationCode", locationLevel.getName());
306 List<OleLocation> oleLocationList = (List<OleLocation>) KRADServiceLocator.getBusinessObjectService().findMatching(OleLocation.class, locationMap);
307 if (oleLocationList.size() > 0) {
308 locationLevel.setCode(locationLevel.getName());
309 locationLevel.setName(oleLocationList.get(0).getLocationName());
310 }
311 locationLevel = locationLevel.getLocationLevel();
312 }
313 }
314 return oleHoldings;
315 }
316
317 private SolrServer getSolrServer() throws SolrServerException {
318 if (null == solrServer) {
319 solrServer = SolrServerManager.getInstance().getSolrServer();
320 }
321 return solrServer;
322 }
323
324 public void setSolrServer(SolrServer solrServer) {
325 this.solrServer = solrServer;
326 }
327
328
329
330
331
332 public InstanceCollection getInstanceCollection(String instanceData) {
333 XStream xs = new XStream();
334 xs.autodetectAnnotations(true);
335 xs.processAnnotations(InstanceCollection.class);
336 InstanceCollection instanceCollection = (InstanceCollection) xs.fromXML(instanceData);
337 return instanceCollection;
338 }
339
340
341 public String generateJsonInstanceContent(HoldingsTrees holdingsTree) {
342 XmlContentHandler xmlContentHandler = new XmlContentHandler();
343 String jsonContent = null;
344 try{
345 jsonContent = xmlContentHandler.marshalToJSON(holdingsTree);
346 }catch(Exception e){
347 LOG.info(e.getMessage());
348
349 }
350 return jsonContent;
351 }
352
353 private XmlContentHandler getXmlContentHandler() {
354 return new XmlContentHandler();
355 }
356
357
358
359
360
361 public String generateXmlInstanceContent(InstanceCollection instanceCollection) {
362 InstanceXmlConverterService oleInstanceXmlConverterService = new InstanceXmlConverterService();
363 String instanceXml = oleInstanceXmlConverterService.generateInstanceCollectionsXml(instanceCollection);
364 return instanceXml;
365 }
366
367 private RequestDocument buildRequestDocument(String cat, String type, String format, String uuid) {
368 RequestDocument reqDoc = new RequestDocument();
369 reqDoc.setCategory(cat);
370 reqDoc.setType(type);
371 reqDoc.setFormat(format);
372 reqDoc.setUuid(uuid);
373 return reqDoc;
374
375 }
376
377 public String getEInstanceContent( org.kuali.ole.docstore.model.xmlpojo.work.einstance.oleml.InstanceCollection eInstanceCollection){
378 XStream xStream = new XStream();
379 xStream.alias(RestConstants.EINSTANCE_COLLECTION,org.kuali.ole.docstore.model.xmlpojo.work.einstance.oleml.InstanceCollection.class);
380 xStream.alias(RestConstants.EINSTANCE,EInstance.class);
381 xStream.aliasField(RestConstants.EINSTANCES,org.kuali.ole.docstore.model.xmlpojo.work.einstance.oleml.InstanceCollection.class,RestConstants.EINSTANCE);
382 return xStream.toXML(eInstanceCollection);
383 }
384
385 public org.kuali.ole.docstore.model.xmlpojo.work.einstance.oleml.InstanceCollection getEInstanceCollection(String instanceData) {
386 XStream xs = new XStream();
387 xs.autodetectAnnotations(true);
388 xs.processAnnotations(org.kuali.ole.docstore.model.xmlpojo.work.einstance.oleml.InstanceCollection.class);
389 xs.alias(RestConstants.EINSTANCE,EInstance.class);
390 xs.aliasField(RestConstants.EINSTANCES,org.kuali.ole.docstore.model.xmlpojo.work.einstance.oleml.InstanceCollection.class,RestConstants.EINSTANCE);
391 org.kuali.ole.docstore.model.xmlpojo.work.einstance.oleml.InstanceCollection instanceCollection = ( org.kuali.ole.docstore.model.xmlpojo.work.einstance.oleml.InstanceCollection) xs.fromXML(instanceData);
392 return instanceCollection;
393 }
394
395 public SerialReceiving populateOleSerialReceiving(OLESerialReceivingDocument oleSerialReceivingDocument){
396 SerialReceiving serialReceiving = new SerialReceiving();
397 serialReceiving.setCallNumber(oleSerialReceivingDocument.getCallNumber());
398 serialReceiving.setUnboundLocation(generateLocation(oleSerialReceivingDocument.getUnboundLocation()));
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421 return serialReceiving;
422
423 }
424
425 public UnboundLocation generateLocation(String locationName){
426 LocationLevel locationLevel = new LocationLevel();
427 locationLevel = createLocationLevel(locationName, locationLevel);
428 UnboundLocation unboundLocation = new UnboundLocation();
429 unboundLocation.setLocationLevel(locationLevel);
430 return unboundLocation;
431 }
432
433 public LocationLevel createLocationLevel(String locationName, LocationLevel locationLevel) {
434 LOG.debug("Inside the instance details createLocationLevel method");
435 if (locationName != null && !locationName.equalsIgnoreCase("")) {
436 BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
437 String[] names = locationName.split("/");
438 Map parentCriteria = new HashMap();
439 parentCriteria.put(OLEConstants.LOC_CD, names[0]);
440 OleLocation oleLocationCollection = businessObjectService.findByPrimaryKey(OleLocation.class, parentCriteria);
441 String levelName = oleLocationCollection.getLocationName();
442 String locationCode = oleLocationCollection.getLocationCode();
443 String levelCode = oleLocationCollection.getOleLocationLevel().getLevelName();
444 locationLevel.setName(levelName);
445 locationLevel.setLevel(levelCode);
446 locationLevel.setCode(locationCode);
447 String locName = "";
448 if (locationName.contains(OLEConstants.SLASH))
449 locName = locationName.replace(names[0] + OLEConstants.SLASH, "");
450 else
451 locName = locationName.replace(names[0], "");
452 if (locName != null && !locName.equals("")) {
453 LocationLevel newLocationLevel = new LocationLevel();
454 locationLevel.setLocationLevel(createLocationLevel(locName, newLocationLevel));
455 }
456 }
457 return locationLevel;
458 }
459
460
461 }