1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.kns.uif.util; |
17 | |
|
18 | |
import java.security.GeneralSecurityException; |
19 | |
import java.util.Map; |
20 | |
|
21 | |
import javax.servlet.http.HttpServletRequest; |
22 | |
|
23 | |
import org.apache.commons.lang.StringUtils; |
24 | |
import org.kuali.rice.core.api.encryption.EncryptionService; |
25 | |
import org.kuali.rice.core.api.services.CoreApiServiceLocator; |
26 | |
import org.kuali.rice.core.web.format.Formatter; |
27 | |
import org.kuali.rice.kns.datadictionary.AttributeDefinition; |
28 | |
import org.kuali.rice.kns.service.KNSServiceLocator; |
29 | |
import org.kuali.rice.kns.service.KNSServiceLocatorWeb; |
30 | |
import org.kuali.rice.kns.uif.control.CheckboxControl; |
31 | |
import org.kuali.rice.kns.uif.control.Control; |
32 | |
import org.kuali.rice.kns.uif.control.RadioGroupControl; |
33 | |
import org.kuali.rice.kns.uif.control.TextAreaControl; |
34 | |
import org.kuali.rice.kns.uif.control.TextControl; |
35 | |
import org.kuali.rice.kns.uif.field.AttributeField; |
36 | |
import org.kuali.rice.kns.util.KNSConstants; |
37 | |
import org.kuali.rice.kns.web.spring.form.UifFormBase; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | 0 | public class LookupInquiryUtils { |
45 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(LookupInquiryUtils.class); |
46 | |
|
47 | |
public static void initializeAttributeFieldFromAttributeDefinition(AttributeField attributeField, |
48 | |
AttributeDefinition attributeDefinition) { |
49 | |
|
50 | 0 | if (StringUtils.isEmpty(attributeField.getLabel())) { |
51 | 0 | attributeField.setLabel(attributeDefinition.getLabel()); |
52 | |
} |
53 | |
|
54 | |
|
55 | 0 | if (StringUtils.isEmpty(attributeField.getShortLabel())) { |
56 | 0 | attributeField.setShortLabel(attributeDefinition.getShortLabel()); |
57 | |
} |
58 | |
|
59 | |
|
60 | 0 | if (attributeField.getAttributeSecurity() == null) { |
61 | 0 | attributeField.setAttributeSecurity(attributeDefinition.getAttributeSecurity()); |
62 | |
} |
63 | |
|
64 | |
|
65 | 0 | setupControlFromAttributeDefinition(attributeField, attributeDefinition); |
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 | 0 | } |
98 | |
|
99 | |
protected static void setupControlFromAttributeDefinition(AttributeField attributeField, |
100 | |
AttributeDefinition attributeDefinition) { |
101 | 0 | if (attributeField.getControl() == null) { |
102 | 0 | attributeField.setControl(convertControlToLookupControl(attributeDefinition)); |
103 | |
} |
104 | 0 | } |
105 | |
|
106 | |
protected static Control convertControlToLookupControl(AttributeDefinition attributeDefinition) { |
107 | 0 | Control newControl = null; |
108 | 0 | if (attributeDefinition.getControlField() == null) { |
109 | 0 | return null; |
110 | |
} |
111 | |
|
112 | 0 | if (CheckboxControl.class.isAssignableFrom(attributeDefinition.getControlField().getClass())) { |
113 | 0 | newControl = generateCustomLookupControlFromExisting(RadioGroupControl.class, |
114 | |
attributeDefinition.getControlField()); |
115 | |
} |
116 | 0 | else if (TextAreaControl.class.isAssignableFrom(attributeDefinition.getControlField().getClass())) { |
117 | 0 | newControl = generateCustomLookupControlFromExisting(TextControl.class, |
118 | |
attributeDefinition.getControlField()); |
119 | |
} |
120 | |
else { |
121 | 0 | newControl = ComponentUtils.copy(attributeDefinition.getControlField()); |
122 | |
} |
123 | 0 | return newControl; |
124 | |
} |
125 | |
|
126 | |
public static Control generateCustomLookupControlFromExisting(Class<? extends Control> controlClass, |
127 | |
Control existingControl) { |
128 | |
try { |
129 | 0 | Control newControl = controlClass.newInstance(); |
130 | 0 | for (String propertyPath : ComponentUtils.getComponentPropertyNames(existingControl.getClass())) { |
131 | 0 | Object propValue = ObjectPropertyUtils.getPropertyValue(existingControl, propertyPath); |
132 | 0 | if (propValue != null) { |
133 | 0 | ObjectPropertyUtils.setPropertyValue(newControl, propertyPath, propValue, true); |
134 | |
} |
135 | 0 | } |
136 | 0 | return newControl; |
137 | |
} |
138 | 0 | catch (InstantiationException e) { |
139 | 0 | LOG.error("Exception instantiating new instance of class '" + controlClass.getClass() + "'", e); |
140 | 0 | throw new RuntimeException(e); |
141 | |
} |
142 | 0 | catch (IllegalAccessException e) { |
143 | 0 | LOG.error("Exception instantiating new instance of class '" + controlClass.getClass() + "'", e); |
144 | 0 | throw new RuntimeException(e); |
145 | |
} |
146 | |
} |
147 | |
|
148 | |
public static String retrieveLookupParameterValue(UifFormBase form, HttpServletRequest request, |
149 | |
Class<?> lookupObjectClass, String propertyName, String propertyValueName) { |
150 | 0 | String parameterValue = ""; |
151 | |
|
152 | |
|
153 | 0 | if (StringUtils.contains(propertyValueName, "'")) { |
154 | 0 | parameterValue = StringUtils.replace(propertyValueName, "'", ""); |
155 | |
} |
156 | 0 | else if (parameterValue.startsWith(KNSConstants.LOOKUP_PARAMETER_LITERAL_PREFIX |
157 | |
+ KNSConstants.LOOKUP_PARAMETER_LITERAL_DELIMITER)) { |
158 | 0 | parameterValue = StringUtils.removeStart(parameterValue, KNSConstants.LOOKUP_PARAMETER_LITERAL_PREFIX |
159 | |
+ KNSConstants.LOOKUP_PARAMETER_LITERAL_DELIMITER); |
160 | |
} |
161 | |
|
162 | 0 | else if (request.getParameterMap().containsKey(propertyValueName)) { |
163 | 0 | parameterValue = request.getParameter(propertyValueName); |
164 | |
} |
165 | |
|
166 | |
else { |
167 | 0 | Object value = ObjectPropertyUtils.getPropertyValue(form, propertyValueName); |
168 | 0 | if (value != null) { |
169 | 0 | if (value instanceof String) { |
170 | 0 | parameterValue = (String) value; |
171 | |
} |
172 | |
|
173 | 0 | Formatter formatter = Formatter.getFormatter(value.getClass()); |
174 | 0 | parameterValue = (String) formatter.format(value); |
175 | |
} |
176 | |
} |
177 | |
|
178 | 0 | if (parameterValue != null |
179 | |
&& lookupObjectClass != null |
180 | |
&& KNSServiceLocatorWeb.getBusinessObjectAuthorizationService() |
181 | |
.attributeValueNeedsToBeEncryptedOnFormsAndLinks(lookupObjectClass, propertyName)) { |
182 | |
try { |
183 | 0 | parameterValue = CoreApiServiceLocator.getEncryptionService().encrypt(parameterValue) |
184 | |
+ EncryptionService.ENCRYPTION_POST_PREFIX; |
185 | |
} |
186 | 0 | catch (GeneralSecurityException e) { |
187 | 0 | LOG.error("Unable to encrypt value for property name: " + propertyName); |
188 | 0 | throw new RuntimeException(e); |
189 | 0 | } |
190 | |
} |
191 | |
|
192 | 0 | return parameterValue; |
193 | |
} |
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
public static String getTitleText(String prependText, Class<?> element, Map<String, String> keyValueMap) { |
209 | 0 | StringBuffer titleText = new StringBuffer(prependText); |
210 | 0 | for (String key : keyValueMap.keySet()) { |
211 | 0 | String fieldVal = keyValueMap.get(key).toString(); |
212 | |
|
213 | 0 | titleText.append(KNSServiceLocatorWeb.getDataDictionaryService().getAttributeLabel(element, key) + "=" |
214 | |
+ fieldVal.toString() + " "); |
215 | 0 | } |
216 | |
|
217 | 0 | return titleText.toString(); |
218 | |
} |
219 | |
|
220 | |
} |