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