Search in sources :

Example 66 with FieldMetadata

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

the class MapStructurePersistenceModule method updateMergedProperties.

@Override
public void updateMergedProperties(PersistencePackage persistencePackage, Map<MergedPropertyType, Map<String, FieldMetadata>> allMergedProperties) throws ServiceException {
    String ceilingEntityFullyQualifiedClassname = persistencePackage.getCeilingEntityFullyQualifiedClassname();
    try {
        PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
        MapStructure mapStructure = (MapStructure) persistencePerspective.getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.MAPSTRUCTURE);
        if (mapStructure != null) {
            PersistentClass persistentClass = persistenceManager.getDynamicEntityDao().getPersistentClass(mapStructure.getKeyClassName());
            Map<String, FieldMetadata> keyMergedProperties;
            if (persistentClass == null) {
                keyMergedProperties = persistenceManager.getDynamicEntityDao().getPropertiesForPrimitiveClass(mapStructure.getKeyPropertyName(), mapStructure.getKeyPropertyFriendlyName(), Class.forName(mapStructure.getKeyClassName()), Class.forName(ceilingEntityFullyQualifiedClassname), MergedPropertyType.MAPSTRUCTUREKEY);
            } else {
                keyMergedProperties = persistenceManager.getDynamicEntityDao().getMergedProperties(mapStructure.getKeyClassName(), new Class[] { Class.forName(mapStructure.getKeyClassName()) }, null, new String[] {}, new ForeignKey[] {}, MergedPropertyType.MAPSTRUCTUREKEY, persistencePerspective.getPopulateToOneFields(), persistencePerspective.getIncludeFields(), persistencePerspective.getExcludeFields(), persistencePerspective.getConfigurationKey(), "");
            }
            allMergedProperties.put(MergedPropertyType.MAPSTRUCTUREKEY, keyMergedProperties);
            persistentClass = persistenceManager.getDynamicEntityDao().getPersistentClass(mapStructure.getValueClassName());
            Map<String, FieldMetadata> valueMergedProperties;
            if (persistentClass == null) {
                if (!SimpleValueMapStructure.class.isAssignableFrom(mapStructure.getClass())) {
                    throw new IllegalStateException("The map structure was determined to not be a simple value, but the system was unable to identify the entity designated for the map structure value(" + mapStructure.getValueClassName() + ")");
                }
                valueMergedProperties = persistenceManager.getDynamicEntityDao().getPropertiesForPrimitiveClass(((SimpleValueMapStructure) mapStructure).getValuePropertyName(), ((SimpleValueMapStructure) mapStructure).getValuePropertyFriendlyName(), Class.forName(mapStructure.getValueClassName()), Class.forName(ceilingEntityFullyQualifiedClassname), MergedPropertyType.MAPSTRUCTUREVALUE);
            } else {
                String valueClassName = mapStructure.getValueClassName();
                Class<?>[] entities = persistenceManager.getPolymorphicEntities(valueClassName);
                valueMergedProperties = persistenceManager.getDynamicEntityDao().getMergedProperties(valueClassName, entities, null, new String[] {}, new ForeignKey[] {}, MergedPropertyType.MAPSTRUCTUREVALUE, persistencePerspective.getPopulateToOneFields(), persistencePerspective.getIncludeFields(), persistencePerspective.getExcludeFields(), persistencePerspective.getConfigurationKey(), "");
            }
            allMergedProperties.put(MergedPropertyType.MAPSTRUCTUREVALUE, valueMergedProperties);
        }
    } catch (Exception e) {
        throw new ServiceException("Unable to fetch results for " + ceilingEntityFullyQualifiedClassname, e);
    }
}
Also used : 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) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) ServiceException(org.broadleafcommerce.common.exception.ServiceException) SecurityServiceException(org.broadleafcommerce.common.exception.SecurityServiceException) PersistentClass(org.hibernate.mapping.PersistentClass) SimpleValueMapStructure(org.broadleafcommerce.openadmin.dto.SimpleValueMapStructure) PersistentClass(org.hibernate.mapping.PersistentClass)

Example 67 with FieldMetadata

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

the class MapStructurePersistenceModule method update.

@Override
public Entity update(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 update 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 (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());
        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 (StringUtils.isEmpty(mapKey)) {
            entity.addValidationError(mapStructure.getKeyPropertyName(), RequiredPropertyValidator.ERROR_MESSAGE);
            LOG.debug("No key property passed in for map, failing validation");
        }
        populate: {
            if (persistentClass != null) {
                Serializable valueInstance = (Serializable) map.get(entity.findProperty("priorKey").getValue());
                if (valueInstance == null) {
                    valueInstance = procureSandBoxMapValue(mapStructure, entity);
                    if (valueInstance == null) {
                        break populate;
                    }
                }
                if (map.get(mapKey) != null && !map.get(mapKey).equals(valueInstance)) {
                    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);
                }
                // allow validation on other properties in order to show key validation errors along with all the other properties
                // validation errors
                valueInstance = createPopulatedInstance(valueInstance, entity, valueMergedProperties, false);
                if (StringUtils.isNotEmpty(mapKey) && !entity.isValidationFailure()) {
                    if (!entity.findProperty("priorKey").getValue().equals(mapKey)) {
                        map.remove(entity.findProperty("priorKey").getValue());
                    }
                    /*
                         * 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(entity.findProperty(mapStructure.getKeyPropertyName()).getValue(), valueInstance);
                }
            } else {
                if (StringUtils.isNotEmpty(mapKey) && !entity.isValidationFailure()) {
                    map.put(entity.findProperty(mapStructure.getKeyPropertyName()).getValue(), entity.findProperty(((SimpleValueMapStructure) mapStructure).getValuePropertyName()).getValue());
                }
            }
        }
        instance = persistenceManager.getDynamicEntityDao().merge(instance);
        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;
            }
        }
        // could be empty if reverting a sandbox item that has experienced a deletion. make sure to at least return an empty instance of Entity.
        return ArrayUtils.isEmpty(responses) ? new Entity() : 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) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) ServiceException(org.broadleafcommerce.common.exception.ServiceException) SecurityServiceException(org.broadleafcommerce.common.exception.SecurityServiceException) PersistentClass(org.hibernate.mapping.PersistentClass) Map(java.util.Map) Property(org.broadleafcommerce.openadmin.dto.Property) SimpleValueMapStructure(org.broadleafcommerce.openadmin.dto.SimpleValueMapStructure) PersistentClass(org.hibernate.mapping.PersistentClass)

Example 68 with FieldMetadata

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

the class MapStructurePersistenceModule method getMapRecords.

protected Entity[] getMapRecords(Serializable record, MapStructure mapStructure, Map<String, FieldMetadata> ceilingMergedProperties, Map<String, FieldMetadata> valueMergedProperties, Property symbolicIdProperty, String[] customCriteria) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, SecurityException, IllegalArgumentException, ClassNotFoundException, NoSuchFieldException {
    // compile a list of mapKeys that were used as mapFields
    List<String> mapFieldKeys = new ArrayList<String>();
    String mapProperty = mapStructure.getMapProperty();
    for (Map.Entry<String, FieldMetadata> entry : ceilingMergedProperties.entrySet()) {
        if (entry.getKey().startsWith(mapProperty + FieldManager.MAPFIELDSEPARATOR)) {
            mapFieldKeys.add(entry.getKey().substring(entry.getKey().indexOf(FieldManager.MAPFIELDSEPARATOR) + FieldManager.MAPFIELDSEPARATOR.length(), entry.getKey().length()));
        }
    }
    Collections.sort(mapFieldKeys);
    FieldManager fieldManager = getFieldManager();
    Map map;
    try {
        map = (Map) fieldManager.getFieldValue(record, mapProperty);
    } catch (FieldNotAvailableException e) {
        throw new IllegalArgumentException(e);
    }
    List<Entity> entities = new ArrayList<Entity>(map.size());
    for (Object key : map.keySet()) {
        if (key instanceof String && mapFieldKeys.contains(key)) {
            continue;
        }
        entities.add(getMapRecord(record.getClass().getName(), (Serializable) map.get(key), mapStructure, valueMergedProperties, symbolicIdProperty, key, customCriteria));
    }
    return entities.toArray(new Entity[entities.size()]);
}
Also used : Entity(org.broadleafcommerce.openadmin.dto.Entity) Serializable(java.io.Serializable) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) ArrayList(java.util.ArrayList) CriteriaTransferObject(org.broadleafcommerce.openadmin.dto.CriteriaTransferObject) Map(java.util.Map)

Example 69 with FieldMetadata

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

the class CustomPersistenceHandlerAdapter method getResultSet.

/**
 * This is a helper method that can be invoked as the last step in a custom inspect phase. It will assemble the
 * appropriate DynamicResultSet from the given parameters.
 */
protected DynamicResultSet getResultSet(PersistencePackage persistencePackage, InspectHelper helper, Map<String, FieldMetadata> metadata) throws ServiceException {
    String entityName = persistencePackage.getCeilingEntityFullyQualifiedClassname();
    try {
        if (helper instanceof PersistenceManager) {
            Class<?>[] entities = ((PersistenceManager) helper).getPolymorphicEntities(entityName);
            Map<MergedPropertyType, Map<String, FieldMetadata>> allMergedProperties = new HashMap<MergedPropertyType, Map<String, FieldMetadata>>();
            allMergedProperties.put(MergedPropertyType.PRIMARY, metadata);
            ClassMetadata mergedMetadata = helper.buildClassMetadata(entities, persistencePackage, allMergedProperties);
            DynamicResultSet results = new DynamicResultSet(mergedMetadata);
            return results;
        }
    } catch (ClassNotFoundException e) {
        throw new ServiceException(e);
    }
    return new DynamicResultSet();
}
Also used : ClassMetadata(org.broadleafcommerce.openadmin.dto.ClassMetadata) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) PersistenceManager(org.broadleafcommerce.openadmin.server.service.persistence.PersistenceManager) HashMap(java.util.HashMap) MergedPropertyType(org.broadleafcommerce.openadmin.dto.MergedPropertyType) ServiceException(org.broadleafcommerce.common.exception.ServiceException) HashMap(java.util.HashMap) Map(java.util.Map) DynamicResultSet(org.broadleafcommerce.openadmin.dto.DynamicResultSet)

Example 70 with FieldMetadata

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

the class TranslationCustomPersistenceHandler method add.

@Override
public Entity add(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
    Entity entity = persistencePackage.getEntity();
    try {
        // Get an instance of SystemProperty with the updated values from the form
        PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
        Translation adminInstance = (Translation) Class.forName(entity.getType()[0]).newInstance();
        Map<String, FieldMetadata> adminProperties = helper.getSimpleMergedProperties(Translation.class.getName(), persistencePerspective);
        adminInstance = (Translation) helper.createPopulatedInstance(adminInstance, entity, adminProperties, false);
        Translation res = translationService.getTranslation(adminInstance.getEntityType(), adminInstance.getEntityId(), adminInstance.getFieldName(), adminInstance.getLocaleCode());
        if (res == null) {
            adminInstance = dynamicEntityDao.merge(adminInstance);
            Entity adminEntity = helper.getRecord(adminProperties, adminInstance, null, null);
            return adminEntity;
        } else {
            Entity errorEntity = new Entity();
            errorEntity.addValidationError("localeCode", "translation.record.exists.for.locale");
            return errorEntity;
        }
    } catch (Exception e) {
        throw new ServiceException("Unable to perform add for entity: " + Translation.class.getName(), e);
    }
}
Also used : Entity(org.broadleafcommerce.openadmin.dto.Entity) Translation(org.broadleafcommerce.common.i18n.domain.Translation) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) ServiceException(org.broadleafcommerce.common.exception.ServiceException) ServiceException(org.broadleafcommerce.common.exception.ServiceException)

Aggregations

FieldMetadata (org.broadleafcommerce.openadmin.dto.FieldMetadata)114 BasicFieldMetadata (org.broadleafcommerce.openadmin.dto.BasicFieldMetadata)80 Entity (org.broadleafcommerce.openadmin.dto.Entity)51 ServiceException (org.broadleafcommerce.common.exception.ServiceException)46 PersistencePerspective (org.broadleafcommerce.openadmin.dto.PersistencePerspective)44 HashMap (java.util.HashMap)39 Property (org.broadleafcommerce.openadmin.dto.Property)38 Map (java.util.Map)28 ArrayList (java.util.ArrayList)23 ClassMetadata (org.broadleafcommerce.openadmin.dto.ClassMetadata)22 Field (java.lang.reflect.Field)19 SecurityServiceException (org.broadleafcommerce.common.exception.SecurityServiceException)19 PersistencePackageRequest (org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest)19 Serializable (java.io.Serializable)18 InvocationTargetException (java.lang.reflect.InvocationTargetException)18 DynamicResultSet (org.broadleafcommerce.openadmin.dto.DynamicResultSet)18 CriteriaTransferObject (org.broadleafcommerce.openadmin.dto.CriteriaTransferObject)17 BasicCollectionMetadata (org.broadleafcommerce.openadmin.dto.BasicCollectionMetadata)15 FieldMetadataOverride (org.broadleafcommerce.openadmin.dto.override.FieldMetadataOverride)15 AdornedTargetCollectionMetadata (org.broadleafcommerce.openadmin.dto.AdornedTargetCollectionMetadata)13