module
	kind jsfile
	$
		var ROOTELEMENT = schema.rootElement.wzId;
	#
		# ${schema.wzId} WizziModelFactory
	var path = require('path')
	var util = require('util')
	var _ = require('lodash')
	+
	var stringify = require('json-stringify-safe')
	$if schema.mTreeIsPreprocessed 
		var ${schema.wzId}mTreePreProcessor = require('./${schema.wzId}-mtree-preprocessor.g')
	+
    var ${schema.wzId}schema = require('./${schema.wzId}-model.g')
	+
    var md = module.exports = {};
	+    
	#
	# called from the wizzi.wizziFactory.getLoadModel method
	# params
		# { wizziObject
			# func loadMTree
				# api-ref wizzi-mtree.loader.loadMTree
			# { file
				# api-ref wizzi-utils.file
			# { verify
				# api-ref wizzi-utils.verify
			# { errors
				# type WizziModelLoadError
			# { wizziFactory
				# api-ref wizzi.wizziFactory
	set md.createLoadModel
		function
			param wizziObject
			var options = wizziObject.options || {}
			var loadMTree = wizziObject.loadMTree
			var file = wizziObject.file
			var verify = wizziObject.verify
			var errors = wizziObject.errors
			var wizziFactory = wizziObject.wizziFactory

			$include loadModelFromMTree

			if options.loadFromMTree
				#
					# Load a WizziModel of type '${schema.wzId}' from an mTree
				return
					function
						param mTree
						param wizziModelRequest
						param callback
						if verify.isFunction(callback) !== true
							set callback = wizziModelRequest
							set wizziModelRequest = {}
						if verify.isFunction(callback) !== true
							throw new TypeError('callback must be a function')
						if verify.isObject(mTree) !== true
							return
								_ callback
									_ error
										@ 'InvalidArgument'
										@ 'mTree'
										@ 'The mTree parameter must be an object'
						_ loadModelFromMTree
							@ mTree
							@ 'Unavailable (loaded from mTree)'
							@ wizziModelRequest || {}
							{
								@ wizziFactory wizziFactory
							@ callback
			else
				#
					# Load a WizziModel of type '${schema.wzId}' from an IttfDocument uri
					# params
						# string ittfDocumentUri
						# { loadContext
							# { __productionManager
								# { productionContext
									# { aclstat
							# { __ittfDocumentStore
							# { mTreeBuildupContext
								# optional
							# { __request
								# This is a legacy that should disappear.
								# See the wizzi.production.productionContext class.
								# boolean dumpAll
								# boolean dumpIttfModel
								# boolean dumpModel
								# boolean dumpModelAfterInitializeAsync
						#   callback
				return
					function loadModel
						param ittfDocumentUri
						param loadContext
						param callback

						if typeof callback !== 'function'
							throw new TypeError('callback must be a function')

						if typeof ittfDocumentUri !== 'string'
							return
								_ callback
									_ error
										@ 'InvalidArgument'
										@ 'loadModel'
										@ 'The ittfDocumentUri parameter must be a string'

						if verify.isObject(loadContext) !== true
							return
								_ callback
									_ error
										@ 'InvalidArgument'
										@ 'loadModel'
										@ 'The loadContext parameter must be an object'

						if verify.isObject(loadContext.__productionManager) !== true
							return
								_ callback
									_ error
										@ 'InvalidArgument'
										@ 'loadModel'
										@ 'The loadContext.__productionManager parameter must be an object'

						set loadContext.mTreeBuildupContext
							_ Object.assign
								{
								_ loadContext.__productionManager.globalContext
								@ loadContext.mTreeBuildupContext

						var wizziModelRequest = loadContext.__request || {}
						var start = Date.now()
			
						# load the magical tree
						_ loadMTree
							@ ittfDocumentUri
							@ loadContext
							function
								param err
								param mTree
								if err
									return callback(err)
					
								# TODO implement a stats object inside the wizziModelRequest object
								# _ log.info('Loaded Wizzi model instance of schema ${schema.wzId} from Ittf document ' + ittfDocumentUri + ' in ' + (Date.now() - start) + ' ms')
					
								if (wizziModelRequest.dumpAll || wizziModelRequest.dumpIttfModel) && file.isFilePath(ittfDocumentUri)
									var ittfDumpPath = path.join(path.dirname(ittfDocumentUri), '_debug', path.basename(ittfDocumentUri) + '.ittf.json')
									_ file.write
										@ ittfDumpPath
										_ stringify
											@ mTree
											@ null
											@ 2

								$if schema.mTreeIsPreprocessed 
									set mTree
										_ ${schema.wzId}mTreePreProcessor
											@ mTree
											@ loadContext
					
								_ loadModelFromMTree
									@ mTree
									@ ittfDocumentUri
									@ wizziModelRequest
									{
										@ wizziFactory wizziFactory
									@ callback
		
	function error		
		param code
		param method
		param message
		return 
			{
				@ __is_error_ true
				@ code code
				@ method method
				@ message message
				@ source __filename
