Skip to content

Commit

Permalink
Merge pull request #596 from ita-social-projects/develop
Browse files Browse the repository at this point in the history
logging: develop -> release
  • Loading branch information
fortamt authored Jan 25, 2023
2 parents bd2622c + 515b6ae commit 5da6460
Show file tree
Hide file tree
Showing 12 changed files with 379 additions and 0 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,13 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-aop'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-mail:2.3.12.RELEASE'
implementation group: 'com.google.apis', name: 'google-api-services-analytics', version: 'v3-rev161-1.25.0'
implementation group: 'com.google.api-client', name: 'google-api-client', version: '1.31.5'
implementation group: 'org.aspectj', name: 'aspectjweaver', version: '1.9.9.1'

implementation 'redis.clients:jedis:3.6.3'

Expand Down
144 changes: 144 additions & 0 deletions src/main/java/com/softserveinc/dokazovi/aop/PostLogger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package com.softserveinc.dokazovi.aop;

import com.softserveinc.dokazovi.dto.post.PostSaveFromUserDTO;
import com.softserveinc.dokazovi.entity.LogEntity;
import com.softserveinc.dokazovi.entity.PostEntity;
import com.softserveinc.dokazovi.entity.UserEntity;
import com.softserveinc.dokazovi.entity.enumerations.PostStatus;
import com.softserveinc.dokazovi.repositories.LogRepository;
import com.softserveinc.dokazovi.repositories.PostRepository;
import com.softserveinc.dokazovi.repositories.UserRepository;
import com.softserveinc.dokazovi.security.UserPrincipal;
import lombok.RequiredArgsConstructor;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Component
@Aspect
@RequiredArgsConstructor
public class PostLogger {

private final LogRepository logRepository;
private final UserRepository userRepository;
private final PostRepository postRepository;

@AfterReturning("execution(* com.softserveinc.dokazovi.service.impl.PostServiceImpl.saveFromUser("
+ "com.softserveinc.dokazovi.dto.post.PostSaveFromUserDTO,"
+ "com.softserveinc.dokazovi.security.UserPrincipal))")
public void saveNewPost(JoinPoint joinPoint) {
Object[] arguments = joinPoint.getArgs();
PostSaveFromUserDTO postSaveFromUserDTO = null;
UserPrincipal userPrincipal = null;
for (Object obj : arguments) {
if (obj instanceof PostSaveFromUserDTO) {
postSaveFromUserDTO = (PostSaveFromUserDTO) obj;
}
if (obj instanceof UserPrincipal) {
userPrincipal = (UserPrincipal) obj;
}
}
UserEntity userEntity = userRepository.findByEmail(userPrincipal.getEmail()).get();
LogEntity log = LogEntity.builder()
.title(postSaveFromUserDTO.getTitle())
.changes("Створено матеріал")
.nameOfChanger(userEntity.getLastName() + " " + userEntity.getFirstName())
.build();

logRepository.save(log);
}

@Around("execution(* com.softserveinc.dokazovi.service.impl.PostServiceImpl.updatePostById("
+ "com.softserveinc.dokazovi.security.UserPrincipal,"
+ "com.softserveinc.dokazovi.dto.post.PostSaveFromUserDTO))")
public Boolean updatePost(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
Object[] arguments = proceedingJoinPoint.getArgs();
Integer postId = null;
for (Object obj : arguments) {
if (obj instanceof PostSaveFromUserDTO) {
postId = ((PostSaveFromUserDTO) obj).getId();
}
}
String postEntityBeforeExecutingStatus = postRepository.getOne(postId).getStatus().name();
final Boolean joinPoint = (Boolean) proceedingJoinPoint.proceed();
UserPrincipal userPrincipal = null;
PostSaveFromUserDTO postSaveFromUserDTO = null;
for (Object obj : arguments) {
if (obj instanceof PostSaveFromUserDTO) {
postSaveFromUserDTO = (PostSaveFromUserDTO) obj;
}
if (obj instanceof UserPrincipal) {
userPrincipal = (UserPrincipal) obj;
}
}
String postEntityChangedStatus = PostStatus.values()[postSaveFromUserDTO.getPostStatus()].name();
UserEntity userEntity = userRepository.findByEmail(userPrincipal.getEmail()).get();
String changes;
if (postEntityBeforeExecutingStatus.equals(postEntityChangedStatus)) {
changes = "Оновлено матеріал";
} else {
switch (postEntityChangedStatus) {
case "ARCHIVED":
changes = "Заархівовано";
break;
case "MODERATION_FIRST_SIGN":
changes = "Відправлено на модерацію";
break;
case "NEEDS_EDITING":
changes = "Повернуто автору на редагування";
break;
case "PLANNED":
changes = "Заплановано публікацію";
break;
case "PUBLISHED":
changes = "Опубліковано";
break;
default: changes = "N/A";
}
}
LogEntity log = LogEntity.builder()
.title(postSaveFromUserDTO.getTitle())
.changes(changes)
.nameOfChanger(userEntity.getLastName() + " " + userEntity.getFirstName())
.build();

logRepository.save(log);
return joinPoint;
}

@AfterReturning("execution(* com.softserveinc.dokazovi.service.impl.PostServiceImpl.removePostById("
+ "com.softserveinc.dokazovi.security.UserPrincipal,"
+ "Integer, boolean))")
public void deletePost(JoinPoint joinPoint) {
Object[] arguments = joinPoint.getArgs();
UserPrincipal userPrincipal = null;
Integer postId = null;
boolean flag = false;
for (Object obj : arguments) {
if (obj instanceof UserPrincipal) {
userPrincipal = (UserPrincipal) obj;
}
if (obj instanceof Integer) {
postId = (Integer) obj;
}
if (obj instanceof Boolean) {
flag = (boolean) obj;
}
}
if (!flag) {
return;
}
PostEntity postEntity = postRepository.getOne(postId);
UserEntity userEntity = userRepository.findByEmail(userPrincipal.getEmail()).get();
LogEntity log = LogEntity.builder()
.title(postEntity.getTitle())
.changes("Матеріал видалено")
.nameOfChanger(userEntity.getLastName() + " " + userEntity.getFirstName())
.build();

logRepository.save(log);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.softserveinc.dokazovi.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@Configuration
@EnableAspectJAutoProxy
public class AspectConfig {
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public final class EndPoints {
public static final String USER_EXPERT_ALL_POST_DIRECTIONS = "/experts/{expertId}/post-directions";
public static final String AUTHOR_GET_AUTHOR_BY_ID = "/{authorId}";
public static final String AUTHOR = "/author";
public static final String LOG = "/log";
public static final String POST_LOGS = "/post-logs";

/**
* Method that adds slash after each endpoint while calling
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.softserveinc.dokazovi.controller;

import com.softserveinc.dokazovi.annotations.ApiPageable;
import com.softserveinc.dokazovi.dto.log.PostLogDTO;
import com.softserveinc.dokazovi.service.LogService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.Authorization;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import java.time.LocalDate;

import static com.softserveinc.dokazovi.controller.EndPoints.LOG;
import static com.softserveinc.dokazovi.controller.EndPoints.POST_LOGS;

@RestController
@RequestMapping(LOG)
@RequiredArgsConstructor
public class LogController {

private final LogService logService;

@GetMapping(POST_LOGS)
@PreAuthorize("hasAuthority('EDIT_AUTHOR')")
@ApiPageable
@ApiOperation(value = "get all post logs",
authorizations = {@Authorization(value = "Authorization")})
public ResponseEntity<Page<PostLogDTO>> getPostLogList(
@PageableDefault(size = 12, direction = Sort.Direction.DESC) Pageable pageable,
@ApiParam(value = "Logs by surname", type = "string")
@RequestParam(required = false, defaultValue = "") String surname,
@ApiParam(value = "Logs by title", type = "string")
@RequestParam(required = false, defaultValue = "") String title,
@ApiParam(value = "yyyy-MM-dd")
@RequestParam(required = false)
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
@ApiParam(value = "yyyy-MM-dd")
@RequestParam(required = false)
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate) {
return ResponseEntity
.status(HttpStatus.OK)
.body(logService.findAllPostLogs(pageable, surname, title, startDate, endDate));
}
}
21 changes: 21 additions & 0 deletions src/main/java/com/softserveinc/dokazovi/dto/log/PostLogDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.softserveinc.dokazovi.dto.log;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.sql.Timestamp;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class PostLogDTO {

private Integer id;
private String title;
private Timestamp dateOfChange;
private String changes;
private String nameOfChanger;
}
42 changes: 42 additions & 0 deletions src/main/java/com/softserveinc/dokazovi/entity/LogEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.softserveinc.dokazovi.entity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.CreationTimestamp;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotBlank;
import java.sql.Timestamp;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Entity(name = "log_entity")
@Table(name = "log")
public class LogEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "log_id")
private Integer id;

@NotBlank
private String title;

@CreationTimestamp
private Timestamp dateOfChange;

@NotBlank
private String changes;

@NotBlank
private String nameOfChanger;
}
14 changes: 14 additions & 0 deletions src/main/java/com/softserveinc/dokazovi/mapper/LogMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.softserveinc.dokazovi.mapper;

import com.softserveinc.dokazovi.dto.log.PostLogDTO;
import com.softserveinc.dokazovi.entity.LogEntity;
import org.mapstruct.CollectionMappingStrategy;
import org.mapstruct.Mapper;
import org.mapstruct.ReportingPolicy;

@Mapper(componentModel = "spring",
unmappedTargetPolicy = ReportingPolicy.IGNORE,
collectionMappingStrategy = CollectionMappingStrategy.TARGET_IMMUTABLE)
public interface LogMapper {
PostLogDTO toPostLogDTO(LogEntity logEntity);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.softserveinc.dokazovi.repositories;

import com.softserveinc.dokazovi.entity.LogEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.sql.Timestamp;

@Repository
public interface LogRepository extends JpaRepository<LogEntity, Integer> {

Page<LogEntity> findAll(Pageable pageable);

Page<LogEntity> findAllByNameOfChangerContainingIgnoreCase(Pageable pageable, String nameOfChanger);

Page<LogEntity> findAllByTitleContainingIgnoreCase(Pageable pageable, String title);

Page<LogEntity> findByDateOfChangeBetween(Pageable pageable, Timestamp startDate, Timestamp endDate);
}
13 changes: 13 additions & 0 deletions src/main/java/com/softserveinc/dokazovi/service/LogService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.softserveinc.dokazovi.service;

import com.softserveinc.dokazovi.dto.log.PostLogDTO;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.time.LocalDate;

public interface LogService {

Page<PostLogDTO> findAllPostLogs(Pageable pageable, String surname, String title,
LocalDate startDate, LocalDate endDate);
}
Loading

0 comments on commit 5da6460

Please sign in to comment.