1
|
/*
|
2
|
* ClientViewHelper.java
|
3
|
*
|
4
|
* Created on June 25, 2007, 9:57 AM
|
5
|
*
|
6
|
* To change this template, choose Tools | Template Manager
|
7
|
* and open the template in the editor.
|
8
|
*/
|
9
|
|
10
|
package edu.ucsb.nceas.metacat.clientview;
|
11
|
|
12
|
import com.oreilly.servlet.multipart.FilePart;
|
13
|
import com.oreilly.servlet.multipart.MultipartParser;
|
14
|
import com.oreilly.servlet.multipart.ParamPart;
|
15
|
import com.oreilly.servlet.multipart.Part;
|
16
|
import edu.ucsb.nceas.metacat.MetaCatUtil;
|
17
|
import edu.ucsb.nceas.metacat.client.MetacatClient;
|
18
|
import edu.ucsb.nceas.metacat.client.MetacatFactory;
|
19
|
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
|
20
|
import edu.ucsb.nceas.utilities.XMLUtilities;
|
21
|
import java.io.BufferedReader;
|
22
|
import java.io.IOException;
|
23
|
import java.io.InputStream;
|
24
|
import java.io.InputStreamReader;
|
25
|
import java.io.Reader;
|
26
|
import java.io.StringReader;
|
27
|
import java.util.HashMap;
|
28
|
import java.util.Iterator;
|
29
|
import java.util.Properties;
|
30
|
import java.util.TreeMap;
|
31
|
import java.util.Vector;
|
32
|
import javax.servlet.http.HttpServletRequest;
|
33
|
import javax.servlet.http.HttpServletResponse;
|
34
|
import javax.servlet.http.HttpSession;
|
35
|
import javax.xml.xpath.XPath;
|
36
|
import javax.xml.xpath.XPathConstants;
|
37
|
import javax.xml.xpath.XPathExpressionException;
|
38
|
import javax.xml.xpath.XPathFactory;
|
39
|
import org.w3c.dom.Document;
|
40
|
import org.w3c.dom.Node;
|
41
|
import org.w3c.dom.NodeList;
|
42
|
|
43
|
/**
|
44
|
*
|
45
|
* @author barteau
|
46
|
*/
|
47
|
public class ClientViewHelper {
|
48
|
private XPath xpath = XPathFactory.newInstance().newXPath();
|
49
|
private HttpSession clientSession;
|
50
|
private ClientView clientViewBean = null;
|
51
|
private MetacatClient metacatClient = null;
|
52
|
private boolean loggedIn = false;
|
53
|
private Document metadataDoc = null;
|
54
|
private int sizeLimit;
|
55
|
private String contactName = "";
|
56
|
|
57
|
private static final String LDAP_TEMPLATE = "uid=%1s,o=%2s,dc=ecoinformatics,dc=org";
|
58
|
|
59
|
/**
|
60
|
* Creates a new instance of ClientViewHelper
|
61
|
*/
|
62
|
public ClientViewHelper(HttpServletRequest request) throws MetacatInaccessibleException {
|
63
|
String metacatPath = "http://%1s%2s/metacat";
|
64
|
String host, context, button, tmp;
|
65
|
|
66
|
clientSession = request.getSession();
|
67
|
|
68
|
host = request.getHeader("host");
|
69
|
context = request.getContextPath();
|
70
|
//metacatPath = String.format(metacatPath, host, context);
|
71
|
tmp = metacatPath.replaceFirst("%1s", host);
|
72
|
metacatPath = tmp.replaceFirst("%2s", context);
|
73
|
metacatClient = (MetacatClient) MetacatFactory.createMetacatConnection(metacatPath);
|
74
|
sizeLimit = (new Integer(MetaCatUtil.getOption("datafilesizelimit"))).intValue();
|
75
|
}
|
76
|
|
77
|
public String clientRequest(HttpServletRequest request, HttpServletResponse response) {
|
78
|
String result = null, action, message, serverResponse;
|
79
|
String button, contentType, posted_ldapUserName, tmp;
|
80
|
MultipartParser multipartParser;
|
81
|
|
82
|
if (clientViewBean == null)
|
83
|
clientViewBean = (ClientView) clientSession.getAttribute(ClientView.CLIENT_VIEW_BEAN);
|
84
|
|
85
|
if (clientViewBean != null) {
|
86
|
action = clientViewBean.getAction();
|
87
|
contentType = request.getContentType();
|
88
|
if (contentType != null && contentType.contains("multipart/form-data"))
|
89
|
action = "Upload";
|
90
|
try {
|
91
|
if (action.equals("Login")) {
|
92
|
//posted_ldapUserName = String.format(LDAP_TEMPLATE, clientViewBean.getUsername(), clientViewBean.getOrganization());
|
93
|
tmp = LDAP_TEMPLATE.replaceFirst("%1s", clientViewBean.getUsername());
|
94
|
posted_ldapUserName = tmp.replaceFirst("%2s", clientViewBean.getOrganization());
|
95
|
|
96
|
serverResponse = metacatClient.login(posted_ldapUserName, clientViewBean.getPassword());
|
97
|
setLoggedIn(serverResponse);
|
98
|
message = parseXml("message", serverResponse);
|
99
|
contactName = parseXml("name", serverResponse);
|
100
|
clientViewBean.setMessage(ClientView.LOGIN_MESSAGE, message);
|
101
|
clientViewBean.setMessage(ClientView.UPLOAD_MESSAGE, "");
|
102
|
if (isLoggedIn()) {
|
103
|
clientViewBean.setSessionid(metacatClient.getSessionId());
|
104
|
}
|
105
|
} else if (action.equals("Logout")) {
|
106
|
message = metacatClient.logout();
|
107
|
setLoggedIn(message);
|
108
|
message = parseXml("message", message);
|
109
|
clientViewBean.setMessage(ClientView.LOGIN_MESSAGE, message);
|
110
|
clientViewBean.setMessage(ClientView.UPLOAD_MESSAGE, "");
|
111
|
if (!isLoggedIn()) {
|
112
|
clientViewBean.setUsername("");
|
113
|
clientViewBean.setPassword("");
|
114
|
clientViewBean.setOrganization("");
|
115
|
}
|
116
|
|
117
|
} else if (action.equals("Delete")) {
|
118
|
ClientFgdcHelper.clientDeleteRequest(clientViewBean, this);
|
119
|
//*** Refresh the select list.
|
120
|
message = handleDocIdSelect();
|
121
|
clientViewBean.setMessage(ClientView.SELECT_MESSAGE, message);
|
122
|
|
123
|
} else if (action.equals("Replace")) {
|
124
|
message = "Not implemented, come back another day";
|
125
|
clientViewBean.setMessage(ClientView.REPLACE_MESSAGE, message);
|
126
|
|
127
|
} else if (action.equals("Upload")) {
|
128
|
message = "";
|
129
|
//*** Only process request if a file upload.
|
130
|
if (isLoggedIn()) {
|
131
|
//*** Init the MultipartParser.
|
132
|
multipartParser = new MultipartParser(request, sizeLimit * 1024 * 1024);
|
133
|
message = handlePackageUpload(clientViewBean, multipartParser);
|
134
|
}
|
135
|
clientViewBean.setMessage(ClientView.UPLOAD_MESSAGE, message);
|
136
|
|
137
|
} else if (action.equals("Update")) {
|
138
|
System.out.println("ClientViewHelper.clientRequest: Update");
|
139
|
} else if (action.equals("Scope")) {
|
140
|
message = handleDocIdSelect();
|
141
|
clientViewBean.setMessage(ClientView.SELECT_MESSAGE, message);
|
142
|
}
|
143
|
//*** Now that the action has been processed, clear it.
|
144
|
clientViewBean.setAction("");
|
145
|
|
146
|
} catch (Exception ex) {
|
147
|
message = ex.getMessage();
|
148
|
clientViewBean.setMessage(ClientView.ERROR_MESSAGE, message);
|
149
|
ex.printStackTrace();
|
150
|
}
|
151
|
} else {
|
152
|
System.out.println("ClientViewHelper.clientRequest: ClientView bean is not instantiated.");
|
153
|
}
|
154
|
return(result);
|
155
|
}
|
156
|
|
157
|
/**
|
158
|
* This is a convenience method to reduce the amount of code in a Metacat Client.
|
159
|
* It handles creating/reusing (per session) an instance of a ClientViewHelper.
|
160
|
* @param request Since this is intended to be used by an Http client, it is passed the
|
161
|
* available "request" variable (the HttpServletRequest).
|
162
|
* @throws edu.ucsb.nceas.metacat.client.MetacatInaccessibleException Received by MetacatFactory.
|
163
|
* @return ClientViewHelper instance.
|
164
|
*/
|
165
|
public static ClientViewHelper clientViewHelperInstance(HttpServletRequest request) {
|
166
|
ClientViewHelper result;
|
167
|
|
168
|
result = (ClientViewHelper) request.getSession().getAttribute("ClientViewHelper");
|
169
|
if (result == null) {
|
170
|
try {
|
171
|
result = new ClientViewHelper(request);
|
172
|
request.getSession().setAttribute("ClientViewHelper", result);
|
173
|
} catch (MetacatInaccessibleException ex) {
|
174
|
ex.printStackTrace();
|
175
|
}
|
176
|
}
|
177
|
|
178
|
return(result);
|
179
|
}
|
180
|
|
181
|
/**
|
182
|
* A convenience method to be used by client code that requires
|
183
|
* the user to be logged in. NOTE: setUser() must have been called first,
|
184
|
* otherwise it will always return false.
|
185
|
* @return boolean true if user has logged in for this session, false otherwise.
|
186
|
*/
|
187
|
public boolean isLoggedIn() {
|
188
|
return(loggedIn);
|
189
|
}
|
190
|
|
191
|
/**
|
192
|
* After calling "login(ldapUserName, pwd)", call this with the username
|
193
|
* and servers response message. You can than use isLoggedIn() to determine if
|
194
|
* the user is logged in, getLoginResponseElement(), etc. The user name will also
|
195
|
* used by calls to doMetadataUpload() for Document Id creation (scope).
|
196
|
* @param userName User name
|
197
|
* @param serverResponse XML login response sent from Metacat.
|
198
|
*/
|
199
|
public void setLoggedIn(String serverResponse) {
|
200
|
loggedIn = (serverResponse != null && serverResponse.contains("login"));
|
201
|
}
|
202
|
|
203
|
public String parseXml(String elementName, String xml) {
|
204
|
String result = null;
|
205
|
Document doc;
|
206
|
|
207
|
try {
|
208
|
doc = XMLUtilities.getXMLReaderAsDOMDocument(new StringReader(xml));
|
209
|
result = (String) xpath.evaluate(elementName, doc.getDocumentElement(), XPathConstants.STRING);
|
210
|
if (result != null)
|
211
|
result = result.trim();
|
212
|
} catch (IOException ex) {
|
213
|
ex.printStackTrace();
|
214
|
} catch (XPathExpressionException ex) {
|
215
|
ex.printStackTrace();
|
216
|
}
|
217
|
return(result);
|
218
|
}
|
219
|
|
220
|
public String handleDocIdSelect() {
|
221
|
String result = "";
|
222
|
TreeMap allDocIds;
|
223
|
|
224
|
if (!clientViewBean.getPathValue().equals("")) {
|
225
|
allDocIds = getSelectQueryMap();
|
226
|
result = ClientHtmlHelper.mapToHtmlSelect(allDocIds, "docId", "width: 240", 10);
|
227
|
}
|
228
|
return(result);
|
229
|
}
|
230
|
|
231
|
/**
|
232
|
* Handles metadata file and data file uploads for inserting new
|
233
|
* Metacat data packages. Note: if content type is not "multipart/form-data",
|
234
|
* nothing will happen.
|
235
|
* @param request HTTP request.
|
236
|
* @return A 1-line status message for the user.
|
237
|
*/
|
238
|
public String handlePackageUpload(ClientView clientViewBean, MultipartParser multipartParser) throws Exception {
|
239
|
String result = "", contentType, formatType;
|
240
|
String lastDocId, nextDocId, metaDocId, metaFileName;
|
241
|
String fileInfo[];
|
242
|
Reader reader;
|
243
|
int sizeLimit, idx;
|
244
|
InputStream inputStream;
|
245
|
HashMap paramsMap, dataDocIDs;
|
246
|
StringBuilder fileName;
|
247
|
boolean sendIt;
|
248
|
|
249
|
//*** Get the First file, which should be the metadata file.
|
250
|
paramsMap = new HashMap();
|
251
|
fileName = new StringBuilder();
|
252
|
inputStream = getNextInputStream(multipartParser, fileName, paramsMap);
|
253
|
metaFileName = fileName.toString();
|
254
|
if (metaFileName.toLowerCase().endsWith(".xml")) {
|
255
|
//*** Keep it here for updating.
|
256
|
setMetadataDoc(inputStream);
|
257
|
//*** Get the Metadata File's DOC ID.
|
258
|
lastDocId = getMetacatClient().getLastDocid(clientViewBean.getUsername());
|
259
|
metaDocId = lastDocId = nextDocId(lastDocId, clientViewBean.getUsername());
|
260
|
|
261
|
//*** Loop thru all of the data files, get fileName and inputStream.
|
262
|
dataDocIDs = new HashMap();
|
263
|
fileName = new StringBuilder();
|
264
|
while ((inputStream = getNextInputStream(multipartParser, fileName, paramsMap)) != null) {
|
265
|
//*** Get the data file's DOC ID.
|
266
|
nextDocId = nextDocId(lastDocId, clientViewBean.getUsername());
|
267
|
|
268
|
//*** Set the file format (just using file extension for now).
|
269
|
idx = fileName.lastIndexOf(".");
|
270
|
if (idx > 1)
|
271
|
formatType = fileName.substring(idx+1).toUpperCase();
|
272
|
else
|
273
|
formatType = "";
|
274
|
|
275
|
fileInfo = new String[2];
|
276
|
fileInfo[ClientView.FORMAT_TYPE] = formatType;
|
277
|
fileInfo[ClientView.FILE_NAME] = fileName.toString();
|
278
|
|
279
|
dataDocIDs.put(nextDocId, fileInfo);
|
280
|
//*** Upload the data file to metacat.
|
281
|
getMetacatClient().upload(nextDocId, fileName.toString(), inputStream, Integer.MAX_VALUE);
|
282
|
|
283
|
lastDocId = nextDocId;
|
284
|
fileName = new StringBuilder();
|
285
|
}
|
286
|
|
287
|
if (ClientFgdcHelper.isFGDC(getMetadataDoc())) {
|
288
|
sendIt = ClientFgdcHelper.handlePackageUpload(metaDocId, dataDocIDs, contactName, metaFileName, getMetadataDoc());
|
289
|
} else {
|
290
|
//TODO add other types of metadata grammars here...
|
291
|
System.out.println("ClientViewHelper.handlePackageUpload: not an FGDC file = " + fileName);
|
292
|
result = fileName + " is not an FGDC file. Files not uploaded.";
|
293
|
sendIt = false;
|
294
|
}
|
295
|
|
296
|
if (sendIt) {
|
297
|
//*** Upload the metadata file to metacat.
|
298
|
reader = XMLUtilities.getDOMTreeAsReader(metadataDoc.getDocumentElement(), false);
|
299
|
getMetacatClient().insert(metaDocId, reader, null);
|
300
|
|
301
|
result = "MetaCat Package Inserted: the Document Identifier is " + metaDocId;
|
302
|
reader.close();
|
303
|
|
304
|
//*** One last detail...grant the public read access.
|
305
|
if (paramsMap.containsKey("publicAccess"))
|
306
|
getMetacatClient().setAccess(metaDocId, "public", "read", "allow", "allowFirst");
|
307
|
}
|
308
|
|
309
|
} else {
|
310
|
result = "The first file must be an XML Metadata file. Files not uploaded.";
|
311
|
}
|
312
|
if (inputStream != null)
|
313
|
inputStream.close();
|
314
|
return(result);
|
315
|
}
|
316
|
|
317
|
public String handleFileUpdate(MultipartParser multipartParser) throws Exception {
|
318
|
Reader reader;
|
319
|
String result = "", fNm, action, lastDocId, newDocId, xPathQuery;
|
320
|
InputStream inputStream;
|
321
|
HashMap paramsMap;
|
322
|
StringBuilder fileName;
|
323
|
Iterator iterIt;
|
324
|
boolean sendIt;
|
325
|
|
326
|
paramsMap = new HashMap();
|
327
|
fileName = new StringBuilder();
|
328
|
if ((inputStream = getNextInputStream(multipartParser, fileName, paramsMap)) != null) {
|
329
|
action = (String) paramsMap.get("action");
|
330
|
lastDocId = (String) paramsMap.get("docid");
|
331
|
fNm = fileName.toString();
|
332
|
try {
|
333
|
if (fNm.toLowerCase().endsWith(".xml") && action.toLowerCase().contains("metadata")) {
|
334
|
//*** Keep it here for updating.
|
335
|
setMetadataDoc(inputStream);
|
336
|
if (ClientFgdcHelper.isFGDC(getMetadataDoc())) {
|
337
|
if (!ClientFgdcHelper.hasMetacatInfo(lastDocId, getMetadataDoc())) {
|
338
|
result = "Update not performed: the Metadata file has no prior Metacat info in it.";
|
339
|
} else {
|
340
|
//xPathQuery = String.format(ClientFgdcHelper.XPATH_QUERY_TEMPLATE, lastDocId);
|
341
|
xPathQuery = ClientFgdcHelper.XPATH_QUERY_TEMPLATE.replaceFirst("%1s", lastDocId);
|
342
|
//*** Update the metadata with the new Doc Id version.
|
343
|
newDocId = nextVersion(lastDocId, xPathQuery);
|
344
|
//*** Upload the metadata file to metacat.
|
345
|
reader = XMLUtilities.getDOMTreeAsReader(getMetadataDoc().getDocumentElement(), false);
|
346
|
getMetacatClient().update(newDocId, reader, null);
|
347
|
reader.close();
|
348
|
result = "MetaCat Package Updated: the Document Identifier is " + newDocId;
|
349
|
}
|
350
|
} else {
|
351
|
//***TODO This is EML.
|
352
|
result = "Currently this functionality only supports FGDC metadata.";
|
353
|
}
|
354
|
} else {
|
355
|
//*** This is a data file.
|
356
|
result = "This is a data file. action: " + action + " lastDocId: " + lastDocId + " fNm: " + fNm;
|
357
|
|
358
|
//newDocId = "TODO";
|
359
|
//reader = null; //TODO
|
360
|
//getMetacatClient().insert(newDocId, reader, null);
|
361
|
//reader.close();
|
362
|
}
|
363
|
|
364
|
|
365
|
//*** Pass on the public read access. TODO
|
366
|
// if(hasPublicAccess)
|
367
|
// getMetacatClient().setAccess(newDocId, "public", "read", "allow", "allowFirst");
|
368
|
} catch (java.io.IOException ex) {
|
369
|
ex.printStackTrace();
|
370
|
}
|
371
|
|
372
|
//DEBUG STUFF
|
373
|
//String key, val;
|
374
|
//result = "-----------------iteration------------<br/>fNm = " + fNm + "<br/>";
|
375
|
//iterIt = paramsMap.keySet().iterator();
|
376
|
//while(iterIt.hasNext()) {
|
377
|
// key = (String) iterIt.next();
|
378
|
// val = (String) paramsMap.get(key);
|
379
|
// result += key + ": " + val + "<br/>";
|
380
|
//}
|
381
|
|
382
|
} else {
|
383
|
result = "Please enter the updated file path/name.";
|
384
|
}
|
385
|
return(result);
|
386
|
}
|
387
|
|
388
|
private InputStream getNextInputStream(MultipartParser multipartParser, StringBuilder fileName, HashMap paramsMap)
|
389
|
throws IOException {
|
390
|
InputStream result = null;
|
391
|
Part part;
|
392
|
String parmName = null, value = null, fnam;
|
393
|
|
394
|
while ((part = multipartParser.readNextPart()) != null) {
|
395
|
if (part.isParam()) {
|
396
|
parmName = part.getName();
|
397
|
value = ((ParamPart) part).getStringValue();
|
398
|
paramsMap.put(parmName, value);
|
399
|
System.out.println("ClientViewHelper.getNextInputStream: parmName = " + parmName + " value = " + value);
|
400
|
|
401
|
} else if (part.isFile()) {
|
402
|
fnam = ((FilePart) part).getFileName();
|
403
|
if (fnam != null && !fnam.equals("")) {
|
404
|
//*** File name is passed back via StringBuilder fileName param.
|
405
|
fileName.append(fnam);
|
406
|
result = ((FilePart) part).getInputStream();
|
407
|
System.out.println("ClientViewHelper.getNextInputStream: fileName = " + fileName + " inputStream = " + result.toString());
|
408
|
break;
|
409
|
}
|
410
|
}
|
411
|
}
|
412
|
return(result);
|
413
|
}
|
414
|
|
415
|
private void getRemainingParameters(MultipartParser multipartParser, HashMap paramsMap)
|
416
|
throws IOException {
|
417
|
InputStream result = null;
|
418
|
Part part;
|
419
|
String parmName = null, value = null, fnam;
|
420
|
|
421
|
while ((part = multipartParser.readNextPart()) != null) {
|
422
|
if (part.isParam()) {
|
423
|
parmName = part.getName();
|
424
|
value = ((ParamPart) part).getStringValue();
|
425
|
paramsMap.put(parmName, value);
|
426
|
System.out.println("ClientViewHelper.getNextInputStream: parmName = " + parmName + " value = " + value);
|
427
|
}
|
428
|
}
|
429
|
}
|
430
|
|
431
|
/**
|
432
|
* Queries Metacat for document listings, and returns the results in a TreeMap,
|
433
|
* where the key is the Doc Id, and the value is the Create Date. If the document
|
434
|
* contains the specified 'returnfield', an addtional entry will be created with
|
435
|
* the value being a Vector of sub-DocId's. The key of this entry will be the
|
436
|
* original DocId with some addtional text added.
|
437
|
* Reads bean properties 'pathExpr' (String[]), 'pathValue' (String)
|
438
|
* and 'returnfield' (String).
|
439
|
* @return TreeMap
|
440
|
*/
|
441
|
public TreeMap getSelectQueryMap() {
|
442
|
TreeMap result;
|
443
|
Document doc;
|
444
|
NodeList nodeLst, subNodeLst;
|
445
|
Node node, subNode;
|
446
|
String key, val, paramExpr, paramVal;
|
447
|
String value, returnFld;
|
448
|
String path;
|
449
|
Vector optGroup;
|
450
|
final String DOCID_EXPR = "./docid";
|
451
|
final String DOCNAME_EXPR = "./createdate";
|
452
|
final String PARAM_EXPR = "./param[@name='%1s']";
|
453
|
|
454
|
path = clientViewBean.getPathExpr();
|
455
|
returnFld = clientViewBean.getReturnfield();
|
456
|
value = clientViewBean.getPathValue();
|
457
|
|
458
|
result = new TreeMap();
|
459
|
//paramExpr = String.format(PARAM_EXPR, returnFld);
|
460
|
paramExpr = PARAM_EXPR.replaceFirst("%1s", returnFld);
|
461
|
//*** Query the database ***
|
462
|
doc = query(path, value, returnFld);
|
463
|
//*** Build the TreeMap to return ***
|
464
|
try {
|
465
|
nodeLst = (NodeList) xpath.evaluate("/resultset/document", doc, XPathConstants.NODESET);
|
466
|
for (int i = 0; i < nodeLst.getLength(); i++) {
|
467
|
node = nodeLst.item(i);
|
468
|
key = xpath.evaluate(DOCID_EXPR, node);
|
469
|
val = xpath.evaluate(DOCNAME_EXPR, node);
|
470
|
result.put(key, key + " (" + val + ")");
|
471
|
|
472
|
//*** returnfield values ***
|
473
|
subNodeLst = (NodeList) xpath.evaluate(paramExpr, node, XPathConstants.NODESET);
|
474
|
if (subNodeLst.getLength() > 0) {
|
475
|
optGroup = new Vector();
|
476
|
for (int k = 0; k < subNodeLst.getLength(); k++) {
|
477
|
subNode = subNodeLst.item(k);
|
478
|
paramVal = xpath.evaluate("text()", subNode);
|
479
|
optGroup.add(paramVal);
|
480
|
}
|
481
|
result.put(key + " Data Files", optGroup);
|
482
|
}
|
483
|
|
484
|
}
|
485
|
} catch (XPathExpressionException ex) {
|
486
|
ex.printStackTrace();
|
487
|
}
|
488
|
return(result);
|
489
|
}
|
490
|
|
491
|
/**
|
492
|
* Query metacat for documents that 'CONTAINS' the value at the specified XPath
|
493
|
* expression. Additionally, returns another non-standard field value.
|
494
|
* Standard info contains: DocId, DocName, DocType, CreateDate, and UpdateDate.
|
495
|
* @param pathExpr String contianing an XPath expression.
|
496
|
* @param pathValue String containing a comparison value at the XPath expression.
|
497
|
* @param returnFld String containing an XPath expression to a field which will be returned
|
498
|
* in addition to the standard info.
|
499
|
* @return DOM Document containing the results.
|
500
|
*/
|
501
|
public Document query(String pathExpr, String pathValue, String returnFld) {
|
502
|
Document result = null;
|
503
|
InputStream response;
|
504
|
BufferedReader buffy;
|
505
|
Properties prop;
|
506
|
|
507
|
try {
|
508
|
prop = new Properties();
|
509
|
prop.put("action", "query");
|
510
|
prop.put("qformat", "xml");
|
511
|
prop.put(pathExpr, pathValue);
|
512
|
if (returnFld != null) {
|
513
|
prop.put("returnfield", returnFld);
|
514
|
}
|
515
|
|
516
|
response = metacatClient.sendData(prop, null, null, 0);
|
517
|
if (response != null) {
|
518
|
buffy = new BufferedReader(new InputStreamReader(response));
|
519
|
result = XMLUtilities.getXMLReaderAsDOMDocument(buffy);
|
520
|
}
|
521
|
} catch (IOException ex) {
|
522
|
ex.printStackTrace();
|
523
|
} catch (Exception ex) {
|
524
|
ex.printStackTrace();
|
525
|
}
|
526
|
return(result);
|
527
|
}
|
528
|
|
529
|
public void setMetadataDoc(Document doc) {
|
530
|
metadataDoc = doc;
|
531
|
}
|
532
|
|
533
|
public void setMetadataDoc(String docId) throws Exception {
|
534
|
Document doc = null;
|
535
|
BufferedReader buffy;
|
536
|
Properties prop;
|
537
|
InputStream response;
|
538
|
|
539
|
//*** MetaCatServlet Properties: action, qformat and docid. ***
|
540
|
prop = new Properties();
|
541
|
prop.put("action", "read");
|
542
|
prop.put("qformat", "xml");
|
543
|
prop.put("docid", docId);
|
544
|
response = metacatClient.sendData(prop, null, null, 0);
|
545
|
if (response != null) {
|
546
|
buffy = new BufferedReader(new InputStreamReader(response));
|
547
|
doc = XMLUtilities.getXMLReaderAsDOMDocument(buffy);
|
548
|
}
|
549
|
setMetadataDoc(doc);
|
550
|
}
|
551
|
|
552
|
public void setMetadataDoc(InputStream ioStream) throws IOException {
|
553
|
BufferedReader buffy;
|
554
|
|
555
|
if (ioStream != null) {
|
556
|
buffy = new BufferedReader(new InputStreamReader(ioStream));
|
557
|
metadataDoc = XMLUtilities.getXMLReaderAsDOMDocument(buffy);
|
558
|
}
|
559
|
}
|
560
|
|
561
|
public Document getMetadataDoc() {
|
562
|
return(metadataDoc);
|
563
|
}
|
564
|
|
565
|
public String nextVersion(String lastDocId, String xPathQuery) throws XPathExpressionException {
|
566
|
String result = null, tokens[], scope, ready2Split, tmp;
|
567
|
int vers, docNum;
|
568
|
final int LAST_TOKEN = 2;
|
569
|
final String TEMPLATE = "%1s.%2d.%3d";
|
570
|
Node node;
|
571
|
|
572
|
//*** Parse the last Doc Id, and increment the version number.
|
573
|
if(lastDocId != null && lastDocId.contains(".")) {
|
574
|
ready2Split = lastDocId.replace('.','~'); //*** This is necessary for the split to work.
|
575
|
tokens = ready2Split.split("~");
|
576
|
if(tokens.length > LAST_TOKEN && !tokens[LAST_TOKEN].equals("")) {
|
577
|
scope = tokens[LAST_TOKEN - 2];
|
578
|
docNum = Integer.parseInt(tokens[LAST_TOKEN - 1]);
|
579
|
try {
|
580
|
vers = Integer.parseInt(tokens[LAST_TOKEN]);
|
581
|
//result = String.format(TEMPLATE, scope, docNum, 1 + vers);
|
582
|
tmp = TEMPLATE.replaceFirst("%1s", scope);
|
583
|
tmp = tmp.replaceFirst("%2d", String.valueOf(docNum));
|
584
|
result = tmp.replaceFirst("%3d", String.valueOf(vers + 1));
|
585
|
|
586
|
} catch (NumberFormatException ex) {
|
587
|
//*** In case the lastDocId has something other than a number.
|
588
|
//result = String.format(TEMPLATE, scope, docNum, 1);
|
589
|
tmp = TEMPLATE.replaceFirst("%1s", scope);
|
590
|
tmp = tmp.replaceFirst("%2d", String.valueOf(docNum));
|
591
|
result = tmp.replaceFirst("%3d", "1");
|
592
|
}
|
593
|
} else {
|
594
|
//*** In case the lastDocId ends with a '.'
|
595
|
result = lastDocId + "1";
|
596
|
}
|
597
|
} else {
|
598
|
//*** In case of missing doc Id.
|
599
|
result = null;
|
600
|
}
|
601
|
//*** Update the Doc Id in the metadata file.
|
602
|
if (getMetadataDoc() != null) {
|
603
|
node = (Node) xpath.evaluate(xPathQuery, getMetadataDoc().getDocumentElement(), XPathConstants.NODE);
|
604
|
node.setTextContent(result);
|
605
|
}
|
606
|
return(result);
|
607
|
}
|
608
|
|
609
|
private String nextDocId(String lastDocId, String scope) {
|
610
|
String result = null, tokens[], tmp;
|
611
|
int vers;
|
612
|
String template = scope.toLowerCase() + ".%1d.%2d";
|
613
|
|
614
|
if(lastDocId != null && lastDocId.contains(".")) {
|
615
|
lastDocId = lastDocId.replace('.','~'); //*** This is necessary for the split to work.
|
616
|
tokens = lastDocId.split("~");
|
617
|
if(tokens.length > 1 && !tokens[1].equals("")) {
|
618
|
try {
|
619
|
vers = Integer.parseInt(tokens[1]);
|
620
|
//result = String.format(template, 1 + vers, 1);
|
621
|
tmp = template.replaceFirst("%1d", String.valueOf(1 + vers));
|
622
|
result = tmp.replaceFirst("%2d", "1");
|
623
|
} catch (NumberFormatException ex) {
|
624
|
//*** In case the lastDocId has something other than a number.
|
625
|
//result = String.format(template, 1, 1);
|
626
|
tmp = template.replaceFirst("%1d", "1");
|
627
|
result = tmp.replaceFirst("%2d", "1");
|
628
|
}
|
629
|
} else {
|
630
|
//*** In case the lastDocId ends with a '.'
|
631
|
//result = String.format(template, 1, 1);
|
632
|
tmp = template.replaceFirst("%1d", "1");
|
633
|
result = tmp.replaceFirst("%2d", "1");
|
634
|
}
|
635
|
} else {
|
636
|
//*** In case there isn't any doc Id's with the user name.
|
637
|
//result = String.format(template, 1, 1);
|
638
|
tmp = template.replaceFirst("%1d", "1");
|
639
|
result = tmp.replaceFirst("%2d", "1");
|
640
|
}
|
641
|
return(result);
|
642
|
}
|
643
|
|
644
|
public MetacatClient getMetacatClient() {
|
645
|
return(metacatClient);
|
646
|
}
|
647
|
}
|