use of org.jbei.ice.storage.DAOException in project ice by JBEI.
the class PermissionDAO method canWrite.
/**
* Determines if the specified account has write privileges on the entries passed on the parameter
*
* @param account user account
* @param groups groups that the account belongs to
* @param entries list of entry Ids to check
* @return true if the user has write privileges on <b>all</b> the entries specified in the parameter
*/
public boolean canWrite(Account account, List<Group> groups, List<Long> entries) {
try {
CriteriaQuery<Long> query = getBuilder().createQuery(Long.class);
Root<Permission> from = query.from(Permission.class);
Join<Permission, Entry> entry = from.join("entry");
query.select(from.get("group"));
List<Predicate> predicates = new ArrayList<>();
predicates.add(entry.get("id").in(entries));
predicates.add(getBuilder().equal(entry.get("ownerEmail"), account.getEmail()));
if (groups.isEmpty()) {
predicates.add(getBuilder().equal(from.get("account"), account));
} else {
predicates.add(getBuilder().or(getBuilder().equal(from.get("account"), account), from.get("group").in(groups)));
}
predicates.add(getBuilder().equal(from.get("canWrite"), true));
query.select(entry.get("id")).where(predicates.toArray(new Predicate[predicates.size()]));
return currentSession().createQuery(query).uniqueResult() == entries.size();
} catch (HibernateException he) {
Logger.error(he);
throw new DAOException(he);
}
// Criteria criteria = currentSession().createCriteria(Permission.class);
// criteria.add(Restrictions.in("entry.id", entries));
// criteria.add(Restrictions.or(Restrictions.eq("entry.ownerEmail", account.getEmail())));
// Disjunction disjunction = Restrictions.disjunction();
//
// if (!groups.isEmpty()) {
// disjunction.add(Restrictions.in("group", groups));
// }
//
// disjunction.add(Restrictions.eq("account", account));
// criteria.createAlias("entry", "entry", JoinType.LEFT_OUTER_JOIN);
// LogicalExpression logicalExpression = Restrictions.and(disjunction, Restrictions.eq("canWrite", true));
//
// criteria.add(logicalExpression);
// criteria.setProjection(Projections.countDistinct("entry.id"));
// Number number = (Number) criteria.uniqueResult();
// return number.intValue() == entries.size();
}
use of org.jbei.ice.storage.DAOException in project ice by JBEI.
the class PermissionDAO method getCanReadEntries.
/**
* Filters the given list, removing those that the specified account does not have read privileges on
* // todo : this doesn't check permissions from folder (get list of all folders entries are in)
*
* @param account account to filter entries by
* @param groups groups that this account belongs to
* @param entries list of entry ids to filter
* @return filtered list such that specified account have read privileges on entries contained in it
*/
public List<Long> getCanReadEntries(Account account, List<Group> groups, List<Long> entries) {
try {
CriteriaQuery<Long> query = getBuilder().createQuery(Long.class);
Root<Permission> from = query.from(Permission.class);
Join<Permission, Entry> entry = from.join("entry", JoinType.LEFT);
Predicate predicate = getBuilder().or(getBuilder().equal(from.get("account"), account), getBuilder().equal(entry.get("ownerEmail"), account.getEmail()));
if (!groups.isEmpty())
predicate.getExpressions().add(from.get("group").in(groups));
List<Predicate> predicates = new ArrayList<>();
predicates.add(predicate);
predicates.add(entry.get("id").in(entries));
predicates.add(entry.get("visibility").in(Visibility.OK.getValue(), Visibility.PENDING.getValue()));
query.select(entry.get("id")).distinct(true);
query.where(predicates.toArray(new Predicate[predicates.size()]));
return currentSession().createQuery(query).list();
} catch (HibernateException he) {
Logger.error(he);
throw new DAOException(he);
}
}
use of org.jbei.ice.storage.DAOException in project ice by JBEI.
the class MessageDAO method retrieveMessageCount.
public int retrieveMessageCount(Account account, List<Group> groups) {
try {
Session session = currentSession();
StringBuilder builder = new StringBuilder();
builder.append("select count(id) from message m where m.id in ").append("(select message_id from message_destination_accounts where account_id = ").append(account.getId()).append(")");
if (groups != null && !groups.isEmpty()) {
builder.append(" OR m.id in (select message_id from message_destination_groups where group_id in").append(" (");
int size = groups.size();
int i = 0;
for (Group group : groups) {
builder.append(group.getId());
if (i < size - 1)
builder.append(", ");
i += 1;
}
builder.append("))");
}
NativeQuery query = session.createNativeQuery(builder.toString());
Number number = (Number) query.uniqueResult();
return number.intValue();
} catch (HibernateException he) {
Logger.error(he);
throw new DAOException();
}
}
use of org.jbei.ice.storage.DAOException in project ice by JBEI.
the class MessageDAO method retrieveMessages.
public List<Message> retrieveMessages(Account account, List<Group> groups, int start, int count) {
try {
StringBuilder builder = new StringBuilder();
builder.append("select id from message m where m.id in ").append("(select message_id from message_destination_accounts where account_id = ").append(account.getId()).append(")");
if (groups != null && !groups.isEmpty()) {
builder.append(" OR m.id in (select message_id from message_destination_groups where group_id in (");
int size = groups.size();
int i = 0;
for (Group group : groups) {
builder.append(group.getId());
if (i < size - 1)
builder.append(", ");
i += 1;
}
builder.append("))");
}
NativeQuery query = currentSession().createNativeQuery(builder.toString());
query.setFirstResult(start);
query.setMaxResults(count);
List list = query.list();
Set<Long> set = new HashSet<>();
for (Object object : list) {
Number number = (Number) object;
set.add(number.longValue());
}
if (set.isEmpty())
return new ArrayList<>();
CriteriaQuery<Message> criteriaQuery = getBuilder().createQuery(Message.class);
Root<Message> from = criteriaQuery.from(Message.class);
criteriaQuery.where(from.get("id").in(set));
criteriaQuery.orderBy(getBuilder().desc(from.get("dateSent")));
return currentSession().createQuery(criteriaQuery).list();
} catch (HibernateException he) {
Logger.error(he);
throw new DAOException(he);
}
}
use of org.jbei.ice.storage.DAOException in project ice by JBEI.
the class ManuscriptModelDAO method list.
public List<ManuscriptModel> list(String sort, boolean asc, int offset, int size, String filter) {
try {
CriteriaQuery<ManuscriptModel> query = getBuilder().createQuery(ManuscriptModel.class);
Root<ManuscriptModel> from = query.from(ManuscriptModel.class);
if (filter != null && !filter.trim().isEmpty()) {
query.where(getBuilder().like(getBuilder().lower(from.get("title")), "%" + filter.toLowerCase() + "%"));
}
query.orderBy(asc ? getBuilder().asc(from.get(sort)) : getBuilder().desc(from.get(sort)));
return currentSession().createQuery(query).setFirstResult(offset).setMaxResults(size).list();
} catch (HibernateException he) {
Logger.error(he);
throw new DAOException(he);
}
}
Aggregations