1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.select.service.impl;
17
18 import org.kuali.ole.select.businessobject.BibInfoBean;
19 import org.kuali.ole.sys.OLEConstants;
20 import org.kuali.ole.sys.context.SpringContext;
21 import org.kuali.rice.core.api.config.property.ConfigurationService;
22 import org.w3c.dom.Document;
23 import org.w3c.dom.Element;
24
25 import javax.xml.parsers.DocumentBuilder;
26 import javax.xml.parsers.DocumentBuilderFactory;
27 import javax.xml.transform.Transformer;
28 import javax.xml.transform.TransformerFactory;
29 import javax.xml.transform.dom.DOMSource;
30 import javax.xml.transform.stream.StreamResult;
31 import java.io.File;
32 import java.util.UUID;
33
34 public class BibInfoBeanToBibXML {
35 private DocumentBuilderFactory docFactory;
36 private DocumentBuilder docBuilder;
37 private Document doc;
38 private Element rootElement;
39 private BibInfoBean bibInfoBean;
40 private String externalDirectory;
41
42 protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(BibInfoBeanToBibXML.class);
43
44 protected static ConfigurationService configurationService;
45
46 private void initiateDocument() throws Exception {
47 docFactory = DocumentBuilderFactory.newInstance();
48 docBuilder = docFactory.newDocumentBuilder();
49 }
50
51 private void createRootElement() throws Exception {
52 doc = docBuilder.newDocument();
53 rootElement = doc.createElement("bib");
54 doc.appendChild(rootElement);
55 }
56
57 private void creatChildElements() throws Exception {
58
59 Element bibData = doc.createElement("bibData");
60
61 Element titleID = doc.createElement("titleId");
62 titleID.appendChild(doc.createTextNode(bibInfoBean.getTitleId()));
63 bibData.appendChild(titleID);
64
65 Element title = doc.createElement("title");
66 title.appendChild(doc.createTextNode(bibInfoBean.getTitle() != null ? bibInfoBean.getTitle() : ""));
67 bibData.appendChild(title);
68
69 Element author = doc.createElement("author");
70 author.appendChild(doc.createTextNode(bibInfoBean.getAuthor() != null ? bibInfoBean.getAuthor() : ""));
71 bibData.appendChild(author);
72
73 Element edition = doc.createElement("edition");
74 edition.appendChild(doc.createTextNode(bibInfoBean.getEdition() != null ? bibInfoBean.getEdition() : ""));
75 bibData.appendChild(edition);
76
77 Element standardNumber = doc.createElement("standardNumber");
78 standardNumber.appendChild(doc.createTextNode(bibInfoBean.getStandardNumber() != null ? bibInfoBean.getStandardNumber() : ""));
79 bibData.appendChild(standardNumber);
80
81 Element publisher = doc.createElement("publisher");
82 publisher.appendChild(doc.createTextNode(bibInfoBean.getPublisher() != null ? bibInfoBean.getPublisher() : ""));
83 bibData.appendChild(publisher);
84
85 Element placeOfPublication = doc.createElement("placeOfPublication");
86 placeOfPublication.appendChild(doc.createTextNode(bibInfoBean.getPlaceOfPublication() != null ? bibInfoBean.getPlaceOfPublication() : ""));
87 bibData.appendChild(placeOfPublication);
88
89 Element yearOfPublication = doc.createElement("yearOfPublication");
90 yearOfPublication.appendChild(doc.createTextNode(bibInfoBean.getYearOfPublication() != null ? bibInfoBean.getYearOfPublication().toString() : ""));
91 bibData.appendChild(yearOfPublication);
92
93 Element physicalDescription = doc.createElement("physicalDescription");
94 physicalDescription.appendChild(doc.createTextNode(bibInfoBean.getPhysicalDescription() != null ? bibInfoBean.getPhysicalDescription().toString() : ""));
95 bibData.appendChild(physicalDescription);
96
97 Element format = doc.createElement("format");
98 format.appendChild(doc.createTextNode(bibInfoBean.getFormat() != null ? bibInfoBean.getFormat().toString() : ""));
99 bibData.appendChild(format);
100
101 Element series = doc.createElement("series");
102 series.appendChild(doc.createTextNode(bibInfoBean.getSeries() != null ? bibInfoBean.getSeries() : ""));
103 bibData.appendChild(series);
104
105 Element subjects = doc.createElement("subjects");
106 subjects.appendChild(doc.createTextNode(bibInfoBean.getSubjects() != null ? bibInfoBean.getSubjects() : ""));
107 bibData.appendChild(subjects);
108
109 Element price = doc.createElement("price");
110 price.appendChild(doc.createTextNode(bibInfoBean.getPrice() != null ? bibInfoBean.getPrice() : ""));
111 bibData.appendChild(price);
112
113 Element requestorContact = doc.createElement("requestorContact");
114 requestorContact.appendChild(doc.createTextNode(bibInfoBean.getRequestorContact() != null ? bibInfoBean.getRequestorContact() : ""));
115 bibData.appendChild(requestorContact);
116
117 Element requestersNotes = doc.createElement("requestersNotes");
118 requestersNotes.appendChild(doc.createTextNode(bibInfoBean.getRequestersNotes() != null ? bibInfoBean.getRequestersNotes() : ""));
119 bibData.appendChild(requestersNotes);
120
121 Element noOfCopies = doc.createElement("noOfCopies");
122 noOfCopies.appendChild(doc.createTextNode(bibInfoBean.getNoOfCopies() != null ? bibInfoBean.getNoOfCopies() : ""));
123 bibData.appendChild(noOfCopies);
124
125 Element category = doc.createElement("category");
126 category.appendChild(doc.createTextNode(bibInfoBean.getCategory() != null ? bibInfoBean.getCategory() : ""));
127 bibData.appendChild(category);
128
129 Element requestSource = doc.createElement("requestSource");
130 requestSource.appendChild(doc.createTextNode(bibInfoBean.getRequestSource() != null ? bibInfoBean.getRequestSource() : ""));
131 bibData.appendChild(requestSource);
132
133 Element selector = doc.createElement("selector");
134 selector.appendChild(doc.createTextNode(bibInfoBean.getSelector() != null ? bibInfoBean.getSelector() : ""));
135 bibData.appendChild(selector);
136
137 Element selectorNotes = doc.createElement("selectorNotes");
138 selectorNotes.appendChild(doc.createTextNode(bibInfoBean.getSelectorNotes() != null ? bibInfoBean.getSelectorNotes() : ""));
139 bibData.appendChild(selectorNotes);
140
141 Element startPage = doc.createElement("startPage");
142 startPage.appendChild(doc.createTextNode(bibInfoBean.getStartPage() != null ? bibInfoBean.getStartPage().toString() : ""));
143 bibData.appendChild(startPage);
144
145 Element endPage = doc.createElement("endPage");
146 endPage.appendChild(doc.createTextNode(bibInfoBean.getEndPage() != null ? bibInfoBean.getEndPage().toString() : ""));
147 bibData.appendChild(endPage);
148
149 doc.getDocumentElement().appendChild(bibData);
150 }
151
152 private void writeToXMLFile() throws Exception {
153 LOG.debug("before writing file...........................");
154 TransformerFactory transformerFactory = TransformerFactory.newInstance();
155 Transformer transformer = transformerFactory.newTransformer();
156 externalDirectory = getConfigurationService().getPropertyValueAsString(OLEConstants.STAGING_DIRECTORY_KEY);
157 String fileName = getConfigurationService().getPropertyValueAsString(OLEConstants.DOCSTORE_FILE_KEY);
158 String filePath = externalDirectory + fileName;
159 if (LOG.isInfoEnabled()) {
160 LOG.info("externalDirectory------------->" + externalDirectory);
161 LOG.info("filePath--------------->" + filePath);
162 }
163 File file = new File(filePath);
164 if (!file.exists()) {
165 LOG.debug("Creating bib xml file...............");
166 initiateDocument();
167 createRootElement();
168 creatChildElements();
169 } else {
170 LOG.debug("Appending bib xml file...............");
171 initiateDocument();
172 doc = docBuilder.parse(filePath);
173 creatChildElements();
174 }
175 StreamResult result = new StreamResult(file);
176
177
178 DOMSource source = new DOMSource(doc);
179 transformer.transform(source, result);
180 LOG.debug("after writing file...........................");
181 }
182
183 private void setBibInfoBean(BibInfoBean bibInfoBean) throws Exception {
184 this.bibInfoBean = bibInfoBean;
185 }
186
187 public void exportToXMLFile(BibInfoBean bibInfoBean) throws Exception {
188 setBibInfoBean(bibInfoBean);
189 generateTitleId();
190
191
192
193 writeToXMLFile();
194 }
195
196 private void generateTitleId() throws Exception {
197 UUID uniqueTitleId = UUID.randomUUID();
198 bibInfoBean.setTitleId(uniqueTitleId.toString());
199 }
200
201
202 protected ConfigurationService getConfigurationService() {
203 if (configurationService == null) {
204 configurationService = SpringContext.getBean(ConfigurationService.class);
205 }
206 return configurationService;
207 }
208 }