{
  "menu": {
    "type": "table",
    "caption": ["Menu","Menus"],
    "columns": [
      { "name": "menu_id", "type": "bigint", "key": true, "identity": true, "null": false },
      { "name": "menu_key", "type": "bigint", "foreignkey": { "menu": "menu_id" } },
      { "name": "site_id", "type": "bigint", "null": false },
      { "name": "menu_file_id", "type": "bigint", "foreignkey": { "menu": "menu_id" } },
      { "name": "menu_orig_id", "type": "bigint", "foreignkey": { "menu": "menu_id" } },
      { "name": "menu_name", "type": "varchar", "length": 256, "null": false },
      { "name": "menu_tag", "type": "varchar", "length": 256, "null": false },
      { "name": "menu_notes", "type": "varchar", "length": -1 },

      { "name": "menu_review_sts", "type": "varchar", "length": 32, "foreignkey": { "code_item_review_sts": "code_val" } },
      { "name": "menu_lang", "type": "varchar", "length": 32, "foreignkey": { "code_lang": "code_val" } },
      
      { "name": "menu_etstmp", "type": "datetime", "length": 7, "default": { "sql": "%%%%%%jsh.map.timestamp%%%%%%" } },
      { "name": "menu_euser", "type": "varchar", "length": 20, "default": { "sql": "%%%%%%jsh.map.current_user%%%%%%" } },
      { "name": "menu_mtstmp", "type": "datetime", "length": 7 },
      { "name": "menu_muser", "type": "varchar", "length": 20 }
    ],
    "triggers": [
      { "on": ["insert"], "exec": [
          "setif(null(menu.menu_key),menu_key,menu.menu_id);",
          "setif(null(menu.menu_file_id),menu_file_id,menu.menu_id);"
        ]
      },
      { "on": ["validate_insert"], "exec": [
          "errorif((inserted(site_id) is not null) and not exists(select site_id from {schema}.site where site_id=inserted(site_id)),'Site ID not found');",
        ]
      },
      { "on": ["validate_update"], "exec": [
          "errorif(update(menu_key) and (deleted(menu_key) is not null),'Cannot update menu_key');",
          "errorif(update(menu_file_id) and (deleted(menu_file_id) is not null),'Cannot update menu_file_id');",
          "errorif(update(menu_name) and (deleted(menu_name) is not null),'Cannot update menu_name');",
          "errorif(update(menu_tag) and (deleted(menu_tag) is not null),'Cannot update menu_tag');",
          "errorif((inserted(site_id) is not null) and not exists(select site_id from {schema}.site where site_id=inserted(site_id)),'Site ID not found');",
        ]
      },
      { "on": ["update", "insert"], "exec": [
          "set(menu_mtstmp,%%%%%%jsh.map.timestamp%%%%%%);",
          "set(menu_muser,%%%%%%jsh.map.current_user%%%%%%);"
        ]
      }
    ],
    "sample_data": [
      { "site_id": 1, "menu_name": "Site Menu", "menu_tag": "site_menu", "_FILES": { "menu_site.json": "menu/{{menu_id}}.json" } },
      { "site_id": 1, "menu_name": "Footer Menu", "menu_tag": "footer_menu", "_FILES": { "menu_footer.json": "menu/{{menu_id}}.json" } }
    ]
  },

  "v_my_menu": {
    "type": "view",
    "caption": ["Menu", "Menus"],
    "tables": {
      "menu": {
        "columns": [
          "menu_id",
          "menu_key",
          "site_id",
          "menu_file_id",
          "menu_orig_id",
          "menu_name",
          "menu_tag",
    
          "menu_review_sts",
          "menu_lang",
          
          "menu_etstmp",
          "menu_euser",
          "menu_mtstmp",
          "menu_muser"
        ]
      },
      "branch_menu": {
        "join_type": "inner",
        "join_columns": {
          "branch_menu.menu_id": "menu.menu_id",
          "branch_menu.branch_id": "{schema}.my_current_branch_id()"
        }
      }
    },
    "triggers": [
      { "on": ["insert"], "exec": [
          [
            "errorif({schema}.my_current_branch_is_editable()=0,'Cannot update a read-only revision');",
            "errorif({schema}.my_current_branch_is_merging()=1,'Cannot update revision when a merge is in progress');",
            "errorif(exists(select * from {schema}.v_my_menu where menu_tag=inserted(menu_tag)),'Menu tag already exists');",
            "with_insert_identity(menu, menu_id, ",
            "  insert into menu(site_id, menu_name, menu_tag, menu_review_sts, menu_lang) values({schema}.my_current_site_id(), inserted(menu_name), inserted(menu_tag), inserted(menu_review_sts), inserted(menu_lang)),",
            "  return_insert_key(menu, menu_key, (menu_key=@@INSERT_ID));",
            "  insert into branch_menu(branch_id, menu_key, menu_id, branch_menu_action) values ({schema}.my_current_branch_id(), @@INSERT_ID, @@INSERT_ID, 'ADD');",
            "  update branch_menu set menu_orig_id=menu_id,branch_menu_action=NULL where branch_id={schema}.my_current_branch_id() and menu_key = @@INSERT_ID and {schema}.my_current_branch_is_versioned()=0;",
            "  update branch set branch_data_mtstmp = %%%%%%jsh.map.timestamp%%%%%%, branch_data_muser = %%%%%%jsh.map.current_user%%%%%% where branch_id = {schema}.my_current_branch_id();",
            "  increment_changes()",
            ")"
          ]
        ]
      },
      { "on": ["update"], "exec": [
          [
            "errorif({schema}.my_current_branch_is_editable()=0,'Cannot update a read-only revision');",
            "errorif({schema}.my_current_branch_is_merging()=1,'Cannot update revision when a merge is in progress');",
            "errorif(exists(select * from {schema}.v_my_menu where menu_tag=inserted(menu_tag) and menu_key<>deleted(menu_key)),'Menu tag already exists');",
            "with_insert_identity(menu, menu_id, ",
            "  insert into menu(site_id, menu_key, menu_file_id, menu_orig_id, menu_name, menu_tag, menu_review_sts, menu_lang) values(deleted(site_id), deleted(menu_key), (case when inserted(menu_file_id) is null then null else deleted(menu_file_id) end), deleted(menu_id), inserted(menu_name), inserted(menu_tag), inserted(menu_review_sts), inserted(menu_lang)),",
            "  update branch_menu set menu_id=@@INSERT_ID,branch_menu_action=(case branch_menu_action when 'ADD' then 'ADD' else 'UPDATE' end) where branch_id={schema}.my_current_branch_id() and menu_key = deleted(menu_key);",
            "  update branch_menu set menu_orig_id=menu_id,branch_menu_action=NULL where branch_id={schema}.my_current_branch_id() and menu_key = deleted(menu_key) and {schema}.my_current_branch_is_versioned()=0;",
            "  update branch set branch_data_mtstmp = %%%%%%jsh.map.timestamp%%%%%%, branch_data_muser = %%%%%%jsh.map.current_user%%%%%% where branch_id = {schema}.my_current_branch_id();",
            "  increment_changes()",
            ")"
          ]
        ]
      },
      { "on": ["delete"], "exec": [
          "errorif({schema}.my_current_branch_is_editable()=0,'Cannot update a read-only revision');",
          "errorif({schema}.my_current_branch_is_merging()=1,'Cannot update revision when a merge is in progress');",
          "update branch_menu set menu_id=null,branch_menu_action='DELETE' where branch_id={schema}.my_current_branch_id() and menu_key = deleted(menu_key) and ((branch_menu_action is null) or (branch_menu_action in ('UPDATE','DELETE')))",
          "delete from branch_menu where branch_id={schema}.my_current_branch_id() and menu_key = deleted(menu_key) and (branch_menu_action='ADD' or {schema}.my_current_branch_is_versioned()=0)",
          "update branch set branch_data_mtstmp = %%%%%%jsh.map.timestamp%%%%%%, branch_data_muser = %%%%%%jsh.map.current_user%%%%%% where branch_id = {schema}.my_current_branch_id();",
          "increment_changes()"
        ]
      }
    ]
  }
}
