/* 
 * Copyright (c) 2009, 2014, Oracle and/or its affiliates. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; version 2 of the
 * License.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */

#ifndef _DB_MYSQL_SQL_EXPORT_H_
#define _DB_MYSQL_SQL_EXPORT_H_

#include "db_mysql_public_interface.h"
#include "grt/grt_manager.h"
#include "grts/structs.db.mysql.h"
#include "grt/grt_string_list_model.h"
#include "db_mysql_validation_page.h"

class WBPLUGINDBMYSQLBE_PUBLIC_FUNC DbMySQLSQLExport : public DbMySQLValidationPage
{
  //bec::GRTManager *_manager;
  db_mysql_CatalogRef _catalog;

  // options
  bool _gen_drops;
  bool _gen_schema_drops;
  bool _gen_warnings;
  bool _gen_create_index;
  bool _no_users_just_privileges;
  bool _no_view_placeholders;
  bool _gen_inserts;
  bool _no_FK_for_inserts;
  bool _triggers_after_inserts;
  std::string _output_filename;
  std::string _output_header;
  bool _tables_are_selected, _triggers_are_selected,
      _routines_are_selected, _views_are_selected, 
      _users_are_selected;
  bool _omit_schemata;
  bool _generate_use;
  bool _skip_foreign_keys;
  bool _skip_fk_indexes;
  bool _case_sensitive;
  bool _gen_doc_props;
  bool _gen_attached_scripts;

  boost::shared_ptr<bec::GrtStringListModel> _users_model;
  boost::shared_ptr<bec::GrtStringListModel> _users_exc_model;
  boost::shared_ptr<bec::GrtStringListModel> _tables_model;
  boost::shared_ptr<bec::GrtStringListModel> _tables_exc_model;
  boost::shared_ptr<bec::GrtStringListModel> _views_model;
  boost::shared_ptr<bec::GrtStringListModel> _views_exc_model;
  boost::shared_ptr<bec::GrtStringListModel> _routines_model;
  boost::shared_ptr<bec::GrtStringListModel> _routines_exc_model;
  boost::shared_ptr<bec::GrtStringListModel> _triggers_model;
  boost::shared_ptr<bec::GrtStringListModel> _triggers_exc_model;

  std::map<std::string,GrtNamedObjectRef> _users_map;
  std::map<std::string,GrtNamedObjectRef> _tables_map;
  std::map<std::string,GrtNamedObjectRef> _views_map;
  std::map<std::string,GrtNamedObjectRef> _routines_map;
  std::map<std::string,GrtNamedObjectRef> _triggers_map;
  grt::DictRef _db_options;

  std::string get_q_name(const char *part1, const char *part2)
  {
    return std::string(part1).append(".").append(part2);
  }

  db_mysql_CatalogRef filter_catalog();

protected:
  virtual db_mysql_CatalogRef get_model_catalog();
  virtual grt::DictRef get_options_as_dict(grt::GRT*);
  //bec::MessageListBE messages_list;
public:

  DbMySQLSQLExport(bec::GRTManager *grtm, db_mysql_CatalogRef catalog = db_mysql_CatalogRef());

  //bec::GRTManager *get_grt_manager() const { return _manager; }
  db_mysql_CatalogRef get_catalog() const { return _catalog; }

  std::string get_output_filename() const { return _output_filename; }
  
  void set_option(const std::string& name, bool value);
  void set_option(const std::string& name, const std::string& value);
  void set_db_options_for_version(const GrtVersionRef &version);
  void set_db_options(grt::DictRef &db_options);

  void start_export(bool wait_finish);
  //void run_validation();

  void export_finished(grt::ValueRef res);
  grt::ValueRef export_task(grt::GRT*, grt::StringRef);

  typedef boost::function<int ()> Task_finish_cb;
  void task_finish_cb(Task_finish_cb cb) { _task_finish_cb= cb; }

  void setup_grt_string_list_models_from_catalog(bec::GrtStringListModel **users_model, 
                                                 bec::GrtStringListModel **users_exc_model,
                                                 bec::GrtStringListModel **tables_model, 
                                                 bec::GrtStringListModel **tables_exc_model,
                                                 bec::GrtStringListModel **views_model, 
                                                 bec::GrtStringListModel **views_exc_model, 
                                                 bec::GrtStringListModel **routines_model, 
                                                 bec::GrtStringListModel **routines_exc_model, 
                                                 bec::GrtStringListModel **triggers_model,
                                                 bec::GrtStringListModel **triggers_exc_model);

  std::string export_sql_script() { return _export_sql_script; }

private:
  //Validation_finished_cb _validation_finished_cb;
  //Validation_step_finished_cb _validation_step_finished_cb;
  Task_finish_cb _task_finish_cb;
  std::string _export_sql_script;
};


grt::StringListRef convert_string_vector_to_grt_list(grt::GRT *grt, const std::vector<std::string>& v);

#endif // _DB_MYSQL_SQL_EXPORT_H_
