/*
    merge.dlg

*!  VERSION 3.0.4  23aug2010

*/

VERSION 11.0

INCLUDE _std_large
INCLUDE _ht340
INCLUDE header

HELP hlp1, view("help merge")
RESET res1

DIALOG main, tabtitle("Main")	///
	label("merge - Merge dataset in memory with dataset on disk")
BEGIN
  GROUPBOX gb_type	_lft	_top	_iwd	_ht10,			///
	label("Type of merge")
  RADIO rb_1_to_1	_ilft	_ss	_inwd	.,			///
	first								///
	onclickon(script main_showvlist_on)				///
	label("One-to-one on key variables")
  RADIO rb_m_to_1	@	_ss	@	.,			///
	onclickon(script main_showvlist_on)				///
	label("Many-to-one on key variables (unique key for data on disk)")
  RADIO rb_1_to_m	@	_ss	@	.,			///
	onclickon(script main_showvlist_on)				///
	label("One-to-many on key variables (unique key for data in memory)")
  RADIO rb_m_to_m	@	_ss	@	.,			///
	onclickon(script main_showvlist_on)				///
	label("Many-to-many on key variables")
  RADIO rb_1_to_1_ob	@	_ss	@	.,			///
	last								///
	onclickon(script main_showvlist_off)				///
	label("One-to-one by observation")
  
  TEXT tx_var		_lft	_xls	_iwd	.,			///
	label("Key variables: (match variables)")
  VARLIST vl_varlist	@	_ss	@	.,			///
	label("Key variables")

  TEXT tx_filename	@	_ls	@	.,			///
	label("Filename of dataset on disk:")
  FILE fi_filename 	@       _ss	@	.,			///
	error("Filename of dataset on disk")				///
	filter("Stata Dataset (*.dta)|*.dta|All Files (*.*)|*.*")	///
	defext(dta) label("Browse...")
END

SCRIPT main_showvlist_on
BEGIN
	main.tx_var.enable
	main.vl_varlist.enable
END

SCRIPT main_showvlist_off
BEGIN
	main.tx_var.disable
	main.vl_varlist.disable
END

SCRIPT opt_PREINIT
BEGIN
	create ARRAY opt_using_variables
	create DOUBLE opt_des_error
END

DIALOG opt, tabtitle("Options")
BEGIN
  TEXT tx_keepusing 	_lft	_top	_iwd	.,			///
	label("Variables to keep from dataset on disk: (by default all are kept)")
  COMBOBOX cb_keepusing _lft	_ss	_lw80	.,			///
	append dropdown contents(opt_using_variables) 			///
	option(keepusing) label("Variables to keep from dataset on disk")
  BUTTON bu_matinput	 _rj80  @	80	.,			///
	label("Populate") onpush(program populate_cb_keepusing)		///
	tooltip("Populate combobox with varlist of dataset on disk")
  TEXT tx_generate	_lft	_ls	_iwd	.,			///
	label("Name of new variable to mark merge results:")
  EDIT ed_generate	_lft	_ss	_vnwd	.,			///
	max(32)	option(generate) default("_merge")			///
	label("Name of new variable to mark merge results")
  CHECKBOX ck_nogen	_lft	_ls	_iwd	.,			///
	onclickon(opt.ed_generate.disable)				///
	onclickoff(opt.ed_generate.enable)				///
	option(nogenerate) label("Do not generate _merge variable")
  CHECKBOX ck_nolabel	_lft	_ms	@	.,			///
	option(nolabel)	label("Do not copy value label definitions from dataset on disk")
  CHECKBOX ck_nonotes	@	_ms	@	.,			///
	label("Do not copy notes from dataset on disk") option(nonotes)
  CHECKBOX ck_update	@	_ms	@	.,			///
	option(update) 							///
	onclickon(script opt_update_on)					///
	onclickoff(script opt_update_off)				///
	label("Update missing values with values from dataset on disk")
  CHECKBOX ck_replace	@	_ms	@	.,			///
	option(replace) nomem 						///
	label("Replace nonmissing values with values from dataset on disk")
  CHECKBOX ck_noreport	@	_ms	@	.,			///
	option(noreport) label("Do not display result summary table")
  CHECKBOX ck_force	@	_ms	@	.,			///
	option(force) label("Allow string/numeric variable type mismatch without error")
END

SCRIPT opt_update_on
BEGIN
	opt.ck_replace.enable
	res.ck_amatch_u.enable
	res.ck_amatch_c.enable
	res.ck_kmatch_u.enable
	res.ck_kmatch_c.enable
END

SCRIPT opt_update_off
BEGIN
	opt.ck_replace.disable
	res.ck_amatch_u.disable
	res.ck_amatch_c.disable
	res.ck_kmatch_u.disable
	res.ck_kmatch_c.disable
END

PROGRAM populate_cb_keepusing
BEGIN
	require main.fi_filename
	call opt_des_error.setvalue 0
	put "merge_wrk_dlg using "
	put `"""'
	put main.fi_filename
	put `"""'
	stata hidden immediate

	if opt_des_error {
		stopbox stop "Unable to get variables from dataset on disk"
	}
	call opt.cb_keepusing.repopulate
END

DIALOG res, tabtitle("Results")
BEGIN
  GROUPBOX gb_assert	_lft	_top	_iwd	_ht12,			///
  	label("Require merge results")
  CHECKBOX ck_amaster	_ilft	_ss	_ibwd	.,			///
	option(master) label("Record appeared in master only")
  CHECKBOX ck_ausing	@	_ms	@	.,			///
	option(using) label("Record appeared in using only")
  CHECKBOX ck_amatch	@	_ms	@	.,			///
	option(match) label("Record appeared in both")
  CHECKBOX ck_amatch_u @	_ms	@	.,			///
	option(match_update) 						///
	label("Record appeared in both, missing values updated")
  CHECKBOX ck_amatch_c	@	_ms	@	.,			///
	option(match_conflict) 						///
	label("Record appeared in both, conflicting-nonmissing values updated")
 
  GROUPBOX gb_keep	_lft	_xls	_iwd	_ht12,			///
  	label("Results to keep")
  CHECKBOX ck_kmaster	_ilft	_ss	_ibwd	.,			///
	option(master) label("Record appeared in master only")
  CHECKBOX ck_kusing	@	_ms	@	.,			///
	option(using) label("Record appeared in using only")
  CHECKBOX ck_kmatch	@	_ms	@	.,			///
	option(match) label("Record appeared in both")
  CHECKBOX ck_kmatch_u @	_ms	@	.,			///
	option(match_update) 						///
	label("Record appeared in both, missing values updated")
  CHECKBOX ck_kmatch_c	@	_ms	@	.,			///
	option(match_conflict) 						///
	label("Record appeared in both, conflicting-nonmissing values updated")
END

PROGRAM build_res_assert_output
BEGIN
	option res.ck_amaster
	option res.ck_ausing
	option res.ck_amatch
	option res.ck_amatch_u
	option res.ck_amatch_c
END

PROGRAM build_res_keep_output
BEGIN
	option res.ck_kmaster
	option res.ck_kusing
	option res.ck_kmatch
	option res.ck_kmatch_u
	option res.ck_kmatch_c
END

PROGRAM command
BEGIN
	require main.fi_filename
	if main.rb_1_to_1 {
		require main.vl_varlist
		put "merge 1:1 " main.vl_varlist
	}
	if main.rb_m_to_1 {
		require main.vl_varlist
		put "merge m:1 " main.vl_varlist
	}
	if main.rb_1_to_m {
		require main.vl_varlist
		put "merge 1:m " main.vl_varlist
	}
	if main.rb_m_to_m {
		require main.vl_varlist
		put "merge m:m " main.vl_varlist
	}
	if main.rb_1_to_1_ob {
		put "merge 1:1 _n " 
	}

	put " " "using "
	put `"""'
	put main.fi_filename
	put `"""'
	beginoptions
		if (res.ck_amaster | res.ck_ausing | ///
			res.ck_amatch | res.ck_amatch_u | res.ck_amatch_c) {
			put " assert("
			put /program build_res_assert_output
			put ")"	
		}
		if (res.ck_kmaster | res.ck_kusing | ///
			res.ck_kmatch | res.ck_kmatch_u | res.ck_kmatch_c) {
			put " keep("
			put /program build_res_keep_output
			put ")"	
		}
		optionarg opt.cb_keepusing
		optionarg /hidedefault opt.ed_generate
		option opt.ck_nogen
		option opt.ck_nolabel
		option opt.ck_nonotes
		option opt.ck_update
		option opt.ck_replace
		option opt.ck_noreport
		option opt.ck_force
	endoptions
END
