Example usage for org.hibernate.criterion ProjectionList add

List of usage examples for org.hibernate.criterion ProjectionList add

Introduction

In this page you can find the example usage for org.hibernate.criterion ProjectionList add.

Prototype

public ProjectionList add(Projection projection) 

Source Link

Document

Add a projection to this list of projections

Usage

From source file:org.fireflow.engine.persistence.hibernate.PersistenceServiceHibernateImpl.java

License:Open Source License

public Integer getCompletedTaskInstanceCountForTask(final String processInstanceId, final String taskId) {
    Integer result = (Integer) this.getHibernateTemplate().execute(new HibernateCallback() {

        public Object doInHibernate(Session arg0) throws HibernateException, SQLException {

            Criteria criteria = arg0.createCriteria(TaskInstance.class);
            criteria.add(Expression.eq("taskId", taskId.trim()));
            criteria.add(Expression.eq("processInstanceId", processInstanceId));

            Criterion cri2 = Expression.eq("state", new Integer(ITaskInstance.COMPLETED));

            criteria.add(cri2);/* w  ww .  j a v  a2 s  .c o  m*/

            ProjectionList prolist = Projections.projectionList();
            prolist.add(Projections.rowCount());
            criteria.setProjection(prolist);

            return criteria.uniqueResult();
        }
    });
    return result;
}

From source file:org.fireflow.engine.persistence.hibernate.PersistenceServiceHibernateImpl.java

License:Open Source License

public Integer getAliveTaskInstanceCountForActivity(final String processInstanceId, final String activityId) {
    Integer result = (Integer) this.getHibernateTemplate().execute(new HibernateCallback() {

        public Object doInHibernate(Session arg0) throws HibernateException, SQLException {

            Criteria criteria = arg0.createCriteria(TaskInstance.class);
            criteria.add(Expression.eq("processInstanceId", processInstanceId.trim()));

            criteria.add(Expression.eq("activityId", activityId.trim()));

            Criterion cri1 = Expression.eq("state", new Integer(ITaskInstance.INITIALIZED));
            Criterion cri2 = Expression.eq("state", new Integer(ITaskInstance.RUNNING));
            Criterion cri_or = Expression.or(cri1, cri2);

            criteria.add(cri_or);//from  ww  w.  j  a va2  s.  c o m

            ProjectionList prolist = Projections.projectionList();
            prolist.add(Projections.rowCount());
            criteria.setProjection(prolist);

            return criteria.uniqueResult();
        }
    });
    return result;
}

From source file:org.fireflow.engine.persistence.hibernate.PersistenceServiceHibernateImpl.java

License:Open Source License

public Integer getAliveProcessInstanceCountForParentTaskInstance(final String taskInstanceId) {
    Integer result = (Integer) this.getHibernateTemplate().execute(new HibernateCallback() {

        public Object doInHibernate(Session arg0) throws HibernateException, SQLException {
            Criteria criteria = arg0.createCriteria(ProcessInstance.class);
            criteria.add(Expression.eq("parentTaskInstanceId", taskInstanceId));

            Criterion cri1 = Expression.eq("state", new Integer(IProcessInstance.INITIALIZED));
            Criterion cri2 = Expression.eq("state", new Integer(IProcessInstance.RUNNING));
            //              Criterion cri3 = Expression.eq("state", new Integer(IProcessInstance.SUSPENDED));
            Criterion cri_or = Expression.or(cri1, cri2);
            //              Criterion cri_or = Expression.or(cri_tmp, cri3);

            criteria.add(cri_or);//  w  w w . ja va2 s  .  c  o  m

            ProjectionList prolist = Projections.projectionList();
            prolist.add(Projections.rowCount());
            criteria.setProjection(prolist);

            return criteria.uniqueResult();
        }
    });
    return result;
}

From source file:org.fornax.cartridges.sculptor.framework.accessimpl.jpahibernate.JpaHibFindByConditionAccessImpl.java

License:Apache License

private void addProjection(Criteria criteria) throws PersistenceException {
    // Prepare projection
    // All orderBy fields has to be in result - SQL limitation
    ProjectionList proj = Projections.projectionList().add(Projections.id());
    for (ConditionalCriteria crit : cndCriterias) {
        if (Operator.OrderAsc.equals(crit.getOperator()) || Operator.OrderDesc.equals(crit.getOperator())) {
            if (crit.getPropertyPath() != null && crit.getPropertyPath().length > 0) {
                throw new PersistenceException("Can't create distinct condition order by foreign field '"
                        + crit.getPropertyFullName() + "'");
            }/*from  w  w w  .  j  a  v a  2 s.  c om*/
            proj.add(Projections.property(crit.getPropertyFullName()));
        }
    }
    criteria.setProjection(Projections.distinct(proj));
}

From source file:org.fornax.cartridges.sculptor.framework.accessimpl.jpahibernate.JpaHibFindByConditionStatAccessImpl.java

License:Apache License

private void addStatProjection(Criteria criteria) throws PersistenceException {
    ProjectionList projList = Projections.projectionList();
    projList.add(Projections.rowCount());
    for (ColumnStat<T> column : statResult) {
        if (column.isCountNotNullFlag()) {
            projList.add(Projections.count(column.getColumn().getName()));
        }/* ww  w  . j  a  v a  2s.c  o m*/
        if (column.isMinFlag()) {
            projList.add(Projections.min(column.getColumn().getName()));
        }
        if (column.isMaxFlag()) {
            projList.add(Projections.max(column.getColumn().getName()));
        }
        if (column.isAverageFlag()) {
            projList.add(Projections.avg(column.getColumn().getName()));
        }
        if (column.isSumFlag()) {
            projList.add(Projections.sum(column.getColumn().getName()));
        }
    }

    criteria.setProjection(projList);
}

From source file:org.generationcp.middleware.dao.dms.DmsProjectDao.java

License:Open Source License

public List<DatasetReference> getDirectChildDatasetsOfStudy(final Integer studyId) {

    final List<DatasetReference> datasetReferences = new ArrayList<>();

    try {//from w  w w .ja  v a  2s.c o m

        final Criteria criteria = this.getSession().createCriteria(this.getPersistentClass());
        criteria.add(Restrictions.eq("study.projectId", studyId));
        // Exclude sub-observation datasets of study
        criteria.add(Restrictions.eq("parent.projectId", studyId));

        final ProjectionList projectionList = Projections.projectionList();
        projectionList.add(Projections.property(DmsProjectDao.PROJECT_ID));
        projectionList.add(Projections.property("name"));
        projectionList.add(Projections.property("description"));
        criteria.setProjection(projectionList);

        criteria.addOrder(Order.asc("name"));

        final List<Object[]> list = criteria.list();

        for (final Object[] row : list) {
            final Integer id = (Integer) row[0]; // project.id
            final String name = (String) row[1]; // project.name
            final String description = (String) row[2]; // project.description
            datasetReferences.add(new DatasetReference(id, name, description));
        }

    } catch (final HibernateException e) {
        LOG.error(e.getMessage(), e);
        throw new MiddlewareQueryException(
                "Error with getDirectChildDatasetsOfStudy query from Project: " + e.getMessage(), e);
    }

    return datasetReferences;

}

From source file:org.generationcp.middleware.dao.dms.ExperimentDao.java

License:Open Source License

public Map<String, Long> countObservationsPerInstance(final Integer datasetId) {

    try {//from w  w  w .  j  a  v  a2s . co  m
        final ProjectionList projectionList = Projections.projectionList();
        projectionList.add(Projections.groupProperty("g.description")).add(Projections.rowCount());

        final Criteria criteria = this.getSession().createCriteria(this.getPersistentClass());
        criteria.createAlias("geoLocation", "g");
        criteria.setProjection(projectionList);
        criteria.add(Restrictions.eq("project.projectId", datasetId));
        final List<Object[]> rows = criteria.list();

        final Map<String, Long> results = new LinkedHashMap<>();
        for (final Object[] row : rows) {
            results.put((String) row[0], (Long) row[1]);
        }
        return results;

    } catch (final HibernateException e) {
        final String message = "Error at countObservationsPerInstance=" + datasetId
                + " query at ExperimentDao: " + e.getMessage();
        ExperimentDao.LOG.error(message, e);
        throw new MiddlewareQueryException(message, e);
    }
}

From source file:org.generationcp.middleware.dao.dms.PhenotypeDao.java

License:Open Source License

public Map<Integer, Long> countOutOfSyncDataOfDatasetsInStudy(final Integer studyId) {
    final Map<Integer, Long> countOutOfSyncPerProjectMap = new HashMap<>();
    final Criteria criteria = this.getSession().createCriteria(this.getPersistentClass());
    criteria.createAlias("experiment", "experiment");
    criteria.createAlias("experiment.project", "project");
    criteria.createAlias("project.study", "study");
    criteria.add(Restrictions.eq("study.projectId", studyId));
    criteria.add(Restrictions.eq("valueStatus", ValueStatus.OUT_OF_SYNC));
    final ProjectionList projectionList = Projections.projectionList();
    projectionList.add(Projections.groupProperty("project.projectId")).add(Projections.rowCount());
    criteria.setProjection(projectionList);
    final List<Object[]> results = criteria.list();
    for (final Object[] row : results) {
        countOutOfSyncPerProjectMap.put((Integer) row[0], (Long) row[1]);
    }//from  w ww . j  a  v  a 2 s.  c  o  m
    return countOutOfSyncPerProjectMap;
}

From source file:org.goobi.production.chart.HibernateProjectionProjectTaskList.java

License:Open Source License

@SuppressWarnings("rawtypes")
private synchronized void calculate(Project inProject, List<IProjectTask> myTaskList, Boolean countImages,
        Integer inMax) {//from  ww  w.j ava2 s.  c  o  m

    Session session = Helper.getHibernateSession();
    Criteria crit = session.createCriteria(Task.class);

    crit.createCriteria("process", "proc");

    crit.addOrder(Order.asc("ordering"));

    crit.add(Restrictions.eq("proc.template", Boolean.FALSE));
    crit.add(Restrictions.eq("proc.project", inProject));

    ProjectionList proList = Projections.projectionList();

    proList.add(Projections.property("title"));
    proList.add(Projections.property("processingStatus"));
    proList.add(Projections.sum("proc.sortHelperImages"));
    proList.add(Projections.count("id"));
    // proList.add(Projections.groupProperty(("reihenfolge")));

    proList.add(Projections.groupProperty(("title")));
    proList.add(Projections.groupProperty(("processingStatus")));

    crit.setProjection(proList);

    List list = crit.list();

    Iterator it = list.iterator();
    if (!it.hasNext()) {
        logger.debug("No any data!");
    } else {
        Integer rowCount = 0;
        while (it.hasNext()) {
            Object[] row = (Object[]) it.next();
            rowCount++;
            StringBuilder message = new StringBuilder();

            String shorttitle;
            if (((String) row[FieldList.stepName.getFieldLocation()]).length() > 60) {
                shorttitle = ((String) row[FieldList.stepName.getFieldLocation()]).substring(0, 60) + "...";
            } else {
                shorttitle = (String) row[FieldList.stepName.getFieldLocation()];
            }

            IProjectTask pt = null;
            for (IProjectTask task : myTaskList) {
                if (task.getTitle().equals(shorttitle)) {
                    pt = task;
                    break;
                }
            }

            if (pt == null) {
                pt = new ProjectTask(shorttitle, 0, 0);
                myTaskList.add(pt);
            }

            if (TaskStatus.DONE.getValue().equals(row[FieldList.stepStatus.getFieldLocation()])) {
                if (countImages) {
                    pt.setStepsCompleted((Integer) row[FieldList.pageCount.getFieldLocation()]);
                } else {
                    pt.setStepsCompleted((Integer) row[FieldList.processCount.getFieldLocation()]);
                }
            }

            if (countImages) {
                pt.setStepsMax(pt.getStepsMax() + (Integer) row[FieldList.pageCount.getFieldLocation()]);
            } else {
                pt.setStepsMax(pt.getStepsMax() + (Integer) row[FieldList.processCount.getFieldLocation()]);
            }

            // TODO remove following lines all the way to system.out
            for (int i = 0; i < row.length; i++) {
                message.append("|");
                message.append(row[i]);
            }
            logger.debug(Integer.toString(rowCount) + message);

        }
    }

}

From source file:org.goobi.production.flow.statistics.hibernate.StatQuestProjectAssociations.java

License:Open Source License

@Override
public List<DataTable> getDataTables(List<? extends BaseDTO> dataSource) {
    ProjectionList proj = Projections.projectionList();
    proj.add(Projections.count("id"));
    proj.add(Projections.groupProperty("project.title"));

    Criteria crit;/*www . ja v  a2s . c  om*/

    //TODO: fix it
    /*if (originalFilter instanceof UserDefinedFilter) {
    crit = new UserDefinedFilter(originalFilter.getIDList()).getCriteria();
    crit.createCriteria("project", "project");
    } else {
    crit = originalFilter.clone().getCriteria();
    }
    // use a clone on the filter and apply the projection on the clone
    crit.setProjection(proj);*/

    String title = StatisticsMode.getByClassName(this.getClass()).getTitle();

    DataTable dtbl = new DataTable(title);
    dtbl.setShowableInPieChart(true);
    DataRow dRow = new DataRow(Helper.getTranslation("count"));

    //TODO: replace empty list with result list
    for (Object obj : new ArrayList<>()) {
        Object[] objArr = (Object[]) obj;
        dRow.addValue(new Converter(objArr[1]).getString(),
                new Converter(new Converter(objArr[0]).getInteger()).getDouble());
    }
    dtbl.addDataRow(dRow);

    List<DataTable> allTables = new ArrayList<>();

    dtbl.setUnitLabel(Helper.getTranslation("project"));
    allTables.add(dtbl);
    return allTables;
}