Example usage for org.hibernate Criteria createCriteria

List of usage examples for org.hibernate Criteria createCriteria

Introduction

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

Prototype

public Criteria createCriteria(String associationPath) throws HibernateException;

Source Link

Document

Create a new Criteria, "rooted" at the associated entity.

Usage

From source file:es.sm2.openppm.core.dao.TimesheetDAO.java

License:Open Source License

/**
 * Find time sheets of the resource//www  . j  a v  a2  s  . co m
 * 
 * @param employee
 * @param initDate
 * @param endDate
 * @param joins
 * @param project
 * @param minStatus
 * @param maxStatus
 * @param filterUser
 * @param statusResource
  * @param settings
 * @return
 */
@SuppressWarnings("unchecked")
public List<Timesheet> findByCriteria(Employee employee, Date initDate, Date endDate, List<String> joins,
        Project project, String minStatus, String maxStatus, Employee filterUser, String statusResource,
        boolean includeClosed, HashMap<String, String> settings) {

    Criteria crit = getSession().createCriteria(getPersistentClass())
            .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
            .add(Restrictions.eq(Timesheet.EMPLOYEE, employee))
            .add(Restrictions.eq(Timesheet.INITDATE, initDate))
            .add(Restrictions.eq(Timesheet.ENDDATE, endDate));

    if (minStatus != null && maxStatus != null) {

        Disjunction disjunction = Restrictions.disjunction();
        disjunction.add(Restrictions.eq(Timesheet.STATUS, minStatus));
        disjunction.add(Restrictions.eq(Timesheet.STATUS, maxStatus));

        if (Constants.TIMESTATUS_APP0.equals(minStatus) && (Constants.TIMESTATUS_APP2.equals(maxStatus)
                || Constants.TIMESTATUS_APP3.equals(maxStatus))) {

            disjunction.add(Restrictions.eq(Timesheet.STATUS, Constants.TIMESTATUS_APP1));
        }

        if (Constants.TIMESTATUS_APP3.equals(maxStatus) && (Constants.TIMESTATUS_APP0.equals(minStatus)
                || Constants.TIMESTATUS_APP1.equals(minStatus))) {

            disjunction.add(Restrictions.eq(Timesheet.STATUS, Constants.TIMESTATUS_APP2));
        }

        //TODO 21/04/2015 - jordi.ripoll- tarea 1095 -pendiente de confirmacion
        // Last level be approved hours app1 by setting
        //
        //            String lastLevelForApprove = SettingUtil.getString(settings, Settings.SETTING_LAST_LEVEL_FOR_APPROVE_SHEET,
        //                    Settings.DEFAULT_LAST_LEVEL_FOR_APPROVE_SHEET);
        //
        //            if ((filterUser != null && lastLevelForApprove.equals(String.valueOf(filterUser.getResourceprofiles().getIdProfile()))) &&
        //                    (settings != null && SettingUtil.getBoolean(settings, GeneralSetting.LAST_LEVEL_APPROVED_APP1))) {
        //
        //                disjunction.add(Restrictions.eq(Timesheet.STATUS, Constants.TIMESTATUS_APP1));
        //            }

        crit.add(disjunction);
    }

    Criteria critEmployee = crit.createCriteria(Timesheet.EMPLOYEE);
    critEmployee.createCriteria(Employee.CONTACT);

    Criteria critActivity = crit.createCriteria(Timesheet.PROJECTACTIVITY);

    Criteria critMembers = critActivity.createCriteria(Projectactivity.TEAMMEMBERS)
            .add(Restrictions.eq(Teammember.EMPLOYEE, employee));

    if (ValidateUtil.isNotNull(statusResource)) {
        critMembers.add(Restrictions.eq(Teammember.STATUS, statusResource));
    }

    Criteria critProject = critActivity.createCriteria(Projectactivity.PROJECT);

    if (!includeClosed) {
        critProject.add(Restrictions.and(Restrictions.ne(Project.STATUS, Constants.STATUS_CLOSED),
                Restrictions.ne(Project.STATUS, Constants.STATUS_ARCHIVED)));
    }

    if (project != null) {
        critProject.add(Restrictions.eq(Project.IDPROJECT, project.getIdProject()));
    } else if (filterUser != null) {

        // Filter by User. Settings by company for last approval.
        Resourceprofiles profile = filterUser.getResourceprofiles();

        if (profile.getIdProfile() == Constants.ROLE_FM) {

            critProject.add(Restrictions.eq(Project.EMPLOYEEBYFUNCTIONALMANAGER, filterUser));
        } else if (profile.getIdProfile() == Constants.ROLE_PMO) {

            critProject.add(Restrictions.eq(Project.PERFORMINGORG, filterUser.getPerformingorg()));
        }
    }

    addJoins(crit, joins);

    return crit.list();
}

From source file:es.sm2.openppm.core.dao.TimesheetDAO.java

License:Open Source License

/**
 * Is in status//w w  w . j av a 2  s  .c o m
 * @param idEmployee
 * @param id
 * @param initDate
 * @param endDate
 * @param status
 * @param filterUser 
 * @return
 */
public boolean isStatusResource(Integer idEmployee, Integer id, Date initDate, Date endDate, String status,
        Employee filterUser) {

    Criteria crit = getSession().createCriteria(getPersistentClass()).setProjection(Projections.rowCount())
            .add(Restrictions.eq(Timesheet.STATUS, status)).add(Restrictions.eq(Timesheet.INITDATE, initDate))
            .add(Restrictions.eq(Timesheet.ENDDATE, endDate))
            .add(Restrictions.eq(Timesheet.EMPLOYEE, new Employee(idEmployee)));

    if (Constants.TIMESTATUS_APP1.equals(status)) {

        if (id.equals(0)) {
            crit.add(Restrictions.isNull(Timesheet.PROJECTACTIVITY));
        } else {
            crit.createCriteria(Timesheet.PROJECTACTIVITY)
                    .add(Restrictions.eq(Projectactivity.PROJECT, new Project(id)));
        }
    } else if (Constants.TIMESTATUS_APP2.equals(status)) {
        Criteria critFilter = crit.createCriteria(Timesheet.PROJECTACTIVITY)
                .createCriteria(Projectactivity.PROJECT);

        // Filter by User. Settings by company for last approval.

        Resourceprofiles profile = filterUser.getResourceprofiles();

        if (profile.getIdProfile() == Constants.ROLE_FM) {

            critFilter.add(Restrictions.eq(Project.EMPLOYEEBYFUNCTIONALMANAGER, filterUser));
        } else if (profile.getIdProfile() == Constants.ROLE_PM) {

            critFilter.add(Restrictions.eq(Project.EMPLOYEEBYPROJECTMANAGER, filterUser));
        } else {

            critFilter.add(Restrictions.eq(Project.PERFORMINGORG, filterUser.getPerformingorg()));
        }
    }

    Integer count = (Integer) crit.uniqueResult();

    return (count != null && count > 0);
}

From source file:es.sm2.openppm.core.dao.TimesheetDAO.java

License:Open Source License

/**
 * Is time status// w ww  .ja  v a 2s  .  c o m
 * @param idEmployee
 * @param initDate
 * @param endDate
 * @param status
 * @param filterUser
 * @param inOperation
 * @return
 */
public boolean isTimeStatus(Integer idEmployee, Date initDate, Date endDate, String status, Employee filterUser,
        boolean inOperation) {

    boolean isStatus = false;

    Criteria crit = getSession().createCriteria(getPersistentClass()).setProjection(Projections.rowCount())
            .add(Restrictions.eq(Timesheet.STATUS, status)).add(Restrictions.eq(Timesheet.INITDATE, initDate))
            .add(Restrictions.eq(Timesheet.ENDDATE, endDate))
            .add(Restrictions.eq(Timesheet.EMPLOYEE, new Employee(idEmployee)));

    if (inOperation) {
        crit.add(Restrictions.isNotNull(Timesheet.OPERATION));
    } else {
        Resourceprofiles profile = filterUser.getResourceprofiles();

        Criterion restriction = null;

        if (profile.getIdProfile() == Constants.ROLE_FM) {
            restriction = Restrictions.eq(Project.EMPLOYEEBYFUNCTIONALMANAGER, filterUser);
        } else if (profile.getIdProfile() == Constants.ROLE_PM) {
            restriction = Restrictions.eq(Project.EMPLOYEEBYPROJECTMANAGER, filterUser);
        } else {
            restriction = Restrictions.eq(Project.PERFORMINGORG, filterUser.getPerformingorg());
        }

        Criteria critFilter = crit.createCriteria(Timesheet.PROJECTACTIVITY)
                .createCriteria(Projectactivity.PROJECT);
        critFilter.add(restriction);
    }

    isStatus = (Integer) crit.uniqueResult() > 0;

    if (!isStatus && !inOperation) {
        isStatus = isTimeStatus(idEmployee, initDate, endDate, status, filterUser, true);
    }

    return isStatus;
}

From source file:es.sm2.openppm.core.dao.TimesheetDAO.java

License:Open Source License

/**
 * Get Hours resource/*from w w  w .jav a  2 s.c  om*/
 * @param idEmployee
 * @param id
 * @param initDate
 * @param endDate
 * @param minStatus
 * @param maxStatus
 * @return
 */
public double getHoursResource(Integer idEmployee, Integer id, Date initDate, Date endDate, String minStatus,
        Employee filterUser) {

    ProjectionList proList = Projections.projectionList();
    proList.add(Projections.sum(Timesheet.HOURSDAY1));
    proList.add(Projections.sum(Timesheet.HOURSDAY2));
    proList.add(Projections.sum(Timesheet.HOURSDAY3));
    proList.add(Projections.sum(Timesheet.HOURSDAY4));
    proList.add(Projections.sum(Timesheet.HOURSDAY5));
    proList.add(Projections.sum(Timesheet.HOURSDAY6));
    proList.add(Projections.sum(Timesheet.HOURSDAY7));

    Criteria crit = getSession().createCriteria(getPersistentClass()).setProjection(proList)
            .add(Restrictions.disjunction().add(Restrictions.eq(Timesheet.STATUS, Constants.TIMESTATUS_APP1))
                    .add(Restrictions.eq(Timesheet.STATUS, Constants.TIMESTATUS_APP2))
                    .add(Restrictions.eq(Timesheet.STATUS, Constants.TIMESTATUS_APP3)))
            .add(Restrictions.eq(Timesheet.INITDATE, initDate)).add(Restrictions.eq(Timesheet.ENDDATE, endDate))
            .add(Restrictions.eq(Timesheet.EMPLOYEE, new Employee(idEmployee)));

    if (Constants.TIMESTATUS_APP1.equals(minStatus)) {
        crit.createCriteria(Timesheet.PROJECTACTIVITY)
                .add(Restrictions.eq(Projectactivity.PROJECT, new Project(id)));
    } else if (Constants.TIMESTATUS_APP2.equals(minStatus)) {

        // Filter by User. Settings by company for last approval.

        Criteria critFilter = crit.createCriteria(Timesheet.PROJECTACTIVITY)
                .createCriteria(Projectactivity.PROJECT);

        Resourceprofiles profile = filterUser.getResourceprofiles();

        if (profile.getIdProfile() == Constants.ROLE_FM) {

            critFilter.add(Restrictions.eq(Project.EMPLOYEEBYFUNCTIONALMANAGER, filterUser));
        } else if (profile.getIdProfile() == Constants.ROLE_PMO) {

            critFilter.add(Restrictions.eq(Project.PERFORMINGORG, filterUser.getPerformingorg()));
        }
    }
    Object[] hoursList = (Object[]) crit.uniqueResult();

    double hours = 0;

    if (hoursList != null) {
        hours += (hoursList[0] == null ? 0 : (Double) hoursList[0]);
        hours += (hoursList[1] == null ? 0 : (Double) hoursList[1]);
        hours += (hoursList[2] == null ? 0 : (Double) hoursList[2]);
        hours += (hoursList[3] == null ? 0 : (Double) hoursList[3]);
        hours += (hoursList[4] == null ? 0 : (Double) hoursList[4]);
        hours += (hoursList[5] == null ? 0 : (Double) hoursList[5]);
        hours += (hoursList[6] == null ? 0 : (Double) hoursList[6]);
    }

    return hours;
}

From source file:es.sm2.openppm.core.dao.TimesheetDAO.java

License:Open Source License

public double getHoursResourceInDates(Project project, Employee member, Date since, Date until,
        Integer idResourcePool, Operation operation, Projectactivity activity) {

    Criteria crit = getSession().createCriteria(getPersistentClass());

    Criteria employeeCrit = null;/*from w w w.j  av  a  2s.  c o m*/

    if (member != null || idResourcePool != null) {
        employeeCrit = crit.createCriteria(Timesheet.EMPLOYEE);
    }

    if (idResourcePool != null) {
        employeeCrit.add(Restrictions.eq(Employee.RESOURCEPOOL, new Resourcepool(idResourcePool)));
    }

    if (member != null) {
        employeeCrit.add(Restrictions.idEq(member.getIdEmployee()));
    }

    if (since != null && until != null) {

        crit.add(Restrictions.disjunction().add(Restrictions.between(Timesheet.INITDATE, since, until))
                .add(Restrictions.between(Timesheet.ENDDATE, since, until))
                .add(Restrictions.and(Restrictions.le(Timesheet.INITDATE, since),
                        Restrictions.ge(Timesheet.ENDDATE, until))));
    }

    if (project != null) {

        crit.add(Restrictions.eq(Timesheet.STATUS, Constants.TIMESTATUS_APP3));

        Criteria activitiesCrit = crit.createCriteria(Timesheet.PROJECTACTIVITY)
                .add(Restrictions.eq(Projectactivity.PROJECT, project));

        // Select only timesheet whose activity is control account
        Criteria wbsnodeCrit = activitiesCrit.createCriteria(Projectactivity.WBSNODE)
                .add(Restrictions.eq(Wbsnode.ISCONTROLACCOUNT, true));
    } else if (operation != null) {

        crit.add(Restrictions.or(Restrictions.eq(Timesheet.STATUS, Constants.TIMESTATUS_APP2),
                Restrictions.eq(Timesheet.STATUS, Constants.TIMESTATUS_APP3)))
                .add(Restrictions.eq(Timesheet.OPERATION, operation));
    } else if (activity != null) {

        crit.add(Restrictions.eq(Timesheet.STATUS, Constants.TIMESTATUS_APP3))
                .add(Restrictions.eq(Timesheet.PROJECTACTIVITY, activity));
    }

    return calcHours(crit.list(), since, until);
}

From source file:es.sm2.openppm.core.dao.TimesheetDAO.java

License:Open Source License

/**
 * Get hours in status of project/*  w  ww  . j  av a 2s .  co  m*/
 *
 * @param project
 * @param status
 * @return
 */
public double getHoursResource(Project project, String status) {

    Criteria crit = getSession().createCriteria(getPersistentClass());

    crit.add(Restrictions.eq(Timesheet.STATUS, status == null ? Constants.TIMESTATUS_APP3 : status));

    Criteria activitiesCrit = crit.createCriteria(Timesheet.PROJECTACTIVITY)
            .add(Restrictions.eq(Projectactivity.PROJECT, project));

    // Select only timesheet whose activity is control account
    Criteria wbsnodeCrit = activitiesCrit.createCriteria(Projectactivity.WBSNODE)
            .add(Restrictions.eq(Wbsnode.ISCONTROLACCOUNT, true));

    return calcHours(crit.list(), null, null);
}

From source file:es.sm2.openppm.core.dao.TimesheetDAO.java

License:Open Source License

@SuppressWarnings("unchecked")
public double getHoursInProject(Project project, Date since, Date until) {

    Criteria crit = getSession().createCriteria(getPersistentClass())
            .add(Restrictions.eq(Timesheet.STATUS, Constants.TIMESTATUS_APP3));

    crit.createCriteria(Timesheet.PROJECTACTIVITY).add(Restrictions.eq(Projectactivity.PROJECT, project));

    return calcHours(crit.list(), since, until);
}

From source file:es.sm2.openppm.core.dao.TimesheetDAO.java

License:Open Source License

/**
 * Calculate Fte for inputed hours in project
 * @param project//from w w w  . jav  a 2  s  .  co  m
 * @param member
 * @param firstWeekDay
 * @param lastWeekDay
 * @return
 */
public double getHoursResource(Project project, Teammember member, Date firstWeekDay, Date lastWeekDay) {

    ProjectionList proList = Projections.projectionList();
    proList.add(Projections.sum(Timesheet.HOURSDAY1));
    proList.add(Projections.sum(Timesheet.HOURSDAY2));
    proList.add(Projections.sum(Timesheet.HOURSDAY3));
    proList.add(Projections.sum(Timesheet.HOURSDAY4));
    proList.add(Projections.sum(Timesheet.HOURSDAY5));
    proList.add(Projections.sum(Timesheet.HOURSDAY6));
    proList.add(Projections.sum(Timesheet.HOURSDAY7));

    Criteria crit = getSession().createCriteria(getPersistentClass()).setProjection(proList)
            .add(Restrictions.eq(Timesheet.STATUS, Constants.TIMESTATUS_APP3));

    crit.createCriteria(Timesheet.EMPLOYEE).createCriteria(Employee.TEAMMEMBERS)
            .add(Restrictions.idEq(member.getIdTeamMember()));

    if (firstWeekDay != null && lastWeekDay != null) {

        crit.add(Restrictions.eq(Timesheet.INITDATE, firstWeekDay))
                .add(Restrictions.eq(Timesheet.ENDDATE, lastWeekDay));
    }

    if (project == null) {
        crit.add(Restrictions.eq(Timesheet.PROJECTACTIVITY, member.getProjectactivity()));
    } else {

        crit.createCriteria(Timesheet.PROJECTACTIVITY).add(Restrictions.eq(Projectactivity.PROJECT, project));
    }

    Object[] hoursList = (Object[]) crit.uniqueResult();

    double hours = 0;

    if (hoursList != null) {
        hours += (hoursList[0] == null ? 0 : (Double) hoursList[0]);
        hours += (hoursList[1] == null ? 0 : (Double) hoursList[1]);
        hours += (hoursList[2] == null ? 0 : (Double) hoursList[2]);
        hours += (hoursList[3] == null ? 0 : (Double) hoursList[3]);
        hours += (hoursList[4] == null ? 0 : (Double) hoursList[4]);
        hours += (hoursList[5] == null ? 0 : (Double) hoursList[5]);
        hours += (hoursList[6] == null ? 0 : (Double) hoursList[6]);
    }

    return hours;
}

From source file:es.sm2.openppm.core.dao.TimesheetDAO.java

License:Open Source License

/**
 * //  w ww  .j av  a  2  s .  c  o  m
 * @param employee
 * @param initDate
 * @param endDate
 * @param joins
 * @return
 */
@SuppressWarnings("unchecked")
public List<Timesheet> findByProject(Employee employee, Project project, Date initDate, Date endDate,
        List<String> joins) {

    Criteria crit = getSession().createCriteria(getPersistentClass())
            .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
            .add(Restrictions.eq(Timesheet.EMPLOYEE, employee))
            .add(Restrictions.eq(Timesheet.STATUS, Constants.TIMESTATUS_APP3))
            .add(Restrictions.ge(Timesheet.INITDATE, initDate)).add(Restrictions.le(Timesheet.ENDDATE, endDate))
            .addOrder(Order.asc(Timesheet.INITDATE));

    crit.createCriteria(Timesheet.PROJECTACTIVITY).add(Restrictions.eq(Projectactivity.PROJECT, project))
            .addOrder(Order.asc(Projectactivity.PROJECT));

    if (joins != null) {
        addJoins(crit, joins);
    }

    return crit.list();
}

From source file:es.sm2.openppm.core.dao.TimesheetDAO.java

License:Open Source License

/**
 * Check for hours pending approval - APP1 or APP2
 * /* w ww  . ja v a2s.com*/
 * @param project
 * @return
 */
public boolean pendingApproval(Project project) {

    Criteria crit = getSession().createCriteria(getPersistentClass()).setProjection(Projections.rowCount())
            .add(Restrictions.or(Restrictions.eq(Timesheet.STATUS, Constants.TIMESTATUS_APP1),
                    Restrictions.eq(Timesheet.STATUS, Constants.TIMESTATUS_APP2)));

    // Project restriction
    crit.createCriteria(Timesheet.PROJECTACTIVITY).add(Restrictions.eq(Projectactivity.PROJECT, project));

    Integer count = (Integer) crit.uniqueResult();

    return (count != null && count > 0);
}