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

Added a metadata table - one-to-many relationship apporach #82

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import uk.ac.ebi.eva.evaseqcol.service.SeqColService;

import java.io.IOException;
import java.util.List;

@RequestMapping("/admin")
@RestController
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package uk.ac.ebi.eva.evaseqcol.controller.seqcol;

import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import uk.ac.ebi.eva.evaseqcol.dto.SeqColMetadataDTO;
import uk.ac.ebi.eva.evaseqcol.service.MetadataService;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/metadata/")
@Tag(name = "Seqcol metadata")
public class MetadataController {

private final MetadataService metadataService;

public MetadataController(MetadataService metadataService) {
this.metadataService = metadataService;
}

@GetMapping(value = "/{digest}")
public ResponseEntity<List<SeqColMetadataDTO>> getSeqColMetadataByDigest(@PathVariable String digest) {
List<SeqColMetadataDTO> metadataList = metadataService.getMetadataBySeqColDigest(digest).stream()
.map(SeqColMetadataDTO::toMetadataDTO)
.collect(Collectors.toList());
if (metadataList.isEmpty()) {
return ResponseEntity.notFound().build();
}
return ResponseEntity.ok(metadataList);
}
}
30 changes: 30 additions & 0 deletions src/main/java/uk/ac/ebi/eva/evaseqcol/dto/SeqColMetadataDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package uk.ac.ebi.eva.evaseqcol.dto;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import uk.ac.ebi.eva.evaseqcol.entities.SeqColEntity;
import uk.ac.ebi.eva.evaseqcol.entities.SeqColMetadataEntity;

import java.util.Date;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class SeqColMetadataDTO {

private String sourceId;
private String sourceUrl;
private SeqColEntity.NamingConvention namingConvention;
private Date timestamp;

public static SeqColMetadataDTO toMetadataDTO(SeqColMetadataEntity metadataEntity) {
return new SeqColMetadataDTO(
metadataEntity.getSourceIdentifier(),
metadataEntity.getSourceUrl(),
metadataEntity.getNamingConvention(),
metadataEntity.getTimestamp()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ public abstract class SeqColEntity {

protected String digest; // The level 0 digest

protected NamingConvention namingConvention;


public enum NamingConvention {
ENA, GENBANK, UCSC, TEST
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
package uk.ac.ebi.eva.evaseqcol.entities;

import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Type;

import uk.ac.ebi.eva.evaseqcol.utils.JSONLevelOne;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import java.util.ArrayList;
import java.util.List;

@Entity
@NoArgsConstructor
@Data
@Table(name = "sequence_collections_L1")
@IdClass(SeqColId.class)
public class SeqColLevelOneEntity extends SeqColEntity{

@Id
Expand All @@ -32,15 +30,25 @@ public class SeqColLevelOneEntity extends SeqColEntity{
@Basic(fetch = FetchType.LAZY)
private JSONLevelOne seqColLevel1Object;

@Id
@Column(nullable = false)
@Enumerated(EnumType.STRING)
protected NamingConvention namingConvention;
@OneToMany(mappedBy = "seqColLevelOne", cascade = CascadeType.ALL)
private List<SeqColMetadataEntity> metadata;

public SeqColLevelOneEntity() {
this.metadata = new ArrayList<>();
}

public SeqColLevelOneEntity(String digest, NamingConvention namingConvention, JSONLevelOne jsonLevelOne){
super(digest, namingConvention);
public SeqColLevelOneEntity(String digest, JSONLevelOne jsonLevelOne, List<SeqColMetadataEntity> metadata){
super(digest);
this.seqColLevel1Object = jsonLevelOne;
this.namingConvention = namingConvention;
this.metadata = metadata;
}

public void addMetadata(SeqColMetadataEntity metadata){
this.metadata.add(metadata);
}

public void addAllMetadata(List<SeqColMetadataEntity> metadata){
this.metadata.addAll(metadata);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,4 @@ public SeqColLevelTwoEntity setDigest(String digest) {
this.digest = digest;
return this;
}

public SeqColLevelTwoEntity setNamingConvention(NamingConvention convention) {
this.namingConvention = convention;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package uk.ac.ebi.eva.evaseqcol.entities;

import lombok.Data;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Objects;

@Entity
@Table(name = "seqcol_md")
@Data
public class SeqColMetadataEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "md_id")
private Long mdId;

@Column(name = "source_id")
private String sourceIdentifier; // Eg: INSDC Acession

private String sourceUrl;

@Enumerated(EnumType.STRING)
private SeqColEntity.NamingConvention namingConvention;

@Column(updatable = false)
@Temporal(TemporalType.TIMESTAMP)
@CreationTimestamp
private Date timestamp;

@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "seqcol_digest")
@OnDelete(action = OnDeleteAction.CASCADE)
private SeqColLevelOneEntity seqColLevelOne;

public SeqColMetadataEntity setTimestamp(Timestamp timestamp) {
this.timestamp = timestamp;
return this;
}

public SeqColMetadataEntity setSourceIdentifier(String sourceIdentifier) {
this.sourceIdentifier = sourceIdentifier;
return this;
}

public SeqColMetadataEntity setSourceUrl(String sourceUrl) {
this.sourceUrl = sourceUrl;
return this;
}

public SeqColMetadataEntity setNamingConvention(SeqColEntity.NamingConvention namingConvention) {
this.namingConvention = namingConvention;
return this;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof SeqColMetadataEntity)) return false;
SeqColMetadataEntity that = (SeqColMetadataEntity) o;
return Objects.equals(sourceIdentifier, that.sourceIdentifier) && Objects.equals(sourceUrl, that.sourceUrl)
&& namingConvention == that.namingConvention && Objects.equals(seqColLevelOne, that.seqColLevelOne);
}

@Override
public int hashCode() {
return Objects.hash(sourceIdentifier, sourceUrl, namingConvention, seqColLevelOne);
}
}
17 changes: 17 additions & 0 deletions src/main/java/uk/ac/ebi/eva/evaseqcol/repo/MetadataRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package uk.ac.ebi.eva.evaseqcol.repo;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import uk.ac.ebi.eva.evaseqcol.entities.SeqColMetadataEntity;

import java.util.List;

@Repository
public interface MetadataRepository extends JpaRepository<SeqColMetadataEntity, Long> {

@Query(value = "select source_id from seqcol_md",nativeQuery = true)
List<String> findAllSourceIds();

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@

import uk.ac.ebi.eva.evaseqcol.entities.SeqColLevelOneEntity;

import java.util.Optional;

@Repository
public interface SeqColLevelOneRepository extends JpaRepository<SeqColLevelOneEntity, String> {
SeqColLevelOneEntity findSeqColLevelOneEntityByDigest(String digest);

Optional<SeqColLevelOneEntity> findSeqColLevelOneEntityByDigest(String digest);
long countSeqColLevelOneEntitiesByDigest(String digest);

void removeSeqColLevelOneEntityByDigest(String digest);

void deleteAll();

}
34 changes: 34 additions & 0 deletions src/main/java/uk/ac/ebi/eva/evaseqcol/service/MetadataService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package uk.ac.ebi.eva.evaseqcol.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import uk.ac.ebi.eva.evaseqcol.entities.SeqColMetadataEntity;
import uk.ac.ebi.eva.evaseqcol.repo.MetadataRepository;

import java.util.List;
import java.util.stream.Collectors;


@Service
public class MetadataService {

private final MetadataRepository metadataRepository;

@Autowired
public MetadataService(MetadataRepository metadataRepository) {
this.metadataRepository = metadataRepository;
}

public List<SeqColMetadataEntity> getMetadataBySeqColDigest(String seqColDigest) {
return metadataRepository.findAll().stream()
.filter(metadataEntity -> metadataEntity.getSeqColLevelOne().getDigest().equals(seqColDigest))
.collect(Collectors.toList());
}
/**
* Return the list of all saved source identifiers. Eg: ["GCA_000146045.2", ...]
* */
public List<String> getAllSourceIds() {
return metadataRepository.findAllSourceIds();
}
}
Loading
Loading