<commit_before>'use strict';

const fs = require('fs'),
    _ = require('lodash'),
    merge = require('../utils/object_utils').merge,
    FieldTypes = require('../core/jhipster/field_types'),
    RelationshipTypes = require('../core/jhipster/relationship_types').RELATIONSHIP_TYPES,
    DatabaseTypes = require('../core/jhipster/database_types').Types,
    formatComment = require('../utils/format_utils').formatComment,
    dateFormatForLiquibase = require('../utils/format_utils').dateFormatForLiquibase,
    buildException = require('../exceptions/exception_factory').buildException,
    exceptions = require('../exceptions/exception_factory').exceptions;

var entities;
var isType;
var jdlObject;

module.exports = {
  parse: parse
};

/**
 * Keys of args:
 *   - jdlObject,
 *   - databaseType,
 */
function parse(args) {
  var merged = merge(defaults(), args);
  if (!args || !merged.jdlObject || !args.databaseType) {
    throw new buildException(
        exceptions.NullPointer,
        'The JDL object and the database type are both mandatory.');
  }
  checkNoSQLModeling(merged.jdlObject, args.databaseType);
  init(merged, args.databaseType);
  initializeEntities();
  setOptions();
  fillEntities();
  return entities;
}

function init(args, databaseType) {
  if (jdlObject) {
    resetState();
  }
  jdlObject = args.jdlObject;
  entities = {};
  switch (databaseType) { // todo: make a method from this code!
    case 'sql':
      isType = FieldTypes.isSQLType;
      break;
    case 'mongodb':
      isType = FieldTypes.isMongoDBType;
      break;
    case 'cassandra':
      isType = FieldTypes.isCassandraType;
      break;
    default:
      resetState();
      throw new buildException(
          exceptions.IllegalArgument,
          "The passed database type must either be 'sql', 'mongodb', or 'cassandra'");
  }
}

function resetState() {
  jdlObject = null;
  entities = null;
  isType = null;
}

function checkNoSQLModeling(passedJdlObject, passedDatabaseType) {
  if (passedJdlObject.relationships.size !== 0 && passedDatabaseType !== DatabaseTypes.sql) {
    throw new buildException(
        exceptions.NoSQLModeling, "NoSQL entities don't have relationships.");
  }
}


function initializeEntities() {
  for (let i = 0, entityNames = Object.keys(jdlObject.entities); i < entityNames.length; i++) {
    entities[entityNames[i]] = { // todo: make a builder/factory instead!
      relationships: [],
      fields: [],
      changelogDate: dateFormatForLiquibase(i),
      javadoc: formatComment(jdlObject.entities[entityNames[i]].comment),
      entityTableName: _.snakeCase(jdlObject.entities[entityNames[i]].tableName),
      dto: 'no',
      pagination: 'no',
      service: 'no'
    };
  }
}

function setOptions() {
  for (let i = 0; i < jdlObject.options.length; i++) {
    let option = jdlObject.options[i];
    if (option.getType() === 'UNARY') {
      continue;
    }
    if (option.entityNames.size() === 1 && option.entityNames.has('*')) {
      option.entityNames = Object.keys(jdlObject.entities).filter(function (entityName) {
        return !option.excludedNames.has(entityName);
      });
    }
    option.entityNames.forEach(function (entityName) {
      entities[entityName][option.name] = option.value;
    });
  }
}

function defaults() {
  return {};
}

function fillEntities() {
  for (let i = 0, entityNames = Object.keys(jdlObject.entities); i < entityNames.length; i++) {
    setFieldsOfEntity(entityNames[i]);
    setRelationshipOfEntity(entityNames[i]);
  }
}

function setFieldsOfEntity(entityName) {
  for (let i = 0, fieldNames = Object.keys(jdlObject.entities[entityName].fields); i < fieldNames.length; i++) {
    let jdlField = jdlObject.entities[entityName].fields[fieldNames[i]];
    let fieldData = {
      fieldName: _.camelCase(fieldNames[i])
    };
    let comment = formatComment(jdlField.comment);
    if (comment) {
      fieldData.comment = comment;
    }
    if (isType(jdlField.type)) {
      fieldData.fieldType = jdlField.type;
    }
    if (jdlObject.enums[jdlField.type]) {
      fieldData.fieldValues = jdlObject.enums[jdlField.type].values.join(',');
    }
    if (fieldData.fieldType === 'ImageBlob') {
      fieldData.fieldType = 'byte[]';
      fieldData.fieldTypeBlobContent = 'image';
    } else if (fieldData.fieldType === 'Blob' || fieldData.fieldType === 'AnyBlob') {
      fieldData.fieldType = 'byte[]';
      fieldData.fieldTypeBlobContent = 'any';
    }
    setValidationsOfField(jdlField, fieldData);
    entities[entityName].fields.push(fieldData);
  }
}

function setValidationsOfField(jdlField, fieldData) {
  if (Object.keys(jdlField.validations).length === 0) {
    return;
  }
  fieldData.fieldValidateRules = [];
  for (let i = 0, validationNames = Object.keys(jdlField.validations); i < validationNames.length; i++) {
    let validation = jdlField.validations[validationNames[i]];
    fieldData.fieldValidateRules.push(validation.name);
    if (validation.name !== 'required') {
      fieldData[`fieldValidateRules${_.capitalize(validation.name)}`] = validation.value;
    }
  }
}
<commit_msg>fix issue with enum fieldType<commit_after>'use strict';

const fs = require('fs'),
    _ = require('lodash'),
    merge = require('../utils/object_utils').merge,
    FieldTypes = require('../core/jhipster/field_types'),
    RelationshipTypes = require('../core/jhipster/relationship_types').RELATIONSHIP_TYPES,
    DatabaseTypes = require('../core/jhipster/database_types').Types,
    formatComment = require('../utils/format_utils').formatComment,
    dateFormatForLiquibase = require('../utils/format_utils').dateFormatForLiquibase,
    buildException = require('../exceptions/exception_factory').buildException,
    exceptions = require('../exceptions/exception_factory').exceptions;

var entities;
var isType;
var jdlObject;

module.exports = {
  parse: parse
};

/**
 * Keys of args:
 *   - jdlObject,
 *   - databaseType,
 */
function parse(args) {
  var merged = merge(defaults(), args);
  if (!args || !merged.jdlObject || !args.databaseType) {
    throw new buildException(
        exceptions.NullPointer,
        'The JDL object and the database type are both mandatory.');
  }
  checkNoSQLModeling(merged.jdlObject, args.databaseType);
  init(merged, args.databaseType);
  initializeEntities();
  setOptions();
  fillEntities();
  return entities;
}

function init(args, databaseType) {
  if (jdlObject) {
    resetState();
  }
  jdlObject = args.jdlObject;
  entities = {};
  switch (databaseType) { // todo: make a method from this code!
    case 'sql':
      isType = FieldTypes.isSQLType;
      break;
    case 'mongodb':
      isType = FieldTypes.isMongoDBType;
      break;
    case 'cassandra':
      isType = FieldTypes.isCassandraType;
      break;
    default:
      resetState();
      throw new buildException(
          exceptions.IllegalArgument,
          "The passed database type must either be 'sql', 'mongodb', or 'cassandra'");
  }
}

function resetState() {
  jdlObject = null;
  entities = null;
  isType = null;
}

function checkNoSQLModeling(passedJdlObject, passedDatabaseType) {
  if (passedJdlObject.relationships.size !== 0 && passedDatabaseType !== DatabaseTypes.sql) {
    throw new buildException(
        exceptions.NoSQLModeling, "NoSQL entities don't have relationships.");
  }
}


function initializeEntities() {
  for (let i = 0, entityNames = Object.keys(jdlObject.entities); i < entityNames.length; i++) {
    entities[entityNames[i]] = { // todo: make a builder/factory instead!
      relationships: [],
      fields: [],
      changelogDate: dateFormatForLiquibase(i),
      javadoc: formatComment(jdlObject.entities[entityNames[i]].comment),
      entityTableName: _.snakeCase(jdlObject.entities[entityNames[i]].tableName),
      dto: 'no',
      pagination: 'no',
      service: 'no'
    };
  }
}

function setOptions() {
  for (let i = 0; i < jdlObject.options.length; i++) {
    let option = jdlObject.options[i];
    if (option.getType() === 'UNARY') {
      continue;
    }
    if (option.entityNames.size() === 1 && option.entityNames.has('*')) {
      option.entityNames = Object.keys(jdlObject.entities).filter(function (entityName) {
        return !option.excludedNames.has(entityName);
      });
    }
    option.entityNames.forEach(function (entityName) {
      entities[entityName][option.name] = option.value;
    });
  }
}

function defaults() {
  return {};
}

function fillEntities() {
  for (let i = 0, entityNames = Object.keys(jdlObject.entities); i < entityNames.length; i++) {
    setFieldsOfEntity(entityNames[i]);
    setRelationshipOfEntity(entityNames[i]);
  }
}

function setFieldsOfEntity(entityName) {
  for (let i = 0, fieldNames = Object.keys(jdlObject.entities[entityName].fields); i < fieldNames.length; i++) {
    let jdlField = jdlObject.entities[entityName].fields[fieldNames[i]];
    let fieldData = {
      fieldName: _.camelCase(fieldNames[i])
    };
    let comment = formatComment(jdlField.comment);
    if (comment) {
      fieldData.comment = comment;
    }
    if (isType(jdlField.type)) {
      fieldData.fieldType = jdlField.type;
    }
    if (jdlObject.enums[jdlField.type]) {
      fieldData.fieldType = jdlField.type;
      fieldData.fieldValues = jdlObject.enums[jdlField.type].values.join(',');
    }
    if (fieldData.fieldType === 'ImageBlob') {
      fieldData.fieldType = 'byte[]';
      fieldData.fieldTypeBlobContent = 'image';
    } else if (fieldData.fieldType === 'Blob' || fieldData.fieldType === 'AnyBlob') {
      fieldData.fieldType = 'byte[]';
      fieldData.fieldTypeBlobContent = 'any';
    }
    setValidationsOfField(jdlField, fieldData);
    entities[entityName].fields.push(fieldData);
  }
}

function setValidationsOfField(jdlField, fieldData) {
  if (Object.keys(jdlField.validations).length === 0) {
    return;
  }
  fieldData.fieldValidateRules = [];
  for (let i = 0, validationNames = Object.keys(jdlField.validations); i < validationNames.length; i++) {
    let validation = jdlField.validations[validationNames[i]];
    fieldData.fieldValidateRules.push(validation.name);
    if (validation.name !== 'required') {
      fieldData[`fieldValidateRules${_.capitalize(validation.name)}`] = validation.value;
    }
  }
}

{'fraction_non_alphanumeric': 0.14195060041003613, 'fraction_numerical': 0.001562042370399297, 'mean_word_length': 3.6563636363636363, 'pattern_counts': {'":': 0, '<': 13, '<?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': '6408971', 'n_tokens_mistral': 3236, 'n_tokens_neox': 3229, 'n_words': 604}