Skip to content

Commit

Permalink
Merge pull request #3232 from keeps/cafonso-dev-fixAIP2
Browse files Browse the repository at this point in the history
Fixed AIPs and isSimilar feature
  • Loading branch information
hmiguim authored Jul 3, 2024
2 parents f2d25c1 + 04f12bc commit c4642c7
Show file tree
Hide file tree
Showing 10 changed files with 212 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,4 @@ public void setPermissions(Permissions permissions) {
this.permissions = permissions;
}

public abstract String getXml();

public abstract void setXml(String xml);

public abstract Set<MetadataValue> getValues();
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,7 @@ public DescriptiveMetadataRequestForm(String id, String filename, String type, S
this.values = values;
}

@Override
public String getXml() {
return null;
}

@Override
public void setXml(String xml) {
// do nothing
}

@Override
public Set<MetadataValue> getValues() {
return values;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,12 @@ public DescriptiveMetadataRequestXML(String id, String filename, String type, St
this.xml = xml;
}

@Override
public String getXml() {
return xml;
}

@Override
public void setXml(String xml) {
this.xml = xml;
}

@Override
public Set<MetadataValue> getValues() {
return Set.of();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.roda.core.data.v2.ip.metadata;

import java.io.Serial;
import java.util.Set;

import org.roda.core.data.v2.generics.MetadataValue;

public class SelectedType {

@Serial
private static final long serialVersionUID = 8626922937067856615L;

private String metadataId;

private Set<MetadataValue> value;

public SelectedType() {
// do nothing
}

public SelectedType(String metadataId, Set<MetadataValue> value) {
this.metadataId = metadataId;
this.value = value;
}

public String getMetadataId() {
return metadataId;
}

public void setMetadataId(String metadataId) {
this.metadataId = metadataId;
}

public Set<MetadataValue> getValue() {
return value;
}

public void setValue(Set<MetadataValue> value) {
this.value = value;
}

@Override
public String toString() {
return "SupportedMetadataValue{" + "value=" + value + '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ core.permissions.org.roda.wui.api.v2.controller.AIPController.getDocumentation =
core.permissions.org.roda.wui.api.v2.controller.AIPController.getSubmissions = READ
core.permissions.org.roda.wui.api.v2.controller.AIPController.retrieveRepresentationSupportedMetadata = READ
core.permissions.org.roda.wui.api.v2.controller.AIPController.createRepresentationDescriptiveMetadata = CREATE
core.permissions.org.roda.wui.api.v2.controller.AIPController.isAIPMetadataSimilar = READ
core.permissions.org.roda.wui.api.v2.controller.AIPController.isRepresentationMetadataSimilar = READ

# Representation permissions
core.permissions.org.roda.wui.api.v2.controller.RepresentationController.createRepresentation = CREATE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ core.roles.org.roda.wui.api.v2.controller.AIPController.createRepresentationDesc
core.roles.org.roda.wui.api.v2.controller.AIPController.updateAIPDescriptiveMetadataFile = descriptive_metadata.update
core.roles.org.roda.wui.api.v2.controller.AIPController.updateRepresentationDescriptiveMetadataFile = descriptive_metadata.update
core.roles.org.roda.wui.api.v2.controller.AIPController.retrieveRepresentationDescriptiveMetadataVersions = descriptive_metadata.read
core.roles.org.roda.wui.api.v2.controller.AIPController.isAIPMetadataSimilar = descriptive_metadata.read
core.roles.org.roda.wui.api.v2.controller.AIPController.isRepresentationMetadataSimilar = descriptive_metadata.read


# Transferred resource roles
core.roles.org.roda.wui.api.v2.controller.TransferredResourceController.createTransferredResourcesFolder = transfer.create
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@
import org.roda.core.data.v2.ip.metadata.DescriptiveMetadataPreview;
import org.roda.core.data.v2.ip.metadata.DescriptiveMetadataPreviewRequest;
import org.roda.core.data.v2.ip.metadata.DescriptiveMetadataRequestForm;
import org.roda.core.data.v2.ip.metadata.DescriptiveMetadataRequestXML;
import org.roda.core.data.v2.ip.metadata.DescriptiveMetadataVersions;
import org.roda.core.data.v2.ip.metadata.SelectedType;
import org.roda.core.data.v2.ip.metadata.SupportedMetadataValue;
import org.roda.core.data.v2.ip.metadata.TypeOptionsInfo;
import org.roda.core.data.v2.jobs.Job;
Expand Down Expand Up @@ -623,9 +625,9 @@ public DescriptiveMetadata createRepresentationDescriptiveMetadata(String aipId,

if (createDescriptiveMetadataRequest instanceof DescriptiveMetadataRequestForm) {
payload = new StringContentPayload(aipService.retrieveDescriptiveMetadataPreview(aipId, representationId,
metadataId, createDescriptiveMetadataRequest.getValues()));
metadataId, ((DescriptiveMetadataRequestForm) createDescriptiveMetadataRequest).getValues()));
} else {
payload = new StringContentPayload(createDescriptiveMetadataRequest.getXml());
payload = new StringContentPayload(((DescriptiveMetadataRequestXML)createDescriptiveMetadataRequest).getXml());
}

// delegate
Expand Down Expand Up @@ -730,9 +732,9 @@ public DescriptiveMetadata createAIPDescriptiveMetadata(String aipId,

if (descriptiveMetadataRequest instanceof DescriptiveMetadataRequestForm) {
payload = new StringContentPayload(aipService.retrieveDescriptiveMetadataPreview(aipId, null, metadataId,
descriptiveMetadataRequest.getValues()));
((DescriptiveMetadataRequestForm) descriptiveMetadataRequest).getValues()));
} else {
payload = new StringContentPayload(descriptiveMetadataRequest.getXml());
payload = new StringContentPayload(((DescriptiveMetadataRequestXML) descriptiveMetadataRequest).getXml());
}

// delegate
Expand Down Expand Up @@ -1136,6 +1138,63 @@ public DescriptiveMetadataPreview retrieveDescriptiveMetadataPreview(String aipI
}
}

@Override
public boolean isRepresentationMetadataSimilar(String aipId, String representationId, String metadataId,
@RequestBody SelectedType selectedType) {
final ControllerAssistant controllerAssistant = new ControllerAssistant() {};
RequestContext requestContext = RequestUtils.parseHTTPRequest(request);
LogEntryState state = LogEntryState.SUCCESS;

try {
// check user permissions
controllerAssistant.checkRoles(requestContext.getUser());

// check object permissions
IndexedAIP aip = RodaCoreFactory.getIndexService().retrieve(IndexedAIP.class, aipId,
RodaConstants.AIP_PERMISSIONS_FIELDS_TO_RETURN);
controllerAssistant.checkObjectPermissions(requestContext.getUser(), aip);

// delegate
return aipService.isMetadataSimilar(aip, representationId, metadataId, selectedType);
} catch (RODAException e) {
state = LogEntryState.FAILURE;
throw new RESTException(e);
} finally {
// register action
controllerAssistant.registerAction(requestContext, state, RodaConstants.CONTROLLER_TEMPLATE_PARAM,
selectedType);
}

}

@Override
public boolean isAIPMetadataSimilar(String aipId, String metadataId,
@RequestBody SelectedType selectedType) {
final ControllerAssistant controllerAssistant = new ControllerAssistant() {};
RequestContext requestContext = RequestUtils.parseHTTPRequest(request);
LogEntryState state = LogEntryState.SUCCESS;

try {
// check user permissions
controllerAssistant.checkRoles(requestContext.getUser());

// check object permissions
IndexedAIP aip = RodaCoreFactory.getIndexService().retrieve(IndexedAIP.class, aipId,
RodaConstants.AIP_PERMISSIONS_FIELDS_TO_RETURN);
controllerAssistant.checkObjectPermissions(requestContext.getUser(), aip);

// delegate
return aipService.isMetadataSimilar(aip, null, metadataId, selectedType);
} catch (RODAException e) {
state = LogEntryState.FAILURE;
throw new RESTException(e);
} finally {
// register action
controllerAssistant.registerAction(requestContext, state, RodaConstants.CONTROLLER_TEMPLATE_PARAM,
selectedType);
}
}

@Override
public boolean requestAIPLock(String aipId) {
boolean lockEnabled = RodaCoreFactory.getRodaConfiguration().getBoolean("core.aip.lockToEdit", false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@
import org.roda.core.data.v2.ip.metadata.DescriptiveMetadata;
import org.roda.core.data.v2.ip.metadata.DescriptiveMetadataInfo;
import org.roda.core.data.v2.ip.metadata.DescriptiveMetadataInfos;
import org.roda.core.data.v2.ip.metadata.DescriptiveMetadataRequestXML;
import org.roda.core.data.v2.ip.metadata.DescriptiveMetadataVersions;
import org.roda.core.data.v2.ip.metadata.LinkingIdentifier;
import org.roda.core.data.v2.ip.metadata.PreservationMetadata;
import org.roda.core.data.v2.ip.metadata.ResourceVersion;
import org.roda.core.data.v2.ip.metadata.SelectedType;
import org.roda.core.data.v2.ip.metadata.SupportedMetadataValue;
import org.roda.core.data.v2.jobs.Job;
import org.roda.core.data.v2.synchronization.central.DistributedInstance;
Expand Down Expand Up @@ -90,6 +92,10 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.xmlunit.builder.DiffBuilder;
import org.xmlunit.diff.DefaultNodeMatcher;
import org.xmlunit.diff.Diff;
import org.xmlunit.diff.ElementSelectors;

/**
* @author Miguel Guimarães <[email protected]>
Expand Down Expand Up @@ -395,7 +401,7 @@ public String retrieveDescriptiveMetadataPreview(String aipId, String representa
throws GenericException, AuthorizationDeniedException, RequestNotValidException, NotFoundException {
String rawTemplate;

String result;
String result = "";
try (
InputStream templateStream = RodaCoreFactory.getConfigurationFileAsStream(RodaConstants.METADATA_TEMPLATE_FOLDER
+ "/" + descriptiveMetadataId + RodaConstants.METADATA_TEMPLATE_EXTENSION)) {
Expand All @@ -418,10 +424,15 @@ public String retrieveDescriptiveMetadataPreview(String aipId, String representa

result = HandlebarsUtility.executeHandlebars(rawTemplate, data);
} else {
Binary binary = RodaCoreFactory.getModelService().retrieveDescriptiveMetadataBinary(aipId, representationId,
descriptiveMetadataId + XML_EXT);
InputStream inputStream = binary.getContent().createInputStream();
result = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
StorageService ss = RodaCoreFactory.getModelService().getStorage();
if (ss.exists(
ModelUtils.getDescriptiveMetadataStoragePath(aipId, representationId, descriptiveMetadataId + XML_EXT))) {

Binary binary = RodaCoreFactory.getModelService().retrieveDescriptiveMetadataBinary(aipId, representationId,
descriptiveMetadataId + XML_EXT);
InputStream inputStream = binary.getContent().createInputStream();
result = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
}
}
} catch (IOException e) {
throw new GenericException(e);
Expand Down Expand Up @@ -482,7 +493,7 @@ public SupportedMetadataValue retrieveSupportedMetadata(User user, IndexedAIP ai
InputStream templateStream = RodaCoreFactory.getConfigurationFileAsStream(RodaConstants.METADATA_TEMPLATE_FOLDER
+ "/" + descriptiveMetadataId + RodaConstants.METADATA_TEMPLATE_EXTENSION)) {

if (checkIfDescriptiveMetadataExists(aip, representation)) {
if (checkIfDescriptiveMetadataExists(aip, representation, descriptiveMetadataId + XML_EXT)) {
template = IOUtils.toString(templateStream, StandardCharsets.UTF_8);
String representationId = representation != null ? representation.getId() : null;
Binary binary = RodaCoreFactory.getModelService().retrieveDescriptiveMetadataBinary(aip.getId(),
Expand All @@ -507,16 +518,30 @@ public SupportedMetadataValue retrieveSupportedMetadata(User user, IndexedAIP ai
return new SupportedMetadataValue(values);
}

private boolean checkIfDescriptiveMetadataExists(IndexedAIP aip, IndexedRepresentation representation)
private boolean checkIfDescriptiveMetadataExists(IndexedAIP aip, IndexedRepresentation representation,
String descriptiveMetadaId)
throws AuthorizationDeniedException, RequestNotValidException, NotFoundException, GenericException {
if (representation != null) {
Representation modelRepresentation = RodaCoreFactory.getModelService().retrieveRepresentation(aip.getId(),
representation.getId());
return !modelRepresentation.getDescriptiveMetadata().isEmpty();
if (!modelRepresentation.getDescriptiveMetadata().isEmpty()) {
for (DescriptiveMetadata dm : modelRepresentation.getDescriptiveMetadata()) {
if (dm.getId().equals(descriptiveMetadaId)) {
return true;
}
}
}
} else {
AIP aipModel = RodaCoreFactory.getModelService().retrieveAIP(aip.getId());
return !aipModel.getDescriptiveMetadata().isEmpty();
if (!aipModel.getDescriptiveMetadata().isEmpty()) {
for (DescriptiveMetadata dm : aipModel.getDescriptiveMetadata()) {
if (dm.getId().equals(descriptiveMetadaId)) {
return true;
}
}
}
}
return false;
}

private Set<MetadataValue> getDefaultDescriptiveMetadataValues(String template, IndexedAIP aip,
Expand Down Expand Up @@ -719,7 +744,8 @@ public DescriptiveMetadata updateDescriptiveMetadataFile(User user, String aipId
String filename = request.getFilename();
String descriptiveMetadataType = request.getType();
String descriptiveMetadataVersion = request.getVersion();
StringContentPayload descriptiveMetadataPayload = new StringContentPayload(request.getXml());
StringContentPayload descriptiveMetadataPayload = new StringContentPayload(
((DescriptiveMetadataRequestXML) request).getXml());

ValidationReport report = ValidationUtils.validateDescriptiveBinary(descriptiveMetadataPayload,
descriptiveMetadataType, descriptiveMetadataVersion, false);
Expand Down Expand Up @@ -810,4 +836,32 @@ public void deleteDescriptiveMetadataVersion(String aipId, String representation
descriptiveMetadataId);
RodaCoreFactory.getStorageService().deleteBinaryVersion(storagePath, versionId);
}

public boolean isMetadataSimilar(IndexedAIP aip, String representationId, String metadataId,
SelectedType selectedType)
throws AuthorizationDeniedException, RequestNotValidException, NotFoundException, GenericException {
boolean isSimilar = false;

try {
Binary binary = RodaCoreFactory.getModelService().retrieveDescriptiveMetadataBinary(aip.getId(), representationId,
metadataId + XML_EXT);
InputStream inputStream = binary.getContent().createInputStream();
String xml = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
String selectedTypeTemplate = retrieveDescriptiveMetadataPreview(aip.getId(), representationId,
selectedType.getMetadataId(), selectedType.getValue());

if (selectedTypeTemplate != "") {
Diff diff = DiffBuilder.compare(xml).withTest(selectedTypeTemplate).ignoreComments().ignoreWhitespace()
.checkForIdentical().checkForSimilar().withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText))
.withNodeFilter(node -> !node.getNodeName().equals("schemaLocation")).build();

isSimilar = !diff.hasDifferences();
}

} catch (IOException e) {
throw new GenericException(e);
}
return isSimilar;
}

}
Loading

0 comments on commit c4642c7

Please sign in to comment.