/** * If a {@link Model} is part of a composition, and is the weak part (is part of other models), * this method returns all the fields containing connection of {@link ConnectionType.BelongsTo} * type. It's useful in the <code>INSERT</code> of the record, for preventing foreign key * costraint inconsistency. * * @param model The given {@link Model}. * @return A list of fields that represent the Models that owns <code>model</code>. */ protected List<String> belongsTo(Class model) { if (belongsToFields.containsKey(model)) return belongsToFields.get(model); List<Class> sC = getSupers(model); List<String> ownerFields = new ArrayList<String>(); Method[] getters = CommonStatic.getDeclaredGetters(model); for (Method g : getters) /* has a belongs to annotation */ if (g.isAnnotationPresent(Connection.class) && g.getAnnotation(Connection.class).type().equals(ConnectionType.BelongsTo)) { ownerFields.add(fieldName(g)); } for (Class s : sC) ownerFields.addAll(belongsTo(s)); belongsToFields.put(model, ownerFields); return ownerFields; }
private void initFieldsTypes(Class type) { if (types.containsKey(type)) return; // TODO: Checks the connections - handle them differently Map<String, Class> tMap = new HashMap<String, Class>(); Method[] getters = CommonStatic.getGetters(type); for (Method m : getters) if (!m.isAnnotationPresent(Connection.class)) tMap.put(fieldName(m), m.getReturnType()); /* In inheritance case */ if (extendsModels(type)) { List<Class> supers = getSupers(type); for (Class c : supers) tMap.put(/*tableName(c)+*/ getModelId(type), c); } tMap.put("CreatedAt", java.sql.Timestamp.class); tMap.put("UpdatedAt", java.sql.Timestamp.class); types.put(type, tMap); }
/** * Returns all the classes related to <code>type</code> by a {@link Connection} * * @param type The given {@link Model} interface. * @return A list that contains all the models in a relationship with <code>type</code> interface. */ protected List<Class> getAllReleatedClasses(Class type) { if (relatedClasses.containsKey(type)) return relatedClasses.get(type); List<Class> related = new ArrayList<Class>(); Method[] getters = CommonStatic.getDeclaredGetters(type); for (Method g : getters) if (g.isAnnotationPresent(Connection.class)) { Class t = g.getReturnType(); t = t.isArray() ? t.getComponentType() : t; if (!related.contains(t)) related.add(t); } relatedClasses.put(type, related); return related; }