<commit_before>var loopback = require('loopback');
var Q = require('q');
var traverse = require('traverse');

var config = require('../support/config');
var logger = require('../support/logger');

var JsonSchemaValidator = require('./json-schema-validator');
var Links = require('./links');
var readOnlyDefaultValuesHandler = require('./readonly-default-values-handler');
var modelPropertiesConverter = require('./model-properties-converter');

var itemSchemaProperties = {
    collectionName: {type: 'string', required: true},
    modelName: {type: 'string', required: true}
};

if (!config.generatedId) {
    itemSchemaProperties.id = {type: 'string', generated: false, id: true};
}
var ItemSchema = module.exports = config.Model.extend('item-schema', itemSchemaProperties);

// fill in loopbackJsonSchema.init
ItemSchema.app = null;

ItemSchema.prototype.allLinks = function() {
    var links = new Links(this.defaultLinks(), this.links);
    return links.all();
};

ItemSchema.prototype.customLinks = function() {
    var links = new Links(this.defaultLinks(), this.links);
    return links.custom();
};

ItemSchema.prototype.defaultLinks = function() {
    var itemUrlTemplate = this.itemUrlTemplate();
    var collectionUrl = this.collectionUrl();
    var schemaUrl = this.url();
    return [
        { rel: 'self', href: itemUrlTemplate },
        { rel: 'item', href: itemUrlTemplate },
        { rel: 'create', method: 'POST', href: collectionUrl, schema: { $ref: schemaUrl } },
        { rel: 'update', method: 'PUT', href: itemUrlTemplate },
        { rel: 'delete', method: 'DELETE', href: itemUrlTemplate }
    ];
};

ItemSchema.prototype.url = function() {
    var path = '/' + this.constructor.pluralModelName + '/' + this.id;
    return path;
};

ItemSchema.prototype.itemUrlTemplate = function() {
    return this.collectionUrl() + '/{id}';
};

ItemSchema.prototype.collectionUrl = function() {
    var path = '/' + this.collectionName;
    return path;
};

ItemSchema.prototype.update$schema = function() {
    if (!this.$schema) {
        this.$schema = "http://json-schema.org/draft-04/hyper-schema#";
    }
};

ItemSchema.prototype.beforeRegisterLoopbackModel = function(app, JsonSchemaModel, callback) {
    logger.debug('Entered beforeRegisterLoopbackModel');

    if (ItemSchema.app._registeredLoopbackHooks[JsonSchemaModel.modelName]) {
        logger.debug('Loopback model name: '+JsonSchemaModel.modelName+' is already registered');
    } else {
        ItemSchema.defineRemoteHooks(JsonSchemaModel);
        ItemSchema.app._registeredLoopbackHooks[JsonSchemaModel.modelName] = true;
    }

    callback();
};

ItemSchema.prototype.constructModel = function() {
    var indexes = this.indexes || {};
    var model = config.Model.extend(
        this.modelName,
        modelProperties.call(this),
        { plural: this.collectionName, indexes: indexes }
    );

    addJsonSchemaValidation.call(this, model);
    model.attachTo(ItemSchema.dataSource);

    return model;
};

ItemSchema.prototype.createIndexes = function(callback) {
    var dataSource = this.getDataSource();

    if (dataSource.connector && dataSource.connector.autoupdate) {
        logger.info('Start creating indexes dynamically.');

        dataSource.connector.autoupdate([this.modelName], function ensureIndexCallback(err) {
            if (err) {
                return callback(err);
            }

            logger.info('Ensured dinamic index.');
            callback(null, true);
        });
    } else {
        callback(null, false);
    }
};

ItemSchema.prototype.model = function() {
    return loopback.findModel(this.modelName) || null;
};

ItemSchema.prototype.collectionSchema = function() {
    return new config.CollectionSchemaClass(this);
};

ItemSchema.findByCollectionName = function(collectionName, callback) {
    ItemSchema.findOne({ where: { collectionName: collectionName }}, function(err, jsonSchema) {
        if (err) {
            logger.error("Error fetching JSON Schema for collectionName:", collectionName, "Error:", err);
        } else if (jsonSchema === null) {
            logger.warn("JSON Schema for collectionName", collectionName, "not found.");
        }
        callback(err, jsonSchema);
    });
};

ItemSchema.preLoadedModels = false;
ItemSchema.preLoadModels = function() {
    ItemSchema.preLoadedModels = false;

    var registerModel = function(schema, model) {
        var deferred = Q.defer();<commit_msg>Remove unnecessary variable.<commit_after>var loopback = require('loopback');
var Q = require('q');
var traverse = require('traverse');

var config = require('../support/config');
var logger = require('../support/logger');

var JsonSchemaValidator = require('./json-schema-validator');
var Links = require('./links');
var readOnlyDefaultValuesHandler = require('./readonly-default-values-handler');
var modelPropertiesConverter = require('./model-properties-converter');

var itemSchemaProperties = {
    collectionName: {type: 'string', required: true},
    modelName: {type: 'string', required: true}
};

if (!config.generatedId) {
    itemSchemaProperties.id = {type: 'string', generated: false, id: true};
}
var ItemSchema = module.exports = config.Model.extend('item-schema', itemSchemaProperties);

// fill in loopbackJsonSchema.init
ItemSchema.app = null;

ItemSchema.prototype.allLinks = function() {
    var links = new Links(this.defaultLinks(), this.links);
    return links.all();
};

ItemSchema.prototype.customLinks = function() {
    var links = new Links(this.defaultLinks(), this.links);
    return links.custom();
};

ItemSchema.prototype.defaultLinks = function() {
    var itemUrlTemplate = this.itemUrlTemplate();
    var collectionUrl = this.collectionUrl();
    var schemaUrl = this.url();
    return [
        { rel: 'self', href: itemUrlTemplate },
        { rel: 'item', href: itemUrlTemplate },
        { rel: 'create', method: 'POST', href: collectionUrl, schema: { $ref: schemaUrl } },
        { rel: 'update', method: 'PUT', href: itemUrlTemplate },
        { rel: 'delete', method: 'DELETE', href: itemUrlTemplate }
    ];
};

ItemSchema.prototype.url = function() {
    var path = '/' + this.constructor.pluralModelName + '/' + this.id;
    return path;
};

ItemSchema.prototype.itemUrlTemplate = function() {
    return this.collectionUrl() + '/{id}';
};

ItemSchema.prototype.collectionUrl = function() {
    var path = '/' + this.collectionName;
    return path;
};

ItemSchema.prototype.update$schema = function() {
    if (!this.$schema) {
        this.$schema = "http://json-schema.org/draft-04/hyper-schema#";
    }
};

ItemSchema.prototype.beforeRegisterLoopbackModel = function(app, JsonSchemaModel, callback) {
    logger.debug('Entered beforeRegisterLoopbackModel');

    if (ItemSchema.app._registeredLoopbackHooks[JsonSchemaModel.modelName]) {
        logger.debug('Loopback model name: '+JsonSchemaModel.modelName+' is already registered');
    } else {
        ItemSchema.defineRemoteHooks(JsonSchemaModel);
        ItemSchema.app._registeredLoopbackHooks[JsonSchemaModel.modelName] = true;
    }

    callback();
};

ItemSchema.prototype.constructModel = function() {
    var indexes = this.indexes || {};
    var model = config.Model.extend(
        this.modelName,
        modelProperties.call(this),
        { plural: this.collectionName, indexes: indexes }
    );

    addJsonSchemaValidation.call(this, model);
    model.attachTo(ItemSchema.dataSource);

    return model;
};

ItemSchema.prototype.createIndexes = function(callback) {
    var dataSource = this.getDataSource();

    if (dataSource.connector && dataSource.connector.autoupdate) {
        logger.info('Start creating indexes dynamically.');

        dataSource.connector.autoupdate([this.modelName], function ensureIndexCallback(err) {
            if (err) {
                return callback(err);
            }

            logger.info('Ensured dinamic index.');
            callback(null, true);
        });
    } else {
        callback(null, false);
    }
};

ItemSchema.prototype.model = function() {
    return loopback.findModel(this.modelName) || null;
};

ItemSchema.prototype.collectionSchema = function() {
    return new config.CollectionSchemaClass(this);
};

ItemSchema.findByCollectionName = function(collectionName, callback) {
    ItemSchema.findOne({ where: { collectionName: collectionName }}, function(err, jsonSchema) {
        if (err) {
            logger.error("Error fetching JSON Schema for collectionName:", collectionName, "Error:", err);
        } else if (jsonSchema === null) {
            logger.warn("JSON Schema for collectionName", collectionName, "not found.");
        }
        callback(err, jsonSchema);
    });
};

ItemSchema.preLoadedModels = false;
ItemSchema.preLoadModels = function() {
    var registerModel = function(schema, model) {
        var deferred = Q.defer();
{'fraction_non_alphanumeric': 0.1404140414041404, 'fraction_numerical': 0.00045004500450045, 'mean_word_length': 3.627277459656429, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3679228', 'n_tokens_mistral': 2491, 'n_tokens_neox': 2451, 'n_words': 529}