Example usage for org.hibernate SQLQuery addScalar

List of usage examples for org.hibernate SQLQuery addScalar

Introduction

In this page you can find the example usage for org.hibernate SQLQuery addScalar.

Prototype

SQLQuery<T> addScalar(String columnAlias, Type type);

Source Link

Document

Declare a scalar query result.

Usage

From source file:br.com.bluesoft.pronto.dao.SprintDao.java

License:Open Source License

@SuppressWarnings("unchecked")
private void preencheTotaisDeEsforcoEValorDeNegocioDosSprints(final Collection<Sprint> sprints) {
    final String sql = "select sprint, sum(t.valor_de_negocio) as valor_de_negocio_total, sum(t.esforco) as esforco_total from ticket t where t.sprint is not null and t.pai is null group by sprint";
    final SQLQuery query = getSession().createSQLQuery(sql);
    query.addScalar("sprint", Hibernate.INTEGER);
    query.addScalar("valor_de_negocio_total", Hibernate.INTEGER);
    query.addScalar("esforco_total", Hibernate.DOUBLE);
    final List<Object[]> list = query.list();
    for (final Object[] o : list) {
        final Integer sprintKey = (Integer) o[0];
        final int valorDeNegocioTotal = (Integer) o[1];
        final double esforcoTotal = (Double) o[2];
        for (final Sprint s : sprints) {
            if (s.getSprintKey() == sprintKey) {
                s.setEsforcoTotal(esforcoTotal);
                s.setValorDeNegocioTotal(valorDeNegocioTotal);
            }/* w  w  w.  jav a  2  s . c o  m*/

        }
    }
}

From source file:br.com.bluesoft.pronto.dao.SprintDao.java

License:Open Source License

private void preencheTotaisDeEsforcoEValorDeNegocioDoSprint(final Sprint sprint) {
    final String sql = "select sprint, sum(t.valor_de_negocio) as valor_de_negocio_total, sum(t.esforco) as esforco_total from ticket t where t.sprint = :sprint and t.pai is null group by sprint";
    final SQLQuery query = getSession().createSQLQuery(sql);
    query.setInteger("sprint", sprint.getSprintKey());
    query.addScalar("sprint", Hibernate.INTEGER);
    query.addScalar("valor_de_negocio_total", Hibernate.INTEGER);
    query.addScalar("esforco_total", Hibernate.DOUBLE);
    final Object[] o = (Object[]) query.uniqueResult();

    int valorDeNegocioTotal = 0;
    double esforcoTotal = 0d;

    if (o != null) {
        valorDeNegocioTotal = (Integer) o[1];
        esforcoTotal = (Double) o[2];
    }//from ww  w.  j av  a2  s .  co m

    sprint.setEsforcoTotal(esforcoTotal);
    sprint.setValorDeNegocioTotal(valorDeNegocioTotal);

}

From source file:br.gov.jfrj.siga.wf.relatorio.RelTempoDoc.java

License:Open Source License

/**
 * Retorna os docs no perodo indicado, ordenadas pelo tempo de
 * demora, podendo estar ou no finalizados.
 * /*from www . ja v a  2 s .c  o  m*/
 * Exemplo da query:
 * 
 * SELECT PI.START_,PI.END_,VI.STRINGVALUE_,PI.ID_ FROM
 * SIGAWF.JBPM_PROCESSINSTANCE PI, (SELECT DISTINCT PROCESSINSTANCE_,
 * STRINGVALUE_ FROM SIGAWF.JBPM_VARIABLEINSTANCE WHERE NAME_ LIKE 'doc_%'
 * AND STRINGVALUE_ LIKE '%-_' AND STRINGVALUE_ IS NOT NULL) VI, (SELECT *
 * FROM SIGAWF.JBPM_PROCESSDEFINITION WHERE NAME_ = 'Contratao: fase de
 * anlise') PD WHERE PI.PROCESSDEFINITION_=PD.ID_ AND PI.END_ IS NOT NULL
 * AND PI.ID_ = VI.PROCESSINSTANCE_ AND (PI.START_ >= To_Date('01/03/2011')
 * and PI.START_ <= To_Date('31/03/2011')) AND (PI.END_ >=
 * To_Date('01/03/2011') and PI.END_ <= To_Date('31/05/2011')) ;
 * 
 * 
 * @param dataInicial
 * @param dataFinal
 * @param dataFinalAte
 * @param dataFinalDe
 * @param incluirAbertos 
 * @return
 */
private Set<Doc> consultarDocs(String nomeProcedimento, Date dataInicialDe, Date dataInicialAte,
        Date dataFinalDe, Date dataFinalAte, Boolean incluirAbertos) {
    SQLQuery query = null;
    if (incluirAbertos) {
        query = (SQLQuery) WfDao.getInstance().getSessao()
                .createSQLQuery(getSQLConsultarDocumentosFinalizadosEAbertosNoPeriodo());
    } else {
        query = (SQLQuery) WfDao.getInstance().getSessao()
                .createSQLQuery(getSQLConsultarDocumentosFinalizadosNoPeriodo());
    }

    query.addScalar("START_", new CalendarType());
    query.addScalar("END_", new CalendarType());
    query.addScalar("STRINGVALUE_", new StringType());
    query.addScalar("ID_", new LongType());

    dataInicialAte = inclusiveData(dataInicialAte);
    dataFinalAte = inclusiveData(dataFinalAte);

    query.setString("nomeProcedimento", nomeProcedimento);
    query.setDate("dataInicialDe", dataInicialDe);
    query.setDate("dataInicialAte", dataInicialAte);
    query.setDate("dataFinalDe", dataFinalDe);
    query.setDate("dataFinalAte", dataFinalAte);
    List<Object[]> resultado = query.list();
    Set<Doc> docs = new TreeSet<Doc>(new DocComparator());
    for (Object[] o : resultado) {
        Doc s = new Doc();
        Calendar inicio = (Calendar) o[0];
        Calendar fim = (Calendar) o[1];
        s.setNumeroDoc(o[2].toString());
        s.setInicio(inicio);
        s.setFim(fim);
        s.setProcessInstanceID((Long) (o[3]));
        docs.add(s);
    }

    return docs;
}

From source file:br.gov.jfrj.siga.wf.relatorio.RelTempoDocDetalhado.java

License:Open Source License

private Set<Tarefa> consultarTarefas(String nomeProcedimento, Date dataInicialDe, Date dataInicialAte,
        Date dataFinalDe, Date dataFinalAte, Boolean incluirAbertos) {
    // ArrayList<Tarefa> tarefas = new ArrayList<Tarefa>();
    // Tarefa t1 = new Tarefa();
    // Tarefa t2 = new Tarefa();
    ////from ww  w .  j  av  a 2  s  . c om
    // t1.setNome("t1");
    // t1.setDataFim("01/01/2000");
    // t1.setDataInicio("01/01/2000");
    // t1.setDuracao("5 seg");
    //
    // t2.setNome("t2");
    // t2.setDataFim("01/01/1999");
    // t2.setDataInicio("01/01/1888");
    // t2.setDuracao("5 seg");
    //
    // tarefas.add(t1);
    // tarefas.add(t2);
    // return tarefas;

    // String sql =
    // "SELECT PI.START_,PI.END_,VI.STRINGVALUE_,PI.ID_ FROM JBPM_PROCESSINSTANCE PI, (SELECT DISTINCT PROCESSINSTANCE_, STRINGVALUE_ FROM JBPM_VARIABLEINSTANCE WHERE NAME_ LIKE 'doc_%' AND STRINGVALUE_ LIKE '%-_' AND STRINGVALUE_ IS NOT NULL) VI, (SELECT ID_ FROM JBPM_PROCESSDEFINITION WHERE NAME_ = :nomeProcedimento) PD WHERE PI.PROCESSDEFINITION_=PD.ID_ AND PI.END_ IS NOT NULL AND PI.ID_ = VI.PROCESSINSTANCE_ AND  (PI.START_ >= :dataInicialDe and PI.START_ <= :dataInicialAte) AND (PI.END_ >= :dataFinalDe and PI.END_ <= :dataFinalAte)";

    SQLQuery query = null;
    if (incluirAbertos) {
        query = (SQLQuery) WfDao.getInstance().getSessao()
                .createSQLQuery(getSQLConsultarDocumentosFinalizadosEAbertosNoPeriodo());
    } else {
        query = (SQLQuery) WfDao.getInstance().getSessao()
                .createSQLQuery(getSQLConsultarDocumentosFinalizadosNoPeriodo());
    }

    query.addScalar("stringvalue_", new StringType());
    query.addScalar("pd_name", new StringType());
    query.addScalar("p_start", new CalendarType());
    query.addScalar("p_end", new CalendarType());
    query.addScalar("t_name", new StringType());
    query.addScalar("t_create", new CalendarType());
    query.addScalar("t_end", new CalendarType());

    query.setString("nomeProcedimento", nomeProcedimento);
    query.setDate("dataInicialDe", dataInicialDe);
    query.setDate("dataInicialAte", dataInicialAte);
    query.setDate("dataFinalDe", dataFinalDe);
    query.setDate("dataFinalAte", dataFinalAte);

    List<Object[]> resultado = query.list();
    Set<Tarefa> tarefas = new TreeSet<Tarefa>(new TarefaComparator());
    for (Object[] o : resultado) {
        Tarefa t = new Tarefa();
        t.setDataInicioProcedimento((Calendar) o[2]);
        t.setDataFimProcedimento((Calendar) o[3]);
        t.setNumeroDocumento((String) o[0]);
        t.setNome((String) o[4]);
        t.setDataInicio((Calendar) o[5]);
        t.setDataFim((Calendar) o[6]);

        tarefas.add(t);

    }
    // Set<Doc> secs = new TreeSet<Doc>(new DocComparator());
    // for (Object[] o : resultado) {
    // Doc s = new Doc();
    // Calendar inicio = (Calendar) o[0];
    // Calendar fim = (Calendar) o[1];
    // s.setNumeroDoc(o[2].toString());
    // s.setInicio(inicio);
    // s.setFim(fim);
    // s.setProcessInstanceID((Long) (o[3]));
    // secs.add(s);
    // }
    //
    return tarefas;

}

From source file:com.bitranger.parknshop.common.ads.PsPromotItemDAO.java

License:Open Source License

public double calAdRevenue() {

    return getHibernateTemplate().execute(new HibernateCallback<Double>() {

        @Override/*from   w w  w. j  a v  a2 s .c om*/
        public Double doInHibernate(Session session) throws HibernateException, SQLException {
            SQLQuery q = session
                    .createSQLQuery(" select sum(AD.num_fetched * AD.weight) as ACC from ps_promot_item as PI  "
                            + "   inner join ps_ad_item as AD on AD.id_promot = PI.id "
                            + " where AD.time_start < CURRENT_TIMESTAMP  "
                            + "      and CURRENT_TIMESTAMP < AD.time_end ");
            q.addScalar("ACC", Hibernate.DOUBLE);
            Double db = (Double) q.uniqueResult();
            return db == null ? 0.0 : db;
        }
    });
}

From source file:com.bitranger.parknshop.common.dao.impl.PersistantMap.java

License:Open Source License

@Override
public Object get(final String key) {
    Assert.notBlank(key);//w  ww.j  a  va  2  s.co m

    byte[] b = getHibernateTemplate().execute(new HibernateCallback<byte[]>() {

        @Override
        public byte[] doInHibernate(Session arg0) throws HibernateException, SQLException {
            SQLQuery query = arg0.createSQLQuery("select val from ps_key_values where key = ?");
            query.setString(0, key);
            query.addScalar("val", Hibernate.BLOB);
            return (byte[]) query.uniqueResult();
        }
    });
    return ObjUtils.fromBytes(b);
}

From source file:com.bitranger.parknshop.seller.dao.impl.PsOrderDAO.java

License:Open Source License

@Override
public double countTnxVolumn() {
    return getHibernateTemplate().execute(new HibernateCallback<Double>() {
        @Override/*from w  w  w . jav a2 s.c om*/
        public Double doInHibernate(Session session) throws HibernateException, SQLException {
            SQLQuery query = session.createSQLQuery(
                    " select sum(OD.price_total)as REV from ps_order as OD " + " where OD.status = 3");
            query.addScalar("REV", Hibernate.DOUBLE);
            Double db = (Double) query.uniqueResult();
            return db == null ? 0.0 : db;
        }
    });
}

From source file:com.cms.dao.AppParamsDAO.java

License:Open Source License

public List<AppParamsDTO> getListProviderFromMineName(String mineName, Map<String, String> map) {
    List<AppParamsDTO> listAppParams = null;
    String taxAuthority = map.get("taxAuthority");
    String startFromDate = map.get("startFromDate");
    String endFromDate = map.get("endFromDate");
    String startToDate = map.get("startToDate");
    String endToDate = map.get("endToDate");
    String fromDateRegister = map.get("fromDateRegister");
    String toDateRegister = map.get("toDateRegister");
    StringBuilder sb = new StringBuilder();
    sb.append("      SELECT    PAR_ID parId,   ");
    sb.append("              DESCRIPTION description,   ");
    sb.append("              PAR_CODE parCode,   ");
    sb.append("              PAR_NAME parName,   ");
    sb.append("              PAR_ORDER parOrder,   ");
    sb.append("              PAR_TYPE parType,   ");
    sb.append("              STATUS status   ");
    sb.append("        FROM APP_PARAMS a   ");
    sb.append("        WHERE a.PAR_TYPE = 'PROVIDER' ");
    sb.append("                     AND lower(a.PAR_CODE) = ANY   ");
    sb.append("              (SELECT DISTINCT lower(ti.PROVIDER)   ");
    sb.append("                 FROM TERM_INFORMATION ti   ");
    sb.append(/*from  ww  w. ja  v a 2  s. com*/
            "                 LEFT JOIN CUSTOMER_STATUS cs on ti.TAX_CODE=cs.TAX_CODE and ti.MINE_NAME = cs.MINE_NAME ");
    sb.append("                 JOIN CUSTOMER c on c.TAX_CODE = ti.TAX_CODE ");
    sb.append("                 WHERE ti.IS_CONTACT_INFO is null AND cs.TAX_CODE IS NULL   ");
    sb.append("                 AND ti.MINE_NAME = ANY (:mineName)   ");

    if (!DataUtil.isStringNullOrEmpty(taxAuthority)) {
        sb.append("         and c.TAX_AUTHORITY = ANY(:taxAuthority)   ");
    }

    if (!DataUtil.isStringNullOrEmpty(startFromDate)) {
        sb.append("         and ti.START_TIME >= TO_DATE(:startFromDate,'dd/MM/yyyy') - 1   ");
    }
    if (!DataUtil.isStringNullOrEmpty(endFromDate)) {
        sb.append("         and ti.START_TIME <= TO_DATE(:endFromDate,'dd/MM/yyyy')   + 1 ");
    }
    if (!DataUtil.isStringNullOrEmpty(startToDate)) {
        sb.append("         and ti.END_TIME >= TO_DATE(:startToDate,'dd/MM/yyyy') - 1    ");
    }
    if (!DataUtil.isStringNullOrEmpty(endToDate)) {
        sb.append("         and ti.END_TIME <= TO_DATE(:endToDate,'dd/MM/yyyy') + 1   ");
    }
    if (!DataUtil.isStringNullOrEmpty(fromDateRegister)) {
        sb.append("   AND ti.DATE_REGISTER >= TO_DATE(:fromDateRegister,'dd/MM/yyyy') - 1 ");
    }
    if (!DataUtil.isStringNullOrEmpty(toDateRegister)) {
        sb.append("   AND ti.DATE_REGISTER <= TO_DATE(:toDateRegister,'dd/MM/yyyy') + 1");
    }
    sb.append("              )   ");
    sb.append("        ORDER BY a.PAR_CODE asc   ");
    SQLQuery query;
    try {
        query = getSession().createSQLQuery(sb.toString());
        //Thuc hien chuyen du lieu lay ve thanh thanh doi tuong            
        query.setResultTransformer(Transformers.aliasToBean(AppParamsDTO.class));
        query.addScalar("parId", new StringType());
        query.addScalar("description", new StringType());
        query.addScalar("parCode", new StringType());
        query.addScalar("parName", new StringType());
        query.addScalar("parOrder", new StringType());
        query.addScalar("parType", new StringType());
        query.addScalar("status", new StringType());
        query.setParameterList("mineName", DataUtil.parseInputListString(mineName));
        if (!DataUtil.isStringNullOrEmpty(taxAuthority)) {
            query.setParameterList("taxAuthority", DataUtil.parseInputListString(taxAuthority));
        }
        if (!DataUtil.isStringNullOrEmpty(startFromDate)) {
            query.setString("startFromDate", startFromDate);
        }
        if (!DataUtil.isStringNullOrEmpty(endFromDate)) {
            query.setString("endFromDate", endFromDate);
        }
        if (!DataUtil.isStringNullOrEmpty(startToDate)) {
            query.setString("startToDate", startToDate);
        }
        if (!DataUtil.isStringNullOrEmpty(endToDate)) {
            query.setString("endToDate", endToDate);
        }
        if (!DataUtil.isStringNullOrEmpty(fromDateRegister)) {
            query.setString("fromDateRegister", fromDateRegister);
        }
        if (!DataUtil.isStringNullOrEmpty(toDateRegister)) {
            query.setString("toDateRegister", toDateRegister);
        }
        listAppParams = query.list();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return listAppParams;
}

From source file:com.cms.dao.AppParamsDAO.java

License:Open Source License

public List<AppParamsDTO> getProviderFromStaffCode(String staffCode, String mineName) {
    List<AppParamsDTO> lstCategoryList = null;
    StringBuilder sb = new StringBuilder();
    sb.append("   SELECT    PAR_ID parId,   ");
    sb.append("           DESCRIPTION description,   ");
    sb.append("           PAR_CODE parCode,   ");
    sb.append("           PAR_NAME parName,   ");
    sb.append("           PAR_ORDER parOrder,   ");
    sb.append("           PAR_TYPE parType,   ");
    sb.append("           STATUS status   ");
    sb.append("        FROM APP_PARAMS a   ");
    sb.append("    WHERE        a.PAR_TYPE       = 'PROVIDER' ");
    sb.append("         AND a.STATUS = '1' ");
    sb.append("         AND lower(a.PAR_CODE) IN ");
    sb.append("      ( SELECT DISTINCT lower(ti.provider) ");
    sb.append("        FROM TERM_INFORMATION ti ");
    sb.append("        WHERE ti.IS_CONTACT_INFO is NULL AND exists ");
    sb.append("             (   SELECT DISTINCT cs.tax_code ");
    sb.append("                 FROM customer_status cs ");
    sb.append("                 WHERE 1 = 1 ");
    sb.append("                 AND cs.tax_code = ti.tax_code  ");
    if (!StringUtils.isStringNullOrEmpty(staffCode)) {
        sb.append("             AND cs.staff_code = :staffCode ");
    } else { //Add 15/04/2017 Them dieu kien neu la admin
        return getProviderFromMineName(mineName);
    }//from   ww  w. j  a  v  a  2  s. com
    if (!StringUtils.isStringNullOrEmpty(mineName)) {
        if (mineName.contains(",")) {
            sb.append("             AND cs.mine_name IN (:mineName) ");
        } else {
            sb.append("             AND cs.mine_name = :mineName ");
        }
    }
    sb.append("             ) ");
    sb.append("      ) ");
    try {
        SQLQuery query = getSession().createSQLQuery(sb.toString());
        //            if (!DataUtil.isStringNullOrEmpty(mineName)) {
        //                
        //                query.setParameter("mineName", mineName);
        //            }
        if (!DataUtil.isStringNullOrEmpty(mineName)) {
            if (mineName.contains(",")) {
                query.setParameterList("mineName", DataUtil.parseInputListString(mineName));
            } else {
                query.setParameter("mineName", mineName);
            }
        }
        if (!StringUtils.isStringNullOrEmpty(staffCode)) {
            query.setParameter("staffCode", staffCode);
        }
        query.setResultTransformer(Transformers.aliasToBean(AppParamsDTO.class));
        query.addScalar("parId", new StringType());
        query.addScalar("description", new StringType());
        query.addScalar("parCode", new StringType());
        query.addScalar("parName", new StringType());
        query.addScalar("parOrder", new StringType());
        query.addScalar("parType", new StringType());
        query.addScalar("status", new StringType());
        lstCategoryList = query.list();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return lstCategoryList;
}

From source file:com.cms.dao.AppParamsDAO.java

License:Open Source License

public List<AppParamsDTO> getProviderFromStaffCodeAndConditions(String staffCode, Map<String, String> map) {

    List<AppParamsDTO> lstCategoryList = null;
    String mineName = map.get("mineName");
    String taxAuthority = map.get("taxAuthority");
    String status = map.get("status");

    if (StringUtils.isStringNullOrEmpty(staffCode)) {
        //Add 17/04/2017 Them dieu kien neu la admin + cac dieu kien status, tinh
        return getProviderFromOtherConditions(mineName, taxAuthority, status);
    }/*from w w  w  .jav a 2 s .  com*/
    StringBuilder sb = new StringBuilder();
    sb.append("   SELECT    PAR_ID parId,   ");
    sb.append("           DESCRIPTION description,   ");
    sb.append("           PAR_CODE parCode,   ");
    sb.append("           PAR_NAME parName,   ");
    sb.append("           PAR_ORDER parOrder,   ");
    sb.append("           PAR_TYPE parType,   ");
    sb.append("           STATUS status   ");
    sb.append("        FROM APP_PARAMS a   ");
    sb.append("    WHERE        a.PAR_TYPE       = 'PROVIDER' ");
    sb.append("         AND a.STATUS = '1' ");
    sb.append("         AND lower(a.PAR_CODE) IN ");
    sb.append("      ( SELECT DISTINCT lower(ti.provider) ");
    sb.append("        FROM TERM_INFORMATION ti ");

    if (!StringUtils.isStringNullOrEmpty(taxAuthority)) {
        sb.append("        INNER JOIN CUSTOMER c ON c.TAX_CODE = ti.TAX_CODE ");
    }
    sb.append("        WHERE 1= 1 ");
    if (!StringUtils.isStringNullOrEmpty(taxAuthority)) {
        sb.append("    AND c.TAX_AUTHORITY = ANY(:taxAuthority) ");
    }
    sb.append("        AND  exists ");
    sb.append("             (   SELECT DISTINCT cs.tax_code ");
    sb.append("                 FROM customer_status cs ");
    sb.append("                 WHERE 1 = 1  ");
    sb.append("                 AND cs.tax_code = ti.tax_code  ");
    if (!StringUtils.isStringNullOrEmpty(status)) {
        sb.append("                 AND cs.status = ANY (:status) ");
    }
    if (!StringUtils.isStringNullOrEmpty(staffCode)) {
        sb.append("             AND cs.staff_code = :staffCode ");
    } else { //Add 17/04/2017 Them dieu kien neu la admin + cac dieu kien status, tinh
        return getProviderFromOtherConditions(mineName, taxAuthority, status);
    }
    if (!StringUtils.isStringNullOrEmpty(mineName)) {
        if (mineName.contains(",")) {
            sb.append("             AND cs.mine_name IN (:mineName) ");
        } else {
            sb.append("             AND cs.mine_name = :mineName ");
        }
    }
    sb.append("             ) ");
    sb.append("      ) ");
    try {
        SQLQuery query = getSession().createSQLQuery(sb.toString());
        if (!DataUtil.isStringNullOrEmpty(mineName)) {
            if (mineName.contains(",")) {
                query.setParameterList("mineName", DataUtil.parseInputListString(mineName));
            } else {
                query.setParameter("mineName", mineName);
            }
        }
        if (!StringUtils.isStringNullOrEmpty(staffCode)) {
            query.setParameter("staffCode", staffCode);
        }
        if (!StringUtils.isStringNullOrEmpty(status)) {
            query.setParameterList("status", DataUtil.parseInputListString(status));
        }
        if (!StringUtils.isStringNullOrEmpty(taxAuthority)) {
            query.setParameterList("taxAuthority", DataUtil.parseInputListString(taxAuthority));
        }
        query.setResultTransformer(Transformers.aliasToBean(AppParamsDTO.class));
        query.addScalar("parId", new StringType());
        query.addScalar("description", new StringType());
        query.addScalar("parCode", new StringType());
        query.addScalar("parName", new StringType());
        query.addScalar("parOrder", new StringType());
        query.addScalar("parType", new StringType());
        query.addScalar("status", new StringType());
        lstCategoryList = query.list();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return lstCategoryList;
}