parent
dd3078a0b0
commit
c91b768aa3
@ -0,0 +1,87 @@
|
|||||||
|
package kr.re.etri.autoflow.controllers;
|
||||||
|
|
||||||
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
|
import io.swagger.v3.oas.annotations.Parameter;
|
||||||
|
import io.swagger.v3.oas.annotations.enums.ParameterIn;
|
||||||
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
|
import kr.re.etri.autoflow.entity.DataGroupEntity;
|
||||||
|
import kr.re.etri.autoflow.payload.request.BaseSearchRequest;
|
||||||
|
import kr.re.etri.autoflow.payload.request.ProjectRequest;
|
||||||
|
import kr.re.etri.autoflow.service.DataGroupService;
|
||||||
|
import kr.re.etri.autoflow.service.ProjectService;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springdoc.core.annotations.ParameterObject;
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Tag(name = "데이터 그룹", description = "Project CRUD 기능 제공")
|
||||||
|
@CrossOrigin(origins = "*", maxAge = 3600)
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/datagroup")
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class DataGroupController {
|
||||||
|
|
||||||
|
private final DataGroupService dataGroupService;
|
||||||
|
|
||||||
|
@Operation(summary = "전체 프로젝트 목록 조회")
|
||||||
|
@GetMapping
|
||||||
|
public ResponseEntity<List<DataGroupEntity>> getAllProjects() {
|
||||||
|
return ResponseEntity.ok(dataGroupService.findAll());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Operation(summary = "ID로 프로젝트 조회")
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
public ResponseEntity<DataGroupEntity> getProjectById(
|
||||||
|
@Parameter(description = "조회할 프로젝트 ID", required = true, in = ParameterIn.PATH)
|
||||||
|
@PathVariable("id") Long id) {
|
||||||
|
|
||||||
|
return dataGroupService.findById(id)
|
||||||
|
.map(ResponseEntity::ok)
|
||||||
|
.orElse(ResponseEntity.notFound().build());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Operation(summary = "검색 및 페이지네이션 프로젝트 목록 조회")
|
||||||
|
@GetMapping("/search")
|
||||||
|
public ResponseEntity<Page<DataGroupEntity>> searchProjects(
|
||||||
|
@ParameterObject @ModelAttribute BaseSearchRequest request) {
|
||||||
|
Page<DataGroupEntity> page = dataGroupService.search(request);
|
||||||
|
return ResponseEntity.ok(page);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Operation(summary = "프로젝트 생성")
|
||||||
|
@PostMapping
|
||||||
|
public ResponseEntity<?> createProject(@RequestBody DataGroupEntity project) {
|
||||||
|
try {
|
||||||
|
DataGroupEntity saved = dataGroupService.create(project);
|
||||||
|
return ResponseEntity.ok(saved);
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
return ResponseEntity.badRequest().body(e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Operation(summary = "프로젝트 수정")
|
||||||
|
@PutMapping("/{id}")
|
||||||
|
public ResponseEntity<DataGroupEntity> updateProject(
|
||||||
|
@Parameter(description = "수정할 프로젝트 ID", required = true, in = ParameterIn.PATH)
|
||||||
|
@PathVariable("id") Long id,
|
||||||
|
@RequestBody ProjectRequest dto) {
|
||||||
|
|
||||||
|
return dataGroupService.update(id, dto)
|
||||||
|
.map(ResponseEntity::ok)
|
||||||
|
.orElse(ResponseEntity.notFound().build());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Operation(summary = "프로젝트 삭제")
|
||||||
|
@DeleteMapping("/{id}")
|
||||||
|
public ResponseEntity<Void> deleteProject(
|
||||||
|
@Parameter(description = "삭제할 프로젝트 ID", required = true, in = ParameterIn.PATH)
|
||||||
|
@PathVariable("id") Long id) {
|
||||||
|
if (dataGroupService.delete(id)) {
|
||||||
|
return ResponseEntity.noContent().build();
|
||||||
|
}
|
||||||
|
return ResponseEntity.notFound().build();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,72 @@
|
|||||||
|
package kr.re.etri.autoflow.entity;
|
||||||
|
|
||||||
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
import lombok.*;
|
||||||
|
import org.hibernate.annotations.Comment;
|
||||||
|
import org.springframework.data.annotation.CreatedDate;
|
||||||
|
import org.springframework.data.annotation.LastModifiedDate;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Schema(description = "데이터그룹")
|
||||||
|
@Comment("데이터그룹")
|
||||||
|
@Entity
|
||||||
|
@Table(name = "tb_datagroup")
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
@Builder
|
||||||
|
public class DataGroupEntity {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
@Schema(description = "ID", example = "null")
|
||||||
|
@Comment("ID")
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
@Schema(description = "데이터셋 이름", example = "배터리 상태 데이터 셋")
|
||||||
|
@Comment("데이터셋 이름")
|
||||||
|
private String dsNm;
|
||||||
|
|
||||||
|
@Schema(description = "데이터셋 설명", example = "EV6 차량의 배터리 상태 모음")
|
||||||
|
@Comment("데이터셋 설명")
|
||||||
|
private String dsDesc;
|
||||||
|
|
||||||
|
@Schema(description = "삭제 여부", example = "N")
|
||||||
|
@Comment("삭제 여부")
|
||||||
|
private String delYn;
|
||||||
|
|
||||||
|
@CreatedDate
|
||||||
|
@Schema(description = "등록 일자")
|
||||||
|
@Comment("등록 일자")
|
||||||
|
private LocalDateTime regDate;
|
||||||
|
|
||||||
|
@Schema(description = "등록 유저 ID", example = "system")
|
||||||
|
@Comment("등록 유저 ID")
|
||||||
|
private String regUserId;
|
||||||
|
|
||||||
|
@Schema(description = "등록 유저 이름", example = "시스템")
|
||||||
|
@Comment("등록 유저 이름")
|
||||||
|
private String regUserNm;
|
||||||
|
|
||||||
|
@LastModifiedDate
|
||||||
|
@Schema(description = "수정 일자")
|
||||||
|
@Comment("수정 일자")
|
||||||
|
private LocalDateTime modDate;
|
||||||
|
|
||||||
|
@Schema(description = "수정 유저 ID", example = "system")
|
||||||
|
@Comment("수정 유저 ID")
|
||||||
|
private String modUserId;
|
||||||
|
|
||||||
|
@Schema(description = "수정 유저 이름", example = "시스템")
|
||||||
|
@Comment("수정 유저 이름")
|
||||||
|
private String modUserNm;
|
||||||
|
|
||||||
|
@OneToMany
|
||||||
|
@JoinColumn(name = "refId", referencedColumnName = "id", insertable = false, updatable = false)
|
||||||
|
private List<DatasetAttachmentEntity> files = new ArrayList<>();
|
||||||
|
}
|
||||||
@ -0,0 +1,40 @@
|
|||||||
|
package kr.re.etri.autoflow.payload.request;
|
||||||
|
|
||||||
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@Schema(description = "프로젝트 요청 정보")
|
||||||
|
public class DataGroupRequest {
|
||||||
|
|
||||||
|
@Schema(description = "데이터셋 이름", example = "배터리 상태 데이터 셋", required = true)
|
||||||
|
private String dsNm;
|
||||||
|
|
||||||
|
@Schema(description = "데이터셋 설명", example = "EV6 차량의 배터리 상태 모음")
|
||||||
|
private String dsDesc;
|
||||||
|
|
||||||
|
@Schema(description = "삭제 여부", example = "N")
|
||||||
|
private String delYn;
|
||||||
|
|
||||||
|
@Schema(description = "등록 유저 ID", example = "system", required = true)
|
||||||
|
private String regUserId;
|
||||||
|
|
||||||
|
@Schema(description = "등록 유저 이름", example = "시스템")
|
||||||
|
private String regUserNm;
|
||||||
|
|
||||||
|
@Schema(description = "수정 유저 ID", example = "system")
|
||||||
|
private String modUserId;
|
||||||
|
|
||||||
|
@Schema(description = "수정 유저 이름", example = "시스템")
|
||||||
|
private String modUserNm;
|
||||||
|
|
||||||
|
@Schema(description = "등록 일자 (생략 가능, 서버에서 세팅 가능)")
|
||||||
|
private LocalDateTime regDate;
|
||||||
|
|
||||||
|
@Schema(description = "수정 일자 (생략 가능, 서버에서 세팅 가능)")
|
||||||
|
private LocalDateTime modDate;
|
||||||
|
}
|
||||||
@ -0,0 +1,12 @@
|
|||||||
|
package kr.re.etri.autoflow.repository;
|
||||||
|
|
||||||
|
import kr.re.etri.autoflow.entity.DataGroupEntity;
|
||||||
|
import kr.re.etri.autoflow.entity.ProjectEntity;
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.Pageable;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||||
|
|
||||||
|
public interface DataGroupRepository extends JpaRepository<DataGroupEntity, Long>, JpaSpecificationExecutor<DataGroupEntity> {
|
||||||
|
//Page<DataGroupEntity> findBy(String keyword, Pageable pageable);
|
||||||
|
}
|
||||||
@ -0,0 +1,100 @@
|
|||||||
|
package kr.re.etri.autoflow.service;
|
||||||
|
|
||||||
|
import kr.re.etri.autoflow.entity.DataGroupEntity;
|
||||||
|
import kr.re.etri.autoflow.payload.request.BaseSearchRequest;
|
||||||
|
import kr.re.etri.autoflow.payload.request.ProjectRequest;
|
||||||
|
import kr.re.etri.autoflow.repository.DataGroupRepository;
|
||||||
|
import kr.re.etri.autoflow.repository.ProjectRepository;
|
||||||
|
import kr.re.etri.autoflow.repository.UserProjectMapRepository;
|
||||||
|
import kr.re.etri.autoflow.repository.UserRepository;
|
||||||
|
import kr.re.etri.autoflow.specification.DataGroupSpecification;
|
||||||
|
import kr.re.etri.autoflow.specification.ProjectSpecification;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.beans.BeanUtils;
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.PageRequest;
|
||||||
|
import org.springframework.data.domain.Pageable;
|
||||||
|
import org.springframework.data.domain.Sort;
|
||||||
|
import org.springframework.data.jpa.domain.Specification;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
import java.time.format.DateTimeParseException;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public class DataGroupService {
|
||||||
|
|
||||||
|
private final DataGroupRepository dataGroupRepository;
|
||||||
|
|
||||||
|
private final DataGroupSpecification dataGroupSpecification;
|
||||||
|
|
||||||
|
private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
||||||
|
|
||||||
|
public List<DataGroupEntity> findAll() {
|
||||||
|
return dataGroupRepository.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<DataGroupEntity> findById(Long id) {
|
||||||
|
return dataGroupRepository.findById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Page<DataGroupEntity> search(BaseSearchRequest request) {
|
||||||
|
int pageIndex = request.getPage() > 0 ? request.getPage() - 1 : 0;
|
||||||
|
|
||||||
|
Pageable pageable = PageRequest.of(
|
||||||
|
pageIndex,
|
||||||
|
request.getSize(),
|
||||||
|
Sort.by(Sort.Direction.fromString(request.getSortDirection()), request.getSortField())
|
||||||
|
);
|
||||||
|
|
||||||
|
LocalDate startDate = parseDate(request.getStartDate());
|
||||||
|
LocalDate endDate = parseDate(request.getEndDate());
|
||||||
|
|
||||||
|
Specification<DataGroupEntity> spec = dataGroupSpecification.searchByConditions(
|
||||||
|
request.getSearchType(),
|
||||||
|
request.getKeyword(),
|
||||||
|
startDate,
|
||||||
|
endDate
|
||||||
|
);
|
||||||
|
|
||||||
|
return dataGroupRepository.findAll(spec, pageable);
|
||||||
|
}
|
||||||
|
|
||||||
|
private LocalDate parseDate(String dateStr) {
|
||||||
|
if (dateStr == null || dateStr.isBlank()) return null;
|
||||||
|
try {
|
||||||
|
return LocalDate.parse(dateStr, formatter);
|
||||||
|
} catch (DateTimeParseException e) {
|
||||||
|
throw new IllegalArgumentException("날짜 형식이 잘못되었습니다. yyyy-MM-dd 형식이어야 합니다: " + dateStr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public DataGroupEntity create(DataGroupEntity project) {
|
||||||
|
return dataGroupRepository.save(project);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Optional<DataGroupEntity> update(Long id, ProjectRequest dto) {
|
||||||
|
return dataGroupRepository.findById(id)
|
||||||
|
.map(project -> {
|
||||||
|
BeanUtils.copyProperties(dto, project);
|
||||||
|
return dataGroupRepository.save(project);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public boolean delete(Long id) {
|
||||||
|
if (!dataGroupRepository.existsById(id)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
dataGroupRepository.deleteById(id);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,80 @@
|
|||||||
|
package kr.re.etri.autoflow.specification;
|
||||||
|
|
||||||
|
import jakarta.annotation.PostConstruct;
|
||||||
|
import jakarta.persistence.EntityManager;
|
||||||
|
import jakarta.persistence.criteria.Predicate;
|
||||||
|
import jakarta.persistence.metamodel.Attribute;
|
||||||
|
import jakarta.persistence.metamodel.EntityType;
|
||||||
|
import jakarta.persistence.metamodel.Metamodel;
|
||||||
|
import kr.re.etri.autoflow.entity.DataGroupEntity;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.data.jpa.domain.Specification;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
public class DataGroupSpecification {
|
||||||
|
|
||||||
|
private final EntityManager entityManager;
|
||||||
|
|
||||||
|
private Set<String> stringFields;
|
||||||
|
|
||||||
|
public DataGroupSpecification(EntityManager entityManager) {
|
||||||
|
this.entityManager = entityManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 스프링 빈 초기화 후 실행
|
||||||
|
@PostConstruct
|
||||||
|
public void init() {
|
||||||
|
Metamodel metamodel = entityManager.getMetamodel();
|
||||||
|
EntityType<DataGroupEntity> entityType = metamodel.entity(DataGroupEntity.class);
|
||||||
|
|
||||||
|
// 문자열 타입 필드명만 추출
|
||||||
|
stringFields = entityType.getAttributes().stream()
|
||||||
|
.filter(attr -> attr.getJavaType().equals(String.class))
|
||||||
|
.map(Attribute::getName)
|
||||||
|
.collect(Collectors.toSet());
|
||||||
|
|
||||||
|
log.info("DataGroupEntity string fields: {}", stringFields);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Specification<DataGroupEntity> searchByConditions(
|
||||||
|
String searchType, String keyword,
|
||||||
|
LocalDate startDate, LocalDate endDate) {
|
||||||
|
|
||||||
|
return (root, query, cb) -> {
|
||||||
|
Predicate predicate = cb.conjunction();
|
||||||
|
|
||||||
|
if (keyword != null && !keyword.isEmpty()) {
|
||||||
|
if (searchType == null || searchType.isEmpty() ||
|
||||||
|
"전체".equalsIgnoreCase(searchType) || "all".equalsIgnoreCase(searchType)) {
|
||||||
|
Predicate orPredicate = cb.disjunction();
|
||||||
|
for (String field : stringFields) {
|
||||||
|
orPredicate = cb.or(orPredicate,
|
||||||
|
cb.like(cb.lower(root.get(field)), "%" + keyword.toLowerCase() + "%"));
|
||||||
|
}
|
||||||
|
predicate = cb.and(predicate, orPredicate);
|
||||||
|
|
||||||
|
} else if (stringFields.contains(searchType)) {
|
||||||
|
predicate = cb.and(predicate,
|
||||||
|
cb.like(cb.lower(root.get(searchType)), "%" + keyword.toLowerCase() + "%"));
|
||||||
|
}
|
||||||
|
// 날짜 타입 필드 검색 시 무시
|
||||||
|
}
|
||||||
|
|
||||||
|
if (startDate != null) {
|
||||||
|
predicate = cb.and(predicate, cb.greaterThanOrEqualTo(root.get("regDate"), startDate.atStartOfDay()));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (endDate != null) {
|
||||||
|
predicate = cb.and(predicate, cb.lessThanOrEqualTo(root.get("regDate"), endDate.atTime(23, 59, 59)));
|
||||||
|
}
|
||||||
|
|
||||||
|
return predicate;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in new issue