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