74 lines
2.4 KiB
Java
74 lines
2.4 KiB
Java
|
package eu.eudat.data.dao.entities;
|
||
|
|
||
|
import eu.eudat.data.dao.DatabaseAccess;
|
||
|
import eu.eudat.data.dao.databaselayer.service.DatabaseService;
|
||
|
import eu.eudat.data.entities.UserAssociation;
|
||
|
import eu.eudat.data.entities.UserInfo;
|
||
|
import eu.eudat.queryable.QueryableList;
|
||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||
|
import org.springframework.stereotype.Component;
|
||
|
|
||
|
import java.util.List;
|
||
|
import java.util.UUID;
|
||
|
import java.util.concurrent.CompletableFuture;
|
||
|
|
||
|
@Component("UserAssociationDao")
|
||
|
public class UserAssociationDaoImpl extends DatabaseAccess<UserAssociation> implements UserAssociationDao {
|
||
|
|
||
|
@Autowired
|
||
|
public UserAssociationDaoImpl(DatabaseService<UserAssociation> databaseService) {
|
||
|
super(databaseService);
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public UserAssociation createOrUpdate(UserAssociation item) {
|
||
|
return this.getDatabaseService().createOrUpdate(item, UserAssociation.class);
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public CompletableFuture<UserAssociation> createOrUpdateAsync(UserAssociation item) {
|
||
|
return CompletableFuture.supplyAsync(() -> this.createOrUpdate(item));
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public UserAssociation find(UUID id) {
|
||
|
return this.getDatabaseService().getQueryable(UserAssociation.class).where(((builder, root) -> builder.equal(root.get("id"), id))).getSingle();
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public UserAssociation find(UUID id, String hint) {
|
||
|
throw new UnsupportedOperationException();
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void delete(UserAssociation item) {
|
||
|
this.getDatabaseService().delete(item);
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public QueryableList<UserAssociation> asQueryable() {
|
||
|
return this.getDatabaseService().getQueryable(UserAssociation.class);
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public List<UserAssociation> getAssociated(UserInfo userId) {
|
||
|
return this.getDatabaseService().getQueryable(UserAssociation.class).where(((builder, root) ->
|
||
|
builder.or(builder.equal(root.get("firstUser"), userId), builder.equal(root.get("secondUser"), userId)))).toList();
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public Boolean areAssociated(UserInfo firstUser, UserInfo secondUser) {
|
||
|
return this.getDatabaseService().getQueryable(UserAssociation.class).where(((builder, root) ->
|
||
|
builder.or(
|
||
|
builder.and(
|
||
|
builder.equal(root.get("firstUser"), firstUser),
|
||
|
builder.equal(root.get("secondUser"), secondUser)
|
||
|
),
|
||
|
builder.and(
|
||
|
builder.equal(root.get("secondUser"), firstUser),
|
||
|
builder.equal(root.get("firstUser"), secondUser)
|
||
|
)
|
||
|
))).count() > 0;
|
||
|
}
|
||
|
}
|