Skip to content

Commit

Permalink
format entity create updater user.
Browse files Browse the repository at this point in the history
  • Loading branch information
vnobo committed Nov 3, 2023
1 parent d6413a8 commit 0b4712d
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,31 +118,34 @@ public static Mono<SecurityDetails> securityDetails() {
.cast(SecurityDetails.class);
}

public static <T> Mono<T> userAuditorSerializable(T obejct) {
PropertyDescriptor[] propertyDescriptors = BeanUtils.getPropertyDescriptors(obejct.getClass());
public static <T> Mono<T> serializeUserAuditor(T object) {
PropertyDescriptor[] propertyDescriptors = BeanUtils.getPropertyDescriptors(object.getClass());
var setPropertyStream = Arrays.stream(propertyDescriptors)
.filter(propertyDescriptor -> propertyDescriptor.getPropertyType() == UserAuditor.class);
var propertyFlux = Flux.fromStream(setPropertyStream).flatMap(propertyDescriptor -> {
try {
UserAuditor userAuditor = (UserAuditor) propertyDescriptor.getReadMethod().invoke(obejct);
if (ObjectUtils.isEmpty(userAuditor)) {
return Mono.just(obejct);
}
return USERS_SERVICE.loadByCode(userAuditor.code()).flatMap(user -> {
try {
propertyDescriptor.getWriteMethod().invoke(obejct, UserAuditor.withUser(user));
return Mono.just(obejct);
} catch (IllegalAccessException | InvocationTargetException e) {
return Mono.error(RestServerException.withMsg(
"User auditor serialization getWriteMethod invoke error!", e));
}
});
} catch (IllegalAccessException | InvocationTargetException e) {
return Mono.error(RestServerException.withMsg(
"User auditor serialization getReadMethod invoke error!", e));
var propertyFlux = Flux.fromStream(setPropertyStream)
.flatMap(propertyDescriptor -> handlePropertyDescriptor(object, propertyDescriptor));
return propertyFlux.then(Mono.just(object));
}

private static <T> Mono<String> handlePropertyDescriptor(T object, PropertyDescriptor propertyDescriptor) {
try {
UserAuditor userAuditor = (UserAuditor) propertyDescriptor.getReadMethod().invoke(object);
if (ObjectUtils.isEmpty(userAuditor)) {
return Mono.just("User auditor is empty, No serializable." + propertyDescriptor.getName());
}
});
return propertyFlux.then(Mono.just(obejct));
return USERS_SERVICE.loadByCode(userAuditor.code()).flatMap(user -> {
try {
propertyDescriptor.getWriteMethod().invoke(object, UserAuditor.withUser(user));
return Mono.just("User auditor serializable success. " + propertyDescriptor.getName());
} catch (IllegalAccessException | InvocationTargetException e) {
return Mono.error(RestServerException.withMsg(
"User auditor serialization getWriteMethod invoke error!", e));
}
});
} catch (IllegalAccessException | InvocationTargetException e) {
return Mono.error(RestServerException.withMsg(
"User auditor serialization getReadMethod invoke error!", e));
}
}

public static String nextId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static String applySort(Sort sort, String prefix) {
* @param prefix the prefix for the SQL clause
* @return the generated WHERE SQL clause
*/
public static ParamSql applyParamsSql(Object object, Collection<String> skipKeys, String prefix) {
public static ParamSql buildParamSql(Object object, Collection<String> skipKeys, String prefix) {

Map<String, Object> objectMap = BeanUtils.beanToMap(object, false, true);
if (ObjectUtils.isEmpty(objectMap)) {
Expand All @@ -86,7 +86,7 @@ public static ParamSql applyParamsSql(Object object, Collection<String> skipKeys
}

objectMap = Maps.filterKeys(objectMap, key -> !removeKeys.contains(key));
return applyParamsSql(objectMap, prefix);
return buildParamSql(objectMap, prefix);
}

/**
Expand All @@ -96,7 +96,7 @@ public static ParamSql applyParamsSql(Object object, Collection<String> skipKeys
* @param prefix a prefix to be added to each column in the WHERE clause
* @return a string representing the WHERE clause for the SQL query
*/
public static ParamSql applyParamsSql(Map<String, Object> objectMap, String prefix) {
public static ParamSql buildParamSql(Map<String, Object> objectMap, String prefix) {
StringJoiner whereSql = new StringJoiner(" and ");
for (Map.Entry<String, Object> entry : objectMap.entrySet()) {
String key = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, entry.getKey());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,19 @@ private Mono<UserDetails> buildUserDetails(User user, Set<GrantedAuthority> auth
private Mono<List<GroupMemberResponse>> loadGroups(String userCode) {
return this.queryWithCache("USER_GROUPS-" + userCode,
QUERY_GROUP_MEMBERS_SQL, Map.of("userCode", userCode), GroupMemberResponse.class)
.flatMap(ContextUtils::userAuditorSerializable).collectList();
.flatMap(ContextUtils::serializeUserAuditor).collectList();
}

private Mono<List<TenantMemberResponse>> loadTenants(String userCode) {
return this.queryWithCache("USER_TENANTS-" + userCode,
QUERY_TENANT_MEMBERS_SQL, Map.of("userCode", userCode), TenantMemberResponse.class)
.flatMap(ContextUtils::userAuditorSerializable).collectList();
.flatMap(ContextUtils::serializeUserAuditor).collectList();
}

private Mono<List<GrantedAuthority>> authorities(String userCode) {
return this.getAuthorities(userCode)
.concatWith(this.getGroupAuthorities(userCode))
.flatMap(ContextUtils::userAuditorSerializable).distinct().collectList();
.flatMap(ContextUtils::serializeUserAuditor).distinct().collectList();
}

private Flux<GrantedAuthority> getAuthorities(String userCode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public Flux<Group> search(GroupRequest request, Pageable pageable) {
var cacheKey = ContextUtils.cacheKey(request, pageable);
Query query = Query.query(request.toCriteria()).with(pageable);
return super.queryWithCache(cacheKey, query, Group.class)
.flatMap(ContextUtils::userAuditorSerializable);
.flatMap(ContextUtils::serializeUserAuditor);
}

public Mono<Page<Group>> page(GroupRequest request, Pageable pageable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public Flux<GroupAuthority> search(GroupAuthorityRequest request, Pageable pagea
Query query = Query.query(request.toCriteria()).with(pageable);

return super.queryWithCache(cacheKey, query, GroupAuthority.class)
.flatMap(ContextUtils::userAuditorSerializable);
.flatMap(ContextUtils::serializeUserAuditor);
}

public Mono<Page<GroupAuthority>> page(GroupAuthorityRequest request, Pageable pageable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public Criteria toCriteria() {
}

public ParamSql toParamSql() {
ParamSql paramSql = CriteriaUtils.applyParamsSql(this, List.of("users", "username"), "a");
ParamSql paramSql = CriteriaUtils.buildParamSql(this, List.of("users", "username"), "a");

StringJoiner criteria = paramSql.sql();
Map<String, Object> params = paramSql.params();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public Flux<Tenant> search(TenantRequest request, Pageable pageable) {
var cacheKey = ContextUtils.cacheKey(request, pageable);
var query = Query.query(request.toCriteria()).with(pageable);
return super.queryWithCache(cacheKey, query, Tenant.class)
.flatMap(ContextUtils::userAuditorSerializable);
.flatMap(ContextUtils::serializeUserAuditor);
}

public Mono<Page<Tenant>> page(TenantRequest request, Pageable pageable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public Criteria toCriteria() {

public ParamSql toParamSql() {
ParamSql paramSql = CriteriaUtils
.applyParamsSql(this, List.of("users", "securityCode", "username"), "a");
.buildParamSql(this, List.of("users", "securityCode", "username"), "a");

StringJoiner criteria = paramSql.sql();
Map<String, Object> params = paramSql.params();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public Flux<UserResponse> search(UserRequest request, Pageable pageable) {
ParamSql paramSql = QueryJson.queryJson(request.getQuery());
String query = "select * from se_users" + paramSql.whereSql() + CriteriaUtils.applyPage(pageable);
return super.queryWithCache(cacheKey, query, paramSql.params(), UserResponse.class)
.flatMapSequential(ContextUtils::userAuditorSerializable);
.flatMapSequential(ContextUtils::serializeUserAuditor);
}

public Mono<Page<UserResponse>> page(UserRequest request, Pageable pageable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public Flux<UserAuthority> search(UserAuthorityRequest request) {
var cacheKey = ContextUtils.cacheKey(request);
Query query = Query.query(request.toCriteria());
return super.queryWithCache(cacheKey, query, UserAuthority.class)
.flatMap(ContextUtils::userAuditorSerializable);
.flatMap(ContextUtils::serializeUserAuditor);
}

public Mono<UserAuthority> operate(UserAuthorityRequest request) {
Expand Down

0 comments on commit 0b4712d

Please sign in to comment.