View Javadoc

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  //import org.kuali.ole.pojo.item.OleHolding;
16  //import org.kuali.ole.pojo.item.OleItem;
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   * DocstoreHelperService is the service class to access Docstore services
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&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, "UTF-8");
56          } else {
57              String xmlContent = buildReuestDocXML(content, uuid, format);
58              queryString = UPDATE_EXISTING_DOCSTORE_RECORD_QUERY_STRING + URLEncoder.encode(xmlContent, "UTF-8");
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(), "UTF-8"));
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     public String buildReuestDocXMLForIngest(String bibXML, String instanceXML) {
111         Request requestObject = new Request();
112         requestObject.setUser(GlobalVariables.getUserSession().getPrincipalName());
113         requestObject.setOperation(OLEConstants.INGEST_OPERATION);
114         RequestDocument requestDocument = new RequestDocument();
115         requestDocument.setId("1");
116         requestDocument.setCategory(OLEConstants.WORK_CATEGORY);
117         requestDocument.setType(OLEConstants.BIB_DOC_TYPE);
118         requestDocument.setFormat(OLEConstants.MARC_FORMAT);
119         requestDocument.setContent(new Content(bibXML));
120 
121         AdditionalAttributes additionalAttributes = new AdditionalAttributes();
122         DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
123         additionalAttributes.setDateEntered(String.valueOf(dateFormat.format(new Date())));
124         additionalAttributes.setLastUpdated(String.valueOf(dateFormat.format(new Date())));
125         additionalAttributes.setFastAddFlag("true");
126         additionalAttributes.setSupressFromPublic("false");
127         additionalAttributes.setAttribute("dateEntered",String.valueOf(dateFormat.format(new Date())));
128         additionalAttributes.setAttribute("lastUpdated",String.valueOf(dateFormat.format(new Date())));
129         additionalAttributes.setAttribute("fastAddFlag","true");
130         additionalAttributes.setAttribute("supressFromPublic","false");
131         requestDocument.setAdditionalAttributes(additionalAttributes);
132 
133         RequestDocument linkedRequestDocument = new RequestDocument();
134         linkedRequestDocument.setId("2");
135         linkedRequestDocument.setCategory(OLEConstants.WORK_CATEGORY);
136         linkedRequestDocument.setType(OLEConstants.INSTANCE_DOC_TYPE);
137         linkedRequestDocument.setContent(new Content(instanceXML));
138         linkedRequestDocument.setFormat(OLEConstants.OLEML_FORMAT);
139 
140         ArrayList<RequestDocument> linkedRequestDocuments = new ArrayList<RequestDocument>();
141         linkedRequestDocuments.add(linkedRequestDocument);
142 
143         requestDocument.setLinkedRequestDocuments(linkedRequestDocuments);
144 
145         ArrayList<RequestDocument> requestDocuments = new ArrayList<RequestDocument>();
146         requestDocuments.add(requestDocument);
147         requestObject.setRequestDocuments(requestDocuments);
148 
149         RequestHandler requestHandler = new RequestHandler();
150         String xml = requestHandler.toXML(requestObject);
151         return xml;
152     }
153 
154 
155     private String buildReuestDocXML(String xmlContent, String uuid, String format) {
156         String INSTANCE_MARC_XML_STRING = "<ole:instanceCollection xmlns:ole=\"http://ole.kuali.org/standards/ole-instance\">\n" +
157                 "    <!--1 or more repetitions:-->\n" +
158                 "    <ole:instance>\n" +
159                 "              <ole:oleHoldings >\n" +
160                 "        </ole:oleHoldings>\n" +
161                 "        <ole:sourceHoldings >\n" +
162                 "        </ole:sourceHoldings>\n" +
163                 "        <ole:items>\n" +
164                 "            <ole:item >\n" +
165                 "            </ole:item>\n" +
166                 "        </ole:items>\n" +
167                 "    </ole:instance>\n" +
168                 "</ole:instanceCollection>";
169 
170         Request requestObject = new Request();
171         RequestDocument requestDocument = new RequestDocument();
172 
173         if (null == uuid) {
174             requestDocument.setId("1");
175             RequestDocument linkedRequestDocument = new RequestDocument();
176             linkedRequestDocument.setId("2");
177             linkedRequestDocument.setCategory(OLEConstants.WORK_CATEGORY);
178             linkedRequestDocument.setType(OLEConstants.INSTANCE_DOC_TYPE);
179             linkedRequestDocument.setContent(new Content(INSTANCE_MARC_XML_STRING));
180             linkedRequestDocument.setFormat(OLEConstants.OLEML_FORMAT);
181 
182             ArrayList<RequestDocument> linkedRequestDocuments = new ArrayList<RequestDocument>();
183             linkedRequestDocuments.add(linkedRequestDocument);
184 
185             requestObject.setOperation(OLEConstants.INGEST_OPERATION);
186             requestObject.setUser(GlobalVariables.getUserSession().getPrincipalName());
187             requestDocument.setLinkedRequestDocuments(linkedRequestDocuments);
188         } else {
189             requestDocument.setId(uuid);
190             requestObject.setOperation(OLEConstants.CHECK_IN_OPERATION);
191             requestObject.setUser("editor");
192         }
193         requestDocument.setCategory(OLEConstants.WORK_CATEGORY);
194         requestDocument.setType(OLEConstants.BIB_DOC_TYPE);
195 
196         if (format.equalsIgnoreCase("marc")) {
197             requestDocument.setFormat(OLEConstants.MARC_FORMAT);
198         } else if (format.equalsIgnoreCase("dublinunq")) {
199             requestDocument.setFormat(OLEConstants.UNQUALIFIED_DUBLIN_FORMAT);
200         }
201 
202         requestDocument.setContent(new Content(xmlContent));
203 
204         if (format.equalsIgnoreCase("marc")) {
205             AdditionalAttributes additionalAttributes = new AdditionalAttributes();
206             DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
207             if (null == uuid) {
208                 additionalAttributes.setDateEntered(String.valueOf(dateFormat.format(new Date())));
209             }
210             additionalAttributes.setLastUpdated(String.valueOf(dateFormat.format(new Date())));
211             additionalAttributes.setFastAddFlag("true");
212             additionalAttributes.setSupressFromPublic("false");
213             requestDocument.setAdditionalAttributes(additionalAttributes);
214         }
215 
216         ArrayList<RequestDocument> requestDocuments = new ArrayList<RequestDocument>();
217         requestDocuments.add(requestDocument);
218         requestObject.setRequestDocuments(requestDocuments);
219 
220         RequestHandler requestHandler = new RequestHandler();
221         String xml = requestHandler.toXML(requestObject);
222 
223         if (format.equalsIgnoreCase(OLEConstants.UNQUALIFIED_DUBLIN_FORMAT)) {
224             xml = modifyXmlContent(xml);
225         }
226         return xml;
227     }
228 
229     public String getInstanceXML(BibliographicRecord bibliographicRecord) {
230         WorkInstanceOlemlRecordProcessor workInstanceOlemlRecordProcessor = new WorkInstanceOlemlRecordProcessor();
231         InstanceCollection instanceCollection = new InstanceCollection();
232         Instance oleInstance = new Instance();
233         List<Item> oleItemList  = new ArrayList<Item>();
234         oleItemList.add(getOleItem(bibliographicRecord)) ;
235         if(oleInstance.getItems() == null){
236             oleInstance.setItems(new Items());
237         }
238         oleInstance.getItems().setItem(oleItemList);
239         oleInstance.setOleHoldings(getOleHolding());
240 
241         Extension extension = new Extension();
242         AdditionalAttributes additionalAttributes = new AdditionalAttributes();
243         DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
244         additionalAttributes.setDateEntered(String.valueOf(dateFormat.format(new Date())));
245         additionalAttributes.setLastUpdated(String.valueOf(dateFormat.format(new Date())));
246         additionalAttributes.setSupressFromPublic("false");
247         additionalAttributes.setFastAddFlag("false");
248         additionalAttributes.setHarvestable("true");
249         additionalAttributes.setStatus("n"); // new Record
250         additionalAttributes.setAttribute("dateEntered", additionalAttributes.getDateEntered());
251         additionalAttributes.setAttribute("lastUpdated", additionalAttributes.getLastUpdated());
252         additionalAttributes.setAttribute("fastAddFlag",additionalAttributes.getFastAddFlag());
253         additionalAttributes.setAttribute("supressFromPublic", additionalAttributes.getSupressFromPublic());
254         additionalAttributes.setAttribute("harvestable",additionalAttributes.getHarvestable());
255         additionalAttributes.setAttribute("status",additionalAttributes.getStatus());
256         extension.getContent().add(additionalAttributes);
257         //List<Extension>   extensionList = new ArrayList<Extension>();
258         //extensionList.add(extension);
259         oleInstance.setExtension(extension);
260         List<Instance> oleInstanceList = new ArrayList<Instance>();
261         oleInstanceList.add(oleInstance);
262         instanceCollection.setInstance(oleInstanceList);
263         //String instanceXML = workInstanceOlemlRecordProcessor.generateXML(getOleHolding(), getOleItem(bibliographicRecord));
264         String instanceXML = workInstanceOlemlRecordProcessor.toXML(instanceCollection);
265         return instanceXML;
266     }
267 
268     public OleHoldings getOleHolding() {
269         return new OleHoldingsRecordHandler().getOleHoldings(attributes);
270     }
271 
272     public Item getOleItem(BibliographicRecord bibliographicRecord) {
273         return new OleItemRecordHandler().getOleItem(bibliographicRecord, attributes);
274     }
275 
276     private List<ProfileAttribute> buildListOfProfileAttributes(List<ProfileAttributeBo> profileAttributes) {
277         for (Iterator iterator = profileAttributes.iterator(); iterator.hasNext(); ) {
278             ProfileAttributeBo profileAttributeBo = (ProfileAttributeBo) iterator.next();
279             ProfileAttribute profileAttribute = new ProfileAttribute();
280             profileAttribute.setAgendaName(profileAttributeBo.getAgendaName());
281             profileAttribute.setAttributeName(profileAttributeBo.getAttributeName());
282             profileAttribute.setAttributeValue(profileAttributeBo.getAttributeValue());
283             profileAttribute.setSystemValue(profileAttributeBo.getSystemValue());
284             attributes.add(profileAttribute);
285         }
286         return attributes;
287     }
288 
289     private String modifyXmlContent(String inputXmlContent) {
290         StringBuffer modifiedContent = new StringBuffer(inputXmlContent);
291         modifiedContent.replace(modifiedContent.indexOf("<oai_dc:dc"), modifiedContent.indexOf(">", modifiedContent.indexOf("<oai_dc:dc")),
292                 "<oai_dc:dc xmlns:oai_dc=\"http://www.openarchives.org/OAI/2.0/oai_dc/\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\"");
293         return modifiedContent.toString();
294     }
295 
296     public void rollbackData(String xmlForRollback) throws Exception {
297         String queryString = ROLLBACK_DATA_FROM_DOCSTORE + URLEncoder.encode(xmlForRollback, "UTF-8");
298         String docstoreURL = PropertyUtil.getPropertyUtil().getProperty(DOCSTORE_URL);
299         postData(docstoreURL, queryString + queryString);
300     }
301 
302 
303     public String updateItem(BibliographicRecord bibliographicRecord, String itemUUID){
304         String responseXML = null;
305         try{
306             Item oleItem = getOleItem(bibliographicRecord);
307             OleItemRecordHandler oleItemRecordHandler = new OleItemRecordHandler();
308             String itemXML = oleItemRecordHandler.toXML(oleItem);
309             String requestXML = buildReuestDocXMLForUpdate(itemXML,itemUUID)  ;
310             String queryString = UPDATE_EXISTING_DOCSTORE_RECORD_QUERY_STRING + URLEncoder.encode(requestXML, "UTF-8");
311             String docstoreURL = PropertyUtil.getPropertyUtil().getProperty(DOCSTORE_URL);
312             responseXML = postData(docstoreURL, queryString);
313         }catch(Exception e){
314             e.printStackTrace();
315         }
316         return responseXML;
317     }
318     private String buildReuestDocXMLForUpdate(String itemXML,String itemUUID) {
319         Request requestObject = new Request();
320         RequestDocument requestDocument = new RequestDocument();
321         requestDocument.setId(itemUUID);
322         requestDocument.setCategory(OLEConstants.WORK_CATEGORY);
323         requestDocument.setType(OLEConstants.ITEM_DOC_TYPE);
324         requestDocument.setFormat(OLEConstants.OLEML_FORMAT);
325         requestDocument.setContent(new Content(itemXML));
326 
327         ArrayList<RequestDocument> requestDocuments = new ArrayList<RequestDocument>();
328         requestDocuments.add(requestDocument);
329         requestObject.setRequestDocuments(requestDocuments);
330         RequestHandler requestHandler = new RequestHandler();
331         String xml = requestHandler.toXML(requestObject);
332         return xml;
333     }
334 
335     /**
336      *   Method to generate Request XML and ingest Instance record to docstore
337      * @param content
338      * @param uuid
339      * @param format
340      * @return  Docstore response for Ingesting New Instance Record
341      * @throws Exception
342      */
343     public String instanceRecordCallToDocstore(String content, String uuid, String format) throws Exception {
344         String docstoreURL = PropertyUtil.getPropertyUtil().getProperty(DOCSTORE_URL);
345         String queryString = null;
346         String xmlContent = buildInstanceRequestDocXML(content, uuid, format);
347         queryString = CREATE_NEW_DOCSTORE_RECORD_QUERY_STRING + URLEncoder.encode(xmlContent, "UTF-8");
348         return postData(docstoreURL, queryString);
349     }
350 
351     /**
352      * Method to generate Request xml for Ingesting Instance record
353      * @param xmlContent
354      * @param uuid
355      * @param format
356      * @return   Request XML content
357      */
358     private String buildInstanceRequestDocXML(String xmlContent, String uuid, String format) {
359         Request requestObject = new Request();
360         RequestDocument requestDocument = new RequestDocument();
361         if (null == uuid) {
362             requestDocument.setId("1");
363             requestObject.setOperation(OLEConstants.INGEST_OPERATION);
364         } else {
365             requestDocument.setId(uuid);
366             requestObject.setOperation(OLEConstants.CHECK_IN_OPERATION);
367         }
368         requestObject.setUser("editor");
369         requestDocument.setCategory(OLEConstants.WORK_CATEGORY);
370         requestDocument.setType(OLEConstants.INSTANCE_DOC_TYPE);
371         requestDocument.setFormat(OLEConstants.OLEML_FORMAT);
372 
373         requestDocument.setContent(new Content(xmlContent));
374 
375         ArrayList<RequestDocument> requestDocuments = new ArrayList<RequestDocument>();
376         requestDocuments.add(requestDocument);
377         requestObject.setRequestDocuments(requestDocuments);
378 
379         RequestHandler requestHandler = new RequestHandler();
380         String requestXml = requestHandler.toXML(requestObject);
381         return requestXml;
382     }
383 
384     /**
385      * Method to update Holding or Item record of an Instance record
386      * @param uuid
387      * @param docType
388      * @param xmlContent
389      * @return  Docstore XML response with success/failure status
390      * @throws Exception
391      */
392     public String updateInstanceRecord(String uuid, String docType, String xmlContent) throws Exception {
393         String docstoreURL = PropertyUtil.getPropertyUtil().getProperty(DOCSTORE_URL);
394         Request requestObject = new Request();
395         requestObject.setUser(GlobalVariables.getUserSession().getPrincipalId());
396         requestObject.setOperation(OLEConstants.CHECK_IN_OPERATION);
397         RequestDocument requestDocument = new RequestDocument();
398 
399         requestDocument.setId(uuid);
400         requestDocument.setCategory(OLEConstants.WORK_CATEGORY);
401         requestDocument.setType(docType);  // docType should be either holdings or item
402         requestDocument.setFormat(OLEConstants.OLEML_FORMAT);
403         requestDocument.setContent(new Content(xmlContent));
404 
405         ArrayList<RequestDocument> requestDocuments = new ArrayList<RequestDocument>();
406         requestDocuments.add(requestDocument);
407         requestObject.setRequestDocuments(requestDocuments);
408 
409         RequestHandler requestHandler = new RequestHandler();
410         String xml = requestHandler.toXML(requestObject);
411 
412         String queryString = UPDATE_EXISTING_DOCSTORE_RECORD_QUERY_STRING + URLEncoder.encode(xml, "UTF-8");
413 
414         return postData(docstoreURL, queryString);
415     }
416 
417     /**
418      * This method used to delete a instance record
419      * @param instance
420      * @return
421      * @throws Exception
422      */
423 
424     public String deleteInstanceRecord(Instance instance) throws Exception {
425         String docstoreURL = PropertyUtil.getPropertyUtil().getProperty(DOCSTORE_URL);
426         //  Request deleteRequest = buildDeleteRequest(instance);
427         String uuid = instance.getInstanceIdentifier();
428         String queryString = "docAction=delete&requestContent=" + uuid;
429         String response = postData(docstoreURL,queryString);
430         return response;
431 
432     }
433 
434     /**
435      * Method to add NEW ITEM for existing Instance record
436      * @param instanceUuid
437      * @param docType
438      * @param xmlContent
439      * @return  Docstore XML response with success/failure status
440      * @throws Exception
441      */
442     public String createItemForInstanceRecord (String instanceUuid, String docType, String xmlContent) throws Exception {
443         String docstoreURL = PropertyUtil.getPropertyUtil().getProperty(DOCSTORE_URL);
444         Request requestObject = new Request();
445         requestObject.setUser(GlobalVariables.getUserSession().getPrincipalId());
446         requestObject.setOperation(OLEConstants.CHECK_IN_OPERATION);
447         RequestDocument requestDocument = new RequestDocument();
448 
449         requestDocument.setId(instanceUuid);
450         requestDocument.setCategory(OLEConstants.WORK_CATEGORY);
451         requestDocument.setType(OLEConstants.INSTANCE_DOC_TYPE);
452         requestDocument.setFormat(OLEConstants.OLEML_FORMAT);
453 
454 
455         RequestDocument linkedRequestDocument = new RequestDocument();
456         linkedRequestDocument.setId(OLEConstants.NEW_ITEM_ID);
457         linkedRequestDocument.setCategory(OLEConstants.WORK_CATEGORY);
458         linkedRequestDocument.setType(docType);
459         linkedRequestDocument.setFormat(OLEConstants.OLEML_FORMAT);
460         linkedRequestDocument.setContent(new Content(xmlContent));
461 
462         List<RequestDocument> linkedRequestDocuments = new ArrayList<RequestDocument>();
463         linkedRequestDocuments.add(linkedRequestDocument);
464         requestDocument.setLinkedRequestDocuments(linkedRequestDocuments);
465 
466 
467         ArrayList<RequestDocument> requestDocuments = new ArrayList<RequestDocument>();
468         requestDocuments.add(requestDocument);
469         requestObject.setRequestDocuments(requestDocuments);
470 
471         RequestHandler requestHandler = new RequestHandler();
472         String xml = requestHandler.toXML(requestObject);
473 
474         String queryString = UPDATE_EXISTING_DOCSTORE_RECORD_QUERY_STRING + URLEncoder.encode(xml, "UTF-8");
475 
476         return postData(docstoreURL, queryString);
477 
478     }
479 
480     public String deleteItemrecord(String uuid) throws Exception {
481         String docstoreURL = PropertyUtil.getPropertyUtil().getProperty(DOCSTORE_URL);
482         String queryString = "docAction=delete&requestContent=" + uuid;
483         String response = postData(docstoreURL,queryString);
484         return response;
485 
486     }
487 
488 }