Skip to content

Commit

Permalink
Merge pull request #14703 from Tcharl/constantref
Browse files Browse the repository at this point in the history
use constants in entity-server
  • Loading branch information
Tcharl authored Apr 21, 2021
2 parents b2b65f4 + 17a382e commit 8b316f7
Show file tree
Hide file tree
Showing 16 changed files with 569 additions and 522 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,46 +24,46 @@ import <%= packageName %>.domain.<%= persistClass %>;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
<%_ } _%>
<%_ if (databaseType === 'sql') { _%>
<%_ if (databaseTypeSql) { _%>
import org.springframework.data.jpa.repository.*;
<%_ if (relationshipsContainEagerLoad) { _%>
<%_ if (relationshipsContainEagerLoad) { _%>
import org.springframework.data.repository.query.Param;
<%_ } _%>
<%_ } _%>
<%_ } _%>
<%_ if (databaseType === 'mongodb') { _%>
<%_ if (databaseTypeMongodb) { _%>
import org.springframework.data.mongodb.repository.Query;
import org.springframework.data.mongodb.repository.MongoRepository;
<%_ } _%>
<%_ if (databaseType === 'neo4j') { _%>
<%_ if (databaseTypeNeo4j) { _%>
import org.springframework.data.neo4j.repository.Neo4jRepository;
<%_ } _%>
<%_ if (searchEngine === 'couchbase') { _%>
<%_ if (databaseTypeCouchbase) { _%>
import <%= packageName %>.repository.search.SearchCouchbaseRepository;
<%_ } _%>
<%_ if (databaseType === 'couchbase' && relationshipsContainEagerLoad === true) { _%>
<%_ if (relationshipsContainEagerLoad) { _%>
import org.springframework.data.couchbase.core.query.Query;
<%_ } _%>
<%_ } _%>
<%_ if (databaseType === 'cassandra') { _%>
<%_ if (databaseTypeCassandra) { _%>
import org.springframework.data.cassandra.repository.CassandraRepository;
<%_ } _%>
import org.springframework.stereotype.Repository;
<%_
if (databaseType === 'sql' || databaseType === 'mongodb' || databaseType === 'couchbase') {
let importList = fieldsContainOwnerManyToMany;
for (r of relationships) {
if (r.relationshipType === 'many-to-one' && r.otherEntityName === 'user') {
importList = true;
}
if (databaseTypeSql || databaseTypeMongodb || databaseTypeCouchbase) {
let importList = fieldsContainOwnerManyToMany;
for (r of relationships) {
if (r.relationshipManyToOne && r.otherEntityUser) {
importList = true;
}
}
_%>
<%_ if (importList) { _%>
<%_ if (importList) { _%>
import java.util.List;
<%_ } _%>
<%_ if (relationshipsContainEagerLoad) { _%>
<%_ } _%>
<%_ if (relationshipsContainEagerLoad) { _%>
import java.util.Optional;
<%_ } _%>
<%_ } _%>
<%_ } _%>
<%_ if (primaryKey.type === 'UUID') { _%>
<%_ if (primaryKey.typeUUID) { _%>
import java.util.UUID;
<%_ } _%>
Expand All @@ -75,46 +75,49 @@ import java.util.UUID;
@SuppressWarnings("unused")
<%_ } _%>
@Repository
public interface <%= entityClass %>Repository extends <% if (databaseType === 'sql') { %>JpaRepository<% } %><% if (databaseType === 'mongodb') { %>MongoRepository<% } %><% if (databaseType === 'neo4j') { %>Neo4jRepository<% } %><% if (databaseType === 'cassandra') { %>CassandraRepository<% } %><% if (databaseType === 'couchbase') { %>N1qlCouchbaseRepository<% } %><<%= persistClass %>, <%= primaryKey.type %>><% if (jpaMetamodelFiltering) { %>, JpaSpecificationExecutor<<%= persistClass %>><% } %><% if (searchEngine === 'couchbase') { %>, SearchCouchbaseRepository<<%= persistClass %>, <%= primaryKey.type %>><% } %> {
<%_ for (const relationship of relationships) {
if (relationship.relationshipType === 'many-to-one' && relationship.otherEntityName === 'user' && databaseType === 'sql') { _%>
public interface <%= entityClass %>Repository extends <% if (databaseTypeSql) { %>JpaRepository<% } %><% if (databaseTypeMongodb) { %>MongoRepository<% } %><% if (databaseTypeNeo4j) { %>Neo4jRepository<% } %><% if (databaseTypeCassandra) { %>CassandraRepository<% } %><% if (databaseTypeCouchbase) { %>N1qlCouchbaseRepository<% } %><<%= persistClass %>, <%= primaryKey.type %>><% if (jpaMetamodelFiltering) { %>, JpaSpecificationExecutor<<%= persistClass %>><% } %><% if (searchEngineCouchbase) { %>, SearchCouchbaseRepository<<%= persistClass %>, <%= primaryKey.type %>><% } %> {
<%_
for (const relationship of relationships) {
if (relationship.relationshipManyToOne && relationship.otherEntityUser && databaseTypeSql) { _%>
@Query("select <%= entityInstanceDbSafe %> from <%= persistClass %> <%= entityInstanceDbSafe %> where <%= entityInstanceDbSafe %>.<%= relationship.relationshipFieldName %>.login = ?#{principal.<% if (authenticationType === 'oauth2') { %>preferredUsername<% } else { %>username<% } %>}")
@Query("select <%= entityInstanceDbSafe %> from <%= persistClass %> <%= entityInstanceDbSafe %> where <%= entityInstanceDbSafe %>.<%= relationship.relationshipFieldName %>.login = ?#{principal.<% if (authenticationTypeOauth2) { %>preferredUsername<% } else { %>username<% } %>}")
List<<%= persistClass %>> findBy<%= relationship.relationshipNameCapitalized %>IsCurrentUser();
<%_ } } _%>
<%_ if (relationshipsContainEagerLoad) {
if (databaseType === 'sql') { _%>
<%_ } _%>
<%_ } _%>
<%_
if (relationshipsContainEagerLoad) {
if (databaseTypeSql) { _%>
@Query(value = "select distinct <%= entityInstanceDbSafe %> from <%= persistClass %> <%= entityInstanceDbSafe %><%
for (const relationship of relationships) {
for (const relationship of relationships) {
if (relationship.relationshipEagerLoad) { %> left join fetch <%= entityInstanceDbSafe %>.<%= relationship.reference.name %><% }
 } %>",
} %>",
countQuery = "select count(distinct <%= entityInstanceDbSafe %>) from <%= persistClass %> <%= entityInstanceDbSafe %>")
Page<<%= persistClass %>> findAllWithEagerRelationships(Pageable pageable);
@Query("select distinct <%= entityInstanceDbSafe %> from <%= persistClass %> <%= entityInstanceDbSafe %><%
for (const relationship of relationships) {
for (const relationship of relationships) {
if (relationship.relationshipEagerLoad) { %> left join fetch <%= entityInstanceDbSafe %>.<%= relationship.reference.name %><% }
 } %>")
} %>")
List<<%= persistClass %>> findAllWithEagerRelationships();
@Query("select <%= entityInstanceDbSafe %> from <%= persistClass %> <%= entityInstanceDbSafe %><%
for (const relationship of relationships) {
for (const relationship of relationships) {
if (relationship.relationshipEagerLoad) { %> left join fetch <%= entityInstanceDbSafe %>.<%= relationship.reference.name %><% }
 } %> where <%= entityInstanceDbSafe %>.id =:id")
} %> where <%= entityInstanceDbSafe %>.id =:id")
Optional<<%= persistClass %>> findOneWithEagerRelationships(@Param("id") <%= primaryKey.type %> id);
<%_
} else if (databaseType === 'mongodb' || databaseType === 'couchbase') { _%>
} else if (databaseTypeMongodb || databaseTypeCouchbase) { _%>
@Query("<%= (databaseType === 'mongodb') ? '{}' : '#{#n1ql.selectEntity} WHERE #{#n1ql.filter}' %>")
@Query("<%= (databaseTypeMongodb) ? '{}' : '#{#n1ql.selectEntity} WHERE #{#n1ql.filter}' %>")
Page<<%= persistClass %>> findAllWithEagerRelationships(Pageable pageable);
@Query("<%= (databaseType === 'mongodb') ? '{}' : '#{#n1ql.selectEntity} WHERE #{#n1ql.filter}' %>")
@Query("<%= (databaseTypeMongodb) ? '{}' : '#{#n1ql.selectEntity} WHERE #{#n1ql.filter}' %>")
List<<%= persistClass %>> findAllWithEagerRelationships();
@Query("<%- (databaseType === 'mongodb') ? "{'id': ?0}" : "#{#n1ql.selectEntity} USE KEYS $1 WHERE #{#n1ql.filter}" %>")
@Query("<%- (databaseTypeMongodb) ? "{'id': ?0}" : "#{#n1ql.selectEntity} USE KEYS $1 WHERE #{#n1ql.filter}" %>")
Optional<<%= persistClass %>> findOneWithEagerRelationships(<%= primaryKey.type %> id);
<%_
}
} _%>
}
} _%>
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@
package <%= packageName %>.repository;
import java.util.function.BiFunction;
<%_ if (fieldsContainBigDecimal === true) { _%>
<%_ if (fieldsContainBigDecimal) { _%>
import java.math.BigDecimal;
<%_} if (fieldsContainInstant === true) { _%>
<%_} if (fieldsContainInstant) { _%>
import java.time.Instant;
<%_ } if (fieldsContainLocalDate === true) { _%>
<%_ } if (fieldsContainLocalDate) { _%>
import java.time.LocalDate;
<%_ } if (fieldsContainZonedDateTime === true) { _%>
<%_ } if (fieldsContainZonedDateTime) { _%>
import java.time.ZonedDateTime;
<%_ } if (fieldsContainDuration === true) { _%>
<%_ } if (fieldsContainDuration) { _%>
import java.time.Duration;
<%_ } if (fieldsContainUUID === true) { _%>
<%_ } if (fieldsContainUUID) { _%>
import java.util.UUID;
<%_ } _%>
import java.util.ArrayList;
Expand All @@ -38,7 +38,7 @@ import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import org.springframework.data.domain.Pageable;
<%_ if (databaseType === 'sql') { _%>
<%_ if (databaseTypeSql) { _%>
import io.r2dbc.spi.Row;
import io.r2dbc.spi.RowMetadata;
Expand All @@ -58,10 +58,10 @@ import org.springframework.r2dbc.core.RowsFetchSpec;
import <%= packageName %>.domain.<%= persistClass %>;
<% relationships.forEach(function(rel) {
if (rel.relationshipType === 'many-to-many' && rel.ownerSide) { _%>
if (rel.relationshipManyToMany && rel.ownerSide) { _%>
import <%= packageName %>.domain.<%= asEntity(rel.otherEntityNameCapitalized) %>;
<%_ } _%>
<%_ } ); _%>
<%_ } _%>
<%_ }); _%>
<%_ Object.keys(uniqueEnums).forEach(function(element) { _%>
import <%= packageName %>.domain.enumeration.<%= element %>;
Expand Down Expand Up @@ -97,7 +97,7 @@ class <%= entityClass %>RepositoryInternalImpl implements <%= entityClass %>Repo
<%_ }); _%>
<%_ relationships.forEach(function(rel) {
if (rel.shouldWriteJoinTable) {
if (rel.shouldWriteJoinTable) {
_%>
private final static EntityManager.LinkTable <%= rel.relationshipName %>Link = new LinkTable("<%= rel.joinTable.name %>", "<%= getColumnName(name) %>_id", "<%= getColumnName(rel.relationshipName) %>_id");
<%_ }
Expand Down Expand Up @@ -133,13 +133,13 @@ _%>
.leftOuterJoin(<%= rel.relationshipName %>Table).on(Column.create("<%= colName %>", entityTable)).equals(Column.create("id", <%= rel.relationshipName %>Table ))<%_ }); _%>;
String select = entityManager.createSelect(selectFrom, <%= persistClass %>.class, pageable, criteria);
<%_ if (databaseType === 'sql') { _%>
<%_ if (databaseTypeSql) { _%>
String alias = entityTable.getReferenceName().getReference();
String selectWhere =
Optional.ofNullable(criteria).map(crit ->
new StringBuilder(select).append(" ").append("WHERE").append(" ").append(alias).append(".").append(crit.toString()).toString()).orElse(select); // TODO remove once https://github.com/spring-projects/spring-data-jdbc/issues/907 will be fixed
<%_ } _%>
return db.sql(select<% if (databaseType === 'sql') { %>Where<% } %>).map(this::process);
return db.sql(select<% if (databaseTypeSql) { %>Where<% } %>).map(this::process);
}

@Override
Expand Down Expand Up @@ -172,9 +172,9 @@ _%>
<%_ } _%>
private <%= persistClass %> process(Row row, RowMetadata metadata) {
<%= persistClass %> entity = <%= entityClass.toLowerCase() %>Mapper.apply(row, "e");
<%_ reactiveEagerRelations.forEach(function(rel) { _%>
<%_ reactiveEagerRelations.forEach(function(rel) { _%>
entity.set<%= rel.relationshipNameCapitalized %>(<%= rel.otherEntityNameCapitalized.toLowerCase() %>Mapper.apply(row, "<%= rel.relationshipName %>"));
<%_ }); _%>
<%_ }); _%>
return entity;
}
Expand Down Expand Up @@ -222,27 +222,27 @@ _%>
protected <S extends <%= persistClass %>> Mono<S> updateRelations(S entity) {
<%_ relationships.filter(function(rel) {
return (rel.relationshipType === 'many-to-many' && rel.ownerSide);
}).forEach(function(rel, idx) {
if (idx === 0) { _%>
return (rel.relationshipManyToMany && rel.ownerSide);
}).forEach(function(rel, idx) {
if (idx === 0) { _%>
Mono<Void> result = entityManager.updateLinkTable(<%= rel.relationshipName %>Link, entity.get<%= primaryKey.nameCapitalized %>(), entity.get<%= rel.relationshipNameCapitalizedPlural %>().stream().map(<%= asEntity(rel.otherEntityNameCapitalized) %>::getId)).then();
<%_ } else { _%>
<%_ } else { _%>
result = result.and(entityManager.updateLinkTable(<%= rel.relationshipName %>Link, entity.get<%= primaryKey.nameCapitalized %>(), entity.get<%= rel.relationshipNameCapitalizedPlural %>().stream().map(<%= asEntity(rel.otherEntityNameCapitalized) %>::getId)));
<%_
}
}); _%>
<%_
}
}); _%>
return result.thenReturn(entity);
}
protected Mono<Void> deleteRelations(Long entityId) {
<%_ relationships.filter(function(rel) {
return (rel.relationshipType === 'many-to-many' && rel.ownerSide);
}).forEach(function(rel, idx) {
if (idx === 0) { _%>
return (rel.relationshipManyToMany && rel.ownerSide);
}).forEach(function(rel, idx) {
if (idx === 0) { _%>
return entityManager.deleteFromLinkTable(<%= rel.relationshipName %>Link, entityId)<%
} else { %>
} else { %>
.and(entityManager.deleteFromLinkTable(<%= rel.relationshipName %>Link, entityId))<% }
}); %>;
}); %>;
}
<%_ } _%>
Expand All @@ -255,7 +255,7 @@ class <%= entityClass %>SqlHelper {
let col = field.fieldNameAsDatabaseColumn;
_%>
columns.add(Column.aliased("<%= col %>", table, columnPrefix + "_<%= col %>"));
<%_ if ((field.fieldType === 'byte[]' || field.fieldType === 'ByteBuffer') && field.fieldTypeBlobContent !== 'text') { _%>
<%_ if ((field.fieldTypeBinary) && !field.blobContentTypeText) { _%>
columns.add(Column.aliased("<%= col %>_content_type", table, columnPrefix + "_<%= col %>_content_type"));
<%_ } _%>
<%_ }); _%>
Expand Down
Loading

0 comments on commit 8b316f7

Please sign in to comment.