/**
   * 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 void insertParameters(EventModel 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_parameters
    // (description,optional,parameter_label,superior_limit,inferior_limit,initial_value,entity_state_id,data_type_id,event_id) "
    //        + " VALUES (?,?,?,?,?,?,?,?,?);";

    if (event.getParameters() != null) {
      for (Parameter parameter : event.getParameters()) {
        ContentValues values = new ContentValues();
        // 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()));
        // valores
        values.put("description", parameter.getDescription());
        values.put("optional", parameter.isOptional());
        values.put("parameter_label", parameter.getLabel());
        values.put("superior_limit", String.valueOf(parameter.getSuperiorLimit()));
        values.put("inferior_limit", String.valueOf(parameter.getInferiorLimit()));
        values.put("initial_value", String.valueOf(parameter.getInitialValue()));
        values.put(
            "entity_state_id",
            (parameter.getRelatedState() == null) ? -1 : parameter.getRelatedState().getId());
        values.put("data_type_id", parameter.getDataType().getId());
        values.put("event_id", event.getId());
        // executar
        parameter.setId((int) (long) this.database.insertOrThrow("event_parameters", null, values));

        if (DeveloperSettings.SHOW_DAO_SQL) {
          Log.d(
              "SQL_DEBUG",
              "INSERT INTO event_parameters (id,description,optional,parameter_label,superior_limit,inferior_limit,initial_value,entity_state_id,data_type_id,event_id) "
                  + " VALUES ("
                  + parameter.getId()
                  + ",'"
                  + parameter.getDescription()
                  + "','"
                  + parameter.getLabel()
                  + "','"
                  + parameter.getSuperiorLimit()
                  + "','"
                  + parameter.getInferiorLimit()
                  + "','"
                  + parameter.getInitialValue()
                  + "',"
                  + ((parameter.getRelatedState() == null)
                      ? -1
                      : parameter.getRelatedState().getId())
                  + ","
                  + parameter.getDataType().getId()
                  + ","
                  + event.getId()
                  + ");");
        }
      }
    }
  }