1 package org.kuali.ole.docstore.discovery.service;
2
3 import com.thoughtworks.xstream.XStream;
4 import org.apache.commons.io.FileUtils;
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.BibliographicRecordHandler;
10 import org.kuali.ole.RepositoryManager;
11 import org.kuali.ole.docstore.discovery.bo.OleDiscoveryMarcExportProfile;
12 import org.kuali.ole.docstore.discovery.bo.OleDiscoveryMarcMappingField;
13 import org.kuali.ole.docstore.model.xmlpojo.work.bib.marc.DataField;
14 import org.kuali.ole.docstore.common.document.content.instance.InstanceCollection;
15 import org.kuali.ole.pojo.OleException;
16 import org.kuali.ole.pojo.bib.BibliographicRecord;
17 import org.kuali.ole.pojo.bib.Collection;
18 import org.kuali.ole.repository.CheckoutManager;
19 import org.marc4j.MarcXmlReader;
20 import org.marc4j.MarcXmlWriter;
21 import org.marc4j.marc.Record;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import javax.jcr.Node;
26 import javax.jcr.NodeIterator;
27 import javax.jcr.RepositoryException;
28 import java.io.ByteArrayInputStream;
29 import java.io.File;
30 import java.io.FileOutputStream;
31 import java.io.InputStream;
32 import java.util.ArrayList;
33 import java.util.Iterator;
34 import java.util.List;
35
36
37
38
39
40
41
42
43 public class OleDocstoreDumpService {
44 private static final Logger LOG = LoggerFactory.getLogger(OleDocstoreDumpService.class);
45
46 public String exportDocstoreData(String requestXML) throws Exception {
47 OleDiscoveryMarcExportProfile oleDiscMarcExpProfile = getMarcRequest(requestXML);
48 if (browseRepositoryContentForDataExport("work", "bibliographic", "marc", oleDiscMarcExpProfile)) {
49 return "Success";
50 } else {
51 return "Failed";
52 }
53 }
54
55 private boolean browseRepositoryContentForDataExport(String category, String type,
56 String format, OleDiscoveryMarcExportProfile oleDiscMarcExpProfile) throws RepositoryException, OleException {
57 try {
58 int fileCount = 0;
59 List<String> bibMarcRecordList = new ArrayList<String>();
60 List<String> recordList = new ArrayList<String>();
61 BibliographicRecordHandler bibliographicRecordHandler = new BibliographicRecordHandler();
62 RepositoryManager oleRepositoryManager = RepositoryManager.getRepositoryManager();
63 Node rootNode = oleRepositoryManager.getSession("repositoryBrowser", "getbibUuids").getRootNode();
64 Node categoryNode = rootNode.getNode(category);
65 Node typeNode = categoryNode.getNode(type);
66 Node formatNode = typeNode.getNode(format);
67 Iterator<Node> formatIterator = formatNode.getNodes();
68 while (formatIterator.hasNext()) {
69 Node levelNode = formatIterator.next();
70 Iterator<Node> levelNodeIterator = levelNode.getNodes();
71 while (levelNodeIterator.hasNext()) {
72 Node fileNode = levelNodeIterator.next();
73 if (fileNode.getName().equalsIgnoreCase("marcFile")) {
74 fileNode.getIdentifier();
75 String bibId = fileNode.getProperty("jcr:uuid").getValue().getString();
76 NodeIterator nodes = fileNode.getNodes();
77 while (nodes.hasNext()) {
78 Node contentNode = nodes.nextNode();
79 if (contentNode.hasProperty("jcr:data")) {
80 System.out.println("uuid: -----> " + contentNode.getIdentifier());
81 String bibContent = contentNode.getProperty("jcr:data").getString();
82 Collection collection = bibliographicRecordHandler.fromXML(bibContent);
83 BibliographicRecord bibliographicRecord = new BibliographicRecord();
84 bibliographicRecord = collection.getRecords().get(0);
85 bibliographicRecord = getInstanceDetails(bibId, bibliographicRecord, oleDiscMarcExpProfile);
86 String bibMarcRecord = bibliographicRecordHandler.generateXML(bibliographicRecord);
87 bibMarcRecordList.add(bibMarcRecord);
88 }
89 }
90 } else {
91 levelNodeIterator = fileNode.getNodes();
92 }
93 }
94 }
95 int totalBibMarcRecords = bibMarcRecordList.size();
96 int noOfRecords = oleDiscMarcExpProfile.getNoOfRecords();
97 int i = 0;
98 if (noOfRecords > 0) {
99 while (i < totalBibMarcRecords) {
100 List<String> records = new ArrayList<String>();
101 for (int j = 0; j < noOfRecords; j++) {
102 records.add(bibMarcRecordList.get(i));
103 if (i == totalBibMarcRecords - 1) {
104 i++;
105 break;
106 }
107 i++;
108 }
109 StringBuffer recordContent = new StringBuffer();
110 recordList.clear();
111 for (int k = 0; k < records.size(); k++) {
112 recordList.add(records.get(k));
113 recordContent.append(recordList.get(k) + "\n");
114 }
115 fileCount++;
116 String xmlVersion = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
117 if (oleDiscMarcExpProfile.getExportFormat().equalsIgnoreCase("MARC XML"))
118 writeFileToLocation(xmlVersion + recordContent.toString(), oleDiscMarcExpProfile.getExportTo(), fileCount);
119 else if (oleDiscMarcExpProfile.getExportFormat().equalsIgnoreCase("MARC21"))
120 generateMarcFromXml(xmlVersion + recordContent.toString(), oleDiscMarcExpProfile.getExportTo(), fileCount);
121 else
122 return false;
123 records.clear();
124 }
125 } else {
126 return false;
127 }
128 } catch (Exception e) {
129 LOG.error(e.getMessage(), e);
130 return false;
131 }
132 return true;
133 }
134
135 private OleDiscoveryMarcExportProfile getMarcRequest(String marcXml) {
136 XStream xstream = new XStream();
137 xstream.alias("request", OleDiscoveryMarcExportProfile.class);
138 xstream.alias("exportProfiles", OleDiscoveryMarcExportProfile.class);
139 xstream.alias("exportProfile", OleDiscoveryMarcExportProfile.class);
140 xstream.aliasField("exportMappingFields", OleDiscoveryMarcExportProfile.class, "oleDiscoveryMarcMappingFields");
141 xstream.alias("exportMappingField", OleDiscoveryMarcMappingField.class);
142
143 OleDiscoveryMarcExportProfile oleDiscMarcExpProfile = (OleDiscoveryMarcExportProfile) xstream.fromXML(marcXml);
144 return oleDiscMarcExpProfile;
145 }
146
147
148 public BibliographicRecord getInstanceDetails(String bibUUID, BibliographicRecord bibliographicRecord, OleDiscoveryMarcExportProfile oleDiscMarcExpProfile) {
149 try {
150 SolrQuery solrQuery = new SolrQuery();
151 solrQuery.setQuery("id:" + bibUUID);
152 solrQuery.setRows(500);
153 SolrServer server = SolrServerManager.getInstance().getSolrServer();
154 QueryResponse response = server.query(solrQuery);
155 List<SolrDocument> solrDocumentList = response.getResults();
156 for (SolrDocument solrDocument : solrDocumentList) {
157 if (solrDocument.getFieldValue("instanceIdentifier") instanceof List) {
158 List<String> instanceIdentifierList = (List<String>) solrDocument.getFieldValue("instanceIdentifier");
159 for (String instanceIdentifier : instanceIdentifierList) {
160 LOG.info("Instant Identifier Value is :" + instanceIdentifier);
161 CheckoutManager checkoutManager = new CheckoutManager();
162 String instanceOutput = checkoutManager.checkOut(instanceIdentifier, null, "checkout");
163 OleDocstoreDataRetrieveService oleDocstoreDataRetrieveService = new OleDocstoreDataRetrieveService();
164 InstanceCollection instanceCollection = oleDocstoreDataRetrieveService.getInstanceCollection(instanceOutput);
165 OleInstanceToMarcConvertor oleInstanceToMarcConvertor = new OleInstanceToMarcConvertor();
166 List<DataField> dataFields = bibliographicRecord.getDatafields();
167 List<DataField> instanceItemDataField = oleInstanceToMarcConvertor.generateDataField(instanceCollection, oleDiscMarcExpProfile);
168 dataFields.addAll(instanceItemDataField);
169 }
170 } else {
171 String instanceIdentifier = (String) solrDocument.getFieldValue("instanceIdentifier");
172 LOG.info("Instant Identifier Value is :" + instanceIdentifier);
173 CheckoutManager checkoutManager = new CheckoutManager();
174 String instanceOutput = checkoutManager.checkOut(instanceIdentifier, null, "checkout");
175 OleDocstoreDataRetrieveService oleDocstoreDataRetrieveService = new OleDocstoreDataRetrieveService();
176 InstanceCollection instanceCollection = oleDocstoreDataRetrieveService.getInstanceCollection(instanceOutput);
177 OleInstanceToMarcConvertor oleInstanceToMarcConvertor = new OleInstanceToMarcConvertor();
178 List<DataField> dataFields = bibliographicRecord.getDatafields();
179 List<DataField> instanceItemDataField = oleInstanceToMarcConvertor.generateDataField(instanceCollection, oleDiscMarcExpProfile);
180 dataFields.addAll(instanceItemDataField);
181 }
182 }
183
184 } catch (Exception e) {
185 LOG.error(e.getMessage(), e);
186 }
187 return bibliographicRecord;
188 }
189
190 private void writeFileToLocation(String bibContent, String location, int fileName) throws Exception {
191 LOG.info("Docstore dump location : " + location);
192 File dumpFile = new File(location + "/" + fileName + ".xml");
193 FileUtils.writeStringToFile(dumpFile, bibContent, "UTF-8");
194 }
195
196 private void generateMarcFromXml(String bibContent, String location, int fileName) throws Exception {
197 LOG.info("Docstore dump location : " + location);
198 InputStream input = new ByteArrayInputStream(bibContent.getBytes());
199 File file = new File(location + "/" + fileName + ".mrk");
200 if (!file.exists()) {
201 file.getParentFile().mkdirs();
202 file.createNewFile();
203 }
204 FileOutputStream fileOutputStream = new FileOutputStream(file);
205 MarcXmlReader marcXmlReader = new MarcXmlReader(input);
206 MarcXmlWriter marcXmlWriter = new MarcXmlWriter(fileOutputStream, true);
207
208 while (marcXmlReader.hasNext()) {
209 Record record = marcXmlReader.next();
210 marcXmlWriter.write(record);
211 }
212 marcXmlWriter.close();
213 }
214
215
216 }