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();
}
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);
}
}
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);
}
}
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;
}
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);
}
Aggregations