1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.uif.control;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.kim.api.identity.Person;
20 import org.kuali.rice.kim.api.identity.PersonService;
21 import org.kuali.rice.kim.api.identity.principal.Principal;
22 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
23 import org.kuali.rice.krad.datadictionary.parse.BeanTag;
24 import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
25 import org.kuali.rice.krad.uif.field.InputField;
26 import org.kuali.rice.krad.uif.util.ScriptUtils;
27 import org.kuali.rice.krad.uif.view.View;
28 import org.kuali.rice.krad.uif.component.Component;
29 import org.kuali.rice.krad.uif.component.MethodInvokerConfig;
30 import org.kuali.rice.krad.uif.field.AttributeQuery;
31 import org.kuali.rice.krad.uif.widget.QuickFinder;
32
33 import java.util.HashMap;
34 import java.util.Map;
35
36
37
38
39
40
41
42 @BeanTag(name = "kimPersonControl-bean", parent = "Uif-KimPersonControl")
43 public class UserControl extends TextControl implements FilterableLookupCriteriaControl {
44 private static final long serialVersionUID = 7468340793076585869L;
45
46 private String principalIdPropertyName;
47 private String personNamePropertyName;
48 private String personObjectPropertyName;
49
50 public UserControl() {
51 super();
52 }
53
54
55
56
57
58 @Override
59 public void performApplyModel(View view, Object model, Component parent) {
60 super.performApplyModel(view, model, parent);
61
62 if (!(parent instanceof InputField)) {
63 return;
64 }
65
66 InputField field = (InputField) parent;
67 field.getAdditionalHiddenPropertyNames().add(principalIdPropertyName);
68
69 if (!field.isReadOnly()) {
70
71 if (StringUtils.isNotBlank(personNamePropertyName)) {
72 field.getPropertyNamesForAdditionalDisplay().add(personNamePropertyName);
73 } else {
74 field.getPropertyNamesForAdditionalDisplay().add(personObjectPropertyName + ".name");
75 }
76
77
78 String idPropertyPath = field.getBindingInfo().getPropertyAdjustedBindingPath(principalIdPropertyName);
79 String onChangeScript = "setValue('" + ScriptUtils.escapeName(idPropertyPath) + "','');";
80
81 if (StringUtils.isNotBlank(getOnChangeScript())) {
82 onChangeScript = getOnChangeScript() + onChangeScript;
83 }
84 setOnChangeScript(onChangeScript);
85 }
86
87 if (field.isReadOnly() && StringUtils.isBlank(field.getReadOnlyDisplaySuffixPropertyName())) {
88 if (StringUtils.isNotBlank(personNamePropertyName)) {
89 field.setReadOnlyDisplaySuffixPropertyName(personNamePropertyName);
90 } else {
91 field.setReadOnlyDisplaySuffixPropertyName(personObjectPropertyName + ".name");
92 }
93 }
94
95
96 AttributeQuery attributeQuery = new AttributeQuery();
97
98 MethodInvokerConfig methodInvokerConfig = new MethodInvokerConfig();
99 PersonService personService = KimApiServiceLocator.getPersonService();
100 methodInvokerConfig.setTargetObject(personService);
101
102 attributeQuery.setQueryMethodInvokerConfig(methodInvokerConfig);
103 attributeQuery.setQueryMethodToCall("getPersonByPrincipalName");
104 attributeQuery.getQueryMethodArgumentFieldList().add(field.getPropertyName());
105 attributeQuery.getReturnFieldMapping().put("principalId", principalIdPropertyName);
106
107 if (StringUtils.isNotBlank(personNamePropertyName)) {
108 attributeQuery.getReturnFieldMapping().put("name", personNamePropertyName);
109 } else {
110 attributeQuery.getReturnFieldMapping().put("name", personObjectPropertyName + ".name");
111 }
112 field.setAttributeQuery(attributeQuery);
113
114
115 QuickFinder quickFinder = field.getQuickfinder();
116 if (quickFinder.isRender()) {
117 if (StringUtils.isBlank(quickFinder.getDataObjectClassName())) {
118 quickFinder.setDataObjectClassName(Person.class.getName());
119 }
120
121 if (quickFinder.getFieldConversions().isEmpty()) {
122 quickFinder.getFieldConversions().put("principalId", principalIdPropertyName);
123
124 if (StringUtils.isNotBlank(personNamePropertyName)) {
125 quickFinder.getFieldConversions().put("name", personNamePropertyName);
126 } else {
127 quickFinder.getFieldConversions().put("name", personObjectPropertyName + ".name");
128 }
129
130 quickFinder.getFieldConversions().put("principalName", field.getPropertyName());
131 }
132 }
133 }
134
135
136
137
138
139
140 @BeanTagAttribute(name="principalIdPropertyName")
141 public String getPrincipalIdPropertyName() {
142 return principalIdPropertyName;
143 }
144
145
146
147
148
149
150 public void setPrincipalIdPropertyName(String principalIdPropertyName) {
151 this.principalIdPropertyName = principalIdPropertyName;
152 }
153
154
155
156
157
158
159 @BeanTagAttribute(name="personNamePropertyName")
160 public String getPersonNamePropertyName() {
161 return personNamePropertyName;
162 }
163
164
165
166
167
168
169 public void setPersonNamePropertyName(String personNamePropertyName) {
170 this.personNamePropertyName = personNamePropertyName;
171 }
172
173
174
175
176
177
178 @BeanTagAttribute(name="personObjectPropertyName")
179 public String getPersonObjectPropertyName() {
180 return personObjectPropertyName;
181 }
182
183
184
185
186
187
188 public void setPersonObjectPropertyName(String personObjectPropertyName) {
189 this.personObjectPropertyName = personObjectPropertyName;
190 }
191
192
193
194
195 @Override
196 public Map<String, String> filterSearchCriteria(String propertyName, Map<String, String> searchCriteria) {
197 Map<String, String> filteredSearchCriteria = new HashMap<String, String>(searchCriteria);
198
199
200
201
202 String principalName = searchCriteria.get(propertyName);
203 if (StringUtils.isNotBlank(principalName)) {
204 Principal principal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(principalName);
205 if (principal == null) {
206 return null;
207 } else {
208 filteredSearchCriteria.put(principalIdPropertyName, principal.getPrincipalId());
209 }
210 }
211
212
213 filteredSearchCriteria.remove(propertyName);
214 filteredSearchCriteria.remove(personNamePropertyName);
215
216 return filteredSearchCriteria;
217 }
218 }