TimeseriesTupleDAO.java

package de.dlr.shepard.context.references.timeseriesreference.daos;

import de.dlr.shepard.common.neo4j.daos.GenericDAO;
import de.dlr.shepard.common.util.CypherQueryHelper;
import de.dlr.shepard.data.timeseries.model.TimeseriesTuple;
import jakarta.enterprise.context.RequestScoped;
import java.util.Map;
import java.util.Optional;

@RequestScoped
public class TimeseriesTupleDAO extends GenericDAO<TimeseriesTuple> {

  /**
   * Find a timeseries tuple in the database and return the matching object.
   * This method mainly is used to obtain the Neo4j-internal ID for further processing.
   *
   * @param timeseriesTuple The TimeseriesTuple to be found. Its fields except ID are expected to be filled.
   * @return The TimeseriesTuple if found in the database, otherwise an empty optional.
   */
  public Optional<TimeseriesTuple> find(TimeseriesTuple timeseriesTuple) {
    return Optional.ofNullable(
      find(
        timeseriesTuple.getMeasurement(),
        timeseriesTuple.getDevice(),
        timeseriesTuple.getLocation(),
        timeseriesTuple.getSymbolicName(),
        timeseriesTuple.getField()
      )
    );
  }

  private TimeseriesTuple find(String measurement, String device, String location, String symbolicName, String field) {
    var query =
      "MATCH (t:TimeseriesTuple { measurement: $measurement, device: $device, location: $location, symbolicName: $symbolicName, field: $field }) %s".formatted(
          CypherQueryHelper.getReturnPart("t")
        );
    Map<String, Object> params = Map.of(
      "measurement",
      measurement,
      "device",
      device,
      "location",
      location,
      "symbolicName",
      symbolicName,
      "field",
      field
    );
    var results = findByQuery(query, params);
    return results.iterator().hasNext() ? results.iterator().next() : null;
  }

  @Override
  public Class<TimeseriesTuple> getEntityType() {
    return TimeseriesTuple.class;
  }
}