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.ProjectActivityDAO.java

License:Open Source License

/**
 * Get root activity of project//  w w  w. ja  v a  2s.c o m
 * 
 * @param proj
 * @return
 */
@SuppressWarnings("unchecked")
public Projectactivity consRootActivity(Project proj) {

    List<Projectactivity> activities = null;

    Criteria crit = getSession().createCriteria(getPersistentClass())
            .add(Restrictions.eq(Projectactivity.PROJECT, proj));

    crit.createCriteria(Projectactivity.WBSNODE).add(Restrictions.isNull(Wbsnode.WBSNODE));

    activities = crit.list();

    Projectactivity rootActivity = (activities != null && !activities.isEmpty() ? activities.get(0) : null);

    // Create Root Activity if not exist
    if (rootActivity == null) {

        Wbsnode wbsRoot = null;

        crit = getSession().createCriteria(Wbsnode.class).add(Restrictions.isNull(Wbsnode.WBSNODE))
                .addOrder(Order.asc(Wbsnode.CODE)).add(Restrictions.eq(Wbsnode.PROJECT, proj));

        List<Wbsnode> list = crit.list();
        if (!list.isEmpty()) {
            wbsRoot = list.get(0);
        }

        if (wbsRoot == null) {

            crit = getSession().createCriteria(Wbsnode.class).setProjection(Projections.rowCount())
                    .add(Restrictions.eq(Wbsnode.PROJECT, proj));

            if ((Integer) crit.uniqueResult() == 0) {

                WBSNodeDAO nodeDAO = new WBSNodeDAO(getSession());
                wbsRoot = new Wbsnode();
                wbsRoot.setProject(proj);
                wbsRoot.setIsControlAccount(true);
                wbsRoot.setName(proj.getProjectName());

                wbsRoot = nodeDAO.makePersistent(wbsRoot);
            }
        }

        if (wbsRoot != null) {

            rootActivity = new Projectactivity();
            rootActivity.setProject(proj);
            rootActivity.setWbsnode(wbsRoot);
            rootActivity.setWbsdictionary(StringPool.BLANK);

            if (wbsRoot.getCode() != null && !StringPool.BLANK.equals(wbsRoot.getCode())) {
                rootActivity.setActivityName(wbsRoot.getCode() + ". " + wbsRoot.getName());
            } else {
                rootActivity.setActivityName(wbsRoot.getName());
            }
            rootActivity = makePersistent(rootActivity);

            updatePlannedDates(proj, rootActivity);
            updateActualDates(proj, rootActivity);

            rootActivity = findById(rootActivity.getIdActivity());
        }
    }

    return rootActivity;
}

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

License:Open Source License

/**
 * List not assigned activities to seller
 * /* ww w .  j  av a 2  s .  c o m*/
 * @param seller
 * @param project
 * @param order
 * @return
 */
@SuppressWarnings("unchecked")
public List<Object[]> consNoAssignedActivities(Seller seller, Project project, Order... order) {

    Criteria usedCrit = getSession().createCriteria(getPersistentClass())
            .setProjection(Projections.property(Projectactivity.IDACTIVITY))
            .add(Restrictions.eq(Projectactivity.PROJECT, project));

    usedCrit.createCriteria(Projectactivity.ACTIVITYSELLERS)
            .add(Restrictions.eq(Activityseller.SELLER, seller));

    List<Projectactivity> lista = usedCrit.list();

    Criteria crit = getSession().createCriteria(getPersistentClass())
            .setProjection(Projections.projectionList().add(Projections.property(Projectactivity.IDACTIVITY))
                    .add(Projections.property(Projectactivity.ACTIVITYNAME)))
            .add(Restrictions.eq(Projectactivity.PROJECT, project));

    crit.createCriteria(Projectactivity.WBSNODE).add(Restrictions.eq(Wbsnode.ISCONTROLACCOUNT, true));

    if (!lista.isEmpty()) {
        crit.add(Restrictions.not(Restrictions.in(Projectactivity.IDACTIVITY, lista)));
    }

    for (Order o : order) {
        crit.addOrder(o);
    }

    return crit.list();
}

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

License:Open Source License

/**
 * Sum EV/*  w  w  w. ja  v a  2 s. c  o m*/
 * 
 * @param proj
 * @return
 */
public double sumEV(Project proj) {

    Criteria crit = getSession().createCriteria(getPersistentClass())
            .setProjection(Projections.sum(Projectactivity.EV))
            .add(Restrictions.eq(Projectactivity.PROJECT, proj));

    crit.createCriteria(Projectactivity.WBSNODE).add(Restrictions.eq(Wbsnode.ISCONTROLACCOUNT, true));

    Double value = (Double) crit.uniqueResult();

    return (value == null ? 0 : value);
}

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

License:Open Source License

public double clacPocBySum(Project project) {

    Criteria crit = getSession().createCriteria(getPersistentClass())
            .add(Restrictions.eq(Projectactivity.PROJECT, project)).setProjection(Projections.projectionList()
                    .add(Projections.sum(Projectactivity.POC)).add(Projections.rowCount()));

    Criteria critWBSNode = crit.createCriteria(Projectactivity.WBSNODE)
            .add(Restrictions.eq(Wbsnode.ISCONTROLACCOUNT, true));

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

    double poc = 0;

    if (value != null && value.length == 2) {
        double sumPoc = (Double) value[0] == null ? 0 : (Double) value[0];
        int rows = (Integer) value[1];

        if (rows > 0) {
            poc = sumPoc / new Double(rows);
        }//from  w ww.ja v  a  2s . co  m
    }

    return poc;
}

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

License:Open Source License

@SuppressWarnings("unchecked")
public Projectactivity findByRedMine(String accountingCode, String wbsnodeCode) {

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

    crit.createCriteria(Projectactivity.PROJECT).add(Restrictions.eq(Project.ACCOUNTINGCODE, accountingCode));

    crit.createCriteria(Projectactivity.WBSNODE).add(Restrictions.eq(Wbsnode.CODE, wbsnodeCode));

    List<Projectactivity> list = crit.list();

    return (list.isEmpty() ? null : list.get(0));
}

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

License:Open Source License

@SuppressWarnings("unchecked")
public List<Projectactivity> consByProjectList(Integer[] ids) {

    List<Projectactivity> activities = null;

    if (ValidateUtil.isNotNull(ids)) {

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

        crit.createCriteria(Projectactivity.PROJECT).add(Restrictions.in(Project.IDPROJECT, ids))
                .addOrder(Order.asc(Project.ACCOUNTINGCODE));

        crit.createCriteria(Projectactivity.WBSNODE).add(Restrictions.eq(Wbsnode.ISCONTROLACCOUNT, true));

        crit.addOrder(Order.asc(Projectactivity.IDACTIVITY));

        activities = crit.list();//  w w  w . ja  v a2  s . c  o m
    } else {
        activities = new ArrayList<Projectactivity>();
    }

    return activities;
}

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

License:Open Source License

/**
 * Find activities inputed//from   w  w  w.  j a v  a2 s.c  om
 *
 * @param ids
 * @param since
 * @param until
 * @param idResourcePool
 * @return
 */
@SuppressWarnings("unchecked")
public List<Projectactivity> findActivitiesInputedByProjectsAndEmployees(Integer[] ids, Date since, Date until,
        Integer idResourcePool) {

    List<Projectactivity> activities = null;

    if (ValidateUtil.isNotNull(ids)) {

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

        // All activities by projects
        //
        crit.createCriteria(Projectactivity.PROJECT).add(Restrictions.in(Project.IDPROJECT, ids))
                .addOrder(Order.asc(Project.ACCOUNTINGCODE));

        crit.createCriteria(Projectactivity.WBSNODE).add(Restrictions.eq(Wbsnode.ISCONTROLACCOUNT, true));

        crit.addOrder(Order.asc(Projectactivity.IDACTIVITY));

        // Teammmembers
        //
        Criteria members = crit.createCriteria(Projectactivity.TEAMMEMBERS);

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

            members.add(Restrictions.disjunction().add(Restrictions.between(Teammember.DATEIN, since, until))
                    .add(Restrictions.between(Teammember.DATEOUT, since, until))
                    .add(Restrictions.and(Restrictions.le(Teammember.DATEIN, since),
                            Restrictions.ge(Teammember.DATEOUT, until))));
        }

        // Employees 
        //
        Criteria employees = members.createCriteria(Teammember.EMPLOYEE);

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

        Criteria sheets = employees.createCriteria(Employee.TIMESHEETS)
                .add(Restrictions.eq(Timesheet.STATUS, Constants.TIMESTATUS_APP3));

        if (since != null && until != null) {
            sheets.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))));
        }

        activities = crit.list();
    } else {
        activities = new ArrayList<Projectactivity>();
    }

    return activities;
}

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

License:Open Source License

/**
 * Find control account activities by project
 * /*from   w  ww .  jav a  2  s .c  om*/
 * @param project
 * @return
 */
@SuppressWarnings("unchecked")
public List<Projectactivity> findControlAccountsByProject(Project project) {

    List<Projectactivity> list = null;

    if (project != null) {

        Criteria crit = getSession().createCriteria(getPersistentClass())
                .add(Restrictions.eq(Projectactivity.PROJECT, project));

        crit.createCriteria(Projectactivity.WBSNODE).add(Restrictions.eq(Wbsnode.ISCONTROLACCOUNT, true));

        addOrder(crit, Projectactivity.ACTIVITYNAME, Constants.ASCENDENT);

        list = crit.list();
    } else {
        list = new ArrayList<Projectactivity>();
    }

    return list;
}

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

License:Open Source License

/**
 * Find child for update dates// www.j  ava  2s .  c  o m
 * @param proj
 * @return
 */
@SuppressWarnings("unchecked")
public List<Project> findChilds(Project proj) {

    Criteria crit = getSession().createCriteria(getPersistentClass());
    crit.setProjection(Projections.property(Projectassociation.PROJECTBYLEAD));

    crit.add(Restrictions.eq(Projectassociation.PROJECTBYDEPENDENT, proj));
    crit.add(Restrictions.eq(Projectassociation.UPDATEDATES, true));

    crit.createCriteria(Projectassociation.PROJECTBYLEAD)
            .add(Restrictions.eq(Project.STATUS, Constants.STATUS_INITIATING));

    return crit.list();
}

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

License:Open Source License

/**
 * Find parents for update dates of child
 * @param proj//from   www .  jav a2s  . c o  m
 * @return
 */
@SuppressWarnings("unchecked")
public List<Project> findParents(Project proj) {

    Criteria crit = getSession().createCriteria(getPersistentClass());
    crit.setProjection(Projections.property(Projectassociation.PROJECTBYDEPENDENT));

    crit.add(Restrictions.eq(Projectassociation.PROJECTBYLEAD, proj));
    crit.add(Restrictions.eq(Projectassociation.UPDATEDATES, true));

    crit.createCriteria(Projectassociation.PROJECTBYDEPENDENT);

    return crit.list();
}