89 lines
4.2 KiB
Java
89 lines
4.2 KiB
Java
package eu.eudat.data.dao.entities;
|
|
|
|
import eu.eudat.data.dao.DatabaseAccess;
|
|
import eu.eudat.data.dao.criteria.DatasetCriteria;
|
|
import eu.eudat.data.dao.databaselayer.service.DatabaseService;
|
|
import eu.eudat.data.entities.Dataset;
|
|
import eu.eudat.data.entities.UserInfo;
|
|
import eu.eudat.queryable.QueryableList;
|
|
import eu.eudat.queryable.types.FieldSelectionType;
|
|
import eu.eudat.queryable.types.SelectionField;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.scheduling.annotation.Async;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import java.util.Arrays;
|
|
import java.util.UUID;
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
|
@Component("datasetDao")
|
|
public class DatasetDaoImpl extends DatabaseAccess<Dataset> implements DatasetDao {
|
|
|
|
@Autowired
|
|
public DatasetDaoImpl(DatabaseService<Dataset> databaseService) {
|
|
super(databaseService);
|
|
}
|
|
|
|
@Override
|
|
public QueryableList<Dataset> getWithCriteria(DatasetCriteria criteria) {
|
|
QueryableList<Dataset> query = getDatabaseService().getQueryable(Dataset.getHints(), Dataset.class);
|
|
if (criteria.getLike() != null && !criteria.getLike().isEmpty())
|
|
query.where((builder, root) -> builder.or(
|
|
builder.like(builder.upper(root.get("label")), "%" + criteria.getLike().toUpperCase() + "%"),
|
|
builder.like(builder.upper(root.get("description")), "%" + criteria.getLike().toUpperCase() + "%")));
|
|
if (criteria.getStatus() != null)
|
|
query.where((builder, root) -> builder.equal(root.get("status"), criteria.getStatus()));
|
|
if (criteria.getProfileDatasetId() != null)
|
|
query.where((builder, root) -> builder.equal(root.get("profile").get("id"), criteria.getProfileDatasetId()));
|
|
if (criteria.getPeriodEnd() != null)
|
|
query.where((builder, root) -> builder.lessThan(root.get("created"), criteria.getPeriodEnd()));
|
|
if (criteria.getPeriodStart() != null)
|
|
query.where((builder, root) -> builder.greaterThan(root.get("created"), criteria.getPeriodStart()));
|
|
if (!criteria.getAllVersions())
|
|
query.initSubQuery(String.class).where((builder, root) -> builder.equal(root.get("dmp").get("version"), query.<String>subQueryMax((builder1, externalRoot, nestedRoot) -> builder1.equal(externalRoot.get("dmp").get("groupId"), nestedRoot.get("dmp").get("groupId")), Arrays.asList(new SelectionField(FieldSelectionType.COMPOSITE_FIELD, "dmp:version")), String.class)));
|
|
if (criteria.getDmpIds() != null && !criteria.getDmpIds().isEmpty())
|
|
query.where((builder, root) -> root.get("dmp").get("id").in(criteria.getDmpIds()));
|
|
query.where(((builder, root) -> builder.notEqual(root.get("status"), Dataset.Status.DELETED.getValue())));
|
|
return query;
|
|
}
|
|
|
|
@Override
|
|
public Dataset createOrUpdate(Dataset item) {
|
|
return getDatabaseService().createOrUpdate(item, Dataset.class);
|
|
}
|
|
|
|
@Override
|
|
public Dataset find(UUID id) {
|
|
return getDatabaseService().getQueryable(Dataset.class).where((builder, root) -> builder.equal((root.get("id")), id)).getSingle();
|
|
}
|
|
|
|
@Override
|
|
public Dataset find(UUID id, String hint) {
|
|
return getDatabaseService().getQueryable(Dataset.getHints(), Dataset.class).withHint(hint).where((builder, root) -> builder.equal((root.get("id")), id)).getSingle();
|
|
}
|
|
|
|
@Override
|
|
public QueryableList<Dataset> getAuthenticated(QueryableList<Dataset> query, UserInfo principal) {
|
|
if (principal.getId() == null) query.where((builder, root) -> builder.equal(root.get("isPublic"), true));
|
|
else {
|
|
query.where((builder, root) -> builder.or(builder.equal(root.get("dmp").get("creator"), principal), builder.isMember(principal, root.get("dmp").get("users"))));
|
|
}
|
|
return query;
|
|
}
|
|
|
|
@Override
|
|
public void delete(Dataset item) {
|
|
this.getDatabaseService().delete(item);
|
|
}
|
|
|
|
@Override
|
|
public QueryableList<Dataset> asQueryable() {
|
|
return this.getDatabaseService().getQueryable(Dataset.class);
|
|
}
|
|
|
|
@Async
|
|
public CompletableFuture<Dataset> createOrUpdateAsync(Dataset item) {
|
|
return CompletableFuture.supplyAsync(() -> this.createOrUpdate(item));
|
|
}
|
|
}
|