Example usage for org.hibernate Criteria add

List of usage examples for org.hibernate Criteria add

Introduction

In this page you can find the example usage for org.hibernate Criteria add.

Prototype

public Criteria add(Criterion criterion);

Source Link

Document

Add a Criterion restriction to constrain the results to be retrieved.

Usage

From source file:au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.RigLogDao.java

License:Open Source License

/**
 * Finds all the stored logs for the specified period for a specific
 * rig, filtered for a specific state. The logs are ordered by their
 * time stamp with the earliest log first.
 * //from www  . ja  v a 2 s  .c  o  m
 * @param rig rig to find the logs for
 * @param begin beginning of a period
 * @param end end of a period
 * @return list of logs
 */
@SuppressWarnings("unchecked")
public List<RigLog> findLogsOfState(Rig rig, String state, Date begin, Date end) {
    Criteria cri = this.session.createCriteria(RigLog.class);
    cri.add(Restrictions.eq("rig", rig));
    cri.add(Restrictions.gt("timeStamp", begin));
    cri.add(Restrictions.lt("timeStamp", end));
    cri.add(Restrictions.eq("newState", state));
    cri.addOrder(Order.asc("timeStamp"));
    return cri.list();
}

From source file:au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.RigTypeDao.java

License:Open Source License

/**
 * Returns the rig type with the specified name. If non exist, 
 * <code>null</code> is returned.
 * /* ww  w .  j a v a  2  s  .  co  m*/
 * @param name rig type name
 * @return rig type or null if not found
 */
public RigType findByName(String name) {
    Criteria cri = this.session.createCriteria(this.clazz);
    cri.add(Restrictions.eq("name", name));
    return (RigType) cri.uniqueResult();
}

From source file:au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.SessionDao.java

License:Open Source License

/**
 * Gets the users active session. If the user does not have an active 
 * session <code>null</code> is returned. If the user has multiple
 * active sessions, only the lastest (ordered by request time) is 
 * returned./*from  w  w  w.  ja v a2  s  .co  m*/
 * 
 * @param user user to find active session of
 * @return active session or null
 */
@SuppressWarnings("unchecked")
public Session findActiveSession(User user) {
    Criteria cri = this.session.createCriteria(Session.class);
    cri.add(Restrictions.eq("user", user));
    cri.add(Restrictions.eq("active", Boolean.TRUE));
    cri.addOrder(Order.desc("requestTime"));
    cri.setMaxResults(1);

    List<Session> sessions = cri.list();
    if (sessions.size() == 0) {
        return null;
    }
    return sessions.get(0);
}

From source file:au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.SessionDao.java

License:Open Source License

/**
 * Returns all the currently active sessions. If no active sessions are
 * found, an empty list is returned.//from w w  w.  j  a v a2 s . c  o m
 * 
 * @return active sessions
 */
@SuppressWarnings("unchecked")
public List<Session> findAllActiveSessions() {
    Criteria cri = this.session.createCriteria(Session.class);
    cri.add(Restrictions.eq("active", Boolean.TRUE));
    return cri.list();
}

From source file:au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.SlaveableRigsDao.java

License:Open Source License

/**
 * Returns info for an active session on the specified rig
 * /*from  w w w .  j  av a  2  s . c  o  m*/
 * @param user user to find active session of
 * @return active session or null
 */
@SuppressWarnings("unchecked")
public SlaveableRigs getInfo(Rig rig) {
    Criteria cri = this.session.createCriteria(SlaveableRigs.class);
    cri.add(Restrictions.eq("rig", rig));
    cri.addOrder(Order.desc("id"));
    cri.setMaxResults(1);

    List<SlaveableRigs> sessions = cri.list();
    if (sessions.size() == 0) {
        return null;
    }
    return sessions.get(0);
}

From source file:au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.SlaveableRigsDao.java

License:Open Source License

/**
 * Finishes the collaborative session//  www.  jav a  2 s .  c  om
 * 
 * @param user user to find active session of
 * @return active session or null
 */
@SuppressWarnings("unchecked")
public void finishCol(Rig rig) {
    Criteria cri = this.session.createCriteria(SlaveableRigs.class);
    cri.add(Restrictions.eq("rig", rig));
    cri.addOrder(Order.desc("id"));

    List<SlaveableRigs> sessions = cri.list();
    for (SlaveableRigs s : sessions) {
        this.delete(s);
    }
}

From source file:au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.UserClassDao.java

License:Open Source License

/**
 * Gets the user with the specified name.
 * /*w w w  .  j a v a  2s. c om*/
 * @param name name of user class
 * @return user class or null if not found
 */
public UserClass findByName(String name) {
    Criteria cri = this.session.createCriteria(UserClass.class);
    cri.add(Restrictions.eq("name", name));
    return (UserClass) cri.uniqueResult();
}

From source file:au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.UserLockDao.java

License:Open Source License

/**
 * Finds the user lock with is for the specified user and specified
 * resource permission./*  w w w  .  ja v a 2s.  c om*/
 *  
 * @param user user who is locked
 * @param perm resource which is locked
 * @return user lock or null if not found
 */
public UserLock findLock(User user, ResourcePermission perm) {
    Criteria cri = this.session.createCriteria(UserLock.class);
    cri.add(Restrictions.eq("user", user));
    cri.add(Restrictions.eq("resourcePermission", perm));

    return (UserLock) cri.uniqueResult();
}

From source file:au.edu.uts.eng.remotelabs.schedserver.permissions.pages.KeysPage.java

License:Open Source License

/**
 * Returns the list of keys for a user class. If the parameter 'historical'
 * is part of the request parameters and has a value of 'true', non-usable
 * keys are returned otherwise usable keys are returned. Usable keys are 
 * those that are 'active', have remaining uses and have not elapsed
 * their expiry time. //  w w w.j  av  a2s  . co  m
 * 
 * @param request request
 * @return list of permission keys
 * @throws JSONException 
 */
@SuppressWarnings("unchecked")
public JSONArray getList(HttpServletRequest request) throws JSONException {
    JSONArray arr = new JSONArray();

    String className = request.getParameter("name");
    if (className == null) {
        this.logger.warn("Unable to provide access key list because the user class name was not provided.");
        return arr;
    }

    Criteria qu = this.db.createCriteria(UserClassKey.class).add(Restrictions.eq("userTargeted", Boolean.FALSE))
            .addOrder(Order.asc("id"));

    if ("true".equals(request.getAttribute("historical"))) {
        qu.add(Restrictions.disjunction().add(Restrictions.eq("active", Boolean.FALSE))
                .add(Restrictions.eq("remaining", 0)))
                .add(Restrictions.or(Restrictions.isNull("expiry"), Restrictions.lt("expiry", new Date())));
    } else {
        qu.add(Restrictions.eq("active", Boolean.TRUE)).add(Restrictions.gt("remaining", 0))
                .add(Restrictions.or(Restrictions.isNull("expiry"), Restrictions.gt("expiry", new Date())));
    }

    qu = qu.createCriteria("userClass").add(Restrictions.eq("name", className));

    for (UserClassKey key : (List<UserClassKey>) qu.list()) {
        JSONObject keyObj = new JSONObject();
        keyObj.put("key", key.getRedeemKey());
        keyObj.put("active", key.isActive());
        keyObj.put("remaining", key.getRemaining());
        arr.put(keyObj);
    }

    return arr;
}

From source file:au.edu.uts.eng.remotelabs.schedserver.permissions.pages.UsersPage.java

License:Open Source License

/**
 * Gets a list of users with a specific search term. The users may be 
 * excluded from a specific class. //from   w w  w .java2s. c  om
 * 
 * @param request
 * @return response
 * @throws Exception
 */
@SuppressWarnings("unchecked")
public JSONArray list(HttpServletRequest request) throws JSONException {
    JSONArray arr = new JSONArray();

    Criteria qu = this.db.createCriteria(User.class);

    String search = request.getParameter("search");
    if (search != null) {
        /* Search filter. */
        qu.add(Restrictions.disjunction().add(Restrictions.like("name", search, MatchMode.ANYWHERE))
                .add(Restrictions.like("firstName", search, MatchMode.ANYWHERE))
                .add(Restrictions.like("lastName", search, MatchMode.ANYWHERE)));
    }

    if (request.getParameter("max") != null) {
        /* Max results. */
        qu.setMaxResults(Integer.parseInt(request.getParameter("max")));
    }

    if (request.getParameter("in") != null) {
        /* Users in class. */
        UserClass inClass = new UserClassDao(this.db).findByName(request.getParameter("in"));
        if (inClass == null) {
            this.logger.warn("Not going to add in class as a user list restriction because the class '"
                    + request.getParameter("in") + "' was not found.");
        } else {
            qu.createCriteria("userAssociations").add(Restrictions.eq("userClass", inClass));
        }
    }

    if (request.getParameter("notIn") != null) {
        /* Users not in class. */
        UserClass notInClass = new UserClassDao(this.db).findByName(request.getParameter("notIn"));
        if (notInClass == null) {
            this.logger.warn("Not going to add not in class as a user list restriction because the class '"
                    + request.getParameter("notIn") + "' was not found.");
        } else {
            DetachedCriteria subQu = DetachedCriteria.forClass(User.class)
                    .setProjection(Property.forName("name")).createCriteria("userAssociations")
                    .add(Restrictions.eq("userClass", notInClass));

            List<String> names = subQu.getExecutableCriteria(this.db).list();
            if (names.size() > 0) {
                qu.add(Restrictions.not(Restrictions.in("name", names)));
            }
        }
    }

    qu.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    qu.addOrder(Order.asc("lastName"));
    qu.addOrder(Order.asc("name"));

    for (User user : (List<User>) qu.list()) {
        JSONObject uo = new JSONObject();
        uo.put("name", user.getNamespace() + "-_-" + user.getName());

        if (user.getFirstName() == null || user.getLastName() == null) {
            uo.put("display", user.getName());
        } else {
            uo.put("display", user.getLastName() + ", " + user.getFirstName());
        }

        arr.put(uo);
    }

    return arr;
}