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:org.openxdata.server.dao.hibernate.HibernateFormDownloadDAO.java

License:Apache License

/**
 * Gets form version xml as returned by a given sql statement.
 * //from w w  w.  j  a v  a 2s. c o m
 * @param sql the sql statement.
 * @return the map of form versions xforms xml keyed by form version id.
 */
@SuppressWarnings("unchecked")
private Map<Integer, String> getFormsVersionXml(User user, boolean defaultForms, Integer studyId) {

    Map<Integer, String> forms = new LinkedHashMap<Integer, String>();
    List<Object[]> formList = null;

    Session session = getSessionFactory().getCurrentSession();

    if (user == null || user.hasAdministrativePrivileges()) {
        // existing query
        if (log.isDebugEnabled() && user != null) {
            log.debug("User " + user.getName() + " is an administrator, so all studies will be loaded");
        }
        String sql = "select form_definition_version_id,xform from form_definition_version fdv"
                + " inner join form_definition fd on fd.form_definition_id=fdv.form_definition_id"
                + " where xform is not null" + (studyId != null ? " and fd.study_id=" + studyId : "")
                + (defaultForms ? " and is_default=1" : "") + " order by fd.name";
        SQLQuery query = session.createSQLQuery(sql);
        query.addScalar("form_definition_version_id", Hibernate.INTEGER);
        query.addScalar("xform", Hibernate.STRING);
        formList = query.list();
    } else {
        // new query
        Query query = session.createQuery(
                "select distinct fdv.formDefVersionId, fdv.xform, fdv.formDef.name from FormDefVersion as fdv, User u"
                        + " where u.name = :name" + (defaultForms ? " and fdv.isDefault = :default" : "")
                        + (studyId != null ? " and fdv.formDef.study.studyId = :studyId" : "")
                        + " and (u in elements(fdv.formDef.users) or u in elements(fdv.formDef.study.users))"
                        + " order by fdv.formDef.name");
        query.setString("name", user.getName());
        query.setBoolean("default", defaultForms);
        if (studyId != null)
            query.setInteger("studyId", studyId);
        formList = query.list();
    }

    // process results
    for (Object[] form : formList) {
        String xform = (String) form[1];
        if (xform != null && xform.trim().length() > 0) {
            forms.put((Integer) form[0], xform);
        } else {
            log.info("Did not load xform with id " + form[0] + " due to empty xform");
        }
    }

    return forms;
}

From source file:org.openxdata.server.dao.hibernate.HibernateFormDownloadDAO.java

License:Apache License

@Override
public String getXformLocaleText(Integer formId, String locale) {
    Session session = getSessionFactory().getCurrentSession();

    SQLQuery query = session
            .createSQLQuery("select xform_text from form_definition_version_text where locale_key='" + locale
                    + "' and form_definition_version_id=" + formId);
    query.addScalar("xform_text", Hibernate.STRING);

    String text = (String) query.uniqueResult();

    return text;//from  w  w  w . ja v a2s.  c  om
}

From source file:org.oscarehr.PMmodule.dao.ProviderDao.java

License:Open Source License

public List<Provider> getSiteProvidersByProviderNo(String providerNo) {
    List<Provider> pList = new ArrayList<Provider>();
    Session sess = getSession();//from  ww w.  j a v  a2s .com
    try {
        SQLQuery q = sess.createSQLQuery("select distinct p.provider_no   " + " from provider p "
                + " inner join providersite ps on ps.provider_no = p.provider_no "
                + " where ps.site_id in (select site_id from providersite where provider_no = :providerno)");
        q.setParameter("providerno", providerNo);
        q.addScalar("provider_no", Hibernate.STRING);
        List providerNos = q.list();
        ProviderDao providerDao = (ProviderDao) SpringUtils.getBean("providerDao");
        for (Object no : providerNos) {
            String provNo = (String) no;
            Provider provider = providerDao.getProvider(provNo);
            pList.add(provider);
        }
    } catch (Exception e) {
        MiscUtils.getLogger().error("Error", e);
    } finally {
        try {
            sess.close();
        } catch (HibernateException e) {
            MiscUtils.getLogger().error("Error", e);
        }
    }
    return pList;
}

From source file:org.squashtest.tm.service.internal.repository.hibernate.HibernateCampaignFolderDao.java

License:Open Source License

@Override
@SuppressWarnings("unchecked")
public List<Long[]> findPairedContentForList(final List<Long> ids) {

    if (ids.isEmpty()) {
        return Collections.emptyList();
    }/*from  w w w  .j  a  v  a  2 s  .c  om*/

    SQLQuery query = currentSession()
            .createSQLQuery(NativeQueries.CAMPAIGN_FOLDER_SQL_FIND_PAIRED_CONTENT_FOR_FOLDERS);
    query.setParameterList("folderIds", ids, LongType.INSTANCE);
    query.addScalar("ancestor_id", LongType.INSTANCE);
    query.addScalar("descendant_id", LongType.INSTANCE);

    List<Object[]> result = query.list();

    return toArrayOfLong(result);
}

From source file:org.squashtest.tm.service.internal.repository.hibernate.HibernateCampaignFolderDao.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*from   ww  w  . ja v  a  2  s. co m*/
public List<Long> findContentForList(List<Long> ids) {
    if (ids.isEmpty()) {
        return Collections.emptyList();
    }

    SQLQuery query = currentSession().createSQLQuery(NativeQueries.CAMPAIGN_FOLDER_SQL_FIND_CONTENT_FOR_FOLDER);
    query.setParameterList("folderIds", ids, LongType.INSTANCE);
    query.addScalar("descendant_id", LongType.INSTANCE);

    return query.list();
}

From source file:org.squashtest.tm.service.internal.repository.hibernate.HibernateRequirementFolderDao.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override//ww  w  . j a v a2 s . co m
public List<Long[]> findPairedContentForList(final List<Long> ids) {

    if (ids.isEmpty()) {
        return Collections.emptyList();
    }

    SQLQuery query = currentSession()
            .createSQLQuery(NativeQueries.REQUIREMENT_FOLDER_SQL_FIND_PAIRED_CONTENT_FOR_FOLDERS);
    query.setParameterList("folderIds", ids, LongType.INSTANCE);
    query.addScalar("ancestor_id", LongType.INSTANCE);
    query.addScalar("descendant_id", LongType.INSTANCE);

    List<Object[]> result = query.list();

    return toArrayOfLong(result);
}

From source file:org.squashtest.tm.service.internal.repository.hibernate.HibernateRequirementFolderDao.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override// w ww .j av a2s.co  m
public List<Long> findContentForList(List<Long> ids) {
    if (ids.isEmpty()) {
        return Collections.emptyList();
    }

    SQLQuery query = currentSession()
            .createSQLQuery(NativeQueries.REQUIREMENT_FOLDER_SQL_FIND_CONTENT_FOR_FOLDER);
    query.setParameterList("folderIds", ids, LongType.INSTANCE);
    query.addScalar("descendant_id", LongType.INSTANCE);

    return query.list();
}

From source file:org.squashtest.tm.service.internal.repository.hibernate.HibernateTestCaseDeletionDao.java

License:Open Source License

@Override
@SuppressWarnings("unchecked")
public void removeOrSetIterationTestPlanInboundReferencesToNull(List<Long> testCaseIds) {

    if (!testCaseIds.isEmpty()) {
        SQLQuery query1 = getSession()
                .createSQLQuery(NativeQueries.TESTCASE_SQL_SELECTCALLINGITERATIONITEMTESTPLANHAVINGEXECUTIONS);
        query1.addScalar("item_test_plan_id", LongType.INSTANCE);
        query1.setParameterList(TEST_CASES_IDS, testCaseIds, LongType.INSTANCE);
        List<Long> itpHavingExecIds = query1.list();

        SQLQuery query2 = getSession().createSQLQuery(
                NativeQueries.TESTCASE_SQL_SELECTCALLINGITERATIONITEMTESTPLANHAVINGNOEXECUTIONS);
        query2.addScalar("item_test_plan_id", LongType.INSTANCE);
        query2.setParameterList(TEST_CASES_IDS, testCaseIds, LongType.INSTANCE);
        List<Long> itpHavingNoExecIds = query2.list();

        setNullCallingIterationItemTestPlanHavingExecutions(itpHavingExecIds);
        removeCallingIterationItemTestPlanHavingNoExecutions(itpHavingNoExecIds);
    }/* w w w .  j  av  a  2  s  . c o m*/

}

From source file:org.squashtest.tm.service.internal.repository.hibernate.HibernateTestCaseFolderDao.java

License:Open Source License

@Override
public List<Long[]> findPairedContentForList(final List<Long> ids) {
    if (ids.isEmpty()) {
        return Collections.emptyList();
    }/* w  w w .  j  av a 2  s  . co m*/

    SQLQuery query = currentSession()
            .createSQLQuery(NativeQueries.TEST_CASE_FOLDER_SQL_FIND_PAIRED_CONTENT_FOR_FOLDERS);
    query.setParameterList("folderIds", ids, LongType.INSTANCE);
    query.addScalar("ancestor_id", LongType.INSTANCE);
    query.addScalar("descendant_id", LongType.INSTANCE);

    List<Object[]> result = query.list();

    return toArrayOfLong(result);
}

From source file:org.squashtest.tm.service.internal.repository.hibernate.HibernateTestCaseFolderDao.java

License:Open Source License

@Override
public List<Long> findContentForList(List<Long> ids) {
    if (ids.isEmpty()) {
        return Collections.emptyList();
    }/*  w  ww. j  ava2s .  c  om*/

    SQLQuery query = currentSession()
            .createSQLQuery(NativeQueries.TEST_CASE_FOLDER_SQL_FIND_CONTENT_FOR_FOLDER);
    query.setParameterList("folderIds", ids, LongType.INSTANCE);
    query.addScalar("descendant_id", LongType.INSTANCE);

    return query.list();
}