Skip to content

Commit

Permalink
WIP: Use dedicated repository for drafts
Browse files Browse the repository at this point in the history
  • Loading branch information
MarekSuchanek committed Nov 24, 2023
1 parent a87a2d9 commit f1df095
Show file tree
Hide file tree
Showing 54 changed files with 1,021 additions and 756 deletions.
2 changes: 1 addition & 1 deletion checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@
<module name="MutableException"/>
<module name="OneTopLevelClass"/>
<module name="ThrowsCount">
<property name="max" value="2"/>
<property name="max" value="4"/>
</module>
<module name="VisibilityModifier">
<property name="ignoreAnnotationCanonicalNames"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import nl.dtls.fairdatapoint.database.rdf.repository.RepositoryMode;
import nl.dtls.fairdatapoint.database.rdf.repository.exception.MetadataRepositoryException;
import nl.dtls.fairdatapoint.database.rdf.repository.generic.GenericMetadataRepository;
import nl.dtls.fairdatapoint.entity.exception.ForbiddenException;
import nl.dtls.fairdatapoint.entity.exception.ValidationException;
import nl.dtls.fairdatapoint.entity.metadata.Metadata;
import nl.dtls.fairdatapoint.entity.metadata.MetadataState;
import nl.dtls.fairdatapoint.entity.resource.ResourceDefinition;
import nl.dtls.fairdatapoint.entity.resource.ResourceDefinitionChild;
import nl.dtls.fairdatapoint.entity.user.User;
Expand Down Expand Up @@ -73,9 +72,6 @@
@RequestMapping("/")
public class GenericController {

private static final String MSG_ERROR_DRAFT_FORBIDDEN =
"You are not allow to view this record in state DRAFT";

@Autowired
@Qualifier("persistentUrl")
private String persistentUrl;
Expand All @@ -89,9 +85,6 @@ public class GenericController {
@Autowired
private MetadataSchemaService metadataSchemaService;

@Autowired
private MetadataStateService metadataStateService;

@Autowired
private MetadataEnhancer metadataEnhancer;

Expand All @@ -101,6 +94,9 @@ public class GenericController {
@Autowired
private GenericMetadataRepository metadataRepository;

@Autowired
private MetadataStateService metadataStateService;

@Autowired
private SearchFilterCache searchFilterCache;

Expand Down Expand Up @@ -130,28 +126,23 @@ public Model getMetaDataExpanded(
final MetadataService metadataService = metadataServiceFactory.getMetadataServiceByUrlPrefix(urlPrefix);
final ResourceDefinition rd = resourceDefinitionService.getByUrlPrefix(urlPrefix);

// 2. Get entity
// 2. Get entity (from repository based on permissions)
final Optional<User> oCurrentUser = currentUserService.getCurrentUser();
IRI entityUri = getMetadataIRI(persistentUrl, urlPrefix, recordId);
Model entity = metadataService.retrieve(entityUri);
final RepositoryMode mode = oCurrentUser.isEmpty() ? RepositoryMode.MAIN : RepositoryMode.COMBINED;
Model entity = metadataService.retrieve(entityUri, mode);
resultRdf.addAll(entity);

// 3. Check if it is DRAFT
final Metadata state = metadataStateService.get(entityUri);
final Optional<User> oCurrentUser = currentUserService.getCurrentUser();
if (state.getState().equals(MetadataState.DRAFT) && oCurrentUser.isEmpty()) {
throw new ForbiddenException(MSG_ERROR_DRAFT_FORBIDDEN);
}

// 4. Enhance
// 3. Enhance
metadataEnhancer.enhanceWithResourceDefinition(entityUri, rd, resultRdf);

// 5. Get parent
// 4. Get parent
while (true) {
final IRI parentUri = i(getStringObjectBy(entity, entityUri, DCTERMS.IS_PART_OF));
if (parentUri == null) {
break;
}
final Model parent = metadataService.retrieve(parentUri);
final Model parent = metadataService.retrieve(parentUri, mode);
resultRdf.addAll(parent);
entity = parent;
entityUri = parentUri;
Expand Down Expand Up @@ -179,34 +170,18 @@ public Model getMetaData(
// 2. Get resource definition
final ResourceDefinition rd = resourceDefinitionService.getByUrlPrefix(urlPrefix);

// 3. Get entity
// 3. Get entity (from repository based on permissions)
final Optional<User> oCurrentUser = currentUserService.getCurrentUser();
final IRI entityUri = getMetadataIRI(persistentUrl, urlPrefix, recordId);
final Model entity = metadataService.retrieve(entityUri);
final RepositoryMode mode = oCurrentUser.isEmpty() ? RepositoryMode.MAIN : RepositoryMode.COMBINED;
final Model entity = metadataService.retrieve(entityUri, mode);
resultRdf.addAll(entity);

// 4. Check if it is DRAFT
final Metadata state = metadataStateService.get(entityUri);
final Optional<User> oCurrentUser = currentUserService.getCurrentUser();
if (state.getState().equals(MetadataState.DRAFT) && oCurrentUser.isEmpty()) {
throw new ForbiddenException(MSG_ERROR_DRAFT_FORBIDDEN);
}

// 5. Filter children
for (ResourceDefinitionChild rdChild : rd.getChildren()) {
final IRI relationUri = i(rdChild.getRelationUri());
for (org.eclipse.rdf4j.model.Value childUri : getObjectsBy(entity, entityUri, relationUri)) {
final Metadata childState = metadataStateService.get(i(childUri.stringValue()));
if (!(childState.getState().equals(MetadataState.PUBLISHED) || oCurrentUser.isPresent())) {
resultRdf.remove(entityUri, relationUri, childUri);
}
}
}

// 6. Add links
// 4. Add links
metadataEnhancer.enhanceWithLinks(entityUri, entity, rd, persistentUrl, resultRdf);
metadataEnhancer.enhanceWithResourceDefinition(entityUri, rd, resultRdf);

// 7. Create response
// 5. Create response
return resultRdf;
}

Expand All @@ -225,7 +200,6 @@ public ResponseEntity<Model> storeMetaData(
}

// 2. Init
// String urlPrefix = getResourceNameForList(getRequestURL(request, persistentUrl));
final MetadataService metadataService = metadataServiceFactory.getMetadataServiceByUrlPrefix(urlPrefix);
final ResourceDefinition rd = resourceDefinitionService.getByUrlPrefix(urlPrefix);

Expand Down Expand Up @@ -341,18 +315,13 @@ public ResponseEntity<Model> getMetaDataChildren(
final String recordId = oRecordId.orElse("");
final MetadataService metadataService = metadataServiceFactory.getMetadataServiceByUrlPrefix(urlPrefix);

// 2. Get entity
final IRI entityUri = getMetadataIRI(persistentUrl, urlPrefix, recordId);
final Model entity = metadataService.retrieve(entityUri);

// 3. Check if it is draft
final Metadata state = metadataStateService.get(entityUri);
// 2. Get entity (from repository based on permissions)
final Optional<User> oCurrentUser = currentUserService.getCurrentUser();
if (state.getState().equals(MetadataState.DRAFT) && oCurrentUser.isEmpty()) {
throw new ForbiddenException(MSG_ERROR_DRAFT_FORBIDDEN);
}
final IRI entityUri = getMetadataIRI(persistentUrl, urlPrefix, recordId);
final RepositoryMode mode = oCurrentUser.isEmpty() ? RepositoryMode.MAIN : RepositoryMode.COMBINED;
final Model entity = metadataService.retrieve(entityUri, mode);

// 4. Get Children
// 3. Get Children
final ResourceDefinition rd = resourceDefinitionService.getByUrlPrefix(urlPrefix);
final ResourceDefinition currentChildRd = resourceDefinitionService.getByUrlPrefix(childPrefix);
final MetadataService childMetadataService = metadataServiceFactory.getMetadataServiceByUrlPrefix(childPrefix);
Expand All @@ -361,35 +330,41 @@ public ResponseEntity<Model> getMetaDataChildren(
if (rdChild.getResourceDefinitionUuid().equals(currentChildRd.getUuid())) {
final IRI relationUri = i(rdChild.getRelationUri());

// 4.1 Get all titles for sort
final Map<String, String> titles = metadataRepository.findChildTitles(entityUri, relationUri);
// 3.1 Get all titles for sort
final Map<String, String> titles =
metadataRepository.findChildTitles(entityUri, relationUri, RepositoryMode.COMBINED);

// 4.2 Get all children sorted
// 3.2 Get all children sorted
final List<Value> children = getObjectsBy(entity, entityUri, relationUri)
.stream()
.filter(childUri -> getResourceNameForChild(childUri.toString()).equals(childPrefix))
.filter(childUri -> {
if (oCurrentUser.isPresent()) {
return true;
try {
return oCurrentUser.isPresent()
|| metadataStateService.isPublished(i(childUri.stringValue()));
}
catch (MetadataServiceException exc) {
return false;
}
final Metadata childState = metadataStateService.get(i(childUri.stringValue()));
return childState.getState().equals(MetadataState.PUBLISHED);
})
.sorted((value1, value2) -> {
final String title1 = titles.get(value1.toString());
final String title2 = titles.get(value2.toString());
if (title1 == null) {
return -1;
}
return title1.compareTo(title2);
})
.toList();

// 4.3 Retrieve children metadata only for requested page
// 3.3 Retrieve children metadata only for requested page
final int childrenCount = children.size();
children.stream().skip((long) page * size).limit(size)
.map(childUri -> retrieveChildModel(childMetadataService, childUri))
.flatMap(Optional::stream)
.forEach(resultRdf::addAll);

// 4.4 Set Link headers and send response
// 3.4 Set Link headers and send response
final HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set(
"Link",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,17 @@
import nl.dtls.fairdatapoint.api.dto.metadata.MetaPathDTO;
import nl.dtls.fairdatapoint.api.dto.metadata.MetaStateChangeDTO;
import nl.dtls.fairdatapoint.api.dto.metadata.MetaStateDTO;
import nl.dtls.fairdatapoint.database.rdf.repository.RepositoryMode;
import nl.dtls.fairdatapoint.entity.metadata.Metadata;
import nl.dtls.fairdatapoint.entity.resource.ResourceDefinition;
import nl.dtls.fairdatapoint.entity.user.User;
import nl.dtls.fairdatapoint.service.member.MemberService;
import nl.dtls.fairdatapoint.service.metadata.common.MetadataService;
import nl.dtls.fairdatapoint.service.metadata.exception.MetadataServiceException;
import nl.dtls.fairdatapoint.service.metadata.factory.MetadataServiceFactory;
import nl.dtls.fairdatapoint.service.metadata.state.MetadataStateService;
import nl.dtls.fairdatapoint.service.resource.ResourceDefinitionService;
import nl.dtls.fairdatapoint.service.user.CurrentUserService;
import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Model;
import org.eclipse.rdf4j.model.vocabulary.DCTERMS;
Expand Down Expand Up @@ -77,6 +80,9 @@ public class GenericMetaController {
@Autowired
private ResourceDefinitionService resourceDefinitionService;

@Autowired
private CurrentUserService currentUserService;

@Operation(hidden = true)
@GetMapping(path = {"meta", "{oUrlPrefix:[^.]+}/{oRecordId:[^.]+}/meta"})
public MetaDTO getMeta(
Expand All @@ -93,8 +99,10 @@ public MetaDTO getMeta(
ResourceDefinition definition = resourceDefinitionService.getByUrlPrefix(urlPrefix);

// 3. Get and check existence entity
final Optional<User> oCurrentUser = currentUserService.getCurrentUser();
final RepositoryMode mode = oCurrentUser.isEmpty() ? RepositoryMode.MAIN : RepositoryMode.COMBINED;
IRI entityUri = getMetadataIRI(persistentUrl, urlPrefix, recordId);
Model entity = metadataService.retrieve(entityUri);
Model entity = metadataService.retrieve(entityUri, mode);

// 4. Get member
final String entityId = getMetadataIdentifier(entity).getIdentifier().getLabel();
Expand All @@ -103,7 +111,7 @@ public MetaDTO getMeta(
final MemberDTO member = oMember.orElse(new MemberDTO(null, null));

// 5. Get state
final MetaStateDTO state = metadataStateService.getState(entityUri, entity, definition);
final MetaStateDTO state = metadataStateService.getStateDTO(entityUri, entity, definition);

// 6. Make path map
final Map<String, MetaPathDTO> pathMap = new HashMap<>();
Expand Down
Loading

0 comments on commit f1df095

Please sign in to comment.