Search in sources :

Example 1 with Owned

use of siena.core.Owned in project siena by mandubian.

the class ClassInfo method buildMany.

private void buildMany(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 Many<T> field " + c.getName() + "." + field.getName() + "with @Filter+@Owned 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);
            manyFieldMap.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());
            manyFieldMap.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);
            manyFieldMap.put(field, fieldMap);
            ownedFields.add(field);
            hasOwnedFields = true;
        } catch (Exception e) {
            throw new SienaException(e);
        }
    }
    allExtendedFields.add(field);
}
Also used : Aggregated(siena.core.Aggregated) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ParameterizedType(java.lang.reflect.ParameterizedType) Field(java.lang.reflect.Field) Owned(siena.core.Owned) InheritFilter(siena.core.InheritFilter) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 2 with Owned

use of siena.core.Owned in project siena by mandubian.

the class ClassInfo method buildQuery.

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);
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ParameterizedType(java.lang.reflect.ParameterizedType) Field(java.lang.reflect.Field) Owned(siena.core.Owned) InheritFilter(siena.core.InheritFilter) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 3 with Owned

use of siena.core.Owned in project siena by mandubian.

the class ClassInfo method buildOne.

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);
}
Also used : Aggregated(siena.core.Aggregated) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ParameterizedType(java.lang.reflect.ParameterizedType) Field(java.lang.reflect.Field) Owned(siena.core.Owned) InheritFilter(siena.core.InheritFilter) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

Field (java.lang.reflect.Field)3 ParameterizedType (java.lang.reflect.ParameterizedType)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 InheritFilter (siena.core.InheritFilter)3 Owned (siena.core.Owned)3 Aggregated (siena.core.Aggregated)2