1 package org.kuali.ole.docstore.process;
2
3 import org.apache.camel.Exchange;
4 import org.apache.camel.Processor;
5 import org.apache.camel.component.file.GenericFile;
6 import org.kuali.ole.docstore.model.enums.DocCategory;
7 import org.kuali.ole.docstore.model.enums.DocFormat;
8 import org.kuali.ole.docstore.model.enums.DocType;
9
10 import java.io.File;
11 import java.io.RandomAccessFile;
12 import java.nio.charset.Charset;
13 import java.text.SimpleDateFormat;
14 import java.util.Date;
15
16
17
18
19
20
21
22
23 public class BulkIngestDocumentReqProcessor
24 implements Processor {
25
26 private String user;
27 private String action;
28 private String category;
29 private String type;
30 private String format;
31 private String target;
32 private String preend = "";
33 private String postend = "";
34
35 public BulkIngestDocumentReqProcessor(String user, String action, String category, String type, String format,
36 String target) {
37 this.user = user;
38 this.action = action;
39 this.category = category;
40 this.type = type;
41 this.format = format;
42 this.target = target;
43 if (DocCategory.WORK.isEqualTo(category) && DocType.BIB.isEqualTo(type) && DocFormat.MARC.isEqualTo(format)) {
44 this.preend = "<collection xmlns=\"http://www.loc.gov/MARC21/slim\">\n\t";
45 this.postend = "\n</collection>\n";
46 } else if (DocCategory.WORK.isEqualTo(category) && DocType.BIB.isEqualTo(type) && DocFormat.DUBLIN_UNQUALIFIED
47 .isEqualTo(format)) {
48 this.preend =
49 "<OAI-PMH xmlns=\"http://www.openarchives.org/OAI/2.0/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
50 + "xsi:schemaLocation=\"http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd\">\n"
51 + "\t<responseDate>" + new Date() + "</responseDate>\n" + "\t<request verb=\"ListRecords\" from=\""
52 + new Date() + "\" metadataPrefix=\"oai_dc\" set=\"hathitrust\">"
53 + "http://quod.lib.umich.edu/cgi/o/oai/oai</request>\n\t<ListRecords>\n";
54 this.postend = "\n\t</ListRecords>\n" + "</OAI-PMH>\n";
55 }
56
57 }
58
59 public void process(Exchange exchange) throws Exception {
60 Integer ind = (Integer) exchange.getProperty("CamelSplitIndex");
61 RandomAccessFile target = null;
62 try {
63 File reqFile = new File(this.target + File.separator + new File(
64 ((GenericFile) exchange.getProperty("CamelFileExchangeFile")).getFileNameOnly()).getName()
65 + ".tmp");
66 if (!reqFile.exists()) {
67 reqFile.createNewFile();
68 }
69 target = new RandomAccessFile(reqFile, "rw");
70 target.seek(target.length());
71 if (ind.equals(0)) {
72 target.writeBytes(
73 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<request>\n" + "\t<user>" + user + "</user>\n"
74 + "\t<operation>" + action + "</operation>\n" + "\t<requestDocuments>\n" + "");
75 }
76 if (exchange.getIn().getBody() != null) {
77 String body = exchange.getIn().getBody().toString().trim();
78 if (body.length() != 0) {
79 target.write(("\t\t<ingestDocument id=\"" + (ind + 1)).getBytes());
80 target.write(("\" category=\"" + category + "\" type=\"" + type + "\" format=\"" + format
81 + "\" > \n\t\t\t<content>").getBytes());
82 target.write("<![CDATA[ \n".getBytes());
83 target.write(preend.getBytes(Charset.forName("UTF-8")));
84 target.write(body.getBytes(Charset.forName("UTF-8")));
85 target.write(postend.getBytes(Charset.forName("UTF-8")));
86 target.write("\n ]]> \n\t\t\t</content>\n\t\t\t<additionalAttributes />\n".getBytes());
87 target.write("\t\t</ingestDocument>\n".getBytes());
88 }
89 }
90 if (exchange.getProperty("CamelSplitComplete") != null && Boolean.TRUE
91 .equals(exchange.getProperty("CamelSplitComplete"))) {
92 target.writeBytes("\t</requestDocuments>\n" + "</request>\n");
93 target.close();
94
95 String newFileName = reqFile.getAbsolutePath()
96 .substring(0, reqFile.getAbsolutePath().lastIndexOf(".tmp"));
97 String fnSuffix = "-Req" + new SimpleDateFormat("(yyyy.MM.dd-HH.mm.ss)").format(new Date());
98 if (newFileName.endsWith(".xml")) {
99 newFileName = newFileName.substring(0, newFileName.lastIndexOf(".xml")) + fnSuffix;
100 } else {
101 newFileName = newFileName + fnSuffix;
102 }
103 newFileName = newFileName + ".xml";
104 reqFile.renameTo(new File(newFileName));
105
106
107 }
108 } catch (Exception e) {
109 throw new Exception(
110 "Unable to Process Record below from File : " + exchange.getProperty("CamelFileExchangeFile"), e);
111 } finally {
112 if (target != null) {
113 target.close();
114 }
115 }
116 }
117 }