From 1b7ecf201ef2b915660ca2b5b89de5be26af04c3 Mon Sep 17 00:00:00 2001 From: Thomas Georgios Giannos Date: Wed, 13 Mar 2024 13:24:21 +0200 Subject: [PATCH] Getting annotation authors on query responses, ui update --- .../web/controllers/AnnotationController.java | 2 +- .../gr/cite/annotation/model/Annotation.java | 12 +++++ .../annotation/model/AnnotationAuthor.java | 31 ++++++++++++ .../model/builder/AnnotationBuilder.java | 47 ++++++++++++++++++- .../inbox/InboxRepositoryImpl.java | 40 ++++++++-------- .../annotation-dialog.component.html | 2 +- .../annotation-dialog.component.ts | 11 +++-- 7 files changed, 117 insertions(+), 28 deletions(-) create mode 100644 annotation-service/annotation/src/main/java/gr/cite/annotation/model/AnnotationAuthor.java diff --git a/annotation-service/annotation-web/src/main/java/gr/cite/annotation/web/controllers/AnnotationController.java b/annotation-service/annotation-web/src/main/java/gr/cite/annotation/web/controllers/AnnotationController.java index 88af9d2f1..f322246b3 100644 --- a/annotation-service/annotation-web/src/main/java/gr/cite/annotation/web/controllers/AnnotationController.java +++ b/annotation-service/annotation-web/src/main/java/gr/cite/annotation/web/controllers/AnnotationController.java @@ -72,7 +72,7 @@ public class AnnotationController { this.censorFactory.censor(AnnotationCensor.class).censor(lookup.getProject(), null); AnnotationQuery query = lookup.enrich(this.queryFactory); - List data = query.collectAs(lookup.getProject()); + List data = query.collect(); List models = this.builderFactory.builder(AnnotationBuilder.class).build(lookup.getProject(), data); long count = (lookup.getMetadata() != null && lookup.getMetadata().getCountAll()) ? query.count() : models.size(); diff --git a/annotation-service/annotation/src/main/java/gr/cite/annotation/model/Annotation.java b/annotation-service/annotation/src/main/java/gr/cite/annotation/model/Annotation.java index adec1d0a3..96a87126a 100644 --- a/annotation-service/annotation/src/main/java/gr/cite/annotation/model/Annotation.java +++ b/annotation-service/annotation/src/main/java/gr/cite/annotation/model/Annotation.java @@ -51,6 +51,10 @@ public class Annotation { public static final String _timeStamp = "timeStamp"; + private AnnotationAuthor author; + + public static final String _author = "author"; + private Instant createdAt; public static final String _createdAt = "createdAt"; @@ -111,6 +115,14 @@ public class Annotation { this.subjectId = subjectId; } + public AnnotationAuthor getAuthor() { + return author; + } + + public void setAuthor(AnnotationAuthor author) { + this.author = author; + } + public UUID getThreadId() { return threadId; } diff --git a/annotation-service/annotation/src/main/java/gr/cite/annotation/model/AnnotationAuthor.java b/annotation-service/annotation/src/main/java/gr/cite/annotation/model/AnnotationAuthor.java new file mode 100644 index 000000000..3670e51c5 --- /dev/null +++ b/annotation-service/annotation/src/main/java/gr/cite/annotation/model/AnnotationAuthor.java @@ -0,0 +1,31 @@ +package gr.cite.annotation.model; + +import java.util.UUID; + +public class AnnotationAuthor { + + private UUID id; + + public static final String _id = "id"; + + private String name; + + public static final String _name = "name"; + + public UUID getId() { + return id; + } + + public void setId(UUID id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + +} diff --git a/annotation-service/annotation/src/main/java/gr/cite/annotation/model/builder/AnnotationBuilder.java b/annotation-service/annotation/src/main/java/gr/cite/annotation/model/builder/AnnotationBuilder.java index 14a677823..de611d3ce 100644 --- a/annotation-service/annotation/src/main/java/gr/cite/annotation/model/builder/AnnotationBuilder.java +++ b/annotation-service/annotation/src/main/java/gr/cite/annotation/model/builder/AnnotationBuilder.java @@ -4,7 +4,13 @@ import gr.cite.annotation.authorization.AuthorizationFlags; import gr.cite.annotation.convention.ConventionService; import gr.cite.annotation.data.AnnotationEntity; import gr.cite.annotation.model.Annotation; +import gr.cite.annotation.model.AnnotationAuthor; +import gr.cite.annotation.model.User; +import gr.cite.annotation.query.UserQuery; +import gr.cite.tools.data.builder.BuilderFactory; +import gr.cite.tools.data.query.QueryFactory; import gr.cite.tools.exception.MyApplicationException; +import gr.cite.tools.fieldset.BaseFieldSet; import gr.cite.tools.fieldset.FieldSet; import gr.cite.tools.logging.DataLogEntry; import gr.cite.tools.logging.LoggerService; @@ -14,15 +20,22 @@ import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; import java.util.*; +import java.util.stream.Collectors; @Component @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) public class AnnotationBuilder extends BaseBuilder { + private final QueryFactory queryFactory; + + private final BuilderFactory builderFactory; + private EnumSet authorize = EnumSet.of(AuthorizationFlags.None); - public AnnotationBuilder(ConventionService conventionService) { + public AnnotationBuilder(ConventionService conventionService, QueryFactory queryFactory, BuilderFactory builderFactory) { super(conventionService, new LoggerService(LoggerFactory.getLogger(AnnotationBuilder.class))); + this.queryFactory = queryFactory; + this.builderFactory = builderFactory; } public AnnotationBuilder authorize(EnumSet values) { @@ -39,6 +52,9 @@ public class AnnotationBuilder extends BaseBuilder List models = new ArrayList<>(); + FieldSet authorFields = fields.extractPrefixed(this.asPrefix(Annotation._author)); + Map authorsMap = this.collectAuthors(authorFields, data); + if (data == null) return models; for (AnnotationEntity d : data) { @@ -55,6 +71,8 @@ public class AnnotationBuilder extends BaseBuilder m.setPayload(d.getPayload()); if (fields.hasField(this.asIndexer(Annotation._subjectId))) m.setSubjectId(d.getSubjectId()); + if (authorsMap != null && authorsMap.containsKey(d.getSubjectId())) + m.setAuthor(authorsMap.get(d.getSubjectId())); if (fields.hasField(this.asIndexer(Annotation._threadId))) m.setThreadId(d.getThreadId()); if (fields.hasField(this.asIndexer(Annotation._parentId))) @@ -75,4 +93,31 @@ public class AnnotationBuilder extends BaseBuilder return models; } + private Map collectAuthors(FieldSet fields, List data) { + if (fields.isEmpty() || data.isEmpty()) return null; + this.logger.debug("checking related - {}", User.class.getSimpleName()); + + Map itemMap = new HashMap<>(); + FieldSet clone = new BaseFieldSet(fields.getFields()).ensure(this.asIndexer(User._id), this.asIndexer(User._name)); + List userIds = data.stream() + .map(AnnotationEntity::getSubjectId) + .distinct() + .collect(Collectors.toList()); + UserQuery query = this.queryFactory.query(UserQuery.class).authorize(this.authorize).ids(userIds); + Map> users = this.builderFactory.builder(UserBuilder.class).authorize(this.authorize).asMasterKey(query, clone, User::getId); + + users.forEach((key, val) -> { + itemMap.put(key, this.authorFromUser(val.getFirst())); + }); + + return itemMap; + } + + private AnnotationAuthor authorFromUser(User user) { + AnnotationAuthor author = new AnnotationAuthor(); + author.setId(user.getId()); + author.setName(user.getName()); + return author; + } + } diff --git a/dmp-backend/core/src/main/java/eu/eudat/integrationevent/inbox/InboxRepositoryImpl.java b/dmp-backend/core/src/main/java/eu/eudat/integrationevent/inbox/InboxRepositoryImpl.java index 9db18331a..bfdcc132a 100644 --- a/dmp-backend/core/src/main/java/eu/eudat/integrationevent/inbox/InboxRepositoryImpl.java +++ b/dmp-backend/core/src/main/java/eu/eudat/integrationevent/inbox/InboxRepositoryImpl.java @@ -51,7 +51,7 @@ public class InboxRepositoryImpl implements InboxRepository { EntityTransaction transaction = null; EntityManager entityManager = null; CandidateInfo candidate = null; - try (FakeRequestScope fakeRequestScope = new FakeRequestScope()) { + try (FakeRequestScope ignored = new FakeRequestScope()) { try { QueryFactory queryFactory = this.applicationContext.getBean(QueryFactory.class); EntityManagerFactory entityManagerFactory = this.applicationContext.getBean(EntityManagerFactory.class); @@ -108,9 +108,9 @@ public class InboxRepositoryImpl implements InboxRepository { public Boolean shouldOmit(CandidateInfo candidate, Function shouldOmit) { EntityTransaction transaction = null; EntityManager entityManager = null; - Boolean success = false; + boolean success = false; - try (FakeRequestScope fakeRequestScope = new FakeRequestScope()) { + try (FakeRequestScope ignored = new FakeRequestScope()) { try { EntityManagerFactory entityManagerFactory = this.applicationContext.getBean(EntityManagerFactory.class); @@ -123,7 +123,7 @@ public class InboxRepositoryImpl implements InboxRepository { QueueInboxEntity item = queryFactory.query(QueueInboxQuery.class).ids(candidate.getId()).first(); if (item == null) { - this.logger.warn("Could not lookup queue inbox {} to process. Continuing...", candidate.getId()); + logger.warn("Could not lookup queue inbox {} to process. Continuing...", candidate.getId()); } else { if (shouldOmit.apply(item)) { item.setStatus(QueueInboxStatus.OMITTED); @@ -136,7 +136,7 @@ public class InboxRepositoryImpl implements InboxRepository { transaction.commit(); } catch (Exception ex) { - this.logger.error("Problem executing purge. rolling back any db changes and marking error. Continuing...", ex); + logger.error("Problem executing purge. rolling back any db changes and marking error. Continuing...", ex); if (transaction != null) transaction.rollback(); success = false; @@ -145,7 +145,7 @@ public class InboxRepositoryImpl implements InboxRepository { entityManager.close(); } } catch (Exception ex) { - this.logger.error("Problem executing purge. rolling back any db changes and marking error. Continuing...", ex); + logger.error("Problem executing purge. rolling back any db changes and marking error. Continuing...", ex); } return success; } @@ -154,9 +154,9 @@ public class InboxRepositoryImpl implements InboxRepository { public boolean shouldWait(CandidateInfo candidate, Function itIsTimeFunc) { EntityTransaction transaction = null; EntityManager entityManager = null; - Boolean success = false; + boolean success = false; - try (FakeRequestScope fakeRequestScope = new FakeRequestScope()) { + try (FakeRequestScope ignored = new FakeRequestScope()) { try { EntityManagerFactory entityManagerFactory = this.applicationContext.getBean(EntityManagerFactory.class); @@ -183,7 +183,7 @@ public class InboxRepositoryImpl implements InboxRepository { } transaction.commit(); } catch (Exception ex) { - this.logger.error("Problem executing purge. rolling back any db changes and marking error. Continuing...", ex); + logger.error("Problem executing purge. rolling back any db changes and marking error. Continuing...", ex); if (transaction != null) transaction.rollback(); success = false; @@ -192,7 +192,7 @@ public class InboxRepositoryImpl implements InboxRepository { entityManager.close(); } } catch (Exception ex) { - this.logger.error("Problem executing purge. rolling back any db changes and marking error. Continuing...", ex); + logger.error("Problem executing purge. rolling back any db changes and marking error. Continuing...", ex); } return success; } @@ -201,9 +201,9 @@ public class InboxRepositoryImpl implements InboxRepository { public QueueInbox create(InboxCreatorParams inboxCreatorParams) { EntityTransaction transaction = null; EntityManager entityManager = null; - Boolean success = false; + boolean success = false; QueueInboxEntity queueMessage = null; - try (FakeRequestScope fakeRequestScope = new FakeRequestScope()) { + try (FakeRequestScope ignored = new FakeRequestScope()) { try { queueMessage = this.createQueueInboxEntity(inboxCreatorParams); EntityManagerFactory entityManagerFactory = this.applicationContext.getBean(EntityManagerFactory.class); @@ -218,7 +218,7 @@ public class InboxRepositoryImpl implements InboxRepository { transaction.commit(); } catch (Exception ex) { - this.logger.error("Problem executing purge. rolling back any db changes and marking error. Continuing...", ex); + logger.error("Problem executing purge. rolling back any db changes and marking error. Continuing...", ex); if (transaction != null) transaction.rollback(); success = false; @@ -227,7 +227,7 @@ public class InboxRepositoryImpl implements InboxRepository { entityManager.close(); } } catch (Exception ex) { - this.logger.error("Problem executing purge. rolling back any db changes and marking error. Continuing...", ex); + logger.error("Problem executing purge. rolling back any db changes and marking error. Continuing...", ex); } return queueMessage; } @@ -255,9 +255,9 @@ public class InboxRepositoryImpl implements InboxRepository { public Boolean emit(CandidateInfo candidateInfo) { EntityTransaction transaction = null; EntityManager entityManager = null; - Boolean success = false; + boolean success = false; - try (FakeRequestScope fakeRequestScope = new FakeRequestScope()) { + try (FakeRequestScope ignored = new FakeRequestScope()) { try { EntityManagerFactory entityManagerFactory = this.applicationContext.getBean(EntityManagerFactory.class); @@ -270,7 +270,7 @@ public class InboxRepositoryImpl implements InboxRepository { QueueInboxEntity queueInboxMessage = queryFactory.query(QueueInboxQuery.class).ids(candidateInfo.getId()).first(); if (queueInboxMessage == null) { - this.logger.warn("Could not lookup queue inbox {} to process. Continuing...", candidateInfo.getId()); + logger.warn("Could not lookup queue inbox {} to process. Continuing...", candidateInfo.getId()); } else { EventProcessingStatus status = this.processMessage(queueInboxMessage.getRoute(), queueInboxMessage.getMessageId().toString(), queueInboxMessage.getApplicationId(), queueInboxMessage.getMessage()); @@ -302,7 +302,7 @@ public class InboxRepositoryImpl implements InboxRepository { transaction.commit(); } catch (Exception ex) { - this.logger.error("Problem executing purge. rolling back any db changes and marking error. Continuing...", ex); + logger.error("Problem executing purge. rolling back any db changes and marking error. Continuing...", ex); if (transaction != null) transaction.rollback(); success = false; @@ -311,7 +311,7 @@ public class InboxRepositoryImpl implements InboxRepository { entityManager.close(); } } catch (Exception ex) { - this.logger.error("Problem executing purge. rolling back any db changes and marking error. Continuing...", ex); + logger.error("Problem executing purge. rolling back any db changes and marking error. Continuing...", ex); } return success; } @@ -339,7 +339,7 @@ public class InboxRepositoryImpl implements InboxRepository { } private Boolean RoutingKeyMatched(String routingKey, List topics) { - if (topics == null || topics.size() == 0) + if (topics == null || topics.isEmpty()) return false; return topics.stream().anyMatch(x -> x.equals(routingKey)); } diff --git a/dmp-frontend/src/app/ui/annotations/annotation-dialog-component/annotation-dialog.component.html b/dmp-frontend/src/app/ui/annotations/annotation-dialog-component/annotation-dialog.component.html index 53e4e48ef..babd1d0bb 100644 --- a/dmp-frontend/src/app/ui/annotations/annotation-dialog-component/annotation-dialog.component.html +++ b/dmp-frontend/src/app/ui/annotations/annotation-dialog-component/annotation-dialog.component.html @@ -46,7 +46,7 @@
{{thread.timeStamp | date:'EEEE, MMMM d, y, h:mm a'}}
{{getAnnotationProtectionType(thread)}}
{{getAnnotationProtectionType(thread)}}
-
{{thread.text}}
+
{{thread.payload}}
{{'ANNOTATION-DIALOG.THREADS.FROM-USER' | translate}} {{thread.author.name}}
diff --git a/dmp-frontend/src/app/ui/annotations/annotation-dialog-component/annotation-dialog.component.ts b/dmp-frontend/src/app/ui/annotations/annotation-dialog-component/annotation-dialog.component.ts index d723748e9..e141ab47e 100644 --- a/dmp-frontend/src/app/ui/annotations/annotation-dialog-component/annotation-dialog.component.ts +++ b/dmp-frontend/src/app/ui/annotations/annotation-dialog-component/annotation-dialog.component.ts @@ -123,12 +123,11 @@ export class AnnotationDialogComponent extends BaseComponent { lookup.project = { fields: [ nameof(x => x.id), - // nameof(x => x.threadId), - // nameof(x => x.threadId), - // nameof(x => x.timeStamp), - // nameof(x => x.author.name), + nameof(x => x.threadId), + nameof(x => x.timeStamp), + nameof(x => x.author.name), nameof(x => x.payload), - // nameof(x => x.protection), + nameof(x => x.protectionType), ] }; @@ -146,6 +145,8 @@ export class AnnotationDialogComponent extends BaseComponent { // this.annotationsPerThread[element.id.toString()] = data.items.filter(x => x.threadId === element.id && x.id !== element.id).sort((a1, a2) => new Date(a1.timeStamp).getTime() - new Date(a2.timeStamp).getTime()); // }); // // this.annotationsChanged.emit(this.threads); + + this.threads = data.items; }, error => this.onCallbackError(error), );