Example usage for javax.persistence Parameter getName

List of usage examples for javax.persistence Parameter getName

Introduction

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

Prototype

String getName();

Source Link

Document

Return the parameter name, or null if the parameter is not a named parameter or no name has been assigned.

Usage

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

/**
 * @param queryString//from   www.  j  a  v a  2  s  .co 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 .  java 2s . c o  m*/
 * @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:net.sf.gazpachoquest.qbe.NamedQueryUtil.java

private void setQueryParameters(final Query query, final SearchParameters sp) {
    // add default parameter if specified in the named query
    for (Parameter<?> p : query.getParameters()) {
        if (NAMED_PARAMETER_NOW.equals(p.getName())) {
            query.setParameter(NAMED_PARAMETER_NOW, Calendar.getInstance().getTime());
        }/*  w  w w  . j av  a 2 s.  c  o  m*/
    }

    // add parameters for the named query
    for (String paramName : sp.getNamedQueryParameters().keySet()) {
        query.setParameter(paramName, sp.getNamedQueryParameter(paramName));
    }
}

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

/**
 * @param queryString/*from ww w.  j  av a2s .  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:org.jdal.dao.jpa.JpaDao.java

/**
 * Apply filter to parametriced Query//from w  w w  .java2  s  . co  m
 * @param query the query to apply filter on
 * @param filter Filter to apply
 */
private void applyFilter(Query query, Filter filter) {
    Map<String, Object> parameterMap = filter.getParameterMap();
    for (Parameter<?> p : query.getParameters()) {
        if (parameterMap.containsKey(p.getName())) {
            query.setParameter(p.getName(), parameterMap.get(p.getName()));
        } else {
            throw new InvalidDataAccessApiUsageException("Parameter " + p.getName() + "was not found in filter "
                    + filter.getFilterName() + ". Review NamedQuery or Filter.");
        }
    }
}

From source file:net.nan21.dnet.core.presenter.action.query.QueryBuilderWithJpql.java

private void bindFilterParams(Query q) throws Exception {

    if (this.defaultFilterItems != null) {
        for (String key : this.defaultFilterItems.keySet()) {
            Object value = this.defaultFilterItems.get(key);
            try {
                if (value instanceof java.lang.String) {
                    q.setParameter(key, ((String) value).replace('*', '%'));
                } else {
                    q.setParameter(key, value);
                }//from ww  w.  ja v a  2  s.c o m
            } catch (IllegalArgumentException e) {
                if (logger.isDebugEnabled()) {
                    logger.debug(e.getMessage());
                }
            }
        }
    }
    if (this.customFilterItems != null) {
        for (String key : this.customFilterItems.keySet()) {
            Object value = this.customFilterItems.get(key);
            if (value instanceof java.lang.String) {
                q.setParameter(key, ((String) value).replace('*', '%'));
            } else {
                q.setParameter(key, value);
            }
        }
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Bound filter params:");
        for (Parameter<?> p : q.getParameters()) {
            try {
                logger.debug(" -> " + p.getName() + " = " + q.getParameterValue(p));
            } catch (Exception e) {
                // maybe a parameter has not been bound
            }
        }
    }
}

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

/**
 * Sets the parameters./*from  ww w .j  a v  a2 s.  com*/
 * 
 * @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  ww  .j a  v a2s  .  c o  m*/
 * 
 * @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:nl.strohalm.cyclos.utils.database.DatabaseQueryHandler.java

/**
 * Sets the query bind named parameters//from   w  w  w  . ja va 2  s  .co  m
 */
public void setQueryParameters(final Query query, final Object parameters) {
    if (parameters != null) {
        if (parameters instanceof Map<?, ?>) {
            final Map<?, ?> map = (Map<?, ?>) parameters;
            final String[] paramNames = new String[query.getParameters().size()];
            int i = 0;
            for (Parameter param : query.getParameters()) {
                paramNames[i] = param.getName();
                i++;
            }
            for (final String param : paramNames) {
                final Object value = map.get(param);
                if (value instanceof Collection<?>) {
                    final Collection<Object> values = new ArrayList<>(((Collection<?>) value).size());
                    for (final Object object : (Collection<?>) value) {
                        if (object instanceof EntityReference) {
                            values.add(fetchDao.fetch((Entity) object));
                        } else {
                            values.add(object);
                        }
                    }
                    query.setParameter(param, values);
                    //query.setParameterList(param, values);
                } else if (value instanceof EntityReference) {
                    query.setParameter(param, fetchDao.fetch((Entity) value));
                } else {
                    query.setParameter(param, value);
                }
            }
        } else {
            //query.setProperties(parameters);
        }
    }
}

From source file:org.debux.webmotion.jpa.GenericDAO.java

/**
 * Set parameter in the query./* w w  w  . j  a  v a2 s. c o m*/
 * 
 * @param query query
 * @param parameters parameters
 */
protected void extract(Query query, Parameters parameters) {
    Set<Parameter<?>> queryParameters = query.getParameters();
    for (Parameter<?> parameter : queryParameters) {
        String parameterName = parameter.getName();
        Object[] values = parameters.get(parameterName);

        List<Object> converted = Arrays.asList(values);
        query.setParameter(parameterName, converted);
    }
}