1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.uif.field;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.apache.log4j.Logger;
20 import org.kuali.rice.kim.api.role.Role;
21 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
22 import org.kuali.rice.krad.datadictionary.parse.BeanTag;
23 import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
24 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
25 import org.kuali.rice.krad.service.ModuleService;
26 import org.kuali.rice.krad.uif.UifConstants;
27 import org.kuali.rice.krad.uif.UifParameters;
28 import org.kuali.rice.krad.uif.util.LifecycleElement;
29 import org.kuali.rice.krad.util.UrlFactory;
30
31 import java.util.Map;
32 import java.util.Properties;
33
34
35
36
37
38
39 @BeanTag(name = "roleLinkField", parent = "Uif-RoleLinkField")
40 public class RoleLinkField extends LinkField {
41 private static final long serialVersionUID = -7175947391547723712L;
42 private static final Logger LOG = Logger.getLogger(RoleLinkField.class);
43
44 private String roleId;
45 private String roleName;
46 private String roleNamespaceCode;
47 private String dataObjectClassName;
48 private String baseInquiryUrl;
49 private boolean namespaceInLinkText;
50 private boolean disableLink;
51 private Map<String, String> additionalInquiryParameters;
52
53
54
55
56
57
58
59
60 @Override
61 public void performFinalize(Object model, LifecycleElement parent) {
62 super.performFinalize(model, parent);
63
64 if (StringUtils.isNotBlank(roleId)) {
65 setHref(buildInquiryUrl());
66 setLinkText(getRoleNameByRoleId(roleId));
67 } else {
68 if (StringUtils.isNotBlank(roleName) && StringUtils.isNotBlank(roleNamespaceCode) ) {
69 setHref(buildInquiryUrl());
70 setLinkText(getRoleNameByNamespaceAndName(roleNamespaceCode, roleName));
71 }
72
73 if (StringUtils.isBlank(roleName) && StringUtils.isNotBlank(roleNamespaceCode) ) {
74 setDisableLink(true);
75
76 if (isNamespaceInLinkText()){
77 setLinkText(roleNamespaceCode);
78 }
79 }
80
81 if (StringUtils.isNotBlank(roleName) && StringUtils.isBlank(roleNamespaceCode) ) {
82 setDisableLink(true);
83 setLinkText(roleName);
84 }
85 }
86
87 if (StringUtils.isBlank(getHref())) {
88 setDisableLink(true);
89 }
90
91 if (StringUtils.isBlank(getLinkText())){
92 setLinkText(UifConstants.KimLink.BLANK_LINK);
93 }
94 }
95
96 protected String buildInquiryUrl() {
97 Class<?> inquiryObjectClass;
98 try {
99 inquiryObjectClass = Class.forName(getDataObjectClassName());
100 } catch (ClassNotFoundException e) {
101 LOG.error("Unable to get class for: " + getDataObjectClassName());
102 throw new RuntimeException(e);
103 }
104
105 Properties urlParameters = new Properties();
106 urlParameters.setProperty(UifParameters.DATA_OBJECT_CLASS_NAME, inquiryObjectClass.getName());
107 urlParameters.setProperty(UifParameters.METHOD_TO_CALL, UifConstants.MethodToCallNames.START);
108
109 if (StringUtils.isNotBlank(roleId)) {
110 urlParameters.setProperty(UifConstants.KimLink.ROLE_ID, roleId);
111 } else {
112 if (StringUtils.isNotBlank(roleName)) {
113 urlParameters.setProperty(UifConstants.KimLink.ROLE_NAME, roleName);
114 urlParameters.setProperty(UifConstants.KimLink.ROLE_NAMESPACE_CODE, roleNamespaceCode);
115 }
116 }
117
118 for (Map.Entry<String, String> inquiryParameter : additionalInquiryParameters.entrySet()) {
119
120 urlParameters.setProperty(inquiryParameter.getKey(), inquiryParameter.getValue());
121 }
122
123
124 String inquiryUrl;
125
126
127 ModuleService responsibleModuleService =
128 KRADServiceLocatorWeb.getKualiModuleService().getResponsibleModuleService(inquiryObjectClass);
129 if (responsibleModuleService != null && responsibleModuleService.isExternalizable(inquiryObjectClass)) {
130 inquiryUrl = responsibleModuleService.getExternalizableDataObjectInquiryUrl(inquiryObjectClass,
131 urlParameters);
132 } else {
133 inquiryUrl = UrlFactory.parameterizeUrl(getBaseInquiryUrl(), urlParameters);
134 }
135
136 return inquiryUrl;
137 }
138
139 protected String getRoleNameByRoleId(String roleId) {
140 Role role = KimApiServiceLocator.getRoleService().getRole(roleId);
141 if (role == null) {
142
143 setDisableLink(true);
144 return roleId;
145 }
146
147 if (isNamespaceInLinkText()){
148 return role.getNamespaceCode() + " " + role.getName();
149 }
150
151 return role.getName();
152 }
153
154 protected String getRoleNameByNamespaceAndName(String namespaceCode, String roleName) {
155 Role role = KimApiServiceLocator.getRoleService().getRoleByNamespaceCodeAndName(namespaceCode, roleName);
156 if (role == null) {
157
158 setDisableLink(true);
159
160 if (isNamespaceInLinkText()){
161 return this.roleNamespaceCode + " " + this.roleName;
162 } else {
163 return this.roleName;
164 }
165 }
166
167 if (isNamespaceInLinkText()){
168 return role.getNamespaceCode() + " " + role.getName();
169 }
170
171 return role.getName();
172 }
173
174
175
176
177
178
179 @BeanTagAttribute
180 public String getRoleName() {
181 return roleName;
182 }
183
184 public void setRoleName(String roleName) {
185 this.roleName = roleName;
186 }
187
188
189
190
191
192
193 @BeanTagAttribute
194 public String getRoleNamespaceCode() {
195 return roleNamespaceCode;
196 }
197
198 public void setRoleNamespaceCode(String roleNamespaceCode) {
199 this.roleNamespaceCode = roleNamespaceCode;
200 }
201
202
203
204
205
206
207 @BeanTagAttribute
208 public String getRoleId() {
209 return roleId;
210 }
211
212 public void setRoleId(String roleId) {
213 this.roleId = roleId;
214 }
215
216
217
218
219
220
221 @BeanTagAttribute
222 public String getDataObjectClassName() {
223 return dataObjectClassName;
224 }
225
226 public void setDataObjectClassName(String dataObjectClassName) {
227 this.dataObjectClassName = dataObjectClassName;
228 }
229
230
231
232
233
234
235 @BeanTagAttribute
236 public String getBaseInquiryUrl() {
237 return baseInquiryUrl;
238 }
239
240 public void setBaseInquiryUrl(String baseInquiryUrl) {
241 this.baseInquiryUrl = baseInquiryUrl;
242 }
243
244
245
246
247
248
249 @BeanTagAttribute
250 public boolean isNamespaceInLinkText() {
251 return namespaceInLinkText;
252 }
253
254 public void setNamespaceInLinkText(boolean namespaceInLinkText) {
255 this.namespaceInLinkText = namespaceInLinkText;
256 }
257
258
259
260
261
262
263 @BeanTagAttribute
264 public boolean isDisableLink() {
265 return disableLink;
266 }
267
268 public void setDisableLink(boolean disableLink) {
269 this.disableLink = disableLink;
270 }
271
272
273
274
275
276
277
278
279
280
281
282
283
284 @BeanTagAttribute
285 public Map<String, String> getAdditionalInquiryParameters() {
286 return additionalInquiryParameters;
287 }
288
289 public void setAdditionalInquiryParameters(Map<String, String> additionalInquiryParameters) {
290 this.additionalInquiryParameters = additionalInquiryParameters;
291 }
292 }