* @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);