Example usage for org.hibernate Criteria setFirstResult

List of usage examples for org.hibernate Criteria setFirstResult

Introduction

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

Prototype

public Criteria setFirstResult(int firstResult);

Source Link

Document

Set the first result to be retrieved.

Usage

From source file:au.edu.uts.eng.remotelabs.schedserver.reports.intf.Reports.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*from  w ww.  j  a v a 2s . c om*/
public QuerySessionAccessResponse querySessionAccess(final QuerySessionAccess querySessionAccess) {
    /* Request parameters. */
    final QuerySessionAccessType request = querySessionAccess.getQuerySessionAccess();
    final QueryFilterType filter = request.getQuerySelect();

    String debug = "Received " + this.getClass().getSimpleName()
            + "#querySessionAccess with params: select operator=" + filter.getOperator() + ", type="
            + filter.getTypeForQuery().toString() + ", like=" + filter.getQueryLike();
    if (request.getStartTime() != null)
        debug += ", start=" + request.getStartTime().getTime();
    if (request.getEndTime() != null)
        debug += ", end=" + request.getEndTime().getTime();
    this.logger.debug(debug);

    /* Response parameters. */
    final QuerySessionAccessResponse response = new QuerySessionAccessResponse();
    final QuerySessionAccessResponseType respType = new QuerySessionAccessResponseType();
    final PaginationType page = new PaginationType();
    page.setNumberOfPages(1);
    page.setPageLength(0);
    page.setPageNumber(1);
    respType.setPagination(page);
    response.setQuerySessionAccessResponse(respType);

    final org.hibernate.Session db = DataAccessActivator.getNewSession();
    try {
        RequestorType uid = request.getRequestor();
        final User requestor = this.getUserFromUserID(uid, db);
        if (requestor == null) {
            this.logger.info("Unable to generate report because the user has not been found. Supplied "
                    + "credentials ID=" + uid.getUserID() + ", namespace=" + uid.getUserNamespace() + ", "
                    + "name=" + uid.getUserName() + '.');
            return response;
        }

        /* We are only interested in sessions that are complete. */
        Criteria query = db.createCriteria(Session.class).add(Restrictions.eq("active", Boolean.FALSE))
                .add(Restrictions.isNotNull("removalTime")).addOrder(Order.asc("requestTime"));

        /* If the user has requested a time period, only add that to query. */
        if (request.getStartTime() != null)
            query.add(Restrictions.ge("requestTime", request.getStartTime().getTime()));
        if (request.getEndTime() != null)
            query.add(Restrictions.le("requestTime", request.getEndTime().getTime()));

        if (request.getPagination() != null) {
            /* Add the requested pagination. */
            final PaginationType pages = request.getPagination();
            final int noPages = pages.getNumberOfPages();
            final int pageNumber = pages.getPageNumber();
            final int pageLength = pages.getPageLength();

            if (noPages > 1)
                query.setMaxResults(pageLength);
            if (pageNumber > 1)
                query.setFirstResult((pageNumber - 1) * pageLength);
        }

        if (filter.getTypeForQuery() == TypeForQuery.RIG) {
            /* Only administrators can do rig queries. */
            if (!User.ADMIN.equals(requestor.getPersona())) {
                this.logger.warn("Cannot provide session report for user '" + requestor.qName()
                        + "' because their persona '" + requestor.getPersona()
                        + "' does not allow rig reporting.");
                return response;
            }

            query.add(Restrictions.eq("assignedRigName", filter.getQueryLike()));
        } else if (filter.getTypeForQuery() == TypeForQuery.RIG_TYPE) {
            /* Only administrators can do rig type queries. */
            if (!User.ADMIN.equals(requestor.getPersona())) {
                this.logger.warn("Cannot provide session report for user '" + requestor.qName()
                        + "' because their persona '" + requestor.getPersona()
                        + "' does not allow rig type reporting.");
                return response;
            }

            final RigType rigType = new RigTypeDao(db).findByName(filter.getQueryLike());
            if (rigType == null) {
                this.logger.warn("Cannot provide session report because rig type '" + filter.getQueryLike()
                        + "' not found.");
                return response;
            }

            query.createCriteria("rig").add(Restrictions.eq("rigType", rigType));
        } else if (filter.getTypeForQuery() == TypeForQuery.REQUEST_CAPABILITIES) {
            /* Only administrators can do request capabilities queries. */
            if (!User.ADMIN.equals(requestor.getPersona())) {
                this.logger.warn("Cannot provide session report for user '" + requestor.qName()
                        + "' because their persona '" + requestor.getPersona()
                        + "' does not allow request capabilities reporting.");
                return response;
            }

            final RequestCapabilities reqCaps = new RequestCapabilitiesDao(db)
                    .findCapabilites(filter.getQueryLike());
            if (reqCaps == null) {
                this.logger.warn("Cannot provide session report because request capabilities '"
                        + filter.getQueryLike() + "' not found.");
                return response;
            }

            List<Rig> capRigs = new ArrayList<Rig>();
            for (MatchingCapabilities match : reqCaps.getMatchingCapabilitieses()) {
                capRigs.addAll(match.getRigCapabilities().getRigs());
            }

            if (capRigs.size() == 0) {
                this.logger.warn(
                        "Cannot provide session report because there are no rigs with request capabilities '"
                                + reqCaps.getCapabilities() + "'.");
                return response;
            }

            query.add(Restrictions.in("rig", capRigs));
        } else if (filter.getTypeForQuery() == TypeForQuery.USER_CLASS) {
            final UserClass userClass = new UserClassDao(db).findByName(filter.getQueryLike());
            if (userClass == null) {
                this.logger.warn("Cannot provide session report because user class '" + filter.getQueryLike()
                        + "' was not found.");
                return response;
            }

            if (User.ACADEMIC.equals(requestor.getPersona())) {
                /* An academic may only generate reports for the classes 
                 * they have have reporting permission in. */
                boolean hasPerm = false;

                final Iterator<AcademicPermission> it = requestor.getAcademicPermissions().iterator();
                while (it.hasNext()) {
                    final AcademicPermission ap = it.next();
                    if (ap.getUserClass().getId().equals(userClass.getId()) && ap.isCanGenerateReports()) {
                        hasPerm = true;
                        break;
                    }
                }

                if (!hasPerm) {
                    this.logger.info("Unable to generate report for user class " + userClass.getName()
                            + " because the user " + requestor.qName()
                            + " does not own or have permission to report it.");
                    return response;
                }

                this.logger.debug("Academic " + requestor.qName()
                        + " has permission to generate report from user class " + userClass.getName() + '.');
            } else if (!User.ADMIN.equals(requestor.getPersona())) {
                this.logger.warn("Cannot provide a user session report for user '" + requestor.qName()
                        + "' because their persona '" + requestor.getPersona() + "' does not allow reporting.");
                return response;
            }

            query.createCriteria("resourcePermission").add(Restrictions.eq("userClass", userClass));
        } else if (filter.getTypeForQuery() == TypeForQuery.USER) {
            final User user = new UserDao(db).findByQName(filter.getQueryLike());
            if (user == null) {
                this.logger.warn("Cannot provide session report because user  '" + filter.getQueryLike()
                        + "' was not found.");
                return response;
            }

            if (User.ACADEMIC.equals(requestor.getPersona())) {
                /* The report can only contain sessions originating from the user
                 * classes the academic has reporting permission in. */
                List<ResourcePermission> perms = new ArrayList<ResourcePermission>();

                final Iterator<AcademicPermission> it = requestor.getAcademicPermissions().iterator();
                while (it.hasNext()) {
                    final AcademicPermission ap = it.next();
                    if (!ap.isCanGenerateReports())
                        continue;

                    perms.addAll(ap.getUserClass().getResourcePermissions());
                }

                if (perms.size() == 0) {
                    this.logger.info("Unable to generate report for user " + user.qName() + " because the user "
                            + requestor.qName() + " does not own or have permission to report "
                            + "on any of the users permissions.");
                    return response;
                }

                query.add(Restrictions.in("resourcePermission", perms));
            } else if (!User.ADMIN.equals(requestor.getPersona())) {
                this.logger.warn("Cannot provide a user session report for user '" + requestor.qName()
                        + "' because their persona '" + requestor.getPersona() + "' does not allow reporting.");
                return response;
            }

            query.add(Restrictions.eq("user", user));
        } else {
            this.logger.error("Cannot provide a session report because the query type '"
                    + filter.getTypeForQuery().toString() + "' was not understood.");
            return response;
        }

        for (final Session s : (List<Session>) query.list()) {
            final AccessReportType report = new AccessReportType();

            /* Set user who was in session. */
            final RequestorType sUser = new RequestorType();
            final UserNSNameSequence nsSequence = new UserNSNameSequence();
            nsSequence.setUserName(s.getUserName());
            nsSequence.setUserNamespace(s.getUserNamespace());
            sUser.setRequestorNSName(nsSequence);
            sUser.setUserQName(s.getUserNamespace() + ':' + s.getUserName());
            report.setUser(sUser);

            /* User class. */
            if (s.getResourcePermission() != null)
                report.setUserClass(s.getResourcePermission().getUserClass().getName());

            /* Rig details. */
            report.setRigName(s.getAssignedRigName());
            if (s.getRig() != null)
                report.setRigType(s.getRig().getRigType().getName());

            /* Session start. */
            Calendar cal = Calendar.getInstance();
            cal.setTime(s.getRequestTime());
            report.setQueueStartTime(cal);

            /* Session timings. */
            if (s.getAssignmentTime() != null) {
                final int queueD = (int) ((s.getAssignmentTime().getTime() - s.getRequestTime().getTime())
                        / 1000);
                report.setQueueDuration(queueD);
                cal = Calendar.getInstance();
                cal.setTime(s.getAssignmentTime());
                report.setSessionStartTime(cal);
                final int sessionD = (int) ((s.getRemovalTime().getTime() - s.getAssignmentTime().getTime())
                        / 1000);
                report.setSessionDuration(sessionD);
            } else {
                final int queueD = (int) ((s.getRemovalTime().getTime() - s.getRequestTime().getTime()) / 1000);
                report.setQueueDuration(queueD);

                cal = Calendar.getInstance();
                cal.setTime(s.getRemovalTime());
                report.setSessionStartTime(cal);
                report.setSessionDuration(0);
            }

            /* Session end. */
            cal = Calendar.getInstance();
            cal.setTime(s.getRemovalTime());
            report.setSessionEndTime(cal);
            report.setReasonForTermination(s.getRemovalReason());

            respType.addAccessReportData(report);
        }

    } finally {
        db.close();
    }

    return response;
}

From source file:au.org.theark.admin.model.dao.AdminDao.java

License:Open Source License

public List<ArkModule> searchPageableArkModules(ArkModule arkModuleCriteria, int first, int count) {
    Criteria criteria = buildArkModuleCriteria(arkModuleCriteria);
    criteria.setFirstResult(first);
    criteria.setMaxResults(count);/* w w  w. j  a  v  a  2 s .c o m*/
    List<ArkModule> list = criteria.list();

    return list;
}

From source file:au.org.theark.admin.model.dao.AdminDao.java

License:Open Source License

public List<ArkFunction> searchPageableArkFunctions(ArkFunction arkFunctionCriteria, int first, int count) {
    Criteria criteria = buildArkFunctionCriteria(arkFunctionCriteria);
    criteria.setFirstResult(first);
    criteria.setMaxResults(count);//from  w  w  w.  j  av a2s  .  c  o  m
    List<ArkFunction> list = criteria.list();

    return list;
}

From source file:au.org.theark.admin.model.dao.AdminDao.java

License:Open Source License

public List<ArkRoleModuleFunctionVO> searchPageableArkRoleModuleFunctionVO(
        ArkRoleModuleFunctionVO arkRoleModuleFunctionVo, int first, int count) {
    Criteria criteria = buildArkRoleModuleFunctionVoCriteria(arkRoleModuleFunctionVo);
    criteria.setFirstResult(first);
    criteria.setMaxResults(count);/*  ww w.j  a v  a2s. c om*/

    // Restrict to NOT show Super Administrator (safety)
    // NOTE arpt alias set in buildarkRoleModuleFunctionVoCriteria 
    criteria.add((Restrictions.ne("arpt.arkRole",
            getArkRoleByName(au.org.theark.core.security.RoleConstants.ARK_ROLE_SUPER_ADMINISTATOR))));

    List<ArkRoleModuleFunctionVO> list = criteria.list();
    return list;
}

From source file:au.org.theark.admin.model.dao.AdminDao.java

License:Open Source License

public List<ArkModuleFunction> searchPageableArkModuleFunctions(ArkModuleFunction arkModuleFunctionCriteria,
        int first, int count) {
    Criteria criteria = buildArkModuleFunctionCriteria(arkModuleFunctionCriteria);
    criteria.setFirstResult(first);
    criteria.setMaxResults(count);//from w  w  w .j  a  v a 2  s .  c  om
    List<ArkModuleFunction> list = criteria.list();
    return list;
}

From source file:au.org.theark.admin.model.dao.AdminDao.java

License:Open Source License

public List<ArkRole> searchPageableArkRoles(ArkRole arkRoleCriteria, int first, int count) {
    Criteria criteria = buildArkRoleCriteria(arkRoleCriteria);
    criteria.setFirstResult(first);
    criteria.setMaxResults(count);// ww w . ja va 2  s . co  m
    List<ArkRole> list = criteria.list();
    return list;
}

From source file:au.org.theark.admin.model.dao.AdminDao.java

License:Open Source License

public List<ArkModuleRole> searchPageableArkModuleRoles(ArkModuleRole arkModulRoleCriteria, int first,
        int count) {
    Criteria criteria = buildArkModuleRoleCriteria(arkModulRoleCriteria);
    criteria.setFirstResult(first);
    criteria.setMaxResults(count);/*from w w  w.ja  v a  2  s.  co  m*/
    List<ArkModuleRole> list = criteria.list();
    return list;
}

From source file:au.org.theark.core.dao.StudyDao.java

License:Open Source License

public List<SubjectVO> searchPageableSubjects(SubjectVO subjectVoCriteria, int first, int count) {
    Criteria criteria = buildGeneralSubjectCriteria(subjectVoCriteria);
    criteria.setFirstResult(first);
    criteria.setMaxResults(count);/*from   ww  w.j  a  v  a  2 s.  c o m*/

    List<Long> longs = criteria.list();

    List<LinkSubjectStudy> list = new ArrayList<LinkSubjectStudy>();//criteria.list();

    for (Long l : longs) {
        try {
            list.add(getLinkSubjectStudy(l));
        } catch (EntityNotFoundException e) {
            e.printStackTrace();
        }
    }

    List<SubjectVO> subjectVOList = new ArrayList<SubjectVO>();

    // TODO analyse
    for (Iterator iterator = list.iterator(); iterator.hasNext();) {

        LinkSubjectStudy linkSubjectStudy = (LinkSubjectStudy) iterator.next();
        // Place the LinkSubjectStudy instance into a SubjectVO and add the
        // SubjectVO into a List
        SubjectVO subject = new SubjectVO();
        subject.setLinkSubjectStudy(linkSubjectStudy);
        // Person person = subject.getLinkSubjectStudy().getPerson();
        subject.setSubjectUID(linkSubjectStudy.getSubjectUID());
        subjectVOList.add(subject);
    }
    return subjectVOList;
}

From source file:au.org.theark.lims.model.dao.BioCollectionDao.java

License:Open Source License

public List<BioCollection> searchPageableBioCollections(BioCollection bioCollectionCriteria, int first,
        int count) {
    Criteria criteria = buildBioCollectionCriteria(bioCollectionCriteria);
    criteria.setFirstResult(first);
    criteria.setMaxResults(count);//from   ww w  . j av  a 2 s .c  o  m
    List<BioCollection> list = criteria.list();

    return list;
}

From source file:au.org.theark.lims.model.dao.BiospecimenDao.java

License:Open Source License

public List<Biospecimen> searchPageableBiospecimens(Biospecimen biospecimenCriteria, int first, int count) {
    Criteria criteria = buildBiospecimenCriteria(biospecimenCriteria);
    criteria.setFirstResult(first);
    criteria.setMaxResults(count);//from   w  ww.  j  ava2s .  c  o  m
    criteria.addOrder(Order.asc("id"));
    List<Biospecimen> list = criteria.list();
    return list;
}