|  1 |     | 
     | 
  |  2 |     | 
     | 
  |  3 |     | 
     | 
  |  4 |     | 
     | 
  |  5 |     | 
     | 
  |  6 |     | 
     | 
  |  7 |     | 
     | 
  |  8 |     | 
     | 
  |  9 |     | 
     | 
  |  10 |     | 
     | 
  |  11 |     | 
     | 
  |  12 |     | 
     | 
  |  13 |     | 
     | 
  |  14 |     | 
     | 
  |  15 |     | 
     | 
  |  16 |     | 
   package org.kuali.rice.kim.service.support.impl;  | 
  |  17 |     | 
     | 
  |  18 |     | 
   import java.beans.PropertyDescriptor;  | 
  |  19 |     | 
   import java.lang.reflect.Method;  | 
  |  20 |     | 
   import java.lang.reflect.Modifier;  | 
  |  21 |     | 
   import java.math.BigDecimal;  | 
  |  22 |     | 
   import java.util.ArrayList;  | 
  |  23 |     | 
   import java.util.HashMap;  | 
  |  24 |     | 
   import java.util.Iterator;  | 
  |  25 |     | 
   import java.util.List;  | 
  |  26 |     | 
   import java.util.Map;  | 
  |  27 |     | 
   import java.util.Set;  | 
  |  28 |     | 
   import java.util.regex.Pattern;  | 
  |  29 |     | 
     | 
  |  30 |     | 
   import org.apache.commons.beanutils.PropertyUtils;  | 
  |  31 |     | 
   import org.apache.commons.lang.StringUtils;  | 
  |  32 |     | 
   import org.kuali.rice.core.util.ClassLoaderUtils;  | 
  |  33 |     | 
   import org.kuali.rice.core.util.KeyValue;  | 
  |  34 |     | 
   import org.kuali.rice.core.util.RiceKeyConstants;  | 
  |  35 |     | 
   import org.kuali.rice.core.util.type.TypeUtils;  | 
  |  36 |     | 
   import org.kuali.rice.core.web.format.Formatter;  | 
  |  37 |     | 
   import org.kuali.rice.core.xml.dto.AttributeSet;  | 
  |  38 |     | 
   import org.kuali.rice.kew.doctype.bo.DocumentType;  | 
  |  39 |     | 
   import org.kuali.rice.kim.bo.types.dto.AttributeDefinitionMap;  | 
  |  40 |     | 
   import org.kuali.rice.kim.bo.types.dto.KimTypeAttributeInfo;  | 
  |  41 |     | 
   import org.kuali.rice.kim.bo.types.dto.KimTypeInfo;  | 
  |  42 |     | 
   import org.kuali.rice.kim.service.KIMServiceLocatorWeb;  | 
  |  43 |     | 
   import org.kuali.rice.kim.service.KimTypeInfoService;  | 
  |  44 |     | 
   import org.kuali.rice.kim.service.support.KimTypeService;  | 
  |  45 |     | 
   import org.kuali.rice.kns.bo.BusinessObject;  | 
  |  46 |     | 
   import org.kuali.rice.kns.datadictionary.AttributeDefinition;  | 
  |  47 |     | 
   import org.kuali.rice.kns.datadictionary.BusinessObjectEntry;  | 
  |  48 |     | 
   import org.kuali.rice.kns.datadictionary.KimDataDictionaryAttributeDefinition;  | 
  |  49 |     | 
   import org.kuali.rice.kns.datadictionary.KimNonDataDictionaryAttributeDefinition;  | 
  |  50 |     | 
   import org.kuali.rice.kns.datadictionary.PrimitiveAttributeDefinition;  | 
  |  51 |     | 
   import org.kuali.rice.kns.datadictionary.RelationshipDefinition;  | 
  |  52 |     | 
   import org.kuali.rice.kns.datadictionary.control.ControlDefinition;  | 
  |  53 |     | 
   import org.kuali.rice.kns.datadictionary.validation.ValidationPattern;  | 
  |  54 |     | 
   import org.kuali.rice.kns.lookup.LookupUtils;  | 
  |  55 |     | 
   import org.kuali.rice.kns.lookup.keyvalues.KeyValuesFinder;  | 
  |  56 |     | 
   import org.kuali.rice.kns.lookup.keyvalues.KimAttributeValuesFinder;  | 
  |  57 |     | 
   import org.kuali.rice.kns.lookup.keyvalues.PersistableBusinessObjectValuesFinder;  | 
  |  58 |     | 
   import org.kuali.rice.kns.service.*;  | 
  |  59 |     | 
   import org.kuali.rice.kns.util.ErrorMessage;  | 
  |  60 |     | 
   import org.kuali.rice.kns.util.FieldUtils;  | 
  |  61 |     | 
   import org.kuali.rice.kns.util.GlobalVariables;  | 
  |  62 |     | 
   import org.kuali.rice.kns.util.KNSUtils;  | 
  |  63 |     | 
   import org.kuali.rice.kns.util.ObjectUtils;  | 
  |  64 |     | 
   import org.kuali.rice.kns.web.comparator.StringValueComparator;  | 
  |  65 |     | 
   import org.kuali.rice.kns.web.ui.Field;  | 
  |  66 |     | 
     | 
  |  67 |     | 
     | 
  |  68 |     | 
     | 
  |  69 |     | 
     | 
  |  70 |     | 
     | 
  |  71 |     | 
     | 
  |  72 |     | 
     | 
  |  73 |    0 |    public class KimTypeServiceBase implements KimTypeService { | 
  |  74 |     | 
     | 
  |  75 |    0 |            private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(KimTypeServiceBase.class);  | 
  |  76 |     | 
     | 
  |  77 |     | 
           private BusinessObjectService businessObjectService;  | 
  |  78 |     | 
           private DictionaryValidationService dictionaryValidationService;  | 
  |  79 |     | 
           private DataDictionaryService dataDictionaryService;  | 
  |  80 |     | 
           private KimTypeInfoService typeInfoService;  | 
  |  81 |     | 
             | 
  |  82 |    0 |            protected List<String> workflowRoutingAttributes = new ArrayList<String>();  | 
  |  83 |    0 |            protected List<String> requiredAttributes = new ArrayList<String>();  | 
  |  84 |    0 |            protected boolean checkRequiredAttributes = false;  | 
  |  85 |     | 
     | 
  |  86 |     | 
           protected KimTypeInfoService getTypeInfoService() { | 
  |  87 |    0 |                    if ( typeInfoService == null ) { | 
  |  88 |    0 |                            typeInfoService = KIMServiceLocatorWeb.getTypeInfoService();  | 
  |  89 |     | 
                   }  | 
  |  90 |    0 |                    return typeInfoService;  | 
  |  91 |     | 
           }  | 
  |  92 |     | 
     | 
  |  93 |     | 
           protected BusinessObjectService getBusinessObjectService() { | 
  |  94 |    0 |                    if ( businessObjectService == null ) { | 
  |  95 |    0 |                            businessObjectService = KNSServiceLocator.getBusinessObjectService();  | 
  |  96 |     | 
                   }  | 
  |  97 |    0 |                    return businessObjectService;  | 
  |  98 |     | 
           }  | 
  |  99 |     | 
     | 
  |  100 |     | 
           protected DictionaryValidationService getDictionaryValidationService() { | 
  |  101 |    0 |                    if ( dictionaryValidationService == null ) { | 
  |  102 |    0 |                            dictionaryValidationService = KNSServiceLocatorWeb.getDictionaryValidationService();  | 
  |  103 |     | 
                   }  | 
  |  104 |    0 |                    return dictionaryValidationService;  | 
  |  105 |     | 
           }  | 
  |  106 |     | 
     | 
  |  107 |     | 
           protected DataDictionaryService getDataDictionaryService() { | 
  |  108 |    0 |                    if ( dataDictionaryService == null ) { | 
  |  109 |    0 |                            dataDictionaryService = KNSServiceLocatorWeb.getDataDictionaryService();  | 
  |  110 |     | 
                   }  | 
  |  111 |    0 |                    return this.dataDictionaryService;  | 
  |  112 |     | 
           }  | 
  |  113 |     | 
     | 
  |  114 |     | 
             | 
  |  115 |     | 
     | 
  |  116 |     | 
     | 
  |  117 |     | 
     | 
  |  118 |     | 
     | 
  |  119 |     | 
           @Override  | 
  |  120 |     | 
           public String getWorkflowDocumentTypeName() { | 
  |  121 |    0 |                    return null;  | 
  |  122 |     | 
           }  | 
  |  123 |     | 
     | 
  |  124 |     | 
             | 
  |  125 |     | 
     | 
  |  126 |     | 
     | 
  |  127 |     | 
     | 
  |  128 |     | 
     | 
  |  129 |     | 
           protected boolean performMatch(AttributeSet inputAttributeSet, AttributeSet storedAttributeSet) { | 
  |  130 |    0 |                    if ( storedAttributeSet == null || inputAttributeSet == null ) { | 
  |  131 |    0 |                            return true;  | 
  |  132 |     | 
                   }  | 
  |  133 |    0 |                    for ( Map.Entry<String, String> entry : storedAttributeSet.entrySet() ) { | 
  |  134 |    0 |                            if (inputAttributeSet.containsKey(entry.getKey()) && !StringUtils.equals(inputAttributeSet.get(entry.getKey()), entry.getValue()) ) { | 
  |  135 |    0 |                                    return false;  | 
  |  136 |     | 
                           }  | 
  |  137 |     | 
                   }  | 
  |  138 |    0 |                    return true;  | 
  |  139 |     | 
           }  | 
  |  140 |     | 
     | 
  |  141 |     | 
           public AttributeSet translateInputAttributeSet(AttributeSet qualification){ | 
  |  142 |    0 |                    return qualification;  | 
  |  143 |     | 
           }  | 
  |  144 |     | 
     | 
  |  145 |     | 
             | 
  |  146 |     | 
     | 
  |  147 |     | 
     | 
  |  148 |     | 
     | 
  |  149 |     | 
           public boolean performMatches(AttributeSet inputAttributeSet, List<AttributeSet> storedAttributeSets){ | 
  |  150 |    0 |                    for ( AttributeSet storedAttributeSet : storedAttributeSets ) { | 
  |  151 |     | 
                             | 
  |  152 |    0 |                            if ( performMatch(inputAttributeSet, storedAttributeSet) ) { | 
  |  153 |    0 |                                    return true;  | 
  |  154 |     | 
                           }  | 
  |  155 |     | 
                   }  | 
  |  156 |    0 |                    return false;  | 
  |  157 |     | 
           }  | 
  |  158 |     | 
             | 
  |  159 |     | 
             | 
  |  160 |     | 
     | 
  |  161 |     | 
     | 
  |  162 |     | 
     | 
  |  163 |     | 
     | 
  |  164 |     | 
     | 
  |  165 |     | 
     | 
  |  166 |     | 
           @Override  | 
  |  167 |     | 
           public AttributeSet validateAttributes(String kimTypeId, AttributeSet attributes) { | 
  |  168 |    0 |                    AttributeSet validationErrors = new AttributeSet();  | 
  |  169 |    0 |                    if ( attributes == null ) { | 
  |  170 |    0 |                            return validationErrors;  | 
  |  171 |     | 
                   }  | 
  |  172 |    0 |                    KimTypeInfo kimType = getTypeInfoService().getKimType(kimTypeId);  | 
  |  173 |     | 
                     | 
  |  174 |    0 |                    for ( String attributeName : attributes.keySet() ) { | 
  |  175 |    0 |                KimTypeAttributeInfo attr = kimType.getAttributeDefinitionByName(attributeName);  | 
  |  176 |    0 |                            List<String> attributeErrors = null;  | 
  |  177 |     | 
                           try { | 
  |  178 |    0 |                                    if ( attr.getComponentName() == null) { | 
  |  179 |    0 |                                            attributeErrors = validateNonDataDictionaryAttribute(attributeName, attributes.get( attributeName ), true);  | 
  |  180 |     | 
                                   } else { | 
  |  181 |     | 
                                             | 
  |  182 |    0 |                                Object componentObject = Class.forName( attr.getComponentName() ).newInstance();  | 
  |  183 |     | 
                                 | 
  |  184 |    0 |                                PropertyDescriptor propertyDescriptor = null;  | 
  |  185 |    0 |                                if ( attr.getAttributeName() != null ) { | 
  |  186 |    0 |                                        propertyDescriptor = PropertyUtils.getPropertyDescriptor(componentObject, attr.getAttributeName());  | 
  |  187 |    0 |                                                    if ( propertyDescriptor != null ) { | 
  |  188 |     | 
                                                             | 
  |  189 |    0 |                                                            Object attributeValue = getAttributeValue(propertyDescriptor, attributes.get(attributeName));  | 
  |  190 |    0 |                                                            propertyDescriptor.getWriteMethod().invoke( componentObject, attributeValue);  | 
  |  191 |    0 |                                                            attributeErrors = validateDataDictionaryAttribute(kimTypeId, attr.getComponentName(), componentObject, propertyDescriptor);  | 
  |  192 |     | 
                                                   }  | 
  |  193 |     | 
                               }  | 
  |  194 |    0 |                                            if ( propertyDescriptor == null ) { | 
  |  195 |    0 |                                                    LOG.warn( "Unable to obtain property descriptor for: " + attr.getComponentName() + "/" + attr.getAttributeName() );  | 
  |  196 |     | 
                                           }  | 
  |  197 |     | 
                                   }  | 
  |  198 |    0 |                            } catch (Exception e) { | 
  |  199 |    0 |                                    LOG.error("Unable to validate attribute: " + attributeName, e); | 
  |  200 |    0 |                            }  | 
  |  201 |     | 
     | 
  |  202 |    0 |                            if ( attributeErrors != null ) { | 
  |  203 |    0 |                                    for ( String err : attributeErrors ) { | 
  |  204 |    0 |                                            validationErrors.put(attributeName, err);  | 
  |  205 |     | 
                                   }  | 
  |  206 |     | 
                           }  | 
  |  207 |    0 |                    }  | 
  |  208 |     | 
                     | 
  |  209 |    0 |                    Map<String, List<String>> referenceCheckErrors = validateReferencesExistAndActive(kimType, attributes, validationErrors);  | 
  |  210 |    0 |                    for ( String attributeName : referenceCheckErrors.keySet() ) { | 
  |  211 |    0 |                            List<String> attributeErrors = referenceCheckErrors.get(attributeName);  | 
  |  212 |    0 |                            for ( String err : attributeErrors ) { | 
  |  213 |    0 |                                    validationErrors.put(attributeName, err);  | 
  |  214 |     | 
                           }  | 
  |  215 |    0 |                    }  | 
  |  216 |     | 
                     | 
  |  217 |    0 |                    return validationErrors;  | 
  |  218 |     | 
           }  | 
  |  219 |     | 
     | 
  |  220 |     | 
           private Object getAttributeValue(PropertyDescriptor propertyDescriptor, String attributeValue){ | 
  |  221 |    0 |                    Object attributeValueObject = null;  | 
  |  222 |    0 |                    if(propertyDescriptor!=null && attributeValue!=null){ | 
  |  223 |    0 |                            Class<?> propertyType = propertyDescriptor.getPropertyType();  | 
  |  224 |    0 |                            if(propertyType!=String.class){ | 
  |  225 |    0 |                                    attributeValueObject = KNSUtils.createObject(propertyType, new Class[]{String.class}, new Object[] {attributeValue}); | 
  |  226 |     | 
                           } else { | 
  |  227 |    0 |                                    attributeValueObject = attributeValue;  | 
  |  228 |     | 
                           }  | 
  |  229 |     | 
                   }  | 
  |  230 |    0 |                    return attributeValueObject;  | 
  |  231 |     | 
           }  | 
  |  232 |     | 
             | 
  |  233 |     | 
           protected Map<String, List<String>> validateReferencesExistAndActive( KimTypeInfo kimType, AttributeSet attributes, Map<String, String> previousValidationErrors) { | 
  |  234 |    0 |                    Map<String, BusinessObject> componentClassInstances = new HashMap<String, BusinessObject>();  | 
  |  235 |    0 |                    Map<String, List<String>> errors = new HashMap<String, List<String>>();  | 
  |  236 |     | 
                     | 
  |  237 |    0 |                    for ( String attributeName : attributes.keySet() ) { | 
  |  238 |    0 |                            KimTypeAttributeInfo attr = kimType.getAttributeDefinitionByName(attributeName);  | 
  |  239 |     | 
                             | 
  |  240 |    0 |                            if (StringUtils.isNotBlank(attr.getComponentName())) { | 
  |  241 |    0 |                                    if (!componentClassInstances.containsKey(attr.getComponentName())) { | 
  |  242 |     | 
                                           try { | 
  |  243 |    0 |                                                    Class<?> componentClass = Class.forName( attr.getComponentName() );  | 
  |  244 |    0 |                                                    if (!BusinessObject.class.isAssignableFrom(componentClass)) { | 
  |  245 |    0 |                                                            LOG.warn("Class " + componentClass.getName() + " does not implement BusinessObject.  Unable to perform reference existence and active validation"); | 
  |  246 |    0 |                                                            continue;  | 
  |  247 |     | 
                                                   }  | 
  |  248 |    0 |                                                    BusinessObject componentInstance = (BusinessObject) componentClass.newInstance();  | 
  |  249 |    0 |                                                    componentClassInstances.put(attr.getComponentName(), componentInstance);  | 
  |  250 |    0 |                                            } catch (Exception e) { | 
  |  251 |    0 |                                                    LOG.error("Unable to instantiate class for attribute: " + attributeName, e); | 
  |  252 |    0 |                                            }  | 
  |  253 |     | 
                                   }  | 
  |  254 |     | 
                           }  | 
  |  255 |    0 |                    }  | 
  |  256 |     | 
                     | 
  |  257 |     | 
                     | 
  |  258 |    0 |                    for ( String attributeName : attributes.keySet() ) { | 
  |  259 |    0 |                            if (!previousValidationErrors.containsKey(attributeName)) { | 
  |  260 |    0 |                                    for (Object componentInstance : componentClassInstances.values()) { | 
  |  261 |     | 
                                           try { | 
  |  262 |    0 |                                                    ObjectUtils.setObjectProperty(componentInstance, attributeName, attributes.get(attributeName));  | 
  |  263 |    0 |                                            } catch (NoSuchMethodException e) { | 
  |  264 |     | 
                                                     | 
  |  265 |    0 |                                            } catch (Exception e) { | 
  |  266 |    0 |                                                    LOG.error("Unable to set object property class: " + componentInstance.getClass().getName() + " property: " + attributeName, e); | 
  |  267 |    0 |                                            }  | 
  |  268 |     | 
                                   }  | 
  |  269 |     | 
                           }  | 
  |  270 |     | 
                   }  | 
  |  271 |     | 
                     | 
  |  272 |    0 |                    for (String componentClass : componentClassInstances.keySet()) { | 
  |  273 |    0 |                            BusinessObject componentInstance = componentClassInstances.get(componentClass);  | 
  |  274 |     | 
                             | 
  |  275 |    0 |                            List<RelationshipDefinition> relationships = getDataDictionaryService().getDataDictionary().getBusinessObjectEntry(componentClass).getRelationships();  | 
  |  276 |    0 |                            if (relationships == null) { | 
  |  277 |    0 |                                    continue;  | 
  |  278 |     | 
                           }  | 
  |  279 |     | 
                             | 
  |  280 |    0 |                            for (RelationshipDefinition relationshipDefinition : relationships) { | 
  |  281 |    0 |                                    List<PrimitiveAttributeDefinition> primitiveAttributes = relationshipDefinition.getPrimitiveAttributes();  | 
  |  282 |     | 
                                     | 
  |  283 |     | 
                                     | 
  |  284 |    0 |                                    String attributeToHighlightOnFail = primitiveAttributes.get(primitiveAttributes.size() - 1).getSourceName();  | 
  |  285 |     | 
                                     | 
  |  286 |     | 
                                     | 
  |  287 |     | 
                                     | 
  |  288 |    0 |                                    if (!attributes.containsKey(attributeToHighlightOnFail)) { | 
  |  289 |     | 
                                             | 
  |  290 |    0 |                                            continue;  | 
  |  291 |     | 
                                   }  | 
  |  292 |     | 
                                     | 
  |  293 |     | 
                                   String attributeDisplayLabel;  | 
  |  294 |    0 |                                    KimTypeAttributeInfo attr = kimType.getAttributeDefinitionByName(attributeToHighlightOnFail);  | 
  |  295 |    0 |                                    if (attr != null) { | 
  |  296 |    0 |                                            if (StringUtils.isNotBlank(attr.getComponentName())) { | 
  |  297 |    0 |                                                    attributeDisplayLabel = getDataDictionaryService().getAttributeLabel(attr.getComponentName(), attributeToHighlightOnFail);  | 
  |  298 |     | 
                                           } else { | 
  |  299 |    0 |                                                    attributeDisplayLabel = attr.getAttributeLabel();  | 
  |  300 |     | 
                                           }  | 
  |  301 |     | 
     | 
  |  302 |    0 |                                            getDictionaryValidationService().validateReferenceExistsAndIsActive(componentInstance, relationshipDefinition.getObjectAttributeName(),  | 
  |  303 |     | 
                                                           attributeToHighlightOnFail, attributeDisplayLabel);  | 
  |  304 |     | 
                                   }  | 
  |  305 |     | 
                                     | 
  |  306 |    0 |                                    errors.put(attributeToHighlightOnFail, extractErrorsFromGlobalVariablesErrorMap(attributeToHighlightOnFail));  | 
  |  307 |    0 |                            }  | 
  |  308 |    0 |                    }  | 
  |  309 |    0 |                    return errors;  | 
  |  310 |     | 
           }  | 
  |  311 |     | 
             | 
  |  312 |     | 
       protected void validateAttributeRequired(String kimTypeId, String objectClassName, String attributeName, Object attributeValue, String errorKey) { | 
  |  313 |     | 
             | 
  |  314 |    0 |            if (attributeValue == null || (attributeValue instanceof String && StringUtils.isBlank((String) attributeValue))) { | 
  |  315 |    0 |                    AttributeDefinitionMap map = getAttributeDefinitions(kimTypeId);  | 
  |  316 |    0 |                    AttributeDefinition definition = map.getByAttributeName(attributeName);  | 
  |  317 |     | 
                     | 
  |  318 |    0 |                Boolean required = definition.isRequired();  | 
  |  319 |    0 |                ControlDefinition controlDef = definition.getControl();  | 
  |  320 |     | 
     | 
  |  321 |    0 |                if (required != null && required.booleanValue() && !(controlDef != null && controlDef.isHidden())) { | 
  |  322 |     | 
     | 
  |  323 |     | 
                     | 
  |  324 |    0 |                    String errorLabel = getAttributeErrorLabel(definition);  | 
  |  325 |    0 |                    GlobalVariables.getMessageMap().putError(errorKey, RiceKeyConstants.ERROR_REQUIRED, errorLabel);  | 
  |  326 |     | 
               }  | 
  |  327 |     | 
           }  | 
  |  328 |    0 |        }  | 
  |  329 |     | 
         | 
  |  330 |     | 
           protected List<String> validateDataDictionaryAttribute(String kimTypeId, String entryName, Object object, PropertyDescriptor propertyDescriptor) { | 
  |  331 |    0 |                    validatePrimitiveFromDescriptor(kimTypeId, entryName, object, propertyDescriptor);  | 
  |  332 |    0 |                    return extractErrorsFromGlobalVariablesErrorMap(propertyDescriptor.getName());  | 
  |  333 |     | 
           }  | 
  |  334 |     | 
     | 
  |  335 |     | 
       protected void validatePrimitiveFromDescriptor(String kimTypeId, String entryName, Object object, PropertyDescriptor propertyDescriptor) { | 
  |  336 |     | 
             | 
  |  337 |    0 |            if (null != propertyDescriptor && getDataDictionaryService().isAttributeDefined(entryName, propertyDescriptor.getName())) { | 
  |  338 |    0 |                Object value = ObjectUtils.getPropertyValue(object, propertyDescriptor.getName());  | 
  |  339 |    0 |                Class<? extends Object> propertyType = propertyDescriptor.getPropertyType();  | 
  |  340 |     | 
     | 
  |  341 |    0 |                if (TypeUtils.isStringClass(propertyType) || TypeUtils.isIntegralClass(propertyType) || TypeUtils.isDecimalClass(propertyType) || TypeUtils.isTemporalClass(propertyType)) { | 
  |  342 |     | 
     | 
  |  343 |     | 
                     | 
  |  344 |    0 |                    if (value != null && StringUtils.isNotBlank(value.toString())) { | 
  |  345 |    0 |                        if (!TypeUtils.isTemporalClass(propertyType)) { | 
  |  346 |    0 |                            validateAttributeFormat(kimTypeId, entryName, propertyDescriptor.getName(), value.toString(), propertyDescriptor.getName());  | 
  |  347 |     | 
                       }  | 
  |  348 |     | 
                   }  | 
  |  349 |     | 
                   else { | 
  |  350 |     | 
                             | 
  |  351 |    0 |                        validateAttributeRequired(kimTypeId, entryName, propertyDescriptor.getName(), value, propertyDescriptor.getName());  | 
  |  352 |     | 
                   }  | 
  |  353 |     | 
               }  | 
  |  354 |     | 
           }  | 
  |  355 |    0 |        }  | 
  |  356 |     | 
         | 
  |  357 |     | 
         | 
  |  358 |     | 
     | 
  |  359 |     | 
     | 
  |  360 |     | 
     | 
  |  361 |     | 
       public static final String VALIDATE_METHOD="validate";  | 
  |  362 |     | 
         | 
  |  363 |     | 
       protected String getAttributeErrorLabel(AttributeDefinition definition) { | 
  |  364 |    0 |            String longAttributeLabel = definition.getLabel();  | 
  |  365 |    0 |            String shortAttributeLabel = definition.getShortLabel();  | 
  |  366 |    0 |            return longAttributeLabel + " (" + shortAttributeLabel + ")"; | 
  |  367 |     | 
       }  | 
  |  368 |     | 
         | 
  |  369 |     | 
       protected Pattern getAttributeValidatingExpression(AttributeDefinition definition) { | 
  |  370 |    0 |                Pattern regex = null;  | 
  |  371 |    0 |            if (definition != null) { | 
  |  372 |    0 |                if (definition.hasValidationPattern()) { | 
  |  373 |    0 |                    regex = definition.getValidationPattern().getRegexPattern();  | 
  |  374 |     | 
               } else { | 
  |  375 |     | 
                     | 
  |  376 |    0 |                    regex = Pattern.compile(".*"); | 
  |  377 |     | 
               }  | 
  |  378 |     | 
           }  | 
  |  379 |     | 
     | 
  |  380 |    0 |            return regex;  | 
  |  381 |     | 
       }  | 
  |  382 |     | 
         | 
  |  383 |     | 
           protected Class<? extends Formatter> getAttributeFormatter(AttributeDefinition definition) { | 
  |  384 |    0 |            Class<? extends Formatter> formatterClass = null;  | 
  |  385 |    0 |            if (definition != null && definition.hasFormatterClass()) { | 
  |  386 |     | 
                           try { | 
  |  387 |    0 |                                    formatterClass = ClassLoaderUtils.getClass(definition.getFormatterClass(), Formatter.class);  | 
  |  388 |    0 |                            } catch (ClassNotFoundException e) { | 
  |  389 |     | 
                                     | 
  |  390 |     | 
                                     | 
  |  391 |     | 
                                     | 
  |  392 |    0 |                                    LOG.warn("Failed to resolve formatter class: " + definition.getFormatterClass(), e); | 
  |  393 |    0 |                            }  | 
  |  394 |     | 
           }  | 
  |  395 |    0 |            return formatterClass;  | 
  |  396 |     | 
       }  | 
  |  397 |     | 
         | 
  |  398 |     | 
           public String getAttributeValidatingErrorMessageKey(AttributeDefinition definition) { | 
  |  399 |    0 |            if (definition != null) { | 
  |  400 |    0 |                    if (definition.hasValidationPattern()) { | 
  |  401 |    0 |                            ValidationPattern validationPattern = definition.getValidationPattern();  | 
  |  402 |    0 |                            return validationPattern.getValidationErrorMessageKey();  | 
  |  403 |     | 
                   }  | 
  |  404 |     | 
           }  | 
  |  405 |    0 |            return null;  | 
  |  406 |     | 
           }  | 
  |  407 |     | 
             | 
  |  408 |     | 
           public String[] getAttributeValidatingErrorMessageParameters(AttributeDefinition definition) { | 
  |  409 |    0 |            if (definition != null) { | 
  |  410 |    0 |                    if (definition.hasValidationPattern()) { | 
  |  411 |    0 |                            ValidationPattern validationPattern = definition.getValidationPattern();  | 
  |  412 |    0 |                            String attributeLabel = getAttributeErrorLabel(definition);  | 
  |  413 |    0 |                            return validationPattern.getValidationErrorMessageParameters(attributeLabel);  | 
  |  414 |     | 
                   }  | 
  |  415 |     | 
           }  | 
  |  416 |    0 |            return null;  | 
  |  417 |     | 
           }  | 
  |  418 |     | 
         | 
  |  419 |     | 
           protected BigDecimal getAttributeExclusiveMin(AttributeDefinition definition) { | 
  |  420 |    0 |            return definition == null ? null : definition.getExclusiveMin();  | 
  |  421 |     | 
       }  | 
  |  422 |     | 
     | 
  |  423 |     | 
           protected BigDecimal getAttributeInclusiveMax(AttributeDefinition definition) { | 
  |  424 |    0 |            return definition == null ? null : definition.getInclusiveMax();  | 
  |  425 |     | 
       }  | 
  |  426 |     | 
             | 
  |  427 |     | 
       protected void validateAttributeFormat(String kimTypeId, String objectClassName, String attributeName, String attributeValue, String errorKey) { | 
  |  428 |    0 |                AttributeDefinitionMap attributeDefinitions = getAttributeDefinitions(kimTypeId);  | 
  |  429 |    0 |                AttributeDefinition definition = attributeDefinitions.getByAttributeName(attributeName);  | 
  |  430 |     | 
                 | 
  |  431 |    0 |            String errorLabel = getAttributeErrorLabel(definition);  | 
  |  432 |     | 
     | 
  |  433 |    0 |            if ( LOG.isDebugEnabled() ) { | 
  |  434 |    0 |                    LOG.debug("(bo, attributeName, attributeValue) = (" + objectClassName + "," + attributeName + "," + attributeValue + ")"); | 
  |  435 |     | 
           }  | 
  |  436 |     | 
     | 
  |  437 |    0 |            if (StringUtils.isNotBlank(attributeValue)) { | 
  |  438 |    0 |                Integer maxLength = definition.getMaxLength();  | 
  |  439 |    0 |                if ((maxLength != null) && (maxLength.intValue() < attributeValue.length())) { | 
  |  440 |    0 |                    GlobalVariables.getMessageMap().putError(errorKey, RiceKeyConstants.ERROR_MAX_LENGTH, new String[] { errorLabel, maxLength.toString() }); | 
  |  441 |    0 |                    return;  | 
  |  442 |     | 
               }  | 
  |  443 |    0 |                Pattern validationExpression = getAttributeValidatingExpression(definition);  | 
  |  444 |    0 |                if (validationExpression != null && !validationExpression.pattern().equals(".*")) { | 
  |  445 |    0 |                        if ( LOG.isDebugEnabled() ) { | 
  |  446 |    0 |                                LOG.debug("(bo, attributeName, validationExpression) = (" + objectClassName + "," + attributeName + "," + validationExpression + ")"); | 
  |  447 |     | 
                       }  | 
  |  448 |     | 
     | 
  |  449 |    0 |                    if (!validationExpression.matcher(attributeValue).matches()) { | 
  |  450 |    0 |                        boolean isError=true;  | 
  |  451 |     | 
                         | 
  |  452 |    0 |                        Class<?> formatterClass=getAttributeFormatter(definition);  | 
  |  453 |    0 |                        if (formatterClass != null) { | 
  |  454 |     | 
                           try { | 
  |  455 |    0 |                                Method validatorMethod=formatterClass.getDeclaredMethod(  | 
  |  456 |     | 
                                       VALIDATE_METHOD, new Class<?>[] {String.class}); | 
  |  457 |    0 |                                Object o=validatorMethod.invoke(  | 
  |  458 |     | 
                                       formatterClass.newInstance(), attributeValue);  | 
  |  459 |    0 |                                if (o instanceof Boolean) { | 
  |  460 |    0 |                                    isError=!((Boolean)o).booleanValue();  | 
  |  461 |     | 
                               }  | 
  |  462 |    0 |                            } catch (Exception e) { | 
  |  463 |    0 |                                LOG.warn(e.getMessage(), e);  | 
  |  464 |    0 |                            }  | 
  |  465 |     | 
                       }  | 
  |  466 |    0 |                        if (isError) { | 
  |  467 |    0 |                                String errorMessageKey = getAttributeValidatingErrorMessageKey(definition);  | 
  |  468 |    0 |                                String[] errorMessageParameters = getAttributeValidatingErrorMessageParameters(definition);  | 
  |  469 |    0 |                            GlobalVariables.getMessageMap().putError(errorKey, errorMessageKey, errorMessageParameters);  | 
  |  470 |     | 
                       }  | 
  |  471 |    0 |                        return;  | 
  |  472 |     | 
                   }  | 
  |  473 |     | 
               }  | 
  |  474 |    0 |                BigDecimal exclusiveMin = getAttributeExclusiveMin(definition);  | 
  |  475 |    0 |                if (exclusiveMin != null) { | 
  |  476 |     | 
                   try { | 
  |  477 |    0 |                        if (exclusiveMin.compareTo(new BigDecimal(attributeValue)) >= 0) { | 
  |  478 |    0 |                            GlobalVariables.getMessageMap().putError(errorKey, RiceKeyConstants.ERROR_EXCLUSIVE_MIN,  | 
  |  479 |     | 
                             | 
  |  480 |     | 
                                   new String[] { errorLabel, exclusiveMin.toString() }); | 
  |  481 |    0 |                            return;  | 
  |  482 |     | 
                       }  | 
  |  483 |     | 
                   }  | 
  |  484 |    0 |                    catch (NumberFormatException e) { | 
  |  485 |     | 
                         | 
  |  486 |    0 |                    }  | 
  |  487 |     | 
               }  | 
  |  488 |    0 |                BigDecimal inclusiveMax = getAttributeInclusiveMax(definition);  | 
  |  489 |    0 |                if (inclusiveMax != null) { | 
  |  490 |     | 
                   try { | 
  |  491 |    0 |                        if (inclusiveMax.compareTo(new BigDecimal(attributeValue)) < 0) { | 
  |  492 |    0 |                            GlobalVariables.getMessageMap().putError(errorKey, RiceKeyConstants.ERROR_INCLUSIVE_MAX,  | 
  |  493 |     | 
                             | 
  |  494 |     | 
                                   new String[] { errorLabel, inclusiveMax.toString() }); | 
  |  495 |    0 |                            return;  | 
  |  496 |     | 
                       }  | 
  |  497 |     | 
                   }  | 
  |  498 |    0 |                    catch (NumberFormatException e) { | 
  |  499 |     | 
                         | 
  |  500 |    0 |                    }  | 
  |  501 |     | 
               }  | 
  |  502 |     | 
           }  | 
  |  503 |    0 |        }  | 
  |  504 |     | 
         | 
  |  505 |     | 
           protected List<String> extractErrorsFromGlobalVariablesErrorMap(String attributeName) { | 
  |  506 |    0 |                    Object results = GlobalVariables.getMessageMap().getErrorMessagesForProperty(attributeName);  | 
  |  507 |    0 |                    List<String> errors = new ArrayList<String>();  | 
  |  508 |    0 |            if (results instanceof String) { | 
  |  509 |    0 |                    errors.add((String)results);  | 
  |  510 |    0 |            } else if ( results != null) { | 
  |  511 |    0 |                    if (results instanceof List) { | 
  |  512 |    0 |                            List<?> errorList = (List<?>)results;  | 
  |  513 |    0 |                            for (Object msg : errorList) { | 
  |  514 |    0 |                                    ErrorMessage errorMessage = (ErrorMessage)msg;  | 
  |  515 |    0 |                                    String retVal = errorMessage.getErrorKey()+":";  | 
  |  516 |    0 |                                    for (String param : errorMessage.getMessageParameters()) { | 
  |  517 |    0 |                                            retVal = retVal + param +";";  | 
  |  518 |     | 
                                   }  | 
  |  519 |    0 |                                    errors.add(retVal);  | 
  |  520 |    0 |                                    }  | 
  |  521 |    0 |                    } else { | 
  |  522 |    0 |                            String [] temp = (String []) results;  | 
  |  523 |    0 |                            for (String string : temp) { | 
  |  524 |    0 |                                            errors.add(string);  | 
  |  525 |     | 
                                   }  | 
  |  526 |     | 
                   }  | 
  |  527 |     | 
           }  | 
  |  528 |    0 |            GlobalVariables.getMessageMap().removeAllErrorMessagesForProperty(attributeName);  | 
  |  529 |    0 |            return errors;  | 
  |  530 |     | 
           }  | 
  |  531 |     | 
             | 
  |  532 |     | 
           protected List<String> validateNonDataDictionaryAttribute(String attributeName, String attributeValue, boolean validateRequired) { | 
  |  533 |    0 |                    return new ArrayList<String>();  | 
  |  534 |     | 
           }  | 
  |  535 |     | 
     | 
  |  536 |     | 
           protected List<KeyValue> getLocalDataDictionaryAttributeValues(KimTypeAttributeInfo attr) throws ClassNotFoundException { | 
  |  537 |    0 |                    List<KeyValue> pairs = new ArrayList<KeyValue>();  | 
  |  538 |    0 |                    BusinessObjectEntry entry = getDataDictionaryService().getDataDictionary().getBusinessObjectEntry(attr.getComponentName());  | 
  |  539 |    0 |                    if ( entry == null ) { | 
  |  540 |    0 |                            LOG.warn( "Unable to obtain BusinessObjectEntry for component name: " + attr.getComponentName() );  | 
  |  541 |    0 |                            return pairs;  | 
  |  542 |     | 
                   }  | 
  |  543 |    0 |                    AttributeDefinition definition = entry.getAttributeDefinition(attr.getAttributeName());  | 
  |  544 |    0 |                    if ( definition == null ) { | 
  |  545 |    0 |                            LOG.warn( "No attribute named " + attr.getAttributeName() + " found on BusinessObjectEntry for: " + attr.getComponentName() );  | 
  |  546 |    0 |                            return pairs;  | 
  |  547 |     | 
                   }  | 
  |  548 |    0 |                    String keyValuesFinderName = definition.getControl().getValuesFinderClass();  | 
  |  549 |    0 |                    if ( StringUtils.isNotBlank(keyValuesFinderName)) { | 
  |  550 |     | 
                           try { | 
  |  551 |    0 |                                    KeyValuesFinder finder = (KeyValuesFinder)Class.forName(keyValuesFinderName).newInstance();  | 
  |  552 |    0 |                                    if (finder instanceof PersistableBusinessObjectValuesFinder) { | 
  |  553 |    0 |                            ((PersistableBusinessObjectValuesFinder) finder).setBusinessObjectClass(ClassLoaderUtils.getClass(definition.getControl().getBusinessObjectClass()));  | 
  |  554 |    0 |                            ((PersistableBusinessObjectValuesFinder) finder).setKeyAttributeName(definition.getControl().getKeyAttribute());  | 
  |  555 |    0 |                            ((PersistableBusinessObjectValuesFinder) finder).setLabelAttributeName(definition.getControl().getLabelAttribute());  | 
  |  556 |    0 |                            if (definition.getControl().getIncludeBlankRow() != null) { | 
  |  557 |    0 |                                    ((PersistableBusinessObjectValuesFinder) finder).setIncludeBlankRow(definition.getControl().getIncludeBlankRow());   | 
  |  558 |     | 
                           }  | 
  |  559 |    0 |                            ((PersistableBusinessObjectValuesFinder) finder).setIncludeKeyInDescription(definition.getControl().getIncludeKeyInLabel());  | 
  |  560 |     | 
                                   }  | 
  |  561 |    0 |                                    pairs = finder.getKeyValues();  | 
  |  562 |    0 |                            } catch ( ClassNotFoundException ex ) { | 
  |  563 |    0 |                                    LOG.info( "Unable to find class: " + keyValuesFinderName + " in the current context." );  | 
  |  564 |    0 |                                    throw ex;  | 
  |  565 |    0 |                            } catch (Exception e) { | 
  |  566 |    0 |                                    LOG.error("Unable to build a KeyValuesFinder for " + attr.getAttributeName(), e); | 
  |  567 |    0 |                            }  | 
  |  568 |     | 
                   } else { | 
  |  569 |    0 |                            LOG.warn( "No values finder class defined on the control definition (" + definition.getControl() + ") on BO / attr = " + attr.getComponentName() + " / " + attr.getAttributeName() ); | 
  |  570 |     | 
                   }  | 
  |  571 |    0 |                    return pairs;  | 
  |  572 |     | 
           }  | 
  |  573 |     | 
     | 
  |  574 |     | 
           protected List<KeyValue> getCustomValueFinderValues(KimTypeAttributeInfo attrib) { | 
  |  575 |    0 |                    return new ArrayList<KeyValue>(0);  | 
  |  576 |     | 
           }  | 
  |  577 |     | 
     | 
  |  578 |     | 
           @Override  | 
  |  579 |     | 
           public List<KeyValue> getAttributeValidValues(String kimTypeId, String attributeName) { | 
  |  580 |    0 |                    if ( LOG.isDebugEnabled() ) { | 
  |  581 |    0 |                            LOG.debug( "getAttributeValidValues(" + kimTypeId + "," + attributeName + ")");                         | 
  |  582 |     | 
                   }  | 
  |  583 |    0 |                    KimTypeAttributeInfo attrib = KIMServiceLocatorWeb.getTypeInfoService().getKimType(kimTypeId).getAttributeDefinitionByName(attributeName);  | 
  |  584 |    0 |                    if ( LOG.isDebugEnabled() ) { | 
  |  585 |    0 |                            LOG.debug( "Found Attribute definition: " + attrib );  | 
  |  586 |     | 
                   }  | 
  |  587 |    0 |                    List<KeyValue> pairs = null;  | 
  |  588 |    0 |                    if ( StringUtils.isNotBlank(attrib.getComponentName()) ) { | 
  |  589 |     | 
                           try { | 
  |  590 |    0 |                                    Class.forName(attrib.getComponentName());  | 
  |  591 |     | 
                                   try { | 
  |  592 |    0 |                                            pairs = getLocalDataDictionaryAttributeValues(attrib);  | 
  |  593 |    0 |                                    } catch ( ClassNotFoundException ex ) { | 
  |  594 |    0 |                                            LOG.error( "Got a ClassNotFoundException resolving a values finder - since this should have been executing in the context of the host system - this should not happen.");  | 
  |  595 |    0 |                                            pairs = new ArrayList<KeyValue>(0);  | 
  |  596 |    0 |                                    }  | 
  |  597 |    0 |                            } catch ( ClassNotFoundException ex ) { | 
  |  598 |    0 |                                    LOG.error( "Got a ClassNotFoundException resolving a component name (" + attrib.getComponentName() + ") - since this should have been executing in the context of the host system - this should not happen."); | 
  |  599 |    0 |                            }  | 
  |  600 |     | 
                   } else { | 
  |  601 |    0 |                            pairs = getCustomValueFinderValues(attrib);  | 
  |  602 |     | 
                   }  | 
  |  603 |    0 |            return pairs;  | 
  |  604 |     | 
           }  | 
  |  605 |     | 
     | 
  |  606 |     | 
             | 
  |  607 |     | 
     | 
  |  608 |     | 
     | 
  |  609 |     | 
     | 
  |  610 |     | 
     | 
  |  611 |     | 
     | 
  |  612 |     | 
           @SuppressWarnings("unchecked") | 
  |  613 |     | 
           protected AttributeDefinition getDataDictionaryAttributeDefinition( String namespaceCode, String kimTypeId, KimTypeAttributeInfo typeAttribute) { | 
  |  614 |     | 
                     | 
  |  615 |    0 |                    KimDataDictionaryAttributeDefinition definition = null;  | 
  |  616 |    0 |                    String componentClassName = typeAttribute.getComponentName();  | 
  |  617 |    0 |                    String attributeName = typeAttribute.getAttributeName();  | 
  |  618 |    0 |                    AttributeDefinition baseDefinition = null;  | 
  |  619 |     | 
                     | 
  |  620 |     | 
                     | 
  |  621 |     | 
                   try { | 
  |  622 |    0 |                            Class.forName(componentClassName);  | 
  |  623 |     | 
                           try { | 
  |  624 |    0 |                                    baseDefinition = getDataDictionaryService().getDataDictionary().getBusinessObjectEntry(componentClassName).getAttributeDefinition(attributeName);  | 
  |  625 |    0 |                            } catch ( Exception ex ) { | 
  |  626 |    0 |                                    LOG.error( "Unable to get base DD definition for " + componentClassName + "." + attributeName, ex );  | 
  |  627 |    0 |                                    return definition;  | 
  |  628 |    0 |                            }  | 
  |  629 |    0 |                    } catch (ClassNotFoundException ex) { | 
  |  630 |    0 |                            if ( LOG.isDebugEnabled() ) { | 
  |  631 |    0 |                                    LOG.debug( "Unable to find class " + componentClassName + " in available classloaders. Deferring to the service bus." );  | 
  |  632 |     | 
                           }  | 
  |  633 |    0 |                            baseDefinition = KNSServiceLocatorWeb.getRiceApplicationConfigurationMediationService().getBusinessObjectAttributeDefinition(componentClassName, attributeName);  | 
  |  634 |    0 |                    }  | 
  |  635 |     | 
                     | 
  |  636 |    0 |                    if (baseDefinition != null) { | 
  |  637 |     | 
     | 
  |  638 |    0 |                            definition = new KimDataDictionaryAttributeDefinition();  | 
  |  639 |     | 
                             | 
  |  640 |    0 |                            definition.setName( baseDefinition.getName() );  | 
  |  641 |    0 |                            definition.setLabel( baseDefinition.getLabel() );  | 
  |  642 |    0 |                            definition.setShortLabel( baseDefinition.getShortLabel() );  | 
  |  643 |    0 |                            definition.setMaxLength( baseDefinition.getMaxLength() );  | 
  |  644 |    0 |                            definition.setRequired( baseDefinition.isRequired() );  | 
  |  645 |     | 
     | 
  |  646 |    0 |                            if (baseDefinition.getFormatterClass() != null) { | 
  |  647 |    0 |                                    definition.setFormatterClass(baseDefinition.getFormatterClass());  | 
  |  648 |     | 
                           }  | 
  |  649 |    0 |                            ControlDefinition control = copy(baseDefinition.getControl());  | 
  |  650 |    0 |                            if ( StringUtils.isNotBlank( control.getValuesFinderClass() ) ) { | 
  |  651 |     | 
     | 
  |  652 |     | 
     | 
  |  653 |     | 
     | 
  |  654 |     | 
                                             | 
  |  655 |    0 |                                            control.setValuesFinderClass(KimAttributeValuesFinder.class.getName());  | 
  |  656 |     | 
     | 
  |  657 |     | 
                           }  | 
  |  658 |    0 |                            definition.setControl(control);  | 
  |  659 |    0 |                            definition.setSortCode(typeAttribute.getSortCode());  | 
  |  660 |    0 |                            definition.setKimAttrDefnId(typeAttribute.getKimAttributeId());  | 
  |  661 |    0 |                            definition.setKimTypeId(kimTypeId);  | 
  |  662 |     | 
     | 
  |  663 |    0 |                            definition.setForceUppercase(baseDefinition.getForceUppercase());  | 
  |  664 |     | 
                             | 
  |  665 |    0 |                            Map<String, String> lookupInputPropertyConversionsMap = new HashMap<String, String>();  | 
  |  666 |    0 |                            Map<String, String> lookupReturnPropertyConversionsMap = new HashMap<String, String>();  | 
  |  667 |     | 
     | 
  |  668 |     | 
                           try { | 
  |  669 |    0 |                                    Class<? extends BusinessObject> componentClass = (Class<? extends BusinessObject>)Class.forName(componentClassName);  | 
  |  670 |    0 |                                    BusinessObject sampleComponent = componentClass.newInstance();  | 
  |  671 |    0 |                                    List<String> displayedFieldNames = new ArrayList<String>( 1 );  | 
  |  672 |    0 |                                    displayedFieldNames.add( attributeName );  | 
  |  673 |    0 |                                    Field field = FieldUtils.getPropertyField(componentClass, attributeName, false);  | 
  |  674 |    0 |                                    if ( field != null ) { | 
  |  675 |    0 |                                            field = LookupUtils.setFieldQuickfinder( sampleComponent, attributeName, field, displayedFieldNames );  | 
  |  676 |    0 |                                            if ( StringUtils.isNotBlank( field.getQuickFinderClassNameImpl() ) ) { | 
  |  677 |    0 |                                                    Class<? extends BusinessObject> lookupClass = (Class<? extends BusinessObject>)Class.forName( field.getQuickFinderClassNameImpl() );  | 
  |  678 |    0 |                                                    definition.setLookupBoClass( lookupClass.getName() );  | 
  |  679 |    0 |                                                    if ( field.getLookupParameters() != null ) { | 
  |  680 |    0 |                                                            String [] lookupInputPropertyConversions = field.getLookupParameters().split(","); | 
  |  681 |    0 |                                                            for (String string : lookupInputPropertyConversions) { | 
  |  682 |    0 |                                                                    String [] keyVal = string.split(":"); | 
  |  683 |    0 |                                                                    lookupInputPropertyConversionsMap.put(keyVal[0], keyVal[1]);  | 
  |  684 |     | 
                                                           }  | 
  |  685 |    0 |                                                            definition.setLookupInputPropertyConversions(lookupInputPropertyConversionsMap);  | 
  |  686 |     | 
                                                   }  | 
  |  687 |    0 |                                                    if ( field.getFieldConversions() != null ) { | 
  |  688 |    0 |                                                            String [] lookupReturnPropertyConversions = field.getFieldConversions().split(","); | 
  |  689 |    0 |                                                            for (String string : lookupReturnPropertyConversions) { | 
  |  690 |    0 |                                                                    String [] keyVal = string.split(":"); | 
  |  691 |    0 |                                                                    lookupReturnPropertyConversionsMap.put(keyVal[0], keyVal[1]);  | 
  |  692 |     | 
                                                           }  | 
  |  693 |    0 |                                                            definition.setLookupReturnPropertyConversions(lookupReturnPropertyConversionsMap);  | 
  |  694 |     | 
                                                   }  | 
  |  695 |     | 
                                           }  | 
  |  696 |     | 
                                   }  | 
  |  697 |    0 |                            } catch (Exception e) { | 
  |  698 |    0 |                                    LOG.warn("Unable to get DD data for: " + typeAttribute, e); | 
  |  699 |    0 |                            }  | 
  |  700 |     | 
                   }  | 
  |  701 |    0 |                    return definition;  | 
  |  702 |     | 
           }  | 
  |  703 |     | 
     | 
  |  704 |     | 
           @SuppressWarnings("unchecked") | 
  |  705 |     | 
           private <T> T copy(final T original) { | 
  |  706 |    0 |                    if ( original == null ) { | 
  |  707 |    0 |                            return null;  | 
  |  708 |     | 
                   }  | 
  |  709 |    0 |                    T copy = null;  | 
  |  710 |     | 
                   try { | 
  |  711 |    0 |                            copy = (T) original.getClass().newInstance();  | 
  |  712 |    0 |                            Class copyClass = copy.getClass();  | 
  |  713 |     | 
                       do { | 
  |  714 |    0 |                                for (java.lang.reflect.Field copyField : copyClass.getDeclaredFields()) { | 
  |  715 |    0 |                                        copyField.setAccessible(true);  | 
  |  716 |    0 |                                        int mods = copyField.getModifiers();  | 
  |  717 |    0 |                                        if (!Modifier.isFinal(mods) && !Modifier.isStatic(mods)) { | 
  |  718 |    0 |                                                copyField.set(copy, copyField.get(original));  | 
  |  719 |     | 
                                       }  | 
  |  720 |     | 
                               }  | 
  |  721 |    0 |                                copyClass = copyClass.getSuperclass();  | 
  |  722 |    0 |                        } while (copyClass != null && !(copyClass.equals(Object.class)));  | 
  |  723 |    0 |                    } catch (Exception e) { | 
  |  724 |    0 |                            LOG.error("Unable to copy " + original, e); | 
  |  725 |    0 |                    }  | 
  |  726 |    0 |                    return copy;  | 
  |  727 |     | 
           }  | 
  |  728 |     | 
     | 
  |  729 |     | 
           protected AttributeDefinition getNonDataDictionaryAttributeDefinition(KimTypeAttributeInfo typeAttribute) { | 
  |  730 |    0 |                    KimNonDataDictionaryAttributeDefinition definition = new KimNonDataDictionaryAttributeDefinition();  | 
  |  731 |    0 |                    definition.setName(typeAttribute.getAttributeName());  | 
  |  732 |    0 |                    definition.setLabel(typeAttribute.getAttributeLabel());  | 
  |  733 |    0 |                    definition.setSortCode(typeAttribute.getSortCode());  | 
  |  734 |    0 |                    definition.setKimAttrDefnId(typeAttribute.getKimAttributeId());  | 
  |  735 |    0 |                    return definition;  | 
  |  736 |     | 
           }  | 
  |  737 |     | 
     | 
  |  738 |     | 
     | 
  |  739 |     | 
     | 
  |  740 |     | 
           @Override  | 
  |  741 |     | 
           public AttributeDefinitionMap getAttributeDefinitions(String kimTypeId) { | 
  |  742 |     | 
     | 
  |  743 |     | 
     | 
  |  744 |    0 |                            List<String> uniqueAttributes = getUniqueAttributes(kimTypeId);  | 
  |  745 |    0 |                            AttributeDefinitionMap definitions = new AttributeDefinitionMap();  | 
  |  746 |    0 |                    KimTypeInfo kimType = getTypeInfoService().getKimType(kimTypeId);  | 
  |  747 |    0 |                    if ( kimType != null ) { | 
  |  748 |    0 |                                    String nsCode = kimType.getNamespaceCode();                  | 
  |  749 |    0 |                                    for (KimTypeAttributeInfo typeAttribute : kimType.getAttributeDefinitions()) { | 
  |  750 |    0 |                                            AttributeDefinition definition = null;  | 
  |  751 |    0 |                                            if (typeAttribute.getComponentName() == null) { | 
  |  752 |    0 |                                                    definition = getNonDataDictionaryAttributeDefinition(typeAttribute);  | 
  |  753 |     | 
                                           } else { | 
  |  754 |    0 |                                                    definition = getDataDictionaryAttributeDefinition(nsCode,kimTypeId,typeAttribute);  | 
  |  755 |     | 
                                           }  | 
  |  756 |     | 
     | 
  |  757 |    0 |                                            if (definition != null) { | 
  |  758 |    0 |                                                    if(uniqueAttributes!=null && uniqueAttributes.contains(definition.getName())){ | 
  |  759 |    0 |                                                            definition.setUnique(true);  | 
  |  760 |     | 
                                                   }  | 
  |  761 |     | 
                                                     | 
  |  762 |     | 
                                                     | 
  |  763 |     | 
                                                     | 
  |  764 |     | 
                                                     | 
  |  765 |     | 
     | 
  |  766 |     | 
                                                     | 
  |  767 |     | 
                                                     | 
  |  768 |     | 
                                                     | 
  |  769 |    0 |                                                    definitions.put(typeAttribute.getSortCode(), definition);  | 
  |  770 |     | 
                                           }  | 
  |  771 |    0 |                                    }  | 
  |  772 |     | 
                                     | 
  |  773 |    0 |                    } else { | 
  |  774 |    0 |                            LOG.warn( "Unable to resolve KIM Type: " + kimTypeId + " - returning an empty AttributeDefinitionMap." );  | 
  |  775 |     | 
                   }  | 
  |  776 |     | 
     | 
  |  777 |    0 |                    return definitions;  | 
  |  778 |     | 
           }  | 
  |  779 |     | 
     | 
  |  780 |    0 |            protected final String COMMA_SEPARATOR = ", ";  | 
  |  781 |     | 
     | 
  |  782 |     | 
           protected void validateRequiredAttributesAgainstReceived(AttributeSet receivedAttributes){ | 
  |  783 |     | 
                     | 
  |  784 |    0 |                    if ( !isCheckRequiredAttributes() ) { | 
  |  785 |    0 |                            return;  | 
  |  786 |     | 
                   }  | 
  |  787 |     | 
                     | 
  |  788 |    0 |                    if ( requiredAttributes == null || requiredAttributes.isEmpty() ) { | 
  |  789 |    0 |                            return;  | 
  |  790 |     | 
                   }  | 
  |  791 |    0 |                    List<String> missingAttributes = new ArrayList<String>();  | 
  |  792 |     | 
                     | 
  |  793 |    0 |                    if ( receivedAttributes == null || receivedAttributes.isEmpty() ) { | 
  |  794 |    0 |                            return;                  | 
  |  795 |     | 
                   } else { | 
  |  796 |    0 |                            for( String requiredAttribute : requiredAttributes ) { | 
  |  797 |    0 |                                    if( !receivedAttributes.containsKey(requiredAttribute) ) { | 
  |  798 |    0 |                                            missingAttributes.add(requiredAttribute);  | 
  |  799 |     | 
                                   }  | 
  |  800 |     | 
                           }  | 
  |  801 |     | 
                   }  | 
  |  802 |    0 |            if(missingAttributes.size()>0) { | 
  |  803 |    0 |                    StringBuffer errorMessage = new StringBuffer();  | 
  |  804 |    0 |                    Iterator<String> attribIter = missingAttributes.iterator();  | 
  |  805 |    0 |                    while ( attribIter.hasNext() ) { | 
  |  806 |    0 |                            errorMessage.append( attribIter.next() );  | 
  |  807 |    0 |                            if( attribIter.hasNext() ) { | 
  |  808 |    0 |                                    errorMessage.append( COMMA_SEPARATOR );  | 
  |  809 |     | 
                           }  | 
  |  810 |     | 
                   }  | 
  |  811 |    0 |                    errorMessage.append( " not found in required attributes for this type." );  | 
  |  812 |    0 |                throw new KimTypeAttributeValidationException(errorMessage.toString());  | 
  |  813 |     | 
           }  | 
  |  814 |    0 |            }  | 
  |  815 |     | 
     | 
  |  816 |     | 
             | 
  |  817 |     | 
     | 
  |  818 |     | 
     | 
  |  819 |     | 
     | 
  |  820 |     | 
     | 
  |  821 |     | 
     | 
  |  822 |     | 
           @Override  | 
  |  823 |     | 
           public List<String> getWorkflowRoutingAttributes(String routeLevel) { | 
  |  824 |    0 |                    return workflowRoutingAttributes;  | 
  |  825 |     | 
           }  | 
  |  826 |     | 
             | 
  |  827 |     | 
           @Override  | 
  |  828 |     | 
           public boolean validateUniqueAttributes(String kimTypeId, AttributeSet newAttributes, AttributeSet oldAttributes){ | 
  |  829 |    0 |                    boolean areAttributesUnique = true;  | 
  |  830 |    0 |                    List<String> uniqueAttributes = getUniqueAttributes(kimTypeId);  | 
  |  831 |    0 |                    if(uniqueAttributes==null || uniqueAttributes.isEmpty()){ | 
  |  832 |    0 |                            areAttributesUnique = true;  | 
  |  833 |     | 
                   } else{ | 
  |  834 |    0 |                            if(areAttributesEqual(uniqueAttributes, newAttributes, oldAttributes)){ | 
  |  835 |    0 |                                    areAttributesUnique = false;  | 
  |  836 |     | 
                           }  | 
  |  837 |     | 
                   }  | 
  |  838 |    0 |                    return areAttributesUnique;  | 
  |  839 |     | 
           }  | 
  |  840 |     | 
             | 
  |  841 |     | 
           protected boolean areAttributesEqual(List<String> uniqueAttributeNames, AttributeSet aSet1, AttributeSet aSet2){ | 
  |  842 |     | 
                   String attrVal1;  | 
  |  843 |     | 
                   String attrVal2;  | 
  |  844 |    0 |                    StringValueComparator comparator = StringValueComparator.getInstance();  | 
  |  845 |    0 |                    for(String uniqueAttributeName: uniqueAttributeNames){ | 
  |  846 |    0 |                            attrVal1 = getAttributeValue(aSet1, uniqueAttributeName);  | 
  |  847 |    0 |                            attrVal2 = getAttributeValue(aSet2, uniqueAttributeName);  | 
  |  848 |    0 |                            attrVal1 = attrVal1==null?"":attrVal1;  | 
  |  849 |    0 |                            attrVal2 = attrVal2==null?"":attrVal2;  | 
  |  850 |    0 |                            if(comparator.compare(attrVal1, attrVal2)!=0){ | 
  |  851 |    0 |                                    return false;  | 
  |  852 |     | 
                           }  | 
  |  853 |     | 
                   }  | 
  |  854 |    0 |                    return true;  | 
  |  855 |     | 
           }  | 
  |  856 |     | 
     | 
  |  857 |     | 
           protected AttributeSet getErrorAttributeSet(String attributeNameKey, String errorKey, String[] errorArguments){ | 
  |  858 |    0 |                    AttributeSet validationErrors = new AttributeSet();  | 
  |  859 |    0 |                    GlobalVariables.getMessageMap().putError(attributeNameKey, errorKey, errorArguments);  | 
  |  860 |    0 |                    List<String> attributeErrors = extractErrorsFromGlobalVariablesErrorMap(attributeNameKey);  | 
  |  861 |    0 |                    if(attributeErrors!=null){ | 
  |  862 |    0 |                            for(String err:attributeErrors){ | 
  |  863 |    0 |                                    validationErrors.put(attributeNameKey, err);  | 
  |  864 |     | 
                           }  | 
  |  865 |     | 
                   }  | 
  |  866 |    0 |                    return validationErrors;  | 
  |  867 |     | 
           }  | 
  |  868 |     | 
             | 
  |  869 |     | 
       protected boolean areAllAttributeValuesEmpty(AttributeSet attributes){ | 
  |  870 |    0 |                boolean areAllAttributesEmpty = true;  | 
  |  871 |    0 |                if(attributes!=null) { | 
  |  872 |    0 |                            for(String attributeNameKey: attributes.keySet()){ | 
  |  873 |    0 |                                    if(StringUtils.isNotEmpty(attributes.get(attributeNameKey))){ | 
  |  874 |    0 |                                            areAllAttributesEmpty = false;  | 
  |  875 |    0 |                                            break;  | 
  |  876 |     | 
                                   }  | 
  |  877 |     | 
                           }  | 
  |  878 |     | 
                   }  | 
  |  879 |    0 |                return areAllAttributesEmpty;  | 
  |  880 |     | 
       }  | 
  |  881 |     | 
     | 
  |  882 |     | 
           protected String getAttributeValue(AttributeSet aSet, String attributeName){ | 
  |  883 |    0 |                    if(StringUtils.isEmpty(attributeName)) { | 
  |  884 |    0 |                            return null;  | 
  |  885 |     | 
                   }  | 
  |  886 |    0 |                    for(String attributeNameKey: aSet.keySet()){ | 
  |  887 |    0 |                            if(attributeName.equals(attributeNameKey)) { | 
  |  888 |    0 |                                    return aSet.get(attributeNameKey);  | 
  |  889 |     | 
                           }  | 
  |  890 |     | 
                   }  | 
  |  891 |    0 |                    return null;  | 
  |  892 |     | 
           }  | 
  |  893 |     | 
             | 
  |  894 |     | 
           @Override  | 
  |  895 |     | 
           public List<String> getUniqueAttributes(String kimTypeId){ | 
  |  896 |    0 |                    KimTypeInfo kimType = getTypeInfoService().getKimType(kimTypeId);  | 
  |  897 |    0 |            List<String> uniqueAttributes = new ArrayList<String>();  | 
  |  898 |    0 |            if ( kimType != null ) { | 
  |  899 |    0 |                    for(KimTypeAttributeInfo attributeDefinition: kimType.getAttributeDefinitions()){ | 
  |  900 |    0 |                            uniqueAttributes.add(attributeDefinition.getAttributeName());  | 
  |  901 |     | 
                   }  | 
  |  902 |     | 
           } else { | 
  |  903 |    0 |                    LOG.error("Unable to retrieve a KimTypeInfo for a null kimTypeId in getUniqueAttributes()"); | 
  |  904 |     | 
           }  | 
  |  905 |    0 |            return uniqueAttributes;  | 
  |  906 |     | 
           }  | 
  |  907 |     | 
     | 
  |  908 |     | 
           @Override  | 
  |  909 |     | 
           public AttributeSet validateUnmodifiableAttributes(String kimTypeId, AttributeSet originalAttributeSet, AttributeSet newAttributeSet){ | 
  |  910 |    0 |                    AttributeSet validationErrors = new AttributeSet();  | 
  |  911 |    0 |                    List<String> attributeErrors = null;  | 
  |  912 |    0 |                    KimTypeInfo kimType = getTypeInfoService().getKimType(kimTypeId);  | 
  |  913 |    0 |                    List<String> uniqueAttributes = getUniqueAttributes(kimTypeId);  | 
  |  914 |    0 |                    for(String attributeNameKey: uniqueAttributes){ | 
  |  915 |    0 |                            KimTypeAttributeInfo attr = kimType.getAttributeDefinitionByName(attributeNameKey);  | 
  |  916 |    0 |                            String mainAttributeValue = getAttributeValue(originalAttributeSet, attributeNameKey);  | 
  |  917 |    0 |                            String delegationAttributeValue = getAttributeValue(newAttributeSet, attributeNameKey);  | 
  |  918 |    0 |                            attributeErrors = null;  | 
  |  919 |    0 |                            if(!StringUtils.equals(mainAttributeValue, delegationAttributeValue)){ | 
  |  920 |    0 |                                    GlobalVariables.getMessageMap().putError(  | 
  |  921 |     | 
                                           attributeNameKey, RiceKeyConstants.ERROR_CANT_BE_MODIFIED,   | 
  |  922 |     | 
                                           dataDictionaryService.getAttributeLabel(attr.getComponentName(), attributeNameKey));  | 
  |  923 |    0 |                                    attributeErrors = extractErrorsFromGlobalVariablesErrorMap(attributeNameKey);  | 
  |  924 |     | 
                           }  | 
  |  925 |    0 |                            if(attributeErrors!=null){ | 
  |  926 |    0 |                                    for(String err:attributeErrors){ | 
  |  927 |    0 |                                            validationErrors.put(attributeNameKey, err);  | 
  |  928 |     | 
                                   }  | 
  |  929 |     | 
                           }  | 
  |  930 |    0 |                    }  | 
  |  931 |    0 |                    return validationErrors;  | 
  |  932 |     | 
           }  | 
  |  933 |     | 
     | 
  |  934 |     | 
           public boolean isCheckRequiredAttributes() { | 
  |  935 |    0 |                    return this.checkRequiredAttributes;  | 
  |  936 |     | 
           }  | 
  |  937 |     | 
     | 
  |  938 |     | 
           public void setCheckRequiredAttributes(boolean checkRequiredAttributes) { | 
  |  939 |    0 |                    this.checkRequiredAttributes = checkRequiredAttributes;  | 
  |  940 |    0 |            }  | 
  |  941 |     | 
     | 
  |  942 |     | 
           @Override  | 
  |  943 |     | 
           public AttributeSet validateAttributesAgainstExisting(String kimTypeId, AttributeSet newAttributes, AttributeSet oldAttributes){ | 
  |  944 |    0 |                    return new AttributeSet();  | 
  |  945 |     | 
           }  | 
  |  946 |     | 
     | 
  |  947 |     | 
           protected String getClosestParentDocumentTypeName(  | 
  |  948 |     | 
                           DocumentType documentType,  | 
  |  949 |     | 
                           Set<String> potentialParentDocumentTypeNames) { | 
  |  950 |    0 |                    if ( potentialParentDocumentTypeNames == null || documentType == null ) { | 
  |  951 |    0 |                            return null;  | 
  |  952 |     | 
                   }  | 
  |  953 |    0 |                    if (potentialParentDocumentTypeNames.contains(documentType.getName())) { | 
  |  954 |    0 |                            return documentType.getName();  | 
  |  955 |     | 
                   }   | 
  |  956 |    0 |                    if ((documentType.getDocTypeParentId() == null)  | 
  |  957 |     | 
                                   || documentType.getDocTypeParentId().equals(  | 
  |  958 |     | 
                                                   documentType.getDocumentTypeId())) { | 
  |  959 |    0 |                            return null;  | 
  |  960 |     | 
                   }   | 
  |  961 |    0 |                    return getClosestParentDocumentTypeName(documentType  | 
  |  962 |     | 
                                   .getParentDocType(), potentialParentDocumentTypeNames);  | 
  |  963 |     | 
           }  | 
  |  964 |     | 
   }  |