Skip to content

🐛 Patch 1.0.1 #80

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

Merged
merged 3 commits into from
Mar 12, 2018
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>io.sfjava.ui</groupId>
<artifactId>sf-java-ui</artifactId>
<version>1.0.1-SNAPSHOT</version>
<version>1.0.2-SNAPSHOT</version>
<packaging>jar</packaging>

<name>sf-java-ui</name>
Expand Down
19 changes: 12 additions & 7 deletions src/main/java/io/asfjava/ui/core/schema/UiFormSchemaGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
Expand All @@ -12,9 +13,12 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import org.reflections.ReflectionUtils;

import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -43,7 +47,8 @@ public final class UiFormSchemaGenerator {
private static UiFormSchemaGenerator instance;

public UiForm generate(Class<? extends Serializable> formDto) throws JsonMappingException {
Field[] declaredFields = formDto.getDeclaredFields();
Set<Field> declaredFields = ReflectionUtils.getAllFields(formDto,
field -> !Modifier.isStatic(field.getModifiers()));
ObjectMapper mapper = new ObjectMapper();

JsonSchemaGenerator schemaGen = initSchemaGen(mapper);
Expand Down Expand Up @@ -105,13 +110,13 @@ private ObjectNode buildActionNode(ObjectMapper mapper, Action action) {
return node;
}

private ObjectNode handlerGroupedFields(ObjectMapper mapper, Field[] declaredFields,
private ObjectNode handlerGroupedFields(ObjectMapper mapper, Set<Field> declaredFields,
Map<Field, JsonNode> sortedNodes) {
Predicate<? super Field> checkFieldSetAnnotation = field -> field.isAnnotationPresent(FieldSet.class);

Map<String, List<JsonNode>> groupedFields = new LinkedHashMap<>();

Arrays.stream(declaredFields).filter(checkFieldSetAnnotation)
declaredFields.stream().filter(checkFieldSetAnnotation)
.forEach(field -> groupFieldsByTab(sortedNodes, field, groupedFields));

ArrayNode groups = mapper.createArrayNode();
Expand All @@ -123,7 +128,7 @@ private ObjectNode handlerGroupedFields(ObjectMapper mapper, Field[] declaredFie

}

private ObjectNode handleTabbedFields(ObjectMapper mapper, Field[] declaredFields, Map<Field, JsonNode> nodes) {
private ObjectNode handleTabbedFields(ObjectMapper mapper, Set<Field> declaredFields, Map<Field, JsonNode> nodes) {
Predicate<? super Field> checkTabAnnotation = field -> field.isAnnotationPresent(Tab.class);

Comparator<? super Field> tabIndexComparator = (field1, field2) -> Integer
Expand All @@ -138,7 +143,7 @@ private ObjectNode handleTabbedFields(ObjectMapper mapper, Field[] declaredField

Map<String, List<JsonNode>> groupedFieldsByTab = new LinkedHashMap<>();

Arrays.stream(declaredFields).filter(checkTabAnnotation).sorted(fieldIndexComparator).sorted(tabIndexComparator)
declaredFields.stream().filter(checkTabAnnotation).sorted(fieldIndexComparator).sorted(tabIndexComparator)
.forEach(field -> groupFieldsByTab(nodes, field, groupedFieldsByTab));

ArrayNode tabs = mapper.createArrayNode();
Expand All @@ -161,10 +166,10 @@ private ObjectNode handleTabbedFields(ObjectMapper mapper, Field[] declaredField

}

private Map<Field, JsonNode> initFieldsFormDefinition(ObjectMapper mapper, Field[] declaredFields) {
private Map<Field, JsonNode> initFieldsFormDefinition(ObjectMapper mapper, Set<Field> declaredFields) {
Map<Field, JsonNode> nodes = new HashMap<>();

Arrays.stream(declaredFields).forEach(field -> buildFormDefinition(nodes, mapper, field));
declaredFields.forEach(field -> buildFormDefinition(nodes, mapper, field));

return nodes;
}
Expand Down