Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace asEntity/asDto by pre calculated variables. #14273

Merged
merged 2 commits into from
Mar 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,26 @@
-%>
// Initialize the database
<%_ if (primaryKey.type === 'UUID' && databaseType !== 'sql') { _%>
<%= asEntity(entityInstance) %>.set<%= primaryKey.nameCapitalized %>(UUID.randomUUID());
<%= persistInstance %>.set<%= primaryKey.nameCapitalized %>(UUID.randomUUID());
<%_ } _%>
<%= entityInstance %>Repository.<%= saveMethod %>(<%= asEntity(entityInstance) %>)<%= callBlock %>;
<%= entityInstance %>Repository.<%= saveMethod %>(<%= persistInstance %>)<%= callBlock %>;

int databaseSizeBeforeUpdate = <%= entityInstance %>Repository.findAll()<%= callListBlock %>.size();

// Update the <%= entityInstance %> using partial update
<%= asEntity(entityClass) %> partialUpdated<%= asEntity(entityClass) %> = new <%= asEntity(entityClass) %>();
partialUpdated<%= asEntity(entityClass) %>.set<%= primaryKey.nameCapitalized %>(<%= asEntity(entityInstance) %>.get<%= primaryKey.nameCapitalized %>());
<%= persistClass %> partialUpdated<%= persistClass %> = new <%= persistClass %>();
partialUpdated<%= persistClass %>.set<%= primaryKey.nameCapitalized %>(<%= persistInstance %>.get<%= primaryKey.nameCapitalized %>());
<%_ fieldsToUpdate = fields.filter(field => field.includeField) %>
<%_ if (fluentMethods && fieldsToUpdate.length > 0) { _%>
partialUpdated<%= asEntity(entityClass) %><% for (field of fieldsToUpdate) { %>
partialUpdated<%= persistClass %><% for (field of fieldsToUpdate) { %>
.<%= field.fieldName %>(<%= 'UPDATED_' + field.fieldNameUnderscored.toUpperCase() %>)<% if (field.fieldWithContentType) { %>
.<%= field.fieldName %>ContentType(<%= 'UPDATED_' + field.fieldNameUnderscored.toUpperCase() %>_CONTENT_TYPE)<% } %><% } %>;
<%_ } else { _%>
<%_ for (field of fieldsToUpdate) { _%>
<%_ if (field.includeField) { %>
partialUpdated<%= asEntity(entityClass) %>.set<%= field.fieldInJavaBeanMethod %>(<%= field.updateWithValue %>);
partialUpdated<%= persistClass %>.set<%= field.fieldInJavaBeanMethod %>(<%= field.updateWithValue %>);
<%_ if (field.fieldWithContentType) { _%>
partialUpdated<%= asEntity(entityClass) %>.set<%= field.fieldInJavaBeanMethod %>ContentType(<%= field.updateWithValue %>_CONTENT_TYPE);
partialUpdated<%= persistClass %>.set<%= field.fieldInJavaBeanMethod %>ContentType(<%= field.updateWithValue %>_CONTENT_TYPE);
<%_ } _%>
<%_ } _%>
<%_ } _%>
Expand All @@ -46,14 +46,14 @@ partialUpdated<%= asEntity(entityClass) %>.set<%= primaryKey.nameCapitalized %>(
<%_ if (!reactive) { _%>
rest<%= entityClass %>MockMvc.perform(patch(ENTITY_API_URL)<% if (testsNeedCsrf) { %>.with(csrf())<% }%>
.contentType("application/merge-patch+json")
.content(TestUtil.convertObjectToJsonBytes(<%= 'partialUpdated' + asEntity(entityClass) %>)))
.content(TestUtil.convertObjectToJsonBytes(<%= 'partialUpdated' + persistClass %>)))
.andExpect(status().isOk());
<%_ } else { _%>
webTestClient
.patch()
.uri(ENTITY_API_URL)
.contentType(MediaType.valueOf("application/merge-patch+json"))
.bodyValue(TestUtil.convertObjectToJsonBytes(<%= 'partialUpdated' + asEntity(entityClass) %>))
.bodyValue(TestUtil.convertObjectToJsonBytes(<%= 'partialUpdated' + persistClass %>))
.exchange()
.expectStatus()
.isOk();
Expand All @@ -63,9 +63,9 @@ webTestClient
<%_ if (databaseType === 'couchbase') { _%>
SecurityContextHolder.setContext(TestSecurityContextHolder.getContext());
<%_ } _%>
List<<%= asEntity(entityClass) %>> <%= entityInstance %>List = <%= entityInstance %>Repository.findAll()<%= callListBlock %>;
List<<%= persistClass %>> <%= entityInstance %>List = <%= entityInstance %>Repository.findAll()<%= callListBlock %>;
assertThat(<%= entityInstance %>List).hasSize(databaseSizeBeforeUpdate);
<%= asEntity(entityClass) %> test<%= entityClass %> = <%= entityInstance %>List.get(<%= entityInstance %>List.size() - 1);
<%= persistClass %> test<%= entityClass %> = <%= entityInstance %>List.get(<%= entityInstance %>List.size() - 1);
<%_ for (field of fields) { if (field.fieldType === 'ZonedDateTime') { _%>
assertThat(test<%= entityClass %>.get<%= field.fieldInJavaBeanMethod %>()).isEqualTo(<%= field.testWithConstant %>);
<%_ } else if ((field.fieldType === 'byte[]' || field.fieldType === 'ByteBuffer') && field.fieldTypeBlobContent !== 'text') { _%>
Expand Down
16 changes: 8 additions & 8 deletions generators/entity-server/templates/partials/save_template.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
See the License for the specific language governing permissions and
limitations under the License.
-%>
<%_ const instanceType = (dto === 'mapstruct') ? asDto(entityClass) : asEntity(entityClass);
const instanceName = (dto === 'mapstruct') ? asDto(entityInstance) : asEntity(entityInstance);
<%_ const instanceType = restClass;
const instanceName = restInstance;
const mapper = entityInstance + 'Mapper';
const dtoToEntity = mapper + '.' + 'toEntity';
const entityToDto = mapper + '.' + 'toDto';
Expand All @@ -40,20 +40,20 @@ if (isUsingMapsId === true) {
<%_ }
if (!viaService) {
if (dto === 'mapstruct') {
resultEntity = asEntity(entityInstance); _%>
<%= asEntity(entityClass) %> <%= asEntity(entityInstance) %> = <%= dtoToEntity %>(<%= instanceName %>);
resultEntity = persistInstance; _%>
<%= persistClass %> <%= persistInstance %> = <%= dtoToEntity %>(<%= instanceName %>);
<%_ if (isUsingMapsId === true) { _%>
<%= mapsIdAssoc.otherEntity.primaryKey.type %> <%= otherEntityName %>Id = <%= instanceName %>.get<%= mapsIdAssoc.relationshipNameCapitalized %>().get<%= primaryKey.nameCapitalized %>();
<%= mapsIdRepoInstance %>.findById(<%= otherEntityName %>Id).ifPresent(<%= asEntity(entityInstance) %>::<%_ if (fluentMethods === false) { _%>set<%= mapsIdAssoc.relationshipNameCapitalized %> <%_ } else { _%><%= mapsIdAssoc.relationshipName %><%_ } _%>);
<%= mapsIdRepoInstance %>.findById(<%= otherEntityName %>Id).ifPresent(<%= persistInstance %>::<%_ if (fluentMethods === false) { _%>set<%= mapsIdAssoc.relationshipNameCapitalized %> <%_ } else { _%><%= mapsIdAssoc.relationshipName %><%_ } _%>);
<%_ } _%>
<%= asEntity(entityInstance) %> = <%= entityInstance %>Repository.save(<%= asEntity(entityInstance) %>);
<%= returnPrefix %> <%= entityToDto %>(<%= asEntity(entityInstance) %>);
<%= persistInstance %> = <%= entityInstance %>Repository.save(<%= persistInstance %>);
<%= returnPrefix %> <%= entityToDto %>(<%= persistInstance %>);
<%_ } else { resultEntity = 'result'; _%>
<%_ if (isUsingMapsId === true) { _%>
<%= mapsIdAssoc.otherEntity.primaryKey.type %> <%= otherEntityName %>Id = <%= instanceName %>.get<%= mapsIdAssoc.relationshipNameCapitalized %>().get<%= primaryKey.nameCapitalized %>();
<%= mapsIdRepoInstance %>.findById(<%= otherEntityName %>Id).ifPresent(<%= instanceName %>::<%_ if (fluentMethods === false) { _%>set<%= mapsIdAssoc.relationshipNameCapitalized %> <%_ } else { _%><%= otherEntityName %><%_ } _%>);
<%_ } _%>
<%= returnPrefix %> <%= entityInstance %>Repository.save(<%= asEntity(entityInstance) %>);
<%= returnPrefix %> <%= entityInstance %>Repository.save(<%= persistInstance %>);
<%_ }
if (searchEngine === 'elasticsearch') { _%>
<%= entityInstance %>SearchRepository.save(<%= resultEntity %>);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
See the License for the specific language governing permissions and
limitations under the License.
-%>
<%_ const instanceType = (dto === 'mapstruct') ? asDto(entityClass) : asEntity(entityClass);
const instanceName = (dto === 'mapstruct') ? asDto(entityInstance) : asEntity(entityInstance);
<%_ const instanceType = restClass;
const instanceName = restInstance;
const mapper = entityInstance + 'Mapper';
const dtoToEntity = mapper + '.' + 'toEntity';
const entityToDtoReference = mapper + '::' + 'toDto';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
limitations under the License.
-%>
<%_
const instanceType = (dto === 'mapstruct') ? asDto(entityClass) : asEntity(entityClass);
const instanceName = (dto === 'mapstruct') ? asDto(entityInstance) : asEntity(entityInstance);
const instanceType = restClass;
const instanceName = restInstance;
const mapper = entityInstance + 'Mapper';
const entityListToDtoListReference = mapper + '.' + 'toDto';
const entityToDtoReference = mapper + '::'+ 'toDto';
Expand Down Expand Up @@ -55,7 +55,7 @@ _%>
<%_ if (viaService) { _%>
return <%= entityInstance %>Service.findAll()<% if (reactive) { %>.collectList()<% } %>;
<%_ } else if (dto === 'mapstruct') { _%>
<%= reactive ? 'Flux' : 'List' %><<%= asEntity(entityClass) %>> <%= entityInstancePlural %> = <%= entityInstance %>Repository.<% if (relationshipsContainEagerLoad) { %>findAllWithEagerRelationships<% } else { %>findAll<% } %>();
<%= reactive ? 'Flux' : 'List' %><<%= persistClass %>> <%= entityInstancePlural %> = <%= entityInstance %>Repository.<% if (relationshipsContainEagerLoad) { %>findAllWithEagerRelationships<% } else { %>findAll<% } %>();
<%_ if (!reactive) { _%>
return <%= entityListToDtoListReference %>(<%= entityInstancePlural %>);
<%_ } else { _%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
limitations under the License.
-%>
<%
const instanceType = (dto === 'mapstruct') ? asDto(entityClass) : asEntity(entityClass);
const instanceType = restClass;
const mapper = entityInstance + 'Mapper';
const entityToDtoReference = mapper + '::' + 'toDto';
const reactiveSql = (databaseType === 'sql' && reactive);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
limitations under the License.
-%>
<% const optionalOrMono = (reactive === true) ? 'Mono' : 'Optional';
const instanceType = (dto === 'mapstruct') ? `${optionalOrMono}<${asDto(entityClass)}>` : `${optionalOrMono}<${asEntity(entityClass)}>`;
const instanceName = (dto === 'mapstruct') ? asDto(entityInstance) : asEntity(entityInstance);
const instanceType = `${optionalOrMono}<${restClass}>`;
const instanceName = restInstance;
const mapper = `${entityInstance}Mapper`;
const entityToDtoReference = `${mapper}::toDto`;
const returnPrefix = returnDirectly ? 'return' : `${instanceType} ${instanceName} =`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
limitations under the License.
-%>
<%
const instanceType = (dto === 'mapstruct') ? asDto(entityClass) : asEntity(entityClass);
const instanceName = (dto === 'mapstruct') ? asDto(entityInstance) : asEntity(entityInstance);
const instanceType = restClass;
const instanceName = restInstance;
const returnType = reactive ? 'Mono' : 'Optional'
const mapOrFlatMap = reactive ? 'flatMap' : 'map'
const returnPrefix = (isService) ? 'return' : returnType + '<' + instanceType + '> result =';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
limitations under the License.
-%>
<%_
const instanceType = (dto === 'mapstruct') ? asDto(entityClass) : asEntity(entityClass);
const instanceName = (dto === 'mapstruct') ? asDto(entityInstance) : asEntity(entityInstance);
const instanceType = restClass;
const instanceName = restInstance;
const listOrFlux = (reactive === true) ? 'Flux' : 'List';
const mapper = entityInstance + 'Mapper';
const entityToDtoReference = mapper + '::' + 'toDto';
Expand All @@ -33,7 +33,7 @@ if (pagination === 'no') { %>
<%_ if (viaService) { _%>
Page<<%= instanceType %>> page = <%= entityInstance %>Service.search(query, pageable);
<%_ } else { _%>
Page<<%= asEntity(entityClass) %>> page = <%= entityInstance %><% if (searchEngine == 'elasticsearch') { %>Search<% } %>Repository.search(<% if (searchEngine === 'elasticsearch') { %>queryStringQuery(query)<% } else { %><%= entityClass %>.PREFIX, query<% } %>, pageable);
Page<<%= persistClass %>> page = <%= entityInstance %><% if (searchEngine == 'elasticsearch') { %>Search<% } %>Repository.search(<% if (searchEngine === 'elasticsearch') { %>queryStringQuery(query)<% } else { %><%= entityClass %>.PREFIX, query<% } %>, pageable);
<%_ } _%>
HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(<% if (!reactive) { %>ServletUriComponentsBuilder.fromCurrentRequest()<% } else { %>UriComponentsBuilder.fromHttpRequest(request)<% } %>, page);
return ResponseEntity.ok().headers(headers).body(<% if (!viaService && dto === 'mapstruct') { %><%= entityListToDtoListReference %>(<% } %>page.getContent()<% if (!viaService && dto === 'mapstruct') { %>)<% } %>);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ import static org.springframework.data.couchbase.core.mapping.id.GenerationStrat
<%_ } _%>
<%_ if (typeof javadoc == 'undefined') { _%>
/**
* A <%= asEntity(entityClass) %>.
* A <%= persistClass %>.
*/
<%_ } else { _%>
<%- formatAsClassJavadoc(javadoc) %>
Expand Down Expand Up @@ -170,7 +170,7 @@ import static org.springframework.data.couchbase.core.mapping.id.GenerationStrat
<%_ if (searchEngine === 'elasticsearch') { _%>
@org.springframework.data.elasticsearch.annotations.Document(indexName = "<%= entityInstance.toLowerCase() %>")
<%_ } _%>
public class <%= asEntity(entityClass) %> implements Serializable {
public class <%= persistClass %> implements Serializable {

private static final long serialVersionUID = 1L;
<%_ if (databaseType === 'couchbase' && !embedded) { _%>
Expand Down Expand Up @@ -497,7 +497,7 @@ relationships.forEach((relationship, idx) => {
}

<%_ if (fluentMethods) { _%>
public <%= asEntity(entityClass) %> <%= primaryKey.name %>(<%= primaryKey.type %> <%= primaryKey.name %>) {
public <%= persistClass %> <%= primaryKey.name %>(<%= primaryKey.type %> <%= primaryKey.name %>) {
this.<%= primaryKey.name %> = <%= primaryKey.name %>;
return this;
}
Expand All @@ -519,9 +519,9 @@ relationships.forEach((relationship, idx) => {
<%_ if (fluentMethods) { _%>

<%_ if (fieldTypeBlobContent !== 'text') { _%>
public <%= asEntity(entityClass) %> <%= fieldName %>(<%= fieldType %> <%= fieldName %>) {
public <%= persistClass %> <%= fieldName %>(<%= fieldType %> <%= fieldName %>) {
<%_ } else { _%>
public <%= asEntity(entityClass) %> <%= fieldName %>(String <%= fieldName %>) {
public <%= persistClass %> <%= fieldName %>(String <%= fieldName %>) {
<%_ } _%>
<%_ if (fieldType === 'BigDecimal' && databaseType === 'sql' && reactive) { _%>
this.<%= fieldName %> = <%= fieldName %> != null ? <%= fieldName %>.stripTrailingZeros() : null;
Expand Down Expand Up @@ -550,7 +550,7 @@ relationships.forEach((relationship, idx) => {
}
<%_ if (fluentMethods) { _%>

public <%= asEntity(entityClass) %> <%= fieldName %>ContentType(String <%= fieldName %>ContentType) {
public <%= persistClass %> <%= fieldName %>ContentType(String <%= fieldName %>ContentType) {
this.<%= fieldName %>ContentType = <%= fieldName %>ContentType;
return this;
}
Expand Down Expand Up @@ -592,7 +592,7 @@ relationships.forEach((relationship, idx) => {
}
<%_ if (fluentMethods) { _%>

public <%= asEntity(entityClass) %> <%= relationshipFieldNamePlural %>(Set<<%= asEntity(otherEntityNameCapitalized) %>> <%= otherEntityNamePlural %>) {
public <%= persistClass %> <%= relationshipFieldNamePlural %>(Set<<%= asEntity(otherEntityNameCapitalized) %>> <%= otherEntityNamePlural %>) {
this.set<%= relationshipNameCapitalizedPlural %>(<%= otherEntityNamePlural %>);
<%_ if (databaseType === 'couchbase' && !otherEntityIsEmbedded) { _%>
this.<%= relationshipFieldName %>Ids = <%= otherEntityNamePlural %>.stream()
Expand All @@ -602,7 +602,7 @@ relationships.forEach((relationship, idx) => {
return this;
}

public <%= asEntity(entityClass) %> add<%= relationshipNameCapitalized %>(<%= asEntity(otherEntityNameCapitalized) %> <%= otherEntityName %>) {
public <%= persistClass %> add<%= relationshipNameCapitalized %>(<%= asEntity(otherEntityNameCapitalized) %> <%= otherEntityName %>) {
this.<%= relationshipFieldNamePlural %>.add(<%= otherEntityName %>);
<%_ if (!otherEntityIsEmbedded || embedded && ownerSide === true) { _%>
<%_ if (databaseType === 'couchbase' && !otherEntityIsEmbedded) { _%>
Expand All @@ -622,7 +622,7 @@ relationships.forEach((relationship, idx) => {
return this;
}

public <%= asEntity(entityClass) %> remove<%= relationshipNameCapitalized %>(<%= asEntity(otherEntityNameCapitalized) %> <%= otherEntityName %>) {
public <%= persistClass %> remove<%= relationshipNameCapitalized %>(<%= asEntity(otherEntityNameCapitalized) %> <%= otherEntityName %>) {
this.<%= relationshipFieldNamePlural %>.remove(<%= otherEntityName %>);
<%_ if (!otherEntityIsEmbedded || (embedded && ownerSide === true)) { _%>
<%_ if (databaseType === 'couchbase' && !otherEntityIsEmbedded) { _%>
Expand Down Expand Up @@ -674,7 +674,7 @@ relationships.forEach((relationship, idx) => {
}
<%_ if (fluentMethods) { _%>

public <%= asEntity(entityClass) %> <%= relationshipFieldName %>(<%= asEntity(otherEntityNameCapitalized) %> <%= otherEntityName %>) {
public <%= persistClass %> <%= relationshipFieldName %>(<%= asEntity(otherEntityNameCapitalized) %> <%= otherEntityName %>) {
this.set<%= relationshipNameCapitalized %>(<%= otherEntityName %>);
<%_ if ((databaseType === 'couchbase' && !otherEntityIsEmbedded) || reactiveRelationshipWithId) { _%>
this.<%= relationshipFieldName %>Id = <%= otherEntityName %> != null ? <%= otherEntityName %>.get<%= relationship.otherEntity.primaryKey.nameCapitalized %>() : null;
Expand Down Expand Up @@ -724,11 +724,11 @@ relationships.forEach((relationship, idx) => {
if (this == o) {
return true;
}
if (!(o instanceof <%= asEntity(entityClass) %>)) {
if (!(o instanceof <%= persistClass %>)) {
return false;
}
<%_ if (!embedded) { _%>
return <%= primaryKey.name %> != null && <%= primaryKey.name %>.equals(((<%= asEntity(entityClass) %>) o).<%= primaryKey.name %>);
return <%= primaryKey.name %> != null && <%= primaryKey.name %>.equals(((<%= persistClass %>) o).<%= primaryKey.name %>);
<%_ } else { _%>
return false;
<%_ } _%>
Expand All @@ -743,7 +743,7 @@ relationships.forEach((relationship, idx) => {
// prettier-ignore
@Override
public String toString() {
return "<%= asEntity(entityClass) %>{" +
return "<%= persistClass %>{" +
<%_ if (!embedded) { _%>
"<%= primaryKey.name %>=" + get<%= primaryKey.nameCapitalized %>() +
<%_ } _%>
Expand Down
Loading