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