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.bookings.intf.BookingsSOAPImpl.java

License:Open Source License

@Override
public FindFreeBookingsResponse findFreeBookings(FindFreeBookings findFreeBookings) {
    /* --------------------------------------------------------------------
     * -- Read request parameters.                                       --
     * -------------------------------------------------------------------- */
    FindBookingSlotType request = findFreeBookings.getFindBookingSlots();
    String debug = "Received " + this.getClass().getSimpleName() + "#findFreeBookings with params: ";

    UserIDType uid = request.getUserID();
    debug += " user ID=" + uid.getUserID() + ", user namespace=" + uid.getUserNamespace() + ", user name="
            + uid.getUserName() + " user QName=" + uid.getUserQName();

    PermissionIDType reqPermission = request.getPermissionID();
    if (reqPermission != null)
        debug += " permission ID=" + reqPermission.getPermissionID();

    ResourceIDType reqResource = request.getResourceID();
    if (reqResource != null)
        debug += " resource type= " + reqResource.getType() + ", ID=" + request.getResourceID().getResourceID()
                + ", name=" + reqResource.getResourceName();

    Calendar reqStart = request.getPeriod().getStartTime();
    Calendar reqEnd = request.getPeriod().getEndTime();
    this.dstHack(reqStart);
    this.dstHack(reqEnd);

    debug += " period start=" + reqStart.getTime() + ", period end=" + reqEnd.getTime();
    this.logger.debug(debug);

    /* --------------------------------------------------------------------
     * -- Generate valid, blank request parameters.                      --
     * -------------------------------------------------------------------- */
    FindFreeBookingsResponse response = new FindFreeBookingsResponse();
    BookingSlotListType slots = new BookingSlotListType();
    response.setFindFreeBookingsResponse(slots);

    PermissionIDType permission = new PermissionIDType();
    slots.setPermissionID(permission);/*  w w  w .ja v  a2  s . c  o  m*/

    ResourceIDType resource = new ResourceIDType();
    resource.setType("TYPE");
    slots.setResourceID(resource);

    Session db = DataAccessActivator.getNewSession();
    try {
        /* ----------------------------------------------------------------
         * -- Load the user.                                             --
         * ---------------------------------------------------------------- */
        User user = this.getUserFromUserID(uid, db);
        if (user == null) {
            this.logger.info("Unable to provide free times because the user has not been found. Supplied "
                    + "credentials ID=" + uid.getUserID() + ", namespace=" + uid.getUserNamespace() + ", "
                    + "name=" + uid.getUserName() + '.');
            return response;
        }

        /* ----------------------------------------------------------------
         * -- Load the permission.                                             --
         * ---------------------------------------------------------------- */
        ResourcePermission perm = null;
        if (reqPermission != null) {
            ResourcePermissionDao resPermissionDao = new ResourcePermissionDao(db);
            perm = resPermissionDao.get(Long.valueOf(reqPermission.getPermissionID()));
        } else if (reqResource != null) {
            Criteria qu = db.createCriteria(ResourcePermission.class);

            /* Add resource restrictions. */
            qu.add(Restrictions.eq("type", reqResource.getType()));
            if (ResourcePermission.TYPE_PERMISSION.equals(reqResource.getType())) {
                if (reqResource.getResourceID() > 0) {
                    qu.add(Restrictions.eq("rigType.id", Long.valueOf(reqResource.getResourceID())));
                }
                if (reqResource.getResourceName() != null) {
                    qu.createCriteria("rigType").add(Restrictions.eq("name", reqResource.getResourceName()));
                }
            } else if (ResourcePermission.RIG_PERMISSION.equals(reqResource.getType())) {
                if (reqResource.getResourceID() > 0) {
                    qu.add(Restrictions.eq("rig.id", Long.valueOf(reqResource.getResourceID())));
                }
                if (reqResource.getResourceName() != null) {
                    qu.createCriteria("rig").add(Restrictions.eq("name", reqResource.getResourceName()));
                }
            } else if (ResourcePermission.CAPS_PERMISSION.equals(reqResource.getType())) {
                if (reqResource.getResourceID() > 0) {
                    qu.add(Restrictions.eq("requestCapabilities.id",
                            Long.valueOf(reqResource.getResourceID())));
                }
                if (reqResource.getResourceName() != null) {
                    qu.createCriteria("requestCapabilities")
                            .add(Restrictions.eq("capabilities", reqResource.getResourceName()));
                }
            } else {
                this.logger.warn("Unable to provide free times because resource type " + reqResource.getType()
                        + " is not understood.");
                return response;
            }

            List<UserClass> uc = new ArrayList<UserClass>();
            for (UserAssociation assoc : user.getUserAssociations())
                uc.add(assoc.getUserClass());

            /* The permission user class must be active and bookable. */
            qu.createCriteria("userClass").add(Restrictions.eq("active", Boolean.TRUE))
                    .add(Restrictions.eq("bookable", Boolean.TRUE));
            qu.add(Restrictions.in("userClass", uc));

            /* Add order in case we need to count in range, latest first. */
            qu.addOrder(Order.desc("startTime"));

            @SuppressWarnings("unchecked")
            List<ResourcePermission> rpList = qu.list();
            if (rpList.size() == 1) {
                /* One permission so good to go. */
                perm = rpList.get(0);
            } else if (rpList.size() > 1) {
                Date rsd = reqStart.getTime();
                Date red = reqEnd.getTime();
                /* Multiple permissions so we take the permission in time range. */
                for (ResourcePermission rp : rpList) {
                    if (rp.getStartTime().before(rsd) && rp.getExpiryTime().after(rsd)
                            || rp.getStartTime().before(red) && rp.getExpiryTime().after(red)
                            || rp.getStartTime().after(rsd) && rp.getExpiryTime().before(red)) {
                        perm = rp;
                        break;
                    }
                }

                /* Nothing in range so it doesn't matter which resource we give. */
                if (perm == null)
                    perm = rpList.get(0);
            }
        }

        /* If no permission is specified, either it doesn't exist or it wasn't
         * specified. Either way, we can't provide any information. */
        if (perm == null) {
            this.logger
                    .warn("Unable to provide free times because no permission or resource has been specified "
                            + "or found to provide the free times of.");
            return response;
        }

        /* There is a permission, but the user doesn't have it. */
        if (!this.checkPermission(user, perm)) {
            this.logger.warn("Unable to provide free times to user " + user.getNamespace() + ':'
                    + user.getName() + " because they do not have permission " + perm.getId() + ".");
            return response;
        }

        /* ----------------------------------------------------------------
         * -- Populate the response with permission parameters.          --
         * ---------------------------------------------------------------- */
        permission.setPermissionID(perm.getId().intValue());
        resource.setType(perm.getType());
        if (ResourcePermission.RIG_PERMISSION.equals(perm.getType())) {
            Rig rig = perm.getRig();
            if (rig == null) {
                this.logger.warn("Unable to provide free times because the rig permission with ID="
                        + perm.getId() + " is not set with a rig.");
                return response;
            }
            resource.setResourceID(rig.getId().intValue());
            resource.setResourceName(rig.getName());
        } else if (ResourcePermission.TYPE_PERMISSION.equals(perm.getType())) {
            RigType rigType = perm.getRigType();
            if (rigType == null) {
                this.logger.warn("Unable to provide free times because the rig type permission with ID="
                        + perm.getId() + " is not set with a rig type.");
                return response;
            }
            resource.setResourceID(rigType.getId().intValue());
            resource.setResourceName(rigType.getName());
        } else if (ResourcePermission.CAPS_PERMISSION.equals(perm.getType())) {
            RequestCapabilities caps = perm.getRequestCapabilities();
            if (caps == null) {
                this.logger.warn(
                        "Unable to provide free times because the request capabilities permission with ID="
                                + perm.getId() + " is not set with a request capabilities.");
                return response;
            }
            resource.setResourceID(caps.getId().intValue());
            resource.setResourceName(caps.getCapabilities());
        } else if (ResourcePermission.CONSUMER_PERMISSION.equalsIgnoreCase(perm.getType())) {
            resource.setResourceID(-1); // Faking ID.
            resource.setResourceName("Fake");
        } else {
            this.logger.warn("Unable to provide free times because the permission with ID=" + perm.getId()
                    + " has type '" + perm.getType() + "' which is not understood.");
            return response;
        }

        BookingsPeriod period = new BookingsServiceImpl().getFreeBookings(reqStart, reqEnd, perm, db);
        for (BookingSlot slot : period.getSlots()) {
            TimePeriodType tp = new TimePeriodType();
            tp.setStartTime(slot.getStart());
            tp.setEndTime(slot.getEnd());

            BookingSlotType bst = new BookingSlotType();
            bst.setSlot(tp);
            bst.setState(SlotState.Factory.fromValue(slot.getState()));

            slots.addBookingSlot(bst);
        }
    } finally {
        db.close();
    }

    return response;
}

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

License:Open Source License

/**
 * Gets all the academic permissions for the specified user. 
 * /*w  w w . j  a va 2s  . c  o  m*/
 * @param user user to find permissions for 
 * @return list of academic permissions 
 */
@SuppressWarnings("unchecked")
public List<AcademicPermission> getByUser(User user) {
    Criteria cri = this.session.createCriteria(AcademicPermission.class);
    cri.add(Restrictions.eq("user", user));
    return cri.list();
}

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

License:Open Source License

/**
 * Gets all the academic permissions for the specified user class.
 * /*ww w  .j  a  va 2  s. c  o m*/
 * @param uClass user class to find permissions for
 * @return list of academic permissions 
 */
@SuppressWarnings("unchecked")
public List<AcademicPermission> getByUserClass(UserClass uClass) {
    Criteria cri = this.session.createCriteria(AcademicPermission.class);
    cri.add(Restrictions.eq("userClass", uClass));
    return cri.list();
}

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

License:Open Source License

/**
 * Gets the academic permission for the specified user and user class. If it is
 * not found <code>null</code>. If more than one permission is found, the first
 * is used./*from w w w . ja  v a2s .co  m*/
 * 
 * @param user user constraint
 * @param uClass user class constraint
 * @return permission or <code>null</code>
 */
@SuppressWarnings("unchecked")
public AcademicPermission getForUserAndUserClass(User user, UserClass uClass) {
    Criteria cri = this.session.createCriteria(AcademicPermission.class);
    cri.add(Restrictions.eq("user", user)).add(Restrictions.eq("userClass", uClass));

    List<AcademicPermission> perms = cri.list();
    if (perms.size() == 0) {
        return null;
    } else if (perms.size() > 1) {
        this.logger.warn("Found more than one academic permission for the user " + user.getName() + " and "
                + "user class " + uClass.getName() + ". Using the first found permission.");
    }
    return perms.get(0);
}

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

License:Open Source License

/**
 * Returns the configuration values with the specified configuration key.
 * /* ww  w .  j  a  va 2s  .co m*/
 * @param key configuration key
 * @return list of matching configuration values
 */
@SuppressWarnings("unchecked")
public List<Config> getConfig(String key) {
    Criteria crit = this.session.createCriteria(this.clazz);
    crit.add(Restrictions.eq("key", key));
    return crit.list();
}

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

License:Open Source License

/**
 * Find the request capabilities entity which match the specified capabilities.
 * <code>null</code> is returned if none is found.
 *  /*from w  w w  . j a  v  a  2  s  .  c  o m*/
 * @param capabilities capabilities to find 
 * @return capabilities or null
 */
public RequestCapabilities findCapabilites(String capabilities) {
    Criteria cri = this.session.createCriteria(RequestCapabilities.class);
    cri.add(Restrictions.eq("capabilities", new Capabilities(capabilities).asCapabilitiesString()));
    return (RequestCapabilities) cri.uniqueResult();
}

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

License:Open Source License

/**
 * Find the rig capabilities entity which match the specified capabilities.
 * <code>null</code> is returned if none is found.
 *  /*from   w w  w  .  j  a  va 2s.com*/
 * @param capabilities capabilities to find 
 * @return capabilities or null
 */
public RigCapabilities findCapabilites(String capabilities) {
    Criteria cri = this.session.createCriteria(RigCapabilities.class);
    cri.add(Restrictions.eq("capabilities", new Capabilities(capabilities).asCapabilitiesString()));
    return (RigCapabilities) cri.uniqueResult();
}

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

License:Open Source License

/**
 * Finds the rig with the specified name or <code>null</code> if not
 * found./*from www .ja  v a2s .  c om*/
 * 
 * @param name rig name
 * @return rig or null if not found
 */
public Rig findByName(String name) {
    Criteria cri = this.session.createCriteria(Rig.class);
    cri.add(Restrictions.eq("name", name));
    return (Rig) cri.uniqueResult();
}

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

License:Open Source License

/**
 * Returns the list of free rigs in a specified rig type. Free rigs are 
 * denoted by the following flags:/*from  w  w w  . j  a va2s .c  om*/
 * <ol>
 *  <li>active - True<li>
 *  <li>online - True<li>
 *  <li>in_session - False<li>
 * <ol>
 * 
 * @param type rig type
 * @return list of free rigs
 */
@SuppressWarnings("unchecked")
public List<Rig> findFreeinType(RigType type) {
    Criteria cri = this.session.createCriteria(Rig.class);
    cri.add(Restrictions.eq("rigType", type));
    cri.add(Restrictions.eq("active", true));
    cri.add(Restrictions.eq("online", true));
    cri.add(Restrictions.eq("inSession", false));
    return cri.list();
}

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. The logs are ordered by their time stamp with the earliest log 
 * first.//from  w ww  .  j  a  va  2  s . com
 * 
 * @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> findLogs(Rig rig, 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.addOrder(Order.asc("timeStamp"));
    return cri.list();
}