forked from jhipster/generator-jhipster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
546 lines (514 loc) · 22.8 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
'use strict';
var util = require('util'),
fs = require('fs'),
path = require('path'),
yeoman = require('yeoman-generator'),
chalk = require('chalk'),
_s = require('underscore.string'),
shelljs = require('shelljs'),
scriptBase = require('../script-base');
var EntityGenerator = module.exports = function EntityGenerator(args, options, config) {
yeoman.generators.NamedBase.apply(this, arguments);
this.useConfigurationFile =false;
if (shelljs.test('-f', '.jhipster.' + this.name + '.json')) {
console.log(chalk.green('Found the .jhipster.' + this.name + '.json configuration file, automatically generating the entity'));
try {
this.fileData = JSON.parse(this.readFileAsString('.jhipster.' + this.name + '.json'))
} catch (err) {
console.log(chalk.red('The configuration file could not be read!'));
return;
}
this.useConfigurationFile = true;
}
this.filenameInheritance = '.jhipster.inheritance.table.json';
if (shelljs.test('-f', this.filenameInheritance)) {
console.log(chalk.green('Found the '+ this.filenameInheritance +' configuration file'));
try {
this.inheritances = JSON.parse(this.readFileAsString(this.filenameInheritance))
} catch (err) {
console.log(chalk.red('The configuration file could not be read!'));
return;
}
} else {
this.inheritances = [];
}
console.log(chalk.red('The entity ' + this.name + ' is being created.'));
this.env.options.appPath = this.config.get('appPath') || 'src/main/webapp';
this.baseName = this.config.get('baseName');
this.packageName = this.config.get('packageName');
this.packageFolder = this.config.get('packageFolder');
this.javaVersion = this.config.get('javaVersion');
this.hibernateCache = this.config.get('hibernateCache');
this.databaseType = this.config.get('databaseType');
this.angularAppName = _s.camelize(_s.slugify(this.baseName)) + 'App';
// Specific Entity sub-generator variables
this.inheritanceFor = false;
this.inheritanceFromClass = '';
this.fieldId = 0;
this.fields = [];
this.fieldsContainLocalDate = false;
this.fieldsContainDateTime = false;
this.fieldsContainCustomTime = false;
this.fieldsContainBigDecimal = false;
this.fieldsContainOwnerManyToMany = false;
this.fieldsContainOneToMany = false;
this.relationshipId = 0;
this.relationships = [];
};
var fieldNamesUnderscored = ['id'];
util.inherits(EntityGenerator, yeoman.generators.Base);
util.inherits(EntityGenerator, scriptBase);
function removefile(file) {
console.log('Remove the file - ' + file)
if (shelljs.test('-f', file)) {
shelljs.rm(file);
}
}
/**
* ask if this entity serve inheritance for others
*/
EntityGenerator.prototype.askForInheritanceFor = function askForInheritanceFor() {
if (this.useConfigurationFile == true) {// don't prompt if data are imported from a file
return;
}
var cb = this.async();
console.log(chalk.green('Generating inheritance annotations - only Single Table Strategy#'));
var prompts = [
{
type: 'confirm',
name: 'inheritanceForAdd',
message: 'Would like your entity be a base class inherited by other entities?',
default: false
}
];
this.prompt(prompts, function (props) {
if (props.inheritanceForAdd) {
this.inheritanceFor = true;
}
console.log(chalk.red('===========' + _s.capitalize(this.name) + '=============='));
if (props.inheritanceForAdd) {
this.askForFields();
} else {
cb();
}
}.bind(this));
}
EntityGenerator.prototype.askForInheritanceFrom = function askForInheritanceFrom() {
if (this.useConfigurationFile == true || this.inheritanceFor == true) {// don't prompt if data are imported from a file
return;
}
var cb = this.async();
console.log(chalk.green('Generating inheritance #'));
var choicesArr = this.inheritances;
var prompts = [
{
type: 'confirm',
name: 'inheritanceFromAdd',
message: 'Would like your entity to inherit from an existing entity?',
default: false
},
{
when: function (response) {
return response.inheritanceFromAdd == true;
},
type: 'list',
name: 'inheritanceFromClass',
message: 'Which class would you like to extend?',
choices: choicesArr,
default: 0
}
];
this.prompt(prompts, function (props) {
if (props.inheritanceFromAdd) {
this.inheritanceFromClass = props.inheritanceFromClass;
}
console.log(chalk.red('===========' + _s.capitalize(this.name) + '=============='));
if (props.inheritanceFromAdd) {
this.askForFields();
} else {
cb();
}
}.bind(this));
}
EntityGenerator.prototype.askForFields = function askForFields() {
if (this.useConfigurationFile == true) {// don't prompt if data are imported from a file
return;
}
var cb = this.async();
this.fieldId++;
console.log(chalk.green('Generating field #' + this.fieldId));
var prompts = [
{
type: 'confirm',
name: 'fieldAdd',
message: 'Do you want to add a field to your entity?',
default: true
},
{
when: function (response) {
return response.fieldAdd == true;
},
type: 'input',
name: 'fieldName',
validate: function (input) {
if ((/^([a-zA-Z0-9_]*)$/.test(input)) && input != '' && input != 'id' && fieldNamesUnderscored.indexOf(_s.underscored(input)) == -1) return true;
return 'Your field name cannot contain special characters or use an already existing field name';
},
message: 'What is the name of your field?'
},
{
when: function (response) {
return response.fieldAdd == true;
},
type: 'list',
name: 'fieldType',
message: 'What is the type of your field?',
choices: [
{
value: 'String',
name: 'String'
},
{
value: 'Integer',
name: 'Integer'
},
{
value: 'Long',
name: 'Long'
},
{
value: 'BigDecimal',
name: 'BigDecimal'
},
{
value: 'LocalDate',
name: 'LocalDate'
},
{
value: 'DateTime',
name: 'DateTime'
},
{
value: 'Boolean',
name: 'Boolean'
}
],
default: 0
}
];
this.prompt(prompts, function (props) {
if (props.fieldAdd) {
var field = {fieldId: this.fieldId,
fieldName: props.fieldName,
fieldType: props.fieldType,
fieldNameCapitalized: _s.capitalize(props.fieldName),
fieldNameUnderscored: _s.underscored(props.fieldName)}
fieldNamesUnderscored.push(_s.underscored(props.fieldName));
this.fields.push(field);
if (props.fieldType == 'LocalDate') {
this.fieldsContainLocalDate = true;
this.fieldsContainCustomTime = true;
}
if (props.fieldType == 'BigDecimal') {
this.fieldsContainBigDecimal = true;
}
if (props.fieldType == 'DateTime') {
this.fieldsContainDateTime = true;
this.fieldsContainCustomTime = true;
}
}
console.log(chalk.red('===========' + _s.capitalize(this.name) + '=============='));
for (var id in this.fields) {
console.log(chalk.red(this.fields[id].fieldName + ' (' + this.fields[id].fieldType + ')'));
}
if (props.fieldAdd) {
this.askForFields();
} else {
cb();
}
}.bind(this));
};
EntityGenerator.prototype.askForRelationships = function askForRelationships() {
if (this.useConfigurationFile == true) {// don't prompt if data are imported from a file
return;
}
if (this.databaseType == 'mongodb') {
return;
}
var packageFolder = this.packageFolder;
var cb = this.async();
this.relationshipId++;
console.log(chalk.green('Generating relationships with other entities'));
var prompts = [
{
type: 'confirm',
name: 'relationshipAdd',
message: 'Do you want to add a relationship to another entity?',
default: true
},
{
when: function (response) {
return response.relationshipAdd == true;
},
type: 'input',
name: 'otherEntityName',
validate: function (input) {
if ((/^([a-zA-Z0-9_]*)$/.test(input)) && input != '' && input != 'id' && fieldNamesUnderscored.indexOf(_s.underscored(input)) == -1) return true;
return 'Your relationship name cannot contain special characters or use an already existing field name';
},
message: 'What is the name of the other entity?'
},
{
when: function (response) {
return response.relationshipAdd == true;
},
type: 'list',
name: 'relationshipType',
message: 'What is the type of the relationship?',
choices: [
{
value: 'one-to-many',
name: 'one-to-many'
},
{
value: 'many-to-one',
name: 'many-to-one'
},
{
value: 'many-to-many',
name: 'many-to-many'
},
{
value: 'one-to-one',
name: 'one-to-one'
}
],
default: 0
},
{
when: function(response) {
return (response.relationshipAdd == true && response.relationshipType == 'many-to-one' && !shelljs.test('-f', 'src/main/java/' + packageFolder + '/domain/' + _s.capitalize(response.otherEntityName) + '.java'))
},
type: 'confirm',
name: 'noOtherEntity',
message: 'WARNING! You are trying to generate a many-to-one relationship on an entity that does not exist. This will probably fail, as you will need to create a foreign key on a table that does not exist. We advise you to create the other side of this relationship first (do the one-to-many before the many-to-one relationship). Are you sure you want to continue?',
default: false
},
{
when: function (response) {
return (response.relationshipAdd == true && (response.relationshipType == 'many-to-many' || response.relationshipType == 'one-to-one'));
},
type: 'confirm',
name: 'ownerSide',
message: 'Is this entity the owner of the relationship?',
default: false
},
{
when: function(response) {
return (response.relationshipAdd == true && response.ownerSide == true && !shelljs.test('-f', 'src/main/java/' + packageFolder + '/domain/' + _s.capitalize(response.otherEntityName) + '.java'))
},
type: 'confirm',
name: 'noOtherEntity2',
message: 'WARNING! You have selected that this entity is the owner of a relationship on another entity, that does not exist yet. This will probably fail, as you will need to create a foreign key on a table that does not exist. We advise you to create the other side of this relationship first (do the non-owning side before the owning side). Are you sure you want to continue?',
default: false
},
{
when: function (response) {
return (!(response.noOtherEntity == false || response.noOtherEntity2 == false) && response.relationshipAdd == true && (response.relationshipType == 'many-to-one' || (response.relationshipType == 'many-to-many' && response.ownerSide == true)));
},
type: 'input',
name: 'otherEntityField',
message: function (response) {
return 'When you display this relationship with AngularJS, which field from \'' + response.otherEntityName + '\' do you want to use?'
},
default: 'id'
}
];
this.prompt(prompts, function (props) {
if (props.noOtherEntity == false || props.noOtherEntity2 == false) {
console.log(chalk.red('Generation aborted, as requested by the user.'));
return;
}
if (props.relationshipAdd) {
var relationship = {relationshipId: this.relationshipId,
otherEntityName: props.otherEntityName.charAt(0).toLowerCase() + props.otherEntityName.slice(1),
relationshipType: props.relationshipType,
otherEntityNameCapitalized: _s.capitalize(props.otherEntityName),
otherEntityField: props.otherEntityField,
ownerSide: props.ownerSide}
if (props.relationshipType == 'many-to-many' && props.ownerSide == true) {
this.fieldsContainOwnerManyToMany = true;
}
if (props.relationshipType == 'one-to-many') {
this.fieldsContainOneToMany = true;
}
fieldNamesUnderscored.push(_s.underscored(props.otherEntityName));
this.relationships.push(relationship);
}
console.log(chalk.red('===========' + _s.capitalize(this.name) + '=============='));
for (var id in this.fields) {
console.log(chalk.red(this.fields[id].fieldName + ' (' + this.fields[id].fieldType + ')'));
}
console.log(chalk.red('-------------------'));
for (var id in this.relationships) {
console.log(chalk.red(this.relationships[id].otherEntityName + ' (' + this.relationships[id].relationshipType + ')'));
}
if (props.relationshipAdd) {
this.askForRelationships();
} else {
console.log(chalk.green('Everything is configured, generating the entity...'));
cb();
}
}.bind(this));
};
EntityGenerator.prototype.searchBaseTable = function searchBaseTable(entity, obj) {
var baseEntityData = '';
var filename = '.jhipster.' + entity + '.json';
try {
baseEntityData = JSON.parse(obj.readFileAsString(filename));
} catch (err) {
return;
}
var baseEntity = baseEntityData.inheritanceFromClass;
if ((baseEntity != '' || baseEntity != 'undefined') && baseEntityData.inheritanceFor != true){
return searchBaseTable(baseEntity, obj);
} else {
return entity;
}
};
EntityGenerator.prototype.getInheritedFields = function getInheritedFields(entity, fields, obj) {
var baseEntityData = '';
var filename = '.jhipster.' + entity + '.json';
try {
baseEntityData = JSON.parse(obj.readFileAsString(filename));
} catch (err) {
return;
}
var baseEntity = baseEntityData.inheritanceFromClass;
fields = fields.concat(baseEntityData.fields);
if ((baseEntity != '' || baseEntity != 'undefined') && baseEntityData.inheritanceFor != true){
return getInheritedFields(baseEntity, fields, obj);
} else {
return fields;
}
};
EntityGenerator.prototype.files = function files() {
if (this.databaseType == "sql") {
this.changelogDate = this.dateFormatForLiquibase();
}
this.name = _s.capitalize(this.name);
if (this.useConfigurationFile == false) { // store informations in a file for further use.
this.data = {};
this.data.inheritanceFor = this.inheritanceFor;
this.data.inheritanceFromClass = this.inheritanceFromClass;
this.data.relationships = this.relationships;
this.data.fields = this.fields;
this.data.fieldNamesUnderscored = this.fieldNamesUnderscored;
this.data.fieldsContainOwnerManyToMany = this.fieldsContainOwnerManyToMany;
this.data.fieldsContainOneToMany = this.fieldsContainOneToMany;
this.data.fieldsContainLocalDate = this.fieldsContainLocalDate;
this.data.fieldsContainCustomTime = this.fieldsContainCustomTime;
this.data.fieldsContainBigDecimal = this.fieldsContainBigDecimal;
this.data.fieldsContainDateTime = this.fieldsContainDateTime;
this.data.changelogDate = this.changelogDate;
this.filename = '.jhipster.' + this.name + '.json';
this.write(this.filename, JSON.stringify(this.data, null, 4));
} else {
this.inheritanceFor = this.fileData.inheritanceFor;
this.inheritanceFromClass = this.fileData.ingeritanceFromClass;
this.relationships = this.fileData.relationships;
this.fields = this.fileData.fields;
this.fieldNamesUnderscored = this.fileData.fieldNamesUnderscored;
this.fieldsContainOwnerManyToMany = this.fileData.fieldsContainOwnerManyToMany;
this.fieldsContainOneToMany = this.fileData.fieldsContainOneToMany;
this.fieldsContainLocalDate = this.fileData.fieldsContainLocalDate;
this.fieldsContainCustomTime = this.fileData.fieldsContainCustomTime;
this.fieldsContainBigDecimal = this.fileData.fieldsContainBigDecimal;
this.fieldsContainDateTime = this.fileData.fieldsContainDateTime;
this.changelogDate = this.fileData.changelogDate;
}
if(this.inheritanceFor == true || this.inheritanceFromClass != '') {
// first read data from file - only update
// add to root
if (this.inheritanceFor) {
this.inheritances.push(this.name);
}
// search root and add to its
if(this.inheritanceFromClass != '') {
// skip AbstractAuditingEntity - its mappedSuperclass
// For further use
//this.inheritances[this.inheritanceFromClass].push(this.name);
this.inheritances.push(this.name);
}
removefile(this.filenameInheritance);
this.write(this.filenameInheritance, JSON.stringify(this.inheritances, null, 4));
}
this.entityClass = _s.capitalize(this.name);
this.entityInstance = this.name.charAt(0).toLowerCase() + this.name.slice(1);
var resourceDir = 'src/main/resources/';
this.template('src/main/java/package/domain/_Entity.java',
'src/main/java/' + this.packageFolder + '/domain/' + this.entityClass + '.java', this, {});
this.template('src/main/java/package/repository/_EntityRepository.java',
'src/main/java/' + this.packageFolder + '/repository/' + this.entityClass + 'Repository.java', this, {});
this.template('src/main/java/package/web/rest/_EntityResource.java',
'src/main/java/' + this.packageFolder + '/web/rest/' + this.entityClass + 'Resource.java', this, {});
if (this.databaseType == "sql") {
if(this.inheritanceFromClass != '') {
this.baseTable = this.searchBaseTable(this.inheritanceFromClass, this);
this.template(resourceDir + '/config/liquibase/changelog/_added_inherited_entity.xml',
resourceDir + 'config/liquibase/changelog/' + this.changelogDate + '_added_inherited_entity_' + this.entityClass + '.xml', this, {});
this.addChangelogToLiquibase(this.changelogDate + '_added_inherited_entity_' + this.entityClass);
} else {
this.template(resourceDir + '/config/liquibase/changelog/_added_entity.xml',
resourceDir + 'config/liquibase/changelog/' + this.changelogDate + '_added_entity_' + this.entityClass + '.xml', this, {});
this.addChangelogToLiquibase(this.changelogDate + '_added_entity_' + this.entityClass);
}
}
// after generating Liquibase we can merge fields from all inherited classes
this.fields = this.getInheritedFields(this.inheritanceFromClass, this.fields, this);
this.template('src/main/webapp/app/_entities.html',
'src/main/webapp/scripts/app/entities/' + this.entityInstance + '/' + this.entityInstance + 's.html', this, {});
this.template('src/main/webapp/app/_entity-detail.html',
'src/main/webapp/scripts/app/entities/' + this.entityInstance + '/' + this.entityInstance + '-detail.html', this, {});
this.addRouterToMenu(this.entityInstance);
this.template('src/main/webapp/app/_entity.js',
'src/main/webapp/scripts/app/entities/' + this.entityInstance + '/' + this.entityInstance + '.js', this, {});
this.addAppScriptToIndex(this.entityInstance + '/' + this.entityInstance + '.js');
this.template('src/main/webapp/app/_entity-controller.js',
'src/main/webapp/scripts/app/entities/' + this.entityInstance + '/' + this.entityInstance + '.controller' + '.js', this, {});
this.addAppScriptToIndex(this.entityInstance + '/' + this.entityInstance + '.controller' + '.js');
this.template('src/main/webapp/app/_entity-detail-controller.js',
'src/main/webapp/scripts/app/entities/' + this.entityInstance + '/' + this.entityInstance + '-detail.controller' + '.js', this, {});
this.addAppScriptToIndex(this.entityInstance + '/' + this.entityInstance + '-detail.controller' + '.js');
this.template('src/main/webapp/components/_entity-service.js',
'src/main/webapp/scripts/components/entities/' + this.entityInstance + '/' + this.entityInstance + '.service' + '.js', this, {});
this.addComponentsScriptToIndex(this.entityInstance + '/' + this.entityInstance + '.service' + '.js');
this.template('src/test/java/package/web/rest/_EntityResourceTest.java',
'src/test/java/' + this.packageFolder + '/web/rest/' + this.entityClass + 'ResourceTest.java', this, {});
// Copy for each
this.copyI18n('ca');
this.copyI18n('da');
this.copyI18n('de');
this.copyI18n('en');
this.copyI18n('es');
this.copyI18n('fr');
this.copyI18n('kr');
this.copyI18n('pl');
this.copyI18n('pt-br');
this.copyI18n('ru');
this.copyI18n('sw');
this.copyI18n('tr');
this.copyI18n('zh-tw');
};
EntityGenerator.prototype.copyI18n = function(language) {
try {
var stats = fs.lstatSync('src/main/webapp/i18n/' + language);
if (stats.isDirectory()) {
this.template('src/main/webapp/i18n/_entity_' + language + '.json', 'src/main/webapp/i18n/' + language + '/' + this.entityInstance + '.json', this, {});
this.addNewEntityToMenu(language, this.entityInstance, this.entityClass);
}
} catch(e) {
// An exception is thrown if the folder doesn't exist
// do nothing
}
};