generated from ita-social-projects/DevTemplate
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #596 from ita-social-projects/develop
logging: develop -> release
- Loading branch information
Showing
12 changed files
with
379 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
src/main/java/com/softserveinc/dokazovi/aop/PostLogger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/softserveinc/dokazovi/config/AspectConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/main/java/com/softserveinc/dokazovi/controller/LogController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
21
src/main/java/com/softserveinc/dokazovi/dto/log/PostLogDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
42
src/main/java/com/softserveinc/dokazovi/entity/LogEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
14
src/main/java/com/softserveinc/dokazovi/mapper/LogMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/softserveinc/dokazovi/repositories/LogRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
src/main/java/com/softserveinc/dokazovi/service/LogService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.