UNPKG

jhipster-rasha-core

Version:

JHipster's own domain language and core objects

271 lines (254 loc) 8.9 kB
'use strict'; const _ = require('lodash'), JDLObject = require('../core/jdl_object'), JDLEntity = require('../core/jdl_entity'), JDLField = require('../core/jdl_field'), JDLValidation = require('../core/jdl_validation'), JDLEnum = require('../core/jdl_enum'), JDLRelationship = require('../core/jdl_relationship'), JDLUnaryOption = require('../core/jdl_unary_option'), JDLBinaryOption = require('../core/jdl_binary_option'), RelationshipTypes = require('../core/jhipster/relationship_types').RELATIONSHIP_TYPES, UnaryOptions = require('../core/jhipster/unary_options').UNARY_OPTIONS, BinaryOptions = require('../core/jhipster/binary_options').BINARY_OPTIONS, buildException = require('../exceptions/exception_factory').buildException, exceptions = require('../exceptions/exception_factory').exceptions; module.exports = { parseEntities: parseEntities, parseServerOptions: parseServerOptions }; const USER = 'User'; const USER_ENTITY = new JDLEntity({name: USER}); /* * Parses the json entities into JDL * entities: json map with entity name as key and entity definition as value * jdl: JDLObject to which the parsed entities are added. If undefined a new JDLObject is created. * returns the JDLObject */ function parseEntities(entities, jdl) { if (!entities) { throw new buildException(exceptions.NullPointer, 'Entities have to be passed to be converted.'); } if (!jdl) { jdl = new JDLObject(); } let skipUserManagement = hasSkipUserManagement(jdl); for (let i = 0, entityNames = Object.keys(entities); i < entityNames.length; i++) { let entityName = entityNames[i]; if (entityName === USER && !skipUserManagement) { throw new buildException( exceptions.IllegalName, 'User entity name is reserved if skipUserManagement is not set.'); } let entity = entities[entityName]; jdl.addEntity(jsonToJDLEntity(entity, entityName)); addEnumsToJDL(jdl, entity); addEntityOptionsToJDL(jdl, entity, entityName); } addRelationshipsToJDL(jdl, entities, skipUserManagement); return jdl; } /* * Parses the jhipster configuration into JDL * jhConfig: the jhipster config ('generator-jhipster' in .yo-rc.json) * jdl: JDLObject to which the parsed options are added. If undefined a new JDLObject is created. * returns the JDLObject */ function parseServerOptions(jhConfig, jdl) { if (!jdl) { jdl = new JDLObject(); } for (let option of [UnaryOptions.SKIP_CLIENT, UnaryOptions.SKIP_SERVER, UnaryOptions.SKIP_USER_MANAGEMENT]) if (jhConfig[option] === true) { jdl.addOption(new JDLUnaryOption({ name: option, value: jhConfig[option] })); } return jdl; } function jsonToJDLEntity(entity, entityName) { const jdlEntity = new JDLEntity({ name: entityName, tableName: entity.entityTableName, comment: entity.javadoc }); for (let field of entity.fields) { jdlEntity.addField(jsonToJDLField(field)); } return jdlEntity; } function jsonToJDLField(field) { const jdlField = new JDLField({ name: _.lowerFirst(field.fieldName), type: field.fieldType, comment: field.javadoc }); if (field.fieldValidateRules) { for (let rule of field.fieldValidateRules) { jdlField.addValidation(jsonToJDLValidation(rule, field)); } } return jdlField; } function jsonToJDLValidation(rule, field) { return new JDLValidation({ name: rule, value: field[`fieldValidateRules${_.upperFirst(rule)}`] }); } function addEnumsToJDL(jdl, entity) { for (let field of entity.fields) { if (field.fieldValues !== undefined) { jdl.addEnum(new JDLEnum({ name: field.fieldType, values: field.fieldValues.split(',') })); } } } /* * Adds relationships for entities to JDL. * The jdl passed must contain the jdl entities concerned by the relationships */ function addRelationshipsToJDL(jdl, entities, skipUserManagement) { for (let i = 0, entityNames = Object.keys(entities); i < entityNames.length; i++) { dealWithRelationships(jdl, entities[entityNames[i]].relationships, entityNames[i], entities, skipUserManagement); } } function dealWithRelationships(jdl, relationships, entityName, entities, skipUserManagement) { for (let relationship of relationships) { let jdlRelationship = getRelationship(relationship, jdl, entityName, entities, skipUserManagement); if (jdlRelationship) { jdl.addRelationship(jdlRelationship); } } } function getInjectedFieldInFrom(relationship) { return relationship.relationshipName + (relationship.otherEntityField && relationship.otherEntityField !== 'id' ? `(${relationship.otherEntityField})` : ''); } function getRelationship(relationship, jdl, entityName, entities, skipUserManagement) { let type; let injectedFieldInTo; let isInjectedFieldInToRequired = false; let commentInTo; let otherEntity; let toJdlEntity; if (relationship.otherEntityName.toLowerCase() === USER.toLowerCase() && !skipUserManagement) { toJdlEntity = USER_ENTITY; } else { otherEntity = entities[_.upperFirst(relationship.otherEntityName)]; if (!otherEntity) { return; } toJdlEntity = jdl.entities[_.upperFirst(relationship.otherEntityName)]; for (let relationship2 of otherEntity.relationships) { if (_.upperFirst(relationship2.otherEntityName) === entityName && relationship2.otherEntityRelationshipName === relationship.relationshipName) { // Bidirectional relationship injectedFieldInTo = relationship2.relationshipName; if (relationship2.otherEntityField !== undefined && relationship2.otherEntityField !== 'id') { injectedFieldInTo += `(${relationship2.otherEntityField})`; } if (relationship2.relationshipValidateRules) { isInjectedFieldInToRequired = true; } commentInTo = relationship2.javadoc; break; } } } if (relationship.relationshipType === 'one-to-one' && relationship.ownerSide === true) { type = RelationshipTypes.ONE_TO_ONE; } if (relationship.relationshipType === 'many-to-many' && relationship.ownerSide === true) { type = RelationshipTypes.MANY_TO_MANY; } if (relationship.relationshipType === 'many-to-one') { if (injectedFieldInTo) { // This is a bidirectional relationship so consider it as a OneToMany return new JDLRelationship({ from: jdl.entities[_.upperFirst(relationship.otherEntityName)], to: jdl.entities[entityName], type: RelationshipTypes.ONE_TO_MANY, injectedFieldInFrom: injectedFieldInTo, injectedFieldInTo: getInjectedFieldInFrom(relationship), isInjectedFieldInFromRequired: isInjectedFieldInToRequired, isInjectedFieldInToRequired: relationship.relationshipValidateRules, commentInFrom: commentInTo, commentInTo: relationship.javadoc }); } else { // Unidirectional ManyToOne type = RelationshipTypes.MANY_TO_ONE; } } if (type) { return new JDLRelationship({ from: jdl.entities[entityName], to: toJdlEntity, type: type, injectedFieldInFrom: getInjectedFieldInFrom(relationship), injectedFieldInTo: injectedFieldInTo, isInjectedFieldInFromRequired: relationship.relationshipValidateRules, isInjectedFieldInToRequired: isInjectedFieldInToRequired, commentInFrom: relationship.javadoc, commentInTo: commentInTo }); } } function addEntityOptionsToJDL(jdl, entity, entityName) { if (entity.fluentMethods === false) { jdl.addOption( new JDLUnaryOption( { name: UnaryOptions.NO_FLUENT_METHOD, entityNames: [entityName] } ) ); } for (let option of [BinaryOptions.DTO, BinaryOptions.PAGINATION, BinaryOptions.SERVICE, BinaryOptions.SEARCH_ENGINE]) { if (entity[option] && entity[option] !== 'no') { jdl.addOption( new JDLBinaryOption( { name: option, value: entity[option], entityNames: [entityName] } ) ); } } // angularSuffix in BINARY_OPTIONS, angularJSSuffix in Json if (entity.angularJSSuffix !== undefined) { jdl.addOption( new JDLBinaryOption( { name: BinaryOptions.ANGULAR_SUFFIX, value: entity.angularJSSuffix, entityNames: [entityName] } ) ); } // microservice in BINARY_OPTIONS, microserviceName in Json if (entity.microserviceName !== undefined) { jdl.addOption( new JDLBinaryOption( { name: BinaryOptions.MICROSERVICE, value: entity.microserviceName, entityNames: [entityName] } ) ); } } function hasSkipUserManagement(jdl) { return jdl.options.filter(o => o.name == UnaryOptions.SKIP_USER_MANAGEMENT).length > 0; }