Skip to content
Merged
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 @@ -35,6 +35,9 @@ public class MetadataEntity extends AbstractEntity<Long> {
@Column(name = "observation_area_id")
private Long observationAreaId;

@Column(name = "direction")
private String direction;

@OneToMany(mappedBy = "metadata", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<CoordinateEntity> geometryCoordinates;

Expand Down Expand Up @@ -93,5 +96,13 @@ public Long getObservationAreaId() {
public void setObservationAreaId(Long observationAreaId) {
this.observationAreaId = observationAreaId;
}

public String getDirection() {
return direction;
}

public void setDirection(String direction) {
this.direction = direction;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ public interface MetadataRepository extends JpaRepository<MetadataEntity, Long>

public MetadataEntity findFirstByNameAndClassification(String name, String classification);

public MetadataEntity findFirstByNameAndClassificationOrderByIdDesc(String name, String classification);

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ public class ObservationJobEntity extends AbstractEntity<Long> {

@Column(name = "center_latitude")
private BigDecimal centerLatitude;


@Column(name = "direction")
private String direction;

@NotNull
@OneToMany(mappedBy = "observationJob", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<PointEntity> geometryPoints;
Expand Down Expand Up @@ -148,5 +151,13 @@ public BigDecimal getCenterLatitude() {
public void setCenterLatitude(BigDecimal centerLatitude) {
this.centerLatitude = centerLatitude;
}

public String getDirection() {
return direction;
}

public void setDirection(String direction) {
this.direction = direction;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE "metadata"
ADD COLUMN "direction" VARCHAR(255) DEFAULT NULL;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "metadata" DROP CONSTRAINT "unique_name_classification";
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE "observation_job"
ADD COLUMN "direction" VARCHAR(255) DEFAULT NULL;
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import de.starwit.persistence.analytics.entity.MetadataEntity;
import de.starwit.persistence.analytics.repository.MetadataRepository;
import de.starwit.persistence.observatory.entity.ObservationJobEntity;
import de.starwit.persistence.observatory.entity.PointEntity;
import de.starwit.service.impl.ServiceInterface;

@Service
Expand All @@ -27,8 +28,8 @@ public MetadataRepository getRepository() {
}

public MetadataEntity saveMetadataForJob(ObservationJobEntity jobEntity) {
MetadataEntity metadata = metadataRepository.findFirstByNameAndClassification(jobEntity.getName(), jobEntity.getClassification());

MetadataEntity metadata = findCurrentMetadata(jobEntity);

if (metadata == null) {
metadata = new MetadataEntity();
Expand All @@ -38,6 +39,7 @@ public MetadataEntity saveMetadataForJob(ObservationJobEntity jobEntity) {
metadata.setCenterLatitude(jobEntity.getCenterLatitude());
metadata.setCenterLongitude(jobEntity.getCenterLongitude());
metadata.setObservationAreaId(jobEntity.getObservationAreaId());
metadata.setDirection(jobEntity.getDirection());

List<CoordinateEntity> coordinates = new ArrayList<>();
if (jobEntity.getGeoReferenced()) {
Expand All @@ -47,7 +49,7 @@ public MetadataEntity saveMetadataForJob(ObservationJobEntity jobEntity) {
}
}
metadata.setGeometryCoordinates(coordinates);

metadata = metadataRepository.saveAndFlush(metadata);
}

Expand All @@ -68,5 +70,58 @@ public MetadataEntity findFirstByNameAndClassification(String name, String class
MetadataEntity metadata = metadataRepository.findFirstByNameAndClassification(name, classification);
return metadata;
}


public MetadataEntity findCurrentMetadata(ObservationJobEntity jobEntity) {
MetadataEntity latestMetadata = metadataRepository
.findFirstByNameAndClassificationOrderByIdDesc(jobEntity.getName(), jobEntity.getClassification());
if (latestMetadata != null && metadataMatchesJob(latestMetadata, jobEntity)) {
return latestMetadata;
} else {
return null;
}
}

/**
* Checks if all metadata fields equal the corresponding job fields (i.e.
* whether given metadata entity belongs to the given job)
*/
private boolean metadataMatchesJob(MetadataEntity metadata, ObservationJobEntity job) {
boolean matches = true;

matches = matches && metadata.getName().equals(job.getName());
matches = matches && (metadata.getClassification() == null && job.getClassification() == null
|| job.getClassification() != null && job.getClassification().equals(metadata.getClassification()));

matches = matches && (metadata.getGeoReferenced() == null && job.getGeoReferenced() == null
|| job.getGeoReferenced() != null && job.getGeoReferenced().equals(metadata.getGeoReferenced()));

matches = matches && (metadata.getCenterLatitude() == null && job.getCenterLatitude() == null
|| job.getCenterLatitude() != null && job.getCenterLatitude().equals(metadata.getCenterLatitude()));

matches = matches && (metadata.getCenterLongitude() == null && job.getCenterLongitude() == null
|| job.getCenterLongitude() != null && job.getCenterLongitude().equals(metadata.getCenterLongitude()));

matches = matches && (metadata.getObservationAreaId() == null && job.getObservationAreaId() == null
|| job.getObservationAreaId() != null
&& job.getObservationAreaId().equals(metadata.getObservationAreaId()));

matches = matches && (metadata.getDirection() == null && job.getDirection() == null
|| job.getDirection() != null && job.getDirection().equals(metadata.getDirection()));

// Check if geometry matches (only if geoReferenced is true, otherwise geometry
// is not relevant and can be ignored)
if (matches && job.getGeoReferenced()) {
for (int i = 0; i < metadata.getGeometryCoordinates().size(); i++) {
CoordinateEntity c1 = metadata.getGeometryCoordinates().get(i);
PointEntity c2 = job.getGeometryPoints().get(i);
if (c1.getLatitude() != null && !c1.getLatitude().equals(c2.getLatitude()))
return false;
if (c1.getLongitude() != null && !c1.getLongitude().equals(c2.getLongitude()))
return false;
}
}

return matches;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public ObservationJobEntity update(Long id, ObservationJobEntity jobUpdate) {
existingJob.setGeometryPoints(jobUpdate.getGeometryPoints());
existingJob.setCenterLatitude(jobUpdate.getCenterLatitude());
existingJob.setCenterLongitude(jobUpdate.getCenterLongitude());
existingJob.setDirection(jobUpdate.getDirection());

ObservationJobEntity updatedJob = observationJobRepository.save(existingJob);

Expand Down
Loading