Example usage for org.hibernate.criterion Projections projectionList

List of usage examples for org.hibernate.criterion Projections projectionList

Introduction

In this page you can find the example usage for org.hibernate.criterion Projections projectionList.

Prototype

public static ProjectionList projectionList() 

Source Link

Document

Create a new projection list.

Usage

From source file:ch.qos.logback.audit.persistent.AuditEventDAO.java

License:Open Source License

@SuppressWarnings("unchecked")
static public List<Object[]> findMaxObject() throws HibernateException {
    Session s = null;/*w w w  .  j a  v  a2  s.  c o m*/
    try {
        s = openSession();

        //Query q = s.createQuery("select count(ae.subject), ae.subject FROM "+ AE_CLASS + " AS ae "+
        //"GROUP BY ae.subject ORDER BY count(ae.subject) desc");

        Criteria crit = s.createCriteria(AuditEvent.class);
        crit.setProjection(Projections.projectionList().add(Projections.count("object").as("gcount"))
                .add(Projections.groupProperty("object")));
        crit.addOrder(Order.desc("gcount"));
        crit.setMaxResults(10);
        return crit.list();
    } finally {
        close(s);
    }
}

From source file:ch.systemsx.cisd.openbis.generic.server.dataaccess.db.EntityPropertyTypeDAO.java

License:Apache License

public void fillTermUsageStatistics(List<VocabularyTermWithStats> termsWithStats, VocabularyPE vocabulary) {
    assert termsWithStats != null : "Unspecified terms.";
    assert vocabulary != null : "Unspecified vocabulary.";
    assert termsWithStats.size() == vocabulary.getTerms()
            .size() : "Sizes of terms to be filled and vocabulary terms don't match.";

    Map<Long, VocabularyTermWithStats> termsById = new HashMap<Long, VocabularyTermWithStats>(
            termsWithStats.size());/*w w  w. jav  a 2s.com*/
    for (VocabularyTermWithStats termWithStats : termsWithStats) {
        Long id = termWithStats.getTerm().getId();
        termsById.put(id, termWithStats);
    }

    final DetachedCriteria criteria = DetachedCriteria.forClass(entityKind.getEntityPropertyClass());
    // alias is the easiest way to restrict on association using criteria
    criteria.createAlias("vocabularyTerm", "term");
    criteria.add(Restrictions.eq("term.vocabularyInternal", vocabulary));
    ProjectionList projectionList = Projections.projectionList();
    projectionList.add(Projections.rowCount());
    projectionList.add(Projections.groupProperty("term.id"));
    criteria.setProjection(projectionList);

    final List<Object[]> results = cast(getHibernateTemplate().findByCriteria(criteria));

    for (Object[] result : results) {
        Integer numberOfUsages = (Integer) result[0];
        Long termId = (Long) result[1];
        termsById.get(termId).registerUsage(entityKind, numberOfUsages);
    }
}

From source file:classes.ReportAction.java

License:Apache License

public String productReport() throws Exception {
    logger.info("Starting productReport()"); //f:log

    ActionContext ac = ActionContext.getContext();
    ServletContext sc = (ServletContext) ac.get(StrutsStatics.SERVLET_CONTEXT);

    JasperReport jasperReport = JasperCompileManager
            .compileReport(sc.getResourceAsStream("/WEB-INF/classes/ProductReport.xml")); //f:jr

    Map<String, Object> parameters = new HashMap<String, Object>(); //f:jr
    parameters.put("ReportTitle", "List of Products"); //f:jr
    parameters.put("DataFile", new Date().toString()); //f:jr

    Session sess = HibernateUtil.getSessionFactory().openSession(); //f:hibernate
    Transaction t = sess.beginTransaction(); //f:hibernate

    Criteria criteria = sess.createCriteria(Product.class); //f:hibernate

    criteria.setProjection(Projections.projectionList().add(Projections.property("id"))
            .add(Projections.property("name")).add(Projections.property("price"))); //f:hibernate

    @SuppressWarnings("unchecked")
    List<Object[]> l = (List<Object[]>) criteria.list(); //f:hibernate

    t.commit(); //f:hibernate
    sess.close(); //f:hibernate

    HibernateQueryResultDataSource ds = new HibernateQueryResultDataSource(l,
            new String[] { "Id", "Name", "Price" }); //f:jr

    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, ds); //f:jr

    byte b[] = JasperExportManager.exportReportToPdf(jasperPrint); //f:jr

    this.inputStream = new ByteArrayInputStream(b);

    logger.info("Finishing productReport()"); //f:log
    return "download";
}

From source file:classes.ReportAction.java

License:Apache License

public String customerReport() throws Exception {
    logger.info("Starting customerReport()"); //f:log

    ActionContext ac = ActionContext.getContext();
    ServletContext sc = (ServletContext) ac.get(StrutsStatics.SERVLET_CONTEXT);

    JasperReport jasperReport = JasperCompileManager
            .compileReport(sc.getResourceAsStream("/WEB-INF/classes/CustomerReport.xml")); //f:jr

    Map<String, Object> parameters = new HashMap<String, Object>(); //f:jr
    parameters.put("ReportTitle", "List of Customers"); //f:jr
    parameters.put("DataFile", new Date().toString()); //f:jr

    Session sess = HibernateUtil.getSessionFactory().openSession(); //f:hibernate
    Transaction t = sess.beginTransaction(); //f:hibernate

    Criteria criteria = sess.createCriteria(Customer.class); //f:hibernate

    criteria.setProjection(Projections.projectionList().add(Projections.property("id"))
            .add(Projections.property("name")).add(Projections.property("phone"))); //f:hibernate

    @SuppressWarnings("unchecked")
    List<Object[]> l = (List<Object[]>) criteria.list(); //f:hibernate

    t.commit(); //f:hibernate
    sess.close(); //f:hibernate

    HibernateQueryResultDataSource ds = new HibernateQueryResultDataSource(l,
            new String[] { "Id", "Name", "Phone" }); //f:jr

    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, ds); //f:jr

    byte b[] = JasperExportManager.exportReportToPdf(jasperPrint); //f:jr

    this.inputStream = new ByteArrayInputStream(b);

    logger.info("Finishing customerReport()"); //f:log
    return "download";
}

From source file:co.id.pegadaian.pasg2.util.SusunTree.java

public Set<BigDecimal> listMenuByUser(Session session, String userId) {

    /* Ambil gropu by user */
    Criteria critGroup = session.createCriteria(TblUserGroup.class);
    critGroup.add(Restrictions.eq("id.userId", userId));
    critGroup.setProjection(Projections.projectionList().add(Projections.property("id.groupId")));//"id.groupId"
    List<BigDecimal> l = critGroup.list();
    if (l.size() > 0) {
        //        for(BigDecimal x :l){System.out.println(x);        }
        /*Menu berdasarkan Priviledge */
        Criteria c = session.createCriteria(TblPriviledge.class);
        c.add(Restrictions.in("id.groupId", l));
        c.setProjection(Projections.projectionList().add(Projections.property("id.menuId")));
        Set<BigDecimal> lMenuId = new HashSet();
        List<BigDecimal> k = c.list();
        for (BigDecimal x : k) {
            lMenuId.add(x);//new BigDecimal(x));
            //            System.out.println(x.getid().getMenuId());
            //                  System.out.println(k);
        }/*from   ww  w.jav  a 2 s.  c  om*/
        return lMenuId;
    } else {
        return null;
    }

}

From source file:co.id.pegadaian.pasg2.util.SusunTreeJeasyUI.java

public Set<BigDecimal> listMenuByUser(Session session, String userId) {
    /* Ambil gropu by user */
    Criteria critGroup = session.createCriteria(TblUserGroup.class);
    critGroup.add(Restrictions.eq("id.userId", userId));
    critGroup.setProjection(Projections.projectionList().add(Projections.property("id.groupId")));//"id.groupId"
    List<BigDecimal> l = critGroup.list();
    if (l.size() > 0) {
        /*Menu berdasarkan Priviledge */
        Criteria c = session.createCriteria(TblPriviledge.class);
        c.add(Restrictions.in("id.groupId", l));
        c.setProjection(Projections.projectionList().add(Projections.property("id.menuId")));
        Set<BigDecimal> lMenuId = new HashSet();
        List<BigDecimal> k = c.list();
        for (BigDecimal x : k) {
            lMenuId.add(x);//new BigDecimal(x));
        }/*  w  w w .j a  v  a 2s  .  c  om*/
        return lMenuId;
    } else {
        return null;
    }

}

From source file:com.abiquo.server.core.infrastructure.network.IpPoolManagementDAO.java

License:Open Source License

public Collection<String> getAllMacs() {
    Criteria criteria = getSession().createCriteria(IpPoolManagement.class);
    ProjectionList projList = Projections.projectionList();
    projList.add(Projections.property(IpPoolManagement.MAC_PROPERTY));

    criteria.setProjection(projList);/*  www. ja v  a 2s  .  c om*/
    return criteria.list();
}

From source file:com.abiquo.server.core.infrastructure.RepositoryDAO.java

License:Open Source License

public boolean existRepositoryInOtherDatacenter(Datacenter datacenter, String repositoryLocation) {
    Criterion notDatacenter = Restrictions.not(thisDatacenter(datacenter));
    Criteria criteria = createCriteria(notDatacenter, thisLocation(repositoryLocation));

    criteria.setProjection(Projections.projectionList().add(Projections.rowCount()));

    Long count = (Long) criteria.uniqueResult();
    return count != null && count.intValue() > 0;
}

From source file:com.abiquo.server.core.infrastructure.RepositoryDAO.java

License:Open Source License

public boolean existRepositoryInSameDatacenter(Datacenter datacenter, String repositoryLocation) {
    Criteria criteria = createCriteria(thisDatacenter(datacenter), thisLocation(repositoryLocation));
    criteria.setProjection(Projections.projectionList().add(Projections.rowCount()));

    Long count = (Long) criteria.uniqueResult();
    return count != null && count.intValue() > 0;
}

From source file:com.abiquo.server.core.statistics.CloudUsageDAO.java

License:Open Source License

public CloudUsage sumTotalCloudUsage() {
    // TODO: Include aggregators functionality in bzengine?
    Session ses = HibernateEntityManagerHelper.getSession(getEntityManager());

    Criteria crit = ses.createCriteria(CloudUsage.class);
    ProjectionList proList = Projections.projectionList();
    proList.add(Projections.sum(CloudUsage.SERVERS_TOTAL_PROPERTY));
    proList.add(Projections.sum(CloudUsage.SERVERS_RUNNING_PROPERTY));
    proList.add(Projections.sum(CloudUsage.STORAGE_TOTAL_PROPERTY));
    // proList.add(Projections.sum(CloudUsage.STORAGE_RESERVED_PROPERTY));
    proList.add(Projections.sum(CloudUsage.STORAGE_USED_PROPERTY));
    proList.add(Projections.sum(CloudUsage.PUBLIC_I_PS_TOTAL_PROPERTY));
    // proList.add(Projections.sum(CloudUsage.PUBLIC_I_PS_RESERVED_PROPERTY));
    proList.add(Projections.sum(CloudUsage.PUBLIC_I_PS_USED_PROPERTY));
    proList.add(Projections.sum(CloudUsage.V_MACHINES_TOTAL_PROPERTY));
    proList.add(Projections.sum(CloudUsage.V_MACHINES_RUNNING_PROPERTY));
    proList.add(Projections.sum(CloudUsage.V_CPU_TOTAL_PROPERTY));
    // proList.add(Projections.sum(CloudUsage.V_CPU_RESERVED_PROPERTY));
    proList.add(Projections.sum(CloudUsage.V_CPU_USED_PROPERTY));
    proList.add(Projections.sum(CloudUsage.V_MEMORY_TOTAL_PROPERTY));
    // proList.add(Projections.sum(CloudUsage.V_MEMORY_RESERVED_PROPERTY));
    proList.add(Projections.sum(CloudUsage.V_MEMORY_USED_PROPERTY));
    proList.add(Projections.sum(CloudUsage.V_STORAGE_TOTAL_PROPERTY));
    // proList.add(Projections.sum(CloudUsage.V_STORAGE_RESERVED_PROPERTY));
    proList.add(Projections.sum(CloudUsage.V_STORAGE_USED_PROPERTY));
    proList.add(Projections.sum(CloudUsage.NUM_USERS_CREATED_PROPERTY));
    proList.add(Projections.sum(CloudUsage.NUM_VDC_CREATED_PROPERTY));
    proList.add(Projections.sum(CloudUsage.NUM_ENTERPRISES_CREATED_PROPERTY));

    crit.setProjection(proList);/*from  w  w w  .j ava  2  s .  c o m*/
    Object[] obj = (Object[]) crit.uniqueResult(); // Returns Object[] ->
    CloudUsage result = new CloudUsage();

    int cont = 0;
    result.setServersTotal((Long) obj[cont++]);
    result.setServersRunning((Long) obj[cont++]);
    result.setStorageTotal((Long) obj[cont++]);
    // result.setStorageReserved((Long) obj[cont++]);
    result.setStorageUsed((Long) obj[cont++]);
    result.setPublicIPsTotal((Long) obj[cont++]);
    // result.setPublicIPsReserved((Long) obj[cont++]);
    result.setPublicIPsUsed((Long) obj[cont++]);
    result.setVirtualMachinesTotal((Long) obj[cont++]);
    result.setVirtualMachinesRunning((Long) obj[cont++]);
    result.setVirtualCpuTotal((Long) obj[cont++]);
    // result.setVirtualCpuReserved((Long) obj[cont++]);
    result.setVirtualCpuUsed((Long) obj[cont++]);
    result.setVirtualMemoryTotal((Long) obj[cont++]);
    // result.setVirtualMemoryReserved((Long) obj[cont++]);
    result.setVirtualMemoryUsed((Long) obj[cont++]);
    result.setVirtualStorageTotal((Long) obj[cont++]);
    // result.setVirtualStorageReserved((Long) obj[cont++]);
    result.setVirtualStorageUsed((Long) obj[cont++]);
    result.setNumUsersCreated((Long) obj[cont++]);
    result.setNumVdcCreated((Long) obj[cont++]);
    result.setNumEnterprisesCreated((Long) obj[cont++]);

    return result;
}