1 package com.silvermindsoftware.hitch.config;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import com.silvermindsoftware.hitch.Binder;
20 import com.silvermindsoftware.hitch.BinderManager;
21 import com.silvermindsoftware.hitch.ReadOnly;
22 import com.silvermindsoftware.hitch.meta.FormMeta;
23 import com.silvermindsoftware.hitch.reflect.ClassManager;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import java.lang.reflect.Field;
28 import java.lang.reflect.Method;
29 import java.util.Arrays;
30 import java.util.List;
31 import java.util.Map;
32
33
34
35
36 public class BinderConfig {
37
38 private static final Logger log = LoggerFactory.getLogger(BinderConfig.class);
39 private FormMeta formMeta;
40 private FormConfig formConfig;
41
42 public BinderConfig(Class componentClass, Binder binder) {
43 this(componentClass, binder, new FormConfig());
44 }
45
46 public BinderConfig(Class componentClass, Binder binder, FormConfig formConfig) {
47 this.formMeta = BinderManager.getFormMetaData(componentClass);
48 this.formConfig = formConfig;
49 }
50
51 public BinderConfig bindModel(ModelObjectConfig modelObjectConfig) {
52
53 bindCompleteModel(
54 modelObjectConfig.getModelField(),
55 modelObjectConfig.getModelField(),
56 modelObjectConfig.isDefault(),
57 modelObjectConfig.isAutoBind(),
58 modelObjectConfig.getIgnoreFields());
59
60 return this;
61 }
62
63
64
65
66
67 public BinderConfig bindDefaultModel(String modelField, String... ignoreFields) {
68 return bindCompleteModel("[default]", modelField, true, true, ignoreFields);
69 }
70
71
72
73
74
75
76 public BinderConfig bindModel(String modelField, String... ignoreFields) {
77 return bindCompleteModel(modelField, modelField, false, true, ignoreFields);
78 }
79
80
81
82
83 public BinderConfig bindDefaultModel(String modelField) {
84 return bindCompleteModel("[default]", modelField, true, true);
85 }
86
87
88
89
90
91 public BinderConfig bindModel(String modelField) {
92 return bindCompleteModel(modelField, modelField, false, true);
93 }
94
95 private BinderConfig bindCompleteModel(String modelFieldName, String modelField, boolean defaultModel, boolean autoBindModelObject, String... ignoreFields) {
96 formMeta.putModelMeta(modelFieldName, modelField);
97 autoBind(modelField, defaultModel, autoBindModelObject, ignoreFields);
98 return this;
99 }
100
101
102 private void autoBind(String modelField, boolean defaultModel, boolean autoBindModelObject, String... ignoreFields) {
103
104 List ignoreFieldsList = Arrays.asList(ignoreFields);
105
106 if (formConfig.isAutoBind() && autoBindModelObject) {
107
108 Class formClass = formMeta.getComponentClass();
109
110 Field field = null;
111 try {
112 field = ClassManager.getClassInfo(formClass).getField(modelField);
113 } catch (NoSuchFieldException e) {
114 log.error("Error while getting field " + modelField);
115 return;
116 }
117
118
119 Class modelObjectType = field.getType();
120
121 if (!Map.class.isAssignableFrom(modelObjectType)) {
122 for (Method moMethod : ClassManager.getClassInfo(modelObjectType).getSetters()) {
123 String componentFieldName =
124 moMethod.getName().substring(3, 4).toLowerCase() +
125 moMethod.getName().substring(4);
126
127 Field formClassField = null;
128
129 try {
130 formClassField = ClassManager.getClassInfo(formClass).getField(componentFieldName);
131
132 if (!ignoreFieldsList.contains(formClassField.getName())) {
133
134 formMeta.addComponentMeta(
135 defaultModel ? "[default]" : field.getName(),
136 componentFieldName, formClassField, void.class,
137 new String[]{}, true, ReadOnly.DEFAULT, moMethod.getParameterTypes()[0]);
138
139 }
140
141 } catch (NoSuchFieldException e) {
142
143 }
144 }
145 }
146 }
147 }
148
149
150
151
152 public BinderConfig bindComponentToDefault(BoundComponentConfig boundComponentConfig) {
153 formMeta.addComponentMeta("[default]", boundComponentConfig, false);
154 return this;
155 }
156
157
158
159
160 public BinderConfig bindComponentToDefault(BoundComponentConfig... boundComponentConfigs) {
161 for (BoundComponentConfig boundComponentConfig : boundComponentConfigs) {
162 formMeta.addComponentMeta("[default]", boundComponentConfig, false);
163 }
164 return this;
165 }
166
167
168
169
170
171 public BinderConfig bindComponent(String modelId, BoundComponentConfig boundComponentConfig) {
172 formMeta.addComponentMeta(modelId, boundComponentConfig, false);
173 return this;
174 }
175
176
177
178
179
180 public BinderConfig bindComponent(String modelId, BoundComponentConfig... boundComponentConfigs) {
181 for (BoundComponentConfig boundComponentConfig : boundComponentConfigs) {
182 formMeta.addComponentMeta(modelId, boundComponentConfig, false);
183 }
184 return this;
185 }
186
187 }