Example usage for javax.persistence Parameter getPosition

List of usage examples for javax.persistence Parameter getPosition

Introduction

In this page you can find the example usage for javax.persistence Parameter getPosition.

Prototype

Integer getPosition();

Source Link

Document

Return the parameter position, or null if the parameter is not a positional parameter.

Usage

From source file:com.impetus.kundera.rest.common.EntityUtils.java

/**
 * @param queryString//from   ww w.j a  v  a  2 s  .c o m
 * @param q
 */
public static void setQueryParameters(String queryString, HashMap<String, String> paramsMap, Query q) {
    KunderaQuery kq = ((QueryImpl) q).getKunderaQuery();
    Set<Parameter<?>> parameters = kq.getParameters();
    for (String paramName : paramsMap.keySet()) {
        String value = paramsMap.get(paramName);

        if (StringUtils.isNumeric(paramName)) {
            for (Parameter param : parameters) {
                if (param.getPosition() == Integer.parseInt(paramName)) {
                    Class<?> paramClass = param.getParameterType();
                    PropertyAccessor accessor = PropertyAccessorFactory.getPropertyAccessor(paramClass);
                    Object paramValue = accessor.fromString(paramClass, value);
                    q.setParameter(Integer.parseInt(paramName), paramValue);
                    break;
                }
            }
        } else {
            for (Parameter param : parameters) {
                if (param.getName().equals(paramName)) {
                    Class<?> paramClass = param.getParameterType();
                    PropertyAccessor accessor = PropertyAccessorFactory.getPropertyAccessor(paramClass);
                    Object paramValue = accessor.fromString(paramClass, value);
                    if (paramName.equalsIgnoreCase("firstResult")) {
                        q.setFirstResult(Integer.parseInt((String) paramValue));
                    } else if (paramName.equalsIgnoreCase("maxResult")) {
                        q.setMaxResults(Integer.parseInt((String) paramValue));
                    } else {
                        q.setParameter(paramName, paramValue);
                    }
                    break;
                }
            }

        }
    }
}

From source file:com.impetus.kundera.rest.common.EntityUtils.java

/**
 * @param queryString//w  w w  .  ja  v a 2s.c  om
 * @param q
 */
public static void setQueryParameters(String queryString, String parameterString, Query q) {
    Map<String, String> paramsMap = new HashMap<String, String>();

    StringTokenizer st = new StringTokenizer(parameterString, "&");
    while (st.hasMoreTokens()) {
        String element = st.nextToken();
        paramsMap.put(element.substring(0, element.indexOf("=")),
                element.substring(element.indexOf("=") + 1, element.length()));
    }
    KunderaQuery kq = ((QueryImpl) q).getKunderaQuery();
    Set<Parameter<?>> parameters = kq.getParameters();
    for (String paramName : paramsMap.keySet()) {
        String value = paramsMap.get(paramName);
        if (paramName.equalsIgnoreCase("firstResult")) {
            q.setFirstResult(Integer.parseInt(value));
        } else if (paramName.equalsIgnoreCase("maxResult")) {
            q.setMaxResults(Integer.parseInt(value));
        } else if (StringUtils.isNumeric(paramName)) {
            for (Parameter param : parameters) {
                if (param.getPosition() == Integer.parseInt(paramName)) {
                    Class<?> paramClass = param.getParameterType();
                    PropertyAccessor accessor = PropertyAccessorFactory.getPropertyAccessor(paramClass);
                    Object paramValue = accessor.fromString(paramClass, value);
                    q.setParameter(Integer.parseInt(paramName), paramValue);
                    break;
                }
            }
        } else {
            for (Parameter param : parameters) {
                if (param.getName().equals(paramName)) {
                    Class<?> paramClass = param.getParameterType();
                    PropertyAccessor accessor = PropertyAccessorFactory.getPropertyAccessor(paramClass);
                    Object paramValue = accessor.fromString(paramClass, value);
                    q.setParameter(paramName, paramValue);

                    break;
                }
            }

        }
    }
}

From source file:com.impetus.kundera.rest.common.EntityUtils.java

/**
 * @param queryString/*from  w  ww . j a v  a  2 s  . c  om*/
 * @param q
 * @param em
 */
public static void setObjectQueryParameters(String queryString, String parameterString, Query q,
        EntityManager em, String mediaType) {
    MetamodelImpl metamodel = (MetamodelImpl) em.getEntityManagerFactory().getMetamodel();

    if (parameterString == null || parameterString.isEmpty()) {
        return;
    }

    Map<String, String> paramsMap = new HashMap<String, String>();
    ObjectMapper mapper = new ObjectMapper();

    try {
        paramsMap = mapper.readValue(parameterString, new TypeReference<HashMap<String, String>>() {
        });
        KunderaQuery kq = ((QueryImpl) q).getKunderaQuery();
        Set<Parameter<?>> parameters = kq.getParameters();
        for (String paramName : paramsMap.keySet()) {
            String value = paramsMap.get(paramName);

            if (paramName.equalsIgnoreCase("firstResult")) {
                q.setFirstResult(Integer.parseInt(value));

            } else if (paramName.equalsIgnoreCase("maxResult")) {
                q.setMaxResults(Integer.parseInt(value));

            } else if (StringUtils.isNumeric(paramName)) {
                for (Parameter param : parameters) {
                    if (param.getPosition() == Integer.parseInt(paramName)) {

                        Class<?> paramClass = param.getParameterType();
                        Object paramValue = null;
                        if (metamodel.isEmbeddable(paramClass)) {
                            paramValue = JAXBUtils.toObject(StreamUtils.toInputStream(value), paramClass,
                                    mediaType);

                        } else {
                            PropertyAccessor accessor = PropertyAccessorFactory.getPropertyAccessor(paramClass);
                            paramValue = accessor.fromString(paramClass, value);
                        }

                        q.setParameter(Integer.parseInt(paramName), paramValue);
                        break;
                    }
                }
            } else {
                for (Parameter param : parameters) {
                    if (param.getName().equals(paramName)) {

                        Class<?> paramClass = param.getParameterType();
                        Object paramValue = null;

                        if (metamodel.isEmbeddable(paramClass)) {
                            paramValue = JAXBUtils.toObject(StreamUtils.toInputStream(value), paramClass,
                                    mediaType);

                        } else {
                            PropertyAccessor accessor = PropertyAccessorFactory.getPropertyAccessor(paramClass);
                            paramValue = accessor.fromString(paramClass, value);
                        }
                        q.setParameter(paramName, paramValue);
                        break;
                    }
                }

            }
        }

    } catch (JsonParseException e) {
        log.error(e.getMessage());
    } catch (JsonMappingException e) {
        log.error(e.getMessage());
    } catch (IOException e) {
        log.error(e.getMessage());
    }
}

From source file:com.impetus.client.rdbms.HibernateClient.java

/**
 * Sets the parameters.//from  ww w .  j ava  2 s .  co  m
 * 
 * @param parameterMap
 *            the parameter map
 * @param q
 *            the q
 */
private void setParameters(Map<Parameter, Object> parameterMap, Query q) {
    if (parameterMap != null && !parameterMap.isEmpty()) {
        for (Parameter parameter : parameterMap.keySet()) {
            Object paramObject = parameterMap.get(parameter);
            if (parameter.getName() != null) {
                if (paramObject instanceof Collection) {
                    q.setParameterList(parameter.getName(), (Collection) paramObject);
                } else {
                    q.setParameter(parameter.getName(), paramObject);
                }
            } else if (parameter.getPosition() != null) {
                if (paramObject instanceof Collection) {
                    q.setParameterList(Integer.toString(parameter.getPosition()), (Collection) paramObject);
                } else {
                    q.setParameter(Integer.toString(parameter.getPosition()), paramObject);
                }
            }
        }
    }
}

From source file:com.impetus.kundera.query.KunderaQuery.java

/**
 * Returns specific clause value.//from w w  w.  j  av  a  2 s.c om
 * 
 * @param param
 *            parameter
 * 
 * @return clause value.
 */
public List<Object> getClauseValue(Parameter param) {
    Parameter match = null;
    if (typedParameter != null && typedParameter.jpaParameters != null) {
        for (Parameter p : typedParameter.jpaParameters) {
            if (p.equals(param)) {
                match = p;
                if (typedParameter.getType().equals(Type.NAMED)) {
                    List<FilterClause> clauses = typedParameter.getParameters().get(":" + p.getName());
                    if (clauses != null) {
                        return clauses.get(0).getValue();
                    }
                } else {
                    List<FilterClause> clauses = typedParameter.getParameters().get("?" + p.getPosition());
                    if (clauses != null) {
                        return clauses.get(0).getValue();
                    } else {
                        UpdateClause updateClause = typedParameter.getUpdateParameters()
                                .get("?" + p.getPosition());
                        if (updateClause != null) {
                            List<Object> value = new ArrayList<Object>();
                            value.add(updateClause.getValue());
                            return value;
                        }

                    }
                }
                break;
            }
        }
        if (match == null) {
            throw new IllegalArgumentException("parameter is not a parameter of the query");
        }
    }

    logger.error("parameter{} is not a parameter of the query", param);
    throw new IllegalArgumentException("parameter is not a parameter of the query");
}

From source file:org.batoo.jpa.core.impl.nativeQuery.NativeQuery.java

/**
 * {@inheritDoc}//from  w w w .  ja va2 s .co m
 * 
 */
@Override
public NativeQuery setParameter(Parameter<Calendar> param, Calendar value, TemporalType temporalType) {
    this.parameterValues.put(this.getParameter0(param.getPosition()),
            this.convertTemporal(temporalType, value));

    return this;

}

From source file:org.batoo.jpa.core.impl.nativeQuery.NativeQuery.java

/**
 * {@inheritDoc}/*from   w w w. ja  v a2  s.  c  o m*/
 * 
 */
@Override
public NativeQuery setParameter(Parameter<Date> param, Date value, TemporalType temporalType) {
    this.parameterValues.put(this.getParameter0(param.getPosition()),
            this.convertTemporal(temporalType, value));

    return this;
}

From source file:org.batoo.jpa.core.impl.nativeQuery.NativeQuery.java

/**
 * {@inheritDoc}/*from   www . jav a2s  . co  m*/
 * 
 */
@Override
public <T> NativeQuery setParameter(Parameter<T> param, T value) {
    this.parameterValues.put(this.getParameter0(param.getPosition()), value);

    return this;
}

From source file:org.eclipse.emf.teneo.hibernate.HbEntityDataStore.java

@SuppressWarnings({ "rawtypes", "unchecked" })
protected QueryImpl<?> repairParameterJavaType(QueryImpl<?> query) {
    try {/*from  ww w.j a v  a2 s .c om*/
        final Set<Parameter<?>> repairedParameters = new HashSet<Parameter<?>>();
        final Object parametersObject = getQueryParametersField().get(query);
        final AbstractQueryImpl queryImpl = AbstractQueryImpl.class.cast(query.getHibernateQuery());
        for (Parameter<?> parameter : (Collection<Parameter>) parametersObject) {
            if (Map.class == parameter.getParameterType()) {
                final Type type;
                if (parameter.getName() != null) {
                    // repair these ones
                    final NamedParameterDescriptor descriptor = queryImpl.getParameterMetadata()
                            .getNamedParameterDescriptor(parameter.getName());
                    type = descriptor.getExpectedType();
                } else {
                    type = queryImpl.getParameterMetadata()
                            .getOrdinalParameterExpectedType(parameter.getPosition());
                }
                if (type instanceof EntityType) {
                    final Parameter<?> param = new ParameterImpl(parameter.getName(), parameter.getPosition(),
                            Object.class);
                    repairedParameters.add(param);
                    // final EntityType entityType = (EntityType) type;
                    // final String entityName = entityType
                    // .getAssociatedEntityName();
                    // final EClass eClass = HbEntityDataStore.this
                    // .getEntityNameStrategy().toEClass(entityName);

                } else {
                    repairedParameters.add(parameter);
                }
            } else {
                repairedParameters.add(parameter);
            }
        }
        getQueryParametersField().set(query, repairedParameters);
        return query;
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
}

From source file:org.springframework.integration.jpa.core.DefaultJpaOperations.java

/**
 * Given a JPQL query, this method gets all parameters defined in this query and
 * use the {@link ParameterSource} to find their values and set them.
 *
 *///from w  w w  .  jav a  2s. c  om
private void setParametersIfRequired(String queryString, ParameterSource source, Query query) {
    Set<Parameter<?>> parameters = query.getParameters();

    if (parameters != null && !parameters.isEmpty()) {
        if (source != null) {
            for (Parameter<?> param : parameters) {
                String paramName = param.getName();
                Integer position = param.getPosition();

                final Object paramValue;

                if (position != null) {

                    if (source instanceof PositionSupportingParameterSource) {
                        paramValue = ((PositionSupportingParameterSource) source).getValueByPosition(position);
                        query.setParameter(position, paramValue);
                    } else {
                        throw new JpaOperationFailedException("Positional Parameters are only support "
                                + "for PositionSupportingParameterSources.", queryString);
                    }

                } else {

                    if (StringUtils.hasText(paramName)) {
                        paramValue = source.getValue(paramName);
                        query.setParameter(paramName, paramValue);
                    } else {
                        throw new JpaOperationFailedException(
                                "This parameter does not contain a parameter name. "
                                        + "Additionally it is not a positional parameter, neither.",
                                queryString);
                    }
                }

            }
        } else {
            throw new IllegalArgumentException("Query has parameters but no parameter source provided");
        }

    }
}