@Override
 public FieldProviderResponse overrideViaXml(
     OverrideViaXmlRequest overrideViaXmlRequest, Map<String, FieldMetadata> metadata) {
   Map<String, FieldMetadataOverride> overrides =
       getTargetedOverride(
           overrideViaXmlRequest.getRequestedConfigKey(),
           overrideViaXmlRequest.getRequestedCeilingEntity());
   if (overrides != null) {
     for (String propertyName : overrides.keySet()) {
       final FieldMetadataOverride localMetadata = overrides.get(propertyName);
       for (String key : metadata.keySet()) {
         if (key.equals(propertyName)) {
           try {
             if (metadata.get(key) instanceof AdornedTargetCollectionMetadata) {
               AdornedTargetCollectionMetadata serverMetadata =
                   (AdornedTargetCollectionMetadata) metadata.get(key);
               if (serverMetadata.getTargetClass() != null) {
                 Class<?> targetClass = Class.forName(serverMetadata.getTargetClass());
                 Class<?> parentClass = null;
                 if (serverMetadata.getOwningClass() != null) {
                   parentClass = Class.forName(serverMetadata.getOwningClass());
                 }
                 String fieldName = serverMetadata.getFieldName();
                 Field field =
                     overrideViaXmlRequest
                         .getDynamicEntityDao()
                         .getFieldManager()
                         .getField(targetClass, fieldName);
                 Map<String, FieldMetadata> temp = new HashMap<String, FieldMetadata>(1);
                 temp.put(field.getName(), serverMetadata);
                 FieldInfo info = buildFieldInfo(field);
                 buildAdornedTargetCollectionMetadata(
                     parentClass,
                     targetClass,
                     temp,
                     info,
                     localMetadata,
                     overrideViaXmlRequest.getDynamicEntityDao());
                 serverMetadata = (AdornedTargetCollectionMetadata) temp.get(field.getName());
                 metadata.put(key, serverMetadata);
                 if (overrideViaXmlRequest.getParentExcluded()) {
                   if (LOG.isDebugEnabled()) {
                     LOG.debug(
                         "applyAdornedTargetCollectionMetadataOverrides:Excluding "
                             + key
                             + "because parent is marked as excluded.");
                   }
                   serverMetadata.setExcluded(true);
                 }
               }
             }
           } catch (Exception e) {
             throw new RuntimeException(e);
           }
         }
       }
     }
   }
   return FieldProviderResponse.HANDLED;
 }
 protected void buildAdminPresentationAdornedTargetCollectionOverride(
     String prefix,
     Boolean isParentExcluded,
     Map<String, FieldMetadata> mergedProperties,
     Map<String, AdminPresentationAdornedTargetCollectionOverride>
         presentationAdornedTargetCollectionOverrides,
     String propertyName,
     String key,
     DynamicEntityDao dynamicEntityDao) {
   AdminPresentationAdornedTargetCollectionOverride override =
       presentationAdornedTargetCollectionOverrides.get(propertyName);
   if (override != null) {
     AdminPresentationAdornedTargetCollection annot = override.value();
     if (annot != null) {
       String testKey = prefix + key;
       if ((testKey.startsWith(propertyName + ".") || testKey.equals(propertyName))
           && annot.excluded()) {
         FieldMetadata metadata = mergedProperties.get(key);
         if (LOG.isDebugEnabled()) {
           LOG.debug(
               "buildAdminPresentationAdornedTargetCollectionOverride:Excluding "
                   + key
                   + "because an override annotation declared "
                   + testKey
                   + "to be excluded");
         }
         metadata.setExcluded(true);
         return;
       }
       if ((testKey.startsWith(propertyName + ".") || testKey.equals(propertyName))
           && !annot.excluded()) {
         FieldMetadata metadata = mergedProperties.get(key);
         if (!isParentExcluded) {
           if (LOG.isDebugEnabled()) {
             LOG.debug(
                 "buildAdminPresentationAdornedTargetCollectionOverride:Showing "
                     + key
                     + "because an override annotation declared "
                     + testKey
                     + " to not be excluded");
           }
           metadata.setExcluded(false);
         }
       }
       if (!(mergedProperties.get(key) instanceof AdornedTargetCollectionMetadata)) {
         return;
       }
       AdornedTargetCollectionMetadata serverMetadata =
           (AdornedTargetCollectionMetadata) mergedProperties.get(key);
       if (serverMetadata.getTargetClass() != null) {
         try {
           Class<?> targetClass = Class.forName(serverMetadata.getTargetClass());
           Class<?> parentClass = null;
           if (serverMetadata.getOwningClass() != null) {
             parentClass = Class.forName(serverMetadata.getOwningClass());
           }
           String fieldName = serverMetadata.getFieldName();
           Field field = dynamicEntityDao.getFieldManager().getField(targetClass, fieldName);
           FieldMetadataOverride localMetadata =
               constructAdornedTargetCollectionMetadataOverride(annot);
           // do not include the previous metadata - we want to construct a fresh metadata from the
           // override annotation
           Map<String, FieldMetadata> temp = new HashMap<String, FieldMetadata>(1);
           FieldInfo info = buildFieldInfo(field);
           buildAdornedTargetCollectionMetadata(
               parentClass, targetClass, temp, info, localMetadata, dynamicEntityDao);
           AdornedTargetCollectionMetadata result =
               (AdornedTargetCollectionMetadata) temp.get(field.getName());
           result.setInheritedFromType(serverMetadata.getInheritedFromType());
           result.setAvailableToTypes(serverMetadata.getAvailableToTypes());
           mergedProperties.put(key, result);
           if (isParentExcluded) {
             if (LOG.isDebugEnabled()) {
               LOG.debug(
                   "buildAdminPresentationAdornedTargetCollectionOverride:Excluding "
                       + key
                       + "because the parent was excluded");
             }
             serverMetadata.setExcluded(true);
           }
         } catch (Exception e) {
           throw new RuntimeException(e);
         }
       }
     }
   }
 }
  @Override
  public FieldProviderResponse overrideViaAnnotation(
      OverrideViaAnnotationRequest overrideViaAnnotationRequest,
      Map<String, FieldMetadata> metadata) {
    if (!canHandleAnnotationOverride(overrideViaAnnotationRequest, metadata)) {
      return FieldProviderResponse.NOT_HANDLED;
    }
    Map<String, AdminPresentationAdornedTargetCollectionOverride>
        presentationAdornedTargetCollectionOverrides =
            new HashMap<String, AdminPresentationAdornedTargetCollectionOverride>();

    AdminPresentationOverrides myOverrides =
        overrideViaAnnotationRequest
            .getRequestedEntity()
            .getAnnotation(AdminPresentationOverrides.class);
    if (myOverrides != null) {
      for (AdminPresentationAdornedTargetCollectionOverride myOverride :
          myOverrides.adornedTargetCollections()) {
        presentationAdornedTargetCollectionOverrides.put(myOverride.name(), myOverride);
      }
    }

    for (String propertyName : presentationAdornedTargetCollectionOverrides.keySet()) {
      for (String key : metadata.keySet()) {
        if (key.startsWith(propertyName)) {
          buildAdminPresentationAdornedTargetCollectionOverride(
              overrideViaAnnotationRequest.getPrefix(),
              overrideViaAnnotationRequest.getParentExcluded(),
              metadata,
              presentationAdornedTargetCollectionOverrides,
              propertyName,
              key,
              overrideViaAnnotationRequest.getDynamicEntityDao());
        }
      }
    }

    AdminPresentationMergeOverrides myMergeOverrides =
        overrideViaAnnotationRequest
            .getRequestedEntity()
            .getAnnotation(AdminPresentationMergeOverrides.class);
    if (myMergeOverrides != null) {
      for (AdminPresentationMergeOverride override : myMergeOverrides.value()) {
        String propertyName = override.name();
        Map<String, FieldMetadata> loopMap = new HashMap<String, FieldMetadata>();
        loopMap.putAll(metadata);
        for (Map.Entry<String, FieldMetadata> entry : loopMap.entrySet()) {
          if (entry.getKey().startsWith(propertyName) || StringUtils.isEmpty(propertyName)) {
            FieldMetadata targetMetadata = entry.getValue();
            if (targetMetadata instanceof AdornedTargetCollectionMetadata) {
              AdornedTargetCollectionMetadata serverMetadata =
                  (AdornedTargetCollectionMetadata) targetMetadata;
              if (serverMetadata.getTargetClass() != null) {
                try {
                  Class<?> targetClass = Class.forName(serverMetadata.getTargetClass());
                  Class<?> parentClass = null;
                  if (serverMetadata.getOwningClass() != null) {
                    parentClass = Class.forName(serverMetadata.getOwningClass());
                  }
                  String fieldName = serverMetadata.getFieldName();
                  Field field =
                      overrideViaAnnotationRequest
                          .getDynamicEntityDao()
                          .getFieldManager()
                          .getField(targetClass, fieldName);
                  Map<String, FieldMetadata> temp = new HashMap<String, FieldMetadata>(1);
                  temp.put(field.getName(), serverMetadata);
                  FieldInfo info = buildFieldInfo(field);
                  FieldMetadataOverride fieldMetadataOverride =
                      overrideAdornedTargetMergeMetadata(override);
                  if (serverMetadata.getExcluded() != null
                      && serverMetadata.getExcluded()
                      && (fieldMetadataOverride.getExcluded() == null
                          || fieldMetadataOverride.getExcluded())) {
                    continue;
                  }
                  buildAdornedTargetCollectionMetadata(
                      parentClass,
                      targetClass,
                      temp,
                      info,
                      fieldMetadataOverride,
                      overrideViaAnnotationRequest.getDynamicEntityDao());
                  serverMetadata = (AdornedTargetCollectionMetadata) temp.get(field.getName());
                  metadata.put(entry.getKey(), serverMetadata);
                } catch (Exception e) {
                  throw new RuntimeException(e);
                }
              }
            }
          }
        }
      }
    }

    return FieldProviderResponse.HANDLED;
  }