Skip to content
Snippets Groups Projects

Ozg 3936 refactor user profile url provider

Merged Felix Reichenbach requested to merge OZG-3936-refactor-UserProfileUrlProvider into main
6 files
+ 44
78
Compare changes
  • Side-by-side
  • Inline
Files
6
@@ -51,13 +51,13 @@ public class LinkedResourceProcessor<T> implements RepresentationModelProcessor<
@Override
public EntityModel<T> process(EntityModel<T> model) {
addLinkByLinkedResourceAnnotationIfMissing(model);
addLinkByLinkedUserProfileResourceAnnotationIfMissing(model);
addResourceLinksIfMissing(model);
addUserProfileLinksIfMissing(model);
return model;
}
void addLinkByLinkedResourceAnnotationIfMissing(EntityModel<T> model) {
getFields(LinkedResource.class, model.getContent())
void addResourceLinksIfMissing(EntityModel<T> model) {
getAnnotatedFields(LinkedResource.class, model.getContent())
.filter(field -> shouldAddLink(model, field))
.forEach(field -> addLinkForLinkedResourceField(model, field));
}
@@ -66,16 +66,16 @@ public class LinkedResourceProcessor<T> implements RepresentationModelProcessor<
getEntityFieldValue(model.getContent(), field).map(Object::toString).filter(StringUtils::isNotBlank)
.ifPresent(val -> model
.add(WebMvcLinkBuilder.linkTo(field.getAnnotation(LinkedResource.class).controllerClass()).slash(val)
.withRel(trimIdSuffix(field.getName()))));
.withRel(getResourceName(field))));
}
void addLinkByLinkedUserProfileResourceAnnotationIfMissing(EntityModel<T> resource) {
getFields(LinkedUserProfileResource.class, resource.getContent())
void addUserProfileLinksIfMissing(EntityModel<T> resource) {
getAnnotatedFields(LinkedUserProfileResource.class, resource.getContent())
.filter(field -> shouldAddLink(resource, field))
.forEach(field -> addLinkForLinkedUserProfileResourceField(resource, field));
}
Stream<Field> getFields(Class<? extends Annotation> annotationClass, T content) {
Stream<Field> getAnnotatedFields(Class<? extends Annotation> annotationClass, T content) {
if (Objects.isNull(content)) {
return Stream.empty();
}
@@ -84,7 +84,8 @@ public class LinkedResourceProcessor<T> implements RepresentationModelProcessor<
}
boolean shouldAddLink(EntityModel<T> resource, Field field) {
return !(field.getType().isArray() || Collection.class.isAssignableFrom(field.getType()) || resource.hasLink(trimIdSuffix(field.getName())));
return !(field.getType().isArray() || Collection.class.isAssignableFrom(field.getType())
|| resource.hasLink(getResourceName(field)));
}
void addLinkForLinkedUserProfileResourceField(EntityModel<T> model, Field field) {
@@ -94,7 +95,7 @@ public class LinkedResourceProcessor<T> implements RepresentationModelProcessor<
private void addUserProfileLink(EntityModel<T> model, Field field, String value) {
Optional.ofNullable(userManagerUrlProvider.getUserProfileTemplate()).filter(StringUtils::isNotBlank)
.ifPresent(template -> model.add(Link.of(template.formatted(value)).withRel(trimIdSuffix(field.getName()))));
.ifPresent(template -> model.add(Link.of(template.formatted(value)).withRel(getResourceName(field))));
}
private Optional<Object> getEntityFieldValue(T content, Field field) {
@@ -109,7 +110,8 @@ public class LinkedResourceProcessor<T> implements RepresentationModelProcessor<
return Optional.empty();
}
private String trimIdSuffix(String fieldName) {
private String getResourceName(Field field) {
var fieldName = field.getName();
if (fieldName.endsWith("Id")) {
return fieldName.substring(0, fieldName.indexOf("Id"));
}
Loading