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 18, 2023
1 parent a87a2d9 commit 93acaca
Show file tree
Hide file tree
Showing 52 changed files with 959 additions and 669 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
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,11 @@
<artifactId>rdf4j-sail-nativerdf</artifactId>
<version>${rdf4j.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.rdf4j</groupId>
<artifactId>rdf4j-tools-federation</artifactId>
<version>${rdf4j.version}</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
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,30 @@ 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. Filter children
// TODO: get drafts only if permissions
// 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);
// }
// }
// }

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

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

Expand All @@ -225,7 +212,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 +327,14 @@ 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);
resultRdf.addAll(entity);

// 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,19 +343,21 @@ 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, mode);

// 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());
Expand All @@ -382,14 +366,14 @@ public ResponseEntity<Model> getMetaDataChildren(
})
.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 @@ -94,6 +94,7 @@ public MetaDTO getMeta(

// 3. Get and check existence entity
IRI entityUri = getMetadataIRI(persistentUrl, urlPrefix, recordId);
// TODO COMBINED/DRAFT
Model entity = metadataService.retrieve(entityUri);

// 4. Get member
Expand All @@ -103,7 +104,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 93acaca

Please sign in to comment.