protected void buildAdornedTargetCollectionMetadata(
      Class<?> parentClass,
      Class<?> targetClass,
      Map<String, FieldMetadata> attributes,
      FieldInfo field,
      FieldMetadataOverride adornedTargetCollectionMetadata,
      DynamicEntityDao dynamicEntityDao) {
    AdornedTargetCollectionMetadata serverMetadata =
        (AdornedTargetCollectionMetadata) attributes.get(field.getName());

    Class<?> resolvedClass = parentClass == null ? targetClass : parentClass;
    AdornedTargetCollectionMetadata metadata;
    if (serverMetadata != null) {
      metadata = serverMetadata;
    } else {
      metadata = new AdornedTargetCollectionMetadata();
    }
    metadata.setTargetClass(targetClass.getName());
    metadata.setFieldName(field.getName());

    if (adornedTargetCollectionMetadata.getReadOnly() != null) {
      metadata.setMutable(!adornedTargetCollectionMetadata.getReadOnly());
    }
    if (adornedTargetCollectionMetadata.getShowIfProperty() != null) {
      metadata.setShowIfProperty(adornedTargetCollectionMetadata.getShowIfProperty());
    }

    org.broadleafcommerce.openadmin.dto.OperationTypes dtoOperationTypes =
        new org.broadleafcommerce.openadmin.dto.OperationTypes(
            OperationType.ADORNEDTARGETLIST,
            OperationType.ADORNEDTARGETLIST,
            OperationType.ADORNEDTARGETLIST,
            OperationType.ADORNEDTARGETLIST,
            OperationType.BASIC);
    if (adornedTargetCollectionMetadata.getAddType() != null) {
      dtoOperationTypes.setAddType(adornedTargetCollectionMetadata.getAddType());
    }
    if (adornedTargetCollectionMetadata.getRemoveType() != null) {
      dtoOperationTypes.setRemoveType(adornedTargetCollectionMetadata.getRemoveType());
    }
    if (adornedTargetCollectionMetadata.getFetchType() != null) {
      dtoOperationTypes.setFetchType(adornedTargetCollectionMetadata.getFetchType());
    }
    if (adornedTargetCollectionMetadata.getInspectType() != null) {
      dtoOperationTypes.setInspectType(adornedTargetCollectionMetadata.getInspectType());
    }
    if (adornedTargetCollectionMetadata.getUpdateType() != null) {
      dtoOperationTypes.setUpdateType(adornedTargetCollectionMetadata.getUpdateType());
    }

    // don't allow additional non-persistent properties or additional foreign keys for an advanced
    // collection datasource - they don't make sense in this context
    PersistencePerspective persistencePerspective;
    if (serverMetadata != null) {
      persistencePerspective = metadata.getPersistencePerspective();
      persistencePerspective.setOperationTypes(dtoOperationTypes);
    } else {
      persistencePerspective =
          new PersistencePerspective(dtoOperationTypes, new String[] {}, new ForeignKey[] {});
      metadata.setPersistencePerspective(persistencePerspective);
    }

    String parentObjectProperty = null;
    if (serverMetadata != null) {
      parentObjectProperty =
          ((AdornedTargetList)
                  serverMetadata
                      .getPersistencePerspective()
                      .getPersistencePerspectiveItems()
                      .get(PersistencePerspectiveItemType.ADORNEDTARGETLIST))
              .getLinkedObjectPath();
    }
    if (!StringUtils.isEmpty(adornedTargetCollectionMetadata.getParentObjectProperty())) {
      parentObjectProperty = adornedTargetCollectionMetadata.getParentObjectProperty();
    }
    if (parentObjectProperty == null && !StringUtils.isEmpty(field.getOneToManyMappedBy())) {
      parentObjectProperty = field.getOneToManyMappedBy();
    }
    if (parentObjectProperty == null && !StringUtils.isEmpty(field.getManyToManyMappedBy())) {
      parentObjectProperty = field.getManyToManyMappedBy();
    }
    if (StringUtils.isEmpty(parentObjectProperty)) {
      throw new IllegalArgumentException(
          "Unable to infer a parentObjectProperty for the @AdminPresentationAdornedTargetCollection annotated field("
              + field.getName()
              + "). If not using the mappedBy property of @OneToMany or @ManyToMany, please make sure to explicitly define the parentObjectProperty property");
    }

    String sortProperty = null;
    if (serverMetadata != null) {
      sortProperty =
          ((AdornedTargetList)
                  serverMetadata
                      .getPersistencePerspective()
                      .getPersistencePerspectiveItems()
                      .get(PersistencePerspectiveItemType.ADORNEDTARGETLIST))
              .getSortField();
    }
    if (!StringUtils.isEmpty(adornedTargetCollectionMetadata.getSortProperty())) {
      sortProperty = adornedTargetCollectionMetadata.getSortProperty();
    }

    metadata.setParentObjectClass(resolvedClass.getName());
    if (adornedTargetCollectionMetadata.getMaintainedAdornedTargetFields() != null) {
      metadata.setMaintainedAdornedTargetFields(
          adornedTargetCollectionMetadata.getMaintainedAdornedTargetFields());
    }
    if (adornedTargetCollectionMetadata.getGridVisibleFields() != null) {
      metadata.setGridVisibleFields(adornedTargetCollectionMetadata.getGridVisibleFields());
    }
    String parentObjectIdProperty = null;
    if (serverMetadata != null) {
      parentObjectIdProperty =
          ((AdornedTargetList)
                  serverMetadata
                      .getPersistencePerspective()
                      .getPersistencePerspectiveItems()
                      .get(PersistencePerspectiveItemType.ADORNEDTARGETLIST))
              .getLinkedIdProperty();
    }
    if (adornedTargetCollectionMetadata.getParentObjectIdProperty() != null) {
      parentObjectIdProperty = adornedTargetCollectionMetadata.getParentObjectIdProperty();
    }
    String targetObjectProperty = null;
    if (serverMetadata != null) {
      targetObjectProperty =
          ((AdornedTargetList)
                  serverMetadata
                      .getPersistencePerspective()
                      .getPersistencePerspectiveItems()
                      .get(PersistencePerspectiveItemType.ADORNEDTARGETLIST))
              .getTargetObjectPath();
    }
    if (adornedTargetCollectionMetadata.getTargetObjectProperty() != null) {
      targetObjectProperty = adornedTargetCollectionMetadata.getTargetObjectProperty();
    }

    String joinEntityClass = null;
    if (serverMetadata != null) {
      joinEntityClass =
          ((AdornedTargetList)
                  serverMetadata
                      .getPersistencePerspective()
                      .getPersistencePerspectiveItems()
                      .get(PersistencePerspectiveItemType.ADORNEDTARGETLIST))
              .getJoinEntityClass();
    }
    if (adornedTargetCollectionMetadata.getJoinEntityClass() != null) {
      joinEntityClass = adornedTargetCollectionMetadata.getJoinEntityClass();
    }

    Class<?> collectionTarget = null;
    try {
      checkCeiling:
      {
        try {
          ParameterizedType pt = (ParameterizedType) field.getGenericType();
          java.lang.reflect.Type collectionType = pt.getActualTypeArguments()[0];
          String ceilingEntityName = ((Class<?>) collectionType).getName();
          collectionTarget = entityConfiguration.lookupEntityClass(ceilingEntityName);
          break checkCeiling;
        } catch (NoSuchBeanDefinitionException e) {
          // We weren't successful at looking at entity configuration to find the type of this
          // collection.
          // We will continue and attempt to find it via the Hibernate annotations
        }
        if (!StringUtils.isEmpty(field.getOneToManyTargetEntity())
            && !void.class.getName().equals(field.getOneToManyTargetEntity())) {
          collectionTarget = Class.forName(field.getOneToManyTargetEntity());
          break checkCeiling;
        }
        if (!StringUtils.isEmpty(field.getManyToManyTargetEntity())
            && !void.class.getName().equals(field.getManyToManyTargetEntity())) {
          collectionTarget = Class.forName(field.getManyToManyTargetEntity());
          break checkCeiling;
        }
      }
      if (StringUtils.isNotBlank(joinEntityClass)) {
        collectionTarget = Class.forName(joinEntityClass);
      }
    } catch (ClassNotFoundException e) {
      throw new RuntimeException(e);
    }
    if (collectionTarget == null) {
      throw new IllegalArgumentException(
          "Unable to infer the type of the collection from the targetEntity property of a OneToMany or ManyToMany collection.");
    }
    Field collectionTargetField =
        dynamicEntityDao.getFieldManager().getField(collectionTarget, targetObjectProperty);
    ManyToOne manyToOne = collectionTargetField.getAnnotation(ManyToOne.class);
    String ceiling = null;
    checkCeiling:
    {
      if (manyToOne != null && manyToOne.targetEntity() != void.class) {
        ceiling = manyToOne.targetEntity().getName();
        break checkCeiling;
      }
      ceiling = collectionTargetField.getType().getName();
    }
    if (!StringUtils.isEmpty(ceiling)) {
      metadata.setCollectionCeilingEntity(ceiling);
    }

    String targetObjectIdProperty = null;
    if (serverMetadata != null) {
      targetObjectIdProperty =
          ((AdornedTargetList)
                  serverMetadata
                      .getPersistencePerspective()
                      .getPersistencePerspectiveItems()
                      .get(PersistencePerspectiveItemType.ADORNEDTARGETLIST))
              .getTargetIdProperty();
    }
    if (adornedTargetCollectionMetadata.getTargetObjectIdProperty() != null) {
      targetObjectIdProperty = adornedTargetCollectionMetadata.getTargetObjectIdProperty();
    }
    Boolean isAscending = true;
    if (serverMetadata != null) {
      isAscending =
          ((AdornedTargetList)
                  serverMetadata
                      .getPersistencePerspective()
                      .getPersistencePerspectiveItems()
                      .get(PersistencePerspectiveItemType.ADORNEDTARGETLIST))
              .getSortAscending();
    }
    if (adornedTargetCollectionMetadata.isSortAscending() != null) {
      isAscending = adornedTargetCollectionMetadata.isSortAscending();
    }

    if (serverMetadata != null) {
      AdornedTargetList adornedTargetList =
          (AdornedTargetList)
              serverMetadata
                  .getPersistencePerspective()
                  .getPersistencePerspectiveItems()
                  .get(PersistencePerspectiveItemType.ADORNEDTARGETLIST);
      adornedTargetList.setCollectionFieldName(field.getName());
      adornedTargetList.setLinkedObjectPath(parentObjectProperty);
      adornedTargetList.setLinkedIdProperty(parentObjectIdProperty);
      adornedTargetList.setTargetObjectPath(targetObjectProperty);
      adornedTargetList.setTargetIdProperty(targetObjectIdProperty);
      adornedTargetList.setJoinEntityClass(joinEntityClass);
      adornedTargetList.setAdornedTargetEntityClassname(collectionTarget.getName());
      adornedTargetList.setSortField(sortProperty);
      adornedTargetList.setSortAscending(isAscending);
      adornedTargetList.setMutable(metadata.isMutable());
    } else {
      AdornedTargetList adornedTargetList =
          new AdornedTargetList(
              field.getName(),
              parentObjectProperty,
              parentObjectIdProperty,
              targetObjectProperty,
              targetObjectIdProperty,
              collectionTarget.getName(),
              sortProperty,
              isAscending);
      adornedTargetList.setJoinEntityClass(joinEntityClass);
      adornedTargetList.setMutable(metadata.isMutable());
      persistencePerspective.addPersistencePerspectiveItem(
          PersistencePerspectiveItemType.ADORNEDTARGETLIST, adornedTargetList);
    }

    if (adornedTargetCollectionMetadata.getExcluded() != null) {
      if (LOG.isDebugEnabled()) {
        if (adornedTargetCollectionMetadata.getExcluded()) {
          LOG.debug(
              "buildAdornedTargetCollectionMetadata:Excluding "
                  + field.getName()
                  + " because it was explicitly declared in config");
        } else {
          LOG.debug(
              "buildAdornedTargetCollectionMetadata:Showing "
                  + field.getName()
                  + " because it was explicitly declared in config");
        }
      }
      metadata.setExcluded(adornedTargetCollectionMetadata.getExcluded());
    }
    if (adornedTargetCollectionMetadata.getFriendlyName() != null) {
      metadata.setFriendlyName(adornedTargetCollectionMetadata.getFriendlyName());
    }
    if (adornedTargetCollectionMetadata.getSecurityLevel() != null) {
      metadata.setSecurityLevel(adornedTargetCollectionMetadata.getSecurityLevel());
    }
    if (adornedTargetCollectionMetadata.getOrder() != null) {
      metadata.setOrder(adornedTargetCollectionMetadata.getOrder());
    }

    if (adornedTargetCollectionMetadata.getTab() != null) {
      metadata.setTab(adornedTargetCollectionMetadata.getTab());
    }
    if (adornedTargetCollectionMetadata.getTabOrder() != null) {
      metadata.setTabOrder(adornedTargetCollectionMetadata.getTabOrder());
    }

    if (adornedTargetCollectionMetadata.getCustomCriteria() != null) {
      metadata.setCustomCriteria(adornedTargetCollectionMetadata.getCustomCriteria());
    }

    if (adornedTargetCollectionMetadata.getUseServerSideInspectionCache() != null) {
      persistencePerspective.setUseServerSideInspectionCache(
          adornedTargetCollectionMetadata.getUseServerSideInspectionCache());
    }

    if (adornedTargetCollectionMetadata.isIgnoreAdornedProperties() != null) {
      metadata.setIgnoreAdornedProperties(
          adornedTargetCollectionMetadata.isIgnoreAdornedProperties());
    }
    if (adornedTargetCollectionMetadata.getCurrencyCodeField() != null) {
      metadata.setCurrencyCodeField(adornedTargetCollectionMetadata.getCurrencyCodeField());
    }

    attributes.put(field.getName(), metadata);
  }
 @Override
 public FieldProviderResponse populateValue(
     PopulateValueRequest populateValueRequest, Serializable instance) {
   boolean dirty = false;
   try {
     // handle some additional field settings (if applicable)
     Class<?> valueType = null;
     String valueClassName = populateValueRequest.getMetadata().getMapFieldValueClass();
     if (valueClassName != null) {
       valueType = Class.forName(valueClassName);
     }
     if (valueType == null) {
       valueType = populateValueRequest.getReturnType();
     }
     if (valueType == null) {
       throw new IllegalAccessException(
           "Unable to determine the valueType for the rule field ("
               + populateValueRequest.getProperty().getName()
               + ")");
     }
     if (ValueAssignable.class.isAssignableFrom(valueType)) {
       ValueAssignable assignableValue;
       try {
         assignableValue =
             (ValueAssignable)
                 populateValueRequest
                     .getFieldManager()
                     .getFieldValue(instance, populateValueRequest.getProperty().getName());
       } catch (FieldNotAvailableException e) {
         throw new IllegalArgumentException(e);
       }
       String key =
           populateValueRequest
               .getProperty()
               .getName()
               .substring(
                   populateValueRequest
                           .getProperty()
                           .getName()
                           .indexOf(FieldManager.MAPFIELDSEPARATOR)
                       + FieldManager.MAPFIELDSEPARATOR.length(),
                   populateValueRequest.getProperty().getName().length());
       boolean persistValue = false;
       if (assignableValue == null) {
         assignableValue = (ValueAssignable) valueType.newInstance();
         persistValue = true;
         dirty = true;
       } else {
         dirty = assignableValue.getValue().equals(populateValueRequest.getProperty().getValue());
         populateValueRequest.getProperty().setOriginalValue(String.valueOf(assignableValue));
         populateValueRequest
             .getProperty()
             .setOriginalDisplayValue(String.valueOf(assignableValue));
       }
       assignableValue.setName(key);
       assignableValue.setValue(populateValueRequest.getProperty().getValue());
       String fieldName =
           populateValueRequest
               .getProperty()
               .getName()
               .substring(
                   0,
                   populateValueRequest
                       .getProperty()
                       .getName()
                       .indexOf(FieldManager.MAPFIELDSEPARATOR));
       Field field =
           populateValueRequest.getFieldManager().getField(instance.getClass(), fieldName);
       FieldInfo fieldInfo = buildFieldInfo(field);
       String manyToField = null;
       if (populateValueRequest.getMetadata().getManyToField() != null) {
         manyToField = populateValueRequest.getMetadata().getManyToField();
       }
       if (manyToField == null) {
         manyToField = fieldInfo.getManyToManyMappedBy();
       }
       if (manyToField == null) {
         manyToField = fieldInfo.getOneToManyMappedBy();
       }
       if (manyToField != null) {
         String propertyName = populateValueRequest.getProperty().getName();
         Object middleInstance = instance;
         if (propertyName.contains(".")) {
           propertyName = propertyName.substring(0, propertyName.lastIndexOf("."));
           middleInstance =
               populateValueRequest.getFieldManager().getFieldValue(instance, propertyName);
         }
         populateValueRequest
             .getFieldManager()
             .setFieldValue(assignableValue, manyToField, middleInstance);
       }
       if (Searchable.class.isAssignableFrom(valueType)) {
         ((Searchable) assignableValue)
             .setSearchable(populateValueRequest.getMetadata().getSearchable());
       }
       if (persistValue) {
         populateValueRequest
             .getPersistenceManager()
             .getDynamicEntityDao()
             .persist(assignableValue);
         populateValueRequest
             .getFieldManager()
             .setFieldValue(
                 instance, populateValueRequest.getProperty().getName(), assignableValue);
       }
     } else {
       // handle the map value set itself
       if (FieldProviderResponse.NOT_HANDLED
           == super.populateValue(populateValueRequest, instance)) {
         return FieldProviderResponse.NOT_HANDLED;
       }
     }
   } catch (Exception e) {
     throw new PersistenceException(e);
   }
   populateValueRequest.getProperty().setIsDirty(dirty);
   return FieldProviderResponse.HANDLED_BREAK;
 }