/*
* 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 CollectionModel = require("./CollectionModel");
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.load = function(content) {
	// 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 models = [];

	var registerModel = function (dsl) {
		models.push(dsl);
	};

	try {
		vm.runInNewContext(out.code, {
			registerModel: registerModel,
			require: require,
			CollectionModel: CollectionModel,
			IndexModel: IndexModel,
			ShowModel: ShowModel,
			Row: Row,
			Column: Column
		});
	} catch(err) {
		throw new MaapError(err);
	}

	return models;
};

module.exports = DslConcreteStrategy;


@@ -1176,17 +1176,20 @@
 ion() %7B%0A
-%09
+    
 this.swe
@@ -1217,17 +1217,16 @@
 piler(%7B%0A
-%09
 %09ambient
@@ -1242,15 +1242,21 @@
 me%09%0A
-%09
+    
 %7D);%0A%0A
-%09
+    
 try 
@@ -1333,17 +1333,20 @@
 tf-8%22);%0A
-%09
+    
 %7D catch 
@@ -1345,33 +1345,32 @@
 %7D catch (err) %7B%0A
-%09
 %09throw new MaapE
@@ -1381,17 +1381,20 @@
 (err);%09%0A
-%09
+    
 %7D%0A%7D;%0A%0ADs
@@ -1444,21 +1444,36 @@
 (content
-) %7B%0A%09
+, connection) %7B%0A    
 // Injec
@@ -1508,17 +1508,20 @@
 content%0A
-%09
+    
 content 
@@ -1560,17 +1560,20 @@
 ent%7D%60;%0A%0A
-%09
+    
 var out 
@@ -1580,24 +1580,26 @@
 = null;%0A
-%09
+    
 try %7B%0A
-%09
 %09out = t
@@ -1628,17 +1628,20 @@
 ntent);%0A
-%09
+    
 %7D catch(
@@ -1647,17 +1647,16 @@
 (err) %7B%0A
-%09
 %09errback
@@ -1677,17 +1677,16 @@
 (err));%0A
-%09
 %09return;
@@ -1686,21 +1686,27 @@
 return;%0A
-%09%7D%0A%0A%09
+    %7D%0A%0A    
 var mode
@@ -1715,17 +1715,20 @@
  = %5B%5D;%0A%0A
-%09
+    
 var regi
@@ -1756,17 +1756,16 @@
 (dsl) %7B%0A
-%09
 %09models.
@@ -1779,21 +1779,26 @@
 l);%0A
-%09%7D;%0A%0A%09
+    %7D;%0A%0A    
 try %7B%0A
-%09
 %09vm.
@@ -1826,18 +1826,20 @@
 ode, %7B%0A%09
-%09%09
+    
 register
@@ -1861,18 +1861,20 @@
 Model,%0A%09
-%09%09
+    
 require:
@@ -1884,18 +1884,49 @@
 quire,%0A%09
-%09%09
+    connection: connection,%0A%09    
 Collecti
@@ -1952,18 +1952,20 @@
 Model,%0A%09
-%09%09
+    
 IndexMod
@@ -1981,18 +1981,20 @@
 Model,%0A%09
-%09%09
+    
 ShowMode
@@ -2008,18 +2008,20 @@
 Model,%0A%09
-%09%09
+    
 Row: Row
@@ -2023,18 +2023,20 @@
 : Row,%0A%09
-%09%09
+    
 Column: 
@@ -2046,15 +2046,17 @@
 umn%0A
-%09
 %09%7D);%0A
-%09
+    
 %7D ca
@@ -2066,17 +2066,16 @@
 (err) %7B%0A
-%09
 %09throw n
@@ -2097,13 +2097,19 @@
 r);%0A
-%09%7D%0A%0A%09
+    %7D%0A%0A    
 retu

Use connection