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, String alias) 

Source Link

Document

Adds a projection to this list of projections after wrapping it with an alias

Usage

From source file:org.apache.usergrid.apm.service.charts.service.NetworkMetricsChartUtil.java

License:Apache License

public static ProjectionList getProjectionList(MetricsChartCriteria cq) {
    ProjectionList projList = Projections.projectionList();
    //Adding GroupBy. We will allow only one groupby so that chart looks cleaner.
    if (cq.isGroupedByApp()) {
        projList.add(Projections.groupProperty("this.appId"), "appId");
    } else//from   ww w  .  ja v  a 2s . c  o m
        projList.add(Projections.property("this.appId"), "appId");

    //projList.add(Projections.groupProperty("appId"),"appId");

    if (cq.isGroupedByNetworkType()) {
        projList.add(Projections.groupProperty("this.networkType"), "networkType");
    }

    else if (cq.isGroupedByNetworkCarrier()) {
        projList.add(Projections.groupProperty("this.networkCarrier"), "networkCarrier");
    }

    else if (cq.isGroupedByAppVersion()) {
        projList.add(Projections.groupProperty("this.applicationVersion"), "applicationVersion");
    } else if (cq.isGroupedByAppConfigType()) {
        projList.add(Projections.groupProperty("this.appConfigType"), "appConfigType");
    } else if (cq.isGroupedByDeviceModel()) {
        projList.add(Projections.groupProperty("this.deviceModel"), "deviceModel");
    } else if (cq.isGroupedbyDevicePlatform()) {
        projList.add(Projections.groupProperty("this.devicePlatform"), "devicePlatform");
    }

    switch (cq.getSamplePeriod()) {
    //see http://stackoverflow.com/questions/84644/hibernate-query-by-example-and-projections on why "this." is needed 
    //in following lines
    case MINUTE:
        projList.add(Projections.groupProperty("this.endMinute"), "endMinute");
        break;
    case HOUR:
        projList.add(Projections.groupProperty("this.endHour"), "endHour");
        break;
    case DAY_WEEK:
        projList.add(Projections.groupProperty("this.endDay"), "endDay");
        break;
    case DAY_MONTH:
        projList.add(Projections.groupProperty("this.endDay"), "endDay");
        break;
    case MONTH:
        projList.add(Projections.groupProperty("this.endMonth"), "endMonth");
        break;
    }

    //Adding Projections

    projList.add(Projections.sum("numSamples"), "numSamples");
    projList.add(Projections.sum("numErrors"), "numErrors");
    projList.add(Projections.sum("sumLatency"), "sumLatency");
    projList.add(Projections.max("maxLatency"), "maxLatency");
    projList.add(Projections.min("minLatency"), "minLatency");

    //may run into this bug because of alias http://stackoverflow.com/questions/84644/hibernate-query-by-example-and-projections
    //And I did run into it. ouch. Fix was to add this.filedName !!

    return projList;

}

From source file:org.apache.usergrid.apm.service.charts.service.SessionMetricsChartUtil.java

License:Apache License

public static ProjectionList getProjectionList(SessionChartCriteria cq) {
    ProjectionList projList = Projections.projectionList();
    //Adding GroupBy. We will allow only one groupby so that chart looks cleaner.
    if (cq.isGroupedByApp()) {
        projList.add(Projections.groupProperty("this.appId"), "appId");
    } else if (cq.isGroupedByNetworkType()) {
        projList.add(Projections.groupProperty("this.networkType"), "networkType");
    }/*from ww w .ja v a 2  s. c o m*/

    else if (cq.isGroupedByNetworkCarrier()) {
        projList.add(Projections.groupProperty("this.networkCarrier"), "networkCarrier");
    }

    switch (cq.getSamplePeriod()) {
    //see http://stackoverflow.com/questions/84644/hibernate-query-by-example-and-projections on why "this." is needed 
    //in following lines
    case MINUTE:
        projList.add(Projections.groupProperty("this.endMinute"), "endMinute");
        break;
    case HOUR:
        projList.add(Projections.groupProperty("this.endHour"), "endHour");
        break;
    case DAY_WEEK:
        projList.add(Projections.groupProperty("this.endDay"), "endDay");
        break;
    case DAY_MONTH:
        projList.add(Projections.groupProperty("this.endDay"), "endDay");
        break;
    case MONTH:
        projList.add(Projections.groupProperty("this.endMonth"), "endMonth");
        break;
    }

    //may run into this bug because of alias http://stackoverflow.com/questions/84644/hibernate-query-by-example-and-projections
    //And I did run into it. ouch. Fix was to add this.filedName !!

    return projList;

}

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

License:Open Source License

public List<DatasetDTO> getDatasets(final Integer studyId) {
    final List<DatasetDTO> datasetDTOS;
    try {//from   w w  w .  j av  a 2 s .c o  m

        final ProjectionList projectionList = Projections.projectionList();
        projectionList.add(Projections.property("project.projectId"), "datasetId");
        projectionList.add(Projections.property("dt.datasetTypeId"), "datasetTypeId");
        projectionList.add(Projections.property("project.name"), "name");
        projectionList.add(Projections.property("parent.projectId"), "parentDatasetId");

        final Criteria criteria = this.getSession().createCriteria(DmsProject.class, "project");
        criteria.createAlias("project.datasetType", "dt");
        criteria.createAlias("project.study", "study");
        criteria.add(Restrictions.eq("study.projectId", studyId));
        criteria.setProjection(projectionList);
        criteria.setResultTransformer(Transformers.aliasToBean(DatasetDTO.class));
        datasetDTOS = criteria.list();

    } catch (final HibernateException e) {
        throw new MiddlewareQueryException(
                "Error getting getDatasets for studyId=" + studyId + ":" + e.getMessage(), e);
    }
    return datasetDTOS;

}

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

License:Open Source License

public DatasetDTO getDataset(final Integer datasetId) {
    final DatasetDTO datasetDTO;
    try {//from w w  w. ja  va2 s . c  o  m

        final ProjectionList projectionList = Projections.projectionList();
        projectionList.add(Projections.property("project.projectId"), "datasetId");
        projectionList.add(Projections.property("dt.datasetTypeId"), "datasetTypeId");
        projectionList.add(Projections.property("project.name"), "name");
        projectionList.add(Projections.property("parent.projectId"), "parentDatasetId");
        final Criteria criteria = this.getSession().createCriteria(DmsProject.class, "project");
        criteria.createAlias("project.parent", "parent");
        criteria.createAlias("project.datasetType", "dt");
        criteria.add(Restrictions.eq("project.projectId", datasetId));
        criteria.setProjection(projectionList);
        criteria.setResultTransformer(Transformers.aliasToBean(DatasetDTO.class));
        datasetDTO = (DatasetDTO) criteria.uniqueResult();

    } catch (final HibernateException e) {
        final String errorMessage = "Error getting getDataset for datasetId =" + datasetId + ":"
                + e.getMessage();
        DmsProjectDao.LOG.error(errorMessage, e);
        throw new MiddlewareQueryException(errorMessage, e);
    }
    return datasetDTO;

}

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

License:Open Source License

public DatasetDTO getDatasetOfSampleList(final Integer sampleListId) {

    final DatasetDTO datasetDTO;
    try {/*from   ww w. ja  va 2  s .  c  om*/

        final ProjectionList projectionList = Projections.projectionList();
        projectionList.add(Projections.property("project.projectId"), "datasetId");
        projectionList.add(Projections.property("dt.datasetTypeId"), "datasetTypeId");
        projectionList.add(Projections.property("project.name"), "name");
        projectionList.add(Projections.property("parent.projectId"), "parentDatasetId");

        final Criteria criteria = this.getSession().createCriteria(SampleList.class);
        criteria.createAlias("samples", "sample").createAlias("samples.experiment", "experiment")
                .createAlias("experiment.project", "project").createAlias("project.parent", "parent")
                .createAlias("project.datasetType", "dt").add(Restrictions.eq("id", sampleListId));
        criteria.setProjection(Projections.distinct(projectionList));
        criteria.setResultTransformer(Transformers.aliasToBean(DatasetDTO.class));
        datasetDTO = (DatasetDTO) criteria.uniqueResult();

    } catch (final HibernateException e) {
        final String errorMessage = "Error getting getDatasetOfSampleList for sampleListId =" + sampleListId
                + ":" + e.getMessage();
        DmsProjectDao.LOG.error(errorMessage, e);
        throw new MiddlewareQueryException(errorMessage, e);
    }

    return datasetDTO;

}

From source file:org.openhie.openempi.blocking.basicblockinghp.dao.hibernate.BlockingDaoHibernate.java

License:Open Source License

@SuppressWarnings({ "unchecked", "rawtypes" })
public List<String> getBlockingKeyValues(final List<String> fields) {
    return (List<String>) getHibernateTemplate().execute(new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            List<String> values = new java.util.ArrayList<String>();
            ProjectionList projectionList = Projections.projectionList();
            for (String field : fields) {
                projectionList.add(Projections.property(field), field);
            }/*from  w w  w  .j a  va 2  s . c om*/
            org.hibernate.Criteria criteria = session.createCriteria(Person.class, "person_")
                    .setProjection(Projections.distinct(projectionList));
            for (String field : fields) {
                criteria.add(Expression.isNotNull("person_." + field));
            }
            log.debug("Blocking criteria query: " + criteria.toString());
            List list = (List<Object[]>) criteria.list();
            if (list.size() == 0) {
                return values;
            }
            // Query returns either a list of Object or a list of arrays of objects depending on
            // whether one or more than one blocking fields are used.
            if (!(list.get(0) instanceof Object[])) {
                List<Object> theValues = (List<Object>) list;
                for (Object value : theValues) {
                    values.add(value.toString());
                }
                return values;
            }
            List<Object[]> objectArrayValues = (List<Object[]>) list;
            for (Object[] row : objectArrayValues) {
                StringBuffer sb = new StringBuffer();
                for (int i = 0; i < row.length; i++) {
                    sb.append(row[i].toString().trim());
                }
                values.add(sb.toString());
            }
            return values;
        }
    });
}

From source file:org.openhie.openempi.blocking.basicblockinghp.dao.hibernate.BlockingDaoHibernate.java

License:Open Source License

@SuppressWarnings({ "unchecked", "rawtypes" })
public List<List<NameValuePair>> getDistinctValues(final List<String> fields) {
    return (List<List<NameValuePair>>) getHibernateTemplate().execute(new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            ProjectionList projectionList = Projections.projectionList();
            for (String field : fields) {
                projectionList.add(Projections.property(field), field);
            }//ww w  . j  ava2 s  .co m
            org.hibernate.Criteria criteria = session.createCriteria(Person.class, "person_")
                    .setProjection(Projections.distinct(projectionList));
            for (String field : fields) {
                criteria.add(Expression.isNotNull("person_." + field));
            }
            log.debug("Blocking criteria query: " + criteria.toString());
            List list = (List<Object[]>) criteria.list();
            List<List<NameValuePair>> pairs = new ArrayList<List<NameValuePair>>(list.size());
            if (list.size() == 0) {
                return pairs;
            }
            // Query returns either a list of Object or a list of arrays of objects depending on
            // whether one or more than one blocking fields are used.
            if (!(list.get(0) instanceof Object[])) {
                List<Object> theValues = (List<Object>) list;
                for (Object value : theValues) {
                    List<NameValuePair> distinctRowValues = new ArrayList<NameValuePair>(1);
                    distinctRowValues.add(new NameValuePair(fields.get(0), value));
                    pairs.add(distinctRowValues);
                }
                return pairs;
            }
            List<Object[]> objectArrayValues = (List<Object[]>) list;
            for (Object[] row : objectArrayValues) {
                List<NameValuePair> distinctRowValues = new ArrayList<NameValuePair>(row.length);
                for (int i = 0; i < row.length; i++) {
                    NameValuePair pair = new NameValuePair(fields.get(i), row[i]);
                    distinctRowValues.add(pair);
                }
                pairs.add(distinctRowValues);
            }
            return pairs;
        }
    });
}

From source file:org.openhie.openempi.blocking.basicblockinghp.dao.hibernate.BlockingDaoHibernate.java

License:Open Source License

@SuppressWarnings({ "unchecked", "rawtypes" })
public List<NameValuePair> getDistinctKeyValuePairs(final List<String> fields) {
    return (List<NameValuePair>) getHibernateTemplate().execute(new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            ProjectionList projectionList = Projections.projectionList();
            for (String field : fields) {
                projectionList.add(Projections.property(field), field);
            }/*from  w w w .ja v a 2 s  .  c om*/
            projectionList.add(Projections.property("personId"), "personId");
            org.hibernate.Criteria criteria = session.createCriteria(Person.class, "person_")
                    .setProjection(Projections.distinct(projectionList));
            for (String field : fields) {
                String property = "person_." + field;
                criteria.add(Expression.isNotNull(property));
                criteria.addOrder(Order.asc(property));
            }
            // We don't want to load deleted values
            criteria.add(Expression.isNull("dateVoided"));
            log.debug("Blocking criteria query: " + criteria.toString());
            List list = (List<Object[]>) criteria.list();
            List<NameValuePair> pairs = new ArrayList<NameValuePair>(list.size());
            if (list.size() == 0) {
                return pairs;
            }
            List<Object[]> objectArrayValues = (List<Object[]>) list;
            for (Object[] row : objectArrayValues) {
                // The last entry is always the person ID
                Integer personId = (Integer) row[row.length - 1];
                Object[] fields = Arrays.copyOfRange(row, 0, row.length - 1);
                String blockingKeyValue = BlockingKeyValueGenerator.generateBlockingKeyValue(fields);
                NameValuePair pair = new NameValuePair(blockingKeyValue, personId);
                pairs.add(pair);
            }
            return pairs;
        }
    });
}

From source file:org.openhie.openempi.dao.hibernate.BlockingDaoHibernate.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<String> getBlockingKeyValues(final List<String> fields) {
    return (List<String>) getHibernateTemplate().execute(new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            List<String> values = new java.util.ArrayList<String>();
            ProjectionList projectionList = Projections.projectionList();
            for (String field : fields) {
                projectionList.add(Projections.property(field), field);
            }//from   w w w . ja va 2 s .co  m
            org.hibernate.Criteria criteria = session.createCriteria(Person.class, "person_")
                    .setProjection(Projections.distinct(projectionList));
            for (String field : fields) {
                criteria.add(Expression.isNotNull("person_." + field));
            }
            log.debug("Blocking criteria query: " + criteria.toString());
            @SuppressWarnings("rawtypes")
            List list = (List<Object[]>) criteria.list();
            if (list.size() == 0) {
                return values;
            }
            // Query returns either a list of Object or a list of arrays of objects depending on
            // whether one or more than one blocking fields are used.
            if (!(list.get(0) instanceof Object[])) {
                List<Object> theValues = (List<Object>) list;
                for (Object value : theValues) {
                    values.add(value.toString());
                }
                return values;
            }
            List<Object[]> objectArrayValues = (List<Object[]>) list;
            for (Object[] row : objectArrayValues) {
                StringBuffer sb = new StringBuffer();
                for (int i = 0; i < row.length; i++) {
                    sb.append(row[i].toString().trim());
                }
                values.add(sb.toString());
            }
            return values;
        }
    });
}

From source file:org.openhie.openempi.dao.hibernate.BlockingDaoHibernate.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<List<NameValuePair>> getDistinctValues(final List<String> fields) {
    return (List<List<NameValuePair>>) getHibernateTemplate().execute(new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            ProjectionList projectionList = Projections.projectionList();
            for (String field : fields) {
                projectionList.add(Projections.property(field), field);
            }//w w w.ja  v  a  2s  .c  o m
            org.hibernate.Criteria criteria = session.createCriteria(Person.class, "person_")
                    .setProjection(Projections.distinct(projectionList));
            for (String field : fields) {
                criteria.add(Expression.isNotNull("person_." + field));
            }
            log.debug("Blocking criteria query: " + criteria.toString());
            @SuppressWarnings("rawtypes")
            List list = (List<Object[]>) criteria.list();
            List<List<NameValuePair>> pairs = new ArrayList<List<NameValuePair>>(list.size());
            if (list.size() == 0) {
                return pairs;
            }
            // Query returns either a list of Object or a list of arrays of objects depending on
            // whether one or more than one blocking fields are used.
            if (!(list.get(0) instanceof Object[])) {
                List<Object> theValues = (List<Object>) list;
                for (Object value : theValues) {
                    List<NameValuePair> distinctRowValues = new ArrayList<NameValuePair>(1);
                    distinctRowValues.add(new NameValuePair(fields.get(0), value));
                    pairs.add(distinctRowValues);
                }
                return pairs;
            }
            List<Object[]> objectArrayValues = (List<Object[]>) list;
            for (Object[] row : objectArrayValues) {
                List<NameValuePair> distinctRowValues = new ArrayList<NameValuePair>(row.length);
                for (int i = 0; i < row.length; i++) {
                    NameValuePair pair = new NameValuePair(fields.get(i), row[i]);
                    distinctRowValues.add(pair);
                }
                pairs.add(distinctRowValues);
            }
            return pairs;
        }
    });
}