Example usage for org.hibernate SQLQuery setBoolean

List of usage examples for org.hibernate SQLQuery setBoolean

Introduction

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

Prototype

@Deprecated
@SuppressWarnings("unchecked")
default Query<R> setBoolean(int position, boolean val) 

Source Link

Document

Bind a positional boolean-valued parameter.

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.
 * //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;
}