vt_view.anubis 14.7 KB
/*
 * Created by PyramIDE.
 * User: フランスのトトロ aka (David RENÉ) 
 * Date: 21/03/2017
 * Time: 22:00
 * © David RENÉ
 */

read hayamiki_lib/types/hayamiki.anubis
read hayamiki_lib/view/hk_filter.anubis
read tools/basis.anubis
read tools/streams.anubis
read system/string.anubis
read fields.anubis
read columns.anubis
read html.anubis
read json.anubis

public define String
  make_list_view_json_row
  (
    HK_Table      table,      //table to format
    String        data_name,  //name of data
    String        indent,     //indentation string to use
    List(HK_Model_Column) columns   //list view to generate
  )=
  since table   is hk_table(table_name, short, model, _),  //get name and model of the table
//  since model   is hk_model( columns, _),     //get all columns of table
//  since display is db_display(views, _),  //
      //and the other set with list_view
//    "\"{ "+ join("+\", ",  map((HK_Model_Column column) |-> column.name+": \"+to_JSON_String(" + to_String(column, data_name, table_name)+")", columns))+"+\" }\""      
    "\"{ id : \"+to_JSON_String(to_String("+data_name+".id))+\", "+
      
      join("+\", ",  map((HK_Model_Column column) |-> column.name+": \"+" + to_JSON_String(column, data_name, table_name), columns)) +"+\" }\""
    
  .
  
define String
  to_VT_view_model
  (
    HK_Model_Column col,
    String          type_name    
  )=
  since col is hk_column(name, col_type, _, help),
  if col_type is 
  {
    p_key                               then "primary_key(\""+to_upper(name)+"\", \""+name+"\")",
    boolean_field                       then "boolean(\""+to_upper(name)+"\", \""+name+"\", "+to_Anubis_source(help)+")",
    date_field(attrs)                   then "date(\""+to_upper(name)+"\", \""+name+"\", "+to_Anubis_source(help)+")",
    time_field(attrs)                   then "time(\""+to_upper(name)+"\", \""+name+"\", "+to_Anubis_source(help)+")",
    datetime_field(attrs)               then "datetime(\""+to_upper(name)+"\", \""+name+"\", "+to_Anubis_source(help)+")",
    foreign_key(app,foreign_table,_,active_filters,_)  then "foreign_text(\""+to_upper(name)+"\", \""+name+"\", hk_form_selector(get_selector_"+foreign_table+"), "+to_Anubis_source(help)+")",
    integer_field(choices)              then 
                                          if choices is 
                                          {
                                            no_choice                     then "integer(\""+to_upper(name)+"\", \""+name+"\", "+to_Anubis_source(help)+")",
                                            int_choices(choices_list)     then
                                              //if choice list is NOT empty we construct the possible choices selector
                                              if length(choices_list) > 0 then
                                                "choices_int(\""+to_upper(name)+"\", \""+name+"\", hk_form_selector(get_choices_selector_"+to_lower(type_name)+"_"+name+"), "+to_Anubis_source(help)+")"
                                              else
                                                "integer(\""+to_upper(name)+"\", \""+name+"\", "+to_Anubis_source(help)+")",
                                          }
    float_field                         then  "float(\""+to_upper(name)+"\", \""+name+"\", "+to_Anubis_source(help)+")",
                                          
    //TODO make choices selection if need
    char_field(choices,_)               then 
                                          if choices is 
                                          {
                                            no_choice                   then "text(\""+to_upper(name)+"\", \""+name+"\", "+to_Anubis_source(help)+")",
                                            text_choices(choices_list)  then
                                              //if choice list is NOT empty we construct the possible choices selector
                                              if length(choices_list) > 0 then
                                                "choices_text(\""+to_upper(name)+"\", \""+name+"\", hk_form_selector(get_choices_selector_"+to_lower(type_name)+"_"+name+"), "+to_Anubis_source(help)+")"
                                              else
                                                "text(\""+to_upper(name)+"\", \""+name+"\", "+to_Anubis_source(help)+")"
                                          }
    password_field(min_size)            then "password(\""+to_upper(name)+"\", \""+name+"\", "+to_Anubis_source(help)+")",
    text_field(txt_attr)                then "text_area(\""+to_upper(name)+"\", \""+name+"\", "+to_Anubis_source(help)+")",
    color_field                         then "color(\""+to_upper(name)+"\", \""+name+"\", "+to_Anubis_source(help)+")",
    phone_field(phone_attr)             then "phone(\""+to_upper(name)+"\", \""+name+"\", "+to_Anubis_source(phone_attr)+", "+to_Anubis_source(help)+")",
//    mobile_field                        then "mobile_phone(\""+to_upper(name)+"\", \""+name+"\", "+to_Anubis_source(help)+")",
    url_field                           then "url(\""+to_upper(name)+"\", \""+name+"\", "+to_Anubis_source(help)+")",
    email_field                         then "email(\""+to_upper(name)+"\", \""+name+"\", "+to_Anubis_source(help)+")"
    
  }.

  
public define String
  make_list_view_model
  (
    List(HK_Model_Column) columns,
    String                type_name,    
    String                indent
  )=
  indent+join(",\n"+indent, map(((HK_Model_Column col) |-> to_VT_view_model(col, type_name)), columns)) 
.

public define String
  make_list_view_header
  (
    List(String)  list_view,
    String        indent
  )=
  indent+join(",\n"+indent, map((String text) |-> "text(\""+to_upper(text)+"\")", list_view)).
  
public define Maybe(One)
/* Generate 

*/
  generate_VT_view
  (
    Stream        stream, //stream file where to write
    HK_Table      table,  //
    HK_Viewer     list_view   //list view to generate
  )=
  since table   is hk_table(table_name, short_name, model, display),
  since model   is hk_model( columns, constraints, _),
  //since display is db_display(list_view, list_edit),
  
  with type_name =  to_upper(table_name, 1), //make the first character to upper case and be an Anubis Type.
  with view_name =  if list_view is
                    {
                      list_view_all               then  "default",
                      list_view(view_name, _, _, _)  then  view_name
                    },
  with current_list_view = if list_view is
                            {
                              list_view_all         then to_List_String(columns, false),
                              list_view(_, cols, _, _) then cols
                            },
  with current_view_models = if list_view is
                            {
                              list_view_all                then columns,
                              list_view(v_name, cols, _, _)   then 
                                if get_HK_Model_Column_list_from_name_list(columns, cols) is
                                {
                                  failure               then println("generate_VT_view: can't get all specified columns for table \""+table_name+"\" view name \""+v_name+"\" cols["+join(", ",cols)+"]");[],
                                  success(got_columns)  then got_columns,
                                }
                            },                            
  //generate the type of the table that contain all components 
  if write_string(stream, 
   "\n\n"+
   //SQL query
   "define VT_view\n"+
   "  _get_viewable_"+table_name+"_"+view_name+"\n"+
   "  =\n"+
   "  with header = vt_view_row_header([\n"+
   // view row header
   make_list_view_header(current_list_view, "                  ")+"\n"+
   "                ]),\n"+
   "  with model  = vt_view_model([\n"+
   //view model
   make_list_view_model(current_view_models, type_name, "                  ")+"\n"+
   "                ]),\n"+   
   "  vt_view(\n"+
   "   \""+table_name+"\", //Name of the database table\n"+
   "   \""+view_name+"\",  //Name of the view\n"+
   "   model,\n"+
   "   header\n"+
//"    vt_view_db_calls(\n"+
//"      get_count_"+table_name+"_by_clause,\n"+
//"      get_all_"+table_name+"_by_clause_to_json\n"+
////"      get_"+xxx+"_rows\n"+         
//"    )\n"+
"  )\n"+
".\n\n"

//   "public define VT_view\n"+
//   "  get_viewable_"+table_name+"_"+view_name+"\n"+
//   "  (\n"+
//   "    SQLite3DataBase  db,\n"+
//   "    String           clause\n"+
//   "  )=\n"+
//   "  _get_viewable_"+table_name+"_"+view_name+"(db, get_all_"+table_name+"_by_clause(db, clause), [], [] ).\n\n"
   ) is                                                           //end of the function
  {
    failure     then  println("can't write generate_VT_view "+type_name); failure,
    success(_)  then  success(unique)
  }.

public define Maybe(One)
/* Generate 

*/
  _generate_VT_view
  (
    Stream              stream,     //stream file where to write
    HK_Table            table,      //
    List(HK_Viewer)  list_view,  //list view to generate
    Bool                has_default //set to true if a default view was encountered.
  )=
  if list_view is
  { 
    []          then
      //if default view was not generated, we force to generate it
      if has_default = false then
        generate_VT_view(stream, table, list_view_all)
      else  
        success(unique),
    [view . t]  then
      if generate_VT_view(stream, table, view) is 
      {
        failure     then failure,
        success(_)  then 
          with new_has_default = if has_default = true then //already true, no need to go further
                                  true
                                 else
                                  if view is
                                  {
                                    list_view_all               then  true, //list_view_all is default view
                                    list_view(view_name, _, _, _)  then    
                                      //if not we search for view named 'default'
                                      if view_name = "default" then true else false
                                  },
          _generate_VT_view(stream, table, t, new_has_default)
      }
  }.

public define String
/* Generate 

*/
  _generate_get_viewable_if_else
  (
    String              table_name,      //
    List(HK_Viewer)  list_view,  //list view to generate
    String              so_far
  )=
  if list_view is
  { 
    []          then
      so_far +
      "\n"+
      "    _get_viewable_"+table_name+"_default\n",
    [view . t]  then
      with view_name =  if view is
                        {
                          list_view_all              then  "default",
                          list_view(view_name, _, _, _) then  view_name
                        },
      _generate_get_viewable_if_else( table_name, t,
        so_far +
        "  if view = \""+view_name+"\" then\n"+
        "    _get_viewable_"+table_name+"_"+view_name+"\n"+
        "  else"
        )
  }.
  
public define Maybe(One)
/* Generate 

*/
  generate_get_viewable
  (
    Stream              stream,     //stream file where to write
    HK_Table            table,      //
    List(HK_Viewer)  list_view   //list view to generate
  )=
  since table   is hk_table(table_name, short_name, model, display),
  if write_string(stream, 
    "public define VT_view\n"+
    "  get_viewable_"+table_name+"\n"+
    "  (\n"+
    "    String           view,\n"+
    "  )=\n"+
    _generate_get_viewable_if_else(table_name, list_view, "")+
    ".\n"
   ) is                                                           //end of the function
  {
    failure     then  println("can't write generate_get_viewable "+table_name); failure,
    success(_)  then  success(unique)
  }.

define String
  make_filter
  (
    List(HK_Filter) filters
  )=
  join(", \n", map((HK_Filter filter) |-> to_Anubis_source(filter), filters))
.

public define List(String)
/* Generate 

*/
  _generate_list_view_name
  (
    List(HK_Viewer) list_view,  //list view to generate
    List(String)    so_far,
    Bool            has_default //set to true if a default view was encountered.
  )=
  if list_view is
  { 
    []          then
      //if default view was not generated, we force to generate it
      if has_default = false then 
        reverse(["(\"default\", [])" . so_far])
      else  
        reverse(so_far),
    [view . t]  then
       with new_has_default = if has_default = true then //already true, no need to go further
                                  true
                                 else
                                  if view is
                                  {
                                    list_view_all               then  true, //list_view_all is default view
                                    list_view(view_name, _, _, _)  then    
                                      //if not we search for view named 'default'
                                      if view_name = "default" then true else false
                                  },
        with entry =  if view is
                  {
                    list_view_all                       then  "(\"default\", [])",
                    list_view(view_name, _, filter, _)  then  "(\""+view_name+"\", ["+make_filter(filter)+"])"
                  },
        _generate_list_view_name(t, [entry . so_far], new_has_default)
  }
.
  
public define Maybe(One)
  _generate_get_all_view_name
  (
    Stream           stream,
    HK_Table         table,      //    
    List(HK_Viewer)  list_view
  )=
  since table   is hk_table(table_name, short_name, model, display),
  if write_string(stream, 
    "\n\npublic define List((String, List(HK_Filter)))\n"+
    "  get_all_"+table_name+"_view_name\n"+
    "  (\n"+
    "    One dummy\n"+
    "  )=\n"+
    "  [\n"+
    "    "+join(",\n    ",_generate_list_view_name(list_view, [], false))+
    "\n  ]\n"+
    ".\n\n"
   ) is                                                           //end of the function
  {
    failure     then  println("can't write _generate_get_all_view_name "+table_name); failure,
    success(_)  then  success(unique)
  }
.



public define Maybe(One)
  generate_VT_all_view
  (
    Stream    stream,
    HK_Table  table
  )=
  since table   is hk_table(name, short_name, model, display),
  since model   is hk_model( columns, constraints, _),
  since display is hk_display(list_view, list_edit),
  
  with type_name = to_upper(name, 1),            //make the first character to upper case to able to be a Anubis Type.
  if _generate_get_all_view_name(stream, table, list_view) is
  {
    failure     then failure,
    success(_)  then
  
    if _generate_VT_view(stream, table, list_view, false) is   //list view to generate
    {
      failure     then  failure,
      success(_)  then  generate_get_viewable(stream, table, list_view)
    }
  }
.