Import FamilySearch ID From RootsMagic.fh_lua

--[[
@Title:       Import FamilySearchID From RootsMagic
@Type:        Standard
@Author:      Mark Draper
@Version:     1.0.1
@LastUpdated: 5 Aug 2024
@Licence:     This plugin is copyright (c) 2024 Mark Draper and is licensed under the MIT License which is
              hereby incorporated by reference (see https://pluginstore.family-historian.co.uk/fh-plugin-licence)
@Description: Imports FamilySearchID values from a RootsMagic database and writes to matching 
              Individuals (common UniqueID) in Family Historian.
]]

fhInitialise(7, 0, 8)
require 'luacom'
require('iuplua')
fh = require('fhUtils')
require('fhSQL')

fh.setIupDefaults()
iup.SetGlobal('UTF8MODE_FILE','YES')

FSO = luacom.CreateObject('Scripting.FileSystemObject')

function main()

	local tblRM = {}
	local Count = 0

	-- check for emulator

	if FSO:FolderExists('Z:\\bin') and FSO:FolderExists('Z:\\etc') then
		local msg = 'Family Historian does not support linking to external databases via plugins when ' ..
			'running on Mac or Linux systems.'
		local msgdlg = iup.messagedlg{value = msg, dialogtype = 'ERROR',
				title = 'Emulator Compatibility Warning'}
		msgdlg:popup()
		return
	end

	-- present main menu

	local Title = 'Import FamilySearch ID from RootsMagic'
	local Message = 'This plugin imports Family Search ID values from a RootsMagic file ' ..
			'into matching Family Historian\nIndividuals with a common UniqueID.  ' ..
			'Any existing values are retained and not overwritten.'
	local Button1 = 'Import RootsMagic file'
	local Button2 = 'Quit'

	if iup.Alarm(Title, Message, Button1, Button2) ~= 1 then return end

	local filedlg = iup.filedlg{dialogtype = 'OPEN', title = 'Open RootsMagic File',
			extfilter = 'RootsMagic Database (*.rmgc, *.rmtree)|*.rmgc;*.rmtree|All Files|*.*|'}
	filedlg:popup()
	if filedlg.Status == '-1' then return end

	local FileName = filedlg.Value

	-- open RM database and create table of FamilySearch ID values

	local database = fhSQL.connectSQLite(FileName)
	local SQL = 'SELECT UniqueID, fsID FROM PersonTable, FamilySearchTable WHERE PersonID = rmID'

	local ResultSet = database:select(SQL)
	for p in ResultSet:rows() do
		tblRM[(p.UniqueID):sub(1,32)] = p.fsID			-- omit checksum
	end

	-- loop through FH Individuals to create FamilySearchID values

	local pI = fhNewItemPtr()
	pI:MoveToFirstRecord('INDI')
	while pI:IsNotNull() do
		local UID = (fhGetItemText(pI, '~._UID')):gsub('-', ''):sub(1,32)		-- ensure in RM format
		local FSID = fhGetItemText(pI, '~._FSID')

		if UID ~= '' and tblRM[UID] and FSID == '' then			-- no existing value and RM value exists
			local p = fhCreateItem('_FSID', pI)
			fhSetValueAsText(p, tblRM[UID])
			Count = Count + 1
		end
		pI:MoveNext()
	end

	local msg = Count .. ' Individual Records updated.'
	if Count == 1 then msg = msg:gsub('Records', 'Record') end

	local msgdlg = iup.messagedlg{value = msg, dialogtype = 'INFORMATION',
			title = 'Import FamilySearch ID From RootsMagic'}
	msgdlg:popup()
end

-- *********************************************************************

main()

Source:Import-FamilySearch-ID-From-RootsMagic-1.fh_lua