Skip to content

Commit

Permalink
Merge pull request #80 from JsonSchema-JavaUI/patch-1.0.1
Browse files Browse the repository at this point in the history
🐛 Patch 1.0.1
  • Loading branch information
SaifJerbi authored Mar 12, 2018
2 parents e300f8c + d321354 commit a3df3ff
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
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

0 comments on commit a3df3ff

Please sign in to comment.