1 package org.kuali.ole.docstore.discovery.service;
2
3 import com.thoughtworks.xstream.XStream;
4 import org.apache.solr.client.solrj.SolrQuery;
5 import org.apache.solr.client.solrj.SolrServer;
6 import org.apache.solr.client.solrj.SolrServerException;
7 import org.apache.solr.client.solrj.response.QueryResponse;
8 import org.apache.solr.common.SolrDocument;
9 import org.kuali.ole.DocumentUniqueIDPrefix;
10 import org.kuali.ole.docstore.common.document.BibTree;
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.xstream.HoldingOlemlRecordProcessor;
15 import org.kuali.ole.docstore.common.document.content.instance.xstream.ItemOlemlRecordProcessor;
16 import org.kuali.ole.docstore.common.service.DocstoreService;
17 import org.kuali.ole.docstore.discovery.circulation.XmlContentHandler;
18 import org.kuali.ole.docstore.discovery.circulation.json.CircInstance;
19 import org.kuali.ole.docstore.discovery.circulation.json.InstanceRecord;
20 import org.kuali.ole.docstore.model.xmlpojo.ingest.Request;
21 import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
22 import org.kuali.ole.docstore.model.xmlpojo.ingest.Response;
23 import org.kuali.ole.docstore.model.xmlpojo.work.einstance.oleml.EInstance;
24 import org.kuali.ole.docstore.service.BeanLocator;
25 import org.kuali.ole.repository.CheckoutManager;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 import javax.xml.bind.JAXBException;
30 import java.util.ArrayList;
31 import java.util.Iterator;
32 import java.util.List;
33 import java.util.Map;
34
35
36
37
38
39
40
41
42 public class OleDocstoreDataRetrieveService {
43 private static final Logger LOG = LoggerFactory.getLogger(OleDocstoreDataRetrieveService.class);
44 DocstoreService docstoreService = BeanLocator.getDocstoreService();
45 ItemOlemlRecordProcessor itemOlemlRecordProcessor = new ItemOlemlRecordProcessor();
46 HoldingOlemlRecordProcessor holdingOlemlRecordProcessor = new HoldingOlemlRecordProcessor();
47 private CheckoutManager checkoutManager;
48 private SolrServer solrServer;
49
50
51
52
53
54
55
56 public String getInstanceDetails(List<String> bibUUIDs, String contentType) {
57 InstanceCollection instanceCollection = new InstanceCollection();
58 org.kuali.ole.docstore.model.xmlpojo.work.einstance.oleml.InstanceCollection eInstanceCollection = new org.kuali.ole.docstore.model.xmlpojo.work.einstance.oleml.InstanceCollection();
59 org.kuali.ole.docstore.model.xmlpojo.work.einstance.oleml.InstanceCollection eInstanceCollectionOutput;
60 InstanceCollection instanceCollectionOutput = new InstanceCollection();
61 List<Instance> instances = new ArrayList<Instance>();
62 StringBuffer output = new StringBuffer();
63 boolean hasData = false;
64 List<HoldingsTree> holdingsTree;
65 BibTree bibTree;
66
67 for (int i = 0; i < bibUUIDs.size(); i++) {
68 try {
69 bibTree = docstoreService.retrieveBibTree("wbm-"+bibUUIDs.get(i));
70 holdingsTree = bibTree.getHoldingsTrees();
71 for(HoldingsTree holdingsTree1 : holdingsTree){
72 hasData=true;
73 Holdings holdings = holdingsTree1.getHoldings();
74 Instance instance = new Instance();
75 OleHoldings oleHoldings = holdingOlemlRecordProcessor.fromXML(holdings.getContent());
76 instance.setOleHoldings(oleHoldings);
77 Items items = new Items();
78 List<Item> itemList = new ArrayList<Item>();
79 for(org.kuali.ole.docstore.common.document.Item item : holdingsTree1.getItems()){
80 Item item1 = itemOlemlRecordProcessor.fromXML(item.getContent());
81 itemList.add(item1);
82 }
83 items.setItem(itemList);
84 instance.setItems(items);
85 List<String> bibIdList = new ArrayList<String>();
86 bibIdList.add(bibUUIDs.get(i));
87 instance.setResourceIdentifier(bibIdList);
88 instances.add(instance);
89 }
90
91 }
92 catch(Exception e){
93 LOG.info("Exception occured while retrieving the instance details for the bibId .");
94 LOG.error(e.getMessage(),e);
95 }
96 }
97 instanceCollection.setInstance(instances);
98 if (hasData) {
99 String instanceXml = generateXmlInstanceContent(instanceCollection);
100 String eInstanceXml = getEInstanceContent(eInstanceCollection);
101
102
103
104
105
106
107
108
109
110
111
112
113 if (null == contentType) {
114 output.append(instanceXml);
115 } else if (contentType.equalsIgnoreCase("xml")) {
116 output.append(instanceXml);
117 } else if (contentType.equalsIgnoreCase("json")) {
118 output.append(generateJsonInstanceContent(instanceCollection));
119 }
120 }
121 LOG.info("Instance Output :" + output.toString());
122
123
124 return output.toString();
125 }
126
127 private CheckoutManager getCheckoutManager() {
128 if (null == checkoutManager) {
129 checkoutManager = new CheckoutManager();
130 }
131 return checkoutManager;
132 }
133
134 private SolrServer getSolrServer() throws SolrServerException {
135 if (null == solrServer) {
136 solrServer = SolrServerManager.getInstance().getSolrServer();
137 }
138 return solrServer;
139 }
140
141 public void setCheckoutManager(CheckoutManager checkoutManager) {
142 this.checkoutManager = checkoutManager;
143 }
144
145 public void setSolrServer(SolrServer solrServer) {
146 this.solrServer = solrServer;
147 }
148
149
150
151
152
153 public InstanceCollection getInstanceCollection(String instanceData) {
154 XStream xs = new XStream();
155 xs.autodetectAnnotations(true);
156 xs.processAnnotations(InstanceCollection.class);
157 InstanceCollection instanceCollection = (InstanceCollection) xs.fromXML(instanceData);
158 return instanceCollection;
159 }
160
161
162
163
164
165
166 public String generateJsonInstanceContent(InstanceCollection instanceCollection) {
167 XmlContentHandler xmlContentHandler = new XmlContentHandler();
168 CircInstance circInstance = new CircInstance();
169 List<Instance> instanceList = (instanceCollection.getInstance());
170 ArrayList<org.kuali.ole.docstore.discovery.circulation.json.InstanceCollection> instanceCollectionArrayList = new ArrayList<org.kuali.ole.docstore.discovery.circulation.json.InstanceCollection>();
171 for (Iterator<Instance> iterator = instanceList.iterator(); iterator.hasNext(); ) {
172 Instance instance = iterator.next();
173 org.kuali.ole.docstore.discovery.circulation.json.InstanceCollection col1 = new org.kuali.ole.docstore.discovery.circulation.json.InstanceCollection();
174 InstanceRecord instanceRecord = new InstanceRecord();
175 instanceRecord.setExtension(instance.getExtension());
176 instanceRecord.setResourceIdentifier(instance.getResourceIdentifier());
177 instanceRecord.setFormerResourceIdentifier(instance.getFormerResourceIdentifier());
178 instanceRecord.setInstanceIdentifier(instance.getInstanceIdentifier());
179 instanceRecord.setItems(instance.getItems().getItem());
180 instanceRecord.setOleHoldings(instance.getOleHoldings());
181 instanceRecord.setSourceHoldings(instance.getSourceHoldings());
182 col1.setInstance(instanceRecord);
183 instanceCollectionArrayList.add(col1);
184 }
185
186
187
188
189 circInstance.setInstanceCollection(instanceCollectionArrayList);
190 String jsonContent = null;
191 try{
192 jsonContent = xmlContentHandler.marshalToJSON(circInstance);
193 }catch(Exception e){
194 LOG.info(e.getMessage());
195
196 }
197 return jsonContent;
198 }
199
200 private XmlContentHandler getXmlContentHandler() {
201 return new XmlContentHandler();
202 }
203
204
205
206
207
208 public String generateXmlInstanceContent(InstanceCollection instanceCollection) {
209 OleInstanceXmlConverterService oleInstanceXmlConverterService = new OleInstanceXmlConverterService();
210 String instanceXml = oleInstanceXmlConverterService.generateInstanceCollectionsXml(instanceCollection);
211 return instanceXml;
212 }
213
214 private RequestDocument buildRequestDocument(String cat, String type, String format, String uuid) {
215 RequestDocument reqDoc = new RequestDocument();
216 reqDoc.setCategory(cat);
217 reqDoc.setType(type);
218 reqDoc.setFormat(format);
219 reqDoc.setUuid(uuid);
220 return reqDoc;
221
222 }
223
224 public String getEInstanceContent( org.kuali.ole.docstore.model.xmlpojo.work.einstance.oleml.InstanceCollection eInstanceCollection){
225 XStream xStream = new XStream();
226 xStream.alias("eInstanceCollection",org.kuali.ole.docstore.model.xmlpojo.work.einstance.oleml.InstanceCollection.class);
227 xStream.alias("eInstance",org.kuali.ole.docstore.model.xmlpojo.work.einstance.oleml.EInstance.class);
228 xStream.aliasField("eInstances",org.kuali.ole.docstore.model.xmlpojo.work.einstance.oleml.InstanceCollection.class,"eInstance");
229 return xStream.toXML(eInstanceCollection);
230 }
231
232 public org.kuali.ole.docstore.model.xmlpojo.work.einstance.oleml.InstanceCollection getEInstanceCollection(String instanceData) {
233 XStream xs = new XStream();
234 xs.autodetectAnnotations(true);
235 xs.processAnnotations(org.kuali.ole.docstore.model.xmlpojo.work.einstance.oleml.InstanceCollection.class);
236 xs.alias("eInstance",org.kuali.ole.docstore.model.xmlpojo.work.einstance.oleml.EInstance.class);
237 xs.aliasField("eInstances",org.kuali.ole.docstore.model.xmlpojo.work.einstance.oleml.InstanceCollection.class,"eInstance");
238 org.kuali.ole.docstore.model.xmlpojo.work.einstance.oleml.InstanceCollection instanceCollection = ( org.kuali.ole.docstore.model.xmlpojo.work.einstance.oleml.InstanceCollection) xs.fromXML(instanceData);
239 return instanceCollection;
240 }
241
242
243 }