Example usage for javax.persistence TypedQuery setParameter

List of usage examples for javax.persistence TypedQuery setParameter

Introduction

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

Prototype

TypedQuery<X> setParameter(int position, Object value);

Source Link

Document

Bind an argument value to a positional parameter.

Usage

From source file:net.triptech.buildulator.model.Person.java

/**
 * Find a person by their openId identifier. If none is found null is returned.
 *
 * @param openIdIdentifier the openId identifier
 * @return the person/*ww w. j a v  a 2  s.  co m*/
 */
public static Person findByOpenIdIdentifier(final String openIdIdentifier) {

    Person person = null;

    if (StringUtils.isBlank(openIdIdentifier)) {
        throw new IllegalArgumentException("The openIdIdentifier identifier argument is required");
    }

    TypedQuery<Person> q = entityManager().createQuery(
            "SELECT p FROM Person" + " AS p WHERE LOWER(p.openIdIdentifier) = LOWER(:openIdIdentifier)",
            Person.class);
    q.setParameter("openIdIdentifier", openIdIdentifier);

    List<Person> people = q.getResultList();

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

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

/**
 * Find submission entries.//from w w w .  j  a  v  a  2s.c  om
 *
 * @param filter the submission filter
 * @param firstResult the first result
 * @param maxResults the max results
 * @return the list
 */
public static List<Submission> findSubmissionEntries(final SubmissionFilter filter, final int firstResult,
        final int maxResults) {

    StringBuilder sql = new StringBuilder("SELECT s FROM Submission s");
    sql.append(buildWhere(filter));
    sql.append(" ORDER BY s.created ASC");

    TypedQuery<Submission> q = entityManager().createQuery(sql.toString(), Submission.class)
            .setFirstResult(firstResult).setMaxResults(maxResults);

    HashMap<String, Long> variables = buildVariables(filter);
    for (String variable : variables.keySet()) {
        q.setParameter(variable, variables.get(variable));
    }

    return q.getResultList();
}

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

/**
 * Find key values for the supplied Record.
 *
 * @param record the record//  w  w  w  .  j a  va2  s . co m
 * @return the key value
 */
public static List<KeyValue> findKeyValues(final Record record) {

    List<KeyValue> keyValues = new ArrayList<KeyValue>();

    if (record == null) {
        throw new IllegalArgumentException("A valid record is required");
    }

    StringBuilder sql = new StringBuilder();

    sql.append("SELECT k FROM KeyValue AS k JOIN k.record r");
    sql.append(" WHERE r.id = :recordId");

    TypedQuery<KeyValue> q = entityManager().createQuery(sql.toString(), KeyValue.class);

    q.setParameter("recordId", record.getId());

    if (q.getResultList() != null) {
        for (KeyValue keyValue : q.getResultList()) {
            keyValues.add(keyValue);
        }
    }
    return keyValues;
}

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

/**
 * Find key value based on its id values.
 *
 * @param def the definition//  w w w . ja  va 2  s  . c  o  m
 * @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.KeyValue.java

/**
 * Find key values for the supplied Record.
 *
 * @param record the record//from   ww  w  .  j a v a2  s  .c  o m
 * @param definitions the list of definitions to lookup
 * @return the key value
 */
public static List<KeyValue> findKeyValues(final Record record, final List<Definition> definitions) {

    List<KeyValue> keyValues = new ArrayList<KeyValue>();

    if (record == null) {
        throw new IllegalArgumentException("A valid record is required");
    }

    if (definitions != null && definitions.size() > 0) {

        StringBuilder sql = new StringBuilder();
        StringBuilder where = new StringBuilder();

        sql.append("SELECT k FROM KeyValue AS k JOIN k.record r");
        sql.append(" LEFT JOIN k.definition d WHERE r.id = :recordId");

        sql.append(" AND (");
        for (Definition definition : definitions) {
            if (where.length() > 0) {
                where.append(" OR ");
            }
            where.append("d.id = ");
            where.append(definition.getId());
        }
        sql.append(where.toString());
        sql.append(")");

        TypedQuery<KeyValue> q = entityManager().createQuery(sql.toString(), KeyValue.class);

        q.setParameter("recordId", record.getId());

        if (q.getResultList() != null) {
            for (KeyValue keyValue : q.getResultList()) {
                keyValues.add(keyValue);
            }
        }
    }
    return keyValues;
}

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

/**
 * Find the related key values based on the supplied key value.
 *
 * @param def the definition/*from   w  w  w. j ava2  s.co  m*/
 * @param primaryId the primary record id
 * @param secondaryId the secondary record id
 * @param tertiaryId the tertiary record id
 * @return the key value
 */
public static List<KeyValue> findRelatedKeyValues(final KeyValue keyValue) {

    List<KeyValue> relatedKeyValues = new ArrayList<KeyValue>();

    if (keyValue == null) {
        throw new IllegalArgumentException("A valid key value is required");
    }

    StringBuilder sql = new StringBuilder();
    HashMap<String, Object> variables = new HashMap<String, Object>();

    sql.append("SELECT k FROM KeyValue AS k LEFT JOIN k.definition d");
    sql.append(" WHERE d.id = :definition AND k.primaryRecordId = :primary");

    variables.put("definition", keyValue.getDefinition().getId());
    variables.put("primary", keyValue.getPrimaryRecordId());

    Applicability applicability = keyValue.getDefinition().getApplicability();

    if (applicability == Applicability.RECORD_SECONDARY) {
        sql.append(" AND k.secondaryRecordId = :secondary");
        variables.put("secondary", keyValue.getSecondaryRecordId());
    }
    if (applicability == Applicability.RECORD_TERTIARY) {
        sql.append(" AND k.tertiaryRecordId = :tertiary");
        variables.put("tertiary", keyValue.getTertiaryRecordId());
    }

    TypedQuery<KeyValue> q = entityManager().createQuery(sql.toString(), KeyValue.class);

    for (String key : variables.keySet()) {
        q.setParameter(key, variables.get(key));
    }

    if (q.getResultList() != null) {
        for (KeyValue kv : q.getResultList()) {
            relatedKeyValues.add(kv);
        }
    }
    System.out.println("Values: " + relatedKeyValues.size());

    return relatedKeyValues;
}

From source file:models.GroupMember.java

/**
 * /*from   w ww  . j  a  v a2s . c  o  m*/
 * @param pageIndex 1
 * @param pageSize ??
 * @param groupId Id
 * @param excludeId GroupMember Id,null?
 * @return
 */
public static Page<GroupMember> queryPageByGroupId(int start, int pageSize, Long groupId, Long excludeId) {
    String hql = " from GroupMember where group.id = :groupId ";
    if (null != excludeId) {
        hql += " and id <> :excludeId";
    }
    String countHql = "select count(id) " + hql;

    TypedQuery<Long> countTypedQuery = JPA.em().createQuery(countHql, Long.class).setParameter("groupId",
            groupId);
    TypedQuery<GroupMember> contentTypedQuery = JPA.em().createQuery(hql, GroupMember.class)
            .setParameter("groupId", groupId).setFirstResult((start - 1) * pageSize).setMaxResults(pageSize);

    if (null != excludeId) {
        countTypedQuery.setParameter("excludeId", excludeId);
        contentTypedQuery.setParameter("excludeId", excludeId);
    }

    Long count = countTypedQuery.getSingleResult();
    List<GroupMember> resultList = contentTypedQuery.getResultList();
    return new Page<GroupMember>(Constants.SUCESS, count, resultList);
}

From source file:com.clustercontrol.monitor.run.util.QueryUtil.java

public static List<MonitorInfo> getMonitorInfoByFilter(String monitorId, String monitorTypeId,
        String description, String calendarId, String regUser, Long regFromDate, Long regToDate,
        String updateUser, Long updateFromDate, Long updateToDate, Boolean monitorFlg, Boolean collectorFlg,
        String ownerRoleId) {//from ww  w.jav a  2s  . co  m

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

    // ??????????
    String notInclude = "NOT:";

    StringBuffer sbJpql = new StringBuffer();
    sbJpql.append("SELECT a FROM MonitorInfo a WHERE true = true");
    // monitorId
    if (monitorId != null && !"".equals(monitorId)) {
        if (!monitorId.startsWith(notInclude)) {
            sbJpql.append(" AND a.monitorId like :monitorId");
        } else {
            sbJpql.append(" AND a.monitorId not like :monitorId");
        }
    }
    // monitorTypeId
    if (monitorTypeId != null && !"".equals(monitorTypeId)) {
        if (!monitorTypeId.startsWith(notInclude)) {
            sbJpql.append(" AND a.monitorTypeId like :monitorTypeId");
        } else {
            sbJpql.append(" AND a.monitorTypeId not like :monitorTypeId");
        }
    }
    // description
    if (description != null && !"".equals(description)) {
        if (!description.startsWith(notInclude)) {
            sbJpql.append(" AND a.description like :description");
        } else {
            sbJpql.append(" AND a.description not like :description");
        }
    }
    // calendarId
    if (calendarId != null && !"".equals(calendarId)) {
        sbJpql.append(" AND a.calendarId like :calendarId");
    }
    // regUser
    if (regUser != null && !"".equals(regUser)) {
        if (!regUser.startsWith(notInclude)) {
            sbJpql.append(" AND a.regUser like :regUser");
        } else {
            sbJpql.append(" AND a.regUser not like :regUser");
        }
    }
    // regFromDate
    if (regFromDate > 0) {
        sbJpql.append(" AND a.regDate >= :regFromDate");
    }
    // regToDate
    if (regToDate > 0) {
        sbJpql.append(" AND a.regDate <= :regToDate");
    }
    // updateUser
    if (updateUser != null && !"".equals(updateUser)) {
        if (!updateUser.startsWith(notInclude)) {
            sbJpql.append(" AND a.updateUser like :updateUser");
        } else {
            sbJpql.append(" AND a.updateUser not like :updateUser");
        }
    }
    // updateFromDate
    if (updateFromDate > 0) {
        sbJpql.append(" AND a.updateDate >= :updateFromDate");
    }
    // updateToDate
    if (updateToDate > 0) {
        sbJpql.append(" AND a.updateDate <= :updateToDate");
    }
    // monitorFlg
    if (monitorFlg != null) {
        sbJpql.append(" AND a.monitorFlg = :monitorFlg");
    }
    // collectorFlg
    if (collectorFlg != null) {
        sbJpql.append(" AND a.collectorFlg = :collectorFlg");
    }
    // ownerRoleId
    if (ownerRoleId != null && !"".equals(ownerRoleId)) {
        if (!ownerRoleId.startsWith(notInclude)) {
            sbJpql.append(" AND a.ownerRoleId like :ownerRoleId");
        } else {
            sbJpql.append(" AND a.ownerRoleId not like :ownerRoleId");
        }
    }
    TypedQuery<MonitorInfo> typedQuery = em.createQuery(sbJpql.toString(), MonitorInfo.class);

    // monitorId
    if (monitorId != null && !"".equals(monitorId)) {
        if (!monitorId.startsWith(notInclude)) {
            typedQuery = typedQuery.setParameter("monitorId", monitorId);
        } else {
            typedQuery = typedQuery.setParameter("monitorId", monitorId.substring(notInclude.length()));
        }
    }
    // monitorTypeId
    if (monitorTypeId != null && !"".equals(monitorTypeId)) {
        if (!monitorTypeId.startsWith(notInclude)) {
            typedQuery = typedQuery.setParameter("monitorTypeId", monitorTypeId);
        } else {
            typedQuery = typedQuery.setParameter("monitorTypeId", monitorTypeId.substring(notInclude.length()));
        }
    }
    // description
    if (description != null && !"".equals(description)) {
        if (!description.startsWith(notInclude)) {
            typedQuery = typedQuery.setParameter("description", description);
        } else {
            typedQuery = typedQuery.setParameter("description", description.substring(notInclude.length()));
        }
    }
    // calendarId
    if (calendarId != null && !"".equals(calendarId)) {
        typedQuery = typedQuery.setParameter("calendarId", calendarId);
    }
    // regUser
    if (regUser != null && !"".equals(regUser)) {
        if (!regUser.startsWith(notInclude)) {
            typedQuery = typedQuery.setParameter("regUser", regUser);
        } else {
            typedQuery = typedQuery.setParameter("regUser", regUser.substring(notInclude.length()));
        }
    }
    // regFromDate
    if (regFromDate > 0) {
        typedQuery = typedQuery.setParameter("regFromDate", regFromDate);
    }
    // regToDate
    if (regToDate > 0) {
        typedQuery = typedQuery.setParameter("regToDate", regToDate);
    }
    // updateUser
    if (updateUser != null && !"".equals(updateUser)) {
        if (!updateUser.startsWith(notInclude)) {
            typedQuery = typedQuery.setParameter("updateUser", updateUser);
        } else {
            typedQuery = typedQuery.setParameter("updateUser", updateUser.substring(notInclude.length()));
        }
    }
    // updateFromDate
    if (updateFromDate > 0) {
        typedQuery = typedQuery.setParameter("updateFromDate", updateFromDate);
    }
    // updateToDate
    if (updateToDate > 0) {
        typedQuery = typedQuery.setParameter("updateToDate", updateToDate);
    }
    // monitorFlg
    if (monitorFlg != null) {
        typedQuery = typedQuery.setParameter("monitorFlg", monitorFlg);
    }
    // collectorFlg
    if (collectorFlg != null) {
        typedQuery = typedQuery.setParameter("collectorFlg", collectorFlg);
    }
    // ownerRoleId
    if (ownerRoleId != null && !"".equals(ownerRoleId)) {
        if (!ownerRoleId.startsWith(notInclude)) {
            typedQuery = typedQuery.setParameter("ownerRoleId", ownerRoleId);
        } else {
            typedQuery = typedQuery.setParameter("ownerRoleId", ownerRoleId.substring(notInclude.length()));
        }
    }
    return typedQuery.getResultList();
}

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//from  ww w  . j  a v  a 2 s.c o  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// ww w  .j a v  a  2  s.c o m
 * @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;
}