develop #38
@@ -2,7 +2,7 @@
|
||||
|
||||
### Product Purpose and Current Phase
|
||||
|
||||
- This is the launched Java 17 and Spring Boot backend for KRI contact operations. Preserve first-release API and data behavior while delivering approved follow-up features.
|
||||
- This is the pre-launch Java 17 and Spring Boot backend for KRI contact operations. Preserve established API and data behavior except where the user explicitly approves a contract change.
|
||||
- The domain includes projects, contact lists and headers/items, uploads and downloads, assignments, call sessions, call/event/update histories, progress, scenarios and images, result codes, users, companies, roles, message templates, reservations, SMS, and email workflows.
|
||||
- Prefer domain correctness, operational safety, and backward-compatible evolution over broad refactoring.
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
|
||||
### Shared Library and Integrations
|
||||
|
||||
- Consume `com.kri.common:starter:beta.1.6.8`. Do not change this approved version or duplicate an available KriCommon feature without separate explicit approval.
|
||||
- Consume `com.kri.common:starter:beta.1.7.0`. Do not change this approved version or duplicate an available KriCommon feature without separate explicit approval.
|
||||
- Import public KriCommon APIs from `com.kri.common.feature.*` and keep application code out of KriCommon runtime internals.
|
||||
- Treat IntranetExtension, NotiHub, ShortUrl, CTI, file storage, and other external dependencies as explicit boundaries with validated inputs, timeouts, error mapping, retry/idempotency decisions, and safe rollback behavior.
|
||||
- Never log API keys, credentials, full message bodies, telephone numbers in bulk, uploaded list contents, or unrestricted personal information.
|
||||
|
||||
@@ -38,7 +38,7 @@ repositories {
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation "com.kri.common:starter:beta.1.6.9"
|
||||
implementation "com.kri.common:starter:beta.1.7.0"
|
||||
|
||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-security'
|
||||
|
||||
@@ -22,8 +22,6 @@ public final class ContactConstant {
|
||||
public static final int RESEARCHER_USER_ROLE = 2000;
|
||||
public static final int OUTSOURCE_USER_ROLE = 4000;
|
||||
public static final int FIELDWORK_USER_ROLE = 9000;
|
||||
public static final int CONTACT_INTERVIEWER_ROLE = 9002;
|
||||
public static final int FIELD_INTERVIEWER_ROLE = 9003;
|
||||
public static final StreamingReadOptions UPPER_CASE_HEADER_OPTIONS =
|
||||
new StreamingReadOptions(null, 0, 1, header -> header == null ? null : header.toUpperCase(
|
||||
Locale.ROOT));
|
||||
@@ -36,11 +34,6 @@ public final class ContactConstant {
|
||||
Stream.of(ListHeaderType.FILE)
|
||||
).toList();
|
||||
|
||||
public static final List<ListHeaderType> ASSIGN_TYPE = List.of(
|
||||
ListHeaderType.OUTSOURCE,
|
||||
ListHeaderType.CONTACT_INTERVIEWER,
|
||||
ListHeaderType.FIELD_INTERVIEWER
|
||||
);
|
||||
|
||||
public static final List<ListHeaderType> EXACT_MATCH_HEADER_TYPE = List.of(
|
||||
ListHeaderType.CONTENT_ITEM
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.kri.contact.server.common.enums;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public enum ListAsyncAssignOperation {
|
||||
ASSIGN, RESET;
|
||||
|
||||
public static ListAsyncAssignOperation fromRequest(ListAsyncJobType type, Map<String, Object> request) {
|
||||
if (type != ListAsyncJobType.LIST_ASSIGN || request == null || request.get("operation") == null) {
|
||||
return null;
|
||||
}
|
||||
return valueOf(request.get("operation").toString());
|
||||
}
|
||||
|
||||
public String displayName() {
|
||||
return this == RESET ? "배정 초기화" : "리스트 배정";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.kri.contact.server.common.enums;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public enum ListAsyncJobStatus {
|
||||
QUEUED, RUNNING, CANCEL_REQUESTED, CANCELLED, SUCCEEDED, FAILED;
|
||||
|
||||
public static final List<ListAsyncJobStatus> ACTIVE = List.of(QUEUED, RUNNING, CANCEL_REQUESTED);
|
||||
|
||||
public boolean isTerminal() {
|
||||
return this == CANCELLED || this == SUCCEEDED || this == FAILED;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.kri.contact.server.common.enums;
|
||||
|
||||
public enum ListAsyncJobType {
|
||||
LIST_UPLOAD, LIST_RESET, LIST_UPDATE, LIST_ASSIGN,
|
||||
LIST_DOWNLOAD, CONTACT_HISTORY_DOWNLOAD, UPDATE_HISTORY_DOWNLOAD, ITEM_ZIP_DOWNLOAD,
|
||||
EMAIL_BULK, SMS_BULK, KKO_BULK;
|
||||
|
||||
public boolean isImportant() {
|
||||
return this == LIST_UPLOAD || this == LIST_RESET || this == LIST_UPDATE
|
||||
|| this == LIST_ASSIGN;
|
||||
}
|
||||
|
||||
public boolean conflictsWith(ListAsyncJobType existing, boolean sameUser) {
|
||||
return isImportant() || existing.isImportant() || (sameUser && this == existing);
|
||||
}
|
||||
|
||||
public String displayName() {
|
||||
return switch (this) {
|
||||
case LIST_UPLOAD -> "리스트 업로드";
|
||||
case LIST_RESET -> "리스트 초기화";
|
||||
case LIST_UPDATE -> "리스트 일괄 수정";
|
||||
case LIST_ASSIGN -> "배정 작업";
|
||||
case LIST_DOWNLOAD -> "리스트 다운로드";
|
||||
case CONTACT_HISTORY_DOWNLOAD -> "컨택 이력 다운로드";
|
||||
case UPDATE_HISTORY_DOWNLOAD -> "리스트 수정 이력 다운로드";
|
||||
case ITEM_ZIP_DOWNLOAD -> "첨부파일 ZIP 다운로드";
|
||||
case EMAIL_BULK -> "이메일 일괄 발송";
|
||||
case SMS_BULK -> "문자 일괄 발송";
|
||||
case KKO_BULK -> "알림톡 일괄 발송";
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.kri.contact.server.config;
|
||||
|
||||
import jakarta.validation.constraints.Min;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.Size;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Validated
|
||||
@ConfigurationProperties(prefix = "kri.list-async")
|
||||
public class ListAsyncProperties {
|
||||
@Min(1)
|
||||
private int threads = 2;
|
||||
@Min(1)
|
||||
private int queueCapacity = 10;
|
||||
@NotBlank
|
||||
@Size(max = 64)
|
||||
private String ownerInstance = "contact-server";
|
||||
@NotBlank
|
||||
private String resultBaseDir = "upload-file/list-async-results";
|
||||
}
|
||||
@@ -4,8 +4,6 @@ import com.kri.common.feature.datetime.DateTimeUtil;
|
||||
import com.kri.common.feature.web.RequestUserContext;
|
||||
import com.kri.contact.server.dto.assign.ProjectCompanyAssignSummaryDto;
|
||||
import com.kri.contact.server.dto.assign.ProjectFieldworkAssignSummaryDto;
|
||||
import com.kri.contact.server.dto.list.ListAssignRequestDto;
|
||||
import com.kri.contact.server.dto.list.ListAssignResetRequestDto;
|
||||
import com.kri.contact.server.service.AssignService;
|
||||
import com.kri.contact.server.service.common.SystemEventLogService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
@@ -14,7 +12,6 @@ import jakarta.validation.Valid;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
@@ -95,33 +92,4 @@ public class AssignController {
|
||||
systemEventLogService.writeAssignCmpnProject(ctx.getUserId(), now, ctx.getClientIp(), prjctIdx,
|
||||
assignCmpnIdList);
|
||||
}
|
||||
|
||||
@Operation(summary = "리스트 배정")
|
||||
@PutMapping("/list/{prjctIdx}/{assignId}")
|
||||
public Integer upsertListAssign(
|
||||
@PathVariable Long prjctIdx,
|
||||
@PathVariable String assignId,
|
||||
@Valid @RequestBody ListAssignRequestDto req,
|
||||
RequestUserContext ctx
|
||||
) {
|
||||
final LocalDateTime now = DateTimeUtil.nowKstLocalDateTime();
|
||||
Integer assignCnt = assignService.upsertListAssign(prjctIdx, assignId, req, ctx.getUserId(), now);
|
||||
systemEventLogService.writeAssignList(ctx.getUserId(), now, ctx.getClientIp(), prjctIdx, req);
|
||||
|
||||
return assignCnt;
|
||||
}
|
||||
|
||||
@Operation(summary = "리스트 배정 초기화")
|
||||
@DeleteMapping("/list/reset/{prjctIdx}")
|
||||
public Integer resetListAssign(
|
||||
@PathVariable Long prjctIdx,
|
||||
@Valid @RequestBody ListAssignResetRequestDto req,
|
||||
RequestUserContext ctx
|
||||
) {
|
||||
final LocalDateTime now = DateTimeUtil.nowKstLocalDateTime();
|
||||
Integer resetCnt = assignService.resetListAssign(prjctIdx, req);
|
||||
systemEventLogService.writeAssignListReset(ctx.getUserId(), now, ctx.getClientIp(), prjctIdx, req);
|
||||
|
||||
return resetCnt;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.kri.contact.server.controller;
|
||||
|
||||
import com.kri.common.feature.web.RequestUserContext;
|
||||
import com.kri.contact.server.dto.list.ListEventHistDownloadRequestDto;
|
||||
import com.kri.contact.server.dto.list.ListFilterDto;
|
||||
import com.kri.contact.server.dto.list.async.ListAsyncJobAcceptedDto;
|
||||
import com.kri.contact.server.service.async.ListAsyncDownloadService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/list/jobs/{prjctIdx}/downloads")
|
||||
@RequiredArgsConstructor
|
||||
@Tag(name = "ListAsyncDownload", description = "리스트 비동기 다운로드 API")
|
||||
public class ListAsyncDownloadController {
|
||||
private final ListAsyncDownloadService downloads;
|
||||
|
||||
@Operation(summary = "리스트 Excel 생성 접수")
|
||||
@PostMapping("/list")
|
||||
public ListAsyncJobAcceptedDto list(@PathVariable Long prjctIdx,
|
||||
@RequestParam(defaultValue = "true") boolean includeResult,
|
||||
@Valid @RequestBody(required = false) ListFilterDto filter, RequestUserContext user) {
|
||||
return downloads.list(prjctIdx, filter, includeResult, user);
|
||||
}
|
||||
|
||||
@Operation(summary = "컨택 이력 Excel 생성 접수")
|
||||
@PostMapping("/event-history")
|
||||
public ListAsyncJobAcceptedDto eventHistory(@PathVariable Long prjctIdx,
|
||||
@RequestParam(defaultValue = "true") boolean includeResult,
|
||||
@Valid @RequestBody ListEventHistDownloadRequestDto request, RequestUserContext user) {
|
||||
return downloads.eventHistory(prjctIdx, request, includeResult, user);
|
||||
}
|
||||
|
||||
@Operation(summary = "수정 이력 Excel 생성 접수")
|
||||
@PostMapping("/update-history")
|
||||
public ListAsyncJobAcceptedDto updateHistory(@PathVariable Long prjctIdx,
|
||||
@Valid @RequestBody(required = false) ListFilterDto filter, RequestUserContext user) {
|
||||
return downloads.updateHistory(prjctIdx, filter, user);
|
||||
}
|
||||
|
||||
@Operation(summary = "다중 첨부 ZIP 생성 접수")
|
||||
@PostMapping("/item-files")
|
||||
public ListAsyncJobAcceptedDto itemFiles(@PathVariable Long prjctIdx,
|
||||
@Valid @RequestBody(required = false) ListFilterDto filter, RequestUserContext user) {
|
||||
return downloads.itemFiles(prjctIdx, filter, user);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
package com.kri.contact.server.controller;
|
||||
|
||||
import com.kri.common.feature.web.BusinessException;
|
||||
import com.kri.common.feature.web.RequestUserContext;
|
||||
import com.kri.contact.server.common.enums.ListAsyncJobType;
|
||||
import com.kri.contact.server.common.enums.ListAsyncAssignOperation;
|
||||
import com.kri.contact.server.dto.list.async.ListAsyncAvailabilityDto;
|
||||
import com.kri.contact.server.dto.list.async.ListAsyncActiveJobsDto;
|
||||
import com.kri.contact.server.dto.list.async.ListAsyncJobStatusDto;
|
||||
import com.kri.contact.server.dto.list.async.ListAsyncResultFileDto;
|
||||
import com.kri.contact.server.dto.list.async.ListUploadResetActiveDto;
|
||||
import com.kri.contact.server.dto.list.async.ListAsyncSidFailuresDto;
|
||||
import com.kri.contact.server.persistence.entity.TListAsyncJobEntity;
|
||||
import com.kri.contact.server.service.async.ListAsyncJobAccess;
|
||||
import com.kri.contact.server.service.async.ListAsyncJobExecutor;
|
||||
import com.kri.contact.server.service.async.ListAsyncJobStore;
|
||||
import com.kri.contact.server.service.file.ListAsyncResultFileService;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.util.List;
|
||||
import java.util.stream.IntStream;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.ContentDisposition;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.CacheControl;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/list/jobs/{prjctIdx}")
|
||||
@RequiredArgsConstructor
|
||||
@Tag(name = "ListAsyncJob", description = "리스트 비동기 작업 관리 API")
|
||||
public class ListAsyncJobController {
|
||||
private final ListAsyncJobAccess access;
|
||||
private final ListAsyncJobExecutor executor;
|
||||
private final ListAsyncResultFileService files;
|
||||
|
||||
@GetMapping("/availability")
|
||||
public ResponseEntity<ListAsyncAvailabilityDto> availability(
|
||||
@PathVariable Long prjctIdx,
|
||||
@RequestParam ListAsyncJobType jobType, RequestUserContext user) {
|
||||
access.requireProject(prjctIdx, user);
|
||||
return ResponseEntity.ok().cacheControl(CacheControl.noStore())
|
||||
.body(executor.availability(prjctIdx, user.getUserId(), jobType));
|
||||
}
|
||||
|
||||
@GetMapping("/upload-reset-active")
|
||||
public ResponseEntity<ListUploadResetActiveDto> uploadResetActive(@PathVariable Long prjctIdx,
|
||||
RequestUserContext user) {
|
||||
var view = access.activeUploadReset(prjctIdx, user).map(job -> {
|
||||
boolean mine = user.getUserId().equals(job.getRequestUser());
|
||||
return new ListUploadResetActiveDto.Job(job.getJobType(), job.getJobStatus(), job.getJobStage(),
|
||||
job.getTotalSidCnt(), processedSidCount(job), mine, mine ? job.getJobKey() : null);
|
||||
});
|
||||
return ResponseEntity.ok().cacheControl(CacheControl.noStore())
|
||||
.body(new ListUploadResetActiveDto(view.isPresent(), view.orElse(null)));
|
||||
}
|
||||
|
||||
@GetMapping("/my-active")
|
||||
public ResponseEntity<ListAsyncActiveJobsDto> myActive(@PathVariable Long prjctIdx, RequestUserContext user) {
|
||||
return ResponseEntity.ok().cacheControl(CacheControl.noStore())
|
||||
.body(new ListAsyncActiveJobsDto(access.myActive(prjctIdx, user).stream().map(this::toStatus).toList()));
|
||||
}
|
||||
|
||||
@GetMapping("/{jobKey}")
|
||||
public ListAsyncJobStatusDto status(@PathVariable Long prjctIdx, @PathVariable String jobKey,
|
||||
RequestUserContext user) {
|
||||
return toStatus(access.require(prjctIdx, jobKey, user));
|
||||
}
|
||||
|
||||
@PostMapping("/{jobKey}/cancel")
|
||||
public ListAsyncJobStatusDto cancel(@PathVariable Long prjctIdx, @PathVariable String jobKey,
|
||||
RequestUserContext user) {
|
||||
access.require(prjctIdx, jobKey, user);
|
||||
executor.cancel(jobKey, user.getUserId());
|
||||
return toStatus(access.require(prjctIdx, jobKey, user));
|
||||
}
|
||||
|
||||
@GetMapping("/{jobKey}/sid-failures")
|
||||
public ListAsyncSidFailuresDto sidFailures(
|
||||
@PathVariable Long prjctIdx, @PathVariable String jobKey,
|
||||
@RequestParam(defaultValue = "0") int offset, @RequestParam(defaultValue = "100") int limit,
|
||||
RequestUserContext user) {
|
||||
var job = access.require(prjctIdx, jobKey, user);
|
||||
if (offset < 0 || limit < 1 || limit > 1000) {
|
||||
throw BusinessException.badRequest("INVALID_FAILURE_PAGE", "offset은 0 이상, limit은 1~1000이어야 합니다.");
|
||||
}
|
||||
return ListAsyncSidFailuresDto.from(job, offset, limit);
|
||||
}
|
||||
|
||||
@GetMapping("/{jobKey}/range-failures")
|
||||
public java.util.Map<String,Object> rangeFailures(@PathVariable Long prjctIdx, @PathVariable String jobKey,
|
||||
@RequestParam(defaultValue = "0") int offset, @RequestParam(defaultValue = "100") int limit, RequestUserContext user) {
|
||||
return failurePage(prjctIdx, jobKey, offset, limit, user, "rangeFailures");
|
||||
}
|
||||
|
||||
@GetMapping("/{jobKey}/validation-errors")
|
||||
public java.util.Map<String,Object> validationErrors(@PathVariable Long prjctIdx, @PathVariable String jobKey,
|
||||
@RequestParam(defaultValue = "0") int offset, @RequestParam(defaultValue = "100") int limit, RequestUserContext user) {
|
||||
return failurePage(prjctIdx, jobKey, offset, limit, user, "validationErrors");
|
||||
}
|
||||
|
||||
private java.util.Map<String,Object> failurePage(Long prjctIdx, String jobKey, int offset, int limit, RequestUserContext user, String field) {
|
||||
var job = access.require(prjctIdx, jobKey, user);
|
||||
if (offset < 0 || limit < 1 || limit > 1000) throw BusinessException.badRequest("INVALID_FAILURE_PAGE", "조회 범위가 올바르지 않습니다.");
|
||||
var detail = job.getErrorDetailJson();
|
||||
var failures = detail != null && detail.get(field) instanceof List<?> values ? values : List.of();
|
||||
int start = Math.min(offset, failures.size());
|
||||
return java.util.Map.of("total", failures.size(), "offset", offset,
|
||||
"items", failures.subList(start, (int) Math.min(failures.size(), (long) start + limit)));
|
||||
}
|
||||
|
||||
@GetMapping("/{jobKey}/files/{index}")
|
||||
public void download(@PathVariable Long prjctIdx, @PathVariable String jobKey,
|
||||
@PathVariable int index, RequestUserContext user, HttpServletResponse response) throws IOException {
|
||||
TListAsyncJobEntity job = access.require(prjctIdx, jobKey, user);
|
||||
List<ListAsyncResultFileDto> results = job.getResultFilesJson();
|
||||
if (results == null || index < 0 || index >= results.size() || !results.get(index).ready()) {
|
||||
throw BusinessException.notFound("LIST_RESULT_FILE_NOT_FOUND", "결과 파일을 찾을 수 없습니다.");
|
||||
}
|
||||
ListAsyncResultFileDto result = results.get(index);
|
||||
files.download(result.fileRef(), path -> {
|
||||
response.setContentType("application/octet-stream");
|
||||
response.setHeader(HttpHeaders.CACHE_CONTROL, "no-store");
|
||||
response.setHeader(HttpHeaders.CONTENT_DISPOSITION,
|
||||
ContentDisposition.attachment().filename(result.fileName(), StandardCharsets.UTF_8).build().toString());
|
||||
response.setContentLengthLong(Files.size(path));
|
||||
Files.copy(path, response.getOutputStream());
|
||||
response.flushBuffer();
|
||||
});
|
||||
}
|
||||
|
||||
private ListAsyncJobStatusDto toStatus(TListAsyncJobEntity job) {
|
||||
var results = job.getResultFilesJson() == null
|
||||
? List.<ListAsyncResultFileDto>of() : job.getResultFilesJson();
|
||||
var resultViews = IntStream.range(0, results.size()).mapToObj(index -> {
|
||||
var file = results.get(index);
|
||||
return new ListAsyncJobStatusDto.ResultFile(index, file.type(), file.fileName(),
|
||||
file.sizeBytes(), file.ready() && files.exists(file.fileRef()));
|
||||
}).toList();
|
||||
long failed = ListAsyncJobStore.failedSidCount(job);
|
||||
long processed = processedSidCount(job);
|
||||
Long remaining = job.getTotalSidCnt() == null ? null : Math.max(0L, job.getTotalSidCnt() - processed);
|
||||
return new ListAsyncJobStatusDto(job.getJobKey(), job.getJobType(),
|
||||
ListAsyncAssignOperation.fromRequest(job.getJobType(), job.getRequestJson()), job.getJobStatus(),
|
||||
job.getJobStage(), job.getTotalSidCnt(), job.getCompletedSidCnt(), job.getCompletedChunkCnt(),
|
||||
failed, processed, remaining,
|
||||
job.getProgressJson(), job.getFailedChunkNo(), job.getErrorCode(), job.getErrorMessage(),
|
||||
job.getCancelReason(), job.getRegDt(), job.getStartedDt(), job.getFinishedDt(), resultViews);
|
||||
}
|
||||
|
||||
private static long processedSidCount(TListAsyncJobEntity job) {
|
||||
return Math.addExact(job.getCompletedSidCnt(), ListAsyncJobStore.failedSidCount(job));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.kri.contact.server.controller;
|
||||
|
||||
import com.kri.common.feature.web.BusinessException;
|
||||
import com.kri.common.feature.web.RequestUserContext;
|
||||
import com.kri.contact.server.common.enums.ListAsyncJobType;
|
||||
import com.kri.contact.server.dto.list.ListResetRequestDto;
|
||||
import com.kri.contact.server.dto.list.ListUploadRequestDto;
|
||||
import com.kri.contact.server.dto.list.async.ListAsyncJobAcceptedDto;
|
||||
import com.kri.contact.server.service.async.ListAsyncJobAccess;
|
||||
import com.kri.contact.server.service.async.ListAsyncMaintenanceService;
|
||||
import com.kri.contact.server.service.file.ListFileService;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.validation.Valid;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.ContentDisposition;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/list/jobs/{prjctIdx}")
|
||||
@RequiredArgsConstructor
|
||||
@Tag(name = "ListAsyncMaintenance", description = "리스트 비동기 업로드 및 초기화 API")
|
||||
public class ListAsyncMaintenanceController {
|
||||
private final ListAsyncMaintenanceService maintenance;
|
||||
private final ListAsyncJobAccess access;
|
||||
private final ListFileService sources;
|
||||
|
||||
@PostMapping("/reset")
|
||||
public ListAsyncJobAcceptedDto reset(@PathVariable Long prjctIdx,
|
||||
@Valid @RequestBody ListResetRequestDto request, RequestUserContext user) {
|
||||
return maintenance.reset(prjctIdx, request, user);
|
||||
}
|
||||
|
||||
@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
||||
public ListAsyncJobAcceptedDto upload(@PathVariable Long prjctIdx, @RequestParam("file") MultipartFile file,
|
||||
@Valid @RequestPart("request") ListUploadRequestDto request, RequestUserContext user) {
|
||||
return maintenance.upload(prjctIdx, file, request, user);
|
||||
}
|
||||
|
||||
@GetMapping("/{jobKey}/source-file")
|
||||
public void source(@PathVariable Long prjctIdx, @PathVariable String jobKey, RequestUserContext user,
|
||||
HttpServletResponse response) throws IOException {
|
||||
var job = access.require(prjctIdx, jobKey, user);
|
||||
if (job.getJobType() != ListAsyncJobType.LIST_UPLOAD || job.getInputFileRef() == null) {
|
||||
throw BusinessException.notFound("LIST_UPLOAD_FILE_NOT_FOUND", "원본 파일을 찾을 수 없습니다.");
|
||||
}
|
||||
response.setContentType("application/octet-stream");
|
||||
response.setHeader(HttpHeaders.CACHE_CONTROL, "no-store");
|
||||
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, ContentDisposition.attachment()
|
||||
.filename("upload_source.xlsx", StandardCharsets.UTF_8).build().toString());
|
||||
sources.copyListFile(prjctIdx, job.getInputFileRef(), response.getOutputStream());
|
||||
response.flushBuffer();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.kri.contact.server.controller;
|
||||
|
||||
import com.kri.common.feature.web.BusinessException;
|
||||
import com.kri.common.feature.web.RequestUserContext;
|
||||
import com.kri.contact.server.common.enums.ChannelType;
|
||||
import com.kri.contact.server.dto.list.async.ListAsyncJobAcceptedDto;
|
||||
import com.kri.contact.server.dto.message.MessageSendBulkRequestDto;
|
||||
import com.kri.contact.server.service.async.ListAsyncJobAccess;
|
||||
import com.kri.contact.server.service.async.ListAsyncMessageService;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.validation.Valid;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/list/jobs/{prjctIdx}")
|
||||
@RequiredArgsConstructor
|
||||
@Tag(name = "ListAsyncMessage", description = "리스트 비동기 메시지 발송 API")
|
||||
public class ListAsyncMessageController {
|
||||
private final ListAsyncMessageService messages;
|
||||
private final ListAsyncJobAccess access;
|
||||
|
||||
@PostMapping("/messages/email")
|
||||
public ListAsyncJobAcceptedDto email(@PathVariable Long prjctIdx, @Valid @RequestBody MessageSendBulkRequestDto request, RequestUserContext user) {
|
||||
return messages.submit(prjctIdx, ChannelType.EMAIL, request, user);
|
||||
}
|
||||
@PostMapping("/messages/sms")
|
||||
public ListAsyncJobAcceptedDto sms(@PathVariable Long prjctIdx, @Valid @RequestBody MessageSendBulkRequestDto request, RequestUserContext user) {
|
||||
return messages.submit(prjctIdx, ChannelType.SMS, request, user);
|
||||
}
|
||||
@PostMapping("/messages/kko")
|
||||
public ListAsyncJobAcceptedDto kko(@PathVariable Long prjctIdx, @Valid @RequestBody MessageSendBulkRequestDto request, RequestUserContext user) {
|
||||
return messages.submit(prjctIdx, ChannelType.KKO_AT, request, user);
|
||||
}
|
||||
public record ReportPage(int total, int offset, List<?> items) {}
|
||||
|
||||
@GetMapping("/{jobKey}/message-report/{section}")
|
||||
public ReportPage report(@PathVariable Long prjctIdx, @PathVariable String jobKey, @PathVariable String section,
|
||||
@RequestParam(defaultValue = "0") int offset, @RequestParam(defaultValue = "100") int limit, RequestUserContext user) {
|
||||
var job = access.require(prjctIdx, jobKey, user);
|
||||
String field = switch (section) {
|
||||
case "batches" -> "messageBatches";
|
||||
case "preparation-failures" -> "preparationFailures";
|
||||
default -> throw BusinessException.badRequest("INVALID_REPORT_SECTION", "조회 항목이 올바르지 않습니다.");
|
||||
};
|
||||
if (offset < 0 || limit < 1 || limit > 1000) throw BusinessException.badRequest("INVALID_REPORT_PAGE", "조회 범위가 올바르지 않습니다.");
|
||||
var detail = job.getErrorDetailJson() == null ? Map.<String,Object>of() : job.getErrorDetailJson();
|
||||
List<?> items = detail.get(field) instanceof List<?> values ? values : List.of();
|
||||
int start = Math.min(offset, items.size());
|
||||
return new ReportPage(items.size(), offset, items.subList(start, (int) Math.min(items.size(), (long) start + limit)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.kri.contact.server.controller;
|
||||
|
||||
import com.kri.common.feature.web.RequestUserContext;
|
||||
import com.kri.contact.server.dto.list.ListAssignRequestDto;
|
||||
import com.kri.contact.server.dto.list.ListAssignResetRequestDto;
|
||||
import com.kri.contact.server.dto.list.ListUpdateRequestDto;
|
||||
import com.kri.contact.server.dto.list.async.ListAsyncJobAcceptedDto;
|
||||
import com.kri.contact.server.service.async.ListAsyncMutationService;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/list/jobs/{prjctIdx}/mutations")
|
||||
@RequiredArgsConstructor
|
||||
@Tag(name = "ListAsyncMutation", description = "리스트 비동기 수정 및 배정 API")
|
||||
public class ListAsyncMutationController {
|
||||
private final ListAsyncMutationService mutations;
|
||||
|
||||
@PostMapping("/update")
|
||||
public ListAsyncJobAcceptedDto update(@PathVariable Long prjctIdx,
|
||||
@Valid @RequestBody ListUpdateRequestDto request, RequestUserContext user) {
|
||||
return mutations.update(prjctIdx, request, user);
|
||||
}
|
||||
|
||||
@PostMapping("/assign/{assignId}")
|
||||
public ListAsyncJobAcceptedDto assign(@PathVariable Long prjctIdx, @PathVariable String assignId,
|
||||
@Valid @RequestBody ListAssignRequestDto request, RequestUserContext user) {
|
||||
return mutations.assign(prjctIdx, assignId, request, user);
|
||||
}
|
||||
|
||||
@PostMapping("/assign-reset")
|
||||
public ListAsyncJobAcceptedDto resetAssignments(@PathVariable Long prjctIdx,
|
||||
@Valid @RequestBody ListAssignResetRequestDto request, RequestUserContext user) {
|
||||
return mutations.resetAssignments(prjctIdx, request, user);
|
||||
}
|
||||
}
|
||||
@@ -7,19 +7,14 @@ import com.kri.common.feature.web.BusinessException;
|
||||
import com.kri.common.feature.web.RequestUserContext;
|
||||
import com.kri.common.feature.web.page.PageResponseDto;
|
||||
import com.kri.contact.server.common.enums.ListSearchResponseType;
|
||||
import com.kri.contact.server.dto.excel.ExcelUploadResultDto;
|
||||
import com.kri.contact.server.dto.list.ListFilterDto;
|
||||
import com.kri.contact.server.dto.list.ListResetRequestDto;
|
||||
import com.kri.contact.server.dto.list.ListSearchRequestDto;
|
||||
import com.kri.contact.server.dto.list.ListSearchWithFullResultDto;
|
||||
import com.kri.contact.server.dto.list.ListUpdateRequestDto;
|
||||
import com.kri.contact.server.dto.list.ListUploadFileInfoDto;
|
||||
import com.kri.contact.server.dto.list.ListUploadHistoryDto;
|
||||
import com.kri.contact.server.dto.list.ListUploadRequestDto;
|
||||
import com.kri.contact.server.dto.project.ProjectInfoDto;
|
||||
import com.kri.contact.server.dto.systemlog.UploadContentDto;
|
||||
import com.kri.contact.server.service.ListDownloadService;
|
||||
import com.kri.contact.server.service.ListResetService;
|
||||
import com.kri.contact.server.service.ListSearchService;
|
||||
import com.kri.contact.server.service.ListUpdateService;
|
||||
import com.kri.contact.server.service.ListUploadService;
|
||||
@@ -38,7 +33,6 @@ import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PatchMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
@@ -46,7 +40,6 @@ import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RequestPart;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
@@ -61,7 +54,6 @@ public class ListController {
|
||||
private final ListSearchService listSearchService;
|
||||
private final ListUploadService listUploadService;
|
||||
private final ListUpdateService listUpdateService;
|
||||
private final ListResetService listResetService;
|
||||
private final SystemEventLogService systemEventLogService;
|
||||
|
||||
@Operation(summary = "리스트 정보 조회 With Sid")
|
||||
@@ -112,49 +104,6 @@ public class ListController {
|
||||
return buildExcelResponse(uploadFile.getFileName(), uploadFile.getBytes());
|
||||
}
|
||||
|
||||
@Operation(summary = "리스트 다운로드")
|
||||
@PostMapping("/excel/download/{prjctIdx}")
|
||||
public ResponseEntity<byte[]> downloadList(
|
||||
@PathVariable Long prjctIdx,
|
||||
@RequestParam(defaultValue = "true") boolean includeResult,
|
||||
@Valid @RequestBody(required = false) ListFilterDto listFilter
|
||||
) {
|
||||
ProjectInfoDto projectInfoDto = getProjectOrThrow(prjctIdx);
|
||||
byte[] bytes = listDownloadService.downloadList(
|
||||
prjctIdx,
|
||||
listFilter,
|
||||
includeResult
|
||||
);
|
||||
return buildExcelResponse(projectInfoDto.getPrjctName() + " list.xlsx", bytes);
|
||||
}
|
||||
|
||||
@Operation(summary = "리스트 수정 이력 다운로드")
|
||||
@PostMapping("/excel/download/update/{prjctIdx}")
|
||||
public ResponseEntity<byte[]> downloadListUpdateHistory(
|
||||
@PathVariable Long prjctIdx,
|
||||
@Valid @RequestBody(required = false) ListFilterDto listFilter
|
||||
) {
|
||||
ProjectInfoDto projectInfoDto = getProjectOrThrow(prjctIdx);
|
||||
byte[] bytes = listDownloadService.downloadSidWithUpdateHist(prjctIdx, listFilter);
|
||||
return buildExcelResponse(projectInfoDto.getPrjctName() + " 수정 이력.xlsx", bytes);
|
||||
}
|
||||
|
||||
@Operation(summary = "리스트 파일 업로드")
|
||||
@PostMapping(value = "/excel/upload/{prjctIdx}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
||||
public ExcelUploadResultDto uploadExcel(
|
||||
@PathVariable Long prjctIdx,
|
||||
@RequestParam("file") MultipartFile file,
|
||||
@Valid @RequestPart("request") ListUploadRequestDto request,
|
||||
RequestUserContext ctx
|
||||
) {
|
||||
getProjectOrThrow(prjctIdx);
|
||||
|
||||
LocalDateTime now = DateTimeUtil.nowKstLocalDateTime();
|
||||
ExcelUploadResultDto result = listUploadService.uploadExcel(prjctIdx, file, request, ctx.getUserId(), now);
|
||||
systemEventLogService.writeUploadList(ctx.getUserId(), now, ctx.getClientIp(), prjctIdx, UploadContentDto.from(file));
|
||||
return result;
|
||||
}
|
||||
|
||||
@Operation(summary = "리스트 수정")
|
||||
@PatchMapping("/update/{prjctIdx}")
|
||||
public int updateList(
|
||||
@@ -189,17 +138,6 @@ public class ListController {
|
||||
systemEventLogService.writeUploadListItemFiles(ctx.getUserId(), now, ctx.getClientIp(), prjctIdx, contents);
|
||||
}
|
||||
|
||||
@Operation(summary = "리스트 별 업로드 파일 다운로드")
|
||||
@PostMapping("/item-file/download/{prjctIdx}")
|
||||
public ResponseEntity<byte[]> downloadItemFile(
|
||||
@PathVariable Long prjctIdx,
|
||||
@Valid @RequestBody(required = false) ListFilterDto listFilter
|
||||
){
|
||||
ProjectInfoDto projectInfoDto = getProjectOrThrow(prjctIdx);
|
||||
byte[] bytes = listDownloadService.downloadItemFile(prjctIdx, listFilter);
|
||||
return buildZipResponse(projectInfoDto.getPrjctName() + " item_files.zip", bytes);
|
||||
}
|
||||
|
||||
@Operation(summary = "리스트 단건 파일 다운로드")
|
||||
@GetMapping("/item-file/download/{prjctIdx}/{sid}/{listHderId}")
|
||||
public ResponseEntity<byte[]> downloadOneItemFile(
|
||||
@@ -212,25 +150,6 @@ public class ListController {
|
||||
return buildFileResponse(file.getFileName(), file.getBytes());
|
||||
}
|
||||
|
||||
@Operation(summary = "리스트 초기화")
|
||||
@DeleteMapping("/{prjctIdx}")
|
||||
public int resetList(
|
||||
@PathVariable Long prjctIdx,
|
||||
@Valid @RequestBody ListResetRequestDto request,
|
||||
RequestUserContext ctx
|
||||
) {
|
||||
LocalDateTime now = DateTimeUtil.nowKstLocalDateTime();
|
||||
int resetCount = listResetService.resetByFilter(
|
||||
prjctIdx,
|
||||
request.getListFilter(),
|
||||
request.getResetMode(),
|
||||
ctx.getUserId(),
|
||||
now
|
||||
);
|
||||
systemEventLogService.writeResetList(ctx.getUserId(), now, ctx.getClientIp(), prjctIdx, request);
|
||||
return resetCount;
|
||||
}
|
||||
|
||||
private ProjectInfoDto getProjectOrThrow(Long prjctIdx) {
|
||||
ProjectInfoDto projectInfoDto = projectService.getProject(prjctIdx);
|
||||
if (projectInfoDto == null) {
|
||||
@@ -239,16 +158,6 @@ public class ListController {
|
||||
return projectInfoDto;
|
||||
}
|
||||
|
||||
private ResponseEntity<byte[]> buildZipResponse(String fileName, byte[] bytes) {
|
||||
String contentDisposition = "attachment; filename*=UTF-8''" +
|
||||
URLEncoder.encode(fileName, StandardCharsets.UTF_8).replace("+", "%20");
|
||||
|
||||
return ResponseEntity.ok()
|
||||
.header(HttpHeaders.CONTENT_DISPOSITION, contentDisposition)
|
||||
.contentType(MediaType.parseMediaType("application/zip"))
|
||||
.body(bytes);
|
||||
}
|
||||
|
||||
private ResponseEntity<byte[]> buildFileResponse(String fileName, byte[] bytes) {
|
||||
String contentDisposition = "attachment; filename*=UTF-8''" +
|
||||
URLEncoder.encode(fileName, StandardCharsets.UTF_8).replace("+", "%20");
|
||||
|
||||
@@ -1,21 +1,14 @@
|
||||
package com.kri.contact.server.controller;
|
||||
|
||||
import static com.kri.contact.server.common.util.ExcelResponseBuilder.buildExcelResponse;
|
||||
|
||||
import com.kri.common.feature.datetime.DateTimeUtil;
|
||||
import com.kri.common.feature.web.BusinessException;
|
||||
import com.kri.common.feature.web.RequestUserContext;
|
||||
import com.kri.contact.server.dto.list.ListEventHistDownloadRequestDto;
|
||||
import com.kri.contact.server.dto.list.ListEventHistFromSurveyDto;
|
||||
import com.kri.contact.server.dto.list.ListEventHistInsertRequestDto;
|
||||
import com.kri.contact.server.dto.list.ListEventHistSurveyMetaDto;
|
||||
import com.kri.contact.server.dto.list.ListEventHistoryDto;
|
||||
import com.kri.contact.server.dto.list.ListUpdateHistoryDto;
|
||||
import com.kri.contact.server.dto.project.ProjectInfoDto;
|
||||
import com.kri.contact.server.service.ListDownloadService;
|
||||
import com.kri.contact.server.service.ListHistoryQueryService;
|
||||
import com.kri.contact.server.service.ListUpdateService;
|
||||
import com.kri.contact.server.service.ProjectService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.validation.Valid;
|
||||
@@ -30,7 +23,6 @@ import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@@ -39,8 +31,6 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
@Tag(name = "ListHistory", description = "리스트 이력 API")
|
||||
public class ListHistoryController {
|
||||
|
||||
private final ProjectService projectService;
|
||||
private final ListDownloadService listDownloadService;
|
||||
private final ListHistoryQueryService listHistoryQueryService;
|
||||
private final ListUpdateService listUpdateService;
|
||||
|
||||
@@ -62,26 +52,6 @@ public class ListHistoryController {
|
||||
return listHistoryQueryService.findUpdateHistories(prjctIdx, sid);
|
||||
}
|
||||
|
||||
@Operation(summary = "리스트 이벤트 이력 다운로드")
|
||||
@PostMapping("/excel/download/event/{prjctIdx}")
|
||||
public ResponseEntity<byte[]> downloadListWithEventHistory(
|
||||
@PathVariable Long prjctIdx,
|
||||
@RequestParam(defaultValue = "true") boolean includeResult,
|
||||
@Valid @RequestBody ListEventHistDownloadRequestDto request
|
||||
) {
|
||||
ProjectInfoDto projectInfoDto = getProjectOrThrow(prjctIdx);
|
||||
byte[] bytes = listDownloadService.downloadListWithEventHist(
|
||||
prjctIdx,
|
||||
request.getListFilter(),
|
||||
request.getResultCodeTypes(),
|
||||
request.getSearchBase(),
|
||||
request.getDateRange(),
|
||||
request.getHistoryOrder(),
|
||||
includeResult
|
||||
);
|
||||
return buildExcelResponse(projectInfoDto.getPrjctName() + " 컨택 이력.xlsx", bytes);
|
||||
}
|
||||
|
||||
@Operation(description = "설문 상태 추가")
|
||||
@PostMapping("/survey")
|
||||
public void putSurveyHistory(
|
||||
@@ -139,12 +109,4 @@ public class ListHistoryController {
|
||||
}
|
||||
return "SYSTEM";
|
||||
}
|
||||
|
||||
private ProjectInfoDto getProjectOrThrow(Long prjctIdx) {
|
||||
ProjectInfoDto projectInfoDto = projectService.getProject(prjctIdx);
|
||||
if (projectInfoDto == null) {
|
||||
throw BusinessException.internalError("프로젝트가 없습니다.");
|
||||
}
|
||||
return projectInfoDto;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package com.kri.contact.server.controller;
|
||||
|
||||
import com.kri.common.feature.web.RequestUserContext;
|
||||
import com.kri.contact.server.dto.message.MessageSendBulkResponseDto;
|
||||
import com.kri.contact.server.dto.message.MessageSendBulkRequestDto;
|
||||
import com.kri.contact.server.dto.message.MessageSendSingleRequestDto;
|
||||
import com.kri.contact.server.dto.message.MessageSendSingleResponseDto;
|
||||
import com.kri.contact.server.service.MessageSendService;
|
||||
@@ -34,16 +32,6 @@ public class MessageSendController {
|
||||
return messageSendService.sendEmailSingle(prjctIdx, request, ctx);
|
||||
}
|
||||
|
||||
@Operation(summary = "Email bulk send")
|
||||
@PostMapping("/email/bulk/{prjctIdx}")
|
||||
public MessageSendBulkResponseDto sendEmailBulk(
|
||||
@PathVariable Long prjctIdx,
|
||||
@Valid @RequestBody MessageSendBulkRequestDto request,
|
||||
RequestUserContext ctx
|
||||
) {
|
||||
return messageSendService.sendEmailBulk(prjctIdx, request, ctx);
|
||||
}
|
||||
|
||||
@Operation(summary = "Kakao Alimtalk single send")
|
||||
@PostMapping("/kko/single/{prjctIdx}")
|
||||
public MessageSendSingleResponseDto sendKkoSingle(
|
||||
@@ -54,16 +42,6 @@ public class MessageSendController {
|
||||
return messageSendService.sendKkoSingle(prjctIdx, request, ctx);
|
||||
}
|
||||
|
||||
@Operation(summary = "Kakao Alimtalk bulk send")
|
||||
@PostMapping("/kko/bulk/{prjctIdx}")
|
||||
public MessageSendBulkResponseDto sendKkoBulk(
|
||||
@PathVariable Long prjctIdx,
|
||||
@Valid @RequestBody MessageSendBulkRequestDto request,
|
||||
RequestUserContext ctx
|
||||
) {
|
||||
return messageSendService.sendKkoBulk(prjctIdx, request, ctx);
|
||||
}
|
||||
|
||||
@Operation(summary = "SMS single send")
|
||||
@PostMapping("/sms/single/{prjctIdx}")
|
||||
public MessageSendSingleResponseDto sendSmsSingle(
|
||||
@@ -73,14 +51,4 @@ public class MessageSendController {
|
||||
) {
|
||||
return messageSendService.sendSmsSingle(prjctIdx, request, ctx);
|
||||
}
|
||||
|
||||
@Operation(summary = "SMS bulk send")
|
||||
@PostMapping("/sms/bulk/{prjctIdx}")
|
||||
public MessageSendBulkResponseDto sendSmsBulk(
|
||||
@PathVariable Long prjctIdx,
|
||||
@Valid @RequestBody MessageSendBulkRequestDto request,
|
||||
RequestUserContext ctx
|
||||
) {
|
||||
return messageSendService.sendSmsBulk(prjctIdx, request, ctx);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.kri.contact.server.dto.list.async;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public record ListAsyncActiveJobsDto(List<ListAsyncJobStatusDto> items) {}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.kri.contact.server.dto.list.async;
|
||||
|
||||
import com.kri.contact.server.common.enums.ListAsyncJobStatus;
|
||||
import com.kri.contact.server.common.enums.ListAsyncJobType;
|
||||
import com.kri.contact.server.common.enums.ListAsyncAssignOperation;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
public record ListAsyncAvailabilityDto(ListAsyncJobType requestedJobType, boolean allowed,
|
||||
String reasonCode, int blockerCount, List<Blocker> blockers) {
|
||||
public record Blocker(ListAsyncJobType jobType, ListAsyncJobStatus jobStatus,
|
||||
String blockingPhase, String requestUserId, String requestUserName,
|
||||
boolean mine, String jobKey, LocalDateTime regDt, ListAsyncAssignOperation operation) {}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
package com.kri.contact.server.dto.list.async;
|
||||
|
||||
public record ListAsyncJobAcceptedDto(String jobKey) {}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.kri.contact.server.dto.list.async;
|
||||
|
||||
import com.kri.contact.server.common.enums.ListAsyncJobStatus;
|
||||
import com.kri.contact.server.common.enums.ListAsyncJobType;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
// Scalar projection avoids OSIV entity staleness and loading request/result/error JSON.
|
||||
public record ListAsyncJobActivity(String jobKey, ListAsyncJobType jobType,
|
||||
ListAsyncJobStatus jobStatus, String requestUser, LocalDateTime regDt, String operation) {}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.kri.contact.server.dto.list.async;
|
||||
|
||||
import com.kri.contact.server.common.enums.ListAsyncJobStatus;
|
||||
import com.kri.contact.server.common.enums.ListAsyncJobType;
|
||||
import com.kri.contact.server.common.enums.ListAsyncAssignOperation;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public record ListAsyncJobStatusDto(
|
||||
String jobKey, ListAsyncJobType jobType, ListAsyncAssignOperation operation,
|
||||
ListAsyncJobStatus jobStatus, String jobStage,
|
||||
Long totalSidCnt, long completedSidCnt, long completedChunkCnt,
|
||||
long failedSidCnt, long processedSidCnt, Long remainingSidCnt,
|
||||
Map<String, Object> progress, Integer failedChunkNo, String errorCode, String errorMessage,
|
||||
String cancelReason, LocalDateTime regDt, LocalDateTime startedDt, LocalDateTime finishedDt,
|
||||
List<ResultFile> resultFiles
|
||||
) {
|
||||
public record ResultFile(int index, String type, String fileName, long sizeBytes, boolean ready) {}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.kri.contact.server.dto.list.async;
|
||||
|
||||
public record ListAsyncResultFileDto(
|
||||
String type, String fileRef, String fileName, long sizeBytes, boolean ready
|
||||
) {}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.kri.contact.server.dto.list.async;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import com.kri.contact.server.persistence.entity.TListAsyncJobEntity;
|
||||
import com.kri.contact.server.service.async.ListAsyncJobStore;
|
||||
|
||||
public record ListAsyncSidFailuresDto(long total, int offset, List<Failure> items) {
|
||||
public record Failure(String sid, String code, int chunkNo) {}
|
||||
|
||||
public static ListAsyncSidFailuresDto from(TListAsyncJobEntity job, int offset, int limit) {
|
||||
var items = new ArrayList<Failure>();
|
||||
long seen = 0;
|
||||
Object groups = job.getErrorDetailJson() == null ? null
|
||||
: job.getErrorDetailJson().get("sidFailureGroups");
|
||||
if (groups instanceof List<?> entries) {
|
||||
for (Object entry : entries) {
|
||||
if (entry instanceof Map<?, ?> group && group.get("sids") instanceof List<?> sids) {
|
||||
for (Object sid : sids) {
|
||||
if (seen++ >= offset && items.size() < limit) {
|
||||
items.add(new Failure((String) sid, (String) group.get("code"),
|
||||
((Number) group.get("chunkNo")).intValue()));
|
||||
}
|
||||
if (items.size() == limit) {
|
||||
return new ListAsyncSidFailuresDto(ListAsyncJobStore.failedSidCount(job), offset, items);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return new ListAsyncSidFailuresDto(ListAsyncJobStore.failedSidCount(job), offset, items);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.kri.contact.server.dto.list.async;
|
||||
|
||||
import com.kri.contact.server.common.enums.ListAsyncJobStatus;
|
||||
import com.kri.contact.server.common.enums.ListAsyncJobType;
|
||||
|
||||
// 프로젝트의 다른 사용자에게도 응답한다. 요청자·요청 본문·오류 상세·파일 정보를 담지 않는다.
|
||||
public record ListUploadResetActiveDto(boolean active, Job job) {
|
||||
public record Job(ListAsyncJobType jobType, ListAsyncJobStatus jobStatus, String jobStage,
|
||||
Long totalSidCnt, long processedSidCnt, boolean mine, String jobKey) {}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.kri.contact.server.persistence.entity;
|
||||
|
||||
import com.kri.contact.server.persistence.entity.id.TListAsyncJobChunkId;
|
||||
import io.hypersistence.utils.hibernate.type.json.JsonType;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.EmbeddedId;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Table;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.hibernate.annotations.Type;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Entity
|
||||
@Table(name = "t_list_async_job_chunk")
|
||||
public class TListAsyncJobChunkEntity {
|
||||
@EmbeddedId
|
||||
private TListAsyncJobChunkId id;
|
||||
|
||||
@Column(name = "operation_type", length = 32, nullable = false)
|
||||
private String operationType;
|
||||
|
||||
@Type(JsonType.class)
|
||||
@Column(name = "completed_sids_json", columnDefinition = "json", nullable = false)
|
||||
private List<String> completedSidsJson;
|
||||
|
||||
@Column(name = "completed_sid_cnt", nullable = false)
|
||||
private Integer completedSidCnt;
|
||||
|
||||
@Column(name = "recorded_dt", nullable = false)
|
||||
private LocalDateTime recordedDt;
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
package com.kri.contact.server.persistence.entity;
|
||||
|
||||
import com.kri.contact.server.common.enums.ListAsyncJobStatus;
|
||||
import com.kri.contact.server.common.enums.ListAsyncJobType;
|
||||
import com.kri.contact.server.dto.list.async.ListAsyncResultFileDto;
|
||||
import io.hypersistence.utils.hibernate.type.json.JsonType;
|
||||
import jakarta.persistence.*;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.hibernate.annotations.Type;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Entity
|
||||
@Table(name = "t_list_async_job")
|
||||
public class TListAsyncJobEntity {
|
||||
|
||||
@Id
|
||||
@Column(name = "job_key", length = 32, nullable = false)
|
||||
private String jobKey;
|
||||
|
||||
@Column(name = "prjct_idx", columnDefinition = "int unsigned", nullable = false)
|
||||
private Long prjctIdx;
|
||||
|
||||
@Enumerated(EnumType.STRING)
|
||||
@Column(name = "job_type", length = 32, nullable = false)
|
||||
private ListAsyncJobType jobType;
|
||||
|
||||
@Column(name = "request_user", length = 32, nullable = false)
|
||||
private String requestUser;
|
||||
|
||||
@Enumerated(EnumType.STRING)
|
||||
@Column(name = "job_status", length = 32, nullable = false)
|
||||
private ListAsyncJobStatus jobStatus;
|
||||
|
||||
@Column(name = "job_stage", length = 32, nullable = false)
|
||||
private String jobStage;
|
||||
|
||||
@Type(JsonType.class)
|
||||
@Column(name = "request_json", columnDefinition = "json", nullable = false)
|
||||
private Map<String, Object> requestJson;
|
||||
|
||||
@Column(name = "input_file_ref", length = 512)
|
||||
private String inputFileRef;
|
||||
|
||||
@Column(name = "total_sid_cnt")
|
||||
private Long totalSidCnt;
|
||||
|
||||
@Column(name = "completed_sid_cnt", nullable = false)
|
||||
private long completedSidCnt;
|
||||
|
||||
@Column(name = "completed_chunk_cnt", nullable = false)
|
||||
private long completedChunkCnt;
|
||||
|
||||
@Type(JsonType.class)
|
||||
@Column(name = "progress_json", columnDefinition = "json")
|
||||
private Map<String, Object> progressJson;
|
||||
|
||||
@Column(name = "list_upload_log_id", length = 32)
|
||||
private String listUploadLogId;
|
||||
|
||||
@Column(name = "list_reset_log_id", length = 32)
|
||||
private String listResetLogId;
|
||||
|
||||
@Column(name = "failed_chunk_no")
|
||||
private Integer failedChunkNo;
|
||||
|
||||
@Column(name = "error_code", length = 64)
|
||||
private String errorCode;
|
||||
|
||||
@Column(name = "error_message", length = 2048)
|
||||
private String errorMessage;
|
||||
|
||||
@Type(JsonType.class)
|
||||
@Column(name = "error_detail_json", columnDefinition = "json")
|
||||
private Map<String, Object> errorDetailJson;
|
||||
|
||||
@Column(name = "cancel_reason", length = 64)
|
||||
private String cancelReason;
|
||||
|
||||
@Column(name = "cancel_request_user", length = 32)
|
||||
private String cancelRequestUser;
|
||||
|
||||
@Column(name = "cancel_requested_dt")
|
||||
private LocalDateTime cancelRequestedDt;
|
||||
|
||||
@Type(JsonType.class)
|
||||
@Column(name = "result_files_json", columnDefinition = "json")
|
||||
private List<ListAsyncResultFileDto> resultFilesJson;
|
||||
|
||||
@Column(name = "owner_instance", length = 64, nullable = false)
|
||||
private String ownerInstance;
|
||||
|
||||
@Column(name = "reg_dt", nullable = false)
|
||||
private LocalDateTime regDt;
|
||||
|
||||
@Column(name = "mod_dt", nullable = false)
|
||||
private LocalDateTime modDt;
|
||||
|
||||
@Column(name = "started_dt")
|
||||
private LocalDateTime startedDt;
|
||||
|
||||
@Column(name = "finished_dt")
|
||||
private LocalDateTime finishedDt;
|
||||
}
|
||||
@@ -154,23 +154,6 @@ public class TListCallHistEntity {
|
||||
.build();
|
||||
}
|
||||
|
||||
public void updateCallResult(ListCallCloseRequestDto request, LocalDateTime endDt) {
|
||||
if (isCompletedRequest(request)) {
|
||||
this.endDt = endDt;
|
||||
this.callStatus = CallStatus.CALL_CLOSE;
|
||||
}
|
||||
if (isPositive(request.getTotalTime())) {
|
||||
this.totalTime = request.getTotalTime();
|
||||
}
|
||||
if (isPositive(request.getTalkTime())) {
|
||||
this.talkTime = request.getTalkTime();
|
||||
}
|
||||
this.mediaServerId = request.getMediaServerId();
|
||||
this.mediaServerName = request.getMediaServerName();
|
||||
if (hasText(request.getRecordFileName())) {
|
||||
this.recordFileName = request.getRecordFileName();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean markUserHangup(LocalDateTime endDt) {
|
||||
if (this.callStatus == CallStatus.CALL_CLOSE) {
|
||||
@@ -210,9 +193,6 @@ public class TListCallHistEntity {
|
||||
return value != null && value > 0;
|
||||
}
|
||||
|
||||
private static boolean hasText(String value) {
|
||||
return value != null && !value.isBlank();
|
||||
}
|
||||
|
||||
private static String defaultString(String value) {
|
||||
return value == null ? "" : value;
|
||||
|
||||
@@ -2,7 +2,6 @@ package com.kri.contact.server.persistence.entity;
|
||||
|
||||
import static com.kri.contact.server.common.consts.ContactConstant.DEFAULT_CALLER_TELNUM;
|
||||
|
||||
import com.kri.common.feature.datetime.DateTimeUtil;
|
||||
import com.kri.contact.server.common.enums.ProjectStatus;
|
||||
import com.kri.contact.server.dto.project.ProjectCreateRequestDto;
|
||||
import com.kri.contact.server.dto.project.ProjectInfoDto;
|
||||
@@ -115,22 +114,6 @@ public class TPrjctEntity {
|
||||
@Column(name = "del_dt")
|
||||
private LocalDateTime delDt;
|
||||
|
||||
public static TPrjctEntity fromDto(ProjectInfoDto dto) {
|
||||
return TPrjctEntity.builder()
|
||||
.prjctCd(dto.getPrjctCd())
|
||||
.prjctStatus(dto.getPrjctStatus().getCode())
|
||||
.prjctNm(dto.getPrjctName())
|
||||
.itrntPrjctCd(dto.getItrntPrjctCd())
|
||||
.ownerDept(dto.getOwnerDept())
|
||||
.ownerFw(dto.getOwnerFw())
|
||||
.callerTelNum(dto.getCallerTelNum())
|
||||
.ff(dto.getFf())
|
||||
.useContactSenario(dto.getUseContactSenario())
|
||||
.contactSenario(dto.getContactSenario())
|
||||
.regUser(dto.getRegUser())
|
||||
.regDt(DateTimeUtil.nowKstLocalDateTime())
|
||||
.build();
|
||||
}
|
||||
|
||||
public static TPrjctEntity create(ProjectCreateRequestDto req, String prjctCd, String regUser,
|
||||
LocalDateTime regDt) {
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.kri.contact.server.persistence.entity.id;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Embeddable;
|
||||
import java.io.Serializable;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Getter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@EqualsAndHashCode
|
||||
@Embeddable
|
||||
public class TListAsyncJobChunkId implements Serializable {
|
||||
@Column(name = "job_key", length = 32, nullable = false)
|
||||
private String jobKey;
|
||||
|
||||
@Column(name = "chunk_no", nullable = false)
|
||||
private Integer chunkNo;
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
package com.kri.contact.server.persistence.entity.id;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Embeddable;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import java.io.Serializable;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@EqualsAndHashCode
|
||||
@Embeddable
|
||||
public class TListEventLatestEntityId implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 2852702807999472163L;
|
||||
|
||||
@NotNull
|
||||
@Column(name = "prjct_idx", nullable = false, columnDefinition = "int UNSIGNED not null")
|
||||
private Long prjctIdx;
|
||||
|
||||
@NotNull
|
||||
@Column(name = "sid", nullable = false)
|
||||
private String sid;
|
||||
|
||||
@NotNull
|
||||
@Column(name = "event_ty", nullable = false)
|
||||
private Short eventTy;
|
||||
}
|
||||
@@ -22,8 +22,8 @@ public class ListResetJdbcSupport {
|
||||
return DeleteCounts.empty();
|
||||
}
|
||||
|
||||
prepareTempSids(sids, chunkSize);
|
||||
try {
|
||||
prepareTempSids(sids, chunkSize);
|
||||
int eventHistCount = deleteByTempSids("t_list_event_hist", prjctIdx);
|
||||
int eventLatestCount = deleteByTempSids("t_list_event_latest", prjctIdx);
|
||||
int updateHistCount = deleteByTempSids("t_list_update_hist", prjctIdx);
|
||||
@@ -50,8 +50,8 @@ public class ListResetJdbcSupport {
|
||||
return DeleteCounts.empty();
|
||||
}
|
||||
|
||||
prepareTempSids(sids, chunkSize);
|
||||
try {
|
||||
prepareTempSids(sids, chunkSize);
|
||||
int fileCount = includeFile ? deleteByTempSids("t_list_item_file", prjctIdx) : 0;
|
||||
int itemCount = deleteByTempSids("t_list_item", prjctIdx);
|
||||
int assignUserCount = deleteByTempSids("t_list_assign_user", prjctIdx);
|
||||
@@ -194,17 +194,5 @@ public class ListResetJdbcSupport {
|
||||
);
|
||||
}
|
||||
|
||||
public int totalDeletedRows() {
|
||||
return fileCount
|
||||
+ callHistCount
|
||||
+ eventHistCount
|
||||
+ eventLatestCount
|
||||
+ updateHistCount
|
||||
+ itemCount
|
||||
+ listCount
|
||||
+ assignUserCount
|
||||
+ assignCmpnCount
|
||||
+ resveCount;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,24 +9,10 @@ import org.springframework.data.jpa.repository.Query;
|
||||
|
||||
public interface ListAssignCmpnRepository extends JpaRepository<TListAssignCmpnEntity, Long> {
|
||||
|
||||
long deleteByCmpnId(String cmpnId);
|
||||
|
||||
boolean existsByPrjctIdxAndSidAndCmpnId(Long prjctIdx, String sid, String cmpnId);
|
||||
|
||||
@Modifying(clearAutomatically = true, flushAutomatically = true)
|
||||
@Query("""
|
||||
delete from TListAssignCmpnEntity a
|
||||
where a.prjctIdx = :prjctIdx
|
||||
""")
|
||||
int deleteByPrjctIdx(Long prjctIdx);
|
||||
|
||||
@Modifying(clearAutomatically = true, flushAutomatically = true)
|
||||
@Query("""
|
||||
delete from TListAssignCmpnEntity a
|
||||
where a.prjctIdx = :prjctIdx
|
||||
and a.sid in :sids
|
||||
""")
|
||||
int deleteWithPrjctIdxAndSids(Long prjctIdx, Collection<String> sids);
|
||||
|
||||
@Modifying(clearAutomatically = true, flushAutomatically = true)
|
||||
@Query("""
|
||||
|
||||
@@ -13,33 +13,8 @@ public interface ListAssignUserRepository extends JpaRepository<TListAssignUserE
|
||||
|
||||
boolean existsByPrjctIdxAndSidAndUserId(Long prjctIdx, String sid, String userId);
|
||||
|
||||
@Modifying(clearAutomatically = true, flushAutomatically = true)
|
||||
@Query("""
|
||||
delete from TListAssignUserEntity a
|
||||
where a.prjctIdx = :prjctIdx
|
||||
""")
|
||||
int deleteByPrjctIdx(Long prjctIdx);
|
||||
|
||||
@Modifying(clearAutomatically = true, flushAutomatically = true)
|
||||
@Query("""
|
||||
delete from TListAssignUserEntity a
|
||||
where a.prjctIdx = :prjctIdx
|
||||
and a.sid in :sids
|
||||
""")
|
||||
int deleteWithPrjctIdxAndSids(Long prjctIdx, Collection<String> sids);
|
||||
|
||||
@Modifying(clearAutomatically = true, flushAutomatically = true)
|
||||
@Query("""
|
||||
delete from TListAssignUserEntity a
|
||||
where a.prjctIdx = :prjctIdx
|
||||
and a.assignRole = :assignRole
|
||||
and a.sid in :sids
|
||||
""")
|
||||
int deleteByPrjctIdxAndAssignRoleAndSidIn(
|
||||
Long prjctIdx,
|
||||
Short assignRole,
|
||||
Collection<String> sids
|
||||
);
|
||||
|
||||
@Query("""
|
||||
select a
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.kri.contact.server.persistence.repository;
|
||||
|
||||
import com.kri.contact.server.persistence.entity.TListAsyncJobChunkEntity;
|
||||
import com.kri.contact.server.persistence.entity.id.TListAsyncJobChunkId;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface ListAsyncJobChunkRepository
|
||||
extends JpaRepository<TListAsyncJobChunkEntity, TListAsyncJobChunkId> {}
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.kri.contact.server.persistence.repository;
|
||||
|
||||
import com.kri.contact.server.common.enums.ListAsyncJobStatus;
|
||||
import com.kri.contact.server.common.enums.ListAsyncJobType;
|
||||
import com.kri.contact.server.dto.list.async.ListAsyncJobActivity;
|
||||
import com.kri.contact.server.persistence.entity.TListAsyncJobEntity;
|
||||
import jakarta.persistence.LockModeType;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Lock;
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
|
||||
public interface ListAsyncJobRepository extends JpaRepository<TListAsyncJobEntity, String> {
|
||||
@Query("""
|
||||
select new com.kri.contact.server.dto.list.async.ListAsyncJobActivity(
|
||||
j.jobKey, j.jobType, j.jobStatus, j.requestUser, j.regDt,
|
||||
cast(function('json_unquote', function('json_extract', j.requestJson, '$.operation')) as string))
|
||||
from TListAsyncJobEntity j
|
||||
where j.prjctIdx = :projectId and (j.jobStatus in :statuses or j.jobKey in :heldKeys)
|
||||
""")
|
||||
List<ListAsyncJobActivity> findActivity(
|
||||
@Param("projectId") Long projectId, @Param("statuses") Collection<ListAsyncJobStatus> statuses,
|
||||
@Param("heldKeys") Collection<String> heldKeys);
|
||||
|
||||
Optional<TListAsyncJobEntity> findFirstByPrjctIdxAndJobTypeInAndJobStatusInOrderByRegDtDescJobKeyDesc(
|
||||
Long projectId, Collection<ListAsyncJobType> types, Collection<ListAsyncJobStatus> statuses);
|
||||
|
||||
List<TListAsyncJobEntity> findByPrjctIdxAndRequestUserAndJobStatusInOrderByRegDtDescJobKeyDesc(
|
||||
Long projectId, String requestUser, Collection<ListAsyncJobStatus> statuses);
|
||||
|
||||
boolean existsByPrjctIdxAndRequestUserAndJobTypeAndJobStatusIn(
|
||||
Long projectId, String requestUser, ListAsyncJobType type, Collection<ListAsyncJobStatus> statuses);
|
||||
|
||||
@Lock(LockModeType.PESSIMISTIC_WRITE)
|
||||
@Query("select j from TListAsyncJobEntity j where j.jobKey = :key")
|
||||
Optional<TListAsyncJobEntity> findForUpdate(@Param("key") String key);
|
||||
|
||||
@Modifying(clearAutomatically = true, flushAutomatically = true)
|
||||
@Query("""
|
||||
update TListAsyncJobEntity j
|
||||
set j.jobStatus = com.kri.contact.server.common.enums.ListAsyncJobStatus.FAILED,
|
||||
j.errorCode = 'SERVICE_RESTART', j.errorMessage = '서비스 재시작으로 작업이 중단되었습니다.',
|
||||
j.finishedDt = :now, j.modDt = :now
|
||||
where j.jobStatus in (
|
||||
com.kri.contact.server.common.enums.ListAsyncJobStatus.QUEUED,
|
||||
com.kri.contact.server.common.enums.ListAsyncJobStatus.RUNNING,
|
||||
com.kri.contact.server.common.enums.ListAsyncJobStatus.CANCEL_REQUESTED)
|
||||
""")
|
||||
int failInterruptedJobs(@Param("now") LocalDateTime now);
|
||||
}
|
||||
@@ -5,8 +5,6 @@ import com.kri.contact.server.persistence.entity.id.TListItemFileEntityId;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
|
||||
public interface ListItemFileRepository extends JpaRepository<TListItemFileEntity, TListItemFileEntityId> {
|
||||
|
||||
@@ -16,21 +14,5 @@ public interface ListItemFileRepository extends JpaRepository<TListItemFileEntit
|
||||
Collection<Integer> listHderIds
|
||||
);
|
||||
|
||||
@Modifying(clearAutomatically = true, flushAutomatically = true)
|
||||
@Query("""
|
||||
delete from TListItemFileEntity f
|
||||
where f.id.prjctIdx = :prjctIdx
|
||||
""")
|
||||
int deleteByPrjctIdx(Long prjctIdx);
|
||||
|
||||
@Modifying(clearAutomatically = true, flushAutomatically = true)
|
||||
@Query("""
|
||||
delete from TListItemFileEntity f
|
||||
where f.id.prjctIdx = :prjctIdx
|
||||
and f.id.sid in :sids
|
||||
""")
|
||||
int deleteWithPrjctIdxAndSids(
|
||||
Long prjctIdx,
|
||||
Collection<String> sids
|
||||
);
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@ import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
|
||||
public interface ListItemRepository extends JpaRepository<TListItemEntity, TListItemEntityId> {
|
||||
@@ -18,36 +17,8 @@ public interface ListItemRepository extends JpaRepository<TListItemEntity, TList
|
||||
|
||||
long deleteByIdPrjctIdxAndIdListHderIdNotIn(Long prjctIdx, List<Integer> listHderIds);
|
||||
|
||||
@Modifying(clearAutomatically = true, flushAutomatically = true)
|
||||
@Query("""
|
||||
delete from TListItemEntity i
|
||||
where i.id.prjctIdx = :prjctIdx
|
||||
""")
|
||||
int deleteByPrjctIdx(Long prjctIdx);
|
||||
|
||||
@Modifying(clearAutomatically = true, flushAutomatically = true)
|
||||
@Query("""
|
||||
delete from TListItemEntity i
|
||||
where i.id.prjctIdx = :prjctIdx
|
||||
and i.id.sid in :sids
|
||||
""")
|
||||
int deleteByPrjctIdxAndSids(
|
||||
Long prjctIdx,
|
||||
Collection<String> sids
|
||||
);
|
||||
|
||||
@Modifying(clearAutomatically = true, flushAutomatically = true)
|
||||
@Query("""
|
||||
delete from TListItemEntity i
|
||||
where i.id.prjctIdx = :prjctIdx
|
||||
and i.id.sid in :sids
|
||||
and i.id.listHderId not in :rejectHeaderIds
|
||||
""")
|
||||
int deleteByPrjctIdxAndSidsExceptHeaderIds(
|
||||
Long prjctIdx,
|
||||
Collection<String> sids,
|
||||
Collection<Integer> rejectHeaderIds
|
||||
);
|
||||
|
||||
@Query("""
|
||||
select t
|
||||
|
||||
@@ -4,10 +4,11 @@ import com.kri.contact.server.persistence.entity.TListEntity;
|
||||
import com.kri.contact.server.persistence.entity.id.TListEntityId;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import jakarta.persistence.LockModeType;
|
||||
import org.springframework.data.jpa.repository.Lock;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
|
||||
public interface ListRepository extends JpaRepository<TListEntity, TListEntityId>,
|
||||
@@ -17,28 +18,11 @@ public interface ListRepository extends JpaRepository<TListEntity, TListEntityId
|
||||
|
||||
boolean existsByIdPrjctIdxAndIdSid(Long prjctIdx, String sid);
|
||||
|
||||
@Query("""
|
||||
select l.id.sid
|
||||
from TListEntity l
|
||||
where l.id.prjctIdx = :prjctIdx
|
||||
""")
|
||||
Set<String> findSidsByPrjctIdx(Long prjctIdx);
|
||||
@Lock(LockModeType.PESSIMISTIC_WRITE)
|
||||
@Query("select l from TListEntity l where l.id.prjctIdx = :projectId and l.id.sid in :sids order by l.id.sid")
|
||||
List<TListEntity> findForUpdate(
|
||||
@Param("projectId") Long projectId, @Param("sids") Collection<String> sids);
|
||||
|
||||
|
||||
@Modifying(clearAutomatically = true, flushAutomatically = true)
|
||||
@Query("""
|
||||
delete from TListEntity l
|
||||
where l.id.prjctIdx = :prjctIdx
|
||||
""")
|
||||
int deleteByPrjctIdx(Long prjctIdx);
|
||||
|
||||
@Modifying(clearAutomatically = true, flushAutomatically = true)
|
||||
@Query("""
|
||||
delete from TListEntity l
|
||||
where l.id.prjctIdx = :prjctIdx
|
||||
and l.id.sid in :sids
|
||||
""")
|
||||
int deleteWithPrjctIdxAndSids(
|
||||
Long prjctIdx,
|
||||
Collection<String> sids
|
||||
);
|
||||
}
|
||||
|
||||
@@ -15,7 +15,6 @@ public interface UserRepository extends JpaRepository<TUserEntity, Long> {
|
||||
|
||||
Optional<TUserEntity> findByUserIdAndDelFalse(String userId);
|
||||
|
||||
List<TUserEntity> findByDelFalse();
|
||||
|
||||
List<TUserEntity> findByUserRoleGreaterThanEqualAndDelFalse(Integer userRoleCd);
|
||||
|
||||
@@ -23,19 +22,14 @@ public interface UserRepository extends JpaRepository<TUserEntity, Long> {
|
||||
|
||||
List<TUserEntity> findByUserIdInAndDelFalse(Collection<String> userIds);
|
||||
|
||||
@Query("""
|
||||
select u
|
||||
from TUserEntity u
|
||||
where u.del = false
|
||||
and u.userCmpn in :userCmpns
|
||||
and u.userRole >= :startRole
|
||||
and u.userRole < :endRole
|
||||
""")
|
||||
List<TUserEntity> findByUserCmpnInAndRoleRangeAndDelFalse(
|
||||
Collection<String> userCmpns,
|
||||
int startRole,
|
||||
int endRole
|
||||
);
|
||||
interface UserNameView {
|
||||
String getUserId();
|
||||
String getUserNm();
|
||||
}
|
||||
|
||||
@Query("select u.userId as userId, u.userNm as userNm from TUserEntity u where u.del = false and u.userId in :userIds")
|
||||
List<UserNameView> findNamesByUserIds(@org.springframework.data.repository.query.Param("userIds") Collection<String> userIds);
|
||||
|
||||
|
||||
@Query("""
|
||||
select u
|
||||
@@ -46,14 +40,6 @@ public interface UserRepository extends JpaRepository<TUserEntity, Long> {
|
||||
""")
|
||||
List<TUserEntity> findByRoleRangeAndDelFalse(int startRole, int endRole);
|
||||
|
||||
@Query("""
|
||||
select u.userId
|
||||
from TUserEntity u
|
||||
where u.del = false
|
||||
and u.userRole >= :startRole
|
||||
and u.userRole < :endRole
|
||||
""")
|
||||
List<TUserEntity> findUserIdByRoleRangeAndDelFalse(int startRole, int endRole);
|
||||
|
||||
@Query("""
|
||||
select distinct u.userCmpn
|
||||
|
||||
@@ -28,6 +28,16 @@ public class ListResetPersistenceSupport {
|
||||
String delUser,
|
||||
int chunkSize
|
||||
) {
|
||||
return resetWithAuditContext(prjctIdx, sids, mode, listResetLogId, delUser, chunkSize, false);
|
||||
}
|
||||
|
||||
public int resetChunkWithAuditContext(Long projectId, Collection<String> sids, ListResetMode mode,
|
||||
String logId, String user, int chunkSize) {
|
||||
return resetWithAuditContext(projectId, sids, mode, logId, user, chunkSize, true);
|
||||
}
|
||||
|
||||
private int resetWithAuditContext(Long prjctIdx, Collection<String> sids, ListResetMode mode,
|
||||
String listResetLogId, String delUser, int chunkSize, boolean accumulate) {
|
||||
int resetCount = mode == ListResetMode.HIST ? sids.size() : 0;
|
||||
|
||||
try (var ignored = deleteSessionContext.openReset(listResetLogId, delUser)) {
|
||||
@@ -51,7 +61,10 @@ public class ListResetPersistenceSupport {
|
||||
entityManager.clear();
|
||||
TListResetLogEntity log = listResetLogRepository.findById(listResetLogId)
|
||||
.orElseThrow(() -> BusinessException.internalError("List reset log not found."));
|
||||
log.setListCnt(resetCount);
|
||||
if (!java.util.Objects.equals(log.getPrjctIdx(), prjctIdx) || !java.util.Objects.equals(log.getMode(), mode.getCode())) {
|
||||
throw new IllegalArgumentException("Reset log does not match project and mode");
|
||||
}
|
||||
log.setListCnt(accumulate ? Math.addExact(log.getListCnt(), resetCount) : resetCount);
|
||||
listResetLogRepository.saveAndFlush(log);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.kri.contact.server.scheduler;
|
||||
|
||||
import com.kri.contact.server.service.file.ListAsyncResultFileService;
|
||||
import java.io.IOException;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class ListAsyncResultCleanupScheduler {
|
||||
private final ListAsyncResultFileService files;
|
||||
|
||||
@Scheduled(fixedDelayString = "${kri.list-async.cleanup-delay-ms:60000}",
|
||||
initialDelayString = "${kri.list-async.cleanup-delay-ms:60000}")
|
||||
public void cleanup() {
|
||||
try {
|
||||
int removed = files.cleanup();
|
||||
if (removed > 0) {
|
||||
log.info("List async result cleanup: removed={}", removed);
|
||||
}
|
||||
} catch (IOException failure) {
|
||||
log.warn("List async result cleanup failed; retry on next schedule");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -156,25 +156,6 @@ public class AssignService {
|
||||
return projectAssignCmpnRepository.findAssignCmpnIdByPrjctIdx(prjctIdx);
|
||||
}
|
||||
|
||||
public List<String> getPrjctAssignableUserIds(Long prjctIdx) {
|
||||
List<String> fieldworkAssign = getPrjctFieldworkAssign(prjctIdx);
|
||||
List<String> cmpnAssign = getPrjctCmpnAssign(prjctIdx);
|
||||
if (cmpnAssign.isEmpty()) {
|
||||
return fieldworkAssign;
|
||||
}
|
||||
|
||||
Set<String> assignableUserIds = new LinkedHashSet<>(fieldworkAssign);
|
||||
userRepository.findByUserCmpnInAndRoleRangeAndDelFalse(
|
||||
cmpnAssign,
|
||||
ContactConstant.OUTSOURCE_USER_ROLE,
|
||||
ContactConstant.OUTSOURCE_USER_ROLE + 1000
|
||||
)
|
||||
.stream()
|
||||
.map(TUserEntity::getUserId)
|
||||
.forEach(assignableUserIds::add);
|
||||
|
||||
return List.copyOf(assignableUserIds);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Map<String, Integer> findActiveUserRoleMap(Collection<String> userIds) {
|
||||
|
||||
@@ -25,6 +25,8 @@ import com.kri.contact.server.persistence.repository.ProjectSurveyRepository;
|
||||
import com.kri.contact.server.persistence.repository.ResultCodeRepository;
|
||||
import com.kri.contact.server.service.file.ListItemFileService;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.concurrent.CancellationException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
@@ -104,11 +106,32 @@ public class ListDownloadService {
|
||||
private final ProjectSurveyRepository projectSurveyRepository;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
public interface DownloadObserver {
|
||||
default void checkCancelled() {}
|
||||
default void onProgress(long processedSidCount, long writtenCount) {}
|
||||
default void beforeWrite() { checkCancelled(); }
|
||||
}
|
||||
|
||||
private static final DownloadObserver NO_OBSERVER = new DownloadObserver() {};
|
||||
|
||||
public byte[] downloadList(
|
||||
Long prjctIdx,
|
||||
ListFilterDto listFilter,
|
||||
boolean includeResult
|
||||
) {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
writeList(prjctIdx, listFilter, includeResult, out, NO_OBSERVER);
|
||||
return out.toByteArray();
|
||||
}
|
||||
|
||||
public void writeList(
|
||||
Long prjctIdx,
|
||||
ListFilterDto listFilter,
|
||||
boolean includeResult,
|
||||
OutputStream out,
|
||||
DownloadObserver observer
|
||||
) {
|
||||
observer.checkCancelled();
|
||||
List<ListHeaderDto> headers = getSortedHeaders(prjctIdx);
|
||||
List<String> baseHeaderNames = toBaseHeaderNames(headers);
|
||||
List<LatestResultColumn> latestResultColumns = includeResult
|
||||
@@ -122,12 +145,14 @@ public class ListDownloadService {
|
||||
.map(LatestResultColumn::headerName)
|
||||
.forEach(excelHeaders::add);
|
||||
|
||||
long processedSidCount = 0;
|
||||
long writtenCount = 0;
|
||||
try (
|
||||
ExcelMapper.StreamingMapWriter writer = ExcelMapper.openStreamingMapWriter(excelHeaders, 100);
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream()
|
||||
ExcelMapper.StreamingMapWriter writer = ExcelMapper.openStreamingMapWriter(excelHeaders, 100)
|
||||
) {
|
||||
String lastSid = null;
|
||||
while (true) {
|
||||
observer.checkCancelled();
|
||||
List<ListSearchService.DownloadListRow> rows = listSearchService.findDownloadListRows(prjctIdx, listFilter, lastSid);
|
||||
if (rows.isEmpty()) {
|
||||
break;
|
||||
@@ -138,6 +163,7 @@ public class ListDownloadService {
|
||||
: Map.of();
|
||||
|
||||
for (ListSearchService.DownloadListRow row : rows) {
|
||||
observer.checkCancelled();
|
||||
writer.append(createListRow(
|
||||
headers,
|
||||
row,
|
||||
@@ -146,14 +172,19 @@ public class ListDownloadService {
|
||||
resultLabelMap
|
||||
));
|
||||
lastSid = row.sid();
|
||||
writtenCount++;
|
||||
}
|
||||
processedSidCount += rows.size();
|
||||
observer.onProgress(processedSidCount, writtenCount);
|
||||
}
|
||||
|
||||
observer.beforeWrite();
|
||||
writer.writeTo(out);
|
||||
return out.toByteArray();
|
||||
} catch (BusinessException e) {
|
||||
observer.checkCancelled();
|
||||
} catch (BusinessException | CancellationException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
observer.checkCancelled();
|
||||
throw BusinessException.internalError("리스트 다운로드 파일 생성 중 오류가 발생했습니다.");
|
||||
}
|
||||
}
|
||||
@@ -167,6 +198,23 @@ public class ListDownloadService {
|
||||
ListEventHistoryOrder historyOrder,
|
||||
boolean includeResult
|
||||
) {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
writeListWithEventHist(prjctIdx, listFilter, resultCodeTypes, searchBase, dateRange, historyOrder, includeResult, out, NO_OBSERVER);
|
||||
return out.toByteArray();
|
||||
}
|
||||
|
||||
public void writeListWithEventHist(
|
||||
Long prjctIdx,
|
||||
ListFilterDto listFilter,
|
||||
Collection<ResultCodeType> resultCodeTypes,
|
||||
ListEventHistoryBase searchBase,
|
||||
DateRangeDto dateRange,
|
||||
ListEventHistoryOrder historyOrder,
|
||||
boolean includeResult,
|
||||
OutputStream out,
|
||||
DownloadObserver observer
|
||||
) {
|
||||
observer.checkCancelled();
|
||||
if (searchBase == null) {
|
||||
throw BusinessException.internalError("이력 기준은 필수입니다.");
|
||||
}
|
||||
@@ -182,7 +230,7 @@ public class ListDownloadService {
|
||||
? createLatestResultColumns(prjctIdx, baseHeaderNames)
|
||||
: List.of();
|
||||
int maxHistoryCount = searchBase == ListEventHistoryBase.ALL
|
||||
? findMaxEventHistoryCount(prjctIdx, listFilter, resultCodeTypes, searchBase, dateRange)
|
||||
? findMaxEventHistoryCount(prjctIdx, listFilter, resultCodeTypes, searchBase, dateRange, observer)
|
||||
: 0;
|
||||
List<String> excelHeaders = new ArrayList<>(baseHeaderNames);
|
||||
latestResultColumns.stream()
|
||||
@@ -196,6 +244,8 @@ public class ListDownloadService {
|
||||
maxHistoryCount
|
||||
);
|
||||
|
||||
long processedSidCount = 0;
|
||||
long writtenCount = 0;
|
||||
try (
|
||||
SXSSFWorkbook workbook = ExcelMapper.createStreamingWorkbook(100);
|
||||
ExcelMapper.StreamingMapWriter writer = ExcelMapper.openStreamingMapWriter(
|
||||
@@ -203,12 +253,12 @@ public class ListDownloadService {
|
||||
excelHeaders,
|
||||
EXCEL_SHEET_NAME,
|
||||
EVENT_HISTORY_HEADER_ROW_INDEX
|
||||
);
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream()
|
||||
)
|
||||
) {
|
||||
appendEventHistorySectionHeaders(workbook, eventHistoryLayout.sections());
|
||||
String lastSid = null;
|
||||
while (true) {
|
||||
observer.checkCancelled();
|
||||
List<ListSearchService.DownloadListRow> rows = listSearchService.findDownloadListRows(prjctIdx, listFilter, lastSid);
|
||||
if (rows.isEmpty()) {
|
||||
break;
|
||||
@@ -227,6 +277,7 @@ public class ListDownloadService {
|
||||
: Map.of();
|
||||
|
||||
for (ListSearchService.DownloadListRow row : rows) {
|
||||
observer.checkCancelled();
|
||||
writer.append(createEventHistoryRow(
|
||||
headers,
|
||||
row,
|
||||
@@ -241,30 +292,44 @@ public class ListDownloadService {
|
||||
latestResultMap.getOrDefault(row.sid(), List.of())
|
||||
));
|
||||
lastSid = row.sid();
|
||||
writtenCount++;
|
||||
}
|
||||
processedSidCount += rows.size();
|
||||
observer.onProgress(processedSidCount, writtenCount);
|
||||
}
|
||||
|
||||
observer.beforeWrite();
|
||||
writer.writeTo(out);
|
||||
return out.toByteArray();
|
||||
} catch (BusinessException e) {
|
||||
observer.checkCancelled();
|
||||
} catch (BusinessException | CancellationException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
observer.checkCancelled();
|
||||
throw BusinessException.internalError("리스트 + 컨택이력 다운로드 파일 생성 중 오류가 발생했습니다.");
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] downloadSidWithUpdateHist(Long prjctIdx, ListFilterDto listFilter) {
|
||||
int maxHistoryGroupCount = findMaxUpdateHistoryGroupCount(prjctIdx, listFilter);
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
writeSidWithUpdateHist(prjctIdx, listFilter, out, NO_OBSERVER);
|
||||
return out.toByteArray();
|
||||
}
|
||||
|
||||
public void writeSidWithUpdateHist(Long prjctIdx, ListFilterDto listFilter, OutputStream out, DownloadObserver observer) {
|
||||
observer.checkCancelled();
|
||||
int maxHistoryGroupCount = findMaxUpdateHistoryGroupCount(prjctIdx, listFilter, observer);
|
||||
List<String> excelHeaders = new ArrayList<>();
|
||||
excelHeaders.add("sid");
|
||||
appendRepeatedHeaders(excelHeaders, UPDATE_HISTORY_HEADERS, maxHistoryGroupCount);
|
||||
|
||||
long processedSidCount = 0;
|
||||
long writtenCount = 0;
|
||||
try (
|
||||
ExcelMapper.StreamingMapWriter writer = ExcelMapper.openStreamingMapWriter(excelHeaders, 100);
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream()
|
||||
ExcelMapper.StreamingMapWriter writer = ExcelMapper.openStreamingMapWriter(excelHeaders, 100)
|
||||
) {
|
||||
String lastSid = null;
|
||||
while (true) {
|
||||
observer.checkCancelled();
|
||||
List<String> sids = listSearchService.findDownloadSids(prjctIdx, listFilter, lastSid);
|
||||
if (sids.isEmpty()) {
|
||||
break;
|
||||
@@ -275,6 +340,7 @@ public class ListDownloadService {
|
||||
|
||||
for (String sid : sids) {
|
||||
lastSid = sid;
|
||||
observer.checkCancelled();
|
||||
List<ListSearchService.UpdateHistoryGroupRow> histories = historyMap.getOrDefault(sid, List.of());
|
||||
if (histories.isEmpty()) {
|
||||
continue;
|
||||
@@ -284,19 +350,31 @@ public class ListDownloadService {
|
||||
histories,
|
||||
maxHistoryGroupCount
|
||||
));
|
||||
writtenCount++;
|
||||
}
|
||||
processedSidCount += sids.size();
|
||||
observer.onProgress(processedSidCount, writtenCount);
|
||||
}
|
||||
|
||||
observer.beforeWrite();
|
||||
writer.writeTo(out);
|
||||
return out.toByteArray();
|
||||
} catch (BusinessException e) {
|
||||
observer.checkCancelled();
|
||||
} catch (BusinessException | CancellationException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
observer.checkCancelled();
|
||||
throw BusinessException.internalError("SID + 수정이력 다운로드 파일 생성 중 오류가 발생했습니다.");
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] downloadItemFile(Long prjctIdx, ListFilterDto listFilter) {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
writeItemFile(prjctIdx, listFilter, out, NO_OBSERVER);
|
||||
return out.toByteArray();
|
||||
}
|
||||
|
||||
public void writeItemFile(Long prjctIdx, ListFilterDto listFilter, OutputStream out, DownloadObserver observer) {
|
||||
observer.checkCancelled();
|
||||
List<ListHeaderDto> fileHeaders = getSortedHeaders(prjctIdx).stream()
|
||||
.filter(header -> Objects.equals(header.getListHderTy(), ListHeaderType.FILE))
|
||||
.toList();
|
||||
@@ -308,14 +386,15 @@ public class ListDownloadService {
|
||||
.map(ListHeaderDto::getListHderId)
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
long processedSidCount = 0;
|
||||
try (
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
ZipOutputStream zip = new ZipOutputStream(out)
|
||||
) {
|
||||
String lastSid = null;
|
||||
int fileCount = 0;
|
||||
|
||||
while (true) {
|
||||
observer.checkCancelled();
|
||||
List<String> sids = listSearchService.findDownloadSids(prjctIdx, listFilter, lastSid);
|
||||
if (sids.isEmpty()) {
|
||||
break;
|
||||
@@ -329,29 +408,34 @@ public class ListDownloadService {
|
||||
);
|
||||
|
||||
for (TListItemFileEntity fileEntity : fileEntities) {
|
||||
observer.checkCancelled();
|
||||
if (fileEntity.getFileNm() == null || fileEntity.getFileNm().isBlank()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
ZipEntry entry = new ZipEntry(sanitizeZipEntryName(fileEntity.getFileNm()));
|
||||
zip.putNextEntry(entry);
|
||||
zip.write(listItemFileService.readItemFile(fileEntity.getFilePath(), fileEntity.getFileNm()));
|
||||
listItemFileService.copyItemFile(fileEntity.getFilePath(), fileEntity.getFileNm(), zip);
|
||||
zip.closeEntry();
|
||||
fileCount++;
|
||||
}
|
||||
|
||||
lastSid = sids.get(sids.size() - 1);
|
||||
processedSidCount += sids.size();
|
||||
observer.onProgress(processedSidCount, fileCount);
|
||||
}
|
||||
|
||||
if (fileCount == 0) {
|
||||
throw BusinessException.notFound("LIST_ITEM_FILE_NOT_FOUND", "다운로드할 리스트 파일이 없습니다.");
|
||||
}
|
||||
|
||||
observer.beforeWrite();
|
||||
zip.finish();
|
||||
return out.toByteArray();
|
||||
} catch (BusinessException e) {
|
||||
observer.checkCancelled();
|
||||
} catch (BusinessException | CancellationException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
observer.checkCancelled();
|
||||
throw BusinessException.internalError("FAIL_DOWNLOAD_LIST_ITEM_FILE", e.getMessage());
|
||||
}
|
||||
}
|
||||
@@ -382,11 +466,13 @@ public class ListDownloadService {
|
||||
ListFilterDto listFilter,
|
||||
Collection<ResultCodeType> resultCodeTypes,
|
||||
ListEventHistoryBase searchBase,
|
||||
DateRangeDto dateRange
|
||||
DateRangeDto dateRange,
|
||||
DownloadObserver observer
|
||||
) {
|
||||
int maxCount = 0;
|
||||
String lastSid = null;
|
||||
while (true) {
|
||||
observer.checkCancelled();
|
||||
List<String> sids = listSearchService.findDownloadSids(prjctIdx, listFilter, lastSid);
|
||||
if (sids.isEmpty()) {
|
||||
return maxCount;
|
||||
@@ -421,10 +507,11 @@ public class ListDownloadService {
|
||||
}
|
||||
}
|
||||
|
||||
private int findMaxUpdateHistoryGroupCount(Long prjctIdx, ListFilterDto listFilter) {
|
||||
private int findMaxUpdateHistoryGroupCount(Long prjctIdx, ListFilterDto listFilter, DownloadObserver observer) {
|
||||
int maxCount = 0;
|
||||
String lastSid = null;
|
||||
while (true) {
|
||||
observer.checkCancelled();
|
||||
List<String> sids = listSearchService.findDownloadSids(prjctIdx, listFilter, lastSid);
|
||||
if (sids.isEmpty()) {
|
||||
return maxCount;
|
||||
|
||||
@@ -260,12 +260,6 @@ public class ListHeaderService {
|
||||
return true;
|
||||
}
|
||||
|
||||
public List<Integer> findHeaderIdsByType(List<ListHeaderDto> headers, ListHeaderType type) {
|
||||
return headers.stream()
|
||||
.filter(h -> h.getListHderTy() == type)
|
||||
.map(ListHeaderDto::getListHderId)
|
||||
.toList();
|
||||
}
|
||||
|
||||
public String findHeaderNameByType(List<ListHeaderDto> headers, ListHeaderType type) {
|
||||
return headers.stream()
|
||||
@@ -275,12 +269,6 @@ public class ListHeaderService {
|
||||
.orElseThrow(() -> BusinessException.badRequest("HEADER_NOT_FOUND", type + " 헤더를 찾을 수 없습니다."));
|
||||
}
|
||||
|
||||
public List<String> findHeaderNamesByTypes(List<ListHeaderDto> headers, List<ListHeaderType> types) {
|
||||
return headers.stream()
|
||||
.filter(h -> types.contains(h.getListHderTy()))
|
||||
.map(ListHeaderDto::getListHderNm)
|
||||
.toList();
|
||||
}
|
||||
|
||||
public Map<Integer, String> createHeaderNameByIdMap(
|
||||
Collection<? extends ListHeaderDto> headers
|
||||
|
||||
@@ -57,7 +57,8 @@ public class ListResetService {
|
||||
prjctIdx, sids, mode, listResetLogId, regUser, ContactConstant.INSERT_CHUNK_SIZE);
|
||||
}
|
||||
|
||||
private String createResetLog(
|
||||
@Transactional
|
||||
public String createResetLog(
|
||||
Long prjctIdx,
|
||||
ListResetMode mode,
|
||||
int listCnt,
|
||||
@@ -78,4 +79,11 @@ public class ListResetService {
|
||||
return listResetLogId;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public int resetAsyncChunk(Long projectId, Set<String> sids, ListResetMode mode,
|
||||
String logId, String user) {
|
||||
return listResetPersistenceSupport.resetChunkWithAuditContext(
|
||||
projectId, sids, mode, logId, user, ContactConstant.INSERT_CHUNK_SIZE);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -144,15 +144,6 @@ public class ListSearchService {
|
||||
));
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public <T extends ListSearchBaseDto> List<T> searchListWithPagingOnlyContents(
|
||||
Long prjctIdx,
|
||||
ListSearchRequestDto request,
|
||||
ListSearchResponseType responseType
|
||||
) {
|
||||
List<TListEntity> listEntities = findPagedListEntities(prjctIdx, request);
|
||||
return createListSearch(prjctIdx, listEntities, responseType);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Set<String> findTargetSids(Long prjctIdx, ListFilterDto listFilter) {
|
||||
|
||||
@@ -20,7 +20,6 @@ import com.kri.contact.server.dto.list.ListEventHistInsertRequestDto;
|
||||
import com.kri.contact.server.dto.list.ListEventHistSurveyMetaDto;
|
||||
import com.kri.contact.server.dto.list.ListEventHistSurveyMetaDto.SurveySnapshot;
|
||||
import com.kri.contact.server.dto.list.ListFilterDto;
|
||||
import com.kri.contact.server.dto.list.ListHeaderDto;
|
||||
import com.kri.contact.server.dto.list.ListSurveyResultUpdateDto;
|
||||
import com.kri.contact.server.dto.list.ListUpdateRequestDto;
|
||||
import com.kri.contact.server.persistence.entity.TListEventHistEntity;
|
||||
@@ -73,7 +72,6 @@ public class ListUpdateService {
|
||||
|
||||
private final ListHeaderRepository listHeaderRepository;
|
||||
private final ListSearchService listSearchService;
|
||||
private final ListContentItemService listContentItemService;
|
||||
private final ListUpdateJdbcSupport listUpdateJdbcSupport;
|
||||
private final ListRepository listRepository;
|
||||
private final ListEventHistRepository listEventHistRepository;
|
||||
@@ -299,6 +297,21 @@ public class ListUpdateService {
|
||||
private int users;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public void validateAsyncUpdate(Long projectId, ListUpdateRequestDto request, Integer userRole) {
|
||||
if (request == null || request.getListFilter() == null) {
|
||||
throw BusinessException.badRequest("INVALID_LIST_UPDATE", "수정 내용과 필터가 필요합니다.");
|
||||
}
|
||||
validateListUpdateRequest(request);
|
||||
validateResultCodes(projectId, request.getUpdateResultCodes());
|
||||
validateSurveyResultCodes(projectId, request.getUpdateSurveyResultCodes());
|
||||
if (!isEmpty(request.getUpdateContents())) {
|
||||
var headers = validateHeaders(projectId, request.getUpdateContents().keySet(), userRole);
|
||||
request.setUpdateContents(filterAllowedUpdateContents(request.getUpdateContents(), headers));
|
||||
}
|
||||
validateListUpdateRequest(request);
|
||||
}
|
||||
|
||||
private void validateListUpdateRequest(ListUpdateRequestDto request){
|
||||
if(
|
||||
CollectionUtil.isEmpty(request.getUpdateContents()) &&
|
||||
@@ -640,7 +653,7 @@ public class ListUpdateService {
|
||||
List<FileToDelete> files = listItemDeleteSupport.deleteItemsWithoutBackup(
|
||||
prjctIdx, itemDeletes, fileDeleteRows, ContactConstant.INSERT_CHUNK_SIZE);
|
||||
for (FileToDelete file : files) {
|
||||
listItemFileService.deleteItemFile(file.filePath(), file.fileName());
|
||||
listItemFileService.deleteItemFileAfterCommit(file.filePath(), file.fileName());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -44,6 +44,7 @@ import com.kri.contact.server.persistence.repository.CmpnRepository;
|
||||
import com.kri.contact.server.persistence.repository.ListHeaderRepository;
|
||||
import com.kri.contact.server.persistence.repository.ListItemFileRepository;
|
||||
import com.kri.contact.server.persistence.repository.ListUploadLogRepository;
|
||||
import com.kri.contact.server.persistence.repository.ListRepository;
|
||||
import com.kri.contact.server.service.file.ListFileService;
|
||||
import com.kri.contact.server.service.file.ListItemFileService;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
@@ -65,6 +66,7 @@ import org.apache.poi.ss.usermodel.Sheet;
|
||||
import org.apache.poi.ss.usermodel.Workbook;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
@Service
|
||||
@@ -84,6 +86,7 @@ public class ListUploadService {
|
||||
private final ObjectMapper objectMapper;
|
||||
private final ListItemFileService listItemFileService;
|
||||
private final ListUpdateService listUpdateService;
|
||||
private final ListRepository listRepository;
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<ListUploadHistoryDto> findUploadHistories(Long prjctIdx) {
|
||||
@@ -308,6 +311,11 @@ public class ListUploadService {
|
||||
List<ListHeaderDto> resolvedHeaders,
|
||||
Path listFilePath
|
||||
) {
|
||||
return createCmpnByUploadText(resolvedHeaders, listFilePath, () -> {});
|
||||
}
|
||||
|
||||
private Map<String, TCmpnEntity> createCmpnByUploadText(List<ListHeaderDto> resolvedHeaders,
|
||||
Path listFilePath, Runnable checkCancelled) {
|
||||
String outsourceHeaderName = findOptionalHeaderNameByType(resolvedHeaders, ListHeaderType.OUTSOURCE);
|
||||
if (outsourceHeaderName == null) {
|
||||
return Collections.emptyMap();
|
||||
@@ -315,6 +323,7 @@ public class ListUploadService {
|
||||
|
||||
Set<String> uploadTexts = new LinkedHashSet<>();
|
||||
ExcelMapper.readStreaming(listFilePath, UPPER_CASE_HEADER_OPTIONS, (row, rowNo) -> {
|
||||
checkCancelled.run();
|
||||
String raw = normalizeBlank(row.get(outsourceHeaderName));
|
||||
if (raw != null) {
|
||||
uploadTexts.add(raw);
|
||||
@@ -340,7 +349,8 @@ public class ListUploadService {
|
||||
return result;
|
||||
}
|
||||
|
||||
private TListUploadLogEntity createPendingLog(
|
||||
@Transactional
|
||||
public TListUploadLogEntity createPendingLog(
|
||||
Long prjctIdx,
|
||||
Path listFilePath,
|
||||
ListUploadRequestDto request,
|
||||
@@ -357,7 +367,7 @@ public class ListUploadService {
|
||||
uploadDt
|
||||
);
|
||||
|
||||
return listUploadLogRepository.save(uploadLog);
|
||||
return listUploadLogRepository.saveAndFlush(uploadLog);
|
||||
}
|
||||
|
||||
private Map<Integer, TListHeaderEntity> findFileHeaderMap(Long prjctIdx) {
|
||||
@@ -441,6 +451,19 @@ public class ListUploadService {
|
||||
);
|
||||
}
|
||||
|
||||
readUploadChunks(prjctIdx, listFilePath, resolvedHeaders, cmpnByUploadText,
|
||||
listUploadLogId, uploadUser, uploadDt, () -> {}, this::insertUploadChunk);
|
||||
}
|
||||
|
||||
public record UploadChunk(List<ListRowInsert> lists, List<ListItemRowInsert> items,
|
||||
List<ListAssignRowInsert> users, List<ListAssignCmpnRowInsert> companies) {
|
||||
public List<String> sids() { return lists.stream().map(ListRowInsert::sid).toList(); }
|
||||
}
|
||||
|
||||
public void readUploadChunks(Long prjctIdx, Path listFilePath,
|
||||
List<ListHeaderDto> resolvedHeaders, Map<String, TCmpnEntity> cmpnByUploadText,
|
||||
String listUploadLogId, String uploadUser, LocalDateTime uploadDt,
|
||||
Runnable checkCancelled, java.util.function.Consumer<UploadChunk> consume) {
|
||||
String sidHeaderName = listHeaderService.findHeaderNameByType(resolvedHeaders, ListHeaderType.SID);
|
||||
String isUseHeaderName = listHeaderService.findHeaderNameByType(resolvedHeaders, ListHeaderType.IS_USE);
|
||||
String memoHeaderName = listHeaderService.findHeaderNameByType(resolvedHeaders, ListHeaderType.MEMO);
|
||||
@@ -453,6 +476,7 @@ public class ListUploadService {
|
||||
List<ListAssignCmpnRowInsert> assignCmpnRows = new ArrayList<>();
|
||||
|
||||
ExcelMapper.readStreaming(listFilePath, UPPER_CASE_HEADER_OPTIONS, (row, rowNo) -> {
|
||||
checkCancelled.run();
|
||||
String sid = normalizeSid(row.get(sidHeaderName));
|
||||
short isUse = parseIsUseSafe(row.get(isUseHeaderName));
|
||||
String memo = normalizeBlank(row.get(memoHeaderName));
|
||||
@@ -532,35 +556,121 @@ public class ListUploadService {
|
||||
}
|
||||
|
||||
if (listRows.size() >= ContactConstant.INSERT_CHUNK_SIZE) {
|
||||
listUploadJdbcSupport.batchInsertList(listRows, ContactConstant.INSERT_CHUNK_SIZE);
|
||||
consume.accept(new UploadChunk(List.copyOf(listRows), List.copyOf(itemRows),
|
||||
List.copyOf(assignRows), List.copyOf(assignCmpnRows)));
|
||||
listRows.clear();
|
||||
}
|
||||
if (itemRows.size() >= ContactConstant.INSERT_CHUNK_SIZE) {
|
||||
listUploadJdbcSupport.batchInsertListItem(itemRows, ContactConstant.INSERT_CHUNK_SIZE);
|
||||
itemRows.clear();
|
||||
}
|
||||
if (assignRows.size() >= ContactConstant.INSERT_CHUNK_SIZE) {
|
||||
listUploadJdbcSupport.batchInsertListAssign(assignRows, ContactConstant.INSERT_CHUNK_SIZE);
|
||||
assignRows.clear();
|
||||
}
|
||||
if (assignCmpnRows.size() >= ContactConstant.INSERT_CHUNK_SIZE) {
|
||||
listUploadJdbcSupport.batchInsertListAssignCmpn(assignCmpnRows, ContactConstant.INSERT_CHUNK_SIZE);
|
||||
assignCmpnRows.clear();
|
||||
}
|
||||
});
|
||||
|
||||
checkCancelled.run();
|
||||
if (!listRows.isEmpty()) {
|
||||
listUploadJdbcSupport.batchInsertList(listRows, ContactConstant.INSERT_CHUNK_SIZE);
|
||||
consume.accept(new UploadChunk(List.copyOf(listRows), List.copyOf(itemRows),
|
||||
List.copyOf(assignRows), List.copyOf(assignCmpnRows)));
|
||||
}
|
||||
if (!itemRows.isEmpty()) {
|
||||
listUploadJdbcSupport.batchInsertListItem(itemRows, ContactConstant.INSERT_CHUNK_SIZE);
|
||||
}
|
||||
|
||||
public record PreparedUpload(int totalCount, Set<String> sids, List<ListHeaderDto> headers,
|
||||
Map<String, TCmpnEntity> companies, boolean createHeaders, List<ListUploadErrorDto> errors) {}
|
||||
|
||||
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||
public PreparedUpload prepareAsync(Long projectId, Path source, ListUploadRequestDto request,
|
||||
String logId, Runnable checkCancelled) {
|
||||
if (request.getUploadMode() == null) {
|
||||
throw new IllegalArgumentException("Upload mode is required");
|
||||
}
|
||||
if (!assignRows.isEmpty()) {
|
||||
listUploadJdbcSupport.batchInsertListAssign(assignRows, ContactConstant.INSERT_CHUNK_SIZE);
|
||||
try {
|
||||
checkCancelled.run();
|
||||
var errors = new ArrayList<ListUploadErrorDto>();
|
||||
var headers = listHeaderService.resolveUploadHeaders(projectId,
|
||||
ExcelMapper.readHeaderStreaming(source, UPPER_CASE_HEADER_OPTIONS), request, errors);
|
||||
var existing = listHeaderService.getHeader(projectId).stream().map(ListHeaderDto.class::cast).toList();
|
||||
boolean createHeaders = existing.stream().noneMatch(h -> h.getListHderGrad() == ListHeaderGrade.COMMON);
|
||||
if (errors.isEmpty() && !createHeaders && !listHeaderService.isEqualHeader(existing, headers, true)) {
|
||||
errors.add(ListUploadErrorDto.of(1, "헤더", "기존 헤더와 업로드 헤더가 일치하지 않습니다."));
|
||||
}
|
||||
if (!errors.isEmpty()) {
|
||||
return new PreparedUpload(0, Set.of(), List.copyOf(headers), Map.of(), createHeaders, errors);
|
||||
}
|
||||
var assignedUsers = assignService.getPrjctFieldworkAssign(projectId);
|
||||
var assignedCompanies = assignService.getPrjctCmpnAssign(projectId);
|
||||
var roles = createAssignRoleByHeaderName(headers);
|
||||
var companies = createCmpnByUploadText(headers, source, checkCancelled);
|
||||
var validation = validateFile(projectId, source, request, headers, assignedUsers, assignedCompanies,
|
||||
assignService.findActiveUserRoleMap(assignedUsers), roles,
|
||||
assignService.findAllowedUserRolesByAssignRole(new LinkedHashSet<>(roles.values())),
|
||||
companies, logId, checkCancelled, request.getUploadMode() != ListUploadMode.RESET_AND_INSERT);
|
||||
checkCancelled.run();
|
||||
var log = listUploadLogRepository.findById(logId).orElseThrow();
|
||||
if (!Objects.equals(log.getPrjctIdx(), projectId)) {
|
||||
throw new IllegalArgumentException("Upload log project mismatch");
|
||||
}
|
||||
markValidated(log, validation.totalCount());
|
||||
return new PreparedUpload(validation.totalCount(), Set.copyOf(validation.uploadedSids()),
|
||||
List.copyOf(headers), Map.copyOf(companies), createHeaders, List.copyOf(validation.errors()));
|
||||
} catch (RuntimeException failure) {
|
||||
checkCancelled.run();
|
||||
throw failure;
|
||||
} finally {
|
||||
// All validation staging is in this transaction; a restart also rolls it back.
|
||||
listUploadJdbcSupport.deleteTemp(logId);
|
||||
}
|
||||
if (!assignCmpnRows.isEmpty()) {
|
||||
listUploadJdbcSupport.batchInsertListAssignCmpn(assignCmpnRows, ContactConstant.INSERT_CHUNK_SIZE);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void replaceAsyncChunk(Long projectId, UploadChunk chunk, PreparedUpload prepared,
|
||||
String resetLogId, String user, LocalDateTime time, boolean first) {
|
||||
var sids = new LinkedHashSet<>(chunk.sids());
|
||||
// Keep concurrent updates/deletes outside the replacement's delete/insert interval.
|
||||
listRepository.findForUpdate(projectId, sids);
|
||||
if (first && prepared.createHeaders()) {
|
||||
createAsyncHeaders(projectId, prepared, user, time);
|
||||
}
|
||||
listResetService.resetAsyncChunk(projectId, sids, ListResetMode.LIST_UPLOAD, resetLogId, user);
|
||||
insertUploadChunk(chunk);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void insertAsyncChunk(Long projectId, UploadChunk chunk, PreparedUpload prepared,
|
||||
String user, LocalDateTime time, boolean first) {
|
||||
if (first && prepared.createHeaders()) createAsyncHeaders(projectId, prepared, user, time);
|
||||
insertUploadChunk(chunk);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void finishPartialAsync(Long projectId) {
|
||||
listContentItemService.refreshListContentItem(projectId, listHeaderService.getHeader(projectId));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void finishAsync(Long projectId, String logId, PreparedUpload prepared, String user,
|
||||
LocalDateTime time) {
|
||||
if (prepared.totalCount() == 0 && prepared.createHeaders()) {
|
||||
createAsyncHeaders(projectId, prepared, user, time);
|
||||
}
|
||||
listContentItemService.refreshListContentItem(projectId, prepared.headers());
|
||||
var log = listUploadLogRepository.findById(logId).orElseThrow();
|
||||
if (!Objects.equals(log.getPrjctIdx(), projectId)) {
|
||||
throw new IllegalArgumentException("Upload log project mismatch");
|
||||
}
|
||||
markSuccess(log, prepared.totalCount());
|
||||
}
|
||||
|
||||
private void createAsyncHeaders(Long projectId, PreparedUpload prepared, String user, LocalDateTime time) {
|
||||
var current = listHeaderService.getHeader(projectId).stream().map(ListHeaderDto.class::cast).toList();
|
||||
if (current.stream().noneMatch(h -> h.getListHderGrad() == ListHeaderGrade.COMMON)) {
|
||||
listHeaderService.upsertHeader(projectId, prepared.headers(), user, time);
|
||||
} else if (!listHeaderService.isEqualHeader(current, prepared.headers(), true)) {
|
||||
throw BusinessException.conflict("LIST_HEADERS_CHANGED", "검증 이후 리스트 헤더가 변경되었습니다.");
|
||||
}
|
||||
}
|
||||
|
||||
private void insertUploadChunk(UploadChunk chunk) {
|
||||
listUploadJdbcSupport.batchInsertList(chunk.lists(), ContactConstant.INSERT_CHUNK_SIZE);
|
||||
listUploadJdbcSupport.batchInsertListItem(chunk.items(), ContactConstant.INSERT_CHUNK_SIZE);
|
||||
listUploadJdbcSupport.batchInsertListAssign(chunk.users(), ContactConstant.INSERT_CHUNK_SIZE);
|
||||
listUploadJdbcSupport.batchInsertListAssignCmpn(chunk.companies(), ContactConstant.INSERT_CHUNK_SIZE);
|
||||
}
|
||||
|
||||
private ValidationSummary validateFile(
|
||||
@@ -576,6 +686,16 @@ public class ListUploadService {
|
||||
Map<String, TCmpnEntity> cmpnByUploadText,
|
||||
String listUploadLogId
|
||||
) {
|
||||
return validateFile(prjctIdx, listFilePath, request, resolvedHeaders, prjctAssignList,
|
||||
prjctAssignCmpnList, userRoleByUserId, assignRoleByHeaderName, allowedUserRolesByAssignRole,
|
||||
cmpnByUploadText, listUploadLogId, () -> {}, true);
|
||||
}
|
||||
|
||||
private ValidationSummary validateFile(Long prjctIdx, Path listFilePath, ListUploadRequestDto request,
|
||||
List<ListHeaderDto> resolvedHeaders, List<String> prjctAssignList, List<String> prjctAssignCmpnList,
|
||||
Map<String, Integer> userRoleByUserId, Map<String, Short> assignRoleByHeaderName,
|
||||
Map<Short, Set<String>> allowedUserRolesByAssignRole, Map<String, TCmpnEntity> cmpnByUploadText,
|
||||
String listUploadLogId, Runnable checkCancelled, boolean checkExistingDuplicates) {
|
||||
Map<Integer, ListHeaderDto> headerById = toHeaderById(resolvedHeaders);
|
||||
|
||||
String sidHeaderName = listHeaderService.findHeaderNameByType(resolvedHeaders, ListHeaderType.SID);
|
||||
@@ -591,6 +711,7 @@ public class ListUploadService {
|
||||
listUploadJdbcSupport.deleteTemp(listUploadLogId);
|
||||
|
||||
ExcelMapper.readStreaming(listFilePath, UPPER_CASE_HEADER_OPTIONS, (row, rowNo) -> {
|
||||
checkCancelled.run();
|
||||
totalCount.incrementAndGet();
|
||||
|
||||
String rawSid = normalizeBlank(row.get(sidHeaderName));
|
||||
@@ -740,7 +861,7 @@ public class ListUploadService {
|
||||
errors.add(ListUploadErrorDto.of(hit.rowNo(), hit.sid(), fieldName, "업로드 파일 내 중복값입니다."));
|
||||
}
|
||||
|
||||
if (request.isCheckDuplicateEmail() || request.isCheckDuplicatePhone()) {
|
||||
if (checkExistingDuplicates && (request.isCheckDuplicateEmail() || request.isCheckDuplicatePhone())) {
|
||||
boolean excludeSameSid = request.getUploadMode() == ListUploadMode.RESET_AND_INSERT
|
||||
|| request.getUploadMode() == ListUploadMode.ADD_AND_CHANGE;
|
||||
for (ListUploadJdbcSupport.DuplicateHit hit : listUploadJdbcSupport.findDbDuplicateRows(
|
||||
|
||||
@@ -345,7 +345,7 @@ public class MessageSendService {
|
||||
templateParams, surveyUrlByName);
|
||||
}
|
||||
|
||||
private String createBulkSendKey(
|
||||
public String createBulkSendKey(
|
||||
MessageSendBulkRequestDto request,
|
||||
RequestUserContext ctx,
|
||||
MessageContext messageContext,
|
||||
@@ -370,7 +370,7 @@ public class MessageSendService {
|
||||
};
|
||||
}
|
||||
|
||||
private NotiHubBulkSendResponseDto sendBulkChunk(
|
||||
public NotiHubBulkSendResponseDto sendBulkChunk(
|
||||
String bulkSendKey,
|
||||
List<NotiHubDestInfoDto> destInfos,
|
||||
ChannelType channelType
|
||||
@@ -521,7 +521,7 @@ public class MessageSendService {
|
||||
return value == null ? 0 : value;
|
||||
}
|
||||
|
||||
private record MessageContext(
|
||||
public record MessageContext(
|
||||
ProjectInfoDto project,
|
||||
NotiHubTemplateDto template,
|
||||
Map<Integer, ListHeaderDto> headerById,
|
||||
@@ -530,6 +530,23 @@ public class MessageSendService {
|
||||
Map<String, String> surveyUrlByName
|
||||
) {}
|
||||
|
||||
public MessageContext prepareAsync(Long projectId, MessageSendBulkRequestDto request) {
|
||||
validateDestinationHeaders(request.getDestHeaderIds());
|
||||
return createMessageContext(projectId, request.getTmplatIdx(), request.getDestHeaderIds());
|
||||
}
|
||||
|
||||
public List<NotiHubDestInfoDto> prepareAsyncRow(ListSearchService.DownloadListRow row,
|
||||
MessageSendBulkRequestDto request, MessageContext context, ChannelType channel) {
|
||||
return createDestInfos(List.of(row), request.getDestHeaderIds(), request.getDestNameHeaderId(),
|
||||
context, channel, new HashSet<>(), false);
|
||||
}
|
||||
|
||||
public boolean retainAsyncDestination(NotiHubDestInfoDto destination, ChannelType channel,
|
||||
Set<String> keys, boolean dedupe) {
|
||||
String value = channel == ChannelType.EMAIL ? destination.getDestEml() : destination.getDestTel();
|
||||
return !isDuplicateDestination(value, channel, keys, dedupe);
|
||||
}
|
||||
|
||||
private static class ListFilterDtoFactory {
|
||||
static com.kri.contact.server.dto.list.ListFilterDto onlySid(String sid) {
|
||||
return com.kri.contact.server.dto.list.ListFilterDto.onlyOneSid(sid);
|
||||
|
||||
@@ -0,0 +1,177 @@
|
||||
package com.kri.contact.server.service.async;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.kri.common.feature.web.BusinessException;
|
||||
import com.kri.common.feature.web.RequestUserContext;
|
||||
import com.kri.contact.server.common.enums.ListAsyncJobType;
|
||||
import com.kri.contact.server.dto.list.ListEventHistDownloadRequestDto;
|
||||
import com.kri.contact.server.dto.list.ListFilterDto;
|
||||
import com.kri.contact.server.dto.list.async.ListAsyncJobAcceptedDto;
|
||||
import com.kri.contact.server.dto.list.async.ListAsyncResultFileDto;
|
||||
import com.kri.contact.server.persistence.repository.ProjectRepository;
|
||||
import com.kri.contact.server.service.ListDownloadService;
|
||||
import com.kri.contact.server.service.file.ListAsyncResultFileService;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class ListAsyncDownloadService {
|
||||
private final ListAsyncJobAccess access;
|
||||
private final ListAsyncJobExecutor executor;
|
||||
private final ListDownloadService downloads;
|
||||
private final ListAsyncResultFileService files;
|
||||
private final ProjectRepository projects;
|
||||
private final ObjectMapper mapper;
|
||||
|
||||
public ListAsyncJobAcceptedDto list(Long projectId, ListFilterDto filter,
|
||||
boolean includeResult, RequestUserContext user) {
|
||||
return submit(projectId, ListAsyncJobType.LIST_DOWNLOAD, filter, includeResult, null, user);
|
||||
}
|
||||
|
||||
public ListAsyncJobAcceptedDto eventHistory(Long projectId,
|
||||
ListEventHistDownloadRequestDto request, boolean includeResult, RequestUserContext user) {
|
||||
if (request == null || request.getSearchBase() == null
|
||||
|| request.getResultCodeTypes() == null || request.getResultCodeTypes().isEmpty()) {
|
||||
throw BusinessException.badRequest("INVALID_HISTORY_DOWNLOAD", "이력 다운로드 조건이 필요합니다.");
|
||||
}
|
||||
var dates = request.getDateRange();
|
||||
if (dates != null && dates.getStartDate() != null && dates.getEndDate() != null
|
||||
&& dates.getStartDate().isAfter(dates.getEndDate())) {
|
||||
throw BusinessException.badRequest("INVALID_DATE_RANGE", "시작일은 종료일보다 늦을 수 없습니다.");
|
||||
}
|
||||
return submit(projectId, ListAsyncJobType.CONTACT_HISTORY_DOWNLOAD,
|
||||
request.getListFilter(), includeResult, request, user);
|
||||
}
|
||||
|
||||
public ListAsyncJobAcceptedDto updateHistory(Long projectId, ListFilterDto filter,
|
||||
RequestUserContext user) {
|
||||
return submit(projectId, ListAsyncJobType.UPDATE_HISTORY_DOWNLOAD, filter, false, null, user);
|
||||
}
|
||||
|
||||
public ListAsyncJobAcceptedDto itemFiles(Long projectId, ListFilterDto filter,
|
||||
RequestUserContext user) {
|
||||
return submit(projectId, ListAsyncJobType.ITEM_ZIP_DOWNLOAD, filter, false, null, user);
|
||||
}
|
||||
|
||||
private ListAsyncJobAcceptedDto submit(Long projectId, ListAsyncJobType type,
|
||||
ListFilterDto filter, boolean includeResult, ListEventHistDownloadRequestDto event,
|
||||
RequestUserContext user) {
|
||||
access.requireProject(projectId, user);
|
||||
var project = projects.findByPrjctIdxAndDelFalse(projectId);
|
||||
if (project == null) {
|
||||
throw BusinessException.notFound("PROJECT_NOT_FOUND", "프로젝트를 찾을 수 없습니다.");
|
||||
}
|
||||
Map<String, Object> parameters = new LinkedHashMap<>();
|
||||
parameters.put("listFilter", filter);
|
||||
parameters.put("includeResult", includeResult);
|
||||
if (event != null) {
|
||||
parameters.put("eventHistory", event);
|
||||
}
|
||||
Map<String, Object> snapshot = mapper.convertValue(parameters, new TypeReference<>() {});
|
||||
// 요청 객체가 접수 후 변경되어도 worker의 조건은 바뀌지 않는다.
|
||||
ListFilterDto savedFilter = mapper.convertValue(snapshot.get("listFilter"), ListFilterDto.class);
|
||||
ListEventHistDownloadRequestDto savedEvent = event == null ? null
|
||||
: mapper.convertValue(snapshot.get("eventHistory"), ListEventHistDownloadRequestDto.class);
|
||||
String name = project.getPrjctNm() + switch (type) {
|
||||
case LIST_DOWNLOAD -> " list.xlsx";
|
||||
case CONTACT_HISTORY_DOWNLOAD -> " 컨택 이력.xlsx";
|
||||
case UPDATE_HISTORY_DOWNLOAD -> " 수정 이력.xlsx";
|
||||
case ITEM_ZIP_DOWNLOAD -> " item_files.zip";
|
||||
default -> throw new IllegalArgumentException("Unsupported download type");
|
||||
};
|
||||
String key = executor.submit(projectId, user.getUserId(), type, snapshot, null,
|
||||
context -> generate(projectId, type, savedFilter, includeResult, savedEvent, name, user, context));
|
||||
return new ListAsyncJobAcceptedDto(key);
|
||||
}
|
||||
|
||||
private void generate(Long projectId, ListAsyncJobType type, ListFilterDto filter,
|
||||
boolean includeResult, ListEventHistDownloadRequestDto event, String name,
|
||||
RequestUserContext user, ListAsyncJobExecutor.JobContext context) {
|
||||
access.requireProject(projectId, user);
|
||||
Progress observer = new Progress(context, type == ListAsyncJobType.ITEM_ZIP_DOWNLOAD);
|
||||
context.progress("PREPARING_FILE", null, observer.values(false));
|
||||
ListAsyncResultFileDto result = null;
|
||||
try {
|
||||
result = files.create("RESULT", name, output -> {
|
||||
OutputStream checked = new OutputStream() {
|
||||
@Override public void write(int value) throws IOException {
|
||||
context.checkCancelled();
|
||||
output.write(value);
|
||||
}
|
||||
@Override public void write(byte[] buffer, int offset, int length) throws IOException {
|
||||
context.checkCancelled();
|
||||
output.write(buffer, offset, length);
|
||||
}
|
||||
@Override public void flush() throws IOException { output.flush(); }
|
||||
// 저장소 서비스가 소유한 스트림은 이 래퍼에서 닫지 않는다.
|
||||
};
|
||||
switch (type) {
|
||||
case LIST_DOWNLOAD -> downloads.writeList(projectId, filter, includeResult, checked, observer);
|
||||
case CONTACT_HISTORY_DOWNLOAD -> downloads.writeListWithEventHist(projectId, filter,
|
||||
event.getResultCodeTypes(), event.getSearchBase(), event.getDateRange(),
|
||||
event.getHistoryOrder(), includeResult, checked, observer);
|
||||
case UPDATE_HISTORY_DOWNLOAD -> downloads.writeSidWithUpdateHist(projectId, filter, checked, observer);
|
||||
case ITEM_ZIP_DOWNLOAD -> downloads.writeItemFile(projectId, filter, checked, observer);
|
||||
default -> throw new IllegalArgumentException("Unsupported download type");
|
||||
}
|
||||
context.checkCancelled();
|
||||
});
|
||||
context.checkCancelled();
|
||||
access.requireProject(projectId, user);
|
||||
context.resultFile(result);
|
||||
context.progress("FILE_READY", null, observer.values(true));
|
||||
} catch (IOException failure) {
|
||||
discard(result, failure);
|
||||
throw BusinessException.internalError("LIST_DOWNLOAD_FAILED", "다운로드 파일 생성 중 오류가 발생했습니다.");
|
||||
} catch (RuntimeException failure) {
|
||||
discard(result, failure);
|
||||
throw failure;
|
||||
}
|
||||
}
|
||||
|
||||
private void discard(ListAsyncResultFileDto file, Exception failure) {
|
||||
if (file != null) {
|
||||
try {
|
||||
files.discard(file.fileRef());
|
||||
} catch (IOException cleanupFailure) {
|
||||
failure.addSuppressed(cleanupFailure);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static final class Progress implements ListDownloadService.DownloadObserver {
|
||||
private final ListAsyncJobExecutor.JobContext context;
|
||||
private final boolean zip;
|
||||
private long processed;
|
||||
private long written;
|
||||
|
||||
private Progress(ListAsyncJobExecutor.JobContext context, boolean zip) {
|
||||
this.context = context;
|
||||
this.zip = zip;
|
||||
}
|
||||
|
||||
@Override public void checkCancelled() { context.checkCancelled(); }
|
||||
|
||||
@Override public void onProgress(long processedSidCount, long writtenCount) {
|
||||
processed = processedSidCount;
|
||||
written = writtenCount;
|
||||
context.progress("GENERATING_FILE", null, values(false));
|
||||
}
|
||||
|
||||
@Override public void beforeWrite() {
|
||||
context.checkCancelled();
|
||||
context.progress("WRITING_FILE", null, values(false));
|
||||
}
|
||||
|
||||
private Map<String, Object> values(boolean ready) {
|
||||
return Map.of("processedSidCnt", processed,
|
||||
zip ? "writtenFileCnt" : "writtenRowCnt", written, "fileReady", ready);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.kri.contact.server.service.async;
|
||||
|
||||
import com.kri.common.feature.web.BusinessException;
|
||||
import com.kri.common.feature.web.RequestUserContext;
|
||||
import com.kri.contact.server.common.consts.ContactConstant;
|
||||
import com.kri.contact.server.persistence.entity.TListAsyncJobEntity;
|
||||
import com.kri.contact.server.persistence.entity.TPrjctEntity;
|
||||
import com.kri.contact.server.persistence.repository.ProjectRepository;
|
||||
import com.kri.contact.server.persistence.support.ProjectSpecs;
|
||||
import java.util.List;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class ListAsyncJobAccess {
|
||||
private final ProjectRepository projects;
|
||||
private final ListAsyncJobStore store;
|
||||
|
||||
public TListAsyncJobEntity require(Long projectId, String key, RequestUserContext user) {
|
||||
if (user == null || !user.isAuthenticated() || user.getUserRole() == null) {
|
||||
throw BusinessException.unauthorized("AUTH_REQUIRED", "로그인이 필요합니다.");
|
||||
}
|
||||
TListAsyncJobEntity job = store.get(key);
|
||||
if (!projectId.equals(job.getPrjctIdx()) || !user.getUserId().equals(job.getRequestUser())) {
|
||||
throw BusinessException.notFound("LIST_JOB_NOT_FOUND", "작업을 찾을 수 없습니다.");
|
||||
}
|
||||
requireProject(projectId, user);
|
||||
return job;
|
||||
}
|
||||
|
||||
// 다른 사용자의 작업도 포함한다. 호출자는 요청자 정보·jobKey 노출을 직접 제한한다.
|
||||
public java.util.Optional<TListAsyncJobEntity> activeUploadReset(Long projectId, RequestUserContext user) {
|
||||
requireProject(projectId, user);
|
||||
return store.activeUploadReset(projectId);
|
||||
}
|
||||
|
||||
public List<TListAsyncJobEntity> myActive(Long projectId, RequestUserContext user) {
|
||||
requireProject(projectId, user);
|
||||
return store.activeByUser(projectId, user.getUserId());
|
||||
}
|
||||
|
||||
public void requireProject(Long projectId, RequestUserContext user) {
|
||||
if (user == null || !user.isAuthenticated() || user.getUserRole() == null) {
|
||||
throw BusinessException.unauthorized("AUTH_REQUIRED", "로그인이 필요합니다.");
|
||||
}
|
||||
Specification<TPrjctEntity> scope = ProjectSpecs.isDelFalse().and(
|
||||
(root, query, cb) -> cb.equal(root.get("prjctIdx"), projectId));
|
||||
int role = user.getUserRole();
|
||||
if (role >= ContactConstant.RESEARCHER_USER_ROLE
|
||||
&& role < ContactConstant.RESEARCHER_USER_ROLE + 1000) {
|
||||
scope = scope.and(ProjectSpecs.ownerDeptEqual(user.getUserTeam()));
|
||||
} else if (role >= ContactConstant.OUTSOURCE_USER_ROLE) {
|
||||
boolean fieldwork = role >= ContactConstant.FIELDWORK_USER_ROLE;
|
||||
scope = scope.and(ProjectSpecs.statusIn(fieldwork ? List.of((short) 1) : List.of((short) 0, (short) 1)))
|
||||
.and(fieldwork ? ProjectSpecs.assignedToUserAndNotExcluded(user.getUserId())
|
||||
: ProjectSpecs.assignedToCmpn(user.getUserCmpn()));
|
||||
}
|
||||
if (!projects.exists(scope)) {
|
||||
throw BusinessException.forbidden("PROJECT_ACCESS_DENIED", "프로젝트 접근 권한이 없습니다.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,280 @@
|
||||
package com.kri.contact.server.service.async;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.kri.common.feature.web.BusinessException;
|
||||
import com.kri.contact.server.common.enums.ListAsyncJobType;
|
||||
import com.kri.contact.server.common.enums.ListAsyncAssignOperation;
|
||||
import com.kri.contact.server.config.ListAsyncProperties;
|
||||
import com.kri.contact.server.dto.list.async.ListAsyncAvailabilityDto;
|
||||
import com.kri.contact.server.dto.list.async.ListAsyncResultFileDto;
|
||||
import com.kri.contact.server.persistence.entity.TListAsyncJobEntity;
|
||||
import com.kri.contact.server.service.file.ListAsyncResultFileService;
|
||||
import jakarta.annotation.PreDestroy;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.CancellationException;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.RejectedExecutionException;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.function.Consumer;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.support.TransactionSynchronizationManager;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class ListAsyncJobExecutor implements ApplicationRunner {
|
||||
private final ListAsyncJobStore store;
|
||||
private final ProjectJobPermits permits;
|
||||
private final ListAsyncProperties properties;
|
||||
private final ObjectMapper mapper;
|
||||
private final ListAsyncResultFileService files;
|
||||
private final com.kri.contact.server.service.file.ListAsyncMessageSpool messageSpool;
|
||||
private final ThreadPoolExecutor executor;
|
||||
private final ConcurrentHashMap<String, JobTask> tasks = new ConcurrentHashMap<>();
|
||||
private volatile boolean accepting;
|
||||
|
||||
public ListAsyncJobExecutor(ListAsyncJobStore store, ProjectJobPermits permits,
|
||||
ListAsyncProperties properties, ObjectMapper mapper, ListAsyncResultFileService files) {
|
||||
this(store, permits, properties, mapper, files, null);
|
||||
}
|
||||
|
||||
@org.springframework.beans.factory.annotation.Autowired
|
||||
public ListAsyncJobExecutor(ListAsyncJobStore store, ProjectJobPermits permits,
|
||||
ListAsyncProperties properties, ObjectMapper mapper, ListAsyncResultFileService files,
|
||||
com.kri.contact.server.service.file.ListAsyncMessageSpool messageSpool) {
|
||||
this.store = store;
|
||||
this.permits = permits;
|
||||
this.properties = properties;
|
||||
this.mapper = mapper;
|
||||
this.files = files;
|
||||
this.messageSpool = messageSpool;
|
||||
this.executor = new ThreadPoolExecutor(properties.getThreads(), properties.getThreads(),
|
||||
0, TimeUnit.SECONDS, new ArrayBlockingQueue<>(properties.getQueueCapacity()),
|
||||
runnable -> new Thread(runnable, "list-async-worker"), new ThreadPoolExecutor.AbortPolicy());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(ApplicationArguments args) throws java.io.IOException {
|
||||
store.recover();
|
||||
if (messageSpool != null) messageSpool.cleanupInterrupted();
|
||||
files.cleanupIncomplete();
|
||||
accepting = true;
|
||||
}
|
||||
|
||||
// 외부 입력으로 임의 작업을 실행하지 않는다. 기능별 서비스가 입력·권한 검증 후 호출한다.
|
||||
public String submit(Long projectId, String user, ListAsyncJobType type,
|
||||
Map<String, Object> request, String inputFileRef, Consumer<JobContext> work) {
|
||||
return submit(projectId, user, type, request, inputFileRef, work, null);
|
||||
}
|
||||
|
||||
public String submit(Long projectId, String user, ListAsyncJobType type,
|
||||
Map<String, Object> request, String inputFileRef, Consumer<JobContext> work,
|
||||
Consumer<TListAsyncJobEntity> initialize) {
|
||||
if (!accepting) {
|
||||
throw BusinessException.conflict("LIST_JOBS_UNAVAILABLE", "작업 접수를 준비 중입니다.");
|
||||
}
|
||||
if (TransactionSynchronizationManager.isActualTransactionActive()) {
|
||||
throw new IllegalStateException("Submit outside a business transaction");
|
||||
}
|
||||
if (projectId == null || user == null || user.isBlank() || user.length() > 32
|
||||
|| type == null || request == null || work == null) {
|
||||
throw new IllegalArgumentException("Invalid job submission");
|
||||
}
|
||||
Map<String, Object> snapshot = mapper.convertValue(request, new TypeReference<>() {});
|
||||
String key = UUID.randomUUID().toString().replace("-", "");
|
||||
ProjectJobPermits.Permit permit = permits.acquire(projectId, user, type, key,
|
||||
ListAsyncAssignOperation.fromRequest(type, snapshot));
|
||||
JobTask task = new JobTask(key, work, permit);
|
||||
try {
|
||||
// The project reservation blocks conflicts until DB commit and final cleanup.
|
||||
if (initialize == null) {
|
||||
store.create(key, projectId, user, type, snapshot, inputFileRef, properties.getOwnerInstance());
|
||||
} else {
|
||||
store.create(key, projectId, user, type, snapshot, inputFileRef, properties.getOwnerInstance(), initialize);
|
||||
}
|
||||
tasks.put(key, task);
|
||||
try {
|
||||
executor.execute(task);
|
||||
} catch (RejectedExecutionException rejected) {
|
||||
store.finish(key, "EXECUTOR_FULL");
|
||||
throw BusinessException.conflict("LIST_JOB_CAPACITY", "작업 대기 용량을 초과했습니다.");
|
||||
}
|
||||
return key;
|
||||
} catch (RuntimeException | Error failure) {
|
||||
tasks.remove(key, task);
|
||||
task.release();
|
||||
throw failure;
|
||||
}
|
||||
}
|
||||
|
||||
public ListAsyncAvailabilityDto availability(
|
||||
Long projectId, String user, ListAsyncJobType type) {
|
||||
return permits.availability(projectId, user, type);
|
||||
}
|
||||
|
||||
public void cancel(String key, String user) {
|
||||
JobTask task = tasks.get(key);
|
||||
// 취소 요청 DB 기록이 구간의 행 잠금을 기다리는 동안에도 worker가 중단을 확인할 수 있다.
|
||||
if (task != null) {
|
||||
task.cancelUser = user;
|
||||
task.cancelled.set(true);
|
||||
}
|
||||
store.requestCancel(key, user);
|
||||
if (task != null && executor.remove(task)) {
|
||||
try {
|
||||
store.finish(key, null);
|
||||
} finally {
|
||||
tasks.remove(key, task);
|
||||
task.release();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
public void shutdown() {
|
||||
accepting = false;
|
||||
tasks.values().forEach(task -> task.cancelled.set(true));
|
||||
executor.shutdown();
|
||||
try {
|
||||
if (!executor.awaitTermination(30, TimeUnit.SECONDS)) {
|
||||
failQueuedOnShutdown();
|
||||
}
|
||||
} catch (InterruptedException interrupted) {
|
||||
failQueuedOnShutdown();
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
|
||||
private void failQueuedOnShutdown() {
|
||||
for (Runnable queued : executor.shutdownNow()) {
|
||||
JobTask task = (JobTask) queued;
|
||||
try {
|
||||
store.finish(task.key, "SERVICE_RESTART");
|
||||
} finally {
|
||||
tasks.remove(task.key, task);
|
||||
task.release();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public final class JobContext {
|
||||
private final JobTask task;
|
||||
private JobContext(JobTask task) { this.task = task; }
|
||||
public String jobKey() { return task.key; }
|
||||
public TListAsyncJobEntity job() { return store.get(task.key); }
|
||||
public void complete(Runnable changes) {
|
||||
store.complete(task.key, changes, this::checkCancelled);
|
||||
}
|
||||
public void reject(String code, String message, Map<String, Object> detail) {
|
||||
checkCancelled();
|
||||
store.reject(task.key, code, message, detail);
|
||||
}
|
||||
public void rangeFailure(String stage, int attempt, List<String> sids) {
|
||||
checkCancelled();
|
||||
store.rangeFailure(task.key, stage, attempt, sids);
|
||||
task.failedChunkNo = null;
|
||||
}
|
||||
public void messageBatch(int number, Map<String, Object> batch) { store.messageBatch(task.key, number, batch); }
|
||||
public void messagePreparation(List<Map<String, Object>> failures) {
|
||||
checkCancelled();
|
||||
store.messagePreparation(task.key, failures);
|
||||
}
|
||||
public void applyPreparationChunk(int number, String operation, List<String> sids, Runnable changes) {
|
||||
store.applyChunk(task.key, number, operation, sids, changes, this::checkCancelled, false);
|
||||
}
|
||||
public void checkCancelled() {
|
||||
if (task.cancelled.get() || Thread.currentThread().isInterrupted()) {
|
||||
throw new CancellationException();
|
||||
}
|
||||
}
|
||||
public void progress(String stage, Long totalSids, Map<String, Object> progress) {
|
||||
checkCancelled();
|
||||
store.progress(task.key, stage, totalSids, progress);
|
||||
}
|
||||
public void resultFile(ListAsyncResultFileDto file) {
|
||||
checkCancelled();
|
||||
store.resultFile(task.key, file);
|
||||
}
|
||||
public void applyChunk(int number, String operation, List<String> sids, Runnable changes) {
|
||||
try {
|
||||
store.applyChunk(task.key, number, operation, sids, changes, this::checkCancelled);
|
||||
} catch (CancellationException cancelled) {
|
||||
throw cancelled;
|
||||
} catch (RuntimeException failure) {
|
||||
task.failedChunkNo = number;
|
||||
throw failure;
|
||||
}
|
||||
}
|
||||
public void applyFixedChunk(Long projectId, int number, String operation, List<String> sids,
|
||||
Consumer<List<String>> changes) {
|
||||
try {
|
||||
store.applyFixedChunk(task.key, projectId, number, operation, sids, changes, this::checkCancelled);
|
||||
} catch (CancellationException cancelled) {
|
||||
throw cancelled;
|
||||
} catch (RuntimeException failure) {
|
||||
task.failedChunkNo = number;
|
||||
throw failure;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private final class JobTask implements Runnable {
|
||||
private final String key;
|
||||
private final Consumer<JobContext> work;
|
||||
private final ProjectJobPermits.Permit permit;
|
||||
private final AtomicBoolean cancelled = new AtomicBoolean();
|
||||
private volatile String cancelUser;
|
||||
private Integer failedChunkNo;
|
||||
|
||||
private JobTask(String key, Consumer<JobContext> work, ProjectJobPermits.Permit permit) {
|
||||
this.key = key;
|
||||
this.work = work;
|
||||
this.permit = permit;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
String errorCode = null;
|
||||
try {
|
||||
if (store.start(key)) {
|
||||
JobContext context = new JobContext(this);
|
||||
context.checkCancelled();
|
||||
work.accept(context);
|
||||
}
|
||||
} catch (CancellationException ignored) {
|
||||
// 사용자 취소는 CANCEL_REQUESTED 기록과 함께 종료한다.
|
||||
cancelled.set(true);
|
||||
} catch (Throwable failure) {
|
||||
// 업로드 내용·전화번호·서버 경로 등이 예외 메시지에 포함될 수 있어 출력하지 않는다.
|
||||
log.warn("List async job failed: jobKey={}, type={}", key, failure.getClass().getSimpleName());
|
||||
errorCode = "JOB_EXECUTION_FAILED";
|
||||
} finally {
|
||||
try {
|
||||
if (!accepting) {
|
||||
errorCode = "SERVICE_RESTART";
|
||||
} else if (cancelled.get()) {
|
||||
store.requestCancel(key, cancelUser);
|
||||
}
|
||||
store.finish(key, errorCode, failedChunkNo);
|
||||
} finally {
|
||||
tasks.remove(key, this);
|
||||
release();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void release() {
|
||||
if (permit != null) {
|
||||
permit.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,400 @@
|
||||
package com.kri.contact.server.service.async;
|
||||
|
||||
import com.kri.common.feature.datetime.DateTimeUtil;
|
||||
import com.kri.common.feature.web.BusinessException;
|
||||
import com.kri.contact.server.common.enums.ListAsyncJobStatus;
|
||||
import com.kri.contact.server.common.enums.ListAsyncJobType;
|
||||
import com.kri.contact.server.dto.list.async.ListAsyncResultFileDto;
|
||||
import com.kri.contact.server.dto.list.async.ListAsyncJobActivity;
|
||||
import com.kri.contact.server.persistence.entity.TListAsyncJobChunkEntity;
|
||||
import com.kri.contact.server.persistence.entity.TListAsyncJobEntity;
|
||||
import com.kri.contact.server.persistence.entity.id.TListAsyncJobChunkId;
|
||||
import com.kri.contact.server.persistence.repository.ListAsyncJobChunkRepository;
|
||||
import com.kri.contact.server.persistence.repository.ListAsyncJobRepository;
|
||||
import com.kri.contact.server.persistence.repository.ListRepository;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.LockModeType;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CancellationException;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class ListAsyncJobStore {
|
||||
private final ListAsyncJobRepository jobs;
|
||||
private final ListAsyncJobChunkRepository chunks;
|
||||
private final ListRepository lists;
|
||||
private final EntityManager entityManager;
|
||||
|
||||
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||
public void recover() {
|
||||
// 현재 단일 인스턴스에서 접수 허용 전에만 실행한다.
|
||||
jobs.failInterruptedJobs(DateTimeUtil.nowKstLocalDateTime());
|
||||
}
|
||||
|
||||
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||
public void create(String key, Long projectId, String user, ListAsyncJobType type,
|
||||
Map<String, Object> request, String inputFileRef, String owner) {
|
||||
create(key, projectId, user, type, request, inputFileRef, owner, job -> {});
|
||||
}
|
||||
|
||||
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||
public void create(String key, Long projectId, String user, ListAsyncJobType type,
|
||||
Map<String, Object> request, String inputFileRef, String owner,
|
||||
java.util.function.Consumer<TListAsyncJobEntity> initialize) {
|
||||
// 호출자는 프로젝트 점유를 먼저 등록하여 다른 접수가 생성 커밋 전에 진입하지 못하게 한다.
|
||||
if (jobs.existsByPrjctIdxAndRequestUserAndJobTypeAndJobStatusIn(projectId, user, type, ListAsyncJobStatus.ACTIVE)) {
|
||||
throw BusinessException.conflict("LIST_JOB_USER_BUSY", "진행 중인 같은 작업이 있습니다. 완료 후 다시 요청해 주세요.");
|
||||
}
|
||||
TListAsyncJobEntity job = new TListAsyncJobEntity();
|
||||
job.setJobKey(key);
|
||||
job.setPrjctIdx(projectId);
|
||||
job.setRequestUser(user);
|
||||
job.setJobType(type);
|
||||
job.setJobStatus(ListAsyncJobStatus.QUEUED);
|
||||
job.setJobStage("ACCEPTED");
|
||||
job.setRequestJson(request);
|
||||
job.setInputFileRef(inputFileRef);
|
||||
job.setOwnerInstance(owner);
|
||||
job.setRegDt(DateTimeUtil.nowKstLocalDateTime());
|
||||
job.setModDt(job.getRegDt());
|
||||
initialize.accept(job);
|
||||
jobs.saveAndFlush(job);
|
||||
}
|
||||
|
||||
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||
public void complete(String key, Runnable changes, Runnable checkCancelled) {
|
||||
requireRunning(lock(key));
|
||||
checkCancelled.run();
|
||||
changes.run();
|
||||
checkCancelled.run();
|
||||
var job = lock(key);
|
||||
requireRunning(job);
|
||||
if ("POST_PROCESSING".equals(job.getJobStage())) {
|
||||
var progress = new java.util.LinkedHashMap<String, Object>(
|
||||
job.getProgressJson() == null ? Map.of() : job.getProgressJson());
|
||||
progress.put("postProcessingComplete", true);
|
||||
job.setProgressJson(progress);
|
||||
}
|
||||
finishLocked(job, null, null);
|
||||
}
|
||||
|
||||
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||
public void reject(String key, String code, String message, Map<String, Object> detail) {
|
||||
TListAsyncJobEntity job = lock(key);
|
||||
requireRunning(job);
|
||||
job.setErrorDetailJson(detail);
|
||||
finishLocked(job, code, null);
|
||||
job.setErrorMessage(message);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public TListAsyncJobEntity get(String key) {
|
||||
return jobs.findById(key).orElseThrow(() ->
|
||||
BusinessException.notFound("LIST_JOB_NOT_FOUND", "작업을 찾을 수 없습니다."));
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true, propagation = Propagation.REQUIRES_NEW)
|
||||
public List<ListAsyncJobActivity> activity(
|
||||
Long projectId, List<String> heldKeys) {
|
||||
return jobs.findActivity(projectId, ListAsyncJobStatus.ACTIVE,
|
||||
heldKeys.isEmpty() ? List.of("") : heldKeys);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public java.util.Optional<TListAsyncJobEntity> activeUploadReset(Long projectId) {
|
||||
return jobs.findFirstByPrjctIdxAndJobTypeInAndJobStatusInOrderByRegDtDescJobKeyDesc(projectId,
|
||||
List.of(ListAsyncJobType.LIST_UPLOAD, ListAsyncJobType.LIST_RESET), ListAsyncJobStatus.ACTIVE);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<TListAsyncJobEntity> activeByUser(Long projectId, String user) {
|
||||
return jobs.findByPrjctIdxAndRequestUserAndJobStatusInOrderByRegDtDescJobKeyDesc(
|
||||
projectId, user, ListAsyncJobStatus.ACTIVE);
|
||||
}
|
||||
|
||||
private TListAsyncJobEntity lock(String key) {
|
||||
var job = jobs.findForUpdate(key).orElseThrow(() ->
|
||||
BusinessException.notFound("LIST_JOB_NOT_FOUND", "작업을 찾을 수 없습니다."));
|
||||
// OSIV may retain the entity loaded for access checks before another worker commits.
|
||||
// A locking query alone does not replace that managed entity's stale field values.
|
||||
entityManager.refresh(job, LockModeType.PESSIMISTIC_WRITE);
|
||||
return job;
|
||||
}
|
||||
|
||||
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||
public boolean start(String key) {
|
||||
TListAsyncJobEntity job = lock(key);
|
||||
if (job.getJobStatus() != ListAsyncJobStatus.QUEUED) {
|
||||
return false;
|
||||
}
|
||||
job.setJobStatus(ListAsyncJobStatus.RUNNING);
|
||||
job.setStartedDt(DateTimeUtil.nowKstLocalDateTime());
|
||||
job.setModDt(job.getStartedDt());
|
||||
return true;
|
||||
}
|
||||
|
||||
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||
public void requestCancel(String key, String user) {
|
||||
TListAsyncJobEntity job = lock(key);
|
||||
if (job.getJobStatus().isTerminal()) {
|
||||
return;
|
||||
}
|
||||
if (job.getJobStatus() == ListAsyncJobStatus.CANCEL_REQUESTED) {
|
||||
if (job.getCancelRequestUser() == null && user != null) {
|
||||
job.setCancelRequestUser(user);
|
||||
}
|
||||
return;
|
||||
}
|
||||
job.setJobStatus(ListAsyncJobStatus.CANCEL_REQUESTED);
|
||||
job.setCancelReason("USER_REQUEST");
|
||||
job.setCancelRequestUser(user);
|
||||
job.setCancelRequestedDt(DateTimeUtil.nowKstLocalDateTime());
|
||||
job.setModDt(job.getCancelRequestedDt());
|
||||
}
|
||||
|
||||
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||
public void finish(String key, String errorCode) {
|
||||
finishLocked(lock(key), errorCode, null);
|
||||
}
|
||||
|
||||
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||
public void finish(String key, String errorCode, Integer failedChunkNo) {
|
||||
finishLocked(lock(key), errorCode, failedChunkNo);
|
||||
}
|
||||
|
||||
private void finishLocked(TListAsyncJobEntity job, String errorCode, Integer failedChunkNo) {
|
||||
if (job.getJobStatus().isTerminal()) {
|
||||
return;
|
||||
}
|
||||
if (errorCode != null) {
|
||||
job.setJobStatus(ListAsyncJobStatus.FAILED);
|
||||
job.setErrorCode(errorCode);
|
||||
job.setErrorMessage("작업 처리 중 오류가 발생했습니다.");
|
||||
job.setFailedChunkNo(failedChunkNo);
|
||||
} else if (job.getJobStatus() == ListAsyncJobStatus.CANCEL_REQUESTED) {
|
||||
job.setJobStatus(ListAsyncJobStatus.CANCELLED);
|
||||
} else if (job.getErrorDetailJson() != null && job.getErrorDetailJson().get("rangeFailures") instanceof List<?> failures && !failures.isEmpty()) {
|
||||
job.setJobStatus(ListAsyncJobStatus.FAILED);
|
||||
job.setJobStage("COMPLETED");
|
||||
job.setErrorCode("PARTIAL_RANGE_FAILURE");
|
||||
job.setErrorMessage("일부 구간이 실패했으며 나머지 구간 처리는 완료되었습니다.");
|
||||
} else if (failedSidCount(job) > 0) {
|
||||
job.setJobStatus(ListAsyncJobStatus.FAILED);
|
||||
job.setJobStage("COMPLETED");
|
||||
job.setErrorCode("PARTIAL_SID_FAILURE");
|
||||
job.setErrorMessage("삭제된 SID를 제외한 나머지 처리가 완료되었습니다.");
|
||||
} else {
|
||||
job.setJobStatus(ListAsyncJobStatus.SUCCEEDED);
|
||||
job.setJobStage("COMPLETED");
|
||||
}
|
||||
job.setFinishedDt(DateTimeUtil.nowKstLocalDateTime());
|
||||
job.setModDt(job.getFinishedDt());
|
||||
}
|
||||
|
||||
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||
public void progress(String key, String stage, Long totalSids, Map<String, Object> progress) {
|
||||
TListAsyncJobEntity job = lock(key);
|
||||
requireRunning(job);
|
||||
if (stage == null || stage.isBlank() || stage.length() > 32 || (totalSids != null && totalSids < 0)) {
|
||||
throw new IllegalArgumentException("Invalid job progress");
|
||||
}
|
||||
job.setJobStage(stage);
|
||||
job.setTotalSidCnt(totalSids);
|
||||
job.setProgressJson(progress);
|
||||
job.setModDt(DateTimeUtil.nowKstLocalDateTime());
|
||||
}
|
||||
|
||||
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||
public void resultFile(String key, ListAsyncResultFileDto file) {
|
||||
TListAsyncJobEntity job = lock(key);
|
||||
requireRunning(job);
|
||||
var files = new java.util.ArrayList<>(
|
||||
job.getResultFilesJson() == null ? List.<ListAsyncResultFileDto>of() : job.getResultFilesJson());
|
||||
files.add(file);
|
||||
job.setResultFilesJson(files);
|
||||
job.setModDt(DateTimeUtil.nowKstLocalDateTime());
|
||||
}
|
||||
|
||||
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||
public void applyChunk(String key, int chunkNo, String operation, List<String> sids,
|
||||
Runnable changes, Runnable checkCancelled) {
|
||||
applyChunk(key, chunkNo, operation, sids, changes, checkCancelled, true);
|
||||
}
|
||||
|
||||
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||
public void applyChunk(String key, int chunkNo, String operation, List<String> sids,
|
||||
Runnable changes, Runnable checkCancelled, boolean countSids) {
|
||||
if (chunkNo < 1 || operation == null || operation.isBlank() || operation.length() > 32
|
||||
|| sids.isEmpty() || sids.stream().anyMatch(java.util.Objects::isNull)
|
||||
|| new HashSet<>(sids).size() != sids.size()) {
|
||||
throw new IllegalArgumentException("Invalid job chunk");
|
||||
}
|
||||
checkCancelled.run();
|
||||
changes.run();
|
||||
checkCancelled.run();
|
||||
TListAsyncJobEntity job = lock(key);
|
||||
requireRunning(job);
|
||||
if (chunkNo != job.getCompletedChunkCnt() + 1) {
|
||||
throw new IllegalArgumentException("Chunk number must follow the last committed chunk");
|
||||
}
|
||||
TListAsyncJobChunkEntity chunk = new TListAsyncJobChunkEntity();
|
||||
chunk.setId(new TListAsyncJobChunkId(key, chunkNo));
|
||||
chunk.setOperationType(operation);
|
||||
chunk.setCompletedSidsJson(List.copyOf(sids));
|
||||
chunk.setCompletedSidCnt(sids.size());
|
||||
chunk.setRecordedDt(DateTimeUtil.nowKstLocalDateTime());
|
||||
chunks.saveAndFlush(chunk);
|
||||
if (countSids) {
|
||||
job.setCompletedSidCnt(Math.addExact(job.getCompletedSidCnt(), sids.size()));
|
||||
}
|
||||
job.setCompletedChunkCnt(job.getCompletedChunkCnt() + 1);
|
||||
if (job.getProgressJson() != null) {
|
||||
var progress = new java.util.LinkedHashMap<String, Object>(job.getProgressJson());
|
||||
if (countSids && "LIST_UPLOAD".equals(operation) && progress.containsKey("insertTotalSidCnt")) {
|
||||
progress.put("insertCompletedSidCnt", job.getCompletedSidCnt());
|
||||
} else if ("RESET_ALL".equals(operation) && progress.containsKey("deleteTotalSidCnt")) {
|
||||
long deleted = ((Number) progress.getOrDefault("deleteCompletedSidCnt", 0)).longValue();
|
||||
progress.put("deleteCompletedSidCnt", Math.addExact(deleted, sids.size()));
|
||||
}
|
||||
job.setProgressJson(progress);
|
||||
}
|
||||
job.setModDt(chunk.getRecordedDt());
|
||||
}
|
||||
|
||||
public static long failedSidCount(TListAsyncJobEntity job) {
|
||||
Object value = job.getErrorDetailJson() == null ? null : job.getErrorDetailJson().get("failedSidCnt");
|
||||
return value instanceof Number number ? number.longValue() : 0;
|
||||
}
|
||||
|
||||
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||
public void rangeFailure(String key, String stage, int attempt, List<String> sids) {
|
||||
var job = lock(key);
|
||||
requireRunning(job);
|
||||
var detail = new java.util.LinkedHashMap<String, Object>(job.getErrorDetailJson() == null ? Map.of() : job.getErrorDetailJson());
|
||||
var failures = new java.util.ArrayList<Object>();
|
||||
if (detail.get("rangeFailures") instanceof List<?> old) failures.addAll(old);
|
||||
failures.add(Map.of("stage", stage, "attempt", attempt, "sids", List.copyOf(sids), "code", "RANGE_EXECUTION_FAILED"));
|
||||
detail.put("rangeFailures", failures);
|
||||
job.setErrorDetailJson(detail);
|
||||
job.setModDt(DateTimeUtil.nowKstLocalDateTime());
|
||||
}
|
||||
|
||||
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||
public void messagePreparation(String key, List<Map<String, Object>> failures) {
|
||||
var job = lock(key);
|
||||
requireRunning(job);
|
||||
var detail = new java.util.LinkedHashMap<String, Object>(job.getErrorDetailJson() == null ? Map.of() : job.getErrorDetailJson());
|
||||
detail.put("preparationFailures", List.copyOf(failures));
|
||||
job.setErrorDetailJson(detail);
|
||||
job.setModDt(DateTimeUtil.nowKstLocalDateTime());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||
public void messageBatch(String key, int number, Map<String, Object> batch) {
|
||||
var job = lock(key);
|
||||
if (job.getJobStatus() != ListAsyncJobStatus.RUNNING && job.getJobStatus() != ListAsyncJobStatus.CANCEL_REQUESTED) {
|
||||
throw new IllegalStateException("Message job is not active");
|
||||
}
|
||||
var detail = new java.util.LinkedHashMap<String, Object>(job.getErrorDetailJson() == null ? Map.of() : job.getErrorDetailJson());
|
||||
var batches = new java.util.ArrayList<Map<String, Object>>();
|
||||
if (detail.get("messageBatches") instanceof List<?> old) {
|
||||
for (Object entry : old) batches.add(new java.util.LinkedHashMap<>((Map<String,Object>) entry));
|
||||
}
|
||||
if (number < 1 || number > batches.size() + 1) throw new IllegalArgumentException("Invalid dispatch sequence");
|
||||
var entryValue = new java.util.LinkedHashMap<String,Object>(batch);
|
||||
entryValue.put("batchNo", number);
|
||||
if (number == batches.size() + 1) batches.add(Map.copyOf(entryValue));
|
||||
else {
|
||||
if (Boolean.TRUE.equals(batches.get(number - 1).get("completed"))) throw new IllegalStateException("Dispatch already recorded");
|
||||
batches.set(number - 1, Map.copyOf(entryValue));
|
||||
}
|
||||
detail.put("messageBatches", batches);
|
||||
job.setErrorDetailJson(detail);
|
||||
var progress = new java.util.LinkedHashMap<String, Object>(job.getProgressJson() == null ? Map.of() : job.getProgressJson());
|
||||
for (String metric : List.of("acceptedRecipientCnt", "failedRecipientCnt", "unknownRecipientCnt", "attemptedRecipientCnt")) {
|
||||
long total = 0;
|
||||
for (var entry : batches) total = Math.addExact(total, ((Number) entry.getOrDefault(metric, 0)).longValue());
|
||||
progress.put(metric, total);
|
||||
}
|
||||
progress.put("recordedBatchCnt", batches.size());
|
||||
long inFlight = batches.stream().filter(entry -> !Boolean.TRUE.equals(entry.get("completed")))
|
||||
.mapToLong(entry -> ((Number) entry.get("targetRecipientCnt")).longValue()).sum();
|
||||
long processed = ((Number) progress.get("acceptedRecipientCnt")).longValue()
|
||||
+ ((Number) progress.get("failedRecipientCnt")).longValue()
|
||||
+ ((Number) progress.get("unknownRecipientCnt")).longValue() - inFlight;
|
||||
progress.put("inFlightRecipientCnt", inFlight);
|
||||
progress.put("processedRecipientCnt", processed);
|
||||
if (progress.get("preparedRecipientCnt") instanceof Number targets) {
|
||||
progress.put("remainingRecipientCnt", Math.max(0, targets.longValue() - processed));
|
||||
}
|
||||
job.setProgressJson(progress);
|
||||
job.setModDt(DateTimeUtil.nowKstLocalDateTime());
|
||||
}
|
||||
|
||||
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||
public void applyFixedChunk(String key, Long projectId, int chunkNo, String operation,
|
||||
List<String> requested, java.util.function.Consumer<List<String>> changes, Runnable checkCancelled) {
|
||||
if (chunkNo < 1 || operation == null || operation.isBlank() || operation.length() > 32
|
||||
|| requested.isEmpty() || requested.stream().anyMatch(java.util.Objects::isNull)
|
||||
|| new HashSet<>(requested).size() != requested.size()) {
|
||||
throw new IllegalArgumentException("Invalid fixed SID chunk");
|
||||
}
|
||||
checkCancelled.run();
|
||||
TListAsyncJobEntity job = lock(key);
|
||||
requireRunning(job);
|
||||
if (!java.util.Objects.equals(projectId, job.getPrjctIdx())
|
||||
|| chunkNo != job.getCompletedChunkCnt() + 1) {
|
||||
throw new IllegalArgumentException("Invalid project or chunk sequence");
|
||||
}
|
||||
var existing = lists.findForUpdate(projectId, requested).stream()
|
||||
.map(row -> row.getId().getSid()).collect(java.util.stream.Collectors.toSet());
|
||||
List<String> present = requested.stream().filter(existing::contains).toList();
|
||||
List<String> missing = requested.stream().filter(sid -> !existing.contains(sid)).toList();
|
||||
checkCancelled.run();
|
||||
if (!present.isEmpty()) {
|
||||
changes.accept(present);
|
||||
}
|
||||
checkCancelled.run();
|
||||
// Business services may clear the persistence context after bulk DML.
|
||||
job = lock(key);
|
||||
requireRunning(job);
|
||||
if (!missing.isEmpty()) {
|
||||
var detail = new java.util.LinkedHashMap<String, Object>(
|
||||
job.getErrorDetailJson() == null ? Map.of() : job.getErrorDetailJson());
|
||||
var groups = new java.util.ArrayList<Object>();
|
||||
if (detail.get("sidFailureGroups") instanceof List<?> previous) {
|
||||
groups.addAll(previous);
|
||||
}
|
||||
groups.add(Map.of("chunkNo", chunkNo, "code", "LIST_NOT_FOUND", "sids", missing));
|
||||
detail.put("sidFailureGroups", groups);
|
||||
detail.put("failedSidCnt", Math.addExact(failedSidCount(job), missing.size()));
|
||||
job.setErrorDetailJson(detail);
|
||||
}
|
||||
var chunk = new TListAsyncJobChunkEntity();
|
||||
chunk.setId(new TListAsyncJobChunkId(key, chunkNo));
|
||||
chunk.setOperationType(operation);
|
||||
chunk.setCompletedSidsJson(present);
|
||||
chunk.setCompletedSidCnt(present.size());
|
||||
chunk.setRecordedDt(DateTimeUtil.nowKstLocalDateTime());
|
||||
chunks.saveAndFlush(chunk);
|
||||
job.setCompletedSidCnt(Math.addExact(job.getCompletedSidCnt(), present.size()));
|
||||
job.setCompletedChunkCnt(job.getCompletedChunkCnt() + 1);
|
||||
job.setModDt(chunk.getRecordedDt());
|
||||
}
|
||||
|
||||
private void requireRunning(TListAsyncJobEntity job) {
|
||||
if (job.getJobStatus() == ListAsyncJobStatus.CANCEL_REQUESTED) {
|
||||
throw new CancellationException();
|
||||
}
|
||||
if (job.getJobStatus() != ListAsyncJobStatus.RUNNING) {
|
||||
throw new IllegalStateException("Job is not running");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,258 @@
|
||||
package com.kri.contact.server.service.async;
|
||||
|
||||
import static com.kri.contact.server.common.util.SidUtil.requireValidFilterConditions;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.kri.common.feature.datetime.DateTimeUtil;
|
||||
import com.kri.common.feature.excel.ExcelMapper;
|
||||
import com.kri.common.feature.web.BusinessException;
|
||||
import com.kri.common.feature.web.RequestUserContext;
|
||||
import com.kri.contact.server.common.consts.ContactConstant;
|
||||
import com.kri.contact.server.common.enums.ListAsyncJobType;
|
||||
import com.kri.contact.server.common.enums.ListResetMode;
|
||||
import com.kri.contact.server.common.enums.ListUploadMode;
|
||||
import com.kri.contact.server.dto.excel.ListUploadErrorDto;
|
||||
import com.kri.contact.server.dto.list.ListFilterDto;
|
||||
import com.kri.contact.server.dto.list.ListResetRequestDto;
|
||||
import com.kri.contact.server.dto.list.ListUploadRequestDto;
|
||||
import com.kri.contact.server.dto.list.async.ListAsyncJobAcceptedDto;
|
||||
import com.kri.contact.server.dto.list.async.ListAsyncResultFileDto;
|
||||
import com.kri.contact.server.dto.systemlog.UploadContentDto;
|
||||
import com.kri.contact.server.service.ListResetService;
|
||||
import com.kri.contact.server.service.ListSearchService;
|
||||
import com.kri.contact.server.service.ListUploadService;
|
||||
import com.kri.contact.server.service.common.SystemEventLogService;
|
||||
import com.kri.contact.server.service.file.ListAsyncResultFileService;
|
||||
import com.kri.contact.server.service.file.ListFileService;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class ListAsyncMaintenanceService {
|
||||
private final ListAsyncJobAccess access;
|
||||
private final ListAsyncJobExecutor executor;
|
||||
private final ListSearchService search;
|
||||
private final ListResetService resets;
|
||||
private final ListUploadService uploads;
|
||||
private final ListFileService sources;
|
||||
private final ListAsyncResultFileService results;
|
||||
private final SystemEventLogService audit;
|
||||
private final ObjectMapper mapper;
|
||||
|
||||
public ListAsyncJobAcceptedDto reset(Long projectId, ListResetRequestDto request, RequestUserContext user) {
|
||||
access.requireProject(projectId, user);
|
||||
if (request == null || request.getResetMode() == null || request.getListFilter() == null) {
|
||||
throw BusinessException.badRequest("INVALID_LIST_RESET", "초기화 범위와 필터가 필요합니다.");
|
||||
}
|
||||
var saved = mapper.convertValue(request, ListResetRequestDto.class);
|
||||
requireValidFilterConditions(saved.getListFilter().getSidSet());
|
||||
Map<String, Object> snapshot = mapper.convertValue(saved, new TypeReference<>() {});
|
||||
String key = executor.submit(projectId, user.getUserId(), ListAsyncJobType.LIST_RESET, snapshot, null,
|
||||
context -> {
|
||||
access.requireProject(projectId, user);
|
||||
context.progress("FREEZING_SIDS", null, Map.of());
|
||||
List<String> frozen = List.copyOf(new LinkedHashSet<>(search.findTargetSids(projectId, saved.getListFilter())));
|
||||
context.progress("PROCESSING", (long) frozen.size(), Map.of());
|
||||
String logId = context.job().getListResetLogId();
|
||||
int number = 0;
|
||||
for (int offset = 0; offset < frozen.size(); offset += ContactConstant.INSERT_CHUNK_SIZE) {
|
||||
context.checkCancelled();
|
||||
access.requireProject(projectId, user);
|
||||
var chunk = List.copyOf(frozen.subList(offset, Math.min(frozen.size(), offset + ContactConstant.INSERT_CHUNK_SIZE)));
|
||||
context.applyFixedChunk(projectId, ++number, "LIST_RESET", chunk, present -> {
|
||||
resets.resetAsyncChunk(projectId, new LinkedHashSet<>(present), saved.getResetMode(), logId, user.getUserId());
|
||||
var auditRequest = new ListResetRequestDto();
|
||||
auditRequest.setResetMode(saved.getResetMode());
|
||||
var filter = ListFilterDto.onlyOneSid(present.get(0));
|
||||
filter.setSidSet(new LinkedHashSet<>(present));
|
||||
auditRequest.setListFilter(filter);
|
||||
audit.writeResetList(user.getUserId(), DateTimeUtil.nowKstLocalDateTime(), user.getClientIp(), projectId, auditRequest);
|
||||
});
|
||||
}
|
||||
context.complete(() -> access.requireProject(projectId, user));
|
||||
}, job -> job.setListResetLogId(resets.createResetLog(projectId, saved.getResetMode(), 0,
|
||||
user.getUserId(), job.getRegDt())));
|
||||
return new ListAsyncJobAcceptedDto(key);
|
||||
}
|
||||
|
||||
public ListAsyncJobAcceptedDto upload(Long projectId, MultipartFile file, ListUploadRequestDto request,
|
||||
RequestUserContext user) {
|
||||
access.requireProject(projectId, user);
|
||||
if (request == null || request.getUploadMode() == null) {
|
||||
throw BusinessException.badRequest("ASYNC_UPLOAD_MODE_REQUIRED", "업로드 모드가 필요합니다.");
|
||||
}
|
||||
var saved = mapper.convertValue(request, ListUploadRequestDto.class);
|
||||
Map<String, Object> snapshot = mapper.convertValue(saved, new TypeReference<>() {});
|
||||
Path source = sources.saveAsyncListFile(file, projectId);
|
||||
var content = UploadContentDto.from(file);
|
||||
snapshot.put("source", mapper.convertValue(content, new TypeReference<Map<String, Object>>() {}));
|
||||
var initialized = new java.util.concurrent.atomic.AtomicBoolean();
|
||||
try {
|
||||
String key = executor.submit(projectId, user.getUserId(), ListAsyncJobType.LIST_UPLOAD, snapshot,
|
||||
source.getFileName().toString(), context -> upload(projectId, source, saved, content, user, context), job -> {
|
||||
sources.deleteOnRegistrationRollback(projectId, source.getFileName().toString());
|
||||
job.setListUploadLogId(uploads.createPendingLog(projectId, source, saved, user.getUserId(),
|
||||
job.getRegDt()).getListUploadLogId());
|
||||
job.setListResetLogId(resets.createResetLog(projectId,
|
||||
saved.getUploadMode() == ListUploadMode.RESET_AND_INSERT ? ListResetMode.ALL : ListResetMode.LIST_UPLOAD, 0,
|
||||
user.getUserId(), job.getRegDt()));
|
||||
initialized.set(true);
|
||||
});
|
||||
return new ListAsyncJobAcceptedDto(key);
|
||||
} catch (RuntimeException failure) {
|
||||
// A registered/rejected job retains its original. Without registration there is no source owner.
|
||||
if (!initialized.get()) {
|
||||
try {
|
||||
sources.deleteListFileIfExists(projectId, source.getFileName().toString());
|
||||
} catch (RuntimeException cleanupFailure) {
|
||||
failure.addSuppressed(cleanupFailure);
|
||||
}
|
||||
}
|
||||
throw failure;
|
||||
}
|
||||
}
|
||||
|
||||
private void upload(Long projectId, Path source, ListUploadRequestDto request, UploadContentDto content, RequestUserContext user,
|
||||
ListAsyncJobExecutor.JobContext context) {
|
||||
access.requireProject(projectId, user);
|
||||
context.progress("VALIDATING", null, Map.of());
|
||||
var job = context.job();
|
||||
var prepared = uploads.prepareAsync(projectId, source, request, job.getListUploadLogId(), context::checkCancelled);
|
||||
context.checkCancelled();
|
||||
if (!prepared.errors().isEmpty()) {
|
||||
context.progress("VALIDATION_FAILED", null, Map.of("validationRowCnt", prepared.totalCount(),
|
||||
"validationErrorCnt", prepared.errors().size()));
|
||||
boolean errorFileReady = publishErrors(prepared.errors(), context);
|
||||
context.reject("LIST_UPLOAD_VALIDATION_FAILED", "업로드 파일 검증에 실패했습니다.",
|
||||
Map.of("errorFileReady", errorFileReady, "validationErrors",
|
||||
mapper.convertValue(prepared.errors(), new TypeReference<List<Map<String, Object>>>() {})));
|
||||
return;
|
||||
}
|
||||
context.progress("PROCESSING", (long) prepared.sids().size(), Map.of("validationRowCnt", prepared.totalCount()));
|
||||
var number = new AtomicInteger();
|
||||
boolean fullReset = request.getUploadMode() == ListUploadMode.RESET_AND_INSERT;
|
||||
if (fullReset && !clearBeforeInsert(projectId, prepared.sids().size(), job.getListResetLogId(), user, context, number)) return;
|
||||
var attempted = new AtomicInteger();
|
||||
var failed = new AtomicInteger();
|
||||
var insertedAny = new java.util.concurrent.atomic.AtomicBoolean();
|
||||
var insertedCount = new AtomicInteger();
|
||||
var seen = new LinkedHashSet<String>();
|
||||
try {
|
||||
uploads.readUploadChunks(projectId, source, prepared.headers(), prepared.companies(), job.getListUploadLogId(),
|
||||
user.getUserId(), job.getStartedDt(), context::checkCancelled, chunk -> {
|
||||
context.checkCancelled();
|
||||
access.requireProject(projectId, user);
|
||||
var sids = chunk.sids();
|
||||
if (!prepared.sids().containsAll(sids) || sids.stream().anyMatch(sid -> !seen.add(sid))) {
|
||||
throw new IllegalStateException("Validated upload SIDs changed");
|
||||
}
|
||||
int attempt = attempted.incrementAndGet();
|
||||
int chunkNo = number.get() + 1;
|
||||
try {
|
||||
context.applyChunk(chunkNo, "LIST_UPLOAD", sids, () -> {
|
||||
if (fullReset) uploads.insertAsyncChunk(projectId, chunk, prepared, user.getUserId(), job.getStartedDt(), !insertedAny.get());
|
||||
else uploads.replaceAsyncChunk(projectId, chunk, prepared, job.getListResetLogId(), user.getUserId(), job.getStartedDt(), chunkNo == 1);
|
||||
audit.writeUploadList(user.getUserId(), DateTimeUtil.nowKstLocalDateTime(), user.getClientIp(), projectId, content);
|
||||
});
|
||||
number.incrementAndGet();
|
||||
insertedAny.set(true);
|
||||
insertedCount.addAndGet(sids.size());
|
||||
} catch (RuntimeException failure) {
|
||||
context.checkCancelled();
|
||||
if (failure instanceof java.util.concurrent.CancellationException || !fullReset) throw failure;
|
||||
context.rangeFailure("INSERTING", attempt, sids);
|
||||
failed.incrementAndGet();
|
||||
}
|
||||
if (fullReset) {
|
||||
var metrics = new java.util.LinkedHashMap<String,Object>(context.job().getProgressJson() == null ? Map.of() : context.job().getProgressJson());
|
||||
metrics.put("insertCompletedSidCnt", insertedCount.get());
|
||||
metrics.put("insertFailedRangeCnt", failed.get());
|
||||
context.progress("INSERTING", (long) prepared.sids().size(), metrics);
|
||||
}
|
||||
});
|
||||
} catch (RuntimeException failure) {
|
||||
context.checkCancelled();
|
||||
throw failure;
|
||||
}
|
||||
if (!seen.equals(prepared.sids())) {
|
||||
throw new IllegalStateException("Validated upload SID count changed");
|
||||
}
|
||||
var finalProgress = new java.util.LinkedHashMap<String,Object>(context.job().getProgressJson() == null ? Map.of() : context.job().getProgressJson());
|
||||
finalProgress.put("dataApplied", true);
|
||||
finalProgress.put("postProcessingComplete", false);
|
||||
context.progress("POST_PROCESSING", (long) seen.size(), finalProgress);
|
||||
context.complete(() -> {
|
||||
access.requireProject(projectId, user);
|
||||
if (failed.get() == 0) uploads.finishAsync(projectId, job.getListUploadLogId(), prepared, user.getUserId(), job.getStartedDt());
|
||||
else uploads.finishPartialAsync(projectId);
|
||||
});
|
||||
}
|
||||
|
||||
private boolean clearBeforeInsert(Long projectId, int insertTotal, String resetLogId, RequestUserContext user,
|
||||
ListAsyncJobExecutor.JobContext context, AtomicInteger committed) {
|
||||
var old = List.copyOf(search.findTargetSids(projectId, null));
|
||||
int failed = 0;
|
||||
int deleted = 0;
|
||||
for (int offset = 0, attempt = 1; offset < old.size(); offset += ContactConstant.INSERT_CHUNK_SIZE, attempt++) {
|
||||
context.checkCancelled();
|
||||
access.requireProject(projectId, user);
|
||||
var sids = List.copyOf(old.subList(offset, Math.min(old.size(), offset + ContactConstant.INSERT_CHUNK_SIZE)));
|
||||
context.progress("RESETTING_ALL", (long) insertTotal, Map.of("deleteTotalSidCnt", old.size(), "deleteCompletedSidCnt", deleted,
|
||||
"deleteFailedRangeCnt", failed, "insertTotalSidCnt", insertTotal));
|
||||
try {
|
||||
context.applyPreparationChunk(committed.get() + 1, "RESET_ALL", sids,
|
||||
() -> resets.resetAsyncChunk(projectId, new LinkedHashSet<>(sids), ListResetMode.ALL, resetLogId, user.getUserId()));
|
||||
committed.incrementAndGet();
|
||||
deleted += sids.size();
|
||||
} catch (RuntimeException failure) {
|
||||
context.checkCancelled();
|
||||
if (failure instanceof java.util.concurrent.CancellationException) throw failure;
|
||||
context.rangeFailure("RESETTING_ALL", attempt, sids);
|
||||
failed++;
|
||||
}
|
||||
}
|
||||
context.progress("RESET_CHECK", (long) insertTotal, Map.of("deleteTotalSidCnt", old.size(), "deleteCompletedSidCnt", deleted,
|
||||
"deleteFailedRangeCnt", failed, "insertTotalSidCnt", insertTotal));
|
||||
if (failed > 0 || !search.findTargetSids(projectId, null).isEmpty()) {
|
||||
var detail = context.job().getErrorDetailJson();
|
||||
context.reject("LIST_RESET_INCOMPLETE", "전체 초기화를 완료하지 못하여 삽입을 시작하지 않았습니다.", detail == null ? Map.of() : detail);
|
||||
return false;
|
||||
}
|
||||
context.progress("INSERTING", (long) insertTotal, Map.of("deleteTotalSidCnt", old.size(), "deleteCompletedSidCnt", deleted,
|
||||
"deleteFailedRangeCnt", 0, "insertTotalSidCnt", insertTotal));
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean publishErrors(List<ListUploadErrorDto> errors, ListAsyncJobExecutor.JobContext context) {
|
||||
ListAsyncResultFileDto file = null;
|
||||
try {
|
||||
file = results.create("ERROR", "upload_errors.xlsx", output -> {
|
||||
context.checkCancelled();
|
||||
try (var workbook = ExcelMapper.writeToWorkbook(errors, ListUploadErrorDto.class)) {
|
||||
workbook.write(output);
|
||||
}
|
||||
context.checkCancelled();
|
||||
});
|
||||
context.resultFile(file);
|
||||
return true;
|
||||
} catch (IOException failure) {
|
||||
context.checkCancelled();
|
||||
return false;
|
||||
} catch (RuntimeException failure) {
|
||||
if (file != null) {
|
||||
try { results.discard(file.fileRef()); } catch (IOException cleanupFailure) { failure.addSuppressed(cleanupFailure); }
|
||||
}
|
||||
context.checkCancelled();
|
||||
throw failure;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,214 @@
|
||||
package com.kri.contact.server.service.async;
|
||||
|
||||
import static com.kri.contact.server.common.util.SidUtil.requireValidFilterConditions;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.kri.common.feature.http.ExternalApiResponseException;
|
||||
import com.kri.common.feature.web.BusinessException;
|
||||
import com.kri.contact.server.common.enums.ChannelType;
|
||||
import com.kri.contact.server.common.enums.ListAsyncJobType;
|
||||
import com.kri.contact.server.common.external.notihub.dto.NotiHubBulkSendResponseDto;
|
||||
import com.kri.contact.server.common.external.notihub.dto.NotiHubDestInfoDto;
|
||||
import com.kri.contact.server.dto.list.ListFilterDto;
|
||||
import com.kri.contact.server.dto.list.async.ListAsyncJobAcceptedDto;
|
||||
import com.kri.contact.server.dto.message.MessageSendBulkRequestDto;
|
||||
import com.kri.common.feature.web.RequestUserContext;
|
||||
import com.kri.contact.server.service.ListSearchService;
|
||||
import com.kri.contact.server.service.MessageSendService;
|
||||
import com.kri.contact.server.service.file.ListAsyncMessageSpool;
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CancellationException;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class ListAsyncMessageService {
|
||||
private final ListAsyncJobAccess access;
|
||||
private final ListAsyncJobExecutor executor;
|
||||
private final ListSearchService search;
|
||||
private final MessageSendService messages;
|
||||
private final ListAsyncMessageSpool spool;
|
||||
private final ObjectMapper mapper;
|
||||
|
||||
public ListAsyncJobAcceptedDto submit(Long projectId, ChannelType channel, MessageSendBulkRequestDto request,
|
||||
RequestUserContext user) {
|
||||
access.requireProject(projectId, user);
|
||||
if (channel == null || request == null || request.getTmplatIdx() == null || request.getResve() == null
|
||||
|| request.getDestHeaderIds() == null || request.getDestHeaderIds().isEmpty()
|
||||
|| request.getDestHeaderIds().stream().anyMatch(Objects::isNull)
|
||||
|| Boolean.TRUE.equals(request.getResve()) && request.getSendDt() == null) {
|
||||
throw BusinessException.badRequest("INVALID_MESSAGE_REQUEST", "템플릿·수신처·예약 조건을 확인해 주세요.");
|
||||
}
|
||||
var saved = mapper.convertValue(request, MessageSendBulkRequestDto.class);
|
||||
requireValidFilterConditions(saved.getListFilter() == null ? null : saved.getListFilter().getSidSet());
|
||||
var type = switch (channel) { case EMAIL -> ListAsyncJobType.EMAIL_BULK; case SMS -> ListAsyncJobType.SMS_BULK; case KKO_AT -> ListAsyncJobType.KKO_BULK; };
|
||||
Map<String,Object> snapshot = mapper.convertValue(saved, new TypeReference<>() {});
|
||||
return new ListAsyncJobAcceptedDto(executor.submit(projectId, user.getUserId(), type, snapshot, null,
|
||||
context -> run(projectId, channel, saved, user, context)));
|
||||
}
|
||||
|
||||
private void run(Long projectId, ChannelType channel, MessageSendBulkRequestDto request,
|
||||
RequestUserContext user, ListAsyncJobExecutor.JobContext context) {
|
||||
access.requireProject(projectId, user);
|
||||
context.progress("FREEZING_SIDS", null, Map.of());
|
||||
var frozen = List.copyOf(search.findTargetSids(projectId, request.getListFilter()));
|
||||
context.progress("PREPARING_MESSAGES", null, Map.of("targetSidCnt", frozen.size()));
|
||||
var messageContext = messages.prepareAsync(projectId, request);
|
||||
var failures = new ArrayList<Map<String,Object>>();
|
||||
var failedSids = new HashSet<String>();
|
||||
var keys = new HashSet<String>();
|
||||
int duplicates = 0;
|
||||
try (var prepared = spool.open(context.jobKey())) {
|
||||
var buffer = new ArrayList<NotiHubDestInfoDto>();
|
||||
for (int offset = 0; offset < frozen.size(); offset += 1000) {
|
||||
context.checkCancelled();
|
||||
access.requireProject(projectId, user);
|
||||
var sids = List.copyOf(frozen.subList(offset, Math.min(frozen.size(), offset + 1000)));
|
||||
var filter = ListFilterDto.onlyOneSid(sids.get(0));
|
||||
filter.setSidSet(new LinkedHashSet<>(sids));
|
||||
List<ListSearchService.DownloadListRow> rows;
|
||||
try { rows = search.findDownloadListRows(projectId, filter, null); }
|
||||
catch (RuntimeException failure) {
|
||||
context.checkCancelled();
|
||||
failures.add(Map.of("code", "TARGET_READ_FAILED", "sids", sids));
|
||||
failedSids.addAll(sids);
|
||||
continue;
|
||||
}
|
||||
var found = new HashSet<String>();
|
||||
for (var row : rows) {
|
||||
context.checkCancelled();
|
||||
if (!filter.getSidSet().contains(row.sid()) || !found.add(row.sid())) throw new IllegalStateException("Unexpected message target");
|
||||
List<NotiHubDestInfoDto> candidates;
|
||||
try { candidates = messages.prepareAsyncRow(row, request, messageContext, channel); }
|
||||
catch (RuntimeException failure) {
|
||||
context.checkCancelled();
|
||||
failures.add(Map.of("code", "ROW_PREPARATION_FAILED", "sids", List.of(row.sid())));
|
||||
failedSids.add(row.sid());
|
||||
continue;
|
||||
}
|
||||
if (candidates.isEmpty()) {
|
||||
failures.add(Map.of("code", "NO_DESTINATION", "sids", List.of(row.sid())));
|
||||
failedSids.add(row.sid());
|
||||
}
|
||||
for (var destination : candidates) {
|
||||
if (channel == ChannelType.EMAIL && com.kri.common.feature.util.StringUtil.normalizeEmail(destination.getDestEml()) == null) {
|
||||
failures.add(Map.of("code", "INVALID_DESTINATION", "sids", List.of(row.sid())));
|
||||
failedSids.add(row.sid());
|
||||
continue;
|
||||
}
|
||||
if (!messages.retainAsyncDestination(destination, channel, keys, Boolean.TRUE.equals(request.getDedupe()))) { duplicates++; continue; }
|
||||
buffer.add(destination);
|
||||
if (buffer.size() == 1000) { prepared.append(buffer); buffer.clear(); }
|
||||
}
|
||||
}
|
||||
var missing = sids.stream().filter(sid -> !found.contains(sid)).toList();
|
||||
if (!missing.isEmpty()) { failures.add(Map.of("code", "LIST_NOT_FOUND", "sids", missing)); failedSids.addAll(missing); }
|
||||
}
|
||||
if (!buffer.isEmpty()) prepared.append(buffer);
|
||||
context.messagePreparation(failures);
|
||||
var progress = new LinkedHashMap<String,Object>();
|
||||
progress.put("targetSidCnt", frozen.size());
|
||||
progress.put("preparedRecipientCnt", prepared.targets());
|
||||
progress.put("preparationFailedSidCnt", failedSids.size());
|
||||
progress.put("duplicateSkippedRecipientCnt", duplicates);
|
||||
for (String metric : List.of("acceptedRecipientCnt", "failedRecipientCnt", "unknownRecipientCnt",
|
||||
"attemptedRecipientCnt", "processedRecipientCnt", "inFlightRecipientCnt")) progress.put(metric, 0L);
|
||||
progress.put("remainingRecipientCnt", (long) prepared.targets());
|
||||
progress.put("recordedBatchCnt", 0);
|
||||
context.progress("PREPARED_MESSAGES", null, progress);
|
||||
if (prepared.targets() == 0) {
|
||||
prepared.close();
|
||||
finishCycle(context);
|
||||
context.reject("NO_DESTINATION", "발송할 수신처가 없습니다.", context.job().getErrorDetailJson());
|
||||
return;
|
||||
}
|
||||
String bulkKey;
|
||||
context.checkCancelled();
|
||||
access.requireProject(projectId, user);
|
||||
try { bulkKey = messages.createBulkSendKey(request, user, messageContext, prepared.targets(), channel); }
|
||||
catch (RuntimeException failure) {
|
||||
// No recipient POST was attempted; do not retry bulk creation on an ambiguous response.
|
||||
for (int number = 1; number <= prepared.batches(); number++) {
|
||||
context.checkCancelled();
|
||||
var batch = prepared.read(number);
|
||||
context.messageBatch(number, outcome(batch, "", "BULK_CREATION_FAILED", 0, batch.size(), 0, true, false));
|
||||
}
|
||||
prepared.close();
|
||||
finishCycle(context);
|
||||
context.reject("MESSAGE_SETUP_FAILED", "발송 작업 생성에 실패하여 수신처 요청을 전달하지 못했습니다.", context.job().getErrorDetailJson());
|
||||
return;
|
||||
}
|
||||
if (bulkKey == null || bulkKey.isBlank()) throw new IllegalStateException("Missing bulk key");
|
||||
progress.put("bulkSendKey", bulkKey);
|
||||
context.progress("DISPATCHING", null, progress);
|
||||
long failed = failedSids.size();
|
||||
for (int number = 1; number <= prepared.batches(); number++) {
|
||||
context.checkCancelled();
|
||||
access.requireProject(projectId, user);
|
||||
var batch = prepared.read(number);
|
||||
// The intent is committed before the external side effect. A crash leaves an UNKNOWN batch.
|
||||
context.messageBatch(number, outcome(batch, bulkKey, "UNKNOWN", 0, 0, batch.size(), false, true));
|
||||
try { context.checkCancelled(); }
|
||||
catch (CancellationException cancelled) {
|
||||
context.messageBatch(number, outcome(batch, bulkKey, "CANCELLED_BEFORE_SEND", 0, 0, 0, true, false));
|
||||
throw cancelled;
|
||||
}
|
||||
Map<String,Object> result;
|
||||
try {
|
||||
var response = messages.sendBulkChunk(bulkKey, batch, channel);
|
||||
result = response(batch, bulkKey, response);
|
||||
} catch (RuntimeException failure) {
|
||||
// Until a rejection contract is available, non-2xx responses are conservatively UNKNOWN.
|
||||
result = outcome(batch, bulkKey, "UNKNOWN", 0, 0, batch.size(), true, true);
|
||||
result.put("errorCode", "UPSTREAM_REQUEST_FAILED");
|
||||
if (failure instanceof ExternalApiResponseException upstream) result.put("httpStatus", upstream.getStatusCode());
|
||||
}
|
||||
// Persist the observed outcome even if cancellation arrived during the HTTP call.
|
||||
context.messageBatch(number, result);
|
||||
failed += ((Number) result.get("failedRecipientCnt")).longValue() + ((Number) result.get("unknownRecipientCnt")).longValue();
|
||||
}
|
||||
prepared.close();
|
||||
finishCycle(context);
|
||||
if (failed > 0) context.reject("MESSAGE_PARTIAL_FAILURE", "전체 대상 처리를 마쳤으며 실패 또는 접수 불명 대상이 있습니다.", context.job().getErrorDetailJson());
|
||||
else context.complete(() -> {});
|
||||
} catch (IOException failure) {
|
||||
throw BusinessException.internalError("MESSAGE_PREPARATION_FAILED", "발송 준비 파일 처리에 실패했습니다.");
|
||||
}
|
||||
}
|
||||
|
||||
private void finishCycle(ListAsyncJobExecutor.JobContext context) {
|
||||
var values = new LinkedHashMap<String,Object>(context.job().getProgressJson());
|
||||
values.put("cycleComplete", true);
|
||||
context.progress("COMPLETED", null, values);
|
||||
}
|
||||
|
||||
private Map<String,Object> response(List<NotiHubDestInfoDto> batch, String key, NotiHubBulkSendResponseDto value) {
|
||||
if (value == null || value.getRequestCount() == null || value.getAcceptCount() == null || value.getFailCount() == null
|
||||
|| value.getRequestCount() != batch.size() || value.getAcceptCount() < 0 || value.getFailCount() < 0
|
||||
|| (long) value.getAcceptCount() + value.getFailCount() > batch.size()
|
||||
|| value.getBulkSendKey() != null && !key.equals(value.getBulkSendKey())) {
|
||||
return outcome(batch, key, "INVALID_RESPONSE", 0, 0, batch.size(), true, true);
|
||||
}
|
||||
int unknown = batch.size() - value.getAcceptCount() - value.getFailCount();
|
||||
return outcome(batch, key, unknown > 0 ? "UNKNOWN" : value.getFailCount() > 0 ? value.getAcceptCount() > 0 ? "PARTIAL" : "REJECTED" : "ACCEPTED",
|
||||
value.getAcceptCount(), value.getFailCount(), unknown, true, true);
|
||||
}
|
||||
|
||||
private Map<String,Object> outcome(List<NotiHubDestInfoDto> batch, String key, String status,
|
||||
int accepted, int failed, int unknown, boolean completed, boolean attempted) {
|
||||
var values = new LinkedHashMap<String,Object>();
|
||||
values.put("bulkSendKey", key);
|
||||
values.put("status", status);
|
||||
values.put("sids", batch.stream().map(NotiHubDestInfoDto::getSid).distinct().toList());
|
||||
values.put("targetRecipientCnt", batch.size());
|
||||
values.put("acceptedRecipientCnt", accepted);
|
||||
values.put("failedRecipientCnt", failed);
|
||||
values.put("unknownRecipientCnt", unknown);
|
||||
values.put("attemptedRecipientCnt", attempted ? batch.size() : 0);
|
||||
values.put("completed", completed);
|
||||
return values;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
package com.kri.contact.server.service.async;
|
||||
|
||||
import static com.kri.contact.server.common.util.SidUtil.requireValidFilterConditions;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.kri.common.feature.datetime.DateTimeUtil;
|
||||
import com.kri.common.feature.web.BusinessException;
|
||||
import com.kri.common.feature.web.RequestUserContext;
|
||||
import com.kri.contact.server.common.consts.ContactConstant;
|
||||
import com.kri.contact.server.common.enums.ListAsyncJobType;
|
||||
import com.kri.contact.server.common.enums.ListAsyncAssignOperation;
|
||||
import com.kri.contact.server.dto.list.ListAssignRequestDto;
|
||||
import com.kri.contact.server.dto.list.ListAssignResetRequestDto;
|
||||
import com.kri.contact.server.dto.list.ListFilterDto;
|
||||
import com.kri.contact.server.dto.list.ListUpdateRequestDto;
|
||||
import com.kri.contact.server.dto.list.async.ListAsyncJobAcceptedDto;
|
||||
import com.kri.contact.server.service.AssignService;
|
||||
import com.kri.contact.server.service.ListSearchService;
|
||||
import com.kri.contact.server.service.ListUpdateService;
|
||||
import com.kri.contact.server.service.common.SystemEventLogService;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class ListAsyncMutationService {
|
||||
private final ListAsyncJobAccess access;
|
||||
private final ListAsyncJobExecutor executor;
|
||||
private final ListSearchService search;
|
||||
private final ListUpdateService updates;
|
||||
private final AssignService assignments;
|
||||
private final SystemEventLogService audit;
|
||||
private final ObjectMapper mapper;
|
||||
|
||||
public ListAsyncJobAcceptedDto update(Long projectId, ListUpdateRequestDto request,
|
||||
RequestUserContext user) {
|
||||
access.requireProject(projectId, user);
|
||||
var saved = mapper.convertValue(request, ListUpdateRequestDto.class);
|
||||
updates.validateAsyncUpdate(projectId, saved, user.getUserRole());
|
||||
return submit(projectId, ListAsyncJobType.LIST_UPDATE, saved, saved.getListFilter(), user, sids -> {
|
||||
var chunk = mapper.convertValue(saved, ListUpdateRequestDto.class);
|
||||
chunk.setListFilter(fixedFilter(sids));
|
||||
var now = DateTimeUtil.nowKstLocalDateTime();
|
||||
updates.updateList(projectId, chunk, user.getUserRole(), user.getUserId(), now);
|
||||
audit.writeUpdateList(user.getUserId(), now, user.getClientIp(), projectId, chunk);
|
||||
});
|
||||
}
|
||||
|
||||
public ListAsyncJobAcceptedDto assign(Long projectId, String assignId, ListAssignRequestDto request,
|
||||
RequestUserContext user) {
|
||||
access.requireProject(projectId, user);
|
||||
if (assignId == null || assignId.isBlank() || request == null || request.getAssignType() == null
|
||||
|| request.getTransferResve() == null) {
|
||||
throw BusinessException.badRequest("INVALID_LIST_ASSIGN", "배정 ID, 타입, 예약 이관 여부가 필요합니다.");
|
||||
}
|
||||
var saved = mapper.convertValue(request, ListAssignRequestDto.class);
|
||||
return submit(projectId, ListAsyncJobType.LIST_ASSIGN,
|
||||
Map.of("operation", ListAsyncAssignOperation.ASSIGN, "assignId", assignId, "request", saved),
|
||||
saved.getListFilter(), user, sids -> {
|
||||
var chunk = mapper.convertValue(saved, ListAssignRequestDto.class);
|
||||
chunk.setListFilter(fixedFilter(sids));
|
||||
var now = DateTimeUtil.nowKstLocalDateTime();
|
||||
assignments.upsertListAssign(projectId, assignId, chunk, user.getUserId(), now);
|
||||
audit.writeAssignList(user.getUserId(), now, user.getClientIp(), projectId, chunk);
|
||||
});
|
||||
}
|
||||
|
||||
public ListAsyncJobAcceptedDto resetAssignments(Long projectId, ListAssignResetRequestDto request,
|
||||
RequestUserContext user) {
|
||||
access.requireProject(projectId, user);
|
||||
if (request == null || request.getAssignType() == null) {
|
||||
throw BusinessException.badRequest("INVALID_LIST_ASSIGN_TYPE", "배정 타입이 필요합니다.");
|
||||
}
|
||||
var saved = mapper.convertValue(request, ListAssignResetRequestDto.class);
|
||||
return submit(projectId, ListAsyncJobType.LIST_ASSIGN,
|
||||
Map.of("operation", ListAsyncAssignOperation.RESET, "request", saved), saved.getListFilter(), user, sids -> {
|
||||
var chunk = mapper.convertValue(saved, ListAssignResetRequestDto.class);
|
||||
chunk.setListFilter(fixedFilter(sids));
|
||||
assignments.resetListAssign(projectId, chunk);
|
||||
audit.writeAssignListReset(user.getUserId(), DateTimeUtil.nowKstLocalDateTime(),
|
||||
user.getClientIp(), projectId, chunk);
|
||||
});
|
||||
}
|
||||
|
||||
private ListAsyncJobAcceptedDto submit(Long projectId, ListAsyncJobType type, Object request,
|
||||
ListFilterDto filter, RequestUserContext user, Consumer<List<String>> changes) {
|
||||
requireValidFilterConditions(filter == null ? null : filter.getSidSet());
|
||||
Map<String, Object> snapshot = mapper.convertValue(request, new TypeReference<>() {});
|
||||
String operation = type == ListAsyncJobType.LIST_ASSIGN ? snapshot.get("operation").toString() : type.name();
|
||||
String key = executor.submit(projectId, user.getUserId(), type, snapshot, null, context -> {
|
||||
access.requireProject(projectId, user);
|
||||
context.checkCancelled();
|
||||
context.progress("FREEZING_SIDS", null, Map.of());
|
||||
List<String> frozen = List.copyOf(new LinkedHashSet<>(search.findTargetSids(projectId, filter)));
|
||||
context.progress("PROCESSING", (long) frozen.size(), Map.of());
|
||||
int chunkNo = 0;
|
||||
for (int offset = 0; offset < frozen.size(); offset += ContactConstant.INSERT_CHUNK_SIZE) {
|
||||
context.checkCancelled();
|
||||
access.requireProject(projectId, user);
|
||||
var chunk = List.copyOf(frozen.subList(offset,
|
||||
Math.min(frozen.size(), offset + ContactConstant.INSERT_CHUNK_SIZE)));
|
||||
context.applyFixedChunk(projectId, ++chunkNo, operation, chunk, changes);
|
||||
}
|
||||
});
|
||||
return new ListAsyncJobAcceptedDto(key);
|
||||
}
|
||||
|
||||
private static ListFilterDto fixedFilter(List<String> sids) {
|
||||
var filter = ListFilterDto.onlyOneSid(sids.get(0));
|
||||
filter.setSidSet(new LinkedHashSet<>(sids));
|
||||
return filter;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
package com.kri.contact.server.service.async;
|
||||
|
||||
import com.kri.common.feature.datetime.DateTimeUtil;
|
||||
import com.kri.common.feature.web.BusinessException;
|
||||
import com.kri.contact.server.common.enums.ListAsyncJobStatus;
|
||||
import com.kri.contact.server.common.enums.ListAsyncJobType;
|
||||
import com.kri.contact.server.common.enums.ListAsyncAssignOperation;
|
||||
import com.kri.contact.server.dto.list.async.ListAsyncAvailabilityDto;
|
||||
import com.kri.contact.server.dto.list.async.ListAsyncJobActivity;
|
||||
import com.kri.contact.server.persistence.repository.UserRepository;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class ProjectJobPermits {
|
||||
private final ListAsyncJobStore store;
|
||||
private final UserRepository users;
|
||||
// Do not remove project slots: removal could create two concurrent admission locks.
|
||||
private final ConcurrentHashMap<Long, Slot> slots = new ConcurrentHashMap<>();
|
||||
|
||||
public Permit acquire(Long projectId, String user, ListAsyncJobType type, String key,
|
||||
ListAsyncAssignOperation operation) {
|
||||
Slot slot = slots.computeIfAbsent(projectId, ignored -> new Slot());
|
||||
synchronized (slot) {
|
||||
var view = availabilityLocked(projectId, user, type, slot);
|
||||
if (!view.allowed()) {
|
||||
throw BusinessException.conflict(view.reasonCode(), conflictMessage(view));
|
||||
}
|
||||
slot.held.put(key, new ListAsyncJobActivity(key, type, ListAsyncJobStatus.QUEUED,
|
||||
user, DateTimeUtil.nowKstLocalDateTime(), operation == null ? null : operation.name()));
|
||||
return new Permit(slot, key);
|
||||
}
|
||||
}
|
||||
|
||||
public ListAsyncAvailabilityDto availability(Long projectId, String user, ListAsyncJobType type) {
|
||||
Slot slot = slots.computeIfAbsent(projectId, ignored -> new Slot());
|
||||
synchronized (slot) {
|
||||
return availabilityLocked(projectId, user, type, slot);
|
||||
}
|
||||
}
|
||||
|
||||
private ListAsyncAvailabilityDto availabilityLocked(Long projectId, String user,
|
||||
ListAsyncJobType type, Slot slot) {
|
||||
Map<String, ListAsyncJobActivity> active = new HashMap<>(slot.held);
|
||||
// Include held terminal rows: final cleanup still blocks after DB completion.
|
||||
var persisted = store.activity(projectId, List.copyOf(slot.held.keySet()));
|
||||
var persistedKeys = persisted.stream().map(ListAsyncJobActivity::jobKey)
|
||||
.collect(java.util.stream.Collectors.toSet());
|
||||
persisted.forEach(job -> active.put(job.jobKey(), job));
|
||||
var blockers = active.values().stream()
|
||||
.filter(job -> type.conflictsWith(job.jobType(), user.equals(job.requestUser())))
|
||||
.sorted(Comparator.comparing(ListAsyncJobActivity::regDt)
|
||||
.thenComparing(ListAsyncJobActivity::jobKey)).toList();
|
||||
boolean groupConflict = blockers.stream().anyMatch(job -> type.isImportant() || job.jobType().isImportant());
|
||||
var shown = blockers.stream().limit(20).toList();
|
||||
Map<String, String> names = new HashMap<>();
|
||||
if (!shown.isEmpty()) {
|
||||
users.findNamesByUserIds(shown.stream().map(ListAsyncJobActivity::requestUser).distinct().toList())
|
||||
.forEach(name -> {
|
||||
if (name.getUserNm() != null && !name.getUserNm().isBlank()) {
|
||||
names.put(name.getUserId(), name.getUserNm());
|
||||
}
|
||||
});
|
||||
}
|
||||
var views = shown.stream().map(job -> {
|
||||
boolean mine = user.equals(job.requestUser());
|
||||
var operation = job.jobType() == ListAsyncJobType.LIST_ASSIGN && job.operation() != null
|
||||
? ListAsyncAssignOperation.valueOf(job.operation()) : null;
|
||||
return new ListAsyncAvailabilityDto.Blocker(job.jobType(), job.jobStatus(),
|
||||
job.jobStatus().isTerminal() ? "FINALIZING" : "ACTIVE", job.requestUser(),
|
||||
names.get(job.requestUser()), mine, mine && persistedKeys.contains(job.jobKey()) ? job.jobKey() : null,
|
||||
job.regDt(), operation);
|
||||
}).toList();
|
||||
return new ListAsyncAvailabilityDto(type, blockers.isEmpty(), blockers.isEmpty() ? null
|
||||
: groupConflict ? "LIST_JOB_PROJECT_CONFLICT" : "LIST_JOB_USER_BUSY", blockers.size(), views);
|
||||
}
|
||||
|
||||
private String conflictMessage(ListAsyncAvailabilityDto view) {
|
||||
var first = view.blockers().get(0);
|
||||
String actor = first.requestUserName() == null ? first.requestUserId()
|
||||
: first.requestUserName() + "(" + first.requestUserId() + ")";
|
||||
String phase = "FINALIZING".equals(first.blockingPhase()) ? "마무리 중"
|
||||
: switch (first.jobStatus()) {
|
||||
case QUEUED -> "대기 중";
|
||||
case CANCEL_REQUESTED -> "취소 처리 중";
|
||||
default -> "진행 중";
|
||||
};
|
||||
String others = view.blockerCount() > 1 ? " 외 " + (view.blockerCount() - 1) + "개 작업" : "";
|
||||
String jobName = first.operation() == null ? first.jobType().displayName() : first.operation().displayName();
|
||||
return actor + "님의 " + jobName + "(" + phase + ")" + others
|
||||
+ "으로 " + view.requestedJobType().displayName()
|
||||
+ " 작업을 시작할 수 없습니다. 작업 종료 후 다시 시도해 주세요.";
|
||||
}
|
||||
|
||||
private static final class Slot {
|
||||
private final Map<String, ListAsyncJobActivity> held = new HashMap<>();
|
||||
}
|
||||
|
||||
public static final class Permit implements AutoCloseable {
|
||||
private final Slot slot;
|
||||
private final String key;
|
||||
|
||||
private Permit(Slot slot, String key) {
|
||||
this.slot = slot;
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
synchronized (slot) {
|
||||
slot.held.remove(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package com.kri.contact.server.service.file;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.kri.contact.server.common.external.notihub.dto.NotiHubDestInfoDto;
|
||||
import com.kri.contact.server.config.ListAsyncProperties;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.*;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class ListAsyncMessageSpool {
|
||||
private final ListAsyncProperties properties;
|
||||
private final ObjectMapper mapper;
|
||||
|
||||
public record Destination(String sid, String destEml, String destTel, String destNm, Map<String,String> msgParam) {
|
||||
static Destination from(NotiHubDestInfoDto value) {
|
||||
return new Destination(value.getSid(), value.getDestEml(), value.getDestTel(), value.getDestNm(), value.getMsgParam());
|
||||
}
|
||||
NotiHubDestInfoDto toRequest() {
|
||||
return NotiHubDestInfoDto.builder().sid(sid).destEml(destEml).destTel(destTel).destNm(destNm).msgParam(msgParam).build();
|
||||
}
|
||||
}
|
||||
|
||||
private Path root() throws IOException {
|
||||
Path path = Path.of(properties.getResultBaseDir()).toAbsolutePath().normalize().resolve("message-preparation");
|
||||
Files.createDirectories(path);
|
||||
if (!path.toRealPath().equals(path)) throw new IOException("Message preparation storage must not contain symbolic links");
|
||||
return path;
|
||||
}
|
||||
|
||||
public Prepared open(String jobKey) throws IOException {
|
||||
if (jobKey == null || !jobKey.matches("[0-9a-f]{32}")) throw new IllegalArgumentException("Invalid job key");
|
||||
Path directory = root().resolve(jobKey);
|
||||
Files.createDirectory(directory);
|
||||
return new Prepared(directory);
|
||||
}
|
||||
|
||||
public void cleanupInterrupted() throws IOException {
|
||||
try (var directories = Files.newDirectoryStream(root())) {
|
||||
for (Path directory : directories) {
|
||||
if (directory.getFileName().toString().matches("[0-9a-f]{32}") && Files.isDirectory(directory, LinkOption.NOFOLLOW_LINKS)) {
|
||||
cleanup(directory);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void cleanup(Path directory) throws IOException {
|
||||
if (!directory.toRealPath().getParent().equals(root())) throw new IOException("Invalid preparation directory");
|
||||
try (var entries = Files.newDirectoryStream(directory)) {
|
||||
for (Path entry : entries) {
|
||||
if (entry.getFileName().toString().matches("[1-9][0-9]*\\.json(?:\\.part)?") && !Files.isSymbolicLink(entry)) {
|
||||
Files.delete(entry);
|
||||
}
|
||||
}
|
||||
}
|
||||
Files.delete(directory);
|
||||
}
|
||||
|
||||
public final class Prepared implements AutoCloseable {
|
||||
private final Path directory;
|
||||
private int batches;
|
||||
private int targets;
|
||||
private boolean closed;
|
||||
private Prepared(Path directory) { this.directory = directory; }
|
||||
public int batches() { return batches; }
|
||||
public int targets() { return targets; }
|
||||
public void append(List<NotiHubDestInfoDto> destinations) throws IOException {
|
||||
if (closed) throw new IllegalStateException("Preparation is closed");
|
||||
if (destinations.isEmpty() || destinations.size() > 1000) throw new IllegalArgumentException("Invalid message batch");
|
||||
int number = batches + 1;
|
||||
Path temporary = directory.resolve(number + ".json.part");
|
||||
mapper.writeValue(temporary.toFile(), destinations.stream().map(Destination::from).toList());
|
||||
Files.move(temporary, directory.resolve(number + ".json"), StandardCopyOption.ATOMIC_MOVE);
|
||||
batches = number;
|
||||
targets = Math.addExact(targets, destinations.size());
|
||||
}
|
||||
public List<NotiHubDestInfoDto> read(int number) throws IOException {
|
||||
if (closed) throw new IllegalStateException("Preparation is closed");
|
||||
if (number < 1 || number > batches) throw new IllegalArgumentException("Invalid batch number");
|
||||
List<Destination> values = mapper.readValue(directory.resolve(number + ".json").toFile(), new TypeReference<>() {});
|
||||
return values.stream().map(Destination::toRequest).toList();
|
||||
}
|
||||
@Override public void close() throws IOException {
|
||||
if (!closed) { cleanup(directory); closed = true; }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,211 @@
|
||||
package com.kri.contact.server.service.file;
|
||||
|
||||
import com.kri.common.feature.web.BusinessException;
|
||||
import com.kri.contact.server.config.ListAsyncProperties;
|
||||
import com.kri.contact.server.dto.list.async.ListAsyncResultFileDto;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.LinkOption;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.nio.file.attribute.BasicFileAttributeView;
|
||||
import java.nio.file.attribute.FileTime;
|
||||
import java.time.Clock;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class ListAsyncResultFileService {
|
||||
private static final Duration RETENTION = Duration.ofHours(1);
|
||||
private final Path root;
|
||||
private final Clock clock;
|
||||
// 파일별 항목을 영구 누적하지 않으면서 생성·전송·삭제를 같은 잠금으로 보호한다.
|
||||
private final ReentrantLock[] locks = new ReentrantLock[64];
|
||||
|
||||
@Autowired
|
||||
public ListAsyncResultFileService(ListAsyncProperties properties) {
|
||||
this(Path.of(properties.getResultBaseDir()), Clock.systemUTC());
|
||||
}
|
||||
|
||||
ListAsyncResultFileService(Path root, Clock clock) {
|
||||
this.root = root.toAbsolutePath().normalize();
|
||||
this.clock = clock;
|
||||
for (int i = 0; i < locks.length; i++) {
|
||||
locks[i] = new ReentrantLock();
|
||||
}
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
public interface FileWriter {
|
||||
void write(OutputStream output) throws IOException;
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
public interface FileReader {
|
||||
void read(Path file) throws IOException;
|
||||
}
|
||||
|
||||
public ListAsyncResultFileDto create(String type, String name, FileWriter writer) throws IOException {
|
||||
if (type == null || type.isBlank() || name == null || name.isBlank()
|
||||
|| name.length() > 255 || name.indexOf('\r') >= 0 || name.indexOf('\n') >= 0) {
|
||||
throw new IllegalArgumentException("Invalid result file metadata");
|
||||
}
|
||||
Files.createDirectories(root);
|
||||
if (Files.isSymbolicLink(root) || !root.toRealPath().equals(root)) {
|
||||
throw new IOException("Result storage must not contain symbolic links");
|
||||
}
|
||||
String ref = UUID.randomUUID().toString().replace("-", "") + ".result";
|
||||
Path target = resolve(ref);
|
||||
Path temporary = root.resolve(ref + ".part");
|
||||
ReentrantLock lock = lockFor(target);
|
||||
lock.lock();
|
||||
try {
|
||||
try (OutputStream output = Files.newOutputStream(temporary, java.nio.file.StandardOpenOption.CREATE_NEW)) {
|
||||
writer.write(output);
|
||||
}
|
||||
Files.move(temporary, target, StandardCopyOption.ATOMIC_MOVE);
|
||||
Instant completed = clock.instant();
|
||||
BasicFileAttributeView attributes = Files.getFileAttributeView(
|
||||
target, BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
|
||||
attributes.setTimes(null, null, FileTime.from(completed));
|
||||
Instant recorded = attributes.readAttributes().creationTime().toInstant();
|
||||
if (Duration.between(recorded, completed).abs().compareTo(Duration.ofSeconds(2)) > 0) {
|
||||
throw new IOException("Filesystem cannot record result file completion as creation time");
|
||||
}
|
||||
return new ListAsyncResultFileDto(type, ref, name, Files.size(target), true);
|
||||
} catch (IOException | RuntimeException failure) {
|
||||
try {
|
||||
Files.deleteIfExists(temporary);
|
||||
Files.deleteIfExists(target);
|
||||
} catch (IOException cleanupFailure) {
|
||||
failure.addSuppressed(cleanupFailure);
|
||||
}
|
||||
throw failure;
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean exists(String ref) {
|
||||
Path file = resolve(ref);
|
||||
return safeRegularFile(file);
|
||||
}
|
||||
|
||||
public void discard(String ref) throws IOException {
|
||||
Path file = resolve(ref);
|
||||
ReentrantLock lock = lockFor(file);
|
||||
lock.lock();
|
||||
try {
|
||||
if (safeRegularFile(file)) {
|
||||
Files.deleteIfExists(file);
|
||||
}
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
// reader가 정상 반환한 경우에만 삭제한다. 전송 실패 시 파일을 남긴다.
|
||||
public void download(String ref, FileReader reader) throws IOException {
|
||||
Path file = resolve(ref);
|
||||
ReentrantLock lock = lockFor(file);
|
||||
lock.lock();
|
||||
try {
|
||||
if (!safeRegularFile(file)) {
|
||||
throw BusinessException.notFound("LIST_RESULT_FILE_GONE", "결과 파일이 삭제되었거나 존재하지 않습니다.");
|
||||
}
|
||||
reader.read(file);
|
||||
try {
|
||||
Files.deleteIfExists(file);
|
||||
} catch (IOException failure) {
|
||||
// 전송 성공 후 삭제 실패를 다운로드 실패로 바꾸지 않는다. 스케줄러가 다시 정리한다.
|
||||
log.warn("List result deletion deferred to cleanup");
|
||||
}
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
// DB를 조회하거나 변경하지 않는다.
|
||||
public int cleanup() throws IOException {
|
||||
if (!Files.exists(root, LinkOption.NOFOLLOW_LINKS)) {
|
||||
return 0;
|
||||
}
|
||||
if (Files.isSymbolicLink(root) || !root.toRealPath().equals(root)) {
|
||||
throw new IOException("Unsafe result storage");
|
||||
}
|
||||
int removed = 0;
|
||||
Instant cutoff = clock.instant().minus(RETENTION);
|
||||
try (var paths = Files.list(root)) {
|
||||
var iterator = paths.filter(path -> path.getFileName().toString()
|
||||
.matches("[0-9a-f]{32}\\.result")).iterator();
|
||||
while (iterator.hasNext()) {
|
||||
Path file = iterator.next();
|
||||
ReentrantLock lock = lockFor(file);
|
||||
if (!lock.tryLock()) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
if (safeRegularFile(file) && !Files.readAttributes(file,
|
||||
java.nio.file.attribute.BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS)
|
||||
.creationTime().toInstant().isAfter(cutoff)) {
|
||||
Files.deleteIfExists(file);
|
||||
removed++;
|
||||
}
|
||||
} catch (IOException failure) {
|
||||
log.warn("List result cleanup will retry a file");
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
}
|
||||
return removed;
|
||||
}
|
||||
|
||||
// 새 작업 접수 전에만 호출한다. 이전 실행이 남긴 미완성 파일만 정리한다.
|
||||
public void cleanupIncomplete() throws IOException {
|
||||
if (!Files.exists(root, LinkOption.NOFOLLOW_LINKS)) {
|
||||
return;
|
||||
}
|
||||
if (Files.isSymbolicLink(root) || !root.toRealPath().equals(root)) {
|
||||
throw new IOException("Unsafe result storage");
|
||||
}
|
||||
try (var paths = Files.list(root)) {
|
||||
var iterator = paths.filter(path -> path.getFileName().toString()
|
||||
.matches("[0-9a-f]{32}\\.result\\.part")).iterator();
|
||||
while (iterator.hasNext()) {
|
||||
Path file = iterator.next();
|
||||
if (safeRegularFile(file)) {
|
||||
Files.deleteIfExists(file);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Path resolve(String ref) {
|
||||
if (ref == null || !ref.matches("[0-9a-f]{32}\\.result")) {
|
||||
throw BusinessException.badRequest("INVALID_RESULT_FILE", "잘못된 결과 파일 참조입니다.");
|
||||
}
|
||||
return root.resolve(ref).normalize();
|
||||
}
|
||||
|
||||
private boolean safeRegularFile(Path file) {
|
||||
try {
|
||||
return !Files.isSymbolicLink(root) && root.toRealPath().equals(root)
|
||||
&& Files.isRegularFile(file, LinkOption.NOFOLLOW_LINKS)
|
||||
&& file.toRealPath().getParent().equals(root);
|
||||
} catch (IOException failure) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private ReentrantLock lockFor(Path file) {
|
||||
return locks[Math.floorMod(file.hashCode(), locks.length)];
|
||||
}
|
||||
}
|
||||
@@ -8,11 +8,15 @@ import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.support.TransactionSynchronization;
|
||||
import org.springframework.transaction.support.TransactionSynchronizationManager;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class ListFileService extends AbstractFileService {
|
||||
|
||||
private final FileStorageProperties properties;
|
||||
@@ -35,6 +39,62 @@ public class ListFileService extends AbstractFileService {
|
||||
}
|
||||
}
|
||||
|
||||
public Path saveAsyncListFile(MultipartFile file, Long projectId) {
|
||||
if (file == null || file.isEmpty()) {
|
||||
throw BusinessException.badRequest("EMPTY_FILE", "업로드할 파일이 없습니다.");
|
||||
}
|
||||
String extension = extractExtension(file.getOriginalFilename());
|
||||
if (!extension.equalsIgnoreCase(".xlsx")) {
|
||||
throw BusinessException.badRequest("INVALID_LIST_FILE", "xlsx 파일만 업로드할 수 있습니다.");
|
||||
}
|
||||
Path dir = getBaseDir().resolve(projectId.toString());
|
||||
String name = java.util.UUID.randomUUID().toString().replace("-", "") + ".xlsx";
|
||||
try {
|
||||
Files.createDirectories(dir);
|
||||
if (!dir.toRealPath().startsWith(getBaseDir().toRealPath())) {
|
||||
throw BusinessException.badRequest("WRONG_FILE_PATH", "잘못된 파일 경로입니다.");
|
||||
}
|
||||
saveFile(dir, file, name);
|
||||
return dir.resolve(name);
|
||||
} catch (IOException | RuntimeException failure) {
|
||||
try {
|
||||
Files.deleteIfExists(dir.resolve(name));
|
||||
} catch (IOException cleanupFailure) {
|
||||
failure.addSuppressed(cleanupFailure);
|
||||
}
|
||||
throw BusinessException.internalError("FAIL_LIST_UPLOAD", "원본 파일 저장에 실패했습니다.");
|
||||
}
|
||||
}
|
||||
|
||||
public void copyListFile(Long projectId, String fileName, java.io.OutputStream output) throws IOException {
|
||||
Path dir = getBaseDir().resolve(projectId.toString()).normalize();
|
||||
Path file = dir.resolve(fileName).normalize();
|
||||
if (!file.startsWith(dir) || !Files.isRegularFile(file)
|
||||
|| !dir.toRealPath().startsWith(getBaseDir().toRealPath())
|
||||
|| !file.toRealPath().startsWith(dir.toRealPath())) {
|
||||
throw BusinessException.notFound("LIST_UPLOAD_FILE_NOT_FOUND", "원본 파일을 찾을 수 없습니다.");
|
||||
}
|
||||
Files.copy(file, output);
|
||||
}
|
||||
|
||||
public void deleteOnRegistrationRollback(Long projectId, String fileName) {
|
||||
if (!TransactionSynchronizationManager.isActualTransactionActive()) {
|
||||
throw new IllegalStateException("Source registration requires an active transaction");
|
||||
}
|
||||
TransactionSynchronizationManager.registerSynchronization(
|
||||
new TransactionSynchronization() {
|
||||
@Override public void afterCompletion(int status) {
|
||||
if (status == STATUS_ROLLED_BACK) {
|
||||
try {
|
||||
deleteListFileIfExists(projectId, fileName);
|
||||
} catch (RuntimeException failure) {
|
||||
log.warn("Unregistered list source cleanup failed: type={}", failure.getClass().getSimpleName());
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Path renameFailedListFile(Long prjctIdx, Path sourceFilePath) {
|
||||
String failPrefix = "uploadfail_";
|
||||
Path dir = getBaseDir().resolve(prjctIdx.toString()).toAbsolutePath().normalize();
|
||||
|
||||
@@ -3,15 +3,20 @@ package com.kri.contact.server.service.file;
|
||||
import com.kri.common.feature.web.BusinessException;
|
||||
import com.kri.contact.server.config.FileStorageProperties;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.support.TransactionSynchronization;
|
||||
import org.springframework.transaction.support.TransactionSynchronizationManager;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class ListItemFileService extends AbstractFileService {
|
||||
|
||||
private final FileStorageProperties properties;
|
||||
@@ -39,6 +44,18 @@ public class ListItemFileService extends AbstractFileService {
|
||||
}
|
||||
}
|
||||
|
||||
public void copyItemFile(String filePath, String fileName, OutputStream output) throws IOException {
|
||||
Path baseDir = getBaseDir();
|
||||
Path target = resolveItemFilePath(baseDir, filePath, fileName);
|
||||
if (!target.startsWith(baseDir)) {
|
||||
throw BusinessException.badRequest("WRONG_FILE_PATH", "잘못된 파일 경로입니다.");
|
||||
}
|
||||
if (!Files.isRegularFile(target) || !target.toRealPath().startsWith(baseDir.toRealPath())) {
|
||||
throw BusinessException.notFound("LIST_ITEM_FILE_NOT_FOUND", "리스트에 업로드 된 파일이 없습니다.");
|
||||
}
|
||||
Files.copy(target, output);
|
||||
}
|
||||
|
||||
public String saveItemFile(MultipartFile file, String filePath, String fileName) {
|
||||
Path baseDir = getBaseDir();
|
||||
Path dir = baseDir.resolve(filePath).toAbsolutePath().normalize();
|
||||
@@ -72,6 +89,25 @@ public class ListItemFileService extends AbstractFileService {
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteItemFileAfterCommit(String filePath, String fileName) {
|
||||
if (!TransactionSynchronizationManager.isActualTransactionActive()
|
||||
|| !TransactionSynchronizationManager.isSynchronizationActive()) {
|
||||
throw new IllegalStateException("File deletion requires an active transaction");
|
||||
}
|
||||
TransactionSynchronizationManager.registerSynchronization(
|
||||
new TransactionSynchronization() {
|
||||
@Override
|
||||
public void afterCommit() {
|
||||
try {
|
||||
deleteItemFile(filePath, fileName);
|
||||
} catch (RuntimeException failure) {
|
||||
// DB commit succeeded; retain the orphan for operational cleanup.
|
||||
log.warn("Committed list item file cleanup failed: type={}", failure.getClass().getSimpleName());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private Path resolveItemFilePath(Path baseDir, String filePath, String fileName) {
|
||||
String relativePath = filePath == null || filePath.isBlank() ? "" : filePath;
|
||||
return baseDir.resolve(relativePath).resolve(fileName).toAbsolutePath().normalize();
|
||||
|
||||
@@ -19,8 +19,8 @@ spring:
|
||||
|
||||
servlet:
|
||||
multipart:
|
||||
max-file-size: 20MB
|
||||
max-request-size: 20MB
|
||||
max-file-size: 100MB
|
||||
max-request-size: 100MB
|
||||
|
||||
logging:
|
||||
file:
|
||||
@@ -95,6 +95,9 @@ kri:
|
||||
cleanup-expire-months: 3
|
||||
cleanup-cron: "0 30 0 * * SUN"
|
||||
|
||||
list-async:
|
||||
cleanup-delay-ms: 60000
|
||||
|
||||
list-event-history:
|
||||
survey-lock-wait-timeout-seconds: 30
|
||||
|
||||
|
||||
Reference in New Issue
Block a user