Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

INSPIRE Validation / Add support to validate using GetRecordById url #5227

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,24 @@
import jeeves.transaction.TransactionManager;
import jeeves.transaction.TransactionTask;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpStatus;
import org.fao.geonet.NodeInfo;
import org.fao.geonet.api.API;
import org.fao.geonet.api.records.editing.InspireValidatorUtils;
import org.fao.geonet.api.records.formatters.FormatType;
import org.fao.geonet.api.records.formatters.FormatterApi;
import org.fao.geonet.api.records.formatters.FormatterWidth;
import org.fao.geonet.api.records.formatters.cache.Key;
import org.fao.geonet.domain.AbstractMetadata;
import org.fao.geonet.domain.MetadataValidation;
import org.fao.geonet.domain.MetadataValidationId;
import org.fao.geonet.domain.MetadataValidationStatus;
import org.fao.geonet.domain.*;
import org.fao.geonet.kernel.AccessManager;
import org.fao.geonet.kernel.DataManager;
import org.fao.geonet.kernel.SchemaManager;
import org.fao.geonet.kernel.datamanager.IMetadataSchemaUtils;
import org.fao.geonet.kernel.datamanager.IMetadataUtils;
import org.fao.geonet.kernel.setting.SettingManager;
import org.fao.geonet.repository.MetadataValidationRepository;
import org.fao.geonet.repository.SourceRepository;
import org.fao.geonet.schema.iso19139.ISO19139Namespaces;
import org.fao.geonet.schema.iso19139.ISO19139SchemaPlugin;
import org.fao.geonet.utils.Log;
import org.fao.geonet.utils.Xml;
Expand Down Expand Up @@ -122,7 +124,7 @@ public Object doInTransaction(TransactionStatus transaction) throws Throwable {
});
}

public void processMetadata(Set<String> uuids) throws Exception {
public void processMetadata(Set<String> uuids, String mode) throws Exception {
IMetadataUtils metadataRepository = appContext.getBean(IMetadataUtils.class);
MetadataValidationRepository metadataValidationRepository = appContext.getBean(MetadataValidationRepository.class);
AccessManager accessManager = appContext.getBean(AccessManager.class);
Expand Down Expand Up @@ -177,33 +179,65 @@ public Object doInTransaction(TransactionStatus transaction) throws Throwable {
}

if (applyCondition) {
String testId = inspireValidatorUtils.submitFile(serviceContext, URL,
new ByteArrayInputStream(mdToValidate.getBytes()), entry.getKey(), record.getUuid());

inspireValidatorUtils.waitUntilReady(serviceContext, URL, testId);

String reportUrl = inspireValidatorUtils.getReportUrl(URL, testId);
String reportXmlUrl = InspireValidatorUtils.getReportUrlXML(URL, testId);
String reportXml = inspireValidatorUtils.retrieveReport(serviceContext, reportXmlUrl);

String validationStatus = inspireValidatorUtils.isPassed(serviceContext, URL, testId);

MetadataValidationStatus metadataValidationStatus =
inspireValidatorUtils.calculateValidationStatus(validationStatus);

MetadataValidation metadataValidation = new MetadataValidation()
.setId(new MetadataValidationId(record.getId(), "inspire"))
.setStatus(metadataValidationStatus).setRequired(false)
.setReportUrl(reportUrl).setReportContent(reportXml);

metadataValidationRepository.save(metadataValidation);

//new RecordValidationTriggeredEvent(record.getId(),
// ApiUtils.getUserSession(request.getSession()).getUserIdAsInt(),
// metadataValidation.getStatus().getCode()).publish(appContext);

reindexMetadata = true;
inspireMetadata = true;
String testId = null;
String getRecordByIdUrl = null;
if (StringUtils.isEmpty(mode)) {
testId = inspireValidatorUtils.submitFile(serviceContext, URL,
new ByteArrayInputStream(mdToValidate.getBytes()), entry.getKey(), record.getUuid());
} else {
String portal = null;
if (!NodeInfo.DEFAULT_NODE.equals(mode)) {
Source source = appContext.getBean(SourceRepository.class).findOneByUuid(mode);
if (source == null) {
metadataAnalysedInError++;
Log.warning(API.LOG_MODULE_NAME, String.format(
"Portal %s not found. There is no CSW endpoint at this URL " +
"that we can send to the validator.", mode));
}
portal = mode;
} else {
portal = NodeInfo.DEFAULT_NODE;
}

if (portal != null) {
getRecordByIdUrl = String.format(
"%s%s/eng/csw?SERVICE=CSW&REQUEST=GetRecordById&VERSION=2.0.2&" +
"OUTPUTSCHEMA=%s&ELEMENTSETNAME=full&ID=%s",
appContext.getBean(SettingManager.class).getBaseURL(),
portal,
ISO19139Namespaces.GMD.getURI(),
record.getUuid());
testId = inspireValidatorUtils.submitUrl(serviceContext, URL, getRecordByIdUrl, entry.getKey(), record.getUuid());
}
}
if (testId != null) {

inspireValidatorUtils.waitUntilReady(serviceContext, URL, testId);

String reportUrl = inspireValidatorUtils.getReportUrl(URL, testId);
String reportXmlUrl = InspireValidatorUtils.getReportUrlXML(URL, testId);
String reportXml = inspireValidatorUtils.retrieveReport(serviceContext, reportXmlUrl);

String validationStatus = inspireValidatorUtils.isPassed(serviceContext, URL, testId);

MetadataValidationStatus metadataValidationStatus =
inspireValidatorUtils.calculateValidationStatus(validationStatus);

MetadataValidation metadataValidation = new MetadataValidation()
.setId(new MetadataValidationId(record.getId(), "inspire"))
.setStatus(metadataValidationStatus).setRequired(false)
.setReportUrl(reportUrl).setReportContent(reportXml);

metadataValidationRepository.save(metadataValidation);

//new RecordValidationTriggeredEvent(record.getId(),
// ApiUtils.getUserSession(request.getSession()).getUserIdAsInt(),
// metadataValidation.getStatus().getCode()).publish(appContext);

reindexMetadata = true;
inspireMetadata = true;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.fao.geonet.api.processing.report.registry.IProcessingReportRegistry;
import org.fao.geonet.api.records.editing.InspireValidatorUtils;
import org.fao.geonet.domain.AbstractMetadata;
import org.fao.geonet.domain.MetadataValidation;
import org.fao.geonet.events.history.RecordValidationTriggeredEvent;
import org.fao.geonet.kernel.AccessManager;
import org.fao.geonet.kernel.DataManager;
Expand All @@ -62,9 +63,11 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayDeque;
import java.util.List;
import java.util.Set;

import static org.fao.geonet.api.ApiParams.*;
import static org.fao.geonet.api.records.InspireValidationApi.API_PARAM_INSPIRE_VALIDATION_MODE;

@RequestMapping(value = {
"/{portal}/api/records"
Expand Down Expand Up @@ -191,6 +194,77 @@ public SimpleMetadataProcessingReport validateRecords(
}


@io.swagger.v3.oas.annotations.Operation(summary = "Clear validation status of one or more records",
description = "")
@RequestMapping(
value = "/validate",
method = RequestMethod.DELETE,
produces = {
MediaType.APPLICATION_JSON_VALUE
}
)
@PreAuthorize("hasAuthority('Editor')")
@ApiResponses(value = {
@ApiResponse(responseCode = "201", description = "Records validation status cleared."),
@ApiResponse(responseCode = "403", description = ApiParams.API_RESPONSE_NOT_ALLOWED_ONLY_EDITOR)
})
@ResponseStatus(HttpStatus.CREATED)
@ResponseBody
public SimpleMetadataProcessingReport cleanValidationStatus(
@Parameter(description = API_PARAM_RECORD_UUIDS_OR_SELECTION,
required = false,
example = "")
@RequestParam(required = false)
String[] uuids,
@Parameter(
description = ApiParams.API_PARAM_BUCKET_NAME,
required = false)
@RequestParam(
required = false
)
String bucket,
@Parameter(hidden = true)
HttpSession session,
@Parameter(hidden = true)
HttpServletRequest request
) throws Exception {
UserSession userSession = ApiUtils.getUserSession(session);

SimpleMetadataProcessingReport report =
new SimpleMetadataProcessingReport();
try {
ServiceContext serviceContext = ApiUtils.createServiceContext(request);

Set<String> records = ApiUtils.getUuidsParameterOrSelection(uuids, bucket, userSession);

for (String uuid : records) {
if (!metadataRepository.existsMetadataUuid(uuid)) {
report.incrementNullRecords();
}
for (AbstractMetadata record : metadataRepository.findAllByUuid(uuid)) {
if (!accessMan.canEdit(serviceContext, String.valueOf(record.getId()))) {
report.addNotEditableMetadataId(record.getId());
} else {
List<MetadataValidation> validationStatus = metadataValidationRepository.findAllById_MetadataId(record.getId());
metadataValidationRepository.deleteAll(validationStatus);
report.addMetadataId(record.getId());
report.incrementProcessedRecords();
}
}
}

// index records
BatchOpsMetadataReindexer r = new BatchOpsMetadataReindexer(dataMan, report.getMetadata());
r.process(true);
} catch (Exception e) {
throw e;
} finally {
report.close();
}
return report;
}


@io.swagger.v3.oas.annotations.Operation(summary = "Validate one or more records in INSPIRE validator",
description = "Update validation status for all records.")
@RequestMapping(
Expand Down Expand Up @@ -220,6 +294,11 @@ public ResponseEntity validateRecordsInspire(
required = false
)
String bucket,
@Parameter(
description = API_PARAM_INSPIRE_VALIDATION_MODE,
required = false)
@RequestParam(required = false)
String mode,
@Parameter(hidden = true)
HttpSession session,
@Parameter(hidden = true)
Expand All @@ -235,7 +314,7 @@ public ResponseEntity validateRecordsInspire(

Set<String> records = ApiUtils.getUuidsParameterOrSelection(uuids, bucket, userSession);

registredMAnalyseProcess.processMetadata(records);
registredMAnalyseProcess.processMetadata(records, mode);
return new ResponseEntity(HttpStatus.CREATED);
}

Expand Down
Loading