Search in sources :

Example 21 with ExtensionResultHolder

use of org.broadleafcommerce.common.extension.ExtensionResultHolder in project BroadleafCommerce by BroadleafCommerce.

the class SolrHelperServiceImpl method getPropertyNameForIndexField.

@Override
public String getPropertyNameForIndexField(IndexField field, FieldType fieldType, String prefix) {
    ExtensionResultHolder<String> erh = new ExtensionResultHolder<>();
    ExtensionResultStatusType result = searchExtensionManager.getProxy().getPropertyNameForIndexField(field, fieldType, prefix, erh);
    if (!ExtensionResultStatusType.NOT_HANDLED.equals(result) && erh.getResult() != null) {
        return erh.getResult();
    }
    String fieldName = field.getField().getAbbreviation();
    if (StringUtils.isEmpty(fieldName)) {
        fieldName = field.getField().getPropertyName();
    }
    return new StringBuilder().append(prefix).append(fieldName).append("_").append(fieldType.getType()).toString();
}
Also used : ExtensionResultStatusType(org.broadleafcommerce.common.extension.ExtensionResultStatusType) ExtensionResultHolder(org.broadleafcommerce.common.extension.ExtensionResultHolder)

Example 22 with ExtensionResultHolder

use of org.broadleafcommerce.common.extension.ExtensionResultHolder in project BroadleafCommerce by BroadleafCommerce.

the class BasicPersistenceModule method add.

@Override
public EntityResult add(PersistencePackage persistencePackage, boolean includeRealEntityObject) throws ServiceException {
    EntityResult entityResult = new EntityResult();
    Entity entity = persistencePackage.getEntity();
    PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
    ForeignKey foreignKey = (ForeignKey) persistencePerspective.getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.FOREIGNKEY);
    if (foreignKey != null && !foreignKey.getMutable()) {
        throw new SecurityServiceException("Entity not mutable");
    }
    try {
        Class<?>[] entities = persistenceManager.getPolymorphicEntities(persistencePackage.getCeilingEntityFullyQualifiedClassname());
        Map<String, FieldMetadata> mergedUnfilteredProperties = persistenceManager.getDynamicEntityDao().getMergedProperties(persistencePackage.getCeilingEntityFullyQualifiedClassname(), entities, foreignKey, persistencePerspective.getAdditionalNonPersistentProperties(), persistencePerspective.getAdditionalForeignKeys(), MergedPropertyType.PRIMARY, persistencePerspective.getPopulateToOneFields(), persistencePerspective.getIncludeFields(), persistencePerspective.getExcludeFields(), persistencePerspective.getConfigurationKey(), "");
        Map<String, FieldMetadata> mergedProperties = filterOutCollectionMetadata(mergedUnfilteredProperties);
        String idProperty = null;
        for (String property : mergedProperties.keySet()) {
            if (((BasicFieldMetadata) mergedProperties.get(property)).getFieldType() == SupportedFieldType.ID) {
                idProperty = property;
                break;
            }
        }
        if (idProperty == null) {
            throw new RuntimeException("Could not find a primary key property in the passed entity with type: " + entity.getType()[0]);
        }
        Object primaryKey = null;
        try {
            primaryKey = getPrimaryKey(entity, mergedProperties);
        } catch (Exception e) {
        // don't do anything - this is a valid case
        }
        if (primaryKey == null) {
            Serializable instance = (Serializable) Class.forName(entity.getType()[0]).newInstance();
            instance = createPopulatedInstance(instance, entity, mergedProperties, false);
            if (foreignKey != null && foreignKey.getSortField() != null) {
                ExtensionResultHolder<Serializable> result = new ExtensionResultHolder<Serializable>();
                extensionManager.getProxy().rebalanceForAdd(this, persistencePackage, instance, mergedProperties, result);
                instance = result.getResult();
            }
            instance = persistenceManager.getDynamicEntityDao().merge(instance);
            if (includeRealEntityObject) {
                entityResult.setEntityBackingObject(instance);
            }
            List<Serializable> entityList = new ArrayList<Serializable>(1);
            entityList.add(instance);
            entity = getRecords(mergedProperties, entityList, null, null, null)[0];
            entityResult.setEntity(entity);
            return entityResult;
        } else {
            return update(persistencePackage, primaryKey, includeRealEntityObject);
        }
    } catch (Exception e) {
        throw new ServiceException("Problem adding new entity : " + e.getMessage(), e);
    }
}
Also used : AdminMainEntity(org.broadleafcommerce.common.admin.domain.AdminMainEntity) 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) ArrayList(java.util.ArrayList) EntityResult(org.broadleafcommerce.openadmin.dto.EntityResult) ForeignKey(org.broadleafcommerce.openadmin.dto.ForeignKey) ServiceException(org.broadleafcommerce.common.exception.ServiceException) InvocationTargetException(java.lang.reflect.InvocationTargetException) PersistenceException(org.broadleafcommerce.openadmin.server.service.persistence.PersistenceException) CriteriaConversionException(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.CriteriaConversionException) BeansException(org.springframework.beans.BeansException) ValidationException(org.broadleafcommerce.openadmin.server.service.ValidationException) ParentEntityPersistenceException(org.broadleafcommerce.openadmin.server.service.persistence.ParentEntityPersistenceException) SecurityServiceException(org.broadleafcommerce.common.exception.SecurityServiceException) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) ServiceException(org.broadleafcommerce.common.exception.ServiceException) SecurityServiceException(org.broadleafcommerce.common.exception.SecurityServiceException) CriteriaTransferObject(org.broadleafcommerce.openadmin.dto.CriteriaTransferObject) ExtensionResultHolder(org.broadleafcommerce.common.extension.ExtensionResultHolder)

Example 23 with ExtensionResultHolder

use of org.broadleafcommerce.common.extension.ExtensionResultHolder in project BroadleafCommerce by BroadleafCommerce.

the class BasicPersistenceModule method update.

protected EntityResult update(PersistencePackage persistencePackage, Object primaryKey, boolean includeRealEntity) throws ServiceException {
    EntityResult entityResult = new EntityResult();
    Entity entity = persistencePackage.getEntity();
    PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
    ForeignKey foreignKey = (ForeignKey) persistencePerspective.getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.FOREIGNKEY);
    if (foreignKey != null && !foreignKey.getMutable()) {
        throw new SecurityServiceException("Entity not mutable");
    }
    try {
        Class<?>[] entities = persistenceManager.getPolymorphicEntities(persistencePackage.getCeilingEntityFullyQualifiedClassname());
        Map<String, FieldMetadata> mergedProperties = persistenceManager.getDynamicEntityDao().getMergedProperties(persistencePackage.getCeilingEntityFullyQualifiedClassname(), entities, foreignKey, persistencePerspective.getAdditionalNonPersistentProperties(), persistencePerspective.getAdditionalForeignKeys(), MergedPropertyType.PRIMARY, persistencePerspective.getPopulateToOneFields(), persistencePerspective.getIncludeFields(), persistencePerspective.getExcludeFields(), persistencePerspective.getConfigurationKey(), "");
        if (primaryKey == null) {
            primaryKey = getPrimaryKey(entity, mergedProperties);
        }
        Serializable instance = persistenceManager.getDynamicEntityDao().retrieve(Class.forName(entity.getType()[0]), primaryKey);
        Assert.isTrue(instance != null, "Entity not found");
        if (!entity.isValidationFailure()) {
            // Re-Balance the list if it is a Foreign Key toMany collection with a sort field property
            if (foreignKey != null && foreignKey.getSortField() != null && entity.findProperty(foreignKey.getSortField()) != null && entity.findProperty(foreignKey.getSortField()).getValue() != null) {
                ExtensionResultHolder<Serializable> result = new ExtensionResultHolder<Serializable>();
                extensionManager.getProxy().rebalanceForUpdate(this, persistencePackage, instance, mergedProperties, primaryKey, result);
                instance = result.getResult();
            } else {
                instance = createPopulatedInstance(instance, entity, mergedProperties, false, persistencePackage.isValidateUnsubmittedProperties());
            }
            instance = persistenceManager.getDynamicEntityDao().merge(instance);
            if (includeRealEntity) {
                entityResult.setEntityBackingObject(instance);
            }
            List<Serializable> entityList = new ArrayList<Serializable>(1);
            entityList.add(instance);
            entity = getRecords(mergedProperties, entityList, null, null, null)[0];
            entityResult.setEntity(entity);
            return entityResult;
        } else {
            entityResult.setEntity(entity);
            return entityResult;
        }
    } catch (Exception e) {
        throw new ServiceException("Problem updating entity : " + e.getMessage(), e);
    }
}
Also used : AdminMainEntity(org.broadleafcommerce.common.admin.domain.AdminMainEntity) 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) ArrayList(java.util.ArrayList) EntityResult(org.broadleafcommerce.openadmin.dto.EntityResult) ForeignKey(org.broadleafcommerce.openadmin.dto.ForeignKey) ServiceException(org.broadleafcommerce.common.exception.ServiceException) InvocationTargetException(java.lang.reflect.InvocationTargetException) PersistenceException(org.broadleafcommerce.openadmin.server.service.persistence.PersistenceException) CriteriaConversionException(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.CriteriaConversionException) BeansException(org.springframework.beans.BeansException) ValidationException(org.broadleafcommerce.openadmin.server.service.ValidationException) ParentEntityPersistenceException(org.broadleafcommerce.openadmin.server.service.persistence.ParentEntityPersistenceException) SecurityServiceException(org.broadleafcommerce.common.exception.SecurityServiceException) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) ServiceException(org.broadleafcommerce.common.exception.ServiceException) SecurityServiceException(org.broadleafcommerce.common.exception.SecurityServiceException) ExtensionResultHolder(org.broadleafcommerce.common.extension.ExtensionResultHolder)

Example 24 with ExtensionResultHolder

use of org.broadleafcommerce.common.extension.ExtensionResultHolder in project BroadleafCommerce by BroadleafCommerce.

the class SparseTranslationOverrideStrategy method initializeOverride.

@Override
public StandardCacheItem initializeOverride(Object entity) {
    String key = getCacheKey((Translation) entity);
    String dto = ((Translation) entity).getTranslatedValue();
    StandardCacheItem cacheItem = new StandardCacheItem();
    ItemStatus status = ItemStatus.NORMAL;
    if (extensionManager != null) {
        ExtensionResultHolder<ItemStatus> response = new ExtensionResultHolder<ItemStatus>();
        ExtensionResultStatusType result = extensionManager.buildStatus(entity, response);
        if (ExtensionResultStatusType.NOT_HANDLED != result && response.getResult() != null) {
            status = response.getResult();
        }
    }
    cacheItem.setItemStatus(status);
    cacheItem.setKey(key);
    cacheItem.setCacheItem(dto);
    return cacheItem;
}
Also used : ItemStatus(org.broadleafcommerce.common.extension.ItemStatus) Translation(org.broadleafcommerce.common.i18n.domain.Translation) StandardCacheItem(org.broadleafcommerce.common.extension.StandardCacheItem) ExtensionResultStatusType(org.broadleafcommerce.common.extension.ExtensionResultStatusType) ExtensionResultHolder(org.broadleafcommerce.common.extension.ExtensionResultHolder)

Example 25 with ExtensionResultHolder

use of org.broadleafcommerce.common.extension.ExtensionResultHolder in project BroadleafCommerce by BroadleafCommerce.

the class TranslationServiceImpl method getTranslatedValue.

@Override
public String getTranslatedValue(Object entity, String property, Locale locale) {
    TranslatedEntity entityType = getEntityType(entity);
    String entityId = dao.getEntityId(entityType, entity);
    String localeCode = locale.getLanguage();
    String localeCountryCode = localeCode;
    if (StringUtils.isNotBlank(locale.getCountry())) {
        localeCountryCode += "_" + locale.getCountry();
    }
    if (TranslationBatchReadCache.hasCache()) {
        Translation translation = TranslationBatchReadCache.getFromCache(entityType, entityId, property, localeCountryCode);
        if (translation != null) {
            return translation.getTranslatedValue();
        } else {
            // There is no translation for this entity if it is not in the cache
            return null;
        }
    }
    boolean isValidForCache = false;
    if (extensionManager != null) {
        ExtensionResultHolder<Boolean> response = new ExtensionResultHolder<Boolean>();
        response.setResult(false);
        extensionManager.getProxy().isValidState(response);
        isValidForCache = response.getResult();
    }
    if (!BroadleafRequestContext.getBroadleafRequestContext().isProductionSandBox() || !isValidForCache) {
        Translation translation = dao.readTranslation(entityType, entityId, property, localeCode, localeCountryCode, ResultType.CATALOG_ONLY);
        if (translation != null) {
            return translation.getTranslatedValue();
        } else {
            return null;
        }
    }
    return getOverrideTranslatedValue(property, entityType, entityId, localeCode, localeCountryCode);
}
Also used : Translation(org.broadleafcommerce.common.i18n.domain.Translation) TranslatedEntity(org.broadleafcommerce.common.i18n.domain.TranslatedEntity) ExtensionResultHolder(org.broadleafcommerce.common.extension.ExtensionResultHolder)

Aggregations

ExtensionResultHolder (org.broadleafcommerce.common.extension.ExtensionResultHolder)56 ExtensionResultStatusType (org.broadleafcommerce.common.extension.ExtensionResultStatusType)35 ArrayList (java.util.ArrayList)11 Product (org.broadleafcommerce.core.catalog.domain.Product)7 Sku (org.broadleafcommerce.core.catalog.domain.Sku)6 PersistenceException (org.broadleafcommerce.openadmin.server.service.persistence.PersistenceException)6 ParentEntityPersistenceException (org.broadleafcommerce.openadmin.server.service.persistence.ParentEntityPersistenceException)5 FieldNotAvailableException (org.broadleafcommerce.openadmin.server.service.persistence.module.FieldNotAvailableException)5 List (java.util.List)4 Set (java.util.Set)4 File (java.io.File)3 Serializable (java.io.Serializable)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 HashMap (java.util.HashMap)3 Query (javax.persistence.Query)3 TypedQuery (javax.persistence.TypedQuery)3 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)3 AdminMainEntity (org.broadleafcommerce.common.admin.domain.AdminMainEntity)3 Site (org.broadleafcommerce.common.site.domain.Site)3 Category (org.broadleafcommerce.core.catalog.domain.Category)3