Exemple #1
0
    private Object makeCompositeKey(Model model) throws Exception {
      initProperties();
      Class<?> idClass = getCompositeKeyClass();
      Object id = idClass.newInstance();
      PropertyDescriptor[] idProperties = PropertyUtils.getPropertyDescriptors(idClass);
      if (idProperties == null || idProperties.length == 0)
        throw new UnexpectedException("Composite id has no properties: " + idClass.getName());
      for (PropertyDescriptor idProperty : idProperties) {
        // do we have a field for this?
        String idPropertyName = idProperty.getName();
        // skip the "class" property...
        if (idPropertyName.equals("class")) continue;
        Model.Property modelProperty = this.properties.get(idPropertyName);
        if (modelProperty == null)
          throw new UnexpectedException(
              "Composite id property missing: "
                  + clazz.getName()
                  + "."
                  + idPropertyName
                  + " (defined in IdClass "
                  + idClass.getName()
                  + ")");
        // sanity check
        Object value = modelProperty.field.get(model);

        if (modelProperty.isMultiple)
          throw new UnexpectedException(
              "Composite id property cannot be multiple: "
                  + clazz.getName()
                  + "."
                  + idPropertyName);
        // now is this property a relation? if yes then we must use its ID in the key (as per specs)
        if (modelProperty.isRelation) {
          // get its id
          if (!Model.class.isAssignableFrom(modelProperty.type))
            throw new UnexpectedException(
                "Composite id property entity has to be a subclass of Model: "
                    + clazz.getName()
                    + "."
                    + idPropertyName);
          // we already checked that cast above
          @SuppressWarnings("unchecked")
          Model.Factory factory =
              Model.Manager.factoryFor((Class<? extends Model>) modelProperty.type);
          if (factory == null)
            throw new UnexpectedException(
                "Failed to find factory for Composite id property entity: "
                    + clazz.getName()
                    + "."
                    + idPropertyName);
          // we already checked that cast above
          if (value != null) value = factory.keyValue((Model) value);
        }
        // now affect the composite id with this id
        PropertyUtils.setSimpleProperty(id, idPropertyName, value);
      }
      return id;
    }
Exemple #2
0
    public Model findById(String id) throws Exception {
      if (id == null) {
        return null;
      }

      Factory factory = Model.Manager.factoryFor(entityClass);
      Object boundId = Binder.directBind(id, factory.keyType());
      return factory.findById(boundId);
    }
Exemple #3
0
 @SuppressWarnings("unchecked")
 public List<Model> findPage(
     int page, String search, String searchFields, String orderBy, String order, String where) {
   int offset = (page - 1) * getPageSize();
   List<String> properties =
       searchFields == null
           ? new ArrayList<String>(0)
           : Arrays.asList(searchFields.split("[ ]"));
   return Model.Manager.factoryFor(entityClass)
       .fetch(offset, getPageSize(), orderBy, order, properties, search, where);
 }
Exemple #4
0
 public Model findById(Object id) {
   if (id == null) {
     return null;
   }
   try {
     return JPA.em()
         .find(
             clazz, Binder.directBind(id.toString(), Model.Manager.factoryFor(clazz).keyType()));
   } catch (Exception e) {
     // Key is invalid, thus nothing was found
     return null;
   }
 }
Exemple #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);
 }
Exemple #6
0
 public ObjectType(Class<? extends Model> modelClass) {
   this.modelName = modelClass.getSimpleName();
   this.entityClass = modelClass;
   this.factory = Model.Manager.factoryFor(entityClass);
   this.keyName = factory.keyName();
 }