private int updateTransaction(Element request, Element xml, ServiceContext context, Set<String> toIndex) throws Exception {
GeonetContext gc = (GeonetContext) context.getHandlerContext(Geonet.CONTEXT_NAME);
DataManager dataMan = gc.getBean(DataManager.class);
if (context.getUserSession().getUserId() == null) {
throw new NoApplicableCodeEx("User not authenticated.");
}
int totalUpdated = 0;
// Update full metadata
if (xml != null) {
// Retrieve schema and the related Namespaces
String schemaId = gc.getBean(SchemaManager.class).autodetectSchema(xml);
if (schemaId == null) {
throw new NoApplicableCodeEx("Can't identify metadata schema");
}
// Retrieve the metadata identifier
String uuid = gc.getBean(DataManager.class).extractUUID(schemaId, xml);
if (uuid.length() == 0) {
throw new NoApplicableCodeEx("Metadata identifier not provided");
}
// Update metadata record
String id = dataMan.getMetadataId(uuid);
if (id == null) {
return totalUpdated;
}
if (!gc.getBean(AccessManager.class).canEdit(context, id)) {
throw new NoApplicableCodeEx("User not allowed to update this metadata(" + id + ").");
}
String changeDate = null;
boolean validate = false;
boolean ufo = false;
boolean index = false;
String language = context.getLanguage();
dataMan.updateMetadata(context, id, xml, validate, ufo, index, language, changeDate, false);
toIndex.add(id);
totalUpdated++;
return totalUpdated;
// Update properties
} else {
//first, search the record in the database to get the record id
final List<Element> constraints = request.getChildren("Constraint", Csw.NAMESPACE_CSW);
Assert.isTrue(constraints.size() == 1, "One and only 1 constraint allowed in update query. Found : " + constraints.size());
Element constr = (Element) constraints.get(0).clone();
List<Element> results = getResultsFromConstraints(context, constr);
@SuppressWarnings("unchecked")
List<Element> recordProperties = request.getChildren("RecordProperty", Csw.NAMESPACE_CSW);
Iterator<Element> it = results.iterator();
if (!it.hasNext())
return totalUpdated;
Set<String> updatedMd = new HashSet<String>();
// Process all records selected
while (it.hasNext()) {
Element result = it.next();
String uuid = result.getChildText("identifier", Csw.NAMESPACE_DC);
String id = dataMan.getMetadataId(uuid);
String changeDate = null;
if (id == null)
continue;
if (!dataMan.getAccessManager().canEdit(context, id))
throw new NoApplicableCodeEx("User not allowed to update this metadata(" + id + ").");
Element metadata = dataMan.getMetadata(context, id, false, false, true);
metadata.removeChild("info", Edit.NAMESPACE);
// Retrieve the schema and Namespaces of metadata to update
String schemaId = gc.getBean(DataManager.class).autodetectSchema(metadata);
if (schemaId == null) {
throw new NoApplicableCodeEx("Can't identify metadata schema");
}
boolean metadataChanged = false;
EditLib editLib = new EditLib(_schemaManager);