@@ -33,4 +33,7 @@ public class ViolationRequest {
|
||||
|
||||
@JsonProperty("action")
|
||||
private String action;
|
||||
|
||||
@JsonProperty("token")
|
||||
private String token;
|
||||
}
|
||||
@@ -12,6 +12,8 @@ import ru.soune.nocopy.dto.violation.ViolationResponse;
|
||||
import ru.soune.nocopy.entity.file.FileEntity;
|
||||
import ru.soune.nocopy.entity.violation.Violation;
|
||||
import ru.soune.nocopy.repository.FileEntityRepository;
|
||||
import ru.soune.nocopy.service.file.FileEntityService;
|
||||
import ru.soune.nocopy.service.register.AuthService;
|
||||
import ru.soune.nocopy.service.violation.ViolationService;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
@@ -31,6 +33,10 @@ public class ViolationHandler implements RequestHandler {
|
||||
|
||||
private final FileEntityRepository fileRepository;
|
||||
|
||||
private final AuthService authService;
|
||||
|
||||
private final FileEntityService fileEntityService;
|
||||
|
||||
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||
|
||||
@Override
|
||||
@@ -38,9 +44,19 @@ public class ViolationHandler implements RequestHandler {
|
||||
ViolationRequest violationRequest = objectMapper.convertValue(request.getMessageBody(), ViolationRequest.class);
|
||||
|
||||
validateRequest(violationRequest);
|
||||
//TODO add response
|
||||
|
||||
String token = violationRequest.getToken();
|
||||
Long userId = authService.useUserAuthToken(token);
|
||||
|
||||
List<FileEntity> targetFiles;
|
||||
|
||||
if (violationRequest.getFileId() != null && !violationRequest.getFileId().isEmpty()) {
|
||||
FileEntity file = fileRepository.findById(violationRequest.getFileId())
|
||||
.orElseThrow(() -> new FileNotFoundException("File not found with id: " + violationRequest.getFileId()));
|
||||
targetFiles = List.of(file);
|
||||
} else {
|
||||
targetFiles = fileEntityService.getAllUserFiles(userId);
|
||||
}
|
||||
|
||||
LocalDateTime startDate = null;
|
||||
LocalDateTime endDate = null;
|
||||
@@ -52,12 +68,11 @@ public class ViolationHandler implements RequestHandler {
|
||||
|
||||
if ("group".equalsIgnoreCase(violationRequest.getAction())) {
|
||||
Map<String, Long> groupedData = violationService.getGroupedViolations(
|
||||
file,
|
||||
targetFiles,
|
||||
violationRequest.getGroupBy(),
|
||||
violationRequest.getStatus(),
|
||||
startDate,
|
||||
endDate
|
||||
);
|
||||
endDate);
|
||||
|
||||
return BaseResponse.builder()
|
||||
.msgId(request.getMsgId())
|
||||
@@ -67,18 +82,18 @@ public class ViolationHandler implements RequestHandler {
|
||||
Page<Violation> violationPage;
|
||||
|
||||
if (startDate != null && endDate != null) {
|
||||
violationPage = violationService.getViolationsByFileAndDateRange(
|
||||
file, startDate, endDate,
|
||||
violationPage = violationService.getViolationsByFilesAndDateRange(
|
||||
targetFiles, startDate, endDate,
|
||||
violationRequest.getPage(), violationRequest.getSize(), violationRequest.getSortDirection()
|
||||
);
|
||||
} else if (violationRequest.getStatus() != null && !violationRequest.getStatus().isEmpty()) {
|
||||
violationPage = violationService.getViolationsByFileAndStatus(
|
||||
file, violationRequest.getStatus(),
|
||||
violationPage = violationService.getViolationsByFilesAndStatus(
|
||||
targetFiles, violationRequest.getStatus(),
|
||||
violationRequest.getPage(), violationRequest.getSize(), violationRequest.getSortDirection()
|
||||
);
|
||||
} else {
|
||||
violationPage = violationService.getViolationsByFile(
|
||||
file,
|
||||
violationPage = violationService.getViolationsByFiles(
|
||||
targetFiles,
|
||||
violationRequest.getPage(), violationRequest.getSize(), violationRequest.getSortDirection()
|
||||
);
|
||||
}
|
||||
@@ -105,12 +120,7 @@ public class ViolationHandler implements RequestHandler {
|
||||
}
|
||||
}
|
||||
|
||||
//TODO add response exception
|
||||
private void validateRequest(ViolationRequest request) {
|
||||
if (request.getFileId() == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (request.getPage() < 0) {
|
||||
request.setPage(0);
|
||||
}
|
||||
@@ -125,7 +135,6 @@ public class ViolationHandler implements RequestHandler {
|
||||
}
|
||||
}
|
||||
|
||||
//TODO add response exception
|
||||
private LocalDateTime parseDate(String dateStr) {
|
||||
try {
|
||||
return LocalDateTime.parse(dateStr, DATE_FORMATTER);
|
||||
|
||||
@@ -41,6 +41,18 @@ public interface ViolationRepository extends JpaRepository<Violation, Long> {
|
||||
|
||||
List<Violation> findByFileEntityIn(List<FileEntity> files);
|
||||
|
||||
Page<Violation> findByFileEntityIn(List<FileEntity> files, Pageable pageable);
|
||||
|
||||
Page<Violation> findByFileEntityInAndStatus(List<FileEntity> files, String status, Pageable pageable);
|
||||
|
||||
List<Violation> findByFileEntityInAndStatus(List<FileEntity> files, String status);
|
||||
|
||||
Page<Violation> findByFileEntityInAndCreatedDateBetween(List<FileEntity> files, LocalDateTime startDate,
|
||||
LocalDateTime endDate, Pageable pageable);
|
||||
|
||||
List<Violation> findByFileEntityInAndStatusAndCreatedDateBetween(List<FileEntity> files, String status,
|
||||
LocalDateTime startDate, LocalDateTime endDate);
|
||||
|
||||
List<Violation> findByFileEntityInAndCreatedDateBetween(List<FileEntity> files,
|
||||
LocalDateTime startDate,
|
||||
LocalDateTime endDate);
|
||||
|
||||
@@ -146,6 +146,46 @@ public class ViolationService {
|
||||
));
|
||||
}
|
||||
|
||||
public Page<Violation> getViolationsByFiles(List<FileEntity> files, int page, int size, String sortDirection) {
|
||||
Sort sort = Sort.by(sortDirection.equalsIgnoreCase("desc") ? Sort.Direction.DESC : Sort.Direction.ASC, "createdDate");
|
||||
Pageable pageable = PageRequest.of(page, size, sort);
|
||||
return violationRepository.findByFileEntityIn(files, pageable);
|
||||
}
|
||||
|
||||
public Page<Violation> getViolationsByFilesAndStatus(List<FileEntity> files, String status, int page, int size, String sortDirection) {
|
||||
Sort sort = Sort.by(sortDirection.equalsIgnoreCase("desc") ? Sort.Direction.DESC : Sort.Direction.ASC, "createdDate");
|
||||
Pageable pageable = PageRequest.of(page, size, sort);
|
||||
return violationRepository.findByFileEntityInAndStatus(files, status, pageable);
|
||||
}
|
||||
|
||||
public Page<Violation> getViolationsByFilesAndDateRange(List<FileEntity> files, LocalDateTime startDate, LocalDateTime endDate,
|
||||
int page, int size, String sortDirection) {
|
||||
Sort sort = Sort.by(sortDirection.equalsIgnoreCase("desc") ? Sort.Direction.DESC : Sort.Direction.ASC, "createdDate");
|
||||
Pageable pageable = PageRequest.of(page, size, sort);
|
||||
return violationRepository.findByFileEntityInAndCreatedDateBetween(files, startDate, endDate, pageable);
|
||||
}
|
||||
|
||||
public Map<String, Long> getGroupedViolations(List<FileEntity> files, String groupBy, String status,
|
||||
LocalDateTime startDate, LocalDateTime endDate) {
|
||||
List<Violation> violations;
|
||||
|
||||
if (status != null && !status.isEmpty() && startDate != null && endDate != null) {
|
||||
violations = violationRepository.findByFileEntityInAndStatusAndCreatedDateBetween(files, status, startDate, endDate);
|
||||
} else if (status != null && !status.isEmpty()) {
|
||||
violations = violationRepository.findByFileEntityInAndStatus(files, status);
|
||||
} else if (startDate != null && endDate != null) {
|
||||
violations = violationRepository.findByFileEntityInAndCreatedDateBetween(files, startDate, endDate);
|
||||
} else {
|
||||
violations = violationRepository.findByFileEntityIn(files);
|
||||
}
|
||||
|
||||
return violations.stream()
|
||||
.collect(Collectors.groupingBy(
|
||||
v -> extractGroupKey(v.getPageUrl(), groupBy),
|
||||
Collectors.counting()
|
||||
));
|
||||
}
|
||||
|
||||
public ViolationStatisticsResponse getViolationStatistics(Long userId, String fileId,
|
||||
LocalDateTime startDate, LocalDateTime endDate) {
|
||||
|
||||
@@ -207,7 +247,6 @@ public class ViolationService {
|
||||
.build();
|
||||
}
|
||||
|
||||
|
||||
private String extractGroupKey(String url, String groupBy) {
|
||||
try {
|
||||
String domain = url.replaceAll("https?://(www\\.)?", "").split("/")[0];
|
||||
|
||||
Reference in New Issue
Block a user