Skip to content

Commit

Permalink
Issue #3221: Use separate entity classes for user notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
tcibinan committed May 17, 2023
1 parent 99878d3 commit 2b090ae
Show file tree
Hide file tree
Showing 16 changed files with 168 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.epam.pipeline.acl.notification;

import com.epam.pipeline.controller.PagedResult;
import com.epam.pipeline.entity.notification.UserNotification;
import com.epam.pipeline.dto.notification.UserNotification;
import com.epam.pipeline.manager.notification.UserNotificationManager;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.epam.pipeline.mapper.datastorage.lifecycle.StorageLifecycleEntityMapper;
import com.epam.pipeline.mapper.git.BitbucketMapper;
import com.epam.pipeline.mapper.notification.ContextualNotificationMapper;
import com.epam.pipeline.mapper.notification.UserNotificationMapper;
import com.epam.pipeline.mapper.ontology.OntologyMapper;
import com.epam.pipeline.mapper.quota.QuotaMapper;
import com.epam.pipeline.mapper.region.CloudRegionMapper;
Expand Down Expand Up @@ -124,6 +125,11 @@ public ContextualNotificationMapper contextualNotificationMapper() {
return Mappers.getMapper(ContextualNotificationMapper.class);
}

@Bean
public UserNotificationMapper userNotificationMapper() {
return Mappers.getMapper(UserNotificationMapper.class);
}

@Bean
public QuotaMapper quotaMapper() {
return Mappers.getMapper(QuotaMapper.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.epam.pipeline.controller.AbstractRestController;
import com.epam.pipeline.controller.PagedResult;
import com.epam.pipeline.controller.Result;
import com.epam.pipeline.entity.notification.UserNotification;
import com.epam.pipeline.dto.notification.UserNotification;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
Expand Down Expand Up @@ -54,8 +54,8 @@ public class UserNotificationController extends AbstractRestController {
@ApiResponses(
value = {@ApiResponse(code = HTTP_STATUS_OK, message = API_STATUS_DESCRIPTION)}
)
public Result<UserNotification> create(@RequestBody final UserNotification userNotification) {
return Result.success(notificationApiService.save(userNotification));
public Result<UserNotification> create(@RequestBody final UserNotification notification) {
return Result.success(notificationApiService.save(notification));
}

@PutMapping("/message/readAll")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import com.epam.pipeline.entity.notification.NotificationEntityClass;
import com.epam.pipeline.entity.notification.NotificationParameter;
import com.epam.pipeline.entity.notification.NotificationType;
import com.epam.pipeline.entity.notification.UserNotificationResource;
import com.epam.pipeline.entity.notification.UserNotificationResourceEntity;
import com.epam.pipeline.entity.pipeline.PipelineRun;
import com.epam.pipeline.entity.user.PipelineUser;
import com.epam.pipeline.mapper.IssueMapper;
Expand Down Expand Up @@ -183,12 +183,12 @@ private Map<String, Object> buildEntities(final NotificationEntityClass entityCl
return parameters;
}

private UserNotificationResource toResource(final NotificationEntityClass entityClass,
final Long entityId) {
return new UserNotificationResource(null, null, entityClass, entityId, null, null);
private UserNotificationResourceEntity toResource(final NotificationEntityClass entityClass,
final Long entityId) {
return new UserNotificationResourceEntity(null, null, entityClass, entityId, null, null);
}

private Map<String, Object> toString(final UserNotificationResource entity) {
private Map<String, Object> toString(final UserNotificationResourceEntity entity) {
return MapUtils.emptyIfNull(jsonMapper.convertValue(entity, new TypeReference<Map<String, Object>>() {}));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
import com.epam.pipeline.common.MessageConstants;
import com.epam.pipeline.common.MessageHelper;
import com.epam.pipeline.controller.PagedResult;
import com.epam.pipeline.entity.notification.UserNotification;
import com.epam.pipeline.dto.notification.UserNotification;
import com.epam.pipeline.entity.notification.UserNotificationEntity;
import com.epam.pipeline.entity.user.PipelineUser;
import com.epam.pipeline.manager.preference.PreferenceManager;
import com.epam.pipeline.manager.preference.SystemPreferences;
import com.epam.pipeline.manager.security.AuthManager;
import com.epam.pipeline.manager.user.UserManager;
import com.epam.pipeline.mapper.notification.UserNotificationMapper;
import com.epam.pipeline.repository.notification.UserNotificationRepository;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.ListUtils;
Expand All @@ -44,15 +46,17 @@
@Service
@RequiredArgsConstructor
public class UserNotificationManager {

private final UserNotificationRepository userNotificationRepository;
private final UserNotificationMapper mapper;
private final PreferenceManager preferenceManager;
private final AuthManager authManager;
private final UserManager userManager;
private final MessageHelper messageHelper;

@Transactional(propagation = Propagation.REQUIRED)
public UserNotification save(final UserNotification notification) {
return userNotificationRepository.save(toEntity(notification));
return toDto(userNotificationRepository.save(toEntity(notification)));
}

@Transactional(propagation = Propagation.REQUIRED)
Expand Down Expand Up @@ -80,10 +84,10 @@ public PagedResult<List<UserNotification>> findByUserId(final Long userId,
final int pageNum,
final int pageSize) {
final Pageable pageable = new PageRequest(Math.max(pageNum, 0), pageSize > 0 ? pageSize : 10);
final Page<UserNotification> notifications = isRead == null ?
final Page<UserNotificationEntity> entities = isRead == null ?
userNotificationRepository.findByUserIdOrderByCreatedDateDesc(userId, pageable) :
userNotificationRepository.findByUserIdAndIsReadOrderByCreatedDateDesc(userId, isRead, pageable);
return new PagedResult<>(toDto(notifications.getContent()), (int) notifications.getTotalElements());
return new PagedResult<>(toDto(entities.getContent()), (int) entities.getTotalElements());
}

public PagedResult<List<UserNotification>> findMy(final Boolean isRead, final int pageNum, final int pageSize) {
Expand All @@ -102,26 +106,28 @@ public void cleanUp() {
cleanUp(LocalDateTime.now().minusDays(expPeriod));
}

private List<UserNotification> toEntity(final List<UserNotification> notifications) {
private List<UserNotificationEntity> toEntity(final List<UserNotification> notifications) {
return ListUtils.emptyIfNull(notifications).stream().map(this::toEntity).collect(Collectors.toList());
}

private UserNotification toEntity(final UserNotification notification) {
if (notification.getId() == null) {
notification.setCreatedDate(LocalDateTime.now());
notification.setIsRead(false);
private UserNotificationEntity toEntity(final UserNotification notification) {
final UserNotificationEntity entity = mapper.toEntity(notification);
entity.setResources(ListUtils.emptyIfNull(entity.getResources()).stream()
.peek(resourceEntity -> resourceEntity.setNotification(entity))
.collect(Collectors.toList()));
if (entity.getId() == null) {
entity.setCreatedDate(LocalDateTime.now());
entity.setIsRead(false);
}
ListUtils.emptyIfNull(notification.getResources()).forEach(resource -> resource.setNotification(notification));
return notification;
return entity;
}

private List<UserNotification> toDto(final List<UserNotification> notifications) {
private List<UserNotification> toDto(final List<UserNotificationEntity> notifications) {
return ListUtils.emptyIfNull(notifications).stream().map(this::toDto).collect(Collectors.toList());
}

private UserNotification toDto(final UserNotification notification) {
ListUtils.emptyIfNull(notification.getResources()).forEach(resource -> resource.setNotification(null));
return notification;
private UserNotification toDto(final UserNotificationEntity notification) {
return mapper.toDto(notification);
}

private Long getPipelineUserId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.epam.pipeline.manager.security.notification;

import com.epam.pipeline.entity.notification.UserNotification;
import com.epam.pipeline.entity.notification.UserNotificationEntity;
import com.epam.pipeline.entity.user.PipelineUser;
import com.epam.pipeline.manager.security.CheckPermissionHelper;
import com.epam.pipeline.manager.user.UserManager;
Expand All @@ -34,15 +34,15 @@ public class NotificationPermissionManager {
private final CheckPermissionHelper permissionsHelper;
private final UserManager userManager;

public boolean hasPermission(final UserNotification userNotification) {
public boolean hasPermission(final UserNotificationEntity userNotification) {
if (Objects.nonNull(userNotification.getId())) {
return hasPermission(userNotification.getId());
}
return hasPermissionByUserId(userNotification.getUserId());
}

public boolean hasPermission(final Long notificationId) {
final UserNotification userNotification = repository.findOne(notificationId);
final UserNotificationEntity userNotification = repository.findOne(notificationId);
if (userNotification == null) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.epam.pipeline.mapper.notification;

import com.epam.pipeline.dto.notification.UserNotification;
import com.epam.pipeline.dto.notification.UserNotificationResource;
import com.epam.pipeline.entity.notification.UserNotificationEntity;
import com.epam.pipeline.entity.notification.UserNotificationResourceEntity;
import org.apache.commons.collections4.ListUtils;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

import java.util.stream.Collectors;

@Mapper(componentModel = "spring")
public interface UserNotificationMapper {

UserNotificationEntity toEntity(UserNotification dto);

@Mapping(target = "notification", ignore = true)
UserNotificationResourceEntity toEntity(UserNotificationResource dto);

default UserNotification toDto(UserNotificationEntity entity) {
return new UserNotification(entity.getId(), entity.getType(), entity.getUserId(), entity.getSubject(),
entity.getText(), entity.getCreatedDate(), entity.getIsRead(), entity.getReadDate(),
ListUtils.emptyIfNull(entity.getResources()).stream()
.map(this::toDto)
.collect(Collectors.toList()));
}

default UserNotificationResource toDto(UserNotificationResourceEntity entity) {
return new UserNotificationResource(entity.getId(), entity.getEntityClass(), entity.getEntityId(),
entity.getStoragePath(), entity.getStorageRuleId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.epam.pipeline.repository.notification;

import com.epam.pipeline.entity.notification.UserNotification;
import com.epam.pipeline.entity.notification.UserNotificationEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
Expand All @@ -25,13 +25,14 @@

import java.time.LocalDateTime;

public interface UserNotificationRepository extends PagingAndSortingRepository<UserNotification, Long> {
Page<UserNotification> findByUserIdAndIsReadOrderByCreatedDateDesc(Long userId, boolean isRead, Pageable pageable);
Page<UserNotification> findByUserIdOrderByCreatedDateDesc(Long userId, Pageable pageable);
public interface UserNotificationRepository extends PagingAndSortingRepository<UserNotificationEntity, Long> {
Page<UserNotificationEntity> findByUserIdAndIsReadOrderByCreatedDateDesc(Long userId, boolean isRead,
Pageable pageable);
Page<UserNotificationEntity> findByUserIdOrderByCreatedDateDesc(Long userId, Pageable pageable);
void deleteByCreatedDateLessThan(LocalDateTime date);
void deleteByUserId(Long userId);

@Modifying
@Query("update UserNotification set isRead = true, readDate = ?2 where userId = ?1")
@Query("update UserNotificationEntity set isRead = true, readDate = ?2 where userId = ?1")
void readAll(Long userId, LocalDateTime readDate);
}
Loading

0 comments on commit 2b090ae

Please sign in to comment.