View Javadoc
1   package de.dlr.shepard.context.references.timeseriesreference.daos;
2   
3   import de.dlr.shepard.common.neo4j.daos.GenericDAO;
4   import de.dlr.shepard.common.util.CypherQueryHelper;
5   import de.dlr.shepard.data.timeseries.model.TimeseriesTuple;
6   import jakarta.enterprise.context.RequestScoped;
7   import java.util.Map;
8   import java.util.Optional;
9   
10  @RequestScoped
11  public class TimeseriesTupleDAO extends GenericDAO<TimeseriesTuple> {
12  
13    /**
14     * Find a timeseries tuple in the database and return the matching object.
15     * This method mainly is used to obtain the Neo4j-internal ID for further processing.
16     *
17     * @param timeseriesTuple The TimeseriesTuple to be found. Its fields except ID are expected to be filled.
18     * @return The TimeseriesTuple if found in the database, otherwise an empty optional.
19     */
20    public Optional<TimeseriesTuple> find(TimeseriesTuple timeseriesTuple) {
21      return Optional.ofNullable(
22        find(
23          timeseriesTuple.getMeasurement(),
24          timeseriesTuple.getDevice(),
25          timeseriesTuple.getLocation(),
26          timeseriesTuple.getSymbolicName(),
27          timeseriesTuple.getField()
28        )
29      );
30    }
31  
32    private TimeseriesTuple find(String measurement, String device, String location, String symbolicName, String field) {
33      var query =
34        "MATCH (t:TimeseriesTuple { measurement: $measurement, device: $device, location: $location, symbolicName: $symbolicName, field: $field }) %s".formatted(
35            CypherQueryHelper.getReturnPart("t")
36          );
37      Map<String, Object> params = Map.of(
38        "measurement",
39        measurement,
40        "device",
41        device,
42        "location",
43        location,
44        "symbolicName",
45        symbolicName,
46        "field",
47        field
48      );
49      var results = findByQuery(query, params);
50      return results.iterator().hasNext() ? results.iterator().next() : null;
51    }
52  
53    @Override
54    public Class<TimeseriesTuple> getEntityType() {
55      return TimeseriesTuple.class;
56    }
57  }