/*
* Name : DslConcreteStrategy.js
* Module : Back-end::Lib::Model::DSLModel
* Location : /lib/controller/middleware
*
* History :
* 
* Version         Date           Programmer
* =================================================
* 0.0.1          2015-05-15     Andrea Mantovani
* -------------------------------------------------
* Aggiornata elaborazione dsl con Sweet.js 1.x
* =================================================
* 0.0.1          2015-05-11     Andrea Mantovani
* -------------------------------------------------
* Rimosso metodo init
* =================================================
* 0.0.1          2014-03-01     Luca De Franceschi
* -------------------------------------------------
* Codifica modulo
* =================================================
*/

"use strict";

var SweetjsCompiler = require("sweetjs-compiler");
var fs = require("fs");
var MaapError = require("../utils/MaapError.js");
var vm = require("vm");

var DslCollectionModel = require("./DslCollectionModel");
var IndexModel = require("./IndexModel");
var ShowModel = require("./ShowModel");
var Row = require("./Row");
var Column = require("./Column");

var DslConcreteStrategy = function() {
	this.sweetjs = new SweetjsCompiler({
		ambient: __dirname	
	});

	try {
    	this.cachedMacro = fs.readFileSync(`${__dirname}/macro.sjs`, "utf-8");
	} catch (err) {
		throw new MaapError(err);	
	}
};

DslConcreteStrategy.prototype.loadDSL = function(content, domain, callback, errback) {
	// Inject the macro syntax into the content
	content = `${this.cachedMacro}\n\n${content}`;

	var out = null;
	try {
		out = this.sweetjs.compile(content);
	} catch(err) {
		errback(new MaapError(err));
		return;
	}

	var collections = [];
	var registerCollection = function(coll) {
		collections.push(coll);
	};

	try {
		vm.runInNewContext(out.code, {
			registerCollection: registerCollection,
			require: require,
			DslCollectionModel: DslCollectionModel,
			IndexModel: IndexModel,
			ShowModel: ShowModel,
			Row: Row,
			Column: Column,
			domain: domain
		});
	} catch(err) {
		errback(new MaapError(err));
		return;
	}

	callback(collections);
};

module.exports = DslConcreteStrategy;


@@ -1698,26 +1698,21 @@
 var 
-collection
+model
 s = %5B%5D;%0A
 %09var
@@ -1707,16 +1707,17 @@
 s = %5B%5D;%0A
+%0A
 %09var reg
@@ -1721,26 +1721,21 @@
 register
-Collection
+Model
  = funct
@@ -1741,39 +1741,33 @@
 tion
-(col
+ (ds
 l) %7B%0A%09%09
-collection
+model
 s.push(
-col
+ds
 l);%0A
@@ -1826,38 +1826,28 @@
 ster
-Collection: registerCollection
+Model: registerModel
 ,%0A%09%09
@@ -2091,18 +2091,13 @@
 ack(
-collection
+model
 s);%0A

Use general name