Revision 3305
Added by barteau over 17 years ago
src/edu/ucsb/nceas/metacat/client/MetacatClient.java | ||
---|---|---|
81 | 81 |
* Identifies the FGDC DTD. |
82 | 82 |
*/ |
83 | 83 |
public static final String FGDC_SYSTEM_ID = "http://www.fgdc.gov/metadata/fgdc-std-001-1998.dtd"; |
84 |
|
|
85 |
/** |
|
86 |
* Data Document ID location within an FGDC document. XPath expression. |
|
87 |
*/ |
|
88 |
public static final String FGDC_DATA_FILE_DOCID_XPATH = "/metadata/distinfo/stdorder/digform/digtopt/onlinopt/computer/networka/networkr"; |
|
89 |
|
|
90 |
/** |
|
91 |
* Metadata Document ID location within an FGDC document. XPath expression. |
|
92 |
*/ |
|
93 |
public static final String FGDC_DOCID_XPATH = "/metadata/distinfo/resdesc"; |
|
94 |
|
|
84 | 95 |
private static XPath xpath = XPathFactory.newInstance().newXPath(); |
85 | 96 |
private Document loginResponse = null, metadataDoc = null; |
86 | 97 |
private String user = null; |
... | ... | |
1015 | 1026 |
} |
1016 | 1027 |
} |
1017 | 1028 |
|
1029 |
public void setMetadataDoc(Document doc) { |
|
1030 |
metadataDoc = doc; |
|
1031 |
} |
|
1032 |
|
|
1033 |
public void setMetadataDoc(String docId) throws Exception { |
|
1034 |
Document doc = null; |
|
1035 |
BufferedReader buffy; |
|
1036 |
Properties prop; |
|
1037 |
InputStream response; |
|
1038 |
|
|
1039 |
//*** MetaCatServlet Properties: action, qformat and docid. *** |
|
1040 |
prop = new Properties(); |
|
1041 |
prop.put("action", "read"); |
|
1042 |
prop.put("qformat", "xml"); |
|
1043 |
prop.put("docid", docId); |
|
1044 |
response = sendData(prop, null, null, 0); |
|
1045 |
if (response != null) { |
|
1046 |
buffy = new BufferedReader(new InputStreamReader(response)); |
|
1047 |
doc = XMLUtilities.getXMLReaderAsDOMDocument(buffy); |
|
1048 |
} |
|
1049 |
setMetadataDoc(doc); |
|
1050 |
} |
|
1051 |
|
|
1052 |
public Document getMetadataDoc() { |
|
1053 |
return(metadataDoc); |
|
1054 |
} |
|
1055 |
|
|
1018 | 1056 |
/** |
1019 | 1057 |
* JSP API: A convenient and efficient method to retrieve info from the "login response". |
1020 | 1058 |
* @param elementName String of the elementName. |
... | ... | |
1063 | 1101 |
} |
1064 | 1102 |
|
1065 | 1103 |
/** |
1066 |
* JSP API: Get the user's name. This will be what was entered for the user name,
|
|
1104 |
* JSP API: Get the user's name. This will be what was entered for the user name, |
|
1067 | 1105 |
* and is not the LDAP user name. |
1068 | 1106 |
* @return String of the users name. |
1069 | 1107 |
*/ |
... | ... | |
1246 | 1284 |
return(result); |
1247 | 1285 |
} |
1248 | 1286 |
|
1249 |
private String nextVersion(String lastDocId) { |
|
1250 |
String result = null, tokens[], scope; |
|
1287 |
private String nextVersion(String lastDocId) throws XPathExpressionException {
|
|
1288 |
String result = null, tokens[], scope, xPathQuery, ready2Split;
|
|
1251 | 1289 |
int vers, docNum; |
1252 | 1290 |
final int LAST_TOKEN = 2; |
1253 | 1291 |
final String TEMPLATE = "%1$s.%2$d.%3$d"; |
1292 |
final String XPATH_QUERY_TEMPLATE = FGDC_DOCID_XPATH + "[text()='%1$s']"; |
|
1293 |
Node node; |
|
1254 | 1294 |
|
1295 |
//*** Parse the last Doc Id, and increment the version number. |
|
1255 | 1296 |
if(lastDocId != null && lastDocId.contains(".")) { |
1256 |
lastDocId = lastDocId.replace('.','~'); //*** This is necessary for the split to work.
|
|
1257 |
tokens = lastDocId.split("~");
|
|
1297 |
ready2Split = lastDocId.replace('.','~'); //*** This is necessary for the split to work.
|
|
1298 |
tokens = ready2Split.split("~");
|
|
1258 | 1299 |
if(tokens.length > LAST_TOKEN && !tokens[LAST_TOKEN].equals("")) { |
1259 | 1300 |
scope = tokens[LAST_TOKEN - 2]; |
1260 | 1301 |
docNum = Integer.parseInt(tokens[LAST_TOKEN - 1]); |
... | ... | |
1273 | 1314 |
//*** In case of missing doc Id. |
1274 | 1315 |
result = null; |
1275 | 1316 |
} |
1317 |
|
|
1318 |
//*** Update the Doc Id in the metadata file. |
|
1319 |
if (getMetadataDoc() != null) { |
|
1320 |
xPathQuery = String.format(XPATH_QUERY_TEMPLATE, lastDocId); |
|
1321 |
node = (Node) xpath.evaluate(xPathQuery, getMetadataDoc().getDocumentElement(), XPathConstants.NODE); |
|
1322 |
node.setTextContent(result); |
|
1323 |
} |
|
1276 | 1324 |
return(result); |
1277 | 1325 |
} |
1278 | 1326 |
|
1279 | 1327 |
private boolean isFGDC() { |
1280 | 1328 |
boolean result = false; |
1281 | 1329 |
DocumentType docType; |
1282 |
String sysId, title = null;
|
|
1330 |
String sysId; |
|
1283 | 1331 |
final String FGDC_TEST_EXPRESSION = "/metadata/idinfo/citation/citeinfo/title"; |
1332 |
Node node = null; |
|
1284 | 1333 |
|
1285 | 1334 |
//*** First, try the rigid proper way of determining it. |
1286 |
if (metadataDoc != null) {
|
|
1287 |
docType = metadataDoc.getDoctype();
|
|
1335 |
if (getMetadataDoc() != null) {
|
|
1336 |
docType = getMetadataDoc().getDoctype();
|
|
1288 | 1337 |
if (docType != null) { |
1289 | 1338 |
sysId = docType.getSystemId(); |
1290 | 1339 |
if (sysId != null) |
... | ... | |
1292 | 1341 |
} |
1293 | 1342 |
} |
1294 | 1343 |
//*** It might not have a doc type line, so try another method. |
1295 |
if (!result) { |
|
1344 |
if (getMetadataDoc() != null && !result) {
|
|
1296 | 1345 |
try { |
1297 |
title = getMetadataDocElement(FGDC_TEST_EXPRESSION);
|
|
1346 |
node = (Node) xpath.evaluate(FGDC_TEST_EXPRESSION, getMetadataDoc().getDocumentElement(), XPathConstants.NODE);
|
|
1298 | 1347 |
} catch (XPathExpressionException ex) { |
1299 | 1348 |
ex.printStackTrace(); |
1300 | 1349 |
} |
1301 |
result = (title != null && !title.equals(""));
|
|
1350 |
result = (node != null);
|
|
1302 | 1351 |
} |
1303 | 1352 |
return(result); |
1304 | 1353 |
} |
... | ... | |
1478 | 1527 |
return(result); |
1479 | 1528 |
} |
1480 | 1529 |
|
1481 |
|
|
1530 |
|
|
1482 | 1531 |
/** |
1483 | 1532 |
* JSP API: A static helper method which takes attribute values and returns |
1484 | 1533 |
* an XHTML INPUT Statement. |
... | ... | |
1496 | 1545 |
return(result); |
1497 | 1546 |
} |
1498 | 1547 |
|
1499 |
|
|
1548 |
|
|
1500 | 1549 |
/** |
1501 | 1550 |
* JSP API: A static helper method which takes a key/value pair and saves |
1502 | 1551 |
* it in the session. |
... | ... | |
1513 | 1562 |
} |
1514 | 1563 |
} |
1515 | 1564 |
|
1516 |
|
|
1565 |
|
|
1517 | 1566 |
/** |
1518 | 1567 |
* JSP API: A static helper method which takes a key and returns the |
1519 | 1568 |
* value. |
... | ... | |
1577 | 1626 |
* contains the specified 'returnfield', an addtional entry will be created with |
1578 | 1627 |
* the value being a Vector of sub-DocId's. The key of this entry will be the |
1579 | 1628 |
* original DocId with some addtional text added. |
1580 |
* Reads request parameters 'pathExpr' (String[]), 'pathValue' (String)
|
|
1629 |
* Reads request parameters 'pathExpr' (String[]), 'pathValue' (String) |
|
1581 | 1630 |
* and 'returnfield' (String). |
1582 | 1631 |
* @param request HttpServletRequest. |
1583 | 1632 |
* @return TreeMap |
... | ... | |
1633 | 1682 |
return(result); |
1634 | 1683 |
} |
1635 | 1684 |
|
1636 |
/** |
|
1637 |
* JSP API: Removes the data Doc Id from the FGDC document in the Metacat database. |
|
1638 |
* It gets the Doc Id to delete from the session (key=docId), and determines what |
|
1639 |
* metadata file is including this file. It then queries metacat for the parent FGDC |
|
1640 |
* document and removes the Doc Id from it, and reloads the new version of the FGDC |
|
1641 |
* document with a new revision number. This is intended to be called after |
|
1642 |
* a call to MetacatClient.delete(docId). |
|
1643 |
* @param request HttpServletRequest which contains the session. |
|
1644 |
* @return String server response, if any. |
|
1645 |
*/ |
|
1646 |
public String removeDataDocIdFromFGDC(HttpServletRequest request) { |
|
1647 |
String docId, parentDocId, result = " ", pathToDigform, revision; |
|
1685 |
private String removeDataDocIdFromFGDC(String docId, String parentDocId) throws Exception { |
|
1686 |
String pathToDigform, revision = ""; |
|
1648 | 1687 |
Document doc; |
1649 | 1688 |
InputStream response; |
1650 | 1689 |
BufferedReader buffy; |
... | ... | |
1652 | 1691 |
Node node; |
1653 | 1692 |
NodeList nodeLst; |
1654 | 1693 |
Reader reader; |
1655 |
final String PATH4QUERY = "/metadata/distinfo/stdorder/digform/digtopt/onlinopt/computer/networka/networkr"; |
|
1656 |
final String PATH4ANCESTOR = PATH4QUERY + "[text()='%1$s']/ancestor::node()[name()='%2$s']"; |
|
1694 |
final String PATH4ANCESTOR = FGDC_DATA_FILE_DOCID_XPATH + "[text()='%1$s']/ancestor::node()[name()='%2$s']"; |
|
1657 | 1695 |
|
1658 |
//*** First, determine what metadata file is including this file (if any). |
|
1696 |
//*** Get the metadata document and remove the digform branch. |
|
1697 |
doc = getMetadataDoc(); |
|
1698 |
if (doc != null) { |
|
1699 |
//System.out.println("MetacatClinet.removeDataDocIdFromFGDC: BEFORE\n" + XMLUtilities.getDOMTreeAsString(doc.getDocumentElement())); |
|
1700 |
pathToDigform = String.format(PATH4ANCESTOR, docId, "digform"); |
|
1701 |
node = (Node) xpath.evaluate(pathToDigform, doc.getDocumentElement(), XPathConstants.NODE); |
|
1702 |
node.getParentNode().removeChild(node); |
|
1703 |
//System.out.println("MetacatClinet.removeDataDocIdFromFGDC: AFTER\n" + XMLUtilities.getDOMTreeAsString(doc.getDocumentElement())); |
|
1704 |
|
|
1705 |
revision = nextVersion(parentDocId); |
|
1706 |
reader = XMLUtilities.getDOMTreeAsReader(doc.getDocumentElement(), false); |
|
1707 |
update(revision, reader, null); |
|
1708 |
} |
|
1709 |
return(revision); |
|
1710 |
} |
|
1711 |
|
|
1712 |
/** |
|
1713 |
* Removes the data Doc Id from the FGDC document in the Metacat database. |
|
1714 |
* Determines what |
|
1715 |
* metadata file is including this file. It then queries metacat for the parent FGDC |
|
1716 |
* document and removes the Doc Id from it, and reloads the new version of the FGDC |
|
1717 |
* document with a new revision number. This is intended to be called after |
|
1718 |
* a call to MetacatClient.delete(docId). |
|
1719 |
* @param docId String which contains the document Id. |
|
1720 |
* @return String server response, if any. |
|
1721 |
*/ |
|
1722 |
public void clientDeleteRequest(HttpServletRequest request) { |
|
1723 |
String result = null, docId, subDocId, parentDocId, revisedDocId; |
|
1724 |
NodeList nodeLst; |
|
1725 |
final String SUB_DOCS_PATH = FGDC_DATA_FILE_DOCID_XPATH + "/text()"; |
|
1726 |
Node node; |
|
1727 |
Document resultSetDoc; |
|
1728 |
|
|
1659 | 1729 |
docId = getSessValue("docId", request); |
1660 |
doc = query(PATH4QUERY, docId, null); |
|
1661 | 1730 |
try { |
1662 |
parentDocId = xpath.evaluate("/resultset/document/docid", doc.getDocumentElement()); |
|
1731 |
//*** First, determine what metadata file is including this file (if any). |
|
1732 |
resultSetDoc = query(FGDC_DATA_FILE_DOCID_XPATH, docId, null); |
|
1733 |
parentDocId = xpath.evaluate("/resultset/document/docid", resultSetDoc.getDocumentElement()); |
|
1663 | 1734 |
if (parentDocId != null && !parentDocId.equals("")) { |
1664 |
//*** Next, get the metadata document and update the networkr node. |
|
1665 |
//*** MetaCatServlet Properties: action, qformat and docid. *** |
|
1666 |
prop = new Properties(); |
|
1667 |
prop.put("action", "read"); |
|
1668 |
prop.put("qformat", "xml"); |
|
1669 |
prop.put("docid", parentDocId); |
|
1670 |
response = sendData(prop, null, null, 0); |
|
1671 |
if (response != null) { |
|
1672 |
buffy = new BufferedReader(new InputStreamReader(response)); |
|
1673 |
doc = XMLUtilities.getXMLReaderAsDOMDocument(buffy); |
|
1674 |
//System.out.println("MetacatClinet.removeDataDocIdFromFGDC: BEFORE\n" + XMLUtilities.getDOMTreeAsString(doc.getDocumentElement())); |
|
1675 |
pathToDigform = String.format(PATH4ANCESTOR, docId, "digform"); |
|
1676 |
node = (Node) xpath.evaluate(pathToDigform, doc, XPathConstants.NODE); |
|
1677 |
node.getParentNode().removeChild(node); |
|
1678 |
//System.out.println("MetacatClinet.removeDataDocIdFromFGDC: AFTER\n" + XMLUtilities.getDOMTreeAsString(doc.getDocumentElement())); |
|
1679 |
reader = XMLUtilities.getDOMTreeAsReader(doc.getDocumentElement(), false); |
|
1680 |
revision = nextVersion(parentDocId); |
|
1681 |
result += update(revision, reader, null); |
|
1735 |
setMetadataDoc(parentDocId); |
|
1736 |
//*** Remove Doc Id from any parent metadata document. |
|
1737 |
revisedDocId = removeDataDocIdFromFGDC(docId, parentDocId); |
|
1738 |
setSessValue("docId", revisedDocId, request); |
|
1739 |
} else { |
|
1740 |
setMetadataDoc(docId); |
|
1741 |
//*** This is a metadata document, so remove all of the sub-docId's. |
|
1742 |
nodeLst = (NodeList) xpath.evaluate(SUB_DOCS_PATH, getMetadataDoc().getDocumentElement(), XPathConstants.NODESET); |
|
1743 |
for(int i = 0; i < nodeLst.getLength(); i++) { |
|
1744 |
node = nodeLst.item(i); |
|
1745 |
subDocId = node.getNodeValue(); |
|
1746 |
//*** Remove the sub-document. |
|
1747 |
try { |
|
1748 |
delete(subDocId); |
|
1749 |
} catch (MetacatException ex) { |
|
1750 |
ex.printStackTrace(); |
|
1751 |
} |
|
1682 | 1752 |
} |
1683 | 1753 |
} |
1754 |
//*** Remove the document. |
|
1755 |
result = delete(docId); |
|
1756 |
//*** Save the server feedback in the session, to be used by the view. |
|
1757 |
setSessValue("updateFeedback", result, request); |
|
1684 | 1758 |
} catch (Exception ex) { |
1685 | 1759 |
ex.printStackTrace(); |
1686 | 1760 |
} |
1687 |
return(result); |
|
1688 | 1761 |
} |
1689 | 1762 |
} |
Also available in: Unified diff
Fixed bug: successfully removes related sub Doc ID's from metadata.
New functionality:
1) Updates field in FGDC metadata Doc ID with new version of Doc ID, and
2) Recurses sub-documents for deletion, when deleting an FGDC metadata Doc ID (deletes entire package).