Skip to content
This repository has been archived by the owner on Oct 31, 2024. It is now read-only.

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
simasch committed Jun 30, 2023
2 parents f6f5cbb + 06beba3 commit ed03a34
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

<groupId>io.seventytwo.oss</groupId>
<artifactId>vaadin-jooq</artifactId>
<version>2.0.3</version>
<version>2.0.4</version>

<name>jOOQ Integration for Vaadin</name>
<name>jOOQ for Vaadin</name>
<description>Integrates Vaadin with jOOQ</description>
<url>https://github.com/simasch/vaadin-jooq</url>
<licenses>
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/io/seventytwo/vaadinjooq/util/VaadinJooqUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
import com.vaadin.flow.data.provider.SortDirection;
import org.jooq.Field;
import org.jooq.OrderField;
import org.jooq.SortField;
import org.jooq.Table;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static java.text.MessageFormat.format;
import static org.jooq.impl.DSL.field;

/**
* A utility class that helps to convert between Vaadin and jOOQ
Expand Down Expand Up @@ -39,4 +43,29 @@ public static List<OrderField<?>> orderFields(Table<?> table, Query<?, ?> query)
return orderFields;
}

/**
* Convert a Java Record to {@link OrderField}
*
* @param recordType Type of the record
* @param query The {@link Query}
* @return a {@link List} of {@link OrderField}
*/
public static List<? extends OrderField<?>> createSortOrder(Class<?> recordType, Query<?, ?> query) {

return query.getSortOrders().stream()
.map(querySortOrder -> mapFieldsOrThrowException(recordType, querySortOrder))
.collect(Collectors.toList());
}

private static SortField<?> mapFieldsOrThrowException(Class<?> recordType, QuerySortOrder querySortOrder) {
Arrays.stream(recordType.getRecordComponents())
.filter(recordComponent -> !querySortOrder.getSorted().equalsIgnoreCase(recordComponent.getName()))
.findAny()
.orElseThrow(() -> new IllegalArgumentException(format("Field {0} is not a field of {1}",
querySortOrder.getSorted(), recordType.getName())));

return querySortOrder.getDirection() == SortDirection.ASCENDING ?
field(querySortOrder.getSorted()).asc() : field(querySortOrder.getSorted()).desc();
}

}

0 comments on commit ed03a34

Please sign in to comment.