Skip to content

Commit

Permalink
npm install create package-lock.json
Browse files Browse the repository at this point in the history
  • Loading branch information
vnobo committed Oct 15, 2023
1 parent 6f67c4c commit 08df27e
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,66 +24,93 @@ public static String applyPage(Pageable pageable) {
}

public static String applyPage(Pageable pageable, String prefix) {
//Apply the sort to the pageable object
String orderSql = applySort(pageable.getSort(), prefix);
//Format the orderSql string with the pageSize and offset from the pageable object
return String.format(orderSql + " limit %d offset %d", pageable.getPageSize(), pageable.getOffset());
}

public static String applySort(Sort sort, String prefix) {
// Check if the sort is null or unsorted
if (sort == null || sort.isUnsorted()) {
return "";
}
// Create a StringJoiner to store the SQL for the sort
StringJoiner sortSql = new StringJoiner(" , ");
// Iterate through the sort and add the sorted property name and order to the StringJoiner
sort.iterator().forEachRemaining((o) -> {
String sortedPropertyName = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, o.getProperty());
// Check if the property should be ignored
String sortedProperty = o.isIgnoreCase() ? "lower(" + sortedPropertyName + ")" : sortedPropertyName;
// Check if a prefix is provided
if (StringUtils.hasLength(prefix)) {
sortedProperty = prefix + "." + sortedProperty;
}
// Add the sorted property and order to the StringJoiner
sortSql.add(sortedProperty + (o.isAscending() ? " asc" : " desc"));
});
// Return the SQL for the sort
return " order by " + sortSql;
}

public static String whereSql(Object object, Collection<String> skipKeys, String prefix) {

//Create a map of the object's properties
Map<String, Object> objectMap = BeanUtils.beanToMap(object, false, true);
//If the objectMap is empty, return an empty string
if (ObjectUtils.isEmpty(objectMap)) {
return "";
}

//Create a set of the keys to be removed
Set<String> removeKeys = new HashSet<>(SKIP_CRITERIA_KEYS);
//If the skipKeys is not empty, add it to the set
if (!ObjectUtils.isEmpty(skipKeys)) {
removeKeys.addAll(skipKeys);
}

//Filter the objectMap, removing any keys that are in the set
objectMap = Maps.filterKeys(objectMap, key -> !removeKeys.contains(key));
//Return the whereSql with the filtered objectMap
return whereSql(objectMap, prefix);
}

public static String whereSql(Map<String, Object> objectMap, String prefix) {

// Check if the objectMap is empty
if (ObjectUtils.isEmpty(objectMap)) {
return "";
}

// Create a StringJoiner object to store the SQL
StringJoiner whereSql = new StringJoiner(" and ");
// Loop through the objectMap
for (Map.Entry<String, Object> entry : objectMap.entrySet()) {
// Convert the key to lower camel case
String key = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, entry.getKey());
// Check if the prefix is not empty
if (StringUtils.hasLength(prefix)) {
// Add the prefix to the key
key = prefix + "." + key;
}
// Check if the value is a String
if (entry.getValue() instanceof String) {
// Add the key and value to the StringJoiner
whereSql.add(key + " like :" + entry.getKey());
// Check if the value is a Collection
} else if (entry.getValue() instanceof Collection<?>) {
// Add the key and value to the StringJoiner
whereSql.add(key + " in (:" + entry.getKey() + ")");
// Otherwise
} else {
// Add the key and value to the StringJoiner
whereSql.add(key + " = :" + entry.getKey());
}
}

// Return the SQL
return "Where " + whereSql;
}

/**
* Builds a Criteria object from the given object excluding the given keys.
* The static skip keys such as {@link CriteriaUtils} are also excluded.
Expand All @@ -93,14 +120,21 @@ public static String whereSql(Map<String, Object> objectMap, String prefix) {
* @return the built Criteria
*/
public static Criteria build(Object object, Collection<String> skipKes) {
//Create a map of the object's properties
Map<String, Object> objectMap = BeanUtils.beanToMap(object, true);
//If the map is not empty
if (!ObjectUtils.isEmpty(objectMap)) {
//Create a set of the criteria keys to skip
Set<String> mergeSet = new HashSet<>(SKIP_CRITERIA_KEYS);
//If the skipKes parameter is not empty
if (!ObjectUtils.isEmpty(skipKes)) {
//Add the skipKes to the set
mergeSet.addAll(skipKes);
}
//Remove the criteria keys from the map
mergeSet.forEach(objectMap::remove);
}
//Return the criteria built from the map
return build(objectMap);
}

Expand All @@ -117,10 +151,10 @@ public static Criteria build(Object object, Collection<String> skipKes) {
* @return A {@link Criteria} built from the input.
*/
public static Criteria build(Map<String, Object> objectMap) {
if (ObjectUtils.isEmpty(objectMap)) {
if (objectMap == null || objectMap.isEmpty()) {
return Criteria.empty();
}
List<Criteria> criteriaList = objectMap.entrySet().parallelStream().map(entry -> {
List<Criteria> criteriaList = objectMap.entrySet().stream().map(entry -> {
if (entry.getValue() instanceof String value) {
return Criteria.where(entry.getKey()).like(String.format("%s", value) + "%").ignoreCase(true);
} else if (entry.getValue() instanceof Collection<?> values) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,22 @@ public class LoggersController {

@GetMapping("search")
public Flux<Logger> search(LoggerRequest request, Pageable pageable) {
// Get the security details from the context
return ContextUtils.securityDetails().flatMapMany(userDetails -> {
// Set the security code to the tenant code
request.setSecurityCode(userDetails.getTenantCode());
// Return the search results
return this.loggersService.search(request, pageable);
});
}

@GetMapping("page")
public Mono<Page<Logger>> page(LoggerRequest request, Pageable pageable) {
// Get the security details from the context
return ContextUtils.securityDetails().flatMap(userDetails -> {
// Set the security code to the tenant code
request.setSecurityCode(userDetails.getTenantCode());
// Return the page of loggers
return this.loggersService.page(request, pageable);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,26 @@ public class LoggersService extends AbstractDatabase {

private final LoggersRepository loggersRepository;


public Flux<Logger> search(LoggerRequest request, Pageable pageable) {
//Create a cache key based on the given request and pageable parameters
String cacheKey = ContextUtils.cacheKey(request, pageable);
//Create a query based on the given request and pageable parameters
Query query = Query.query(request.toCriteria()).with(pageable);
//Return the query with the cache key and Logger class
return this.queryWithCache(cacheKey, query, Logger.class);
}

public Mono<Page<Logger>> page(LoggerRequest request, Pageable pageable) {
//Create a cache key based on the request
String cacheKey = ContextUtils.cacheKey(request);
//Create a query based on the request
Query query = Query.query(request.toCriteria());
//Collect a list of Loggers based on the request and pageable
var searchMono = this.search(request, pageable).collectList();
//Count the number of Loggers based on the cache key, query, and Logger class
var countMono = this.countWithCache(cacheKey, query, Logger.class);
//Return a Mono of a Page of Loggers based on the searchMono and countMono
return Mono.zip(searchMono, countMono)
.map(tuple2 -> new PageImpl<>(tuple2.getT1(), pageable, tuple2.getT2()));
}
Expand Down
3 changes: 3 additions & 0 deletions ui/web/src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import {Component} from '@angular/core';

@Component({
// Selector for the component
selector: 'app-root',
// Template URL for the component
templateUrl: './app.component.html',
// Styles URL for the component
styleUrls: ['./app.component.scss']
})
export class AppComponent {
Expand Down

0 comments on commit 08df27e

Please sign in to comment.