1 package org.kuali.ole.docstore.discovery.service;
2
3 import com.thoughtworks.xstream.XStream;
4 import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
5 import org.apache.solr.client.solrj.SolrQuery;
6 import org.apache.solr.client.solrj.SolrServer;
7 import org.apache.solr.client.solrj.response.QueryResponse;
8 import org.apache.solr.common.SolrDocument;
9 import org.kuali.ole.docstore.model.xmlpojo.work.instance.oleml.InstanceCollection;
10 import org.kuali.ole.repository.CheckoutManager;
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13
14 import java.util.List;
15
16
17
18
19
20
21
22
23 public class OleDocstoreDataRetrieveService {
24 private static final Logger LOG = LoggerFactory.getLogger(OleDocstoreDataRetrieveService.class);
25
26
27
28
29
30
31
32 public String getInstanceDetails(List<String> bibUUIDs, String contentType) {
33 StringBuffer output = new StringBuffer();
34 try {
35 for (int i = 0; i < bibUUIDs.size(); i++) {
36 SolrQuery solrQuery = new SolrQuery();
37 solrQuery.setQuery("id:" + bibUUIDs.get(i));
38 solrQuery.setRows(500);
39 SolrServer server = SolrServerManager.getInstance().getSolrServer();
40 QueryResponse response = server.query(solrQuery);
41 List<SolrDocument> solrDocumentList = response.getResults();
42 for (SolrDocument solrDocument : solrDocumentList) {
43 if (solrDocument.getFieldValue("instanceIdentifier") instanceof List) {
44 List<String> instanceIdentifierList = (List<String>) solrDocument.getFieldValue("instanceIdentifier");
45 for (String instanceIdentifier : instanceIdentifierList) {
46 LOG.info("Instant Identifier Value is :" + instanceIdentifier);
47 CheckoutManager checkoutManager = new CheckoutManager();
48 String instanceOutput = checkoutManager.checkOut(instanceIdentifier, null, "checkout");
49 if (contentType.equals("xml")) {
50 output.append(generateXmlInstanceContent(instanceOutput));
51 } else if (contentType.equals("json")) {
52 InstanceCollection instanceCollection = getInstanceCollection(instanceOutput);
53 output.append(generateJsonInstanceContent(instanceCollection));
54 }
55 }
56 } else {
57 String instanceIdentifier = (String) solrDocument.getFieldValue("instanceIdentifier");
58 LOG.info("Instant Identifier Value is :" + instanceIdentifier);
59 CheckoutManager checkoutManager = new CheckoutManager();
60 String instanceOutput = checkoutManager.checkOut(instanceIdentifier, null, "checkout");
61 if (contentType.equals("xml")) {
62 output.append(generateXmlInstanceContent(instanceOutput));
63 } else if (contentType.equals("json")) {
64 InstanceCollection instanceCollection = getInstanceCollection(instanceOutput);
65 output.append(generateJsonInstanceContent(instanceCollection));
66 }
67
68 }
69 }
70 }
71 LOG.info("Instance Output :" + output.toString());
72
73 } catch (Exception e) {
74 e.printStackTrace();
75 }
76 return output.toString();
77 }
78
79
80
81
82
83 public InstanceCollection getInstanceCollection(String instanceData) {
84 XStream xs = new XStream();
85 xs.autodetectAnnotations(true);
86 xs.processAnnotations(InstanceCollection.class);
87 InstanceCollection instanceCollection = (InstanceCollection) xs.fromXML(instanceData);
88 return instanceCollection;
89 }
90
91
92
93
94
95
96 public String generateJsonInstanceContent(InstanceCollection instanceCollection) {
97 XStream stream = new XStream(new JettisonMappedXmlDriver());
98 stream.autodetectAnnotations(true);
99 stream.processAnnotations(InstanceCollection.class);
100 stream.setMode(XStream.NO_REFERENCES);
101 String jsonContent = stream.toXML(instanceCollection);
102 return jsonContent;
103 }
104
105
106
107
108
109
110 public String generateXmlInstanceContent(String instanceContent) {
111 InstanceCollection instanceCollection = getInstanceCollection(instanceContent);
112 OleInstanceXmlConverterService oleInstanceXmlConverterService = new OleInstanceXmlConverterService();
113 String instanceXml = oleInstanceXmlConverterService.generateInstanceCollectionsXml(instanceCollection);
114 return instanceXml;
115 }
116
117 }