/**
   * 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;
  }
Exemple #2
-21
  /**
   * 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;
  }