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

Use migration schema for norm abbreviation #791

Merged
merged 21 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 2 additions & 2 deletions .talismanrc
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ fileignoreconfig:
- filename: backend/src/test/kotlin/unit/application/service/GetFileServiceTest.kt
checksum: 8133265c6f388df84a3e0f61050428e8628853e0ba80b432e4723750a34b781e
- filename: compose.yaml
checksum: 0cf11aa3c7711e07ad50cc83cad9b4c5a2081e7400d949aa84a43b1c5354058e
checksum: cd08d156c78274d39b8db8f42af9696da21b9f6815c09fbbededa937709ff945
- filename: doc/norms/backend-api.yaml
checksum: df4fa1f7e6d7023f9a7a98d1052c2b88c1785366962004b3165453edcd5bf4db
- filename: doc/structurizr/workspace.json
checksum: 541780133a0bf013ac75f6a3bc402c77981a273654c5648eabeb32ccb271a834
- filename: frontend/Dockerfile.prod
checksum: 3307752ddc7656606048f085ec2158b8a671844970944178fda705f64789d97a
checksum: 7947d7152ade77bee6bf112a3721442669b05f74ca0c8e70dc81d08bb4381b6c
- filename: frontend/FRONTEND_STYLEGUIDE.md
checksum: e6b8194eba2012f2a769c5a5d3577715a573b441a7b94e744c32b3149d0d3a93
- filename: frontend/src/main.ts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,14 @@ public NormAbbreviationController(NormAbbreviationService service) {
public Flux<NormAbbreviation> getAllNormAbbreviationsBySearchQuery(
@RequestParam(value = "q", required = false, defaultValue = "") String query,
@RequestParam(value = "sz", required = false) Integer size,
@RequestParam(value = "pg", required = false) Integer page) {

@RequestParam(value = "pg", required = false, defaultValue = "0") Integer page) {
return service.getNormAbbreviationBySearchQuery(query, size, page);
}

@GetMapping("/{uuid}")
@PreAuthorize("isAuthenticated()")
public Mono<NormAbbreviation> getNormAbbreviationController(@PathVariable("uuid") UUID uuid) {
return service.getNormAbbreviationById(uuid);
return Mono.just(service.getNormAbbreviationById(uuid));
}

@GetMapping("/search")
Expand All @@ -48,7 +47,6 @@ public Mono<List<NormAbbreviation>> getAllNormAbbreviationsByAwesomeSearchQuery(
@RequestParam(value = "q", required = false, defaultValue = "") String query,
@RequestParam(value = "sz", required = false) Integer size,
@RequestParam(value = "pg", required = false) Integer page) {

return service.getNormAbbreviationByAwesomeSearchQuery(query, size, page);
}

Expand All @@ -58,6 +56,7 @@ public Mono<ResponseEntity<String>> refreshMaterializedViews() {
return service
.refreshMaterializedViews()
.thenReturn(
ResponseEntity.ok("Refreshed the materialized view 'norm_abbreviation_search'"));
ResponseEntity.ok(
"Refreshed the materialized view 'norm_abbreviation_search_migration'"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,18 @@ public NormAbbreviationService(NormAbbreviationRepository repository) {
this.repository = repository;
}

public Mono<NormAbbreviation> getNormAbbreviationById(UUID uuid) {
public NormAbbreviation getNormAbbreviationById(UUID uuid) {
return repository.findById(uuid);
}

public Flux<NormAbbreviation> getNormAbbreviationBySearchQuery(
HPrinz marked this conversation as resolved.
Show resolved Hide resolved
String query, Integer size, Integer page) {

Integer pageOffset = null;
if (page != null && size != null) {
pageOffset = page * size;
}

return repository.findBySearchQuery(query, size, pageOffset);
return repository.findBySearchQuery(query, size, page);
}

public Mono<List<NormAbbreviation>> getNormAbbreviationByAwesomeSearchQuery(
String query, Integer size, Integer page) {
HPrinz marked this conversation as resolved.
Show resolved Hide resolved

Integer pageOffset = null;
if (page != null && size != null) {
pageOffset = page * size;
}

return repository.findByAwesomeSearchQuery(query, size, pageOffset);
return repository.findByAwesomeSearchQuery(query, size);
}

public Mono<Void> refreshMaterializedViews() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package de.bund.digitalservice.ris.caselaw.adapter.database.jpa;

import java.util.UUID;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface DatabaseDocumentCategoryRepository
extends JpaRepository<DocumentCategoryDTO, UUID> {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package de.bund.digitalservice.ris.caselaw.adapter.database.jpa;

import jakarta.transaction.Transactional;
import java.util.List;
import java.util.UUID;
import org.springframework.context.annotation.Primary;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

@Repository
@Primary
public interface DatabaseNormAbbreviationRepository
extends JpaRepository<NormAbbreviationDTO, UUID> {

List<NormAbbreviationDTO> findByAbbreviationIgnoreCase(String abbreviation, PageRequest of);

List<NormAbbreviationDTO> findByAbbreviationContainsOrderByAbbreviation(
String query, PageRequest of);

List<NormAbbreviationDTO> findByOfficialLetterAbbreviationIgnoreCase(
String officialLetterAbbreviation, PageRequest of);

List<NormAbbreviationDTO> findByAbbreviationStartsWithIgnoreCase(
String abbreviation, PageRequest of);

List<NormAbbreviationDTO> findByOfficialLetterAbbreviationStartsWithIgnoreCase(
String officialLetterAbbreviation, PageRequest size);

@Query(
value =
"select"
+ " id,"
+ " abbreviation,"
+ " decision_date,"
+ " document_id,"
+ " document_number,"
+ " official_letter_abbreviation,"
+ " official_long_title,"
+ " official_short_title,"
+ " source,"
+ " region_id,"
+ " ts_rank_cd(weighted_vector, to_tsquery('german', '' || :tsQuery || '')) rank"
+ " from norm_abbreviation_search_migration"
+ " where weighted_vector @@ to_tsquery('german', '' || :tsQuery || '')"
+ " order by rank desc"
+ " limit :size",
nativeQuery = true)
List<NormAbbreviationDTO> findByRankWeightedVector(String tsQuery, Integer size);

@Transactional
@Modifying
@Query(value = "REFRESH MATERIALIZED VIEW norm_abbreviation_search_migration", nativeQuery = true)
void refreshMaterializedViews();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package de.bund.digitalservice.ris.caselaw.adapter.database.jpa;

import java.util.UUID;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface DatabaseRegionRepository extends JpaRepository<RegionDTO, UUID> {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package de.bund.digitalservice.ris.caselaw.adapter.database.jpa;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import java.util.UUID;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Entity
@Table(name = "document_category", schema = "incremental_migration")
public class DocumentCategoryDTO {
@Id @GeneratedValue private UUID id;

@Column
@NotBlank
@Size(max = 1)
private String label;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package de.bund.digitalservice.ris.caselaw.adapter.database.jpa;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import jakarta.persistence.Transient;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.util.UUID;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Entity
@ToString(onlyExplicitlyIncluded = true)
@Table(name = "document_type", schema = "incremental_migration")
public class DocumentTypeDTO {
@Id @GeneratedValue private UUID id;

@Column
@NotBlank
@Size(min = 1, max = 32)
private String abbreviation;

@ManyToOne
@JoinColumn(name = "document_category_id")
@Valid
@NotNull
private DocumentCategoryDTO category;

@Column private String label;

@Column(name = "super_label_1")
private String superLabel1;

@Column(name = "super_label_2")
private String superLabel2;

@Column @NotNull private Boolean multiple;

@Transient @ToString.Include private String jurisID;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package de.bund.digitalservice.ris.caselaw.adapter.database.jpa;

import java.util.UUID;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface JPADatabaseDocumentTypeRepository extends JpaRepository<DocumentTypeDTO, UUID> {}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
import org.springframework.stereotype.Repository;

@Repository
public interface JPADatabaseNormElementRepository extends JpaRepository<JPANormElementDTO, UUID> {
public interface JPADatabaseNormElementRepository extends JpaRepository<NormElementDTO, UUID> {

@Query(
value =
"SELECT ne.id, ne.label, ne.has_number_designation, ne.norm_code "
+ "FROM norm_element ne "
+ "INNER JOIN document_category dc ON ne.document_category_id=dc.id "
+ "FROM incremental_migration.norm_element ne "
+ "INNER JOIN incremental_migration.document_category dc ON ne.document_category_id=dc.id "
+ "WHERE dc.label = 'R'",
nativeQuery = true)
List<JPANormElementDTO> findAllByDocumentCategoryLabelR();
List<NormElementDTO> findAllByDocumentCategoryLabelR();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package de.bund.digitalservice.ris.caselaw.adapter.database.jpa;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinTable;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.time.LocalDate;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder(toBuilder = true)
@Entity()
@Table(name = "norm_abbreviation", schema = "incremental_migration")
public class NormAbbreviationDTO {

@Id @GeneratedValue private UUID id;

@Column
@Size(max = 255)
@NotBlank
private String abbreviation;

@Column(name = "decision_date")
private LocalDate decisionDate;

@Column(nullable = false, unique = true, name = "document_id")
@NotNull
private Long documentId;

@Column(name = "document_number")
private String documentNumber;

@OneToMany(fetch = FetchType.EAGER)
@JoinTable(
name = "norm_abbreviation_document_type",
schema = "incremental_migration",
joinColumns = @JoinColumn(name = "norm_abbreviation_id"),
inverseJoinColumns = @JoinColumn(name = "document_type_id"))
@Builder.Default
private Set<DocumentTypeDTO> documentTypeList = new HashSet<>();

@Column(name = "official_letter_abbreviation")
private String officialLetterAbbreviation;

@Column(name = "official_long_title")
private String officialLongTitle;

@Column(name = "official_short_title")
private String officialShortTitle;

@ManyToOne(fetch = FetchType.EAGER)
@JoinTable(
name = "norm_abbreviation_region",
schema = "incremental_migration",
joinColumns = @JoinColumn(name = "norm_abbreviation_id"),
inverseJoinColumns = @JoinColumn(name = "region_id"))
private RegionDTO region;

@Column
@Size(min = 1, max = 1)
private String source;

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

NormAbbreviationDTO that = (NormAbbreviationDTO) o;

return Objects.equals(id, that.id);
}

@Override
public int hashCode() {
return id != null ? id.hashCode() : 0;
}
}
Loading
Loading