@Override public Object bind( RootParamNode rootParamNode, String name, Class clazz, java.lang.reflect.Type type, Annotation[] annotations) { // TODO need to be more generic in order to work with JPASupport if (clazz.isAnnotationPresent(Entity.class)) { ParamNode paramNode = rootParamNode.getChild(name, true); String[] keyNames = new JPAModelLoader(clazz).keyNames(); ParamNode[] ids = new ParamNode[keyNames.length]; // Collect the matching ids int i = 0; for (String keyName : keyNames) { ids[i++] = paramNode.getChild(keyName, true); } if (ids != null && ids.length > 0) { try { EntityManager em = JPABase.getJPAConfig(clazz).getJPAContext().em(); StringBuilder q = new StringBuilder().append("from ").append(clazz.getName()).append(" o where"); int keyIdx = 1; for (String keyName : keyNames) { q.append(" o.").append(keyName).append(" = ?").append(keyIdx++).append(" and "); } if (q.length() > 4) { q = q.delete(q.length() - 4, q.length()); } Query query = em.createQuery(q.toString()); // The primary key can be a composite. Class[] pk = new JPAModelLoader(clazz).keyTypes(); int j = 0; for (ParamNode id : ids) { if (id.getValues() == null || id.getValues().length == 0 || id.getFirstValue(null) == null || id.getFirstValue(null).trim().length() <= 0) { // We have no ids, it is a new entity return GenericModel.create(rootParamNode, name, clazz, annotations); } query.setParameter( j + 1, Binder.directBind( id.getOriginalKey(), annotations, id.getValues()[0], pk[j++], null)); } Object o = query.getSingleResult(); return GenericModel.edit(rootParamNode, name, o, annotations); } catch (NoResultException e) { // ok } catch (Exception e) { throw new UnexpectedException(e); } } return GenericModel.create(rootParamNode, name, clazz, annotations); } return null; }
public RootParamNode getRootParamNode() { checkAndParse(); if (!rootParamsNodeIsGenerated) { rootParamNode = ParamNode.convert(data); rootParamsNodeIsGenerated = true; } return rootParamNode; }
public static Object[] getActionMethodArgs(Method method, Object o) throws Exception { String[] paramsNames = Java.parameterNames(method); if (paramsNames == null && method.getParameterTypes().length > 0) { throw new UnexpectedException("Parameter names not found for method " + method); } // Check if we have already performed the bind operation Object[] rArgs = CachedBoundActionMethodArgs.current().retrieveActionMethodArgs(method); if (rArgs != null) { // We have already performed the binding-operation for this method // in this request. return rArgs; } rArgs = new Object[method.getParameterTypes().length]; for (int i = 0; i < method.getParameterTypes().length; i++) { Class<?> type = method.getParameterTypes()[i]; Map<String, String[]> params = new HashMap<String, String[]>(); // In case of simple params, we don't want to parse the body. if (type.equals(String.class) || Number.class.isAssignableFrom(type) || type.isPrimitive()) { params.put(paramsNames[i], Scope.Params.current().getAll(paramsNames[i])); } else { params.putAll(Scope.Params.current().all()); } Logger.trace( "getActionMethodArgs name [" + paramsNames[i] + "] annotation [" + Utils.join(method.getParameterAnnotations()[i], " ") + "]"); RootParamNode root = ParamNode.convert(params); rArgs[i] = Binder.bind( root, paramsNames[i], method.getParameterTypes()[i], method.getGenericParameterTypes()[i], method.getParameterAnnotations()[i], new Binder.MethodAndParamInfo(o, method, i + 1)); } CachedBoundActionMethodArgs.current().storeActionMethodArgs(method, rArgs); return rArgs; }
public RootParamNode getRootParamNodeFromRequest() { return ParamNode.convert(data); }