Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/delete study #21

Merged
merged 6 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion pegase-env-local/sql_utils/init_project.sql
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,16 @@ values ('me00247', 1),
('me00247', 2),
('me00247', 3),
('no0099', 1),
('no0099', 2);
('no0099', 2);

insert into pegase_local_db_schema.trajectory (id, file_name, file_size, checksum, type, version, created_by, creation_date, last_modification_content_date)
values (3, 'areas_BP23_A_ref_v2', 6822, 'd73a71ca53c7952eb99ab46eec3aeb24fda4d109652f0f539581227124c25010', 'AREA', 1, 'zayd', '2024-07-22 15:13:56.860045', '2024-07-09 10:55:27.467000'),
(1, 'areas_BP23_A_ref', 7132, 'bf64818cc16aa62110184b7e889188ce7cf0ee6a8b0f04a7721209bbd64c4b46', 'AREA', 1, 'MOUAD', '2024-07-22 14:52:31.234005', '2024-07-22 12:38:14.614000'),
(2, 'links_BP23_A_ref', 12679, '55be2a4685154fa0a5c45e5bac70a637fbb15a354d58afd5ff56b2c61b5be082', 'LINK', 1, 'mouad', '2024-07-22 14:52:56.325083', '2024-05-28 16:19:51.429000');

insert into pegase_local_db_schema.scenario_trajectory (scenario_id, trajectory_id)
values (1, 1),
(1, 2),
(1, 3),
(2, 1),
(3, 2);
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,10 @@ public ResponseEntity<ProjectDto> findProjectById(@PathVariable Integer id) {
public void deleteProject(@PathVariable Integer id) {
projectService.deleteProjectById(id);
}

@Operation(summary = "Search projects by partial name for auto-completion")
@GetMapping("/autocomplete")
public ResponseEntity<List<ProjectDto>> searchProjectsByName(@RequestParam String partialName) {
return new ResponseEntity<>(projectService.searchProjectsByName(partialName), HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.rte_france.antares.datamanager_back.dto.StudyDTO;
import com.rte_france.antares.datamanager_back.service.StudyService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
Expand All @@ -10,10 +11,9 @@
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.rte_france.antares.datamanager_back.mapper.StudyMapper.toStudyPage;

Expand All @@ -35,14 +35,34 @@ public ResponseEntity<Page<StudyDTO>> searchStudies(
@RequestParam(value = "sortColumn", required = false) String sortColumn,
@RequestParam(value = "sortDirection", required = false) String sortDirection) {

Sort sorting = Sort.by(SORTING_CRITERION);
Sort sorting = Sort.by(Sort.Direction.DESC,SORTING_CRITERION);

if (!sortColumn.isEmpty() && !sortDirection.isEmpty()) {
if (sortColumn != null && !sortColumn.isEmpty() && !sortDirection.isEmpty()) {
Sort.Direction direction = Sort.Direction.fromString(sortDirection);
sorting = Sort.by(direction, sortColumn);
}
Pageable paging = PageRequest.of(page - 1, size, sorting);

return new ResponseEntity<>(toStudyPage(studyService.findStudiesByCriteria(search, projectId, paging)), HttpStatus.OK);
}


@Operation(summary = "Search keywords by partial name for auto-completion")
@GetMapping("/keywords/search")
public ResponseEntity<List<String>> searchKeywordsByPartialName(@RequestParam String partialName) {
return new ResponseEntity<>(studyService.searchKeywordsByPartialName(partialName), HttpStatus.OK);
}


@PostMapping
public ResponseEntity<StudyDTO> createStudy(@RequestBody StudyDTO studyDTO) {
StudyDTO createdStudy = studyService.createStudy(studyDTO);
return new ResponseEntity<>(createdStudy, HttpStatus.CREATED);
}

@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteStudyById(@PathVariable Integer id) {
studyService.deleteStudyById(id);
return ResponseEntity.noContent().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,7 @@ public class StudyDTO {

@JsonProperty("horizon")
String horizon;

@JsonProperty("trajectoryIds")
List<Integer> trajectoryIds;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@

import com.rte_france.antares.datamanager_back.dto.StudyDTO;
import com.rte_france.antares.datamanager_back.repository.model.StudyEntity;
import com.rte_france.antares.datamanager_back.repository.model.TrajectoryEntity;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.NoArgsConstructor;
import lombok.Value;
import org.springframework.data.domain.Page;

import java.util.Collections;

@Value
@Builder(toBuilder = true)
@NoArgsConstructor(access = AccessLevel.PRIVATE)
Expand All @@ -24,6 +27,7 @@ public static StudyDTO toStudyDTO(StudyEntity entity) {
.tags(entity.getTags())
.horizon(entity.getHorizon())
.status(entity.getStatus().name())
.trajectoryIds(entity.getTrajectories() != null ? entity.getTrajectories().stream().map(TrajectoryEntity::getId).toList() : Collections.emptyList())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,17 @@
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

import java.util.List;

public interface ProjectRepository extends JpaRepository<ProjectEntity, Integer> {

Page<ProjectEntity> findAll(Specification<ProjectEntity> spec, Pageable pageable);

Optional<ProjectEntity> findByName(String name);

List<ProjectEntity> findByNameContainingIgnoreCase(String partialName);


}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,22 @@
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface StudyRepository extends JpaRepository<StudyEntity, String> {
import java.util.List;

public interface StudyRepository extends JpaRepository<StudyEntity, Integer> {

Page<StudyEntity> findAll(Specification<StudyEntity> spec, Pageable pageable);

@Query("SELECT DISTINCT t FROM StudyEntity s JOIN s.tags t WHERE t IN :tags")
List<String> findExistingTags(@Param("tags") List<String> tags);

@Query("SELECT DISTINCT tag FROM StudyEntity s JOIN s.tags tag WHERE tag LIKE %:partialName%")
List<String> findKeywordsByPartialName(String partialName);


@Query("SELECT CASE WHEN COUNT(s) > 0 THEN true ELSE false END FROM StudyEntity s WHERE s.name = :name AND s.project.name = :projectName")
boolean existsByNameAndProjectName(@Param("name") String name, @Param("projectName") String projectName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import java.util.Optional;

@Repository
public interface TrajectoryRepository extends JpaRepository<TrajectoryEntity, String> {
public interface TrajectoryRepository extends JpaRepository<TrajectoryEntity, Integer> {

Optional<TrajectoryEntity> findTrajectoryEntityById(Integer id);

@ExecutionTime
Optional<TrajectoryEntity> findFirstByFileNameOrderByVersionDesc(String fileName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
@Table(name = "project")
public class ProjectEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "project_seq_gen")
@SequenceGenerator(name = "project_seq_gen", sequenceName = "project_sequence", allocationSize = 1)
@Column(name = "id", nullable = false)
private Integer id;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import jakarta.persistence.*;
import jakarta.validation.constraints.Size;
import lombok.*;
import org.hibernate.annotations.Cascade;

import java.time.LocalDateTime;
import java.util.LinkedHashSet;
Expand All @@ -18,7 +19,8 @@
@Table(name = "scenario")
public class StudyEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "study_seq_gen")
@SequenceGenerator(name = "study_seq_gen", sequenceName = "study_sequence", allocationSize = 1)
@Column(name = "id", nullable = false)
private Integer id;

Expand All @@ -42,6 +44,9 @@ public class StudyEntity {

private String horizon;

@OneToMany(mappedBy = "studyEntity", orphanRemoval = true)
private Set<StudyTrajectoryEntity> studyTrajectoryEntities = new LinkedHashSet<>();

@ManyToMany(mappedBy = "scenarioEntities")
private Set<TrajectoryEntity> trajectories = new LinkedHashSet<>();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.rte_france.antares.datamanager_back.service;

import com.rte_france.antares.datamanager_back.dto.ProjectDto;
import com.rte_france.antares.datamanager_back.repository.model.ProjectEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand All @@ -20,6 +21,8 @@ public interface ProjectService {

void deleteProjectById(Integer projectId);

List<ProjectDto> searchProjectsByName(String partialName);


}

Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package com.rte_france.antares.datamanager_back.service;

import com.rte_france.antares.datamanager_back.dto.StudyDTO;
import com.rte_france.antares.datamanager_back.repository.model.StudyEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;

public interface StudyService {


Page<StudyEntity> findStudiesByCriteria(String search,Integer idProject, Pageable pageable);

StudyDTO createStudy(StudyDTO studyDTO);

List<String> searchKeywordsByPartialName(String partialName);

void deleteStudyById(Integer id);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.rte_france.antares.datamanager_back.service.impl;

import com.rte_france.antares.datamanager_back.dto.ProjectDto;
import com.rte_france.antares.datamanager_back.exception.BadRequestException;
import com.rte_france.antares.datamanager_back.exception.ResourceNotFoundException;
import com.rte_france.antares.datamanager_back.mapper.ProjectMapper;
import com.rte_france.antares.datamanager_back.repository.PinnedProjectRepository;
import com.rte_france.antares.datamanager_back.repository.ProjectRepository;
import com.rte_france.antares.datamanager_back.repository.model.PinnedProjectEntity;
Expand All @@ -26,6 +28,7 @@
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


@Slf4j
Expand Down Expand Up @@ -67,6 +70,14 @@ public Page<ProjectEntity> findProjectsByCriteria(String search, Pageable paging
return projectRepository.findAll(paging);
}

@Override
public List<ProjectDto> searchProjectsByName(String partialName) {
List<ProjectEntity> projectEntities = projectRepository.findByNameContainingIgnoreCase(partialName);
return projectEntities.stream()
.map(ProjectMapper::toProjectDto)
.collect(Collectors.toList());
}

public static Specification<ProjectEntity> hasStudyName(String studyName) {
return (Root<ProjectEntity> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) -> {
Join<ProjectEntity, StudyEntity> studies = root.join("studies");
Expand Down
Loading
Loading