예제 #1
0
  /**
   * Returns the correct implementation instance for the interface <code>type</code>. For convention
   * the implentation is named <code>InterfaceName + Impl</code>.
   *
   * @param type The interface type
   * @return The correct ModelProxy subclass (if exists), a ModelProxy instance otherwise
   * @throws MalformedModelException is the interface or the implementation are not well formed
   *     (they don't respect the conventions).
   * @throws ModelRuntimeException is any error occurs during the instantiation
   */
  protected ModelProxy createInstance(Class type) {
    Class backClass;
    if (implementations.containsKey(type)) backClass = implementations.get(type);
    else {
      /* type never seen */

      try {
        Package pkg = type.getPackage();
        String pkgN = pkg == null ? "" : pkg.getName();

        backClass = Class.forName(pkgN + tableName(type) + CommonStatic.getImplementationSuffix());

      } catch (Exception e) {
        backClass = ModelProxy.class;
      }

      Validator.validateModel(type, backClass);

      initFieldsTypes(type);
      implementations.put(type, backClass);
    }

    ModelProxy impl = null;
    try {
      impl = (ModelProxy) backClass.newInstance();
    } catch (Exception e) {
      throw new ModelRuntimeException(e.getMessage());
    }

    return impl;
  }
예제 #2
0
  /**
   * Returns the conventional table name for a {@link Model} interface.
   *
   * @param type A valid Model interface.
   * @return The conventional table name for the given interface.
   */
  protected String tableName(Class type) {

    if (tableNames.containsKey(type)) return tableNames.get(type);

    String tN = CommonStatic.tableName(type);

    tableNames.put(type, tN);
    return tN;
  }
예제 #3
0
  /**
   * Returns the conventional field name for a getter method.
   *
   * @param m The field getter.
   * @return The field name.
   * @throws IllegalArgumentException if the method isn't a valid getter.
   */
  protected String fieldName(Method m) {

    if (fieldNames.containsKey(m)) return fieldNames.get(m);

    String name = CommonStatic.fieldName(m);

    fieldNames.put(m, name);
    return name;
  }
예제 #4
0
  protected boolean isAnArrayConnection(Class model, String field) {

    if (!arrayConnections.containsKey(model)) {
      arrayConnections.put(model, new HashMap<String, Boolean>());
    }

    Map<String, Boolean> map = arrayConnections.get(model);

    if (map.containsKey(field.toLowerCase())) return map.get(field.toLowerCase());
    else map.put(field.toLowerCase(), CommonStatic.isAnArrayConnection(model, field));

    return map.get(field.toLowerCase());
  }
예제 #5
0
  protected Class getConnectionFieldType(Class model, String field) {

    if (!connectionType.containsKey(model)) {
      connectionType.put(model, new HashMap<String, Class>());
    }

    Map<String, Class> map = connectionType.get(model);

    if (map.containsKey(field.toLowerCase())) return map.get(field.toLowerCase());
    else map.put(field.toLowerCase(), CommonStatic.getConnectionFieldType(model, field));

    return map.get(field.toLowerCase());
  }
예제 #6
0
  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);
  }
예제 #7
0
  protected Class getModelIdType(Class model) {
    if (!modelIdTypes.containsKey(model))
      modelIdTypes.put(model, CommonStatic.getModelIdType(model));

    return modelIdTypes.get(model);
  }
예제 #8
0
  /**
   * Returns the field with the @link{Id} in the Model interface. For single intheritance, if the Id
   * is not specified, will be the same of his father class. In multiple inheritance, in order to
   * avoid multiple keys identification the Id must be defined by the user.
   *
   * @param The model interface
   * @return The Id field name, or "Id" (the default id) if there's not.
   */
  protected String getModelId(Class model) {
    if (!modelIds.containsKey(model)) modelIds.put(model, CommonStatic.getModelId(model));

    return modelIds.get(model);
  }