@Test
  public void givenPropertyType_whenCreate_thenReturnExpectedPropertyViewAttributeInstance() {
    for (Class<?> propertyType : propertyTypeToViewAttributeMappings.keySet()) {
      PropertyViewAttribute<?, ?> propertyViewAttribute = attribute.create(null, propertyType);

      assertThat(
          propertyViewAttribute, instanceOf(propertyTypeToViewAttributeMappings.get(propertyType)));
    }
  }
Example #2
0
 @Override
 protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   viewDelegate.create(getLayoutInflater(), null, savedInstanceState);
   setContentView(viewDelegate.getRootView());
   viewDelegate.initWidget();
   initToolbar();
   bindEvenListener();
 }
 public Long create(T dao, Object model) {
   Long id;
   try {
     id = (Long) dao.create(model);
   } catch (DAOException ex) {
     throw new CoreException("Erro de de acesso ao banco de dados: " + ex.getMessage());
   }
   return id;
 }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    create(savedInstanceState);

    try {
      mView = getViewClass().newInstance();
      mView.bindPresenter(this);
      setContentView(mView.create(getLayoutInflater(), null));
      mView.bindEvent();
      created(savedInstanceState);
    } catch (Exception e) {
      throw new RuntimeException(e.getMessage());
    }
  }
Example #5
0
  public static <T extends Factory> T objectFromJson(JsonObject json, T prototype)
      throws ParseException {
    T object = (T) prototype.create();

    Method[] methods = object.getClass().getMethods();

    for (final Method method : methods) {
      if (method.getName().startsWith("set") && method.getParameterTypes().length == 1) {

        final String name = Introspector.decapitalize(method.getName().substring(3));
        Class<?> parameterType = method.getParameterTypes()[0];

        if (json.containsKey(name))
          try {
            if (parameterType.equals(boolean.class)) {
              method.invoke(object, json.getBoolean(name));
            } else if (parameterType.equals(int.class)) {
              method.invoke(object, json.getJsonNumber(name).intValue());
            } else if (parameterType.equals(long.class)) {
              if (json.get(name).getValueType() == JsonValue.ValueType.NUMBER) {
                method.invoke(object, json.getJsonNumber(name).longValue());
              }
            } else if (parameterType.equals(double.class)) {
              method.invoke(object, json.getJsonNumber(name).doubleValue());
            } else if (parameterType.equals(String.class)) {
              method.invoke(object, json.getString(name));
            } else if (parameterType.equals(Date.class)) {
              method.invoke(object, dateFormat.parse(json.getString(name)));
            } else if (parameterType.equals(Map.class)) {
              method.invoke(object, MiscFormatter.fromJson(json.getJsonObject(name)));
            }
          } catch (IllegalAccessException | InvocationTargetException error) {
          }
      }
    }

    return object;
  }
Example #6
0
  public <T extends Factory> Collection<T> executeQuery(T prototype) throws SQLException {
    List<T> result = new LinkedList<>();

    if (query != null) {

      try {

        try (ResultSet resultSet = statement.executeQuery()) {

          ResultSetMetaData resultMetaData = resultSet.getMetaData();

          List<ResultSetProcessor<T>> processors = new LinkedList<>();

          Method[] methods = prototype.getClass().getMethods();

          for (final Method method : methods) {
            if (method.getName().startsWith("set") && method.getParameterTypes().length == 1) {

              final String name = method.getName().substring(3);

              // Check if column exists
              boolean column = false;
              for (int i = 1; i <= resultMetaData.getColumnCount(); i++) {
                if (name.equalsIgnoreCase(resultMetaData.getColumnLabel(i))) {
                  column = true;
                  break;
                }
              }
              if (!column) {
                continue;
              }

              Class<?> parameterType = method.getParameterTypes()[0];

              if (parameterType.equals(boolean.class)) {
                processors.add(
                    new ResultSetProcessor<T>() {
                      @Override
                      public void process(T object, ResultSet resultSet) throws SQLException {
                        try {
                          method.invoke(object, resultSet.getBoolean(name));
                        } catch (IllegalAccessException | InvocationTargetException error) {
                        }
                      }
                    });
              } else if (parameterType.equals(int.class)) {
                processors.add(
                    new ResultSetProcessor<T>() {
                      @Override
                      public void process(T object, ResultSet resultSet) throws SQLException {
                        try {
                          method.invoke(object, resultSet.getInt(name));
                        } catch (IllegalAccessException | InvocationTargetException error) {
                        }
                      }
                    });
              } else if (parameterType.equals(long.class)) {
                processors.add(
                    new ResultSetProcessor<T>() {
                      @Override
                      public void process(T object, ResultSet resultSet) throws SQLException {
                        try {
                          method.invoke(object, resultSet.getLong(name));
                        } catch (IllegalAccessException | InvocationTargetException error) {
                        }
                      }
                    });
              } else if (parameterType.equals(double.class)) {
                processors.add(
                    new ResultSetProcessor<T>() {
                      @Override
                      public void process(T object, ResultSet resultSet) throws SQLException {
                        try {
                          method.invoke(object, resultSet.getDouble(name));
                        } catch (IllegalAccessException | InvocationTargetException error) {
                        }
                      }
                    });
              } else if (parameterType.equals(String.class)) {
                processors.add(
                    new ResultSetProcessor<T>() {
                      @Override
                      public void process(T object, ResultSet resultSet) throws SQLException {
                        try {
                          method.invoke(object, resultSet.getString(name));
                        } catch (IllegalAccessException | InvocationTargetException error) {
                        }
                      }
                    });
              } else if (parameterType.equals(Date.class)) {
                processors.add(
                    new ResultSetProcessor<T>() {
                      @Override
                      public void process(T object, ResultSet resultSet) throws SQLException {
                        try {
                          method.invoke(object, new Date(resultSet.getTimestamp(name).getTime()));
                        } catch (IllegalAccessException | InvocationTargetException error) {
                        }
                      }
                    });
              } else if (parameterType.equals(Map.class)) {
                processors.add(
                    new ResultSetProcessor<T>() {
                      @Override
                      public void process(T object, ResultSet resultSet) throws SQLException {
                        try (JsonReader reader =
                            Json.createReader(new StringReader(resultSet.getString(name)))) {
                          method.invoke(object, MiscFormatter.fromJson(reader.readObject()));
                        } catch (IllegalAccessException
                            | InvocationTargetException
                            | JsonParsingException error) {
                        }
                      }
                    });
              }
            }
          }

          while (resultSet.next()) {
            T object = (T) prototype.create();
            for (ResultSetProcessor<T> processor : processors) {
              processor.process(object, resultSet);
            }
            result.add(object);
          }
        }

      } finally {
        statement.close();
        connection.close();
      }
    }

    return result;
  }