Skip to content

Commit

Permalink
Merge pull request #24949 from mshima/optional-layers
Browse files Browse the repository at this point in the history
make layers optional
  • Loading branch information
DanielFran authored Jan 25, 2024
2 parents 22c613e + 84b7a90 commit 3bfac62
Show file tree
Hide file tree
Showing 20 changed files with 405 additions and 172 deletions.
2 changes: 1 addition & 1 deletion generators/app/generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe(`generator - ${generator}`, () => {
describe('default config', () => {
let runResult;
before(async () => {
runResult = await helpers.run(generatorPath).withJHipsterConfig().withSkipWritingPriorities();
runResult = await helpers.run(generatorPath).withJHipsterConfig().withSkipWritingPriorities().withMockedSource();
});

it('should match snapshot', () => {
Expand Down
160 changes: 81 additions & 79 deletions generators/base-application/types/entity.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* limitations under the License.
*/

import { SpringEntity } from '../../server/types.js';
import Field from './field.js';
import Relationship from './relationship.js';

Expand All @@ -35,84 +36,85 @@ export type BaseEntity = {
skipServer?: boolean;
};

type Entity = Required<BaseEntity> & {
builtIn?: boolean;
builtInUser?: boolean;
builtInAuthority?: boolean;
microserviceName?: string;

entityNameCapitalized: string;
entityClass: string;
entityInstance: string;
entityTableName: string;
entityNamePlural: string;

dtoClass?: string;
dtoInstance?: string;

persistClass: string;
persistInstance: string;
restClass: string;
restInstance: string;

entityNamePluralizedAndSpinalCased: string;
entityClassPlural: string;
entityInstancePlural: string;

entityI18nVariant: string;
entityClassHumanized: string;
entityClassPluralHumanized: string;

entityFileName: string;
entityFolderName: string;
entityModelFileName: string;
entityParentPathAddition: string;
entityPluralFileName: string;
entityServiceFileName: string;

entityAngularName: string;
entityAngularNamePlural: string;
entityReactName: string;

entityApiUrl: string;
entityStateName: string;
entityUrl: string;

entityTranslationKey: string;
entityTranslationKeyMenu: string;

i18nKeyPrefix: string;
i18nAlertHeaderPrefix: string;

entityApi: string;
entityPage: string;

anyFieldIsBigDecimal: boolean;
/**
* Any file is of type Bytes or ByteBuffer
*/
anyFieldIsBlobDerived: boolean;
/**
* Any field is of type ZonedDateTime, Instant or LocalDate
*/
anyFieldIsDateDerived: boolean;
anyFieldIsDuration: boolean;
anyFieldIsInstant: boolean;
anyFieldIsLocalDate: boolean;
/**
* Any field is of type ZonedDateTime or Instant
*/
anyFieldIsTimeDerived: boolean;
anyFieldIsUUID: boolean;
anyFieldIsZonedDateTime: boolean;

anyFieldHasDocumentation: boolean;
anyFieldHasImageContentType: boolean;
anyFieldHasTextContentType: boolean;
/**
* Any field has image or any contentType
*/
anyFieldHasFileBasedContentType: boolean;
};
type Entity = Required<BaseEntity> &
SpringEntity & {
builtIn?: boolean;
builtInUser?: boolean;
builtInAuthority?: boolean;
microserviceName?: string;

entityNameCapitalized: string;
entityClass: string;
entityInstance: string;
entityTableName: string;
entityNamePlural: string;

dtoClass?: string;
dtoInstance?: string;

persistClass: string;
persistInstance: string;
restClass: string;
restInstance: string;

entityNamePluralizedAndSpinalCased: string;
entityClassPlural: string;
entityInstancePlural: string;

entityI18nVariant: string;
entityClassHumanized: string;
entityClassPluralHumanized: string;

entityFileName: string;
entityFolderName: string;
entityModelFileName: string;
entityParentPathAddition: string;
entityPluralFileName: string;
entityServiceFileName: string;

entityAngularName: string;
entityAngularNamePlural: string;
entityReactName: string;

entityApiUrl: string;
entityStateName: string;
entityUrl: string;

entityTranslationKey: string;
entityTranslationKeyMenu: string;

i18nKeyPrefix: string;
i18nAlertHeaderPrefix: string;

entityApi: string;
entityPage: string;

anyFieldIsBigDecimal: boolean;
/**
* Any file is of type Bytes or ByteBuffer
*/
anyFieldIsBlobDerived: boolean;
/**
* Any field is of type ZonedDateTime, Instant or LocalDate
*/
anyFieldIsDateDerived: boolean;
anyFieldIsDuration: boolean;
anyFieldIsInstant: boolean;
anyFieldIsLocalDate: boolean;
/**
* Any field is of type ZonedDateTime or Instant
*/
anyFieldIsTimeDerived: boolean;
anyFieldIsUUID: boolean;
anyFieldIsZonedDateTime: boolean;

anyFieldHasDocumentation: boolean;
anyFieldHasImageContentType: boolean;
anyFieldHasTextContentType: boolean;
/**
* Any field has image or any contentType
*/
anyFieldHasFileBasedContentType: boolean;
};

export default Entity;
16 changes: 14 additions & 2 deletions generators/bootstrap-application-base/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ export function createUserEntity(customUserData = {}, application) {
dto: true,
adminUserDto: `AdminUser${application.dtoSuffix ?? ''}`,
builtInUser: true,
skipClient: application.clientFrameworkReact || application.clientFrameworkVue,
skipDbChangelog: true,
entityDomainLayer: false,
entityPersistenceLayer: false,
entityRestLayer: false,
entitySearchLayer: false,
hasImageField: !application.databaseTypeNo && !application.databaseTypeCassandra,
...customUserData,
};
Expand Down Expand Up @@ -119,8 +125,14 @@ export function createAuthorityEntity(customAuthorityData = {}, application) {
relationships: [],
fields: entityDefinition ? entityDefinition.fields || [] : [],
builtInAuthority: true,
skipClient: application.clientFrameworkReact || application.clientFrameworkVue,
skipClient: true,
searchEngine: 'no',
service: 'no',
dto: 'no',
skipDbChangelog: true,
entityDomainLayer: false,
entityPersistenceLayer: false,
entityRestLayer: false,
...customAuthorityData,
};

Expand All @@ -133,7 +145,7 @@ export function createAuthorityEntity(customAuthorityData = {}, application) {
fieldName: 'name',
fieldType: TYPE_STRING,
id: true,
fieldValidateRules: [Validations.MAXLENGTH],
fieldValidateRules: [Validations.MAXLENGTH, Validations.REQUIRED],
fieldValidateRulesMaxlength: 50,
builtIn: true,
},
Expand Down
6 changes: 6 additions & 0 deletions generators/bootstrap-application/generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ describe(`generator - ${generator}`, () => {
"entityClassPlural": "Users",
"entityClassPluralHumanized": "Users",
"entityContainsCollectionField": false,
"entityDomainLayer": false,
"entityFileName": "user",
"entityFolderName": "user",
"entityI18nVariant": "default",
Expand All @@ -242,8 +243,11 @@ describe(`generator - ${generator}`, () => {
"entityPackage": undefined,
"entityPage": "user",
"entityParentPathAddition": "",
"entityPersistenceLayer": false,
"entityPluralFileName": "usersundefined",
"entityReactName": "User",
"entityRestLayer": false,
"entitySearchLayer": false,
"entityServiceFileName": "user",
"entityStateName": "user",
"entitySuffix": "",
Expand Down Expand Up @@ -675,6 +679,8 @@ describe(`generator - ${generator}`, () => {
"service": "no",
"serviceImpl": false,
"serviceNo": true,
"skipClient": false,
"skipDbChangelog": true,
"skipUiGrouping": false,
"springDataDescription": "Spring Data JPA",
"tsKeyType": "string",
Expand Down
6 changes: 4 additions & 2 deletions generators/java/entity-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,23 @@ import { javaMainPackageTemplatesBlock, javaTestPackageTemplatesBlock } from './
export const entityServerFiles: WriteFileSection = {
model: [
javaMainPackageTemplatesBlock({
condition: ctx => ctx.entityDomainLayer,
templates: ['_entityPackage_/domain/_persistClass_.java.jhi'],
}),
],
modelTestFiles: [
javaTestPackageTemplatesBlock({
condition: ctx => ctx.entityDomainLayer,
templates: ['_entityPackage_/domain/_persistClass_Test.java', '_entityPackage_/domain/_persistClass_TestSamples.java'],
}),
],
server: [
javaMainPackageTemplatesBlock({
condition: ctx => ctx.useJakartaValidation,
condition: ctx => ctx.useJakartaValidation && ctx.entityDomainLayer,
templates: ['_entityPackage_/domain/_persistClass_.java.jhi.jakarta_validation'],
}),
javaMainPackageTemplatesBlock({
condition: ctx => ctx.useJacksonIdentityInfo,
condition: ctx => ctx.useJacksonIdentityInfo && ctx.entityDomainLayer,
templates: ['_entityPackage_/domain/_persistClass_.java.jhi.jackson_identity_info'],
}),
],
Expand Down
17 changes: 16 additions & 1 deletion generators/java/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { JAVA_COMPATIBLE_VERSIONS } from '../generator-constants.js';
import { matchMainJavaFiles } from './support/package-info-transform.js';
import { entityServerFiles, enumFiles } from './entity-files.js';
import { getEnumInfo } from '../base-application/support/index.js';
import { mutateData } from '../base/support/index.js';

export type ApplicationDefinition = GenericApplicationDefinition<JavaApplication>;
export type GeneratorDefinition = BaseApplicationGeneratorDefinition<ApplicationDefinition & GenericSourceTypeDefinition>;
Expand Down Expand Up @@ -86,6 +87,20 @@ export default class JavaGenerator extends BaseApplicationGenerator<GeneratorDef
return this.asConfiguringTaskGroup(this.delegateTasksToBlueprint(() => this.configuring));
}

get preparingEachEntity() {
return this.asPreparingEachEntityTaskGroup({
prepareEntity({ entity }) {
mutateData(entity, {
entityDomainLayer: true,
});
},
});
}

get [BaseApplicationGenerator.PREPARING_EACH_ENTITY]() {
return this.delegateTasksToBlueprint(() => this.preparingEachEntity);
}

get default() {
return this.asDefaultTaskGroup({
generatedAnnotation({ application }) {
Expand Down Expand Up @@ -149,7 +164,7 @@ export default class JavaGenerator extends BaseApplicationGenerator<GeneratorDef
if (!this.generateEntities) return;

const { useJakartaValidation, useJacksonIdentityInfo } = this;
for (const entity of entities.filter(entity => !entity.skipServer && !entity.builtIn)) {
for (const entity of entities.filter(entity => !entity.skipServer)) {
await this.writeFiles({
sections: entityServerFiles,
context: { ...application, ...entity, useJakartaValidation, useJacksonIdentityInfo },
Expand Down
21 changes: 21 additions & 0 deletions generators/server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# server sub-generador

Server generator.

## Customizing

### Entities

#### Notable customizations

##### Layers

Every layer is generated for every Entity by default.
It's possible to disable layers.

```
@EntityDomainLayer(false)
@EntityPersistenceLayer(false)
@EntityRestLayer(false)
entity OptionalLayers {}
```
10 changes: 6 additions & 4 deletions generators/server/entity-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ const { SERVICE_CLASS, SERVICE_IMPL } = ServiceTypes;
export const restFiles = {
restFiles: [
{
condition: generator => !generator.embedded,
condition: generator => !generator.embedded && generator.entityRestLayer,
...javaMainPackageTemplatesBlock('_entityPackage_/'),
templates: ['web/rest/_entityClass_Resource.java'],
},
],
restTestFiles: [
{
condition: generator => !generator.embedded,
condition: generator => !generator.embedded && generator.entityRestLayer,
path: SERVER_TEST_SRC_DIR,
templates: [
{
Expand Down Expand Up @@ -182,16 +182,18 @@ export function writeFiles() {
},

async writeServerFiles({ application, entities }) {
const rootTemplatesPath = application.reactive ? ['reactive', '', '../../java/templates/'] : ['', '../../java/templates/'];
for (const entity of entities.filter(entity => !entity.skipServer)) {
if (entity.builtInUser) {
await this.writeFiles({
sections: userFiles,
rootTemplatesPath,
context: { ...application, ...entity },
});
} else if (!entity.builtIn) {
} else {
await this.writeFiles({
sections: serverFiles,
rootTemplatesPath: application.reactive ? ['reactive', '', '../../java/templates/'] : ['', '../../java/templates/'],
rootTemplatesPath,
context: { ...application, ...entity },
});
}
Expand Down
Loading

0 comments on commit 3bfac62

Please sign in to comment.