Search in sources :

Example 6 with SimpleValueMapStructure

use of org.broadleafcommerce.openadmin.dto.SimpleValueMapStructure in project BroadleafCommerce by BroadleafCommerce.

the class MapFieldMetadataProvider method buildMapMetadata.

protected void buildMapMetadata(Class<?> parentClass, Class<?> targetClass, Map<String, FieldMetadata> attributes, FieldInfo field, FieldMetadataOverride map, DynamicEntityDao dynamicEntityDao, String prefix) {
    MapMetadata serverMetadata = (MapMetadata) attributes.get(field.getName());
    Class<?> resolvedClass = parentClass == null ? targetClass : parentClass;
    MapMetadata metadata;
    if (serverMetadata != null) {
        metadata = serverMetadata;
    } else {
        metadata = new MapMetadata();
    }
    if (map.getReadOnly() != null) {
        metadata.setMutable(!map.getReadOnly());
    }
    if (map.getShowIfProperty() != null) {
        metadata.setShowIfProperty(map.getShowIfProperty());
    }
    if (map.getShowIfFieldEquals() != null) {
        metadata.setShowIfFieldEquals(map.getShowIfFieldEquals());
    }
    metadata.setPrefix(prefix);
    metadata.setTargetClass(targetClass.getName());
    metadata.setFieldName(field.getName());
    org.broadleafcommerce.openadmin.dto.OperationTypes dtoOperationTypes = new org.broadleafcommerce.openadmin.dto.OperationTypes(OperationType.MAP, OperationType.MAP, OperationType.MAP, OperationType.MAP, OperationType.MAP);
    if (map.getAddType() != null) {
        dtoOperationTypes.setAddType(map.getAddType());
    }
    if (map.getRemoveType() != null) {
        dtoOperationTypes.setRemoveType(map.getRemoveType());
    }
    if (map.getFetchType() != null) {
        dtoOperationTypes.setFetchType(map.getFetchType());
    }
    if (map.getInspectType() != null) {
        dtoOperationTypes.setInspectType(map.getInspectType());
    }
    if (map.getUpdateType() != null) {
        dtoOperationTypes.setUpdateType(map.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 parentObjectClass = resolvedClass.getName();
    Map idMetadata;
    if (parentClass != null) {
        idMetadata = dynamicEntityDao.getIdMetadata(parentClass);
    } else {
        idMetadata = dynamicEntityDao.getIdMetadata(targetClass);
    }
    String parentObjectIdField = (String) idMetadata.get("name");
    String keyClassName = null;
    if (serverMetadata != null) {
        keyClassName = ((MapStructure) metadata.getPersistencePerspective().getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.MAPSTRUCTURE)).getKeyClassName();
    }
    if (map.getKeyClass() != null && !void.class.getName().equals(map.getKeyClass())) {
        keyClassName = map.getKeyClass();
    }
    if (keyClassName == null) {
        java.lang.reflect.Type type = field.getGenericType();
        if (type instanceof ParameterizedType) {
            ParameterizedType pType = (ParameterizedType) type;
            Class<?> clazz = (Class<?>) pType.getActualTypeArguments()[0];
            if (!ArrayUtils.isEmpty(dynamicEntityDao.getAllPolymorphicEntitiesFromCeiling(clazz))) {
                throw new IllegalArgumentException("Key class for AdminPresentationMap was determined to be a JPA managed type. Only primitive types for the key type are currently supported.");
            }
            keyClassName = clazz.getName();
        }
    }
    if (keyClassName == null) {
        keyClassName = String.class.getName();
    }
    String keyPropertyName = "key";
    String mapKeyValueProperty = "";
    if (StringUtils.isNotBlank(field.getMapKey())) {
        mapKeyValueProperty = field.getMapKey();
    }
    if (StringUtils.isNotBlank(map.getMapKeyValueProperty())) {
        mapKeyValueProperty = map.getMapKeyValueProperty();
    }
    String keyPropertyFriendlyName = null;
    if (serverMetadata != null) {
        keyPropertyFriendlyName = ((MapStructure) serverMetadata.getPersistencePerspective().getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.MAPSTRUCTURE)).getKeyPropertyFriendlyName();
    }
    if (map.getKeyPropertyFriendlyName() != null) {
        keyPropertyFriendlyName = map.getKeyPropertyFriendlyName();
    }
    Boolean deleteEntityUponRemove = null;
    if (serverMetadata != null) {
        deleteEntityUponRemove = ((MapStructure) serverMetadata.getPersistencePerspective().getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.MAPSTRUCTURE)).getDeleteValueEntity();
    }
    if (map.isDeleteEntityUponRemove() != null) {
        deleteEntityUponRemove = map.isDeleteEntityUponRemove();
    }
    String valuePropertyName = "value";
    String valuePropertyFriendlyName = null;
    if (serverMetadata != null) {
        MapStructure structure = (MapStructure) serverMetadata.getPersistencePerspective().getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.MAPSTRUCTURE);
        if (structure instanceof SimpleValueMapStructure) {
            valuePropertyFriendlyName = ((SimpleValueMapStructure) structure).getValuePropertyFriendlyName();
        } else {
            valuePropertyFriendlyName = "";
        }
    }
    if (map.getValuePropertyFriendlyName() != null) {
        valuePropertyFriendlyName = map.getValuePropertyFriendlyName();
    }
    if (map.getMediaField() != null) {
        metadata.setMediaField(map.getMediaField());
    }
    if (map.getToOneTargetProperty() != null) {
        metadata.setToOneTargetProperty(map.getToOneTargetProperty());
    }
    if (map.getToOneParentProperty() != null) {
        metadata.setToOneParentProperty((map.getToOneParentProperty()));
    }
    if (map.getValueClass() != null && !void.class.getName().equals(map.getValueClass())) {
        metadata.setValueClassName(map.getValueClass());
    }
    if (metadata.getValueClassName() == null) {
        java.lang.reflect.Type type = field.getGenericType();
        if (type instanceof ParameterizedType) {
            ParameterizedType pType = (ParameterizedType) type;
            Class<?> clazz = (Class<?>) pType.getActualTypeArguments()[1];
            Class<?>[] entities = dynamicEntityDao.getAllPolymorphicEntitiesFromCeiling(clazz);
            if (!ArrayUtils.isEmpty(entities)) {
                metadata.setValueClassName(entities[entities.length - 1].getName());
            }
        }
    }
    if (metadata.getValueClassName() == null) {
        if (!StringUtils.isEmpty(field.getManyToManyTargetEntity())) {
            metadata.setValueClassName(field.getManyToManyTargetEntity());
        }
    }
    if (metadata.getValueClassName() == null) {
        metadata.setValueClassName(String.class.getName());
    }
    Boolean simpleValue = null;
    if (map.getSimpleValue() != null && map.getSimpleValue() != UnspecifiedBooleanType.UNSPECIFIED) {
        simpleValue = map.getSimpleValue() == UnspecifiedBooleanType.TRUE;
    }
    if (simpleValue == null) {
        java.lang.reflect.Type type = field.getGenericType();
        if (type instanceof ParameterizedType) {
            ParameterizedType pType = (ParameterizedType) type;
            Class<?> clazz = (Class<?>) pType.getActualTypeArguments()[1];
            Class<?>[] entities = dynamicEntityDao.getAllPolymorphicEntitiesFromCeiling(clazz);
            simpleValue = ArrayUtils.isEmpty(entities);
        }
    }
    if (simpleValue == null) {
        // ManyToMany manyToMany = field.getAnnotation(ManyToMany.class);
        if (!StringUtils.isEmpty(field.getManyToManyTargetEntity())) {
            simpleValue = false;
        }
    }
    if (simpleValue == null) {
        throw new IllegalArgumentException("Unable to infer if the value for the map is of a complex or simple type based on any parameterized type or ManyToMany annotation. Please explicitly set the isSimpleValue property.");
    }
    metadata.setSimpleValue(simpleValue);
    if (map.getKeys() != null) {
        metadata.setKeys(map.getKeys());
    }
    metadata.setMapKeyValueProperty(mapKeyValueProperty);
    if (map.getMapKeyOptionEntityClass() != null) {
        if (!void.class.getName().equals(map.getMapKeyOptionEntityClass())) {
            metadata.setMapKeyOptionEntityClass(map.getMapKeyOptionEntityClass());
        } else {
            metadata.setMapKeyOptionEntityClass("");
        }
    }
    if (map.getMapKeyOptionEntityDisplayField() != null) {
        metadata.setMapKeyOptionEntityDisplayField(map.getMapKeyOptionEntityDisplayField());
    }
    if (map.getMapKeyOptionEntityValueField() != null) {
        metadata.setMapKeyOptionEntityValueField(map.getMapKeyOptionEntityValueField());
    }
    if (map.getForceFreeFormKeys() != null) {
        if (!map.getForceFreeFormKeys() && ArrayUtils.isEmpty(metadata.getKeys()) && (StringUtils.isEmpty(metadata.getMapKeyOptionEntityClass()) || StringUtils.isEmpty(metadata.getMapKeyOptionEntityValueField()) || StringUtils.isEmpty(metadata.getMapKeyOptionEntityDisplayField()))) {
            throw new IllegalArgumentException("Could not ascertain method for generating key options for the annotated map (" + field.getName() + "). Must specify either an array of AdminPresentationMapKey values for the keys property, or utilize the mapOptionKeyClass, mapOptionKeyDisplayField and mapOptionKeyValueField properties. If you wish to allow free form entry for key values, then set forceFreeFormKeys on AdminPresentationMap.");
        }
    }
    MapStructure mapStructure;
    if (serverMetadata != null) {
        ForeignKey foreignKey = (ForeignKey) persistencePerspective.getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.FOREIGNKEY);
        foreignKey.setManyToField(parentObjectIdField);
        foreignKey.setForeignKeyClass(parentObjectClass);
        if (metadata.isSimpleValue()) {
            mapStructure = (SimpleValueMapStructure) persistencePerspective.getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.MAPSTRUCTURE);
            mapStructure.setKeyClassName(keyClassName);
            mapStructure.setKeyPropertyName(keyPropertyName);
            mapStructure.setKeyPropertyFriendlyName(keyPropertyFriendlyName);
            mapStructure.setValueClassName(metadata.getValueClassName());
            ((SimpleValueMapStructure) mapStructure).setValuePropertyName(valuePropertyName);
            ((SimpleValueMapStructure) mapStructure).setValuePropertyFriendlyName(valuePropertyFriendlyName);
            mapStructure.setMapProperty(prefix + field.getName());
            mapStructure.setMutable(metadata.isMutable());
        } else {
            mapStructure = (MapStructure) persistencePerspective.getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.MAPSTRUCTURE);
            mapStructure.setKeyClassName(keyClassName);
            mapStructure.setKeyPropertyName(keyPropertyName);
            mapStructure.setKeyPropertyFriendlyName(keyPropertyFriendlyName);
            mapStructure.setValueClassName(metadata.getValueClassName());
            mapStructure.setMapProperty(prefix + field.getName());
            mapStructure.setDeleteValueEntity(deleteEntityUponRemove);
            mapStructure.setMutable(metadata.isMutable());
        }
    } else {
        ForeignKey foreignKey = new ForeignKey(parentObjectIdField, parentObjectClass);
        persistencePerspective.addPersistencePerspectiveItem(PersistencePerspectiveItemType.FOREIGNKEY, foreignKey);
        if (metadata.isSimpleValue()) {
            mapStructure = new SimpleValueMapStructure(keyClassName, keyPropertyName, keyPropertyFriendlyName, metadata.getValueClassName(), valuePropertyName, valuePropertyFriendlyName, prefix + field.getName(), mapKeyValueProperty);
            mapStructure.setMutable(metadata.isMutable());
        } else {
            mapStructure = new MapStructure(keyClassName, keyPropertyName, keyPropertyFriendlyName, metadata.getValueClassName(), prefix + field.getName(), deleteEntityUponRemove, mapKeyValueProperty);
            mapStructure.setMutable(metadata.isMutable());
        }
        persistencePerspective.addPersistencePerspectiveItem(PersistencePerspectiveItemType.MAPSTRUCTURE, mapStructure);
    }
    if (!StringUtils.isEmpty(map.getManyToField())) {
        mapStructure.setManyToField(map.getManyToField());
    }
    if (mapStructure.getManyToField() == null) {
        // try to infer the value
        if (field.getManyToManyMappedBy() != null) {
            mapStructure.setManyToField(field.getManyToManyMappedBy());
        }
    }
    if (mapStructure.getManyToField() == null) {
        // try to infer the value
        if (field.getOneToManyMappedBy() != null) {
            mapStructure.setManyToField(field.getOneToManyMappedBy());
        }
    }
    if (map.getExcluded() != null) {
        if (LOG.isDebugEnabled()) {
            if (map.getExcluded()) {
                LOG.debug("buildMapMetadata:Excluding " + field.getName() + " because it was explicitly declared in config");
            } else {
                LOG.debug("buildMapMetadata:Showing " + field.getName() + " because it was explicitly declared in config");
            }
        }
        metadata.setExcluded(map.getExcluded());
    }
    if (map.getLazyFetch() != null) {
        metadata.setLazyFetch(map.getLazyFetch());
    }
    if (map.getManualFetch() != null) {
        metadata.setManualFetch(map.getManualFetch());
    }
    if (map.getFriendlyName() != null) {
        metadata.setFriendlyName(map.getFriendlyName());
    }
    if (map.getSecurityLevel() != null) {
        metadata.setSecurityLevel(map.getSecurityLevel());
    }
    if (map.getOrder() != null) {
        metadata.setOrder(map.getOrder());
    }
    if (map.getTab() != null) {
        metadata.setTab(map.getTab());
    }
    if (map.getTabOrder() != null) {
        metadata.setTabOrder(map.getTabOrder());
    }
    if (map.getGroup() != null) {
        metadata.setGroup(map.getGroup());
    }
    if (map.getCustomCriteria() != null) {
        metadata.setCustomCriteria(map.getCustomCriteria());
    }
    if (map.getUseServerSideInspectionCache() != null) {
        persistencePerspective.setUseServerSideInspectionCache(map.getUseServerSideInspectionCache());
    }
    if (map.getCurrencyCodeField() != null) {
        metadata.setCurrencyCodeField(map.getCurrencyCodeField());
    }
    if (map.getForceFreeFormKeys() != null) {
        metadata.setForceFreeFormKeys(map.getForceFreeFormKeys());
    }
    attributes.put(field.getName(), metadata);
}
Also used : ForeignKey(org.broadleafcommerce.openadmin.dto.ForeignKey) ParameterizedType(java.lang.reflect.ParameterizedType) MapStructure(org.broadleafcommerce.openadmin.dto.MapStructure) SimpleValueMapStructure(org.broadleafcommerce.openadmin.dto.SimpleValueMapStructure) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) AdminPresentationOperationTypes(org.broadleafcommerce.common.presentation.AdminPresentationOperationTypes) MapMetadata(org.broadleafcommerce.openadmin.dto.MapMetadata) HashMap(java.util.HashMap) Map(java.util.Map) AdminPresentationMap(org.broadleafcommerce.common.presentation.AdminPresentationMap) SimpleValueMapStructure(org.broadleafcommerce.openadmin.dto.SimpleValueMapStructure)

Aggregations

SimpleValueMapStructure (org.broadleafcommerce.openadmin.dto.SimpleValueMapStructure)6 ForeignKey (org.broadleafcommerce.openadmin.dto.ForeignKey)5 MapStructure (org.broadleafcommerce.openadmin.dto.MapStructure)5 PersistencePerspective (org.broadleafcommerce.openadmin.dto.PersistencePerspective)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 ParseException (java.text.ParseException)4 SecurityServiceException (org.broadleafcommerce.common.exception.SecurityServiceException)4 ServiceException (org.broadleafcommerce.common.exception.ServiceException)4 BasicFieldMetadata (org.broadleafcommerce.openadmin.dto.BasicFieldMetadata)4 Entity (org.broadleafcommerce.openadmin.dto.Entity)4 FieldMetadata (org.broadleafcommerce.openadmin.dto.FieldMetadata)4 PersistentClass (org.hibernate.mapping.PersistentClass)4 Serializable (java.io.Serializable)3 Map (java.util.Map)3 Property (org.broadleafcommerce.openadmin.dto.Property)3 ParameterizedType (java.lang.reflect.ParameterizedType)1 Timestamp (java.sql.Timestamp)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1