View Javadoc

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   * Created with IntelliJ IDEA.
18   * User: ?
19   * Date: 2/7/13
20   * Time: 5:42 PM
21   * To change this template use File | Settings | File Templates.
22   */
23  public class OleDocstoreDataRetrieveService {
24      private static final Logger LOG = LoggerFactory.getLogger(OleDocstoreDataRetrieveService.class);
25  
26      /**
27       * @param bibUUIDs
28       * @param contentType
29       * @return the instance xml for the list of bibuuids
30       * @throws Exception
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       * @param instanceData
81       * @return the instance Collection object from the given xml content
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       * @param instanceCollection
94       * @return the json format of  the instance collection object
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      * @param instanceContent
108      * @return   the required xml format content.
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 }