ServiceContext context,
Set<Integer> metadata) throws Exception {
GeonetContext gc = (GeonetContext) context
.getHandlerContext(Geonet.CONTEXT_NAME);
DataManager dataMan = gc.getBean(DataManager.class);
SchemaManager schemaMan = gc.getBean(SchemaManager.class);
AccessManager accessMan = gc.getBean(AccessManager.class);
report.incrementProcessedRecords();
// When a record is deleted the UUID is in the selection manager
// and when retrieving id, return null
if (id == null) {
report.incrementNullRecords();
return null;
}
int iId = Integer.valueOf(id);
Metadata metadataEntity = context.getBean(MetadataRepository.class).findOne(iId);
MetadataDataInfo info = metadataEntity.getDataInfo();
// Get metadata title from the index
String metadataTitle = LuceneSearcher.getMetadataFromIndexById(context.getLanguage(), id, "title");
if (StringUtils.isEmpty(metadataTitle)) metadataTitle = metadataEntity.getUuid();
if (info == null) {
MassiveReplaceReportEntry notFoundEntry =
new MassiveReplaceReportEntry(id, "", null);
report.addNotFound(notFoundEntry);
} else if (!accessMan.isOwner(context, id)) {
MassiveReplaceReportEntry notOwnerEntry =
new MassiveReplaceReportEntry(metadataEntity.getUuid(), metadataTitle, null);
report.addNotEditable(notOwnerEntry);
} else {
// -----------------------------------------------------------------------
// --- check processing exist for current schema
String schema = info.getSchemaId();
String filePath = schemaMan.getSchemaDir(schema) + "/process/" + process + ".xsl";
File xslProcessing = new File(filePath);
if (!xslProcessing.exists()) {
context.info(" Processing instruction not found for " + schema
+ " schema.");
MassiveReplaceReportEntry notOwnerEntry =
new MassiveReplaceReportEntry(metadataEntity.getUuid(), metadataTitle, null);
report.addNoProcessFound(notOwnerEntry);
return null;
}
// --- Process metadata
Element processedMetadata = null;
try {
Element md = dataMan.getMetadataNoInfo(context, id);
// -- here we send parameters set by user from
// URL if needed.
List<Element> children = params.getChildren();
Map<String, Object> xslParameter = new HashMap<String, Object>();
for (Element param : children) {
if (param.getChildren().size() > 0) {
xslParameter.put(param.getName(), param);
} else {
xslParameter.put(param.getName(), param.getTextTrim());
}
}
processedMetadata = Xml.transformWithXmlParam(md, filePath, paramNameXml, paramXml);
// Get changes
String filePath2 = schemaMan.getSchemaDir(schema) + "/process/massive-content-update-extract-changes.xsl";
List<Element> changesEl = Xml.transform(processedMetadata, filePath2).getChildren("change");
boolean hasChanges = (changesEl.size() > 0);
MassiveReplaceReportEntry mdEntry = new MassiveReplaceReportEntry(metadataEntity.getUuid(),
metadataTitle,
changesEl);
if (hasChanges) {
report.addChanged(mdEntry);
} else {
report.addNotChanged(mdEntry);
}
// --- save metadata and return status
if ((changesEl.size() > 0) && (!params.getChildText("test").equalsIgnoreCase("true"))) {
// Clean geonet:changes elements
String filePath3 = schemaMan.getSchemaDir(schema) + "/process/massive-content-update-clean-changes.xsl";
processedMetadata = Xml.transform(processedMetadata, filePath3);
dataMan.updateMetadata(context, id, processedMetadata,
false, true, true,
context.getLanguage(),
new ISODate().toString(), true);
}