GeonetContext gc = (GeonetContext) context.getHandlerContext(Geonet.CONTEXT_NAME);
// get metadata
Element md = dataMan.getMetadataNoInfo(context, "" + metadataEntity.getId());
MetadataDataInfo mdInfo = metadataEntity.getDataInfo();
// Build a list of all Namespaces in the metadata document
List<Namespace> metadataNamespaces = namespaceList.get(mdInfo.getSchemaId());
if (metadataNamespaces == null) {
metadataNamespaces = new ArrayList<Namespace>();
Namespace ns = md.getNamespace();
if (ns != null) {
metadataNamespaces.add(ns);
metadataNamespaces.addAll(md.getAdditionalNamespaces());
namespaceList.put(mdInfo.getSchemaId(), metadataNamespaces);
}
}
new Document(md);
// select all nodes that come back from the xpath selectNodes
List<?> nodes = Xml.selectNodes(md, xpath, metadataNamespaces);
if (context.isDebugEnabled() || !doChanges) {
context.debug("xpath \n"+xpath+"\n returned "+nodes.size()+" results");
if (!doChanges) {
response.addContent(new Element("xpath").setText(xpath));
response.addContent(new Element("xpathReturned").setText(nodes.size()+" results"));
}
}
// for each node
for (Iterator<?> iter = nodes.iterator(); iter.hasNext();) {
Object o = iter.next();
if (o instanceof Element) {
Element elem = (Element)o;
if (context.isDebugEnabled() || !doChanges) {
context.debug("Test: Subtemplate with \n"+Xml.getString(elem));
if (!doChanges) {
response.addContent(new Element("subtemplate").setText(Xml.getString(elem)));
}
}
// extract title from node
String title = null;
if (getTit.length() > 0) { // use xslt path in getTit
Element xmlTitle = Xml.transform((Element)elem.clone(), getTit);
if (context.isDebugEnabled() || !doChanges) {
context.debug("Test: Title \n"+Xml.getString(xmlTitle));
}
title = xmlTitle.getText();
} else { // use xpathTit
List<?> titNodes = Xml.selectNodes(elem, xpathTit, metadataNamespaces);
StringBuilder sb = new StringBuilder();
for (Iterator<?> iterTit = titNodes.iterator(); iterTit.hasNext();) {
Object oTit = iterTit.next();
if (oTit instanceof Element) { // getText
Element eTit = (Element)oTit;
sb.append(eTit.getTextTrim());
} else if (oTit instanceof Comment) { // not sure about this one!
Comment cTit = (Comment)oTit;
sb.append(cTit.getText());
} else if (oTit instanceof Attribute) {
Attribute aTit = (Attribute)oTit;
sb.append(aTit.getValue());
} else if (oTit instanceof Text) {
Text tTit = (Text)oTit;
sb.append(tTit.getTextTrim());
}
sb.append(" ");
}
title = sb.toString().trim();
}
if (!doChanges) {
response.addContent(new Element("title").setText(title));
}
String uuid = elem.getAttributeValue("uuid");
if (uuid == null || uuid.length() == 0) {
// calc uuid based on sha1sum of content - this is the way to
// generate this safely
uuid = Sha1Encoder.encodeString(elem.getValue());
}
if (doChanges) { // insert subtemplate if it isn't already present
if (dataMan.getMetadataId(uuid) != null) {
context.debug("Test: Subtemplate with uuid "+uuid+" already exists");
} else {
//if (context.isDebug()) context.info("Test: Add subtemplate uuid "+uuid);
// add node as a subtemplate
String docType = null, createDate = null, changeDate = null;
String group = "1";
int user = context.getUserSession().getUserIdAsInt();
boolean ufo = false, indexImmediate = false;
String sId = dataMan.insertMetadata(context, mdInfo.getSchemaId(), (Element) elem.clone(), uuid, user, group,
gc.getBean(SettingManager.class).getSiteId(), "s", docType, category, createDate, changeDate, ufo, indexImmediate);
subtemplates.add(Integer.valueOf(sId));
}
}