1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.krad.service.impl; |
17 | |
|
18 | |
import org.apache.commons.lang.StringUtils; |
19 | |
import org.kuali.rice.core.api.config.property.ConfigurationService; |
20 | |
import org.kuali.rice.core.api.exception.RiceRuntimeException; |
21 | |
import org.kuali.rice.core.api.uif.RemotableAbstractControl; |
22 | |
import org.kuali.rice.core.api.uif.RemotableAbstractWidget; |
23 | |
import org.kuali.rice.core.api.uif.RemotableAttributeField; |
24 | |
import org.kuali.rice.core.api.uif.RemotableCheckboxGroup; |
25 | |
import org.kuali.rice.core.api.uif.RemotableHiddenInput; |
26 | |
import org.kuali.rice.core.api.uif.RemotableQuickFinder; |
27 | |
import org.kuali.rice.core.api.uif.RemotableRadioButtonGroup; |
28 | |
import org.kuali.rice.core.api.uif.RemotableSelect; |
29 | |
import org.kuali.rice.core.api.uif.RemotableTextInput; |
30 | |
import org.kuali.rice.core.api.uif.RemotableTextarea; |
31 | |
import org.kuali.rice.core.api.util.KeyValue; |
32 | |
import org.kuali.rice.kim.api.KimConstants; |
33 | |
import org.kuali.rice.kns.kim.type.DataDictionaryTypeServiceHelper; |
34 | |
import org.kuali.rice.kns.lookup.LookupUtils; |
35 | |
import org.kuali.rice.kns.util.FieldUtils; |
36 | |
import org.kuali.rice.kns.web.ui.Field; |
37 | |
import org.kuali.rice.krad.bo.BusinessObject; |
38 | |
import org.kuali.rice.krad.bo.DataObjectRelationship; |
39 | |
import org.kuali.rice.krad.datadictionary.AttributeDefinition; |
40 | |
import org.kuali.rice.krad.service.DataDictionaryRemoteFieldService; |
41 | |
import org.kuali.rice.krad.service.DataDictionaryService; |
42 | |
import org.kuali.rice.krad.service.DataObjectMetaDataService; |
43 | |
import org.kuali.rice.krad.service.KRADServiceLocator; |
44 | |
import org.kuali.rice.krad.service.KRADServiceLocatorWeb; |
45 | |
import org.kuali.rice.krad.uif.control.CheckboxGroupControl; |
46 | |
import org.kuali.rice.krad.uif.control.Control; |
47 | |
import org.kuali.rice.krad.uif.control.GroupControl; |
48 | |
import org.kuali.rice.krad.uif.control.HiddenControl; |
49 | |
import org.kuali.rice.krad.uif.control.MultiValueControl; |
50 | |
import org.kuali.rice.krad.uif.control.RadioGroupControl; |
51 | |
import org.kuali.rice.krad.uif.control.SelectControl; |
52 | |
import org.kuali.rice.krad.uif.control.TextAreaControl; |
53 | |
import org.kuali.rice.krad.uif.control.TextControl; |
54 | |
import org.kuali.rice.krad.uif.control.UserControl; |
55 | |
import org.kuali.rice.krad.util.KRADConstants; |
56 | |
|
57 | |
import java.util.Collections; |
58 | |
import java.util.HashMap; |
59 | |
import java.util.List; |
60 | |
import java.util.Map; |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | 0 | public class DataDictionaryRemoteFieldServiceImpl implements DataDictionaryRemoteFieldService { |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
public RemotableAttributeField buildRemotableFieldFromAttributeDefinition(String componentClassName, |
74 | |
String attributeName) { |
75 | |
AttributeDefinition baseDefinition; |
76 | |
Class<?> componentClass; |
77 | |
|
78 | |
try { |
79 | 0 | componentClass = (Class<? extends BusinessObject>) Class.forName(componentClassName); |
80 | 0 | baseDefinition = getDataDictionaryService().getDataDictionary().getDataObjectEntry(componentClassName) |
81 | |
.getAttributeDefinition(attributeName); |
82 | 0 | } catch (ClassNotFoundException ex) { |
83 | 0 | throw new RiceRuntimeException("Unable to find attribute definition for attribute : " + attributeName); |
84 | 0 | } |
85 | |
|
86 | 0 | RemotableAttributeField.Builder definition = RemotableAttributeField.Builder.create(baseDefinition.getName()); |
87 | |
|
88 | 0 | definition.setLongLabel(baseDefinition.getLabel()); |
89 | 0 | definition.setShortLabel(baseDefinition.getShortLabel()); |
90 | 0 | definition.setMaxLength(baseDefinition.getMaxLength()); |
91 | 0 | definition.setRequired(baseDefinition.isRequired()); |
92 | 0 | definition.setForceUpperCase(baseDefinition.getForceUppercase()); |
93 | |
|
94 | 0 | RemotableAbstractControl.Builder control = createControl(baseDefinition); |
95 | 0 | if (control != null) { |
96 | 0 | definition.setControl(control); |
97 | |
} |
98 | |
|
99 | 0 | RemotableQuickFinder.Builder qf = createQuickFinder(componentClass, attributeName); |
100 | 0 | if (qf != null) { |
101 | 0 | definition.setWidgets(Collections.<RemotableAbstractWidget.Builder>singletonList(qf)); |
102 | |
} |
103 | |
|
104 | 0 | return definition.build(); |
105 | |
} |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
protected RemotableAbstractControl.Builder createControl(AttributeDefinition attr) { |
115 | 0 | Control control = attr.getControlField(); |
116 | |
|
117 | 0 | if (control != null) { |
118 | 0 | if (control instanceof CheckboxGroupControl) { |
119 | 0 | return RemotableCheckboxGroup.Builder.create(getValues(attr)); |
120 | 0 | } else if (control instanceof HiddenControl) { |
121 | 0 | return RemotableHiddenInput.Builder.create(); |
122 | 0 | } else if (control instanceof SelectControl) { |
123 | 0 | RemotableSelect.Builder b = RemotableSelect.Builder.create(getValues(attr)); |
124 | 0 | b.setMultiple(((SelectControl) control).isMultiple()); |
125 | 0 | b.setSize(((SelectControl) control).getSize()); |
126 | 0 | } else if (control instanceof RadioGroupControl) { |
127 | 0 | return RemotableRadioButtonGroup.Builder.create(getValues(attr)); |
128 | 0 | } else if (control instanceof TextControl) { |
129 | 0 | final RemotableTextInput.Builder b = RemotableTextInput.Builder.create(); |
130 | 0 | b.setSize(((TextControl) control).getSize()); |
131 | 0 | return b; |
132 | 0 | } else if (control instanceof UserControl) { |
133 | 0 | final RemotableTextInput.Builder b = RemotableTextInput.Builder.create(); |
134 | 0 | b.setSize(((UserControl) control).getSize()); |
135 | 0 | return b; |
136 | 0 | } else if (control instanceof GroupControl) { |
137 | 0 | final RemotableTextInput.Builder b = RemotableTextInput.Builder.create(); |
138 | 0 | b.setSize(((GroupControl) control).getSize()); |
139 | 0 | return b; |
140 | 0 | } else if (control instanceof TextAreaControl) { |
141 | 0 | final RemotableTextarea.Builder b = RemotableTextarea.Builder.create(); |
142 | 0 | b.setCols(((TextAreaControl) control).getCols()); |
143 | 0 | b.setRows(((TextAreaControl) control).getRows()); |
144 | 0 | return b; |
145 | |
} |
146 | |
} |
147 | |
|
148 | 0 | return null; |
149 | |
} |
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
protected Map<String, String> getValues(AttributeDefinition attr) { |
159 | 0 | Control control = attr.getControlField(); |
160 | |
|
161 | 0 | if ((control instanceof MultiValueControl) |
162 | |
&& (((MultiValueControl) control).getOptions() != null) |
163 | |
&& !((MultiValueControl) control).getOptions().isEmpty()) { |
164 | 0 | List<KeyValue> keyValues = ((MultiValueControl) control).getOptions(); |
165 | 0 | Map<String, String> options = new HashMap<String, String>(); |
166 | 0 | for (KeyValue keyValue : keyValues) { |
167 | 0 | options.put(keyValue.getKey(), keyValue.getValue()); |
168 | |
} |
169 | 0 | return options; |
170 | 0 | } else if (attr.getOptionsFinder() != null) { |
171 | 0 | return attr.getOptionsFinder().getKeyLabelMap(); |
172 | |
} |
173 | |
|
174 | 0 | return Collections.emptyMap(); |
175 | |
} |
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
protected RemotableQuickFinder.Builder createQuickFinder(Class<?> componentClass, String attributeName) { |
192 | |
Object sampleComponent; |
193 | |
try { |
194 | 0 | sampleComponent = componentClass.newInstance(); |
195 | 0 | } catch (InstantiationException e) { |
196 | 0 | throw new RiceRuntimeException(e); |
197 | 0 | } catch (IllegalAccessException e) { |
198 | 0 | throw new RiceRuntimeException(e); |
199 | 0 | } |
200 | |
|
201 | 0 | String lookupClassName = null; |
202 | 0 | Map<String, String> fieldConversions = new HashMap<String, String>(); |
203 | 0 | Map<String, String> lookupParameters = new HashMap<String, String>(); |
204 | |
|
205 | 0 | DataObjectRelationship relationship = getDataObjectMetaDataService().getDataObjectRelationship(sampleComponent, |
206 | |
componentClass, attributeName, "", true, true, false); |
207 | 0 | if (relationship != null) { |
208 | 0 | lookupClassName = relationship.getRelatedClass().getName(); |
209 | |
|
210 | 0 | for (Map.Entry<String, String> entry : relationship.getParentToChildReferences().entrySet()) { |
211 | 0 | String fromField = entry.getValue(); |
212 | 0 | String toField = entry.getKey(); |
213 | 0 | fieldConversions.put(fromField, toField); |
214 | 0 | } |
215 | |
|
216 | 0 | for (Map.Entry<String, String> entry : relationship.getParentToChildReferences().entrySet()) { |
217 | 0 | String fromField = entry.getKey(); |
218 | 0 | String toField = entry.getValue(); |
219 | |
|
220 | 0 | if (relationship.getUserVisibleIdentifierKey() == null || relationship.getUserVisibleIdentifierKey() |
221 | |
.equals(fromField)) { |
222 | 0 | lookupParameters.put(fromField, toField); |
223 | |
} |
224 | 0 | } |
225 | |
} else { |
226 | |
|
227 | 0 | String titleAttribute = getDataObjectMetaDataService().getTitleAttribute(componentClass); |
228 | 0 | if (StringUtils.equals(titleAttribute, attributeName)) { |
229 | 0 | lookupClassName = componentClass.getName(); |
230 | |
|
231 | 0 | List<String> pkAttributes = getDataObjectMetaDataService().listPrimaryKeyFieldNames(componentClass); |
232 | 0 | for (String pkAttribute : pkAttributes) { |
233 | 0 | fieldConversions.put(pkAttribute, pkAttribute); |
234 | 0 | if (!StringUtils.equals(pkAttribute, attributeName)) { |
235 | 0 | lookupParameters.put(pkAttribute, pkAttribute); |
236 | |
} |
237 | |
} |
238 | |
} |
239 | |
} |
240 | |
|
241 | 0 | if (StringUtils.isNotBlank(lookupClassName)) { |
242 | 0 | String baseUrl = getKualiConfigurationService().getPropertyValueAsString(KRADConstants.KRAD_LOOKUP_URL_KEY); |
243 | 0 | RemotableQuickFinder.Builder builder = RemotableQuickFinder.Builder.create(baseUrl, lookupClassName); |
244 | 0 | builder.setLookupParameters(lookupParameters); |
245 | 0 | builder.setFieldConversions(fieldConversions); |
246 | |
|
247 | 0 | return builder; |
248 | |
} |
249 | |
|
250 | 0 | return null; |
251 | |
} |
252 | |
|
253 | |
protected DataDictionaryService getDataDictionaryService() { |
254 | 0 | return KRADServiceLocatorWeb.getDataDictionaryService(); |
255 | |
} |
256 | |
|
257 | |
protected DataObjectMetaDataService getDataObjectMetaDataService() { |
258 | 0 | return KRADServiceLocatorWeb.getDataObjectMetaDataService(); |
259 | |
} |
260 | |
|
261 | |
protected ConfigurationService getKualiConfigurationService() { |
262 | 0 | return KRADServiceLocator.getKualiConfigurationService(); |
263 | |
} |
264 | |
} |