Skip to content

Commit 2959ba2

Browse files
committed
adding allfieldsDisplay support #75
cleaner solution
1 parent 99b832c commit 2959ba2

File tree

6 files changed

+99
-20
lines changed

6 files changed

+99
-20
lines changed

demo/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
<dependency>
3939
<groupId>io.sfjava.ui</groupId>
4040
<artifactId>sf-java-ui</artifactId>
41-
<version>1.0.0</version>
41+
<version>1.1.0-SNAPSHOT</version>
4242
</dependency>
4343
</dependencies>
4444

demo/src/main/java/io/asfjava/ui/demo/renderer/UiDemoRenderer.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66

77
import io.asfjava.ui.core.schema.UiFormSchemaGenerator;
88
import io.asfjava.ui.demo.screen.DemoForm;
9+
import io.asfjava.ui.demo.screen.dto.Event;
910
import io.asfjava.ui.dto.UiForm;
1011

1112
@Service
1213
public class UiDemoRenderer {
1314
public UiForm renderForm() throws JsonMappingException {
14-
return UiFormSchemaGenerator.get().generate(DemoForm.class);
15+
return UiFormSchemaGenerator.get().generate(Event.class);
1516
}
1617
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package io.asfjava.ui.demo.screen.dto;
2+
3+
import java.io.Serializable;
4+
5+
import io.asfjava.ui.core.form.DisplayAllFields;
6+
import io.asfjava.ui.core.form.Index;
7+
import io.asfjava.ui.core.form.TextField;
8+
9+
@DisplayAllFields
10+
public class Event implements Serializable {
11+
12+
@Index(0)
13+
@TextField(title = "Title", placeHolder = "Event's title")
14+
private String title;
15+
16+
@Index(2)
17+
@TextField(title = "Location", placeHolder = "Event's location")
18+
private String location;
19+
20+
@Index(1)
21+
private Speaker speaker;
22+
23+
public String getTitle() {
24+
return title;
25+
}
26+
27+
public void setTitle(String title) {
28+
this.title = title;
29+
}
30+
31+
public String getLocation() {
32+
return location;
33+
}
34+
35+
public void setLocation(String location) {
36+
this.location = location;
37+
}
38+
39+
public Speaker getSpeaker() {
40+
return speaker;
41+
}
42+
43+
public void setSpeaker(Speaker speaker) {
44+
this.speaker = speaker;
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package io.asfjava.ui.demo.screen.dto;
2+
3+
public class Speaker {
4+
private String fullName;
5+
private String nationality;
6+
private String description;
7+
8+
public String getFullName() {
9+
return fullName;
10+
}
11+
12+
public void setFullName(String fullName) {
13+
this.fullName = fullName;
14+
}
15+
16+
public String getNationality() {
17+
return nationality;
18+
}
19+
20+
public void setNationality(String nationality) {
21+
this.nationality = nationality;
22+
}
23+
24+
public String getDescription() {
25+
return description;
26+
}
27+
28+
public void setDescription(String description) {
29+
this.description = description;
30+
}
31+
}

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
<groupId>io.sfjava.ui</groupId>
55
<artifactId>sf-java-ui</artifactId>
6-
<version>1.0.1-SNAPSHOT</version>
6+
<version>1.1.0-SNAPSHOT</version>
77
<packaging>jar</packaging>
88

99
<name>sf-java-ui</name>

src/main/java/io/asfjava/ui/core/schema/UiFormSchemaGenerator.java

+18-17
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import java.util.Map.Entry;
1414
import java.util.Optional;
1515
import java.util.function.Predicate;
16-
import java.util.stream.Collectors;
1716

1817
import com.fasterxml.jackson.annotation.JsonIgnore;
1918
import com.fasterxml.jackson.databind.JsonMappingException;
@@ -62,25 +61,21 @@ public UiForm generate(Class<? extends Serializable> formDto) throws JsonMapping
6261

6362
ArrayNode formDefinition = mapper.createArrayNode();
6463
tabbedFields.ifPresent(formDefinition::add);
65-
sortedNodes.entrySet().stream().forEach(nodesElement -> formDefinition.add(nodesElement.getValue()));
66-
67-
if (formDto.getDeclaredAnnotation(DisplayAllFields.class) != null) {
68-
// check if fields is not handled and haven't jackson json ignore annotation
69-
List<Field> unannotatedFiedls = Arrays.stream(declaredFields)
70-
.filter(field -> !nodes.containsKey(field) && !field.isAnnotationPresent(JsonIgnore.class))
71-
.collect(Collectors.toList());
72-
handleUnAnnotatedFields(formDefinition, unannotatedFiedls);
73-
}
64+
65+
sortedNodes.entrySet().stream().forEach(nodesElement -> {
66+
if (nodesElement.getValue() != null) {
67+
formDefinition.add(nodesElement.getValue());
68+
} else if (formDto.getDeclaredAnnotation(DisplayAllFields.class) != null) {
69+
// if no definition it must be added as key to the form definition
70+
formDefinition.add(nodesElement.getKey().getName());
71+
}
72+
});
7473

7574
handleActionsAnnotation(mapper, formDto, formDefinition);
7675

7776
return new UiForm(schema, formDefinition);
7877
}
7978

80-
private void handleUnAnnotatedFields(ArrayNode formDefinition, List<Field> unannotatedFiedls) {
81-
unannotatedFiedls.stream().forEach(field -> formDefinition.add(field.getName()));
82-
}
83-
8479
private void handleActionsAnnotation(ObjectMapper mapper, Class<? extends Serializable> formDto,
8580
ArrayNode formDefinition) {
8681
ObjectNode groupedActionsNode = mapper.createObjectNode();
@@ -180,6 +175,12 @@ private Map<Field, JsonNode> initFieldsFormDefinition(ObjectMapper mapper, Field
180175

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

178+
// check if fields is not handled and haven't jackson json ignore annotation it
179+
// must be added
180+
Arrays.stream(declaredFields)
181+
.filter(field -> !nodes.containsKey(field) && !field.isAnnotationPresent(JsonIgnore.class))
182+
.forEach(field -> nodes.put(field, null));
183+
183184
return nodes;
184185
}
185186

@@ -229,9 +230,9 @@ private Map<Field, JsonNode> reorderFieldsBasedOnIndex(Map<Field, JsonNode> node
229230
field2Index != null ? field2Index.value() : Integer.MAX_VALUE);
230231
};
231232

232-
return nodes.entrySet().stream().sorted(tabIndexComparator).collect(Collectors.toMap(Map.Entry::getKey,
233-
Map.Entry::getValue, (oldValue, newValue) -> oldValue, LinkedHashMap::new));
234-
233+
return nodes.entrySet().stream().sorted(tabIndexComparator).collect(LinkedHashMap::new,
234+
(result, currentElement) -> result.put(currentElement.getKey(), currentElement.getValue()),
235+
Map::putAll);
235236
}
236237

237238
public static UiFormSchemaGenerator get() {

0 commit comments

Comments
 (0)