Example usage for javax.persistence TypedQuery getResultList

List of usage examples for javax.persistence TypedQuery getResultList

Introduction

In this page you can find the example usage for javax.persistence TypedQuery getResultList.

Prototype

List<X> getResultList();

Source Link

Document

Execute a SELECT query and return the query results as a typed List.

Usage

From source file:net.triptech.metahive.model.KeyValue.java

/**
 * Find key value based on its id values.
 *
 * @param def the definition/*from   w w  w  . j a v a  2 s . com*/
 * @param primaryId the primary record id
 * @param secondaryId the secondary record id
 * @param tertiaryId the tertiary record id
 * @return the key value
 */
public static KeyValue findKeyValue(final Definition def, final String primaryId, final String secondaryId,
        final String tertiaryId) {

    KeyValue keyValue = null;

    if (def == null) {
        throw new IllegalArgumentException("A valid defintion is required");
    }
    if (StringUtils.isBlank(primaryId)) {
        throw new IllegalArgumentException("The primaryId argument is required");
    }

    StringBuilder sql = new StringBuilder();
    sql.append("SELECT k FROM KeyValue AS k JOIN k.definition d");
    sql.append(" WHERE d.id = :definitionId AND");
    sql.append(" LOWER(k.primaryRecordId) = LOWER(:primaryRecordId)");
    sql.append(" AND LOWER(k.secondaryRecordId) = LOWER(:secondaryRecordId)");
    sql.append(" AND LOWER(k.tertiaryRecordId) = LOWER(:tertiaryRecordId)");

    TypedQuery<KeyValue> q = entityManager().createQuery(sql.toString(), KeyValue.class);
    q.setParameter("definitionId", def.getId());
    q.setParameter("primaryRecordId", primaryId);
    q.setParameter("secondaryRecordId", secondaryId);
    q.setParameter("tertiaryRecordId", tertiaryId);

    List<KeyValue> keyValues = q.getResultList();

    if (keyValues != null && keyValues.size() > 0) {
        keyValue = keyValues.get(0);
    }
    return keyValue;
}

From source file:net.triptech.metahive.model.Definition.java

/**
 * A helper function to find summarised definitions
 * based on the supplied definition id.//from  ww w.ja  v a 2 s. c  om
 *
 * @param def the definition
 * @return the list
 */
public static List<Definition> findSummarisedDefinitions(final Definition def) {

    TypedQuery<Definition> q = entityManager()
            .createQuery("SELECT d FROM Definition AS d WHERE d.summaryDefinition = :def", Definition.class);
    q.setParameter("def", def);

    return q.getResultList();
}

From source file:com.clustercontrol.jobmanagement.util.QueryUtil.java

public static List<MonitorInfo> getMonitorInfoByMonitorTypeIds_OR(List<String> monitorTypeIds,
        String ownerRoleId) {/* www.j a v  a  2  s.  c  om*/
    HinemosEntityManager em = new JpaTransactionManager().getEntityManager();
    StringBuffer sbJpql = new StringBuffer();
    sbJpql.append("SELECT a FROM MonitorInfo a");
    if (monitorTypeIds != null && monitorTypeIds.size() > 0) {
        sbJpql.append(" WHERE a.monitorTypeId IN ("
                + HinemosEntityManager.getParamNameString("monitorTypeId", monitorTypeIds) + ")");
    }
    sbJpql.append(" ORDER BY a.monitorId");
    TypedQuery<MonitorInfo> typedQuery = em.createQuery_OR(sbJpql.toString(), MonitorInfo.class, ownerRoleId);
    if (monitorTypeIds != null && monitorTypeIds.size() > 0) {
        typedQuery = HinemosEntityManager.appendParam(typedQuery, "monitorTypeId",
                monitorTypeIds.toArray(new String[0]));
    }
    return typedQuery.getResultList();
}

From source file:net.triptech.metahive.model.Definition.java

/**
 * Find the top level definitions (i.e. those that are not summarised).
 *
 * @param applicability the applicability of the definition to filter by
 * @return the list of definitions/*  ww  w  .j a  v a2s  .  c o m*/
 */
public static List<Definition> findTopLevelDefinitions(final Applicability applicability) {

    StringBuilder sql = new StringBuilder("SELECT d FROM Definition d");
    sql.append(" WHERE d.summaryDefinition IS NULL");

    if (applicability != null) {
        sql.append(" AND d.applicability = :applicability");
    }
    sql.append(" ORDER BY d.name ASC");

    TypedQuery<Definition> q = entityManager().createQuery(sql.toString(), Definition.class);
    if (applicability != null) {
        q.setParameter("applicability", applicability);
    }

    return q.getResultList();
}

From source file:com.clustercontrol.accesscontrol.util.QueryUtil.java

public static List<ObjectPrivilegeInfo> getAllObjectPrivilegeByFilter(String objectType, String objectId,
        String roleId, String objectPrivilege) {

    HinemosEntityManager em = new JpaTransactionManager().getEntityManager();

    StringBuffer sbJpql = new StringBuffer();
    sbJpql.append("SELECT a FROM ObjectPrivilegeInfo a WHERE true = true");
    if (objectType != null && !"".equals(objectType)) {
        sbJpql.append(" AND a.id.objectType = :objectType");
    }/*  ww  w.j a  va 2  s .  c  o m*/
    if (objectId != null && !"".equals(objectId)) {
        sbJpql.append(" AND a.id.objectId = :objectId");
    }
    if (roleId != null && !"".equals(roleId)) {
        sbJpql.append(" AND a.id.roleId = :roleId");
    }
    if (objectPrivilege != null && !"".equals(objectPrivilege)) {
        sbJpql.append(" AND a.id.objectPrivilege = :objectPrivilege");
    }
    TypedQuery<ObjectPrivilegeInfo> typedQuery = em.createQuery(sbJpql.toString(), ObjectPrivilegeInfo.class);
    if (objectType != null && !"".equals(objectType)) {
        typedQuery = typedQuery.setParameter("objectType", objectType);
    }
    if (objectId != null && !"".equals(objectId)) {
        typedQuery = typedQuery.setParameter("objectId", objectId);
    }
    if (roleId != null && !"".equals(roleId)) {
        typedQuery = typedQuery.setParameter("roleId", roleId);
    }
    if (objectPrivilege != null && !"".equals(objectPrivilege)) {
        typedQuery = typedQuery.setParameter("objectPrivilege", objectPrivilege);
    }
    return typedQuery.getResultList();
}

From source file:net.triptech.metahive.model.Definition.java

/**
 * Find the definition going by the supplied name.
 *
 * @param name/*from  w  w  w.  ja  va  2 s  . c o m*/
 *            the name
 * @return the definition
 */
public static Definition findDefinitionByNameEquals(String name) {

    Definition definition = null;

    if (name == null || name.length() == 0) {
        throw new IllegalArgumentException("The name argument is required");
    }

    TypedQuery<Definition> q = entityManager()
            .createQuery("SELECT d FROM Definition AS d WHERE LOWER(d.name) = LOWER(:name)", Definition.class);
    q.setParameter("name", name);

    List<Definition> definitions = q.getResultList();

    if (definitions != null && definitions.size() > 0) {
        definition = definitions.get(0);
    }
    return definition;
}

From source file:net.triptech.metahive.model.Definition.java

/**
 * Find the definition which is the unique identifier.
 *
 * @param name//w w w.  j  a va  2 s .c  om
 *            the name
 * @return the definition
 */
public static Definition findUniqueIdentifierDefinition() {

    Definition definition = null;

    TypedQuery<Definition> q = entityManager()
            .createQuery("SELECT d FROM Definition AS d WHERE d.dataType = :dataType", Definition.class);
    q.setParameter("dataType", DataType.TYPE_UNIQUEID);

    List<Definition> definitions = q.getResultList();

    if (definitions != null && definitions.size() > 0) {
        definition = definitions.get(0);
    }
    return definition;
}

From source file:net.triptech.metahive.model.Definition.java

/**
 * Find definitions that have data supplied by the supplied organisation.
 *
 * @param organisation the organisation to filter by
 * @return the list of definitions//w w w  .  j  a v a  2 s  .  co m
 */
public static List<Definition> findSubmissionDefinitions(final Organisation organisation) {

    List<Definition> definitions = new ArrayList<Definition>();

    if (organisation != null && organisation.getId() != null) {

        StringBuilder sql = new StringBuilder("SELECT d FROM Definition d");
        sql.append(" JOIN d.dataSources ds JOIN ds.organisation o");
        sql.append(" WHERE d.definitionType = :definitionType");
        sql.append(" AND o.id = :organisationId ORDER BY d.name ASC");

        TypedQuery<Definition> q = entityManager().createQuery(sql.toString(), Definition.class);
        q.setParameter("definitionType", DefinitionType.STANDARD);
        q.setParameter("organisationId", organisation.getId());

        definitions = q.getResultList();
    }
    return definitions;
}

From source file:net.triptech.metahive.model.Definition.java

/**
 * Find the calculated definitions that include the supplied
 * definition as a calculation variable.
 *
 * @param definition the definition/*from  w w w  .j a v  a2  s . c om*/
 * @return the list
 */
public static List<Definition> findDefinitionsWithVariable(final Definition definition) {

    List<Definition> definitions = new ArrayList<Definition>();

    if (definition != null && definition.getId() != null) {

        StringBuilder calculation = new StringBuilder("%<span class=\"variable\">D");
        calculation.append(definition.getId());
        calculation.append("</span>%");

        StringBuilder sql = new StringBuilder("SELECT d FROM Definition d");
        sql.append(" WHERE upper(d.calculation) LIKE :calculation");
        sql.append(" AND d.definitionType = :definitionType");

        TypedQuery<Definition> q = entityManager().createQuery(sql.toString(), Definition.class);
        q.setParameter("calculation", calculation.toString().toUpperCase());
        q.setParameter("definitionType", DefinitionType.CALCULATED);

        definitions = q.getResultList();
    }
    return definitions;
}

From source file:net.triptech.metahive.model.Definition.java

/**
 * Find the definitions that could be potentially related to the supplied definition.
 *
 * @param definition the definition/*from w  w  w .j  av a  2 s . co m*/
 * @return the list
 */
public static List<Definition> findPotentialRelatedDefinitions(final Definition definition) {

    List<Definition> relatedDefinitions = new ArrayList<Definition>();
    HashMap<String, Object> variables = new HashMap<String, Object>();

    StringBuilder sql = new StringBuilder();

    if (definition != null) {
        StringBuilder where = new StringBuilder();

        List<Definition> defs = new ArrayList<Definition>();

        if (definition.getDefinitionType() == DefinitionType.CALCULATED) {
            defs = definition.getCalculatedDefinitions();
        }
        if (definition.getDefinitionType() == DefinitionType.SUMMARY) {
            defs = definition.getSummarisedDefinitions();
        }

        for (Definition def : defs) {
            if (where.length() > 0) {
                where.append(" AND ");
            }
            where.append("d.id != ");
            where.append(def.getId());
        }
        if (where.length() > 0) {
            where.insert(0, "(");
            where.append(")");
        }

        if (definition.getDefinitionType() == DefinitionType.CALCULATED) {
            if (where.length() > 0) {
                where.append(" AND ");
            }
            where.append("(d.dataType = :dataType1 OR d.dataType = :dataType2");
            where.append(" OR d.dataType = :dataType3)");

            variables.put("dataType1", DataType.TYPE_CURRENCY);
            variables.put("dataType2", DataType.TYPE_NUMBER);
            variables.put("dataType3", DataType.TYPE_PERCENTAGE);
        }

        if (definition.getDefinitionType() == DefinitionType.SUMMARY) {
            if (where.length() > 0) {
                where.append(" AND ");
            }
            where.append("d.definitionType != :definitionType");
            where.append(" AND d.summaryDefinition is null");

            variables.put("definitionType", DefinitionType.SUMMARY);
        }

        if (definition.getDefinitionType() == DefinitionType.CALCULATED
                || definition.getDefinitionType() == DefinitionType.SUMMARY) {
            // Load all of the definitions, less the definitions already associated
            sql.append("SELECT d FROM Definition d");

            if (where.length() > 0) {
                sql.append(" WHERE ");
                sql.append(where.toString());
            }
            sql.append(" ORDER BY d.name ASC");
        }
    }

    if (sql.length() > 0) {
        TypedQuery<Definition> q = entityManager().createQuery(sql.toString(), Definition.class);
        for (String variable : variables.keySet()) {
            q.setParameter(variable, variables.get(variable));
        }
        relatedDefinitions = q.getResultList();
    }

    return relatedDefinitions;
}