Example usage for org.hibernate.criterion Restrictions eq

List of usage examples for org.hibernate.criterion Restrictions eq

Introduction

In this page you can find the example usage for org.hibernate.criterion Restrictions eq.

Prototype

public static SimpleExpression eq(String propertyName, Object value) 

Source Link

Document

Apply an "equal" constraint to the named property

Usage

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

License:Open Source License

@Test
public void testDeleteWithRefs() {
    Session db = DataAccessActivator.getNewSession();

    db.beginTransaction();//  ww  w.j  a  va 2 s. c  om

    UserClass uc = new UserClass();
    uc.setName("UC_Test");
    uc.setPriority((short) 1);
    uc.setTimeHorizon(0);
    db.persist(uc);

    UserClassKey key = new UserClassKey();
    key.setRemaining(1);
    key.setUserClass(uc);
    key.generateKey();
    db.persist(key);

    UserClassKeyConstraint keyConst = new UserClassKeyConstraint();
    keyConst.setName("Const_1");
    keyConst.setValue("Value_1");
    keyConst.setClassKey(key);
    db.persist(keyConst);

    UserClassKeyConstraint keyConst2 = new UserClassKeyConstraint();
    keyConst2.setName("Const_2");
    keyConst2.setValue("Value_2");
    keyConst2.setClassKey(key);
    db.persist(keyConst2);

    User user = new User();
    user.setName("uck_test");
    user.setNamespace("TEST");
    user.setPersona("USER");
    db.persist(user);

    User user2 = new User();
    user2.setName("uck_test2");
    user2.setNamespace("TEST");
    user2.setPersona("USER");
    db.persist(user2);

    UserClassKeyRedemption redemp = new UserClassKeyRedemption();
    redemp.setClassKey(key);
    redemp.setUser(user);
    db.persist(redemp);

    UserClassKeyRedemption redemp2 = new UserClassKeyRedemption();
    redemp2.setClassKey(key);
    redemp2.setUser(user2);
    db.persist(redemp2);

    db.getTransaction().commit();

    int preDelete = (Integer) db.createCriteria(UserClassKey.class)
            .add(Restrictions.eq("redeemKey", key.getRedeemKey())).setProjection(Projections.rowCount())
            .uniqueResult();

    int preConstRefCount = (Integer) db.createCriteria(UserClassKeyConstraint.class)
            .add(Restrictions.eq("classKey.id", key.getId())).setProjection(Projections.rowCount())
            .uniqueResult();

    int preRedempRefCount = (Integer) db.createCriteria(UserClassKeyRedemption.class)
            .add(Restrictions.eq("classKey.id", key.getId())).setProjection(Projections.rowCount())
            .uniqueResult();

    this.dao.delete(this.dao.get(key.getId()));

    int postDelete = (Integer) db.createCriteria(UserClassKey.class)
            .add(Restrictions.eq("redeemKey", key.getRedeemKey())).setProjection(Projections.rowCount())
            .uniqueResult();

    int postConstRefCount = (Integer) db.createCriteria(UserClassKeyConstraint.class)
            .add(Restrictions.eq("classKey.id", key.getId())).setProjection(Projections.rowCount())
            .uniqueResult();

    int postRedempRefCount = (Integer) db.createCriteria(UserClassKeyRedemption.class)
            .add(Restrictions.eq("classKey.id", key.getId())).setProjection(Projections.rowCount())
            .uniqueResult();

    db.beginTransaction();
    db.delete(user);
    db.delete(user2);
    db.delete(uc);
    db.getTransaction().commit();

    assertEquals(1, preDelete);
    assertEquals(2, preConstRefCount);
    assertEquals(2, preRedempRefCount);
    assertEquals(0, postDelete);
    assertEquals(0, postConstRefCount);
    assertEquals(0, postRedempRefCount);
}

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

License:Open Source License

/**
 * Deletes the user association of the user and user class.
 * //from   w ww .j  av  a  2  s  .c  o m
 * @param user user whose association to delete
 * @param userClass associated user class
 */
public void delete(User user, UserClass userClass) {
    UserAssociation ua = (UserAssociation) this.session.createCriteria(UserAssociation.class)
            .add(Restrictions.eq("user", user)).add(Restrictions.eq("userClass", userClass)).uniqueResult();

    if (ua == null) {
        this.logger.debug("Unable to delete user association between user '" + user.qName() + "' and class '"
                + userClass.getName() + "'.");
    } else {
        this.delete(ua);
    }
}

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

License:Open Source License

/**
 * Gets the user with the specified name.
 * /*from   w  w  w . j av  a  2 s  .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.UserClassDao.java

License:Open Source License

/**
 * Deletes all the user associations that contain the specified user class.
 * // w  w  w. j av  a2  s  .c  o m
 * @param uc user class whose associations to delete
 */
public void deleteUserAssociations(UserClass uc) {
    int num = (Integer) this.session.createCriteria(UserAssociation.class).add(Restrictions.eq("userClass", uc))
            .setProjection(Projections.count("user")).uniqueResult();
    if (num > 0) {
        /* Delete all user associations. */
        this.logger.debug("To delete user class '" + uc.getName() + "', " + num + " user associations have to"
                + " removed.");
        this.session.beginTransaction();
        int numDeleted = this.session.createQuery("delete UserAssociation ua where ua.userClass = :userclass")
                .setEntity("userclass", uc).executeUpdate();

        this.logger.info("Deleted " + numDeleted + " user associations when deleting user class '"
                + uc.getName() + "'.");
        this.session.getTransaction().commit();
    }
}

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

License:Open Source License

/**
 * Finds a key using is redeem key value. If not found 
 * <tt>null</tt> is returned.
 * //from  w  w w .  j a va 2  s .  com
 * @param key key value
 * @return key or null if not found
 */
public UserClassKey findKey(String key) {
    return (UserClassKey) this.session.createCriteria(UserClassKey.class).add(Restrictions.eq("redeemKey", key))
            .uniqueResult();
}

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

License:Open Source License

@Override
public void delete(UserClassKey key) {
    this.logger.info("Deleting user class key '" + key.getRedeemKey() + "'.");

    /* Delete all constraints. */
    if (key.getConstraints().size() > 0) {
        this.session.beginTransaction();
        for (UserClassKeyConstraint constraint : key.getConstraints()) {
            this.session.delete(constraint);
        }// w  ww  . j ava  2s.c  o m
        this.session.getTransaction().commit();
    }

    /* As there are potentially a large number of redepemtions, a DML
     * operation is used. */
    int num = (Integer) this.session.createCriteria(UserClassKeyRedemption.class)
            .add(Restrictions.eq("classKey", key)).setProjection(Projections.count("id")).uniqueResult();
    if (num > 0) {
        /* Delete all redemptions. */
        this.logger.debug("To delete user class key '" + key.getRedeemKey() + "', " + num
                + " redemptions have to" + " deleted.");
        this.session.beginTransaction();
        int numDeleted = this.session.createQuery(
                "delete " + UserClassKeyRedemption.class.getSimpleName() + " uk where uk.classKey = :key")
                .setEntity("key", key).executeUpdate();

        this.logger.info("Deleted " + numDeleted + " redemptions when deleting user class key '"
                + key.getRedeemKey() + "'.");
        this.session.getTransaction().commit();
    }

    super.delete(key);
}

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

License:Open Source License

/**
 * Finds the user with the specified qualified name. A qualified name has
 * the format &lt;namespace&gt;:&lt;name&gt;. If the user isn't found or
 * the specified qualified name is incorrect <code>null</code> is returned.
 * /*from w  w  w.ja  v  a 2s.c om*/
 * @param qname user qualified name
 * @return user or null if not found
 */
public User findByQName(String qName) {
    String parts[] = qName.split(":", 2);

    /* Sanity check for incorrect format. */
    if (parts.length < 2)
        return null;

    return (User) this.session.createCriteria(User.class).add(Restrictions.eq("namespace", parts[0]))
            .add(Restrictions.eq("name", parts[1])).uniqueResult();
}

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

License:Open Source License

/**
 * Returns the user with the specified name space and name or 
 * <code>null</code> if not found.
 * /*from w w  w . jav  a  2 s  . c om*/
 * @param ns user namespace
 * @param name user name
 * @return user or null if not found
 */
public User findByName(String ns, String name) {
    return (User) this.session.createCriteria(User.class).add(Restrictions.eq("namespace", ns))
            .add(Restrictions.eq("name", name)).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.//from   w  ww  .j  a va2  s  .  com
 *  
 * @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. //from w w  w.  ja  v  a  2 s . c  o 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;
}