Example usage for org.hibernate Criteria setProjection

List of usage examples for org.hibernate Criteria setProjection

Introduction

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

Prototype

public Criteria setProjection(Projection projection);

Source Link

Document

Used to specify that the query results will be a projection (scalar in nature).

Usage

From source file:com.aquest.emailmarketing.web.dao.TrackingResponseDao.java

/**
 * Gets the no of opens by broadcast./* ww  w  . jav  a2s .com*/
 *
 * @param broadcast_id the broadcast_id
 * @return the no of opens by broadcast
 */
public int getNoOfOpensByBroadcast(String broadcast_id) {
    Criteria crit = session().createCriteria(TrackingResponse.class);
    crit.add(Restrictions.eq("broadcast_id", broadcast_id));
    crit.add(Restrictions.eq("response_type", "Open"));
    List result = crit.setProjection(Projections.projectionList().add(Projections.groupProperty("unique_id")))
            .list();
    return result.size();
}

From source file:com.aquest.emailmarketing.web.dao.TrackingResponseDao.java

/**
 * Gets the no of click by broadcast./*ww w . j  a v  a 2  s . c  om*/
 *
 * @param broadcast_id the broadcast_id
 * @return the no of click by broadcast
 */
public int getNoOfClickByBroadcast(String broadcast_id) {
    Criteria crit = session().createCriteria(TrackingResponse.class);
    crit.add(Restrictions.eq("broadcast_id", broadcast_id));
    crit.add(Restrictions.eq("response_type", "Click"));
    List result = crit.setProjection(Projections.projectionList().add(Projections.groupProperty("unique_id")))
            .list();
    return result.size();
}

From source file:com.ar.dev.tierra.api.dao.impl.ChartDAOImpl.java

@SuppressWarnings("unchecked")
@Override//from  w ww. ja  v a2  s .  c  om
public List<Chart> getVentaVendedores(int idVendedor) {
    Calendar calendarInitial = Calendar.getInstance();
    Calendar calendarClosing = Calendar.getInstance();
    calendarInitial.set(Calendar.HOUR_OF_DAY, 0);
    calendarInitial.set(Calendar.MINUTE, 0);
    calendarInitial.set(Calendar.SECOND, 0);
    calendarInitial.set(Calendar.MILLISECOND, 0);
    Date fromDate = calendarInitial.getTime();
    calendarClosing.set(Calendar.HOUR_OF_DAY, 23);
    calendarClosing.set(Calendar.MINUTE, 59);
    calendarClosing.set(Calendar.SECOND, 59);
    calendarClosing.set(Calendar.MILLISECOND, 59);
    Date toDate = calendarClosing.getTime();
    int days = 0;
    List<Chart> chartVenta = new ArrayList<>();
    while (days <= 6) {
        Chart chart = new Chart();
        Criteria facturas = getSession().createCriteria(Factura.class);
        facturas.add(Restrictions.like("estado", "CONFIRMADO"));
        facturas.add(Restrictions.between("fechaCreacion", fromDate, toDate));
        Criteria vendedorFactura = facturas.createCriteria("idVendedor");
        vendedorFactura.add(Restrictions.eq("idUsuario", idVendedor));
        vendedorFactura.setProjection(Projections.rowCount());
        Long counter = (Long) facturas.uniqueResult();
        chart.setValue(counter.intValue());
        chart.setDate(fromDate);
        chartVenta.add(chart);
        calendarInitial.add(Calendar.DAY_OF_MONTH, -1);
        fromDate = calendarInitial.getTime();
        calendarClosing.add(Calendar.DAY_OF_MONTH, -1);
        toDate = calendarClosing.getTime();
        days++;
    }
    return chartVenta;
}

From source file:com.ar.dev.tierra.api.dao.impl.ChartDAOImpl.java

@Override
public List<Chart> getDineroVendedores(int idVendedor) {
    Calendar calendarInitial = Calendar.getInstance();
    Calendar calendarClosing = Calendar.getInstance();
    calendarInitial.set(Calendar.HOUR_OF_DAY, 0);
    calendarInitial.set(Calendar.MINUTE, 0);
    calendarInitial.set(Calendar.SECOND, 0);
    calendarInitial.set(Calendar.MILLISECOND, 0);
    Date fromDate = calendarInitial.getTime();
    calendarClosing.set(Calendar.HOUR_OF_DAY, 23);
    calendarClosing.set(Calendar.MINUTE, 59);
    calendarClosing.set(Calendar.SECOND, 59);
    calendarClosing.set(Calendar.MILLISECOND, 59);
    Date toDate = calendarClosing.getTime();
    int days = 0;
    List<Chart> chartVenta = new ArrayList<>();
    while (days <= 6) {
        Chart chart = new Chart();
        Criteria facturas = getSession().createCriteria(Factura.class);
        facturas.add(Restrictions.like("estado", "CONFIRMADO"));
        facturas.add(Restrictions.between("fechaCreacion", fromDate, toDate));
        Criteria vendedorFactura = facturas.createCriteria("idVendedor");
        vendedorFactura.add(Restrictions.eq("idUsuario", idVendedor));
        facturas.setProjection(Projections.sum("total"));
        BigDecimal counter = (BigDecimal) facturas.uniqueResult();
        if (counter != null) {
            chart.setValue(counter.intValue());
        } else {/*w w  w . ja  v a 2  s .c  o  m*/
            chart.setValue(0);
        }
        chart.setDate(fromDate);
        chartVenta.add(chart);
        calendarInitial.add(Calendar.DAY_OF_MONTH, -1);
        fromDate = calendarInitial.getTime();
        calendarClosing.add(Calendar.DAY_OF_MONTH, -1);
        toDate = calendarClosing.getTime();
        days++;
    }
    return chartVenta;
}

From source file:com.ar.dev.tierra.api.dao.impl.ChartDAOImpl.java

@Override
public List<Chart> getMontoMedioPago(int idMedioPago) {
    Calendar calendarInitial = Calendar.getInstance();
    Calendar calendarClosing = Calendar.getInstance();
    calendarInitial.set(Calendar.HOUR_OF_DAY, 0);
    calendarInitial.set(Calendar.MINUTE, 0);
    calendarInitial.set(Calendar.SECOND, 0);
    calendarInitial.set(Calendar.MILLISECOND, 0);
    Date fromDate = calendarInitial.getTime();
    calendarClosing.set(Calendar.HOUR_OF_DAY, 23);
    calendarClosing.set(Calendar.MINUTE, 59);
    calendarClosing.set(Calendar.SECOND, 59);
    calendarClosing.set(Calendar.MILLISECOND, 59);
    Date toDate = calendarClosing.getTime();
    int days = 0;
    List<Chart> chartMedioPago = new ArrayList<>();
    while (days <= 6) {
        Chart chart = new Chart();
        Criteria metodo = getSession().createCriteria(MetodoPagoFactura.class);
        metodo.add(Restrictions.eq("estado", true));
        Criteria planPago = metodo.createCriteria("planPago");
        Criteria tarjeta = planPago.createCriteria("tarjeta");
        Criteria medioPago = tarjeta.createCriteria("medioPago");
        medioPago.add(Restrictions.eq("idMedioPago", idMedioPago));
        metodo.add(Restrictions.between("fechaCreacion", fromDate, toDate));
        metodo.setProjection(Projections.sum("montoPago"));
        BigDecimal counter = (BigDecimal) metodo.uniqueResult();
        if (counter != null) {
            chart.setValue(counter.intValue());
        } else {//w  w w.  j ava2 s  .c  om
            chart.setValue(0);
        }
        chart.setDate(fromDate);
        chartMedioPago.add(chart);
        calendarInitial.add(Calendar.DAY_OF_MONTH, -1);
        fromDate = calendarInitial.getTime();
        calendarClosing.add(Calendar.DAY_OF_MONTH, -1);
        toDate = calendarClosing.getTime();
        days++;
    }
    return chartMedioPago;
}

From source file:com.ar.dev.tierra.api.dao.impl.ChartDAOImpl.java

@Override
public List<Chart> getVentaMedioPago(int idMedioPago) {
    Calendar calendarInitial = Calendar.getInstance();
    Calendar calendarClosing = Calendar.getInstance();
    calendarInitial.set(Calendar.HOUR_OF_DAY, 0);
    calendarInitial.set(Calendar.MINUTE, 0);
    calendarInitial.set(Calendar.SECOND, 0);
    calendarInitial.set(Calendar.MILLISECOND, 0);
    Date fromDate = calendarInitial.getTime();
    calendarClosing.set(Calendar.HOUR_OF_DAY, 23);
    calendarClosing.set(Calendar.MINUTE, 59);
    calendarClosing.set(Calendar.SECOND, 59);
    calendarClosing.set(Calendar.MILLISECOND, 59);
    Date toDate = calendarClosing.getTime();
    int days = 0;
    List<Chart> chartMedioPago = new ArrayList<>();
    while (days <= 6) {
        Chart chart = new Chart();
        Criteria metodo = getSession().createCriteria(MetodoPagoFactura.class);
        metodo.add(Restrictions.eq("estado", true));
        Criteria planPago = metodo.createCriteria("planPago");
        Criteria tarjeta = planPago.createCriteria("tarjeta");
        Criteria medioPago = tarjeta.createCriteria("medioPago");
        medioPago.add(Restrictions.eq("idMedioPago", idMedioPago));
        metodo.add(Restrictions.between("fechaCreacion", fromDate, toDate));
        metodo.setProjection(Projections.rowCount());
        Long counter = (Long) metodo.uniqueResult();
        if (counter != null) {
            chart.setValue(counter.intValue());
        } else {/*from w ww  .  j a va2s  . co  m*/
            chart.setValue(0);
        }
        chart.setDate(fromDate);
        chartMedioPago.add(chart);
        calendarInitial.add(Calendar.DAY_OF_MONTH, -1);
        fromDate = calendarInitial.getTime();
        calendarClosing.add(Calendar.DAY_OF_MONTH, -1);
        toDate = calendarClosing.getTime();
        days++;
    }
    return chartMedioPago;
}

From source file:com.ar.dev.tierra.api.dao.impl.ProductoDAOImpl.java

@SuppressWarnings("unchecked")
@Override//  w w  w .j  a  v  a  2s.  co m
public List<Producto> getAll() {
    Criteria criteria = getSession().createCriteria(Producto.class);
    ProjectionList projList = Projections.projectionList();
    projList.add(Projections.property("idProducto"), "idProducto");
    projList.add(Projections.property("marcas"), "marcas");
    projList.add(Projections.property("descripcion"), "descripcion");
    projList.add(Projections.property("colorProducto"), "colorProducto");
    projList.add(Projections.property("cantidadTotal"), "cantidadTotal");
    projList.add(Projections.property("talla"), "talla");
    criteria.add(Restrictions.eq("estadoProducto", true));
    criteria.addOrder(Order.desc("idProducto"));
    criteria.setProjection(Projections.distinct(projList));
    criteria.setResultTransformer(Transformers.aliasToBean(Producto.class));
    List<Producto> list = criteria.list();
    return list;
}

From source file:com.ar.dev.tierra.api.dao.impl.UsuariosDAOImpl.java

@SuppressWarnings("unchecked")
@Override/* w  w w .j  av a2  s .  c o m*/
public List<Usuarios> allUsuarios() {
    Criteria criteria = getSession().createCriteria(Usuarios.class);
    criteria.addOrder(Order.asc("idUsuario"));
    ProjectionList projList = Projections.projectionList();
    projList.add(Projections.property("idUsuario"), "idUsuario");
    projList.add(Projections.property("roles"), "roles");
    projList.add(Projections.property("nombre"), "nombre");
    projList.add(Projections.property("apellido"), "apellido");
    projList.add(Projections.property("dni"), "dni");
    projList.add(Projections.property("telefono"), "telefono");
    projList.add(Projections.property("email"), "email");
    projList.add(Projections.property("fechaNacimiento"), "fechaNacimiento");
    projList.add(Projections.property("domicilio"), "domicilio");
    projList.add(Projections.property("estado"), "estado");
    projList.add(Projections.property("ultimaConexion"), "ultimaConexion");
    projList.add(Projections.property("usuarioSucursal"), "usuarioSucursal");
    criteria.setProjection(Projections.distinct(projList));
    criteria.setResultTransformer(Transformers.aliasToBean(Usuarios.class));
    List<Usuarios> us = criteria.list();
    return us;
}

From source file:com.arg.arsoft.siantluis.repository.imp.CustomerRepository.java

@Override
public Map findByQuery(CustomerQuery query) {
    int pageSize = Configs.PAGE_SIZE;
    Criteria criteria = factory.getCurrentSession().createCriteria(Customer.class);
    if (query.getCode() != null && !query.getCode().equals("")) {
        if (query.getCode().contains("*") || query.getCode().contains("?")) {
            criteria.add(Expression.like("code", query.getCode().replace("*", "%").replace("?", "_")));
        } else {//from w w w  .j a  va2 s  . co  m
            criteria.add(Expression.eq("code", query.getCode()));
        }
    }
    if (query.getName() != null && !query.getName().equals("")) {
        if (query.getName().contains("*") || query.getName().contains("?")) {
            criteria.add(Expression.like("name", query.getName().replace("*", "%").replace("?", "_")));
        } else {
            criteria.add(Expression.eq("name", query.getName()));
        }
    }
    if (query.getCompany() != null && !query.getCompany().equals("")) {
        if (query.getCompany().contains("*") || query.getCompany().contains("?")) {
            criteria.add(Expression.like("company", query.getCompany().replace("*", "%").replace("?", "_")));
        } else {
            criteria.add(Expression.eq("company", query.getCompany()));
        }
    }
    if (query.getContactName() != null && !query.getContactName().equals("")) {
        if (query.getContactName().contains("*") || query.getContactName().contains("?")) {
            criteria.add(Expression.like("contact.contactName",
                    query.getContactName().replace("*", "%").replace("?", "_")));

        } else {
            criteria.add(Expression.eq("contact.contactName", query.getContactName()));
        }
    }
    if (query.getProvince() != null && query.getProvince() != 0) {
        criteria.createAlias("address.province", "p", JoinType.LEFT_OUTER_JOIN);
        criteria.add(Expression.eq("p.id", query.getProvince()));
    }
    List<CustomerType> types = new ArrayList<CustomerType>();
    if (query.getFlagCompany().equals("Y")) {
        types.add(CustomerType.C);
    }
    if (query.getFlagPersonal().equals("Y")) {
        types.add(CustomerType.P);
    }

    if (types.size() > 0) {
        criteria.add(Expression.in("customerType", types.toArray()));
    }

    long totalRecord = (long) criteria.setProjection(Projections.count(Projections.id().toString()))
            .uniqueResult();

    criteria.setProjection(null);

    int start = ((query.getPage() - 1) * pageSize);
    criteria.setFetchMode("address.province", FetchMode.JOIN);
    criteria.setFetchMode("address.amphur", FetchMode.JOIN);
    List<Customer> result = criteria.setFirstResult(start).setMaxResults(pageSize)
            .setResultTransformer(Criteria.ROOT_ENTITY).list();

    long totalPage = totalRecord / pageSize;
    if ((totalRecord % pageSize) > 0) {
        totalPage++;
    }
    List<Integer> pages = new ArrayList<Integer>();
    for (int index = 1; index <= totalPage; index++) {
        pages.add(index);
    }

    Map data = new HashMap();
    data.put("list", result);
    data.put("totalPage", totalPage);
    data.put("totalRecord", totalRecord);
    data.put("pages", pages);

    System.out.println(pages.size());
    return data;
}

From source file:com.arg.arsoft.siantluis.repository.imp.EmployeeRepository.java

@Override
public Map findByQuery(EmployeeQuery query) {

    Session session = factory.getCurrentSession();
    Criteria criteria = session.createCriteria(Employee.class);
    if (query.getCode() != null && !query.getCode().equals("")) {
        if (query.getCode().contains("*") || query.getCode().contains("?")) {
            criteria.add(Expression.like("code", query.getCode().replace("*", "%").replace("?", "_")));
        } else {/*from   w  ww .j  a v  a2s  .  c o  m*/
            criteria.add(Expression.eq("code", query.getCode()));
        }

    }
    if (query.getFirstName() != null && !query.getFirstName().equals("")) {
        if (query.getFirstName().contains("*") || query.getFirstName().contains("?")) {
            criteria.add(
                    Expression.like("firstName", query.getFirstName().replace("*", "%").replace("?", "_")));
        } else {
            criteria.add(Expression.eq("firstName", query.getFirstName()));
        }
    }
    if (query.getLastName() != null && !query.getLastName().equals("")) {
        if (query.getLastName().contains("*") || query.getLastName().contains("?")) {
            criteria.add(Expression.like("lastName", query.getLastName().replace("*", "%").replace("?", "_")));
        } else {
            criteria.add(Expression.eq("lastName", query.getLastName()));
        }
    }

    long totalRecord = (long) criteria.setProjection(Projections.count(Projections.id().toString()))
            .uniqueResult();
    long totalPage = totalRecord / Configs.PAGE_SIZE;
    if ((totalRecord % Configs.PAGE_SIZE) > 0) {
        totalPage++;
    }
    criteria.setProjection(null);
    int start = ((query.getPage() - 1) * Configs.PAGE_SIZE);//+ 1;
    List<Employee> result = criteria.setFetchMode("title", FetchMode.JOIN)
            .setFetchMode("position", FetchMode.JOIN).setFirstResult(start).setMaxResults(Configs.PAGE_SIZE)
            .setResultTransformer(Criteria.ROOT_ENTITY).list();
    List<Integer> pages = new ArrayList<Integer>();
    for (int index = 1; index <= totalPage; index++) {
        pages.add(index);
    }
    Map data = new HashMap();
    data.put("list", result);
    data.put("totalPage", totalPage);
    data.put("totalRecord", totalRecord);
    data.put("pages", pages);

    System.out.println(pages.size());
    return data;
}