Skip to content

Commit 193ae8f

Browse files
pvlisssendilkumarn
authored andcommitted
Use single expression functions (#165)
* Use single expression functions Fix #153 * Fix ktlint format for maven * Formatting * Use single expression functions Fix #153 * Use single expression functions - Review fixes Fix #153
1 parent c231365 commit 193ae8f

File tree

123 files changed

+936
-1123
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

123 files changed

+936
-1123
lines changed

generators/entity-server/templates/src/main/kotlin/package/common/get_all_template.ejs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,14 @@
2222
const mapper = entityInstance + 'Mapper';
2323
const entityListToDtoListReference = mapper + '.' + 'toDto';
2424
const entityToDtoReference = mapper + '::'+ 'toDto';
25-
if (jpaMetamodelFiltering) { %>
26-
fun getAll<%= entityClassPlural %>(criteria: <%= entityClass %>Criteria<% if (pagination != 'no') { %>, pageable: Pageable, @RequestParam queryParams: MultiValueMap<String, String>, uriBuilder: UriComponentsBuilder<% } %>): ResponseEntity<MutableList<<%= instanceType %>>> {
25+
if (jpaMetamodelFiltering) {
26+
_%>
27+
fun getAll<%= entityClassPlural %>(
28+
criteria: <%= entityClass %>Criteria<% if (pagination != 'no') { %>,
29+
pageable: Pageable,
30+
@RequestParam queryParams: MultiValueMap<String, String>,
31+
uriBuilder: UriComponentsBuilder<% } %>
32+
): ResponseEntity<MutableList<<%= instanceType %>>> {
2733
log.debug("REST request to get <%= entityClassPlural %> by criteria: {}", criteria)
2834
<%_ if (pagination === 'no') { _%>
2935
val entityList = <%= entityInstance %>QueryService.findByCriteria(criteria)
@@ -55,7 +61,13 @@
5561
return <%= entityListToDtoListReference %>(<%= entityInstancePlural %>)<% } else { %>
5662
return <%= entityInstance %>Repository.<% if (fieldsContainOwnerManyToMany) { %>findAllWithEagerRelationships<% } else { %>findAll<% } %>()<% } %>
5763
<%_ } if (pagination !== 'no') { _%>
58-
fun getAll<%= entityClassPlural %>(pageable: Pageable, @RequestParam queryParams: MultiValueMap<String, String>, uriBuilder: UriComponentsBuilder<% if (fieldsContainNoOwnerOneToOne) { %>, @RequestParam(required = false) filter: String?<% } %><% if (fieldsContainOwnerManyToMany) { %>, @RequestParam(required = false, defaultValue = "false") eagerload: Boolean<% } %>): ResponseEntity<MutableList<<%= instanceType %>>> {<%- include('get_all_stream_template', {viaService: viaService}); -%>
64+
fun getAll<%= entityClassPlural %>(
65+
pageable: Pageable,
66+
@RequestParam queryParams: MultiValueMap<String, String>,
67+
uriBuilder: UriComponentsBuilder<% if (fieldsContainNoOwnerOneToOne) { %>,
68+
@RequestParam(required = false) filter: String?<% } if (fieldsContainOwnerManyToMany) { %>,
69+
@RequestParam(required = false, defaultValue = "false") eagerload: Boolean<% } %>
70+
) : ResponseEntity<MutableList<<%= instanceType %>>> {<%- include('get_all_stream_template', {viaService: viaService}); -%>
5971
log.debug("REST request to get a page of <%= entityClassPlural %>")
6072
<%_ if (viaService) { _%>
6173
<%_ if (fieldsContainOwnerManyToMany) { _%>

generators/entity-server/templates/src/main/kotlin/package/repository/EntityRepository.kt.ejs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,11 @@ interface <%=entityClass%>Repository : <% if (databaseType === 'sql') { %>JpaRep
6969
<%_ if (fieldsContainOwnerManyToMany === true) {
7070
if (databaseType === 'sql') { _%>
7171
72-
@Query(value = "select distinct <%= entityInstance %> from <%= asEntity(entityClass) %> <%= entityInstance %><% for (idx in relationships) {
72+
@Query(
73+
value = "select distinct <%= entityInstance %> from <%= asEntity(entityClass) %> <%= entityInstance %><% for (idx in relationships) {
7374
if (relationships[idx].relationshipType === 'many-to-many' && relationships[idx].ownerSide === true) { %> left join fetch <%=entityInstance%>.<%=relationships[idx].relationshipFieldNamePlural%><%} }%>",
74-
countQuery = "select count(distinct <%= entityInstance %>) from <%= asEntity(entityClass) %> <%= entityInstance %>")
75+
countQuery = "select count(distinct <%= entityInstance %>) from <%= asEntity(entityClass) %> <%= entityInstance %>"
76+
)
7577
fun findAllWithEagerRelationships(pageable: Pageable): Page<<%= asEntity(entityClass) %>>
7678
7779
@Query(value = "select distinct <%= entityInstance %> from <%= asEntity(entityClass) %> <%= entityInstance %><% for (idx in relationships) {

generators/entity-server/templates/src/main/kotlin/package/service/EntityQueryService.kt.ejs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class <%= serviceClassName %>(
7676
<%- include('../common/inject_template', {viaService: false, constructorName: serviceClassName, queryService: false, isUsingMapsId: false, mapsIdAssoc: null, isController: false}); -%>
7777
) : QueryService<<%= asEntity(entityClass) %>>() {
7878
79-
private val log = LoggerFactory.getLogger(<%= serviceClassName %>::class.java)
79+
private val log = LoggerFactory.getLogger(javaClass)
8080
8181
/**
8282
* Return a [MutableList] of [<%= instanceType %>] which matches the criteria from the database.

generators/entity-server/templates/src/main/kotlin/package/service/impl/EntityServiceImpl.kt.ejs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class <%= serviceClassName %>(
8686
<%- include('../../common/inject_template', {asEntity, asDto, viaService: viaService, constructorName: serviceClassName, queryService: false, isUsingMapsId: isUsingMapsId, mapsIdAssoc: mapsIdAssoc, isController: false}); -%>
8787
)<% if (service === 'serviceImpl') { %> : <%= entityClass %>Service<% } %> {
8888
89-
private val log = LoggerFactory.getLogger(<%= serviceClassName %>::class.java)
89+
private val log = LoggerFactory.getLogger(javaClass)
9090
9191
/**
9292
* Save a <%= entityInstance %>.

generators/entity-server/templates/src/main/kotlin/package/service/mapper/EntityMapper.kt.ejs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ import org.mapstruct.Mappings
3535
if (!existingMappings.includes(relationships[idx].otherEntityNameCapitalized) && asEntity(relationships[idx].otherEntityNameCapitalized) !== entityClass) {
3636
existingMappings.push(relationships[idx].otherEntityNameCapitalized);
3737
} } } %><%= existingMappings.map(otherEntityNameCapitalized => otherEntityNameCapitalized + 'Mapper::class').join(', ') %>])
38-
interface <%= entityClass %>Mapper : EntityMapper<<%= asDto(entityClass) %>, <%= asEntity(entityClass) %>> {
38+
interface <%= entityClass %>Mapper :
39+
EntityMapper<<%= asDto(entityClass) %>, <%= asEntity(entityClass) %>> {
3940
<%_
4041
// entity -> DTO mapping
4142
var mappedRelsEnt = [];

generators/entity-server/templates/src/main/kotlin/package/web/rest/EntityResource.kt.ejs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,14 @@ import java.util.UUID<% } %><% if (searchEngine === 'elasticsearch') { %>
104104
import org.elasticsearch.index.query.QueryBuilders.queryStringQuery
105105
<%_ } _%>
106106
107+
<%_
108+
let entityName = entityInstance;
109+
if (clientRootFolder && !skipUiGrouping) {
110+
entityName = _.camelCase(`${clientRootFolder}${entityClass}`)
111+
}
112+
_%>
113+
private const val ENTITY_NAME = "<%= entityName %>"
114+
107115
/**
108116
* REST controller for managing [<%= packageName %>.domain.<%= asEntity(entityClass) %>].
109117
*/
@@ -117,14 +125,10 @@ class <%= entityClass %>Resource(
117125
_%><%- include('../../common/inject_template', {viaService: viaService, constructorName: entityClass + 'Resource', queryService: jpaMetamodelFiltering, isUsingMapsId: isUsingMapsId, mapsIdAssoc: mapsIdAssoc, isController: true}); -%>
118126
) {
119127
120-
private val log = LoggerFactory.getLogger(this.javaClass)
128+
private val log = LoggerFactory.getLogger(javaClass)
121129
122130
@Value("\${jhipster.clientApp.name}")
123131
private var applicationName: String? = null
124-
<%_ let entityName = entityInstance;
125-
if (clientRootFolder && !skipUiGrouping) {
126-
entityName = _.camelCase(`${clientRootFolder}${entityClass}`)
127-
} _%>
128132
129133
/**
130134
* `POST /<%= entityApiUrl %>` : Create a new <%= entityInstance %>.
@@ -137,7 +141,10 @@ _%><%- include('../../common/inject_template', {viaService: viaService, construc
137141
fun create<%= entityClass %>(<% if (validation) { %>@Valid <% } %>@RequestBody <%= instanceName %>: <%= instanceType %>): ResponseEntity<<%= instanceType %>> {
138142
log.debug("REST request to save <%= entityClass %> : {}", <%= instanceName %>)
139143
if (<%= instanceName %>.id != null) {
140-
throw BadRequestAlertException("A new <%= entityInstance %> cannot already have an ID", ENTITY_NAME, "idexists")
144+
throw BadRequestAlertException(
145+
"A new <%= entityInstance %> cannot already have an ID",
146+
ENTITY_NAME, "idexists"
147+
)
141148
}
142149
<%_ if (saveUserSnapshot) { %>
143150
if (<%= instanceName %>.user != null) {
@@ -177,7 +184,12 @@ _%><%- include('../../common/inject_template', {viaService: viaService, construc
177184
<%_ } _%>
178185
<%- include('../../common/save_template', {asEntity, asDto, viaService: viaService, returnDirectly: false, isUsingMapsId: false, mapsIdAssoc: mapsIdAssoc}); -%>
179186
return ResponseEntity.ok()
180-
.headers(HeaderUtil.createEntityUpdateAlert(applicationName, <%= enableTranslation %>, ENTITY_NAME, <%= instanceName %>.id.toString()))
187+
.headers(
188+
HeaderUtil.createEntityUpdateAlert(
189+
applicationName, <%= enableTranslation %>, ENTITY_NAME,
190+
<%= instanceName %>.id.toString()
191+
)
192+
)
181193
.body(result)
182194
}
183195
@@ -240,7 +252,8 @@ _%><%- include('../../common/inject_template', {viaService: viaService, construc
240252
fun delete<%= entityClass %>(@PathVariable id: <%= pkType %>): ResponseEntity<Void> {
241253
log.debug("REST request to delete <%= entityClass %> : {}", id)
242254
<%- include('../../common/delete_template', {viaService: viaService}); -%>
243-
return ResponseEntity.noContent().headers(HeaderUtil.createEntityDeletionAlert(applicationName, <%= enableTranslation %>, ENTITY_NAME, id<% if (pkType !== 'String') { %>.toString()<% } %>)).build()
255+
return ResponseEntity.noContent()
256+
.headers(HeaderUtil.createEntityDeletionAlert(applicationName, <%= enableTranslation %>, ENTITY_NAME, id<% if (pkType !== 'String') { %>.toString()<% } %>)).build()
244257
}<% if (searchEngine === 'elasticsearch') { %>
245258
246259
/**
@@ -254,8 +267,4 @@ _%><%- include('../../common/inject_template', {viaService: viaService, construc
254267
* @return the result of the search.
255268
*/
256269
@GetMapping("/_search/<%= entityApiUrl %>")<%- include('../../common/search_template', {asEntity, asDto, viaService}); -%><% } %>
257-
258-
companion object {
259-
private const val ENTITY_NAME = "<%= entityName %>"
260-
}
261270
}

generators/server/files.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1941,7 +1941,7 @@ function writeFiles() {
19411941
</dependencies>`;
19421942
this.addMavenPlugin('org.jetbrains.kotlin', 'kotlin-maven-plugin', '${kotlin.version}', kotlinOther);
19431943

1944-
removeDefaultMavenCompilerPlugin(this);
1944+
updatePom(this);
19451945
const defaultCompileOther = ` <executions>
19461946
<!-- Replacing default-compile as it is treated specially by maven -->
19471947
<execution>
@@ -2099,25 +2099,31 @@ function writeFilesToDisk(files, generator, returnFiles, prefix) {
20992099
}
21002100

21012101
/**
2102-
* remove the default <maven-compiler-plugin> configuration from pom.xml.
2102+
* Manually updates the pom.xml file to perform the following operations:
2103+
* 1. Set the Kotlin source directories as the default (Needed for the ktlint plugin to properly format the sources)
2104+
* 2. Remove the default <maven-compiler-plugin> configuration.
21032105
*/
2104-
function removeDefaultMavenCompilerPlugin(generator) {
2106+
function updatePom(generator) {
21052107
const _this = generator || this;
21062108

21072109
const fullPath = path.join(process.cwd(), 'pom.xml');
21082110
const artifactId = 'maven-compiler-plugin';
21092111

21102112
const xml = _this.fs.read(fullPath).toString();
2111-
21122113
const $ = cheerio.load(xml, { xmlMode: true });
21132114

2115+
// 1. Set the Kotlin source directories as the default
2116+
$('build > defaultGoal').after(`
2117+
2118+
<sourceDirectory>src/main/kotlin</sourceDirectory>
2119+
<testSourceDirectory>src/test/kotlin</testSourceDirectory>
2120+
`);
2121+
// 2. Remove the default <maven-compiler-plugin> configuration
21142122
$(`build > plugins > plugin > artifactId:contains('${artifactId}')`)
21152123
.parent()
21162124
.remove();
21172125

2118-
const modifiedXml = $.xml();
2119-
2120-
_this.fs.write(fullPath, modifiedXml);
2126+
_this.fs.write(fullPath, $.xml());
21212127
}
21222128

21232129
module.exports = {

generators/server/templates/src/main/kotlin/package/Application.kt.ejs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ import java.net.UnknownHostException
6767
<%_ } _%>
6868
class <%= mainClass %>(private val env: Environment) : InitializingBean {
6969

70-
private val log = LoggerFactory.getLogger(<%= mainClass %>::class.java)
70+
private val log = LoggerFactory.getLogger(javaClass)
7171

7272
/**
7373
* Initializes <%= baseName %>.

generators/server/templates/src/main/kotlin/package/aop/logging/LoggingAspect.kt.ejs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,8 @@ open class LoggingAspect(private val env: Environment) {
4949
" || within(@org.springframework.stereotype.Service *)" +
5050
" || within(@org.springframework.web.bind.annotation.RestController *)"
5151
)
52-
fun springBeanPointcut() {
53-
// Method is empty as this is just a Pointcut, the implementations are in the advices.
54-
}
52+
fun springBeanPointcut() =
53+
Unit // Method is empty as this is just a Pointcut, the implementations are in the advices.
5554

5655
/**
5756
* Pointcut that matches all Spring beans in the application's main packages.
@@ -61,9 +60,8 @@ open class LoggingAspect(private val env: Environment) {
6160
" || within(<%=packageName%>.service..*)" +
6261
" || within(<%=packageName%>.web.rest..*)"
6362
)
64-
fun applicationPackagePointcut() {
65-
// Method is empty as this is just a Pointcut, the implementations are in the advices.
66-
}
63+
fun applicationPackagePointcut() =
64+
Unit // Method is empty as this is just a Pointcut, the implementations are in the advices.
6765

6866
/**
6967
* Advice that logs methods throwing exceptions.

generators/server/templates/src/main/kotlin/package/client/JWT_UserFeignClientInterceptor.kt.ejs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,12 @@ import feign.RequestInterceptor
2323
import feign.RequestTemplate
2424
import org.springframework.stereotype.Component
2525

26+
private const val AUTHORIZATION_HEADER = "Authorization"
27+
private const val BEARER_TOKEN_TYPE = "Bearer"
28+
2629
@Component
2730
class UserFeignClientInterceptor : RequestInterceptor {
2831

29-
override fun apply(template: RequestTemplate) {
32+
override fun apply(template: RequestTemplate) =
3033
getCurrentUserJWT().ifPresent { s -> template.header(AUTHORIZATION_HEADER,"$BEARER_TOKEN_TYPE $s") }
31-
}
32-
33-
companion object {
34-
private const val AUTHORIZATION_HEADER = "Authorization"
35-
private const val BEARER_TOKEN_TYPE = "Bearer"
36-
}
37-
3834
}

0 commit comments

Comments
 (0)