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