Commit 170fee85 authored by Bertrand PINEL's avatar Bertrand PINEL
Browse files

Rough processing of JDL files

parent d31bd2ae
/* eslint-env node */
const fs = require('fs-extra');
const jhipster = require('jhipster-core');
const inflection = require('inflection');
const shelljs = require('shelljs');
module.exports = {
name: 'jdlimporter',
......@@ -25,49 +27,82 @@ module.exports = {
var jdlModel = jhipster.parseFromFiles(files);
//this.ui.writeLine(JSON.stringify(jdlModel));
entities = jdlModel.entities;
let entities = jdlModel.entities;
let relationships = jdlModel.relationships;
for (var i=0; i<entities.length;i++) {
// Add an array field to further hold relationships
entities[i].relationships = [];
let entity = entities[i];
// Add an array field to further hold options to pass to entity-factory blueprint
entity.options = [];
for (var j=0; j<entity.body.length;j++) {
let property = entity.body[j];
switch (property.type) {
case 'String':
case 'TextBlob':
entity.options.push(property.name+':string');
break;
case 'LocalDate':
case 'Instant':
entity.options.push(property.name+':date');
break;
case 'Integer':
case 'Float':
case 'Double':
entity.options.push(property.name+':number');
break;
default:
this.ui.writeLine("Huston, we have a problem with unknow type "+property.name);
break;
}
}
// Check if current object is involved in a relationship
// First see the 'from' relationships
relationships.filter(relation => relation.from.name == entity.name).forEach(relation => {
let toObjectName = relation.to.name.toLowerCase();
switch (relation.cardinality) {
case "one-to-one":
case "many-to-one":
entity.options.push(toObjectName+':belongs-to:'+toObjectName);
break;
case "one-to-many":
case "many-to-many":
entity.options.push(inflection.pluralize(toObjectName)+':has-many:'+toObjectName);
break;
default:
this.ui.writeLine("Huston, we have a problem with unknow relationship type "+relation.cardinality);
break;
}
});
// Do the same for to relationships
relationships.filter(relation => relation.to.name == entity.name).forEach(relation => {
let toObjectName = relation.from.name.toLowerCase();
switch (relation.cardinality) {
case "one-to-one":
case "one-to-many":
entity.options.push(toObjectName+':has-many:'+toObjectName);
break;
case "many-to-one":
case "many-to-many":
entity.options.push(inflection.pluralize(toObjectName)+':has-many:'+toObjectName);
break;
default:
this.ui.writeLine("Huston, we have a problem with unknow relationship type "+relation.cardinality);
break;
}
});
console.log('ember g entity-factory ' + entity.name.toLowerCase() + ' ' + entity.options.join(" "));
}
let relationships = jdlModel.relationships;
this.ui.writeLine("many-to-one");
relationships.filter(relation => /many-to-one/.test(relation.cardinality)).forEach(relation => {
// DS.belongsTo + DS.hasMany
this.ui.writeLine("\t"+JSON.stringify(relation));
let fromObject = relation.from;
let toObject = relation.to;
entities.filter(entity => fromObject.name === entity.name).forEach(entity => {
//BelongsTo
let rel = {type: "DS.belongsTo", injectedField: fromObject.injectedField, required: fromObject.required};
entity.relationships.push(rel);
});
entities.filter(entity => toObject.name === entity.name).forEach(entity => {
//hasMany
let rel = {type: "DS.hasMany"};
entity.relationships.push(rel);
});
});
this.ui.writeLine("one-to-many");
relationships.filter(relation => /one-to-many/.test(relation.cardinality)).forEach(relation => {
// DS.hasMany + DS.belongsTo
this.ui.writeLine("\t"+JSON.stringify(relation));
});
this.ui.writeLine("many-to-many");
relationships.filter(relation => /many-to-many/.test(relation.cardinality)).forEach(relation => {
// DS.hasMany
this.ui.writeLine("\t"+JSON.stringify(relation));
});
this.ui.writeLine("one-to-one");
relationships.filter(relation => /one-to-one/.test(relation.cardinality)).forEach(relation => {
// DS.belongsTo
this.ui.writeLine("\t"+JSON.stringify(relation));
});
},
locals: function(options) {
const templateAws = fs.readFileSync('./template.json', 'utf-8'); //require('json:./template.json').default(options.target);
const usingProxy = "const usingProxy = () => {\n var usingProxyArg = !!process.argv.filter(function(arg) {\n return arg.indexOf('--proxy') === 0 || arg.indexOf('-pr') === 0 || arg.indexOf('-pxy') === 0;\n }).length;\n\n var hasGeneratedProxies = false;\n var proxiesDir = process.env.PWD + '/server/proxies';\n try {\n fs.lstatSync(proxiesDir);\n hasGeneratedProxies = true;\n } catch (e) {}\n\n return usingProxyArg || hasGeneratedProxies;\n}";
},
afterInstall(options) {
......@@ -78,147 +113,3 @@ module.exports = {
}
};
const getAllMapping = (conf) => {
const json = {
"TableName": conf.pluralName
}
return JSON.stringify(json)
}
const getMapping = (conf) => {
const json = {
"TableName": conf.pluralName,
"KeyConditionExpression": "id = :v1",
"ExpressionAttributeValues": {
":v1": {
"N": "$input.params('id')"
}
}
}
return JSON.stringify(json)
}
const postMapping = (conf) => {
const json = {
"TableName": conf.pluralName,
"Item": {
"id": {
"N": "$input.path('$.id')"
}
}
}
Object.keys(conf.options).forEach(attr => {
const inputPath = "$input.path('$." + attr + "')"
let type
if (conf.options[attr] === "string") {
type = {
"S": inputPath
}
} else if (conf.options[attr] === "number") {
type = {
"N": inputPath
}
}
json.Item[attr] = type
})
return JSON.stringify(json)
}
const configureTemplate = (confs, template) => {
if (!confs.length > 0) return template
let ret = JSON.parse(JSON.stringify(template))
const resources = ret.Resources
resources.Api.Properties.Name = 'emberBackendEntity'
confs.forEach(conf => {
resources.Resource.Properties.PathPart = conf.pluralName;
resources.myDynamoDBTable.Properties.TableName = conf.pluralName;
resources.AllTemplateRequestGET.Properties.Integration.RequestTemplates["application/json"] = getAllMapping(conf);
resources.AllTemplateRequestGET.Properties.ResourceId.Ref = conf.name + "AllResource";
resources.TemplateRequestGET.Properties.Integration.RequestTemplates["application/json"] = getMapping(conf);
resources.TemplateRequestGET.Properties.ResourceId.Ref = conf.name + "Resource";
resources.TemplateRequestPOST.Properties.Integration.RequestTemplates["application/json"] = postMapping(conf);
resources.TemplateRequestPOST.Properties.ResourceId.Ref = conf.name + "Resource";
resources.AllResource.DependsOn = conf.name + "Resource";
resources.AllResource.Properties.ParentId.Ref = conf.name + "Resource";
resources[conf.name + "AllResource"] = JSON.parse(JSON.stringify(resources.AllResource));
resources[conf.name + "Resource"] = JSON.parse(JSON.stringify(resources.Resource));
resources[conf.name + "DynamoDBTable"] = JSON.parse(JSON.stringify(resources.myDynamoDBTable));
resources["All" + conf.name + "RequestGET"] = JSON.parse(JSON.stringify(resources.AllTemplateRequestGET));
resources[conf.name + "RequestGET"] = JSON.parse(JSON.stringify(resources.TemplateRequestGET));
resources[conf.name + "RequestPOST"] = JSON.parse(JSON.stringify(resources.TemplateRequestPOST));
resources.ApiDeployment.DependsOn = [
"All" + conf.name + "RequestGET",
conf.name + "RequestGET",
conf.name + "RequestPOST"
];
})
delete resources.Resource
delete resources.AllResource
delete resources.myDynamoDBTable
delete resources.TemplateRequestGET
delete resources.TemplateRequestPOST
delete resources.AllTemplateRequestGET
return ret
}
//const generateTemplate = (url) => {
const generateTemplate = (data) => {
//const data = require('../../helpers/JdlJSONParser').default(url + '/.jdl')
let entities = data.entities
const template = require('./template.json')
const rel = data.relations.map(rel => {
if (/one-to-many/.test(rel.relationshipType)) {
return [{
entity: rel.otherEntityName,
otherEntityName: rel.entityName,
relationName: stringUtils.camelize('belongs-to-' + rel.relationshipName + '-' + rel.otherEntityName)
}]
}
if (/many-to-one/.test(rel.relationshipType)) {
return [{
entity: rel.entityName,
otherEntityName: rel.otherEntityName,
relationName: stringUtils.camelize('belongs-to-' + rel.relationshipName + '-' + rel.entityName)
}]
}
if (/one-to-one/.test(rel.relationshipType)) {
return [{
entity: rel.entityName,
otherEntityName: rel.otherEntityName,
relationName: stringUtils.camelize('belongs-to-' + rel.relationshipName + '-' + rel.entityName)
},
{
entity: rel.otherEntityName,
otherEntityName: rel.entityName,
relationName: stringUtils.camelize('belongs-to-' + rel.relationshipName + '-' + rel.otherEntityName)
}
]
}
}).reduce((acc, el) => acc.concat(el), [])
rel.forEach(relation => {
let index = entities.findIndex(ent => relation.entity.localeCompare(ent.name) == 0)
if ((index > 0)) {
entities[index].options[relation.relationName] = 'number'
}
})
const generatedFile = configureTemplate(entities, template)
return {
template: generatedFile,
entities
}
}
{
"name": "ember-aws-ehipster",
"version": "0.1.4",
"version": "0.2.0",
"description": "The default blueprint for ember-cli addons.",
"keywords": [
"ember-addon"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment