Ejemplo n.º 1
1
  @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;
  }
Ejemplo n.º 2
0
 @Override
 public Object bindBean(RootParamNode rootParamNode, String name, Object bean) {
   if (bean instanceof JPABase) {
     return GenericModel.edit(rootParamNode, name, bean, null);
   }
   return null;
 }
Ejemplo n.º 3
0
 @Override
 public Object bind(String name, Object o, Map<String, String[]> params) {
   if (o instanceof JPABase) {
     return GenericModel.edit(o, name, params, null);
   }
   return null;
 }
Ejemplo n.º 4
0
 public static String getStringId(GenericModel<?> hasId) {
   String stringId = null;
   if (hasId != null) {
     Long id = hasId.getId();
     stringId = getStringId(id);
   }
   return stringId;
 }
Ejemplo n.º 5
0
 @Override
 @SuppressWarnings("unchecked")
 public Object bind(
     String name,
     Class clazz,
     java.lang.reflect.Type type,
     Annotation[] annotations,
     Map<String, String[]> params) {
   // TODO need to be more generic in order to work with JPASupport
   if (JPABase.class.isAssignableFrom(clazz)) {
     String keyName = Model.Manager.factoryFor(clazz).keyName();
     String idKey = name + "." + keyName;
     if (params.containsKey(idKey)
         && params.get(idKey).length > 0
         && params.get(idKey)[0] != null
         && params.get(idKey)[0].trim().length() > 0) {
       String id = params.get(idKey)[0];
       try {
         Query query =
             JPA.em().createQuery("from " + clazz.getName() + " o where o." + keyName + " = ?");
         query.setParameter(
             1,
             play.data.binding.Binder.directBind(
                 name, annotations, id + "", Model.Manager.factoryFor(clazz).keyType()));
         Object o = query.getSingleResult();
         return GenericModel.edit(o, name, params, annotations);
       } catch (NoResultException e) {
         // ok
       } catch (Exception e) {
         throw new UnexpectedException(e);
       }
     }
     return GenericModel.create(clazz, name, params, annotations);
   }
   return super.bind(name, clazz, type, annotations, params);
 }