Search in sources :

Example 41 with DAOException

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();
}
Also used : DAOException(org.jbei.ice.storage.DAOException) HibernateException(org.hibernate.HibernateException) ArrayList(java.util.ArrayList)

Example 42 with DAOException

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);
    }
}
Also used : DAOException(org.jbei.ice.storage.DAOException) HibernateException(org.hibernate.HibernateException) ArrayList(java.util.ArrayList)

Example 43 with DAOException

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();
    }
}
Also used : DAOException(org.jbei.ice.storage.DAOException) Group(org.jbei.ice.storage.model.Group) NativeQuery(org.hibernate.query.NativeQuery) HibernateException(org.hibernate.HibernateException) Session(org.hibernate.Session)

Example 44 with 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);
    }
}
Also used : Group(org.jbei.ice.storage.model.Group) Message(org.jbei.ice.storage.model.Message) HibernateException(org.hibernate.HibernateException) DAOException(org.jbei.ice.storage.DAOException) NativeQuery(org.hibernate.query.NativeQuery) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet)

Example 45 with DAOException

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);
    }
}
Also used : DAOException(org.jbei.ice.storage.DAOException) HibernateException(org.hibernate.HibernateException) ManuscriptModel(org.jbei.ice.storage.model.ManuscriptModel)

Aggregations

DAOException (org.jbei.ice.storage.DAOException)150 HibernateException (org.hibernate.HibernateException)144 ArrayList (java.util.ArrayList)26 Group (org.jbei.ice.storage.model.Group)14 Account (org.jbei.ice.storage.model.Account)8 Request (org.jbei.ice.storage.model.Request)8 Predicate (javax.persistence.criteria.Predicate)7 SampleRequest (org.jbei.ice.lib.dto.sample.SampleRequest)6 Entry (org.jbei.ice.storage.model.Entry)6 Session (org.hibernate.Session)5 ApiKey (org.jbei.ice.storage.model.ApiKey)5 BulkUpload (org.jbei.ice.storage.model.BulkUpload)5 NativeQuery (org.hibernate.query.NativeQuery)4 Feature (org.jbei.ice.storage.model.Feature)4 Preference (org.jbei.ice.storage.model.Preference)4 Sample (org.jbei.ice.storage.model.Sample)4 Attachment (org.jbei.ice.storage.model.Attachment)3 Audit (org.jbei.ice.storage.model.Audit)3 RemoteClientModel (org.jbei.ice.storage.model.RemoteClientModel)3 ShotgunSequence (org.jbei.ice.storage.model.ShotgunSequence)3