Esempio n. 1
0
  private void buildQuery(Field field, Class<?> c) {
    Class<?> type = field.getType();

    Filter filter = field.getAnnotation(Filter.class);
    Owned related = field.getAnnotation(Owned.class);
    if (filter == null && related == null) {
      throw new SienaException(
          "Found Query<T> field without @Filter or @Owned annotation at "
              + c.getName()
              + "."
              + field.getName());
    }

    ParameterizedType pt = (ParameterizedType) field.getGenericType();
    Class<?> cl = (Class<?>) pt.getActualTypeArguments()[0];

    if (filter != null) {
      try {
        Map<FieldMapKeys, Object> fieldMap = new HashMap<FieldMapKeys, Object>();
        fieldMap.put(FieldMapKeys.CLASS, cl);
        fieldMap.put(FieldMapKeys.FILTER, filter.value());
        queryFieldMap.put(field, fieldMap);
        ownedFields.add(field);
        hasOwnedFields = true;
      } catch (Exception e) {
        throw new SienaException(e);
      }
    } else if (related != null) {
      String as = related.mappedBy();
      // if related.as not specified, tries to find the first field with this type
      if ("".equals(as) || as == null) {
        ClassInfo fieldInfo = ClassInfo.getClassInfo(cl);
        Field f = fieldInfo.getFirstFieldFromType(clazz);
        if (f == null) {
          throw new SienaException(
              "@Owned without 'as' attribute and no field of type "
                  + clazz.getName()
                  + "found in class "
                  + type.getName());
        }

        as = ClassInfo.getSimplestColumnName(f);
      }
      try {
        Map<FieldMapKeys, Object> fieldMap = new HashMap<FieldMapKeys, Object>();
        fieldMap.put(FieldMapKeys.CLASS, cl);
        fieldMap.put(FieldMapKeys.FILTER, as);
        queryFieldMap.put(field, fieldMap);
        ownedFields.add(field);
        hasOwnedFields = true;
      } catch (Exception e) {
        throw new SienaException(e);
      }
    }

    allExtendedFields.add(field);
  }
Esempio n. 2
0
  private void buildOne(Field field, Class<?> c) {
    Class<?> type = field.getType();
    ParameterizedType pt = (ParameterizedType) field.getGenericType();
    Class<?> cl = (Class<?>) pt.getActualTypeArguments()[0];

    Aggregated agg = field.getAnnotation(Aggregated.class);
    Filter filter = field.getAnnotation(Filter.class);
    Owned related = field.getAnnotation(Owned.class);
    if ((agg != null && filter != null) || (agg != null && related != null)) {
      throw new SienaException(
          "Found One<T> field "
              + c.getName()
              + "."
              + field.getName()
              + "with @Filter+@Aggregated or @Filter+@Owned: this is not authorized");
    }
    if (agg != null) {
      try {
        Map<FieldMapKeys, Object> fieldMap = new HashMap<FieldMapKeys, Object>();
        fieldMap.put(FieldMapKeys.CLASS, cl);
        fieldMap.put(FieldMapKeys.MODE, RelationMode.AGGREGATION);
        oneFieldMap.put(field, fieldMap);
        aggregatedFields.add(field);
        hasAggregatedFields = true;
      } catch (Exception e) {
        throw new SienaException(e);
      }
    } else if (filter != null) {
      try {
        Field filterField = cl.getField(filter.value());
        if (filterField == null) {
          throw new SienaException(
              "@Filter error: Couldn't find field " + filter.value() + "in class " + cl.getName());
        }
        Map<FieldMapKeys, Object> fieldMap = new HashMap<FieldMapKeys, Object>();
        fieldMap.put(FieldMapKeys.CLASS, cl);
        fieldMap.put(FieldMapKeys.MODE, RelationMode.RELATION);
        fieldMap.put(FieldMapKeys.FIELD, filterField);
        fieldMap.put(FieldMapKeys.FILTER, filter.value());
        oneFieldMap.put(field, fieldMap);
        ownedFields.add(field);
        hasOwnedFields = true;
      } catch (Exception e) {
        throw new SienaException(e);
      }
    } else if (related != null) {
      String as = related.mappedBy();
      // if related.as not specified, tries to find the first field with this type
      if ("".equals(as) || as == null) {
        ClassInfo fieldInfo = ClassInfo.getClassInfo(cl);
        Field f = fieldInfo.getFirstFieldFromType(clazz);
        if (f == null) {
          throw new SienaException(
              "@Owned without 'as' attribute and no field of type "
                  + clazz.getName()
                  + "found in class "
                  + type.getName());
        }

        as = ClassInfo.getSimplestColumnName(f);
      }
      try {
        Field asField = cl.getField(as);
        if (asField == null) {
          throw new SienaException(
              "@Filter error: Couldn't find field " + as + "in class " + cl.getName());
        }

        Map<FieldMapKeys, Object> fieldMap = new HashMap<FieldMapKeys, Object>();
        fieldMap.put(FieldMapKeys.CLASS, cl);
        fieldMap.put(FieldMapKeys.MODE, RelationMode.RELATION);
        fieldMap.put(FieldMapKeys.FIELD, asField);
        fieldMap.put(FieldMapKeys.FILTER, as);
        oneFieldMap.put(field, fieldMap);
        ownedFields.add(field);
        hasOwnedFields = true;
      } catch (Exception e) {
        throw new SienaException(e);
      }
    }

    allExtendedFields.add(field);
  }
 public static void main(String[] args) {
   for (Filter filter : Filterable.class.getAnnotationsByType(Filter.class)) {
     System.out.println(filter.value());
   }
 }