const DBH_CONSTANTS = require('./dbh-constants');
const jhipsterCore = require('jhipster-core');
const pluralize = require('pluralize');


/**
 * assert parameter is a non-empty string
 * @todo Now unused, consider removal
 */
const isTrueString = x => typeof x === 'string' && x !== '';


const getColumnIdName = name => `${hibernateSnakeCase(name)}_id`;


const getPluralColumnIdName = name => getColumnIdName(pluralize(name));


/**
 * get hibernate SnakeCase in JHipster preferred style.
 *
 * @param {string} value - table column name or table name string
 * @see org.springframework.boot.orm.jpa.hibernate.SpringNamingStrategy
 */
const hibernateSnakeCase = value => {
    let res = '';
    if (value) {
        value = value.replace('.', '_');
        res = value[0];
        for (let i = 1, len = value.length - 1; i < len; i++) {
            if (value[i - 1] !== value[i - 1].toUpperCase() &&
                value[i] !== value[i].toLowerCase() &&
                value[i + 1] !== value[i + 1].toUpperCase()
            ) {
                res += `_${value[i]}`;
            } else {
                res += value[i];
            }
        }
        res += value[value.length - 1];
        res = res.toLowerCase();
    }
    return res;
};


/**
 * Check if these relationships add constraints.
 * Typically, an one-to-many relationship doesn't add a constraint to the entity on the one side.
 *
 * @param relationships an array of relationship to check
 * @returns {boolean}
 */
const hasConstraints = relationships => {
    for (let idx = 0; idx < relationships.length; idx++) {
        if (relationships[idx].relationshipType === 'many-to-one' || (relationships[idx].relationshipType === 'one-to-one' && relationships[idx].ownerSide)) {
            return true;
        }
        if (relationships[idx].relationshipType === 'many-to-many' && relationships[idx].ownerSide) {
            return true;
        }
    }

    return false;
};


/** Validate user input when asking for a SQL column name */
const validateColumnName = (input, dbType) => {
    if (!/^([a-zA-Z0-9_]*)$/.test(input)) {
        return 'Your column name cannot contain special characters';
    } else if (input === '') {
        return 'Your column name cannot be empty';
    } else if (dbType === 'oracle' && input.length > 26) {
        return 'Your column name is too long for Oracle, try a shorter name';
    }
    return true;
};


/**
 * Validate user input when asking for a SQL table name
 * This function closely follows JHipster's own validateTableName function
 * (in generator-jhipster/generators/entity/index.js)
 */
const validateTableName = (input, dbType) => {
    if (!/^([a-zA-Z0-9_]*)$/.test(input)) {
        return 'The table name cannot contain special characters';
    } else if (input === '') {
        return 'The table name cannot be empty';
    } else if (dbType === 'oracle' && input.length > 26) {
        return 'The table name is too long for Oracle, try a shorter name';
    } else if (dbType === 'oracle' && input.length > 14) {
        return 'The table name is long for Oracle, long table names can cause issues when used to create constraint names and join table names';
    } else if (jhipsterCore.isReservedTableName(input, dbType)) {
        return `'${input}' is a ${dbType} reserved keyword.`;
    }
    return true;
};


module.exports = {
    getColumnIdName,
    getPluralColumnIdName,
    hasConstraints,
    isTrueString,
    validateColumnName,
    validateTableName
};

@@ -421,24 +421,994 @@
 e(name));%0A%0A%0A
+/**%0A * from the JHipster files where the original Spring naming strategies can be found,%0A * remove the files that don't exist, depending on the current application build tool (Maven or Gradle)%0A * if the app uses Maven, remove the Gradle file(s)%0A * if the app uses Gradle, remove the Maven file(s)%0A * the returned array holds only the existing files%0A */%0Aconst getFilesWithNamingStrategy = (buildTool) =%3E %7B%0A    // fail when application build tool is unknown%0A    if (buildTool !== 'maven' && buildTool !== 'gradle') %7B%0A        throw new Error(%60build tool '$%7BbuildTool%7D' unknown%60);%0A    %7D%0A%0A    // utilities used to filter the files with naming strategy%0A    // TODO: no hardcoded values%0A    const removeGradleFiles = (item) =%3E item !== './gradle/liquibase.gradle';%0A    const removeMavenFiles = (item) =%3E item !== './pom.xml';%0A%0A    const files = DBH_CONSTANTS.filesWithNamingStrategy.filter(buildTool === 'maven' ? removeGradleFiles : removeMavenFiles);%0A%0A    return files;%0A%7D;%0A%0A%0A
 /**%0A * get h
@@ -4344,24 +4344,56 @@
 lumnIdName,%0A
+    getFilesWithNamingStrategy,%0A
     getPlura

add new function to dbh.js for naming strategies replacement