Skip to content

Commit

Permalink
feat: store operation info in local db
Browse files Browse the repository at this point in the history
  • Loading branch information
Pfeil committed Sep 1, 2023
1 parent 620e459 commit 63690f3
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 15 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
65 changes: 59 additions & 6 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,13 +80,31 @@ 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;
int result = 1;
result = prime * result + ((pid == null) ? 0 : pid.hashCode());
result = prime * result + ((created == null) ? 0 : created.hashCode());
result = prime * result + ((modified == null) ? 0 : modified.hashCode());
result = prime * result + ((pid == null) ? 0 : pid.hashCode());
result = prime * result + ((supportedTypes == null) ? 0 : supportedTypes.hashCode());
result = prime * result + ((supportedLocations == null) ? 0 : supportedLocations.hashCode());
return result;
}

Expand All @@ -73,9 +114,14 @@ public boolean equals(Object obj) {
return true;
if (obj == null)
return false;
if (!(obj instanceof KnownPid))
if (getClass() != obj.getClass())
return false;
KnownPid other = (KnownPid) obj;
if (pid == null) {
if (other.pid != null)
return false;
} else if (!pid.equals(other.pid))
return false;
if (created == null) {
if (other.created != null)
return false;
Expand All @@ -86,16 +132,23 @@ public boolean equals(Object obj) {
return false;
} else if (!modified.equals(other.modified))
return false;
if (pid == null) {
if (other.pid != null)
if (supportedTypes == null) {
if (other.supportedTypes != null)
return false;
} else if (!pid.equals(other.pid))
} else if (!supportedTypes.equals(other.supportedTypes))
return false;
if (supportedLocations == null) {
if (other.supportedLocations != null)
return false;
} else if (!supportedLocations.equals(other.supportedLocations))
return false;
return true;
}

@Override
public String toString() {
return "KnownPID [created=" + created + ", modified=" + modified + ", pid=" + pid + "]";
return "KnownPid [pid=" + pid + ", created=" + created + ", modified=" + modified + ", supportedTypes="
+ supportedTypes + ", supportedLocations=" + supportedLocations + "]";
}

}
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 DISTINCT(k) FROM KnownPid k WHERE ?1 MEMBER OF k.supportedLocations")
Collection<KnownPid> findBySupportedLocationsContain(String location);

@Query("SELECT DISTINCT(k) FROM KnownPid k WHERE ?1 MEMBER OF k.supportedTypes")
Collection<KnownPid> findBySupportedTypesContain(String type);
}
34 changes: 34 additions & 0 deletions src/test/java/edu/kit/datamanager/pit/pidlog/KnownPidsDaoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
import java.time.DateTimeException;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
Expand All @@ -23,6 +24,10 @@
import org.springframework.data.domain.Pageable;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestPropertySource;
import org.springframework.transaction.annotation.Transactional;

import edu.kit.datamanager.pit.domain.PIDRecord;
import edu.kit.datamanager.pit.pitservice.ITypingService;

@SpringBootTest
@TestPropertySource("/test/application-test.properties")
Expand All @@ -32,6 +37,9 @@ public class KnownPidsDaoTest {
@Autowired
private KnownPidsDao knownPidsDao;

@Autowired
private ITypingService typingService;

private static final Instant NOW = Instant.now();

private static final Instant TOO_SOON = NOW.minus(2, ChronoUnit.DAYS).truncatedTo(ChronoUnit.MILLIS);
Expand Down Expand Up @@ -204,4 +212,30 @@ void testFindDistinctPidsByModifiedBetweenPageable() {
page = page_siblings.nextPageable();
} while (page_siblings.hasNext());
}

@Test
@Transactional
void testRetrieveBySupportedType() throws IOException {
knownPidsDao.deleteAll();
knownPidsDao.flush();

String supportedType = "some/supported-type";

PIDRecord r = new PIDRecord();
r.setPid("not-a-pid");
r.addEntry("21.T11148/2694e4a7a5a00d44e62b", "", supportedType);
r.addEntry("21.T11148/2694e4a7a5a00d44e62b", "", "second/supported-type");
KnownPid w = new KnownPid(r, typingService.getOperations());

assertEquals(0, knownPidsDao.count());
knownPidsDao.save(w);
assertEquals(1, knownPidsDao.count());

Collection<KnownPid> result = knownPidsDao.findBySupportedLocationsContain("other/type");
assertEquals(0, result.size());

result = knownPidsDao.findBySupportedTypesContain(supportedType);
assertEquals(1, result.size());
assertEquals(w, result.iterator().next());
}
}

0 comments on commit 63690f3

Please sign in to comment.