← servoy magazine

[Video] Coding Weekly #6: Text Processing

  • Video

In this coding session, I tackle a coding task that is common to every language and development platform ever created: text processing. Yea baby, sure to bring back fond memories of that odious homework assignment in school or the weekend you slept on the floor at the office getting your data cleaned up by monday morning.

Along the way I touch on the following topics:

1 Getting data with a SQL query
2 Datasets and foundsets
3 Task flow
4 Regular expressions
5 Error checking

Whatever your background is, I'm sure you'll be able to relate to how I go about this task in Servoy.

Due to YouTube 15 minute upload limit, in 4 parts:

Part 1: http://www.youtube.com/datamosaic#p/u/3/4JVED6yX7co
Part 2: http://www.youtube.com/datamosaic#p/u/2/WxrAcqqsz1k
Part 3: http://www.youtube.com/datamosaic#p/u/1/s_cmyDFERTU
Part 4: http://www.youtube.com/datamosaic#p/u/0/HafRngYvs8w

 

function TEMP_replace_src_string() {
	
	// siteID input
	var siteID = forms.WEB_0D_site.id_site
	
	if (!siteID) return "no site selected"
	
	// grab the content data for specified site
	var dataset = databaseManager.getDataSetByQuery(
			"sutra_cms",
			"SELECT web_block_data.id_block_data FROM web_block_data, web_block, web_area, web_page, web_site \
			where web_block_data.id_block = web_block.id_block and \
			web_block.id_area = web_area.id_area and \
			web_area.id_page = web_page.id_page and \
			web_page.id_site = ?",
			[siteID],-1)
			
	if (!dataset.getMaxRowIndex()) return "no data found"
	
	// convert to foundset
	var fsBlockData = databaseManager.getFoundSet('sutra_cms', 'web_block_data')
	fsBlockData.loadRecords(dataset)
	
	// iterate over dataset
	for (var i = 1; i <= fsBlockData.getSize(); i++) {
		var record = fsBlockData.getRecord(i)
		if (record.data_value && record.data_value.search(/src="site\//) >= 0) {
			// replace regexp		
			record.data_value = record.data_value.replace(/src="site\//gi, 'src="sites/' + forms.WEB_0D_site.directory + '/')
			databaseManager.saveData(record)
		}
	}
	
	// cleanup
	plugins.dialogs.showInfoDialog( "Done", "Done")
}