/
FormUtils.java
246 lines (223 loc) · 8.41 KB
/
FormUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package fr.redfroggy.dynamicforms.utils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
import org.apache.commons.lang3.ArrayUtils;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.validation.constraints.NotNull;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.util.*;
/**
* Form reflection utils
* Created by Michael DESIGAUD on 25/02/2016.
*/
public class FormUtils {
private static final Logger log = LoggerFactory.getLogger(FormUtils.class);
public static List<Form> forms = new ArrayList<>();
public static final String TEXT_TYPE = "text";
public static final String DATE_TYPE = "date";
public static final String OBJECT_TYPE = "object";
public static final String RELATION_TYPE = "relation";
public static final String CHECKBOX_TYPE = "checkbox";
public static final String DROPDOWN_TYPE = "dropdown";
private static final Map<Class<?>[],String> HTML_TYPES = new HashMap(){{
put(new Class[]{String.class,Integer.class},TEXT_TYPE);
put(new Class[]{Date.class,DateTime.class},DATE_TYPE);
put(new Class[]{Boolean.class},CHECKBOX_TYPE);
put(new Class[]{Enum.class},DROPDOWN_TYPE);
}};
public static String getFieldType(Field field){
String type = null;
for(Map.Entry<Class<?>[],String> entry : HTML_TYPES.entrySet()){
Class<?>[] javaTypes = entry.getKey();
if(ArrayUtils.contains(javaTypes,field.getType())){
type = entry.getValue();
break;
}
}
if(field.getType().isEnum()){
type = DROPDOWN_TYPE;
}
if(type == null){
type = OBJECT_TYPE;
}
return type;
}
/**
* Check if the given field is required
* @param formField form field
* @return true if the given field is required; false otherwise
*/
public static Boolean isMandatory(FormField formField){
if(formField != null){
return formField.getRequired();
}
return false;
}
/**
* Check if the given field is required
* @param type type
* @param name field name
* @return true if the given field is required; false otherwise
*/
/*public static Boolean isMandatory(Class<?> type, String name){
return isMandatory(getField(type,name));
}*/
/**
* Check if the field is mandatory (i.e: annotated with @NotNull or @NotEmpty or @FormRequired)
* @see @javax.validation.constraints.NotNull
* @see @javax.validation.constraints.NotEmpty
* @param field field to check
* @return true if the field is mandatory, false otherwise
*/
protected static Boolean isReflectFieldMandatory(Field field){
if(field != null){
for(Annotation annotation : field.getDeclaredAnnotations()){
if(annotation instanceof NotNull ||
annotation instanceof org.hibernate.validator.constraints.NotEmpty){
return true;
}
}
}
return false;
}
/**
* Get all reflection fields (including parent classes fields)
* @see java.lang.reflect.Field
* @param type entity type
* @return List of Field
*/
public static List<Field> getReflectionFields(Class type){
List<Field> fields = new ArrayList<>();
//Get all fields from current class and parent ones
Class currentClass = type;
while(!currentClass.equals(Object.class)){
CollectionUtils.addAll(fields, currentClass.getDeclaredFields());
currentClass = currentClass.getSuperclass();
}
return fields;
}
/**
* Get field by name
* Lookup on parent class also
* @param entityType current class to look into
* @param name name of the field to find
* @return Field if found or null otherwise
*/
public static Field getField(Class<?> entityType,String name){
List<Field> fields = getReflectionFields(entityType);
if(fields != null){
for(Field field : fields){
if(field.getName().equals(name)){
return field;
}
}
}
return null;
}
/**
* Get fields
* @param type dto type
* @param onlyExtraFields include only extra fields or no
* @return List of FormField
*/
public static List<FormField> getFields(Class<?> type,Boolean onlyExtraFields){
Boolean useOnlyExtraFields = onlyExtraFields;
if(type == null){
return new ArrayList<>();
}
if(useOnlyExtraFields == null){
useOnlyExtraFields = false;
}
Set<FormField> formFields = new HashSet<>();
if(!useOnlyExtraFields) {
for (Field field : getReflectionFields(type)) {
log.info("Reflect field: {}", field);
//Ignore static field and make sure the user has the authority to read field
// && (fieldList == null || fieldList.contains(field.getName()))
if (!Modifier.isStatic(field.getModifiers())) {
final FormField formField = new FormField();
formField.setExtrafield(false);
formField.setEntityName(type.getSimpleName());
formField.setRequired(isReflectFieldMandatory(field));
formField.setName(field.getName());
formField.setType(getFieldType(field));
formField.setLabel(null);
if (DROPDOWN_TYPE.equals(formField.getType())) {
formField.setEnumValues(new ArrayList<String>());
for (Object enumValue : field.getType().getEnumConstants()) {
formField.getEnumValues().add(((Enum) enumValue).name());
}
}
formField.setReadable(true);
formField.setWritable(true);
Boolean existingField = CollectionUtils.exists(formFields, new Predicate() {
@Override
public boolean evaluate(Object object) {
return ((FormField) object).getName().equals(formField.getName());
}
});
if (!existingField) {
formFields.add(formField);
}
}
}
}
Form form = getFormByType(type);
if(form != null){
formFields.addAll(form.getFields());
}
return new ArrayList<>(formFields);
}
public static Form getFormByType(final Class<?> entityType){
if(!forms.isEmpty()){
return (Form) CollectionUtils.find(forms, new Predicate() {
@Override
public boolean evaluate(Object o) {
Form form = (Form) o;
return form != null && form.getEntityName().toLowerCase().equals(entityType.getSimpleName().toLowerCase());
}
});
}
return null;
}
/**
* Describe entity
* @param type dto type
* @param onlyExtraFields include only extra fields or no
* @return ExtraFieldEntity
*/
public static Form describe(Class<?> type,Boolean onlyExtraFields){
if(type != null) {
Form form = new Form();
form.setEntityName(type.getSimpleName());
form.setFields(getFields(type,onlyExtraFields));
return form;
}
return null;
}
/**
* Get list generic type
* @param field current field
* @return List generic type
*/
public static Class<?> getJavaType(Field field){
if(field.getGenericType() instanceof ParameterizedType){
ParameterizedType pt = (ParameterizedType) field.getGenericType();
return (Class<?>) pt.getActualTypeArguments()[0];
}
return field.getType();
}
/**
* Check if the field is a collection type
* @param field field to check
* @return true if the field is assignable from Collection
*/
public static Boolean isCollection(Field field){
return Collection.class.isAssignableFrom(field.getType());
}
}