add inapp notification listing like filter and read all sql query

This commit is contained in:
amentis 2024-01-10 15:12:26 +02:00
parent c7ea2ce2db
commit 29bea9f246
7 changed files with 55 additions and 41 deletions

View File

@ -31,7 +31,6 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource; import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.management.InvalidApplicationException; import javax.management.InvalidApplicationException;
@ -112,7 +111,7 @@ public class InAppNotificationController {
@PostMapping("{id}/read") @PostMapping("{id}/read")
@Transactional @Transactional
public ResponseEntity<String> persist(@PathVariable UUID id) public Boolean persist(@PathVariable UUID id)
{ {
logger.debug(new MapLogEntry("marking as read").And("id", id)); logger.debug(new MapLogEntry("marking as read").And("id", id));
@ -120,12 +119,12 @@ public class InAppNotificationController {
this.auditService.track(AuditableAction.InApp_Notification_Read, Map.of("id", id)); this.auditService.track(AuditableAction.InApp_Notification_Read, Map.of("id", id));
//this.auditService.trackIdentity(AuditableAction.IdentityTracking_Action); //this.auditService.trackIdentity(AuditableAction.IdentityTracking_Action);
return ResponseEntity.ok("ok"); return true;
} }
@PostMapping("read-all") @PostMapping("read-all")
@Transactional @Transactional
public ResponseEntity<String> MarkAsReadAllUserInAppNotification() throws InvalidApplicationException { public Boolean MarkAsReadAllUserInAppNotification() throws InvalidApplicationException, NoSuchFieldException {
logger.debug(new MapLogEntry("marking as read all")); logger.debug(new MapLogEntry("marking as read all"));
UUID userId = this.userScope.getUserId(); UUID userId = this.userScope.getUserId();
@ -134,12 +133,12 @@ public class InAppNotificationController {
this.inAppNotificationService.markAsReadAllUserNotification(userId); this.inAppNotificationService.markAsReadAllUserNotification(userId);
this.auditService.track(AuditableAction.InApp_Notification_Read_All, Map.of("userId", userId)); this.auditService.track(AuditableAction.InApp_Notification_Read_All, Map.of("userId", userId));
return ResponseEntity.ok("ok"); return true;
} }
@GetMapping("count-unread") @GetMapping("count-unread")
@Transactional @Transactional
public ResponseEntity<Integer> CountUnread() throws InvalidApplicationException { public Integer CountUnread() throws InvalidApplicationException {
logger.debug("count-unread"); logger.debug("count-unread");
UUID userId = this.userScope.getUserId(); UUID userId = this.userScope.getUserId();
@ -152,7 +151,7 @@ public class InAppNotificationController {
//this.auditService.trackIdentity(AuditableAction.IdentityTracking_Action); //this.auditService.trackIdentity(AuditableAction.IdentityTracking_Action);
return ResponseEntity.ok(count); return count;
} }
@DeleteMapping("{id}") @DeleteMapping("{id}")

View File

@ -8,12 +8,12 @@ import gr.cite.tools.exception.MyForbiddenException;
import org.hibernate.Session; import org.hibernate.Session;
import org.springframework.context.annotation.Scope; import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.context.annotation.RequestScope;
import javax.management.InvalidApplicationException; import javax.management.InvalidApplicationException;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.FlushModeType; import javax.persistence.FlushModeType;
import javax.persistence.PersistenceContext; import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.UUID; import java.util.UUID;
@Service @Service
@ -32,14 +32,16 @@ public class TenantScopedEntityManager {
public int getBulkSize() { public int getBulkSize() {
Session session = this.entityManager.unwrap(Session.class); Session session = this.entityManager.unwrap(Session.class);
if (session.getJdbcBatchSize() != null) return session.getJdbcBatchSize(); return session.getJdbcBatchSize();
return 0;
} }
public void setBulkSize(int size) { public void setBulkSize(int size) {
Session session = this.entityManager.unwrap(Session.class); Session session = this.entityManager.unwrap(Session.class);
session.setJdbcBatchSize(size); session.setJdbcBatchSize(size);
} }
public Query createQuery(String query){
return this.entityManager.createQuery(query);
}
public void persist(Object entity) { public void persist(Object entity) {
this.entityManager.persist(entity); this.entityManager.persist(entity);
} }

View File

@ -1,13 +1,9 @@
package gr.cite.notification.query; package gr.cite.notification.query;
import gr.cite.commons.web.authz.service.AuthorizationService;
import gr.cite.notification.authorization.AuthorizationFlags; import gr.cite.notification.authorization.AuthorizationFlags;
import gr.cite.notification.authorization.Permission;
import gr.cite.notification.common.enums.*; import gr.cite.notification.common.enums.*;
import gr.cite.notification.common.scope.user.UserScope;
import gr.cite.notification.data.InAppNotificationEntity; import gr.cite.notification.data.InAppNotificationEntity;
import gr.cite.notification.data.InAppNotificationEntity; import gr.cite.notification.data.LanguageEntity;
import gr.cite.notification.model.InAppNotification;
import gr.cite.notification.model.InAppNotification; import gr.cite.notification.model.InAppNotification;
import gr.cite.tools.data.query.FieldResolver; import gr.cite.tools.data.query.FieldResolver;
import gr.cite.tools.data.query.QueryBase; import gr.cite.tools.data.query.QueryBase;
@ -25,6 +21,7 @@ import java.util.*;
@Component @Component
@Scope(BeanDefinition.SCOPE_PROTOTYPE) @Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class InAppNotificationQuery extends QueryBase<InAppNotificationEntity> { public class InAppNotificationQuery extends QueryBase<InAppNotificationEntity> {
private String like;
private Collection<UUID> ids; private Collection<UUID> ids;
private List<UUID> excludeIds; private List<UUID> excludeIds;
private List<UUID> userId; private List<UUID> userId;
@ -37,6 +34,11 @@ public class InAppNotificationQuery extends QueryBase<InAppNotificationEntity> {
private Instant to; private Instant to;
private EnumSet<AuthorizationFlags> authorize = EnumSet.of(AuthorizationFlags.None); private EnumSet<AuthorizationFlags> authorize = EnumSet.of(AuthorizationFlags.None);
public InAppNotificationQuery like(String value) {
this.like = value;
return this;
}
public InAppNotificationQuery ids(UUID value) { public InAppNotificationQuery ids(UUID value) {
this.ids = List.of(value); this.ids = List.of(value);
return this; return this;
@ -164,6 +166,10 @@ public class InAppNotificationQuery extends QueryBase<InAppNotificationEntity> {
predicates.add(queryContext.Root.get(InAppNotificationEntity.Field._id).in(ids)); predicates.add(queryContext.Root.get(InAppNotificationEntity.Field._id).in(ids));
} }
if (this.like != null && !this.like.isEmpty()) {
predicates.add(queryContext.CriteriaBuilder.like(queryContext.Root.get(InAppNotificationEntity.Field._subject), this.like));
}
if (this.excludeIds != null) { if (this.excludeIds != null) {
predicates.add(queryContext.CriteriaBuilder.not(queryContext.Root.get(InAppNotificationEntity.Field._id).in(ids))); predicates.add(queryContext.CriteriaBuilder.not(queryContext.Root.get(InAppNotificationEntity.Field._id).in(ids)));
} }

View File

@ -13,6 +13,8 @@ import java.util.List;
import java.util.UUID; import java.util.UUID;
public class InAppNotificationLookup extends Lookup { public class InAppNotificationLookup extends Lookup {
private String like;
private Collection<UUID> ids; private Collection<UUID> ids;
private List<UUID> excludeIds; private List<UUID> excludeIds;
private List<UUID> userId; private List<UUID> userId;
@ -24,6 +26,14 @@ public class InAppNotificationLookup extends Lookup {
private Instant from; private Instant from;
private Instant to; private Instant to;
public String getLike() {
return like;
}
public void setLike(String like) {
this.like = like;
}
public Collection<UUID> getIds() { public Collection<UUID> getIds() {
return ids; return ids;
} }
@ -106,6 +116,7 @@ public class InAppNotificationLookup extends Lookup {
public InAppNotificationQuery enrich(QueryFactory queryFactory) { public InAppNotificationQuery enrich(QueryFactory queryFactory) {
InAppNotificationQuery query = queryFactory.query(InAppNotificationQuery.class); InAppNotificationQuery query = queryFactory.query(InAppNotificationQuery.class);
if (this.like != null) query.like(this.like);
if (this.isActive != null) query.isActive(this.isActive); if (this.isActive != null) query.isActive(this.isActive);
if (this.ids != null) query.ids(this.ids); if (this.ids != null) query.ids(this.ids);
if (this.type != null) query.type(this.type); if (this.type != null) query.type(this.type);

View File

@ -9,5 +9,5 @@ public interface InAppNotificationService {
void markAsRead(List<UUID> ids); void markAsRead(List<UUID> ids);
void markAsRead(UUID id); void markAsRead(UUID id);
void deleteAndSave(UUID id) throws InvalidApplicationException; void deleteAndSave(UUID id) throws InvalidApplicationException;
void markAsReadAllUserNotification(UUID userId); void markAsReadAllUserNotification(UUID userId) throws NoSuchFieldException;
} }

View File

@ -18,6 +18,8 @@ import org.springframework.stereotype.Service;
import org.springframework.web.context.annotation.RequestScope; import org.springframework.web.context.annotation.RequestScope;
import javax.management.InvalidApplicationException; import javax.management.InvalidApplicationException;
import javax.persistence.Column;
import javax.persistence.Query;
import java.time.Instant; import java.time.Instant;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
@ -66,31 +68,25 @@ public class InAppNotificationServiceImpl implements InAppNotificationService{
} }
} }
public void markAsReadAllUserNotification(UUID userId) public void markAsReadAllUserNotification(UUID userId) throws NoSuchFieldException {
{
try {
logger.debug(new DataLogEntry("marking as read all user in-app notifications", userId));
if (userId == null || userId.equals(new UUID(0L, 0L))) return; String entity = InAppNotificationEntity.class.getSimpleName();
String trackingStateProperty = InAppNotificationEntity.class.getDeclaredField(InAppNotificationEntity.Field._trackingState).getAnnotation(Column.class).name().replace("\"", "");
String updateProperty = InAppNotificationEntity.class.getDeclaredField(InAppNotificationEntity.Field._updatedAt).getAnnotation(Column.class).name().replace("\"", "");
String readTimeProperty = InAppNotificationEntity.class.getDeclaredField(InAppNotificationEntity.Field._readTime).getAnnotation(Column.class).name().replace("\"", "");
String userProperty = InAppNotificationEntity.class.getDeclaredField(InAppNotificationEntity.Field._userId).getAnnotation(Column.class).name().replace("\"", "");
List<InAppNotificationEntity> inAppNotificationEntities = this.queryFactory.query(InAppNotificationQuery.class) String sqlQuery = "UPDATE " + entity + " SET "+ trackingStateProperty +" = :trackingState, "+ updateProperty + "= :updatedAt, " + readTimeProperty + " = :readTime " +
.userId(userId) "WHERE " + trackingStateProperty + " = :trackingStateCondition";//TODO AND \"'user\" = :userId";
.trackingState(NotificationInAppTracking.STORED)
.collect(); Query query = this.entityManager.createQuery(sqlQuery)
Instant now = Instant.now(); .setParameter("trackingState", NotificationInAppTracking.DELIVERED.getValue())
int bulkSize = this.entityManager.getBulkSize(); .setParameter("updatedAt", Instant.now())
this.entityManager.setBulkSize(inAppNotificationEntities.size()); .setParameter("readTime", Instant.now())
for (InAppNotificationEntity entity : inAppNotificationEntities) { //.setParameter("userId", userId)
entity.setReadTime(now); .setParameter("trackingStateCondition", NotificationInAppTracking.STORED.getValue());
entity.setUpdatedAt(now);
entity.setTrackingState(NotificationInAppTracking.DELIVERED); int updateCount = query.executeUpdate();
this.entityManager.merge(entity);
this.entityManager.persist(entity);
}
this.entityManager.setBulkSize(bulkSize);
} catch (InvalidApplicationException e) {
logger.error(e.getMessage(), e);
}
} }

View File

@ -121,8 +121,8 @@ export class InAppNotificationEditorComponent extends BaseComponent implements O
} }
onCallbackSuccess(data?: any): void { onCallbackSuccess(data?: any): void {
this.uiNotificationService.snackBarNotification(this.language.instant('COMMONS.SNACK-BAR.SUCCESSFUL-UPDATE'), SnackBarNotificationLevel.Success); // this.uiNotificationService.snackBarNotification(this.language.instant('COMMONS.SNACK-BAR.SUCCESSFUL-UPDATE'), SnackBarNotificationLevel.Success);
this.router.navigate(['/mine-notifications']); // this.router.navigate(['/mine-notifications']);
} }
onCallbackError(errorResponse: HttpErrorResponse) { onCallbackError(errorResponse: HttpErrorResponse) {