<commit_before>'use strict';

var _ = require('lodash');
var BaseManager = require('./base.manager.js');
var virtualDomainValidator = require('../validators/virtualDomain.validator');

var VirtualDomainManager = function (options) {
  this.events = options.events;
  this.Phrases = options.Phrases;
  this.Snippets = options.Snippets;
};

VirtualDomainManager.prototype = new BaseManager({
  itemName: 'virtualDomain',
  item: '__virtualDomains',
  validator: virtualDomainValidator
});

//Compilation
VirtualDomainManager.prototype._compile = function (vdomain) {
  return vdomain;
};

VirtualDomainManager.prototype._addToList = function (domain, vdModel) {
  if (!domain || !vdModel) {
    return false;
  }

  if (!this.__virtualDomains[domain]) {
    this.__virtualDomains[domain] = {};
  }

  this.__virtualDomains[domain] = vdModel;

  this.Phrases.register(this.domain, vdModel.getRawPhrases());
  this.Snippets.register(this.domain, vdModel.getRawSnippets());
  
  return true;
};

VirtualDomainManager.prototype._unregister = function (domain) {
  if (!domain) {
    this.events.emit('warn', 'virtualDomain:unregister:missing:parameters', domain);
    return false;
  }

  if (this.__virtualDomains[domain]) {
    //@TODO: go to phrases and unregister all
    delete this.__virtualDomains[domain];
    this.events.emit('debug', 'virtualDomain:unregistered', domain);
    return true;
  } else {
    this.events.emit('warn', 'virtualDomain:unregister:not:found', domain);
    return false;
  }
};

VirtualDomainManager.prototype.getVirtualDomains = function (domain) {
  if (!domain) {
    return this._getVirtualDomainsAsList();
  }

  return this.__virtualDomains[domain] ? this.__virtualDomains[domain] : null;
};

VirtualDomainManager.prototype._getVirtualDomainsAsList = function () {
  var module = this;
  return _.flatten(Object.keys(this.__phrases).map(function (key) {
    return module.__phrases[key];
  }));
};

module.exports = VirtualDomainManager;<commit_msg>new methods in virtualDomain added<commit_after>'use strict';

var _ = require('lodash');
var BaseManager = require('./base.manager.js');
var virtualDomainValidator = require('../validators/virtualDomain.validator');
var virtualDomainDao = require('../loaders/virtualDomainDao')

var VirtualDomainManager = function (options) {
  this.events = options.events;
  this.Phrases = options.Phrases;
  this.Snippets = options.Snippets;
};

VirtualDomainManager.prototype = new BaseManager({
  itemName: 'virtualDomain',
  item: '__virtualDomains',
  validator: virtualDomainValidator
});

//Compilation
VirtualDomainManager.prototype._compile = function (vdomain) {
  return vdomain;
};

VirtualDomainManager.prototype._addToList = function (domain, vdModel) {
  if (!domain || !vdModel) {
    return false;
  }

  if (!this.__virtualDomains[domain]) {
    this.__virtualDomains[domain] = {};
  }

  this.__virtualDomains[domain][vModel.getApiId()] = vdModel;

  this.Phrases.register(this.domain, vdModel.getRawPhrases());
  this.Snippets.register(this.domain, vdModel.getRawSnippets());
  
  return true;
};

VirtualDomainManager.prototype._unregister = function (domain) {
  if (!domain) {
    this.events.emit('warn', 'virtualDomain:unregister:missing:parameters', domain);
    return false;
  }

  if (this.__virtualDomains[domain]) {
    //@TODO: go to phrases and unregister all
    delete this.__virtualDomains[domain];
    this.events.emit('debug', 'virtualDomain:unregistered', domain);
    return true;
  } else {
    this.events.emit('warn', 'virtualDomain:unregister:not:found', domain);
    return false;
  }
};

VirtualDomainManager.prototype.getVirtualDomains = function (domain) {
  if (!domain) {
    return this._getVirtualDomainsAsList();
  }

  return this.__virtualDomains[domain] ? this.__virtualDomains[domain] : null;
};

VirtualDomainManager.prototype._getVirtualDomainsAsList = function () {
  var module = this;
  return _.flatten(Object.keys(this.__phrases).map(function (key) {
    return module.__phrases[key];
  }));
};

VirtualDomainManager.prototype.getById = function(id) {
    var domain = this._extracDomainFromId

    if(this.__virtualDomains[domain][id] && _.size(this.__virtualDomains[domain][id]) > 0){
        return this.__virtualDomains[domain][id]
    } else {
        return this.loadAndRegisterById(id)
    }
};

VirtualDomainManager.prototype.getByDomain = function(domain) {
    if(this.__virtualDomains[domain] && _.size(this.__virtualDomains[domain]) > 0){
        return this.__virtualDomains[domain]
    } else {
        return this.loadAndRegisterByDomain(domain)
    }
};

VirtualDomainManager.prototype.getAll = function() {
    if(this.__virtualDomains && _.size(this.__virtualDomains) > 0){
        return this.__virtualDomains
    } else {
        return this.loadAndRegisterAll()
    }
};

VirtualDomainManager.prototype.loadAndRegisterById = function(id) {
    var that = this
    var vd
    var domain = this._extracDomainFromId(id)

    return virtualDomainDao.load(id)
    .then(function(vd){
        return that.getVirtualDomainModels(vd)
    })
    .then(function(vdModel){
         that.events.emit('debug', 
        'VirtualDomain', vmodel.getId(), 
        'Phrases', vmodel.getRawPhrases().length,
        'Snippets', vmodel.getRawSnippets().length);

        return this._register(domain, vdModel)
    })
    .catch(function(err) {
      that.events.emit('warn', 'data:error:loading');
      return Promise.reject(err && err.data ? err.data : err);
    });

};

VirtualDomainManager.prototype.loadAndRegisterByDomain = function(domain) {
};

VirtualDomainManager.prototype.loadAndRegisterAll = function() {
};

VirtualDomainManager.prototype.save = function(vd) {
};

VirtualDomainManager.prototype.validate = function(vd) {
    return this.validate(vd)
};

module.exports = VirtualDomainManager;
{'fraction_non_alphanumeric': 0.13347022587268995, 'fraction_numerical': 0.000513347022587269, 'mean_word_length': 4.4779756326148075, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4918291', 'n_tokens_mistral': 1787, 'n_tokens_neox': 1770, 'n_words': 303}