Example usage for javax.persistence Parameter getParameterType

List of usage examples for javax.persistence Parameter getParameterType

Introduction

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

Prototype

Class<T> getParameterType();

Source Link

Document

Return the Java type of the parameter.

Usage

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

/**
 * @param queryString/*from w w w  .  j a v a2s  .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/*from   w w w  .  ja  v a  2  s.  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//ww  w . jav a  2 s .  co m
 * @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:org.eclipse.emf.teneo.hibernate.HbEntityDataStore.java

@SuppressWarnings({ "rawtypes", "unchecked" })
protected QueryImpl<?> repairParameterJavaType(QueryImpl<?> query) {
    try {/*from   w w w  .j av  a 2 s .co  m*/
        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);
    }
}