1 package org.kuali.ole.select.gokb.util;
2
3 import org.apache.commons.lang.StringUtils;
4 import org.kuali.ole.GOKBConnectionReader;
5 import org.kuali.ole.OLEConstants;
6 import org.kuali.rice.core.api.config.property.ConfigContext;
7 import org.kuali.rice.core.api.exception.RiceRuntimeException;
8 import org.kuali.rice.core.api.util.xml.XmlHelper;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11 import org.w3c.dom.Document;
12 import org.w3c.dom.Node;
13 import org.w3c.dom.NodeList;
14
15 import javax.xml.xpath.XPath;
16 import javax.xml.xpath.XPathConstants;
17 import javax.xml.xpath.XPathFactory;
18 import java.io.ByteArrayInputStream;
19 import java.sql.Date;
20 import java.sql.Timestamp;
21 import java.text.DateFormat;
22 import java.text.SimpleDateFormat;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.TimeZone;
26
27
28
29
30 public class OleGokbXmlUtil {
31
32 private static final Logger LOG = LoggerFactory.getLogger(OleGokbXmlUtil.class);
33
34
35 private static String COMMON_URL=getGokbUrl();
36 private static final String PACKAGE_URL = "packages?verb=";
37 private static final String TITLE_URL = "titles?verb=";
38 private static final String PLATFORM_URL = "platforms?verb=";
39 private static final String ORGS_URL = "orgs?verb=";
40 private static final String LIST_RECORDS_URL = "listRecords&metadataPrefix=gokb&resumptionToken=||";
41 private static final String GET_IDENTIFIER_RECORDS_URL = "getRecord&metadataPrefix=gokb&identifier=";
42 private static final String LIST_IDENTIFIERS_URL = "ListIdentifiers&metadataPrefix=gokb&resumptionToken=||";
43 private static final String FROM_URL = "&from=";
44 private static final String PIPE = "|";
45 private static final String GOKB = "gokb";
46 private static final String COMPLETE_LIST_SIZE = "//resumptionToken/@completeListSize";
47 private static XPath xPath = XPathFactory.newInstance().newXPath();
48
49
50
51
52
53 public static String getPackageResponseXmlFromGokb(int endIndex) {
54
55 String url = COMMON_URL + PACKAGE_URL + LIST_RECORDS_URL + endIndex + PIPE + GOKB;
56 String responseXml = null;
57 try {
58 responseXml = GOKBConnectionReader.getInstance().doHttpUrlConnectionAction(url);
59 } catch (Exception e) {
60 LOG.error("Exception while getting package response from url", e);
61 }
62 return responseXml;
63 }
64
65
66
67
68
69
70 public static String getTitleResponseXmlFromGokb(int endIndex) {
71
72 String url = COMMON_URL + TITLE_URL + LIST_RECORDS_URL + endIndex + PIPE + GOKB;
73 String responseXml = null;
74 try {
75 responseXml = GOKBConnectionReader.getInstance().doHttpUrlConnectionAction(url);
76 } catch (Exception e) {
77 LOG.error("Exception while getting title response from url", e);
78 }
79 return responseXml;
80 }
81
82
83
84
85
86
87 public static String getPlatformResponseXmlFromGokb(int endIndex) {
88
89 String url = COMMON_URL + PLATFORM_URL + LIST_RECORDS_URL + endIndex + PIPE + GOKB;
90 String responseXml = null;
91 try {
92 responseXml = GOKBConnectionReader.getInstance().doHttpUrlConnectionAction(url);
93 } catch (Exception e) {
94 LOG.error("Exception while getting platform response from url", e);
95 }
96 return responseXml;
97 }
98
99
100
101
102
103
104 public static String getOrgsResponseXmlFromGokb(int endIndex) {
105
106 String url = COMMON_URL + ORGS_URL + LIST_RECORDS_URL + endIndex + PIPE + GOKB;
107 String responseXml = null;
108 try {
109 responseXml = GOKBConnectionReader.getInstance().doHttpUrlConnectionAction(url);
110 } catch (Exception e) {
111 LOG.error("Exception while getting orgs response from url", e);
112 }
113 return responseXml;
114 }
115
116
117
118
119
120
121
122 public static String getPackageResponseXmlFromGokb(String lastUpdatedTime, int endIndex) {
123
124 String url = COMMON_URL + PACKAGE_URL + LIST_IDENTIFIERS_URL + endIndex + FROM_URL + lastUpdatedTime;
125 String responseXml = null;
126 try {
127 responseXml = GOKBConnectionReader.getInstance().doHttpUrlConnectionAction(url);
128 } catch (Exception e) {
129 LOG.error("Exception while getting package response from url", e);
130 }
131 return responseXml;
132 }
133
134
135
136
137
138
139
140 public static String getPlatformResponseXmlFromGokb(String lastUpdatedTime, int endIndex) {
141
142 String url = COMMON_URL + PLATFORM_URL + LIST_IDENTIFIERS_URL + endIndex + FROM_URL + lastUpdatedTime;
143 String responseXml = null;
144 try {
145 responseXml = GOKBConnectionReader.getInstance().doHttpUrlConnectionAction(url);
146 } catch (Exception e) {
147 LOG.error("Exception while getting platform response from url", e);
148 }
149 return responseXml;
150 }
151
152
153
154
155
156
157
158 public static String getTitleResponseXmlFromGokb(String lastUpdatedTime, int endIndex) {
159
160 String url = COMMON_URL + TITLE_URL + LIST_IDENTIFIERS_URL + endIndex + FROM_URL + lastUpdatedTime;
161 String responseXml = null;
162 try {
163 responseXml = GOKBConnectionReader.getInstance().doHttpUrlConnectionAction(url);
164 } catch (Exception e) {
165 LOG.error("Exception while getting title response from url", e);
166 }
167 return responseXml;
168 }
169
170
171
172
173
174
175
176 public static String getOrgsResponseXmlFromGokb(String lastUpdatedTime, int endIndex) {
177
178 String url = COMMON_URL + ORGS_URL + LIST_IDENTIFIERS_URL + endIndex + FROM_URL + lastUpdatedTime;
179 String responseXml = null;
180 try {
181 responseXml = GOKBConnectionReader.getInstance().doHttpUrlConnectionAction(url);
182 } catch (Exception e) {
183 LOG.error("Exception while getting orgs response from url", e);
184 }
185 return responseXml;
186 }
187
188
189
190
191
192
193
194 public static String getPackageResponseXmlFromGokb(String identifier) {
195
196 String url = COMMON_URL + PACKAGE_URL + GET_IDENTIFIER_RECORDS_URL + identifier;
197 String responseXml = null;
198 try {
199 responseXml = GOKBConnectionReader.getInstance().doHttpUrlConnectionAction(url);
200 } catch (Exception e) {
201 LOG.error("Exception while getting package identifier response from url", e);
202 }
203 return responseXml;
204 }
205
206
207
208
209
210
211 public static String getPlatformResponseXmlFromGokb(String identifier) {
212
213 String url = COMMON_URL + PLATFORM_URL + GET_IDENTIFIER_RECORDS_URL + identifier;
214 String responseXml = null;
215 try {
216 responseXml = GOKBConnectionReader.getInstance().doHttpUrlConnectionAction(url);
217 } catch (Exception e) {
218 LOG.error("Exception while getting platform identifier response from url", e);
219 }
220 return responseXml;
221 }
222
223
224
225
226
227
228 public static String getTitleResponseXmlFromGokb(String identifier) {
229
230 String url = COMMON_URL + TITLE_URL + GET_IDENTIFIER_RECORDS_URL + identifier;
231 String responseXml = null;
232 try {
233 responseXml = GOKBConnectionReader.getInstance().doHttpUrlConnectionAction(url);
234 } catch (Exception e) {
235 LOG.error("Exception while getting title identifier response from url", e);
236 }
237 return responseXml;
238 }
239
240
241
242
243
244
245 public static String getOrgsResponseXmlFromGokb(String identifier) {
246
247 String url = COMMON_URL + ORGS_URL + GET_IDENTIFIER_RECORDS_URL + identifier;
248 String responseXml = null;
249 try {
250 responseXml = GOKBConnectionReader.getInstance().doHttpUrlConnectionAction(url);
251 } catch (Exception e) {
252 LOG.error("Exception while getting orgs identifier response from url", e);
253 }
254 return responseXml;
255 }
256
257
258
259
260
261
262
263 public static NodeList getElementNodeList(String docContent, String xpathExpression) {
264 try {
265 Document document = XmlHelper.trimXml(new ByteArrayInputStream(docContent.getBytes()));
266 NodeList nodeList = (NodeList) xPath.compile(xpathExpression).evaluate(document, XPathConstants.NODESET);
267 return nodeList;
268
269 } catch (Exception e) {
270 throw new RiceRuntimeException();
271 }
272 }
273
274
275
276
277
278
279
280
281 private static String getElementValue(String docContent, String xpathExpression) {
282 try {
283 Document document = XmlHelper.trimXml(new ByteArrayInputStream(docContent.getBytes()));
284 String value = (String) xPath.evaluate(xpathExpression, document, XPathConstants.STRING);
285 return value;
286 } catch (Exception e) {
287 throw new RiceRuntimeException();
288 }
289 }
290
291
292
293
294
295
296 public static int getPageSizeFromResponse(String responseXml) {
297 NodeList resumptionTokenNode = getElementNodeList(responseXml, OLEConstants.OleGokb.RESUMPTION_TOKEN_EXP);
298 if (null != resumptionTokenNode && null != resumptionTokenNode.item(0)) {
299 String resumptionTokenValue = StringUtils.substringAfter(resumptionTokenNode.item(0).getTextContent(), PIPE + PIPE);
300 if (resumptionTokenValue.contains(PIPE + GOKB)) {
301 return Integer.parseInt(StringUtils.substringBefore(resumptionTokenValue, PIPE + GOKB));
302 } else {
303 return Integer.parseInt(resumptionTokenValue);
304 }
305 }
306 return 0;
307 }
308
309
310
311
312
313
314 public static int getCompleteListSize(String results) {
315 String completeListSize = getElementValue(results, COMPLETE_LIST_SIZE);
316 if (StringUtils.isNotBlank(completeListSize)) {
317 return Integer.parseInt(completeListSize);
318 } else {
319 return 0;
320 }
321 }
322
323
324
325
326
327
328 public static List<String> getUpdatedDates(String results) {
329 List<String> updatedDates = new ArrayList<>();
330 NodeList records = OleGokbXmlUtil.getElementNodeList(results, OLEConstants.OleGokb.RECORD_XPATH_EXP);
331 for (int i = 0; i < records.getLength(); i++) {
332 NodeList nodeList = records.item(i).getChildNodes();
333 for (int j = 0; j < nodeList.getLength(); j++) {
334 if (nodeList.item(j).getNodeName().equalsIgnoreCase(OLEConstants.OleGokb.HEADER)) {
335 NodeList headerChildNodes = nodeList.item(j).getChildNodes();
336 for (int k = 0; k < headerChildNodes.getLength(); k++) {
337 if (headerChildNodes.item(k).getNodeName().equalsIgnoreCase(OLEConstants.OleGokb.DATE_STAMP)) {
338 updatedDates.add(headerChildNodes.item(k).getTextContent());
339 }
340 }
341 }
342 }
343 }
344 return updatedDates;
345 }
346
347
348
349
350
351 private static DateFormat getUTCDateFormat() {
352 DateFormat df = new SimpleDateFormat(OLEConstants.OleGokb.UTC_DATE_FORMAT);
353 df.setTimeZone(TimeZone.getTimeZone(OLEConstants.OleGokb.UTC_TIME_ZONE));
354 return df;
355 }
356
357
358
359
360
361
362 public static Timestamp getTimeStampFromString(String date) {
363 Timestamp timestamp = null;
364 try {
365 if (StringUtils.isNotBlank(date)) {
366 timestamp = new Timestamp(getUTCDateFormat().parse(date).getTime());
367 }
368 } catch (Exception e) {
369 LOG.error("Exception while parsing date" + e);
370 }
371 return timestamp;
372 }
373
374
375
376
377
378
379 public static String getStringFromTimeStamp(Timestamp timestamp) {
380 String utcTime = null;
381 try {
382 if (timestamp != null) {
383 Date date = new Date(timestamp.getTime());
384 utcTime = getUTCDateFormat().format(date);
385 }
386 } catch (Exception e) {
387 LOG.error("Exception while formatting date" + e);
388 }
389 return utcTime;
390 }
391
392
393
394
395
396
397 public static String getIdentifierFromHeader(Node headerNode) {
398 return headerNode.getChildNodes().item(0).getTextContent();
399 }
400
401 public static String getGokbUrl(){
402 if(ConfigContext.getCurrentContextConfig() != null){
403 return ConfigContext.getCurrentContextConfig().getProperty("gokb.url");
404 }else
405 return null;
406 }
407
408 public static void setGokbUrl(String url){
409 COMMON_URL=url;
410 }
411
412 }