Search in sources :

Example 1 with ValueAssignable

use of org.broadleafcommerce.common.value.ValueAssignable in project BroadleafCommerce by BroadleafCommerce.

the class MapFieldPersistenceProvider method populateValue.

@Override
public MetadataProviderResponse populateValue(PopulateValueRequest populateValueRequest, Serializable instance) {
    if (!canHandlePersistence(populateValueRequest, instance)) {
        return MetadataProviderResponse.NOT_HANDLED;
    }
    boolean dirty = false;
    try {
        Class<?> startingValueType = getStartingValueType(populateValueRequest);
        Class<?> valueType = getValueType(populateValueRequest, startingValueType);
        if (ValueAssignable.class.isAssignableFrom(valueType)) {
            boolean persistValue = false;
            ValueAssignable assignableValue;
            Object parent;
            try {
                parent = populateValueRequest.getFieldManager().getFieldValue(instance, populateValueRequest.getProperty().getName());
                if (parent instanceof List) {
                    parent = ((List) parent).get(0);
                }
                if (parent == null) {
                    parent = startingValueType.newInstance();
                    if (!startingValueType.equals(valueType)) {
                        setupJoinEntityParent(populateValueRequest, instance, parent);
                    }
                    populateValueRequest.getFieldManager().setFieldValue(instance, populateValueRequest.getProperty().getName(), parent);
                    persistValue = true;
                }
                assignableValue = establishAssignableValue(populateValueRequest, parent);
            } catch (FieldNotAvailableException e) {
                throw new IllegalArgumentException(e);
            }
            dirty = persistValue || (assignableValue != null && ObjectUtils.notEqual(assignableValue.getValue(), populateValueRequest.getProperty().getValue()));
            if (dirty) {
                updateAssignableValue(populateValueRequest, instance, parent, valueType, persistValue, assignableValue);
            }
        } else {
            // handle the map value set itself
            if (MetadataProviderResponse.NOT_HANDLED == super.populateValue(populateValueRequest, instance)) {
                return MetadataProviderResponse.NOT_HANDLED;
            }
        }
    } catch (Exception e) {
        throw new PersistenceException(e);
    }
    populateValueRequest.getProperty().setIsDirty(dirty);
    return MetadataProviderResponse.HANDLED_BREAK;
}
Also used : ValueAssignable(org.broadleafcommerce.common.value.ValueAssignable) FieldNotAvailableException(org.broadleafcommerce.openadmin.server.service.persistence.module.FieldNotAvailableException) PersistenceException(org.broadleafcommerce.openadmin.server.service.persistence.PersistenceException) List(java.util.List) FieldNotAvailableException(org.broadleafcommerce.openadmin.server.service.persistence.module.FieldNotAvailableException) PersistenceException(org.broadleafcommerce.openadmin.server.service.persistence.PersistenceException)

Example 2 with ValueAssignable

use of org.broadleafcommerce.common.value.ValueAssignable in project BroadleafCommerce by BroadleafCommerce.

the class MapFieldPersistenceProvider method extractValue.

@Override
public MetadataProviderResponse extractValue(ExtractValueRequest extractValueRequest, Property property) throws PersistenceException {
    if (!canHandleExtraction(extractValueRequest, property)) {
        return MetadataProviderResponse.NOT_HANDLED;
    }
    checkValue: {
        if (extractValueRequest.getRequestedValue() != null) {
            Object requestedValue = extractValueRequest.getRequestedValue();
            if (!StringUtils.isEmpty(extractValueRequest.getMetadata().getToOneTargetProperty())) {
                try {
                    requestedValue = extractValueRequest.getFieldManager().getFieldValue(requestedValue, extractValueRequest.getMetadata().getToOneTargetProperty());
                } catch (IllegalAccessException e) {
                    throw ExceptionHelper.refineException(e);
                } catch (FieldNotAvailableException e) {
                    throw ExceptionHelper.refineException(e);
                }
            }
            if (requestedValue instanceof ValueAssignable) {
                ValueAssignable assignableValue = (ValueAssignable) requestedValue;
                String val = (String) assignableValue.getValue();
                property.setValue(val);
                property.setDisplayValue(extractValueRequest.getDisplayVal());
                break checkValue;
            }
        }
        if (MetadataProviderResponse.NOT_HANDLED == super.extractValue(extractValueRequest, property)) {
            return MetadataProviderResponse.NOT_HANDLED;
        }
    }
    return MetadataProviderResponse.HANDLED;
}
Also used : ValueAssignable(org.broadleafcommerce.common.value.ValueAssignable) FieldNotAvailableException(org.broadleafcommerce.openadmin.server.service.persistence.module.FieldNotAvailableException)

Example 3 with ValueAssignable

use of org.broadleafcommerce.common.value.ValueAssignable in project BroadleafCommerce by BroadleafCommerce.

the class SelectizeCollectionUtils method intersectIterable.

private static Collection intersectIterable(final Iterable a, final Iterable b) {
    Collection response;
    if (!IterableUtils.isEmpty(a) && (a instanceof ArrayList) && !IterableUtils.isEmpty(b) && (b instanceof ArrayList)) {
        // TODO this is a bit of a hack to allow the intersection of two collections of different types. This is primarily
        // used to facilitate some MVEL execution. We really should be fixing the MVEL to call a method that retrieves
        // a list of Strings, rather than a list of ValueAssignables.
        Object aVal = ((ArrayList) a).get(0);
        Object bVal = ((ArrayList) b).get(0);
        if (aVal instanceof ValueAssignable && bVal instanceof String) {
            response = valueAssignableIntersection(a, b);
        } else {
            response = CollectionUtils.intersection(a, b);
        }
    } else {
        response = CollectionUtils.intersection(a, b);
    }
    return response;
}
Also used : ValueAssignable(org.broadleafcommerce.common.value.ValueAssignable) ArrayList(java.util.ArrayList) Collection(java.util.Collection)

Example 4 with ValueAssignable

use of org.broadleafcommerce.common.value.ValueAssignable in project BroadleafCommerce by BroadleafCommerce.

the class MapStructurePersistenceModule method add.

@Override
public Entity add(PersistencePackage persistencePackage) throws ServiceException {
    String[] customCriteria = persistencePackage.getCustomCriteria();
    if (customCriteria != null && customCriteria.length > 0) {
        LOG.warn("custom persistence handlers and custom criteria not supported for add types other than BASIC");
    }
    PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
    Entity entity = persistencePackage.getEntity();
    MapStructure mapStructure = (MapStructure) persistencePerspective.getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.MAPSTRUCTURE);
    if (!mapStructure.getMutable()) {
        throw new SecurityServiceException("Field not mutable");
    }
    try {
        Map<String, FieldMetadata> ceilingMergedProperties = getSimpleMergedProperties(entity.getType()[0], persistencePerspective);
        String mapKey = entity.findProperty(mapStructure.getKeyPropertyName()).getValue();
        if (StringUtils.isEmpty(mapKey)) {
            entity.addValidationError(mapStructure.getKeyPropertyName(), RequiredPropertyValidator.ERROR_MESSAGE);
            LOG.debug("No key property passed in for map, failing validation");
        }
        if (ceilingMergedProperties.containsKey(mapStructure.getMapProperty() + FieldManager.MAPFIELDSEPARATOR + mapKey)) {
            throw new ServiceException("\"" + mapKey + "\" is a reserved property name.");
        }
        Serializable instance = persistenceManager.getDynamicEntityDao().retrieve(Class.forName(entity.getType()[0]), Long.valueOf(entity.findProperty("symbolicId").getValue()));
        Assert.isTrue(instance != null, "Entity not found");
        FieldManager fieldManager = getFieldManager();
        Map map = (Map) fieldManager.getFieldValue(instance, mapStructure.getMapProperty());
        if (map.containsKey(mapKey)) {
            entity.addValidationError(mapStructure.getKeyPropertyName(), "keyExistsValidationError");
        }
        if (StringUtils.isNotBlank(mapStructure.getMapKeyValueProperty())) {
            Property p = entity.findProperty("key");
            Property newP = new Property();
            newP.setName(mapStructure.getMapKeyValueProperty());
            newP.setValue(p.getValue());
            newP.setIsDirty(p.getIsDirty());
            entity.addProperty(newP);
        }
        PersistentClass persistentClass = persistenceManager.getDynamicEntityDao().getPersistentClass(mapStructure.getValueClassName());
        Map<String, FieldMetadata> valueUnfilteredMergedProperties;
        if (persistentClass == null) {
            valueUnfilteredMergedProperties = persistenceManager.getDynamicEntityDao().getPropertiesForPrimitiveClass(((SimpleValueMapStructure) mapStructure).getValuePropertyName(), ((SimpleValueMapStructure) mapStructure).getValuePropertyFriendlyName(), Class.forName(mapStructure.getValueClassName()), Class.forName(entity.getType()[0]), MergedPropertyType.MAPSTRUCTUREVALUE);
        } else {
            String valueClassName = mapStructure.getValueClassName();
            Class<?>[] mapEntities = persistenceManager.getPolymorphicEntities(valueClassName);
            valueUnfilteredMergedProperties = persistenceManager.getDynamicEntityDao().getMergedProperties(valueClassName, mapEntities, null, new String[] {}, new ForeignKey[] {}, MergedPropertyType.MAPSTRUCTUREVALUE, persistencePerspective.getPopulateToOneFields(), persistencePerspective.getIncludeFields(), persistencePerspective.getExcludeFields(), persistencePerspective.getConfigurationKey(), "");
        }
        Map<String, FieldMetadata> valueMergedProperties = filterOutCollectionMetadata(valueUnfilteredMergedProperties);
        if (persistentClass != null) {
            Serializable valueInstance = (Serializable) Class.forName(mapStructure.getValueClassName()).newInstance();
            valueInstance = createPopulatedInstance(valueInstance, entity, valueMergedProperties, false);
            if (valueInstance instanceof ValueAssignable) {
                // This is likely a OneToMany map (see productAttributes) whose map key is actually the name field from
                // the mapped entity.
                ((ValueAssignable) valueInstance).setName(entity.findProperty(mapStructure.getKeyPropertyName()).getValue());
            }
            if (mapStructure.getManyToField() != null) {
                // Need to fulfill a bi-directional association back to the parent entity
                fieldManager.setFieldValue(valueInstance, mapStructure.getManyToField(), instance);
            }
            valueInstance = persistenceManager.getDynamicEntityDao().persist(valueInstance);
            /*
                 * TODO this map manipulation code currently assumes the key value is a String. This should be widened to accept
                 * additional types of primitive objects.
                 */
            map.put(mapKey, valueInstance);
        } else {
            String propertyName = ((SimpleValueMapStructure) mapStructure).getValuePropertyName();
            String value = entity.findProperty(propertyName).getValue();
            Object convertedPrimitive = convertPrimitiveBasedOnType(propertyName, value, valueMergedProperties);
            map.put(mapKey, convertedPrimitive);
        }
        Entity[] responses = getMapRecords(instance, mapStructure, ceilingMergedProperties, valueMergedProperties, entity.findProperty("symbolicId"), null);
        for (Entity response : responses) {
            if (response.findProperty(mapStructure.getKeyPropertyName()).getValue().equals(persistencePackage.getEntity().findProperty(mapStructure.getKeyPropertyName()).getValue())) {
                return response;
            }
        }
        return responses[0];
    } catch (Exception e) {
        throw new ServiceException("Problem updating entity : " + e.getMessage(), e);
    }
}
Also used : Entity(org.broadleafcommerce.openadmin.dto.Entity) SecurityServiceException(org.broadleafcommerce.common.exception.SecurityServiceException) Serializable(java.io.Serializable) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) ForeignKey(org.broadleafcommerce.openadmin.dto.ForeignKey) ParseException(java.text.ParseException) ServiceException(org.broadleafcommerce.common.exception.ServiceException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SecurityServiceException(org.broadleafcommerce.common.exception.SecurityServiceException) MapStructure(org.broadleafcommerce.openadmin.dto.MapStructure) SimpleValueMapStructure(org.broadleafcommerce.openadmin.dto.SimpleValueMapStructure) ValueAssignable(org.broadleafcommerce.common.value.ValueAssignable) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) ServiceException(org.broadleafcommerce.common.exception.ServiceException) SecurityServiceException(org.broadleafcommerce.common.exception.SecurityServiceException) PersistentClass(org.hibernate.mapping.PersistentClass) CriteriaTransferObject(org.broadleafcommerce.openadmin.dto.CriteriaTransferObject) Map(java.util.Map) Property(org.broadleafcommerce.openadmin.dto.Property) SimpleValueMapStructure(org.broadleafcommerce.openadmin.dto.SimpleValueMapStructure) PersistentClass(org.hibernate.mapping.PersistentClass)

Example 5 with ValueAssignable

use of org.broadleafcommerce.common.value.ValueAssignable in project BroadleafCommerce by BroadleafCommerce.

the class MvelTestOverloadUtils method intersection.

public static Collection intersection(final Iterable a, final Iterable b) {
    Collection response;
    if (!IterableUtils.isEmpty(a) && (a instanceof ArrayList) && !IterableUtils.isEmpty(b) && (b instanceof ArrayList)) {
        // TODO this is a bit of a hack to allow the intersection of two collections of different types. This is primarily
        // used to facilitate some MVEL execution. We really should be fixing the MVEL to call a method that retrieves
        // a list of Strings, rather than a list of ValueAssignables.
        Object aVal = ((ArrayList) a).get(0);
        Object bVal = ((ArrayList) b).get(0);
        if (aVal instanceof ValueAssignable && bVal instanceof String) {
            response = valueAssignableIntersection(a, b);
        } else {
            response = CollectionUtils.intersection(a, b);
        }
    } else {
        if (a == null || b == null) {
            return CollectionUtils.emptyCollection();
        }
        response = CollectionUtils.intersection(a, b);
    }
    return response;
}
Also used : ValueAssignable(org.broadleafcommerce.common.value.ValueAssignable) ArrayList(java.util.ArrayList) Collection(java.util.Collection)

Aggregations

ValueAssignable (org.broadleafcommerce.common.value.ValueAssignable)5 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 FieldNotAvailableException (org.broadleafcommerce.openadmin.server.service.persistence.module.FieldNotAvailableException)2 Serializable (java.io.Serializable)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ParseException (java.text.ParseException)1 List (java.util.List)1 Map (java.util.Map)1 SecurityServiceException (org.broadleafcommerce.common.exception.SecurityServiceException)1 ServiceException (org.broadleafcommerce.common.exception.ServiceException)1 BasicFieldMetadata (org.broadleafcommerce.openadmin.dto.BasicFieldMetadata)1 CriteriaTransferObject (org.broadleafcommerce.openadmin.dto.CriteriaTransferObject)1 Entity (org.broadleafcommerce.openadmin.dto.Entity)1 FieldMetadata (org.broadleafcommerce.openadmin.dto.FieldMetadata)1 ForeignKey (org.broadleafcommerce.openadmin.dto.ForeignKey)1 MapStructure (org.broadleafcommerce.openadmin.dto.MapStructure)1 PersistencePerspective (org.broadleafcommerce.openadmin.dto.PersistencePerspective)1 Property (org.broadleafcommerce.openadmin.dto.Property)1 SimpleValueMapStructure (org.broadleafcommerce.openadmin.dto.SimpleValueMapStructure)1