2018-03-21 11:57:56 +01:00
|
|
|
package eu.eudat.data.dao.entities;
|
2017-09-14 12:37:36 +02:00
|
|
|
|
2018-03-21 11:57:56 +01:00
|
|
|
import eu.eudat.data.dao.DatabaseAccess;
|
2019-07-31 16:57:34 +02:00
|
|
|
import eu.eudat.data.dao.criteria.GrantCriteria;
|
2018-03-21 11:57:56 +01:00
|
|
|
import eu.eudat.data.dao.databaselayer.service.DatabaseService;
|
2019-05-14 15:08:49 +02:00
|
|
|
import eu.eudat.data.entities.DMP;
|
2019-07-31 16:57:34 +02:00
|
|
|
import eu.eudat.data.entities.Grant;
|
2018-03-21 11:57:56 +01:00
|
|
|
import eu.eudat.data.entities.UserInfo;
|
2017-12-17 22:34:24 +01:00
|
|
|
import eu.eudat.queryable.QueryableList;
|
2019-07-31 16:57:34 +02:00
|
|
|
import eu.eudat.types.grant.GrantStateType;
|
2017-12-17 22:34:24 +01:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
2018-10-02 16:33:58 +02:00
|
|
|
import org.springframework.scheduling.annotation.Async;
|
2017-12-15 00:01:26 +01:00
|
|
|
import org.springframework.stereotype.Component;
|
2017-09-14 12:37:36 +02:00
|
|
|
|
2018-05-28 11:50:42 +02:00
|
|
|
import javax.persistence.criteria.JoinType;
|
2018-08-24 17:21:02 +02:00
|
|
|
import java.util.Date;
|
2018-02-16 11:34:02 +01:00
|
|
|
import java.util.UUID;
|
2018-02-21 11:07:31 +01:00
|
|
|
import java.util.concurrent.CompletableFuture;
|
2018-02-16 11:34:02 +01:00
|
|
|
|
2019-07-31 16:57:34 +02:00
|
|
|
@Component("grantDao")
|
|
|
|
public class GrantDaoImpl extends DatabaseAccess<Grant> implements GrantDao {
|
2017-09-14 12:37:36 +02:00
|
|
|
|
2018-02-02 17:21:46 +01:00
|
|
|
@Autowired
|
2019-07-31 16:57:34 +02:00
|
|
|
public GrantDaoImpl(DatabaseService<Grant> databaseService) {
|
2018-05-28 11:50:42 +02:00
|
|
|
super(databaseService);
|
2018-02-02 17:21:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-07-31 16:57:34 +02:00
|
|
|
public QueryableList<Grant> getWithCriteria(GrantCriteria criteria) {
|
|
|
|
QueryableList<Grant> query = getDatabaseService().getQueryable(Grant.class);
|
2018-02-02 17:21:46 +01:00
|
|
|
if (criteria.getLike() != null && !criteria.getLike().isEmpty())
|
2019-02-11 14:46:29 +01:00
|
|
|
query.where((builder, root) ->
|
2018-10-09 10:14:29 +02:00
|
|
|
builder.or(builder.like(builder.upper(root.get("label")), "%" + criteria.getLike().toUpperCase() + "%"),
|
|
|
|
builder.or(builder.like(builder.upper(root.get("description")), "%" + criteria.getLike().toUpperCase() + "%"))));
|
2018-02-02 17:21:46 +01:00
|
|
|
if (criteria.getPeriodEnd() != null)
|
|
|
|
query.where((builder, root) -> builder.lessThan(root.get("enddate"), criteria.getPeriodEnd()));
|
|
|
|
if (criteria.getPeriodStart() != null)
|
|
|
|
query.where((builder, root) -> builder.greaterThan(root.get("startdate"), criteria.getPeriodStart()));
|
|
|
|
if (criteria.getReference() != null)
|
|
|
|
query.where((builder, root) -> builder.equal(root.get("reference"), criteria.getReference()));
|
2019-07-31 16:57:34 +02:00
|
|
|
if (criteria.getGrantStateType() != null) {
|
|
|
|
if (criteria.getGrantStateType().equals(GrantStateType.FINISHED.getValue()))
|
2018-08-24 17:21:02 +02:00
|
|
|
query.where((builder, root) -> builder.lessThan(root.get("enddate"), new Date()));
|
2019-07-31 16:57:34 +02:00
|
|
|
if (criteria.getGrantStateType().equals(GrantStateType.ONGOING.getValue()))
|
2018-08-24 17:21:02 +02:00
|
|
|
query.where((builder, root) ->
|
|
|
|
builder.or(builder.greaterThan(root.get("enddate"), new Date())
|
|
|
|
, builder.isNull(root.get("enddate"))));
|
|
|
|
}
|
2019-05-14 15:08:49 +02:00
|
|
|
if (criteria.isPublic()) {
|
|
|
|
query.where((builder, root) -> builder.equal(root.join("dmps").get("status"), DMP.DMPStatus.FINALISED.getValue())).distinct();
|
|
|
|
}
|
2019-07-31 16:57:34 +02:00
|
|
|
query.where((builder, root) -> builder.notEqual(root.get("status"), Grant.Status.DELETED.getValue()));
|
2018-02-02 17:21:46 +01:00
|
|
|
return query;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-07-31 16:57:34 +02:00
|
|
|
public Grant createOrUpdate(Grant item) {
|
|
|
|
return getDatabaseService().createOrUpdate(item, Grant.class);
|
2018-02-02 17:21:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-07-31 16:57:34 +02:00
|
|
|
public Grant find(UUID id) {
|
|
|
|
return getDatabaseService().getQueryable(Grant.class).where((builder, root) -> builder.equal((root.get("id")), id)).getSingle();
|
2018-02-02 17:21:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-07-31 16:57:34 +02:00
|
|
|
public void delete(Grant item) {
|
2018-02-02 17:21:46 +01:00
|
|
|
this.getDatabaseService().delete(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-07-31 16:57:34 +02:00
|
|
|
public QueryableList<Grant> asQueryable() {
|
|
|
|
return this.getDatabaseService().getQueryable(Grant.class);
|
2018-02-02 17:21:46 +01:00
|
|
|
}
|
|
|
|
|
2019-07-31 16:57:34 +02:00
|
|
|
public QueryableList<Grant> getAuthenticated(QueryableList<Grant> query, UserInfo principal) {
|
2019-06-13 09:32:45 +02:00
|
|
|
query.where((builder, root) -> builder.or(builder.equal(root.get("creationUser"), principal), builder.equal(root.join("dmps", JoinType.LEFT).join("users", JoinType.LEFT).join("user", JoinType.LEFT).get("id"), principal.getId()))).distinct();
|
2018-02-02 17:21:46 +01:00
|
|
|
return query;
|
|
|
|
}
|
2018-02-21 11:07:31 +01:00
|
|
|
|
2018-10-02 16:33:58 +02:00
|
|
|
@Async
|
2018-02-21 11:07:31 +01:00
|
|
|
@Override
|
2019-07-31 16:57:34 +02:00
|
|
|
public CompletableFuture<Grant> createOrUpdateAsync(Grant item) {
|
2018-02-21 11:07:31 +01:00
|
|
|
return CompletableFuture.supplyAsync(() -> this.createOrUpdate(item));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-07-31 16:57:34 +02:00
|
|
|
public Grant find(UUID id, String hint) {
|
2018-02-21 11:07:31 +01:00
|
|
|
throw new UnsupportedOperationException();
|
|
|
|
}
|
2017-09-14 12:37:36 +02:00
|
|
|
}
|