public Content getLastEventContentByLabelAndValue(
      Object value, String label, int eventModelId, int entityModelId, int componentModelId)
      throws SQLException, Exception {
    Content content = null;
    String sql =
        " SELECT event_contents.id as id, reading_value, time "
            + " FROM  generated_events, event_contents , event_parameters, events "
            + " WHERE reading_value = ? AND parameter_label = ? AND events.model_id = ? AND generated_events.entity_id = ? AND component_id = ? "
            + " AND generated_event_id = generated_events.id AND event_parameter_id = event_parameters.id AND events.id = generated_events.event_id "
            + " ORDER BY time DESC LIMIT 1;";

    Cursor cursor =
        this.database.rawQuery(
            sql,
            new String[] {
              String.valueOf(value),
              String.valueOf(label),
              String.valueOf(eventModelId),
              String.valueOf(entityModelId),
              String.valueOf(componentModelId)
            });

    if (cursor.moveToNext()) {
      content = new Content();
      content.setTime(new Date(cursor.getLong(cursor.getColumnIndex("time"))));
      content.setId(cursor.getInt(cursor.getColumnIndex("id")));
      content.setValue(value);
    }
    if (!cursor.isClosed()) {
      cursor.close();
    }
    return content;
  }
  public Content getCurrentContentValue(Parameter parameter) throws SQLException {
    if (this.database == null || !this.database.isOpen()) {
      try {
        this.database = (SQLiteDatabase) dataManager.getDatabaseHelper().openDatabaseConnection();
      } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
    Content content = null;
    String sql =
        "SELECT id, reading_value, reading_time "
            + "FROM event_contents\n"
            + "WHERE event_parameter_id = ? ORDER BY id DESC LIMIT 1;";

    // consulta
    Cursor cursor = this.database.rawQuery(sql, new String[] {String.valueOf(parameter.getId())});

    if (cursor.moveToFirst()) {
      content = new Content();
      // pegar o valor atual
      content.setId(cursor.getInt(cursor.getColumnIndex("id")));
      content.setTime(
          new Date(Long.parseLong(cursor.getString(cursor.getColumnIndex("reading_time")))));
      content.setValue(
          Content.parseContent(
              parameter.getDataType(), cursor.getString(cursor.getColumnIndex("reading_value"))));
    }
    if (!cursor.isClosed()) {
      cursor.close();
    }
    return content;
  }
  public List<Parameter> getEventParameterContents(int dataBaseEventId) throws SQLException {
    List<Parameter> parameters = new ArrayList();
    Parameter parameter;
    Content content;
    String sql =
        "SELECT event_contents.id as content_id, reading_value, reading_time, event_parameters.id as parameter_id, "
            + " parameter_label as label, event_parameters.description as parameter_desc, optional, superior_limit, "
            + " inferior_limit, entity_state_id, event_parameters.initial_value, data_type_id, "
            + " data_types.initial_value as data_initial_value, data_types.description as data_desc "
            + " FROM event_contents, event_parameters, data_types "
            + " WHERE generated_event_id = ?  AND event_parameter_id = event_parameters.id AND data_types.id = data_type_id "
            + " ORDER BY generated_event_id ; ";

    Cursor cursor = this.database.rawQuery(sql, new String[] {String.valueOf(dataBaseEventId)});

    while (cursor.moveToNext()) {
      parameter = new Parameter();
      parameter.setId(cursor.getInt(cursor.getColumnIndex("parameter_id")));
      parameter.setDescription(cursor.getString(cursor.getColumnIndex("parameter_desc")));
      parameter.setLabel(cursor.getString(cursor.getColumnIndex("label")));
      DataType type = new DataType();
      type.setId(cursor.getInt(cursor.getColumnIndex("data_type_id")));
      type.setDescription(cursor.getString(cursor.getColumnIndex("data_desc")));
      type.setInitialValue(cursor.getString(cursor.getColumnIndex("data_initial_value")));
      parameter.setDataType(type);
      // trata o tipo de dado do estado
      parameter.setSuperiorLimit(
          Content.parseContent(parameter.getDataType(), parameter.getSuperiorLimit()));
      parameter.setInferiorLimit(
          Content.parseContent(parameter.getDataType(), parameter.getInferiorLimit()));
      parameter.setInitialValue(
          Content.parseContent(parameter.getDataType(), parameter.getInitialValue()));
      parameter.setInferiorLimit(
          Content.parseContent(type, cursor.getString(cursor.getColumnIndex("inferior_limit"))));
      parameter.setSuperiorLimit(
          Content.parseContent(type, cursor.getString(cursor.getColumnIndex("superior_limit"))));
      parameter.setInitialValue(
          Content.parseContent(type, cursor.getString(cursor.getColumnIndex("initial_value"))));
      parameter.setOptional(cursor.getInt(cursor.getColumnIndex("optional")) > 0);
      if (cursor.getInt(cursor.getColumnIndex("entity_state_id")) > 0) {
        if (this.dataManager == null) {
          EntityStateDAO dao = new EntityStateDAO(this.database);
          parameter.setRelatedState(
              dao.getState(cursor.getInt(cursor.getColumnIndex("entity_state_id"))));
        } else {
          parameter.setRelatedState(
              this.dataManager
                  .getEntityStateDAO()
                  .getState(cursor.getInt(cursor.getColumnIndex("entity_state_id"))));
        }
      }
      // pega o valor utilizado na ação
      content = new Content();
      content.setId(cursor.getInt(cursor.getColumnIndex("content_id")));
      content.setTime(
          new Date(Long.parseLong(cursor.getString(cursor.getColumnIndex("reading_time")))));
      content.setValue(
          Content.parseContent(
              parameter.getDataType(), cursor.getString(cursor.getColumnIndex("reading_value"))));
      parameter.setContent(content);

      parameter.setPossibleContents(this.getPossibleContents(parameter));
      parameters.add(parameter);
    }
    if (!cursor.isClosed()) {
      cursor.close();
    }
    return parameters;
  }