1 package org.kuali.ole.editor.service;
2
3
4 import org.kuali.ole.*;
5 import org.kuali.ole.docstore.model.xmlpojo.ingest.AdditionalAttributes;
6 import org.kuali.ole.docstore.model.xmlpojo.ingest.Content;
7 import org.kuali.ole.docstore.model.xmlpojo.ingest.Request;
8 import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
9 import org.kuali.ole.docstore.model.xmlpojo.work.instance.oleml.*;
10 import org.kuali.ole.docstore.model.xstream.ingest.RequestHandler;
11 import org.kuali.ole.docstore.model.xstream.work.instance.oleml.WorkInstanceOlemlRecordProcessor;
12 import org.kuali.ole.ingest.pojo.ProfileAttributeBo;
13 import org.kuali.ole.pojo.ProfileAttribute;
14 import org.kuali.ole.pojo.bib.BibliographicRecord;
15
16
17
18 import org.kuali.rice.krad.util.GlobalVariables;
19
20 import java.io.*;
21 import java.net.URL;
22 import java.net.URLConnection;
23 import java.net.URLEncoder;
24 import java.text.DateFormat;
25 import java.text.SimpleDateFormat;
26 import java.util.ArrayList;
27 import java.util.Date;
28 import java.util.Iterator;
29 import java.util.List;
30
31
32
33
34 public class DocstoreHelperService {
35
36 private static final String DOCSTORE_URL = "docstore.url";
37 private final String CREATE_NEW_DOCSTORE_RECORD_QUERY_STRING = "docAction=ingestContent&stringContent=";
38 private final String CHECKOUT_DOCSTORE_RECORD_QUERY_STRING = "docAction=checkOut&dataType=raw&uuid=";
39 private final String UPDATE_EXISTING_DOCSTORE_RECORD_QUERY_STRING = "docAction=checkIn&stringContent=";
40 private final String ROLLBACK_DATA_FROM_DOCSTORE = "docAction=deleteWithLinkedDocs&requestContent=";
41
42 private List<ProfileAttribute> attributes = new ArrayList<ProfileAttribute>();
43
44 public String persistToDocstoreForEditor(String content, String uuid, String format) throws Exception {
45 String responseFromDocstore = callToDocstore(content, uuid, format);
46 return responseFromDocstore;
47 }
48
49 private String callToDocstore(String content, String uuid, String format) throws Exception {
50 String docstoreURL = PropertyUtil.getPropertyUtil().getProperty(DOCSTORE_URL);
51 String queryString = null;
52
53 if (null == uuid) {
54 String xmlContent = buildReuestDocXML(content, format);
55 queryString = CREATE_NEW_DOCSTORE_RECORD_QUERY_STRING + URLEncoder.encode(xmlContent);
56 } else {
57 String xmlContent = buildReuestDocXML(content, uuid, format);
58 queryString = UPDATE_EXISTING_DOCSTORE_RECORD_QUERY_STRING + URLEncoder.encode(xmlContent);
59 }
60
61 return postData(docstoreURL, queryString);
62 }
63
64 public String persistNewToDocstoreForIngest(BibliographicRecord bibliographicRecord, List<ProfileAttributeBo> profileAttributes) throws Exception {
65 this.attributes = buildListOfProfileAttributes(profileAttributes);
66 BibliographicRecordHandler bibliographicRecordHandler = new BibliographicRecordHandler();
67 String bibXMLContent = bibliographicRecordHandler.generateXML(bibliographicRecord);
68 String instanceXMLContent = getInstanceXML(bibliographicRecord);
69 String requestXML = buildReuestDocXMLForIngest(bibXMLContent, instanceXMLContent);
70 String queryString = CREATE_NEW_DOCSTORE_RECORD_QUERY_STRING + URLEncoder.encode(requestXML);
71 String docstoreURL = PropertyUtil.getPropertyUtil().getProperty(DOCSTORE_URL);
72 String responseXML = postData(docstoreURL, queryString + queryString);
73 return responseXML;
74 }
75
76 public String getDocstoreData(String uuid) throws Exception {
77 String docstoreURL = PropertyUtil.getPropertyUtil().getProperty(DOCSTORE_URL);
78 String queryString = CHECKOUT_DOCSTORE_RECORD_QUERY_STRING + uuid;
79 String responseFromDocstore = postData(docstoreURL, queryString);
80 return responseFromDocstore;
81 }
82
83 public static String postData(String target, String content) throws Exception {
84 String response = "";
85 URL url = new URL(target);
86 URLConnection conn = url.openConnection();
87 conn.setDoInput(true);
88 conn.setDoOutput(true);
89 conn.setUseCaches(false);
90 conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
91
92 Writer w = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
93 w.write(content);
94 w.close();
95 BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
96 String temp;
97 while ((temp = in.readLine()) != null) {
98 response += temp + "\n";
99 }
100 in.close();
101 return response;
102 }
103
104
105 private String buildReuestDocXML(String xmlContent, String format) {
106 return buildReuestDocXML(xmlContent, null, format);
107 }
108
109
110 private String buildReuestDocXMLForIngest(String bibXML, String instanceXML) {
111 Request requestObject = new Request();
112 RequestDocument requestDocument = new RequestDocument();
113 requestDocument.setId("1");
114 requestDocument.setCategory(OLEConstants.WORK_CATEGORY);
115 requestDocument.setType(OLEConstants.BIB_DOC_TYPE);
116 requestDocument.setFormat(OLEConstants.MARC_FORMAT);
117 requestDocument.setContent(new Content(bibXML));
118
119 AdditionalAttributes additionalAttributes = new AdditionalAttributes();
120 DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
121 additionalAttributes.setDateEntered(String.valueOf(dateFormat.format(new Date())));
122 additionalAttributes.setLastUpdated(String.valueOf(dateFormat.format(new Date())));
123 additionalAttributes.setFastAddFlag("true");
124 additionalAttributes.setSupressFromPublic("false");
125 additionalAttributes.setAttribute("dateEntered",String.valueOf(dateFormat.format(new Date())));
126 additionalAttributes.setAttribute("lastUpdated",String.valueOf(dateFormat.format(new Date())));
127 additionalAttributes.setAttribute("fastAddFlag","true");
128 additionalAttributes.setAttribute("supressFromPublic","false");
129 requestDocument.setAdditionalAttributes(additionalAttributes);
130
131 RequestDocument linkedRequestDocument = new RequestDocument();
132 linkedRequestDocument.setId("2");
133 linkedRequestDocument.setCategory(OLEConstants.WORK_CATEGORY);
134 linkedRequestDocument.setType(OLEConstants.INSTANCE_DOC_TYPE);
135 linkedRequestDocument.setContent(new Content(instanceXML));
136 linkedRequestDocument.setFormat(OLEConstants.OLEML_FORMAT);
137
138 ArrayList<RequestDocument> linkedRequestDocuments = new ArrayList<RequestDocument>();
139 linkedRequestDocuments.add(linkedRequestDocument);
140
141 requestDocument.setLinkedRequestDocuments(linkedRequestDocuments);
142
143 ArrayList<RequestDocument> requestDocuments = new ArrayList<RequestDocument>();
144 requestDocuments.add(requestDocument);
145 requestObject.setRequestDocuments(requestDocuments);
146
147 RequestHandler requestHandler = new RequestHandler();
148 String xml = requestHandler.toXML(requestObject);
149 return xml;
150 }
151
152
153 private String buildReuestDocXML(String xmlContent, String uuid, String format) {
154 String INSTANCE_MARC_XML_STRING = "<instanceCollection xmlns=\"http://ole.kuali.org/standards/ole-instance\" " +
155 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://ole.kuali.org/standards/ole-instance " +
156 "OLE Instance Schema v8.1.xsd \">" +
157 "<instance>" +
158 "<holdings></holdings>" +
159 "<item></item>" +
160 "</instance>" +
161 "</instanceCollection> ";
162
163 Request requestObject = new Request();
164 RequestDocument requestDocument = new RequestDocument();
165
166 if (null == uuid) {
167 requestDocument.setId("1");
168 RequestDocument linkedRequestDocument = new RequestDocument();
169 linkedRequestDocument.setId("2");
170 linkedRequestDocument.setCategory(OLEConstants.WORK_CATEGORY);
171 linkedRequestDocument.setType(OLEConstants.INSTANCE_DOC_TYPE);
172 linkedRequestDocument.setContent(new Content(INSTANCE_MARC_XML_STRING));
173 linkedRequestDocument.setFormat(OLEConstants.OLEML_FORMAT);
174
175 ArrayList<RequestDocument> linkedRequestDocuments = new ArrayList<RequestDocument>();
176 linkedRequestDocuments.add(linkedRequestDocument);
177
178 requestDocument.setLinkedRequestDocuments(linkedRequestDocuments);
179 } else {
180 requestDocument.setId(uuid);
181 requestObject.setOperation("checkIn");
182 requestObject.setUser("editor");
183 }
184 requestDocument.setCategory(OLEConstants.WORK_CATEGORY);
185 requestDocument.setType(OLEConstants.BIB_DOC_TYPE);
186
187 if (format.equalsIgnoreCase("marc")) {
188 requestDocument.setFormat(OLEConstants.MARC_FORMAT);
189 } else if (format.equalsIgnoreCase("dublinunq")) {
190 requestDocument.setFormat(OLEConstants.UNQUALIFIED_DUBLIN_FORMAT);
191 }
192
193 requestDocument.setContent(new Content(xmlContent));
194
195 if (format.equalsIgnoreCase("marc")) {
196 AdditionalAttributes additionalAttributes = new AdditionalAttributes();
197 DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
198 if (null == uuid) {
199 additionalAttributes.setDateEntered(String.valueOf(dateFormat.format(new Date())));
200 }
201 additionalAttributes.setLastUpdated(String.valueOf(dateFormat.format(new Date())));
202 additionalAttributes.setFastAddFlag("true");
203 additionalAttributes.setSupressFromPublic("false");
204 requestDocument.setAdditionalAttributes(additionalAttributes);
205 }
206
207 ArrayList<RequestDocument> requestDocuments = new ArrayList<RequestDocument>();
208 requestDocuments.add(requestDocument);
209 requestObject.setRequestDocuments(requestDocuments);
210
211 RequestHandler requestHandler = new RequestHandler();
212 String xml = requestHandler.toXML(requestObject);
213
214 if (format.equalsIgnoreCase(OLEConstants.UNQUALIFIED_DUBLIN_FORMAT)) {
215 xml = modifyXmlContent(xml);
216 }
217 return xml;
218 }
219
220 public String getInstanceXML(BibliographicRecord bibliographicRecord) {
221 WorkInstanceOlemlRecordProcessor workInstanceOlemlRecordProcessor = new WorkInstanceOlemlRecordProcessor();
222 InstanceCollection instanceCollection = new InstanceCollection();
223 OleInstance oleInstance = new OleInstance();
224 List<OleItem> oleItemList = new ArrayList<OleItem>();
225 oleItemList.add(getOleItem(bibliographicRecord)) ;
226 oleInstance.setItem(oleItemList);
227 oleInstance.setHoldings(getOleHolding());
228
229 Extension extension = new Extension();
230 AdditionalAttributes additionalAttributes = new AdditionalAttributes();
231 DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
232 additionalAttributes.setDateEntered(String.valueOf(dateFormat.format(new Date())));
233 additionalAttributes.setLastUpdated(String.valueOf(dateFormat.format(new Date())));
234 additionalAttributes.setSupressFromPublic("false");
235 additionalAttributes.setFastAddFlag("false");
236 additionalAttributes.setHarvestable("true");
237 additionalAttributes.setStatus("n");
238 additionalAttributes.setAttribute("dateEntered", additionalAttributes.getDateEntered());
239 additionalAttributes.setAttribute("lastUpdated", additionalAttributes.getLastUpdated());
240 additionalAttributes.setAttribute("fastAddFlag",additionalAttributes.getFastAddFlag());
241 additionalAttributes.setAttribute("supressFromPublic", additionalAttributes.getSupressFromPublic());
242 additionalAttributes.setAttribute("harvestable",additionalAttributes.getHarvestable());
243 additionalAttributes.setAttribute("status",additionalAttributes.getStatus());
244 extension.getContent().add(additionalAttributes);
245 List<Extension> extensionList = new ArrayList<Extension>();
246 extensionList.add(extension);
247 oleInstance.setExtension(extensionList);
248 List<OleInstance> oleInstanceList = new ArrayList<OleInstance>();
249 oleInstanceList.add(oleInstance);
250 instanceCollection.setInstanceCollection(oleInstanceList);
251
252 String instanceXML = workInstanceOlemlRecordProcessor.toXML(instanceCollection);
253 return instanceXML;
254 }
255
256 public OleHolding getOleHolding() {
257 return new OleHoldingsRecordHandler().getOleHoldings(attributes);
258 }
259
260 public OleItem getOleItem(BibliographicRecord bibliographicRecord) {
261 return new OleItemRecordHandler().getOleItem(bibliographicRecord, attributes);
262 }
263
264 private List<ProfileAttribute> buildListOfProfileAttributes(List<ProfileAttributeBo> profileAttributes) {
265 for (Iterator iterator = profileAttributes.iterator(); iterator.hasNext(); ) {
266 ProfileAttributeBo profileAttributeBo = (ProfileAttributeBo) iterator.next();
267 ProfileAttribute profileAttribute = new ProfileAttribute();
268 profileAttribute.setAgendaName(profileAttributeBo.getAgendaName());
269 profileAttribute.setAttributeName(profileAttributeBo.getAttributeName());
270 profileAttribute.setAttributeValue(profileAttributeBo.getAttributeValue());
271 profileAttribute.setSystemValue(profileAttributeBo.getSystemValue());
272 attributes.add(profileAttribute);
273 }
274 return attributes;
275 }
276
277 private String modifyXmlContent(String inputXmlContent) {
278 StringBuffer modifiedContent = new StringBuffer(inputXmlContent);
279 modifiedContent.replace(modifiedContent.indexOf("<oai_dc:dc"), modifiedContent.indexOf(">", modifiedContent.indexOf("<oai_dc:dc")),
280 "<oai_dc:dc xmlns:oai_dc=\"http://www.openarchives.org/OAI/2.0/oai_dc/\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\"");
281 return modifiedContent.toString();
282 }
283
284 public void rollbackData(String xmlForRollback) throws Exception {
285 String queryString = ROLLBACK_DATA_FROM_DOCSTORE + URLEncoder.encode(xmlForRollback);
286 String docstoreURL = PropertyUtil.getPropertyUtil().getProperty(DOCSTORE_URL);
287 postData(docstoreURL, queryString + queryString);
288 }
289
290
291 public String updateItem(BibliographicRecord bibliographicRecord, String itemUUID){
292 String responseXML = null;
293 try{
294 OleItem oleItem = getOleItem(bibliographicRecord);
295 OleItemRecordHandler oleItemRecordHandler = new OleItemRecordHandler();
296 String itemXML = oleItemRecordHandler.toXML(oleItem);
297 String requestXML = buildReuestDocXMLForUpdate(itemXML,itemUUID) ;
298 String queryString = UPDATE_EXISTING_DOCSTORE_RECORD_QUERY_STRING + URLEncoder.encode(requestXML);
299 String docstoreURL = PropertyUtil.getPropertyUtil().getProperty(DOCSTORE_URL);
300 responseXML = postData(docstoreURL, queryString);
301 }catch(Exception e){
302 e.printStackTrace();
303 }
304 return responseXML;
305 }
306 private String buildReuestDocXMLForUpdate(String itemXML,String itemUUID) {
307 Request requestObject = new Request();
308 RequestDocument requestDocument = new RequestDocument();
309 requestDocument.setId(itemUUID);
310 requestDocument.setCategory(OLEConstants.WORK_CATEGORY);
311 requestDocument.setType(OLEConstants.ITEM_DOC_TYPE);
312 requestDocument.setFormat(OLEConstants.OLEML_FORMAT);
313 requestDocument.setContent(new Content(itemXML));
314
315 ArrayList<RequestDocument> requestDocuments = new ArrayList<RequestDocument>();
316 requestDocuments.add(requestDocument);
317 requestObject.setRequestDocuments(requestDocuments);
318 RequestHandler requestHandler = new RequestHandler();
319 String xml = requestHandler.toXML(requestObject);
320 return xml;
321 }
322
323
324
325
326
327
328
329
330
331 public String instanceRecordCallToDocstore(String content, String uuid, String format) throws Exception {
332 String docstoreURL = PropertyUtil.getPropertyUtil().getProperty(DOCSTORE_URL);
333 String queryString = null;
334 String xmlContent = buildInstanceRequestDocXML(content, uuid, format);
335 queryString = CREATE_NEW_DOCSTORE_RECORD_QUERY_STRING + URLEncoder.encode(xmlContent);
336 return postData(docstoreURL, queryString);
337 }
338
339
340
341
342
343
344
345
346 private String buildInstanceRequestDocXML(String xmlContent, String uuid, String format) {
347 Request requestObject = new Request();
348 RequestDocument requestDocument = new RequestDocument();
349 if (null == uuid) {
350 requestDocument.setId("1");
351 requestObject.setOperation("ingest");
352 } else {
353 requestDocument.setId(uuid);
354 requestObject.setOperation("checkIn");
355 }
356 requestObject.setUser("editor");
357 requestDocument.setCategory(OLEConstants.WORK_CATEGORY);
358 requestDocument.setType(OLEConstants.INSTANCE_DOC_TYPE);
359 requestDocument.setFormat(OLEConstants.OLEML_FORMAT);
360
361 requestDocument.setContent(new Content(xmlContent));
362
363 ArrayList<RequestDocument> requestDocuments = new ArrayList<RequestDocument>();
364 requestDocuments.add(requestDocument);
365 requestObject.setRequestDocuments(requestDocuments);
366
367 RequestHandler requestHandler = new RequestHandler();
368 String requestXml = requestHandler.toXML(requestObject);
369 return requestXml;
370 }
371
372
373
374
375
376
377
378
379
380 public String updateInstanceRecord(String uuid, String docType, String xmlContent) throws Exception {
381 String docstoreURL = PropertyUtil.getPropertyUtil().getProperty(DOCSTORE_URL);
382 Request requestObject = new Request();
383 requestObject.setUser(GlobalVariables.getUserSession().getPrincipalId());
384 requestObject.setOperation(OLEConstants.CHECK_IN_OPERATION);
385 RequestDocument requestDocument = new RequestDocument();
386
387 requestDocument.setId(uuid);
388 requestDocument.setCategory(OLEConstants.WORK_CATEGORY);
389 requestDocument.setType(docType);
390 requestDocument.setFormat(OLEConstants.OLEML_FORMAT);
391 requestDocument.setContent(new Content(xmlContent));
392
393 ArrayList<RequestDocument> requestDocuments = new ArrayList<RequestDocument>();
394 requestDocuments.add(requestDocument);
395 requestObject.setRequestDocuments(requestDocuments);
396
397 RequestHandler requestHandler = new RequestHandler();
398 String xml = requestHandler.toXML(requestObject);
399
400 String queryString = UPDATE_EXISTING_DOCSTORE_RECORD_QUERY_STRING +
401 URLEncoder.encode(xml);
402
403 return postData(docstoreURL, queryString);
404 }
405
406
407
408
409
410
411
412
413
414 public String createItemForInstanceRecord (String instanceUuid, String docType, String xmlContent) throws Exception {
415 String docstoreURL = PropertyUtil.getPropertyUtil().getProperty(DOCSTORE_URL);
416 Request requestObject = new Request();
417 requestObject.setUser(GlobalVariables.getUserSession().getPrincipalId());
418 requestObject.setOperation(OLEConstants.CHECK_IN_OPERATION);
419 RequestDocument requestDocument = new RequestDocument();
420
421 requestDocument.setId(instanceUuid);
422 requestDocument.setCategory(OLEConstants.WORK_CATEGORY);
423 requestDocument.setType(OLEConstants.INSTANCE_DOC_TYPE);
424 requestDocument.setFormat(OLEConstants.OLEML_FORMAT);
425
426
427 RequestDocument linkedRequestDocument = new RequestDocument();
428 linkedRequestDocument.setId(OLEConstants.NEW_ITEM_ID);
429 linkedRequestDocument.setCategory(OLEConstants.WORK_CATEGORY);
430 linkedRequestDocument.setType(docType);
431 linkedRequestDocument.setFormat(OLEConstants.OLEML_FORMAT);
432 linkedRequestDocument.setContent(new Content(xmlContent));
433
434 List<RequestDocument> linkedRequestDocuments = new ArrayList<RequestDocument>();
435 linkedRequestDocuments.add(linkedRequestDocument);
436 requestDocument.setLinkedRequestDocuments(linkedRequestDocuments);
437
438
439 ArrayList<RequestDocument> requestDocuments = new ArrayList<RequestDocument>();
440 requestDocuments.add(requestDocument);
441 requestObject.setRequestDocuments(requestDocuments);
442
443 RequestHandler requestHandler = new RequestHandler();
444 String xml = requestHandler.toXML(requestObject);
445
446 String queryString = UPDATE_EXISTING_DOCSTORE_RECORD_QUERY_STRING +
447 URLEncoder.encode(xml);
448
449 return postData(docstoreURL, queryString);
450
451 }
452
453 }