/*
* Name : DslDomain.js
* Module : Back-end::Lib::Model::DSLModel
* Location : /lib/model/dslmodel/
*
* History :
* 
* Version         Date           Programmer
* =================================================
* 0.0.1          2016-05-30     Davide Polonio
* -------------------------------------------------
* Corretto typo. Trovato errore in `new Engine()`
* =================================================
* 0.0.1          2016-05-11     Andrea Mantovani
* -------------------------------------------------
* Rimosso metodo init
* Rinominato metodo DslLoadFile in DslLoad
* =================================================
* 0.0.1          2014-03-01     Federico Poli
* -------------------------------------------------
* Codifica modulo
* =================================================
*/

"use strict";

var DslConcreteStrategy = require("./DslConcreteStrategy");
var MaapError = require("../utils/MaapError.js");

var _prefix = "_id";

var DslDomain = function() {
    this.modelRegistry = {};
    this.strategy = new DslConcreteStrategy();
    this.id = 0;
};

DslDomain.prototype.loadDSL = function(data, callback) {
    var self = this;
    self.strategy.loadDSL(data, function(models) {
		var ids = []

		models.forEach(function(model) {
			ids.push(self.register(model));
		});

		callback(undefined, ids);

    }, function(maaperror) {
		callback(maaperror, undefined);
    });
};

DslDomain.prototype.register = function(model) {
    var idc = `${_prefix}${this.id++}`;
    this.modelRegistry[idc] = model;
    
    return {
		token: idc,
		type: model.constructor.name
	};
};

DslDomain.prototype.getByToken = function(token) {
	return this.modelRegistry[token];
};

DslDomain.prototype.getAll = function() {
	return this.modelRegistry;
};


var compareCollectionWeight = function(a, b) {
	var aw = a.getWeight();
	var bw = b.getWeight();
	if (aw < bw) {
		return -1;
	}
	if (aw > bw) {
		return 1;
	}
	return 0;
};

DslDomain.prototype.getCollectionModels = function() {
	var models = [];

	for (var id in this.modelRegistry) {
		if (this.modelRegistry.hasOwnProperty(id)) {
			models.push(this.modelRegistry[id]);
		}
	}
	models.sort(compareCollectionWeight);
	
	return models;
};

module.exports = DslDomain;

@@ -1763,451 +1763,8 @@
 %7D;%0A%0A
-%0Avar compareCollectionWeight = function(a, b) %7B%0A%09var aw = a.getWeight();%0A%09var bw = b.getWeight();%0A%09if (aw %3C bw) %7B%0A%09%09return -1;%0A%09%7D%0A%09if (aw %3E bw) %7B%0A%09%09return 1;%0A%09%7D%0A%09return 0;%0A%7D;%0A%0ADslDomain.prototype.getCollectionModels = function() %7B%0A%09var models = %5B%5D;%0A%0A%09for (var id in this.modelRegistry) %7B%0A%09%09if (this.modelRegistry.hasOwnProperty(id)) %7B%0A%09%09%09models.push(this.modelRegistry%5Bid%5D);%0A%09%09%7D%0A%09%7D%0A%09models.sort(compareCollectionWeight);%0A%09%0A%09return models;%0A%7D;%0A%0A
 modu

Move method in CollectionEngine