/**
   * Gera o evento e salva o valor do parâmetros. Se algum parâmetro obrigatório estiver com valor
   * nulo é gerada uma exceção.
   *
   * @param event
   * @param eventModel
   * @throws SQLException
   * @throws Exception
   */
  public void insert(Event event, EventModel eventModel) throws SQLException, Exception {
    if (this.database == null || !this.database.isOpen()) {
      try {
        this.database = (SQLiteDatabase) dataManager.getDatabaseHelper().openDatabaseConnection();
      } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
    // Criar evento
    // String sql = "INSERT INTO generated_events
    // (event_id,entity_id,component_id,time,timeout,event_type) "
    //        + " VALUES (?,?,?,?,?,?);";
    ContentValues values = new ContentValues();
    values.put("event_id", eventModel.getId());
    values.put("entity_id", event.getEntityId());
    values.put("component_id", event.getComponentManager().getComponentId());
    values.put("time", event.getTime().getTime());
    values.put("timeout", event.getTimeout());
    values.put("event_type", event.getEventType());

    event.setDatabaseId((int) (long) this.database.insertOrThrow("generated_events", null, values));

    // adicionar conteúdos dos parâmetros
    for (Parameter p : eventModel.getParameters()) {
      if (event.getParameters().get(p.getLabel()) == null && !p.isOptional()) {
        throw new Exception(
            "Parameter "
                + p.getLabel()
                + " from the event "
                + eventModel.getDescription()
                + " id "
                + eventModel.getId()
                + " was not found. Such parameter is not optional!");
      } else {
        if (event.getParameters().get(p.getLabel()) != null) {
          p.setContent(
              new Content(
                  Content.parseContent(p.getDataType(), event.getParameters().get(p.getLabel())),
                  event.getTime()));
          this.insertContent(p, event);
        }
      }
    }
  }
  public Event getEvent(int dataBaseActionId, BaseComponentManager bcm) 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();
      }
    }
    Event event = null;
    String sql =
        " SELECT generated_events.id, generated_events.event_id, generated_events.entity_id, generated_events.component_id, generated_events.time, "
            + " timeout, generated_events.event_type, description, synchronous "
            + " FROM generated_events, generated_actions, events "
            + " WHERE generated_actions.id = ? AND events.id = generated_events.event_id AND generated_actions.event_id = generated_events.id;";

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

    if (cursor.moveToNext()) {
      for (ComponentManager cm : bcm.getComponentManagers()) {
        if (cursor.getInt(cursor.getColumnIndex("component_id")) == cm.getComponentId()) {
          event = new SystemEvent(cm);
        }
      }
      event.setId(cursor.getInt(cursor.getColumnIndex("event_id")));
      event.setDatabaseId(cursor.getInt(cursor.getColumnIndex("id")));
      event.setEventType(cursor.getInt(cursor.getColumnIndex("event_type")));
      event.setEntityId(cursor.getInt(cursor.getColumnIndex("entity_id")));
      event.setTime(new Date(cursor.getLong(cursor.getColumnIndex("time"))));
      event.setTimeout(cursor.getInt(cursor.getColumnIndex("timeout")));
      event.setName(cursor.getString(cursor.getColumnIndex("description")));
      event.setSynchronous(cursor.getInt(cursor.getColumnIndex("synchronous")) > 0);
    }
    if (!cursor.isClosed()) {
      cursor.close();
    }
    return event;
  }
  public void insertContent(Parameter parameter, Event event) 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();
      }
    }
    // String sql = "INSERT INTO event_contents (reading_value,reading_time,event_parameter_id,
    // generated_event_id) "
    //        + " VALUES (?,?,?,?);";

    ContentValues values = new ContentValues();
    values.put(
        "reading_value",
        String.valueOf(
            Content.parseContent(parameter.getDataType(), parameter.getContent().getValue())));
    values.put("reading_time", parameter.getContent().getTime().getTime());
    values.put("event_parameter_id", parameter.getId());
    values.put("generated_event_id", event.getDatabaseId());
    // executar
    parameter
        .getContent()
        .setId((int) (long) this.database.insertOrThrow("event_contents", null, values));

    if (DeveloperSettings.SHOW_DAO_SQL) {
      Log.d(
          "SQL_DEBUG",
          "INSERT INTO event_contents (id,reading_value,reading_time,event_parameter_id) "
              + " VALUES ("
              + parameter.getContent().getId()
              + ",'"
              + Content.parseContent(parameter.getDataType(), parameter.getContent().getValue())
              + "',"
              + ",'"
              + parameter.getContent().getTime().getTime()
              + "',"
              + ","
              + parameter.getId()
              + ");");
    }
  }