Skip to content

Commit

Permalink
feat: store operation info in local db (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pfeil committed Sep 1, 2023
1 parent 620e459 commit ad86e02
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 9 deletions.
9 changes: 5 additions & 4 deletions src/main/java/edu/kit/datamanager/pit/domain/Operations.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import org.joda.time.DateTime;
Expand Down Expand Up @@ -55,13 +56,13 @@ public Operations(ITypingService typingService) {
* @param pidRecord the record to extract the information from.
* @return the extracted "supported types", if any.
*/
public List<String> findSupportedTypes(PIDRecord pidRecord) {
public Set<String> findSupportedTypes(PIDRecord pidRecord) {
return KNOWN_SUPPORTED_TYPES
.stream()
.map(pidRecord::getPropertyValues)
.map(Arrays::asList)
.flatMap(List<String>::stream)
.collect(Collectors.toList());
.collect(Collectors.toSet());
}

/**
Expand All @@ -75,13 +76,13 @@ public List<String> findSupportedTypes(PIDRecord pidRecord) {
* @param pidRecord the record to extract the information from.
* @return the extracted "supported locations", if any.
*/
public List<String> findSupportedLocations(PIDRecord pidRecord) {
public Set<String> findSupportedLocations(PIDRecord pidRecord) {
return KNOWN_SUPPORTED_LOCATIONS
.stream()
.map(pidRecord::getPropertyValues)
.map(Arrays::asList)
.flatMap(List<String>::stream)
.collect(Collectors.toList());
.collect(Collectors.toSet());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ public interface PidRecordElasticRepository extends ElasticsearchRepository<PidR
Page<PidRecordElasticWrapper> findByPid(String pid, Pageable pageable);

@Query("{\"match\": {\"supportedLocations\": \"?0\"}}")
Collection<PidRecordElasticWrapper> findBySupportedLocationsContain(
String location
);
Collection<PidRecordElasticWrapper> findBySupportedLocationsContain(String location);

@Query("{\"match\": {\"supportedTypes\": \"?0\"}}")
Collection<PidRecordElasticWrapper> findBySupportedTypesContain(String type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.persistence.ElementCollection;
import javax.persistence.FetchType;
Expand Down Expand Up @@ -55,10 +57,10 @@ public class PidRecordElasticWrapper {
private Date lastUpdate;

@ElementCollection(fetch = FetchType.EAGER)
private List<String> supportedTypes = new ArrayList<>();
private Set<String> supportedTypes = new HashSet<>();

@ElementCollection(fetch = FetchType.EAGER)
private List<String> supportedLocations = new ArrayList<>();
private Set<String> supportedLocations = new HashSet<>();

@Field(type = FieldType.Text)
private List<String> read = new ArrayList<>();
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/edu/kit/datamanager/pit/pidlog/KnownPid.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
package edu.kit.datamanager.pit.pidlog;

import java.io.IOException;
import java.io.Serializable;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

import edu.kit.datamanager.pit.domain.Operations;
import edu.kit.datamanager.pit.domain.PIDRecord;

/**
* Stores information about a known PID so it can be stored in a database.
*
Expand All @@ -17,13 +26,19 @@
@Entity
public class KnownPid implements Serializable {
@Id
@org.springframework.data.annotation.Id
@NotBlank(message = "The known PID.")
private String pid;
@NotNull(message = "The date the PID was created")
private Instant created;
@NotNull(message = "The timestamp of the most recently performed modification.")
private Instant modified;

@ElementCollection(fetch = FetchType.EAGER)
private Set<String> supportedTypes = new HashSet<>();

@ElementCollection(fetch = FetchType.EAGER)
private Set<String> supportedLocations = new HashSet<>();

public KnownPid() {}

Expand All @@ -33,6 +48,14 @@ public KnownPid(String pid, Instant created, Instant modified) {
this.modified = modified;
}

public KnownPid(PIDRecord pidRecord, Operations pidOperations) throws IOException {
this.pid = pidRecord.getPid();
this.created = pidOperations.findDateCreated(pidRecord).orElse(new Date()).toInstant();
this.modified = pidOperations.findDateModified(pidRecord).orElse(new Date()).toInstant();
this.supportedTypes = pidOperations.findSupportedTypes(pidRecord);
this.supportedLocations = pidOperations.findSupportedLocations(pidRecord);
}

public String getPid() {
return pid;
}
Expand All @@ -57,6 +80,22 @@ public void setModified(Instant modified) {
this.modified = modified.truncatedTo(ChronoUnit.MILLIS);
}

public Set<String> getSupportedTypes() {
return supportedTypes;
}

public void setSupportedTypes(Set<String> supportedTypes) {
this.supportedTypes = supportedTypes;
}

public Set<String> getSupportedLocations() {
return supportedLocations;
}

public void setSupportedLocations(Set<String> supportedLocations) {
this.supportedLocations = supportedLocations;
}

@Override
public int hashCode() {
final int prime = 31;
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/edu/kit/datamanager/pit/pidlog/KnownPidsDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

/**
* Object to access known PIDs from the database.
Expand All @@ -18,6 +19,9 @@
*
* as well as the general concept documented in
* https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.core-concepts
*
* There is a introduction to a more high-level query language (JPQL) at
* https://www.baeldung.com/spring-data-jpa-query
*/
public interface KnownPidsDao extends JpaRepository<KnownPid, String>, JpaSpecificationExecutor<KnownPid> {
Optional<KnownPid> findByPid(String pid);
Expand All @@ -41,4 +45,10 @@ public interface KnownPidsDao extends JpaRepository<KnownPid, String>, JpaSpecif
long countDistinctPidsByCreatedBetween(Instant from, Instant to);

long countDistinctPidsByModifiedBetween(Instant from, Instant to);

@Query("SELECT w FROM KnownPid w WHERE ?1 IN w.supportedLocations")
Collection<KnownPid> findBySupportedLocationsContain(String location);

@Query("SELECT w FROM KnownPid w WHERE ?1 IN w.supportedLocations")
Collection<KnownPid> findBySupportedTypesContain(String type);
}

0 comments on commit ad86e02

Please sign in to comment.