DescriptionTemplate refactor

This commit is contained in:
Efstratios Giannopoulos 2023-11-03 15:53:18 +02:00
parent 08caf64d02
commit 9cf91aab50
4 changed files with 56 additions and 15 deletions

View File

@ -2,6 +2,7 @@ package eu.eudat.model.persist;
import eu.eudat.commons.enums.DescriptionTemplateStatus; import eu.eudat.commons.enums.DescriptionTemplateStatus;
import eu.eudat.commons.enums.UserDescriptionTemplateRole;
import eu.eudat.commons.validation.FieldNotNullIfOtherSet; import eu.eudat.commons.validation.FieldNotNullIfOtherSet;
import eu.eudat.commons.validation.ValidEnum; import eu.eudat.commons.validation.ValidEnum;
import eu.eudat.commons.validation.ValidId; import eu.eudat.commons.validation.ValidId;
@ -45,8 +46,8 @@ public class DescriptionTemplatePersist {
@Valid @Valid
private DefinitionPersist definition = null; private DefinitionPersist definition = null;
private List<UUID> users = null; @Valid
private List<UserDescriptionTemplatePersist> users = null;
private String hash; private String hash;
@ -114,12 +115,13 @@ public class DescriptionTemplatePersist {
this.definition = definition; this.definition = definition;
} }
public List<UUID> getUsers() { public List<UserDescriptionTemplatePersist> getUsers() {
return users; return users;
} }
public void setUsers(List<UUID> users) { public void setUsers(List<UserDescriptionTemplatePersist> users) {
this.users = users; this.users = users;
} }
} }

View File

@ -44,7 +44,9 @@ public class NewVersionDescriptionTemplatePersist {
@NotNull(message = "{validation.empty}") @NotNull(message = "{validation.empty}")
@Valid @Valid
private DefinitionPersist definition = null; private DefinitionPersist definition = null;
private List<UUID> users = null;
@Valid
private List<UserDescriptionTemplatePersist> users = null;
private String hash; private String hash;
@ -112,11 +114,11 @@ public class NewVersionDescriptionTemplatePersist {
this.definition = definition; this.definition = definition;
} }
public List<UUID> getUsers() { public List<UserDescriptionTemplatePersist> getUsers() {
return users; return users;
} }
public void setUsers(List<UUID> users) { public void setUsers(List<UserDescriptionTemplatePersist> users) {
this.users = users; this.users = users;
} }
} }

View File

@ -0,0 +1,36 @@
package eu.eudat.model.persist;
import eu.eudat.commons.enums.UserDescriptionTemplateRole;
import eu.eudat.commons.validation.FieldNotNullIfOtherSet;
import eu.eudat.commons.validation.ValidEnum;
import eu.eudat.commons.validation.ValidId;
import jakarta.validation.constraints.NotNull;
import java.util.UUID;
@FieldNotNullIfOtherSet(message = "{validation.hashempty}")
public class UserDescriptionTemplatePersist {
@NotNull(message = "{validation.empty}")
@ValidId(message = "{validation.invalidid}")
private UUID userId = null;
@ValidEnum(message = "{validation.empty}")
private UserDescriptionTemplateRole role;
public UUID getUserId() {
return userId;
}
public void setUserId(UUID userId) {
this.userId = userId;
}
public UserDescriptionTemplateRole getRole() {
return role;
}
public void setRole(UserDescriptionTemplateRole role) {
this.role = role;
}
}

View File

@ -28,6 +28,7 @@ import eu.eudat.model.descriptiontemplatedefinition.Page;
import eu.eudat.model.descriptiontemplatedefinition.Section; import eu.eudat.model.descriptiontemplatedefinition.Section;
import eu.eudat.model.persist.DescriptionTemplatePersist; import eu.eudat.model.persist.DescriptionTemplatePersist;
import eu.eudat.model.persist.NewVersionDescriptionTemplatePersist; import eu.eudat.model.persist.NewVersionDescriptionTemplatePersist;
import eu.eudat.model.persist.UserDescriptionTemplatePersist;
import eu.eudat.model.persist.descriptiontemplatedefinition.*; import eu.eudat.model.persist.descriptiontemplatedefinition.*;
import eu.eudat.model.persist.descriptiontemplatedefinition.fielddata.BaseFieldDataPersist; import eu.eudat.model.persist.descriptiontemplatedefinition.fielddata.BaseFieldDataPersist;
import eu.eudat.query.DescriptionTemplateQuery; import eu.eudat.query.DescriptionTemplateQuery;
@ -171,26 +172,26 @@ public class DescriptionTemplateServiceImpl implements DescriptionTemplateServic
return this.builderFactory.builder(DescriptionTemplateBuilder.class).authorize(AuthorizationFlags.OwnerOrPermission).build(BaseFieldSet.build(fields, DescriptionTemplate._id), data); return this.builderFactory.builder(DescriptionTemplateBuilder.class).authorize(AuthorizationFlags.OwnerOrPermission).build(BaseFieldSet.build(fields, DescriptionTemplate._id), data);
} }
private void persistUsers(UUID id, List<UUID> users) throws InvalidApplicationException { private void persistUsers(UUID id, List<UserDescriptionTemplatePersist> users) throws InvalidApplicationException {
if (users == null) users = new ArrayList<>(); if (users == null) users = new ArrayList<>();
List<UserDescriptionTemplateEntity> items = this.queryFactory.query(UserDescriptionTemplateQuery.class).isActive(IsActive.Active).descriptionTemplateIds(id).collect(); List<UserDescriptionTemplateEntity> items = this.queryFactory.query(UserDescriptionTemplateQuery.class).isActive(IsActive.Active).descriptionTemplateIds(id).collect();
for (UUID userId : users) { List<UUID> updatedCreatedIds = new ArrayList<>();
UserDescriptionTemplateEntity data = items.stream().filter(x -> x.getUser() == userId).findFirst().orElse(null); for (UserDescriptionTemplatePersist user : users) {
UserDescriptionTemplateEntity data = items.stream().filter(x -> x.getUser() == user.getUserId() && x.getRole() == user.getRole()).findFirst().orElse(null);
if (data == null){ if (data == null){
data = new UserDescriptionTemplateEntity(); data = new UserDescriptionTemplateEntity();
data.setId(UUID.randomUUID()); data.setId(UUID.randomUUID());
data.setIsActive(IsActive.Active); data.setIsActive(IsActive.Active);
data.setCreatedAt(Instant.now()); data.setCreatedAt(Instant.now());
data.setDescriptionTemplate(id); data.setDescriptionTemplate(id);
data.setUser(userId); data.setUser(user.getUserId());
data.setUser(userId); data.setRole(user.getRole());
data.setRole(UserDescriptionTemplateRole.Member);
this.entityManager.persist(data); this.entityManager.persist(data);
this.sendJoinMail(data); this.sendJoinMail(data);
} }
updatedCreatedIds.add(data.getId());
} }
List<UUID> finalUsers = users; List<UserDescriptionTemplateEntity> toDelete = items.stream().filter(x-> updatedCreatedIds.stream().noneMatch(y-> y.equals(x.getId()))).collect(Collectors.toList());
List<UserDescriptionTemplateEntity> toDelete = items.stream().filter(x-> finalUsers.stream().noneMatch(y-> y.equals(x.getUser()))).collect(Collectors.toList());
this.deleterFactory.deleter(UserDescriptionTemplateDeleter.class).delete(toDelete); this.deleterFactory.deleter(UserDescriptionTemplateDeleter.class).delete(toDelete);
} }