use of org.broadleafcommerce.common.extension.ExtensionResultHolder in project BroadleafCommerce by BroadleafCommerce.
the class PageServiceUtility method getFieldDefinition.
protected FieldDefinition getFieldDefinition(Page page, String fieldKey) {
ExtensionResultHolder<FieldDefinition> erh = new ExtensionResultHolder<FieldDefinition>();
ExtensionResultStatusType result = extensionManager.getProxy().getFieldDefinition(erh, page, fieldKey);
if (result == ExtensionResultStatusType.HANDLED) {
return erh.getResult();
}
if (page.getPageTemplate() != null) {
for (PageTemplateFieldGroupXref fgXrefs : page.getPageTemplate().getFieldGroupXrefs()) {
for (FieldDefinition fd : fgXrefs.getFieldGroup().getFieldDefinitions()) {
if (fd.getName().equals(fieldKey)) {
return fd;
}
}
}
}
return null;
}
use of org.broadleafcommerce.common.extension.ExtensionResultHolder in project BroadleafCommerce by BroadleafCommerce.
the class StaticAssetStorageServiceImpl method getFileFromLocalRepository.
protected File getFileFromLocalRepository(String cachedFileName) {
// Look for a shared file (this represents a file that was based on a file originally in the classpath.
File cacheFile = null;
if (extensionManager != null) {
ExtensionResultHolder holder = new ExtensionResultHolder();
ExtensionResultStatusType result = extensionManager.getProxy().fileExists(cachedFileName, holder);
if (ExtensionResultStatusType.HANDLED.equals(result)) {
cacheFile = (File) holder.getResult();
}
}
if (cacheFile == null) {
cacheFile = broadleafFileService.getSharedLocalResource(cachedFileName);
}
if (cacheFile.exists()) {
return cacheFile;
} else {
return broadleafFileService.getLocalResource(cachedFileName);
}
}
use of org.broadleafcommerce.common.extension.ExtensionResultHolder in project BroadleafCommerce by BroadleafCommerce.
the class MediaFieldPersistenceProvider method extractValue.
@Override
public MetadataProviderResponse extractValue(ExtractValueRequest extractValueRequest, Property property) throws PersistenceException {
if (!canHandleExtraction(extractValueRequest, property)) {
return MetadataProviderResponse.NOT_HANDLED;
}
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 Media) {
Media media = (Media) requestedValue;
String jsonString = convertMediaToJson(media);
if (extensionManager != null) {
ExtensionResultHolder<Long> resultHolder = new ExtensionResultHolder<Long>();
ExtensionResultStatusType result = extensionManager.getProxy().transformId(media, resultHolder);
if (ExtensionResultStatusType.NOT_HANDLED != result && resultHolder.getResult() != null) {
Class<?> type;
if (media.isUnwrappableAs(Media.class)) {
type = media.unwrap(Media.class).getClass();
} else {
type = media.getClass();
}
Media converted = mediaBuilderService.convertJsonToMedia(jsonString, type);
converted.setId(resultHolder.getResult());
jsonString = convertMediaToJson(converted);
}
}
property.setValue(jsonString);
property.setUnHtmlEncodedValue(jsonString);
property.setDisplayValue(extractValueRequest.getDisplayVal());
return MetadataProviderResponse.HANDLED_BREAK;
} else {
throw new UnsupportedOperationException("MEDIA type is currently only supported on fields of type Media");
}
}
return MetadataProviderResponse.HANDLED;
}
use of org.broadleafcommerce.common.extension.ExtensionResultHolder in project BroadleafCommerce by BroadleafCommerce.
the class RuleFieldPersistenceProvider method updateQuantityRule.
protected boolean updateQuantityRule(EntityManager em, DataDTOToMVELTranslator translator, String entityKey, String fieldService, String jsonPropertyValue, Collection<QuantityBasedRule> criteriaList, Class<?> memberType, Object parent, String mappedBy, Property property) {
boolean dirty = false;
if (!StringUtils.isEmpty(jsonPropertyValue)) {
// avoid lazy init exception on the criteria list for criteria created during an add
criteriaList.size();
DataWrapper dw = ruleFieldExtractionUtility.convertJsonToDataWrapper(jsonPropertyValue);
if (dw != null && StringUtils.isEmpty(dw.getError())) {
List<QuantityBasedRule> updatedRules = new ArrayList<QuantityBasedRule>();
for (DataDTO dto : dw.getData()) {
if (dto.getPk() != null && !CollectionUtils.isEmpty(criteriaList)) {
checkId: {
// Update Existing Criteria
for (QuantityBasedRule quantityBasedRule : criteriaList) {
// make compatible with enterprise module
boolean isParentRelated = sandBoxHelper.isRelatedToParentCatalogIds(quantityBasedRule, dto.getPk());
boolean isMatch = isParentRelated || dto.getPk().equals(quantityBasedRule.getId());
if (isMatch) {
String mvel;
// don't update if the data has not changed
if (!quantityBasedRule.getQuantity().equals(dto.getQuantity())) {
dirty = true;
}
try {
mvel = ruleFieldExtractionUtility.convertDTOToMvelString(translator, entityKey, dto, fieldService);
if (!quantityBasedRule.getMatchRule().equals(mvel)) {
dirty = true;
}
} catch (MVELTranslationException e) {
throw new RuntimeException(e);
}
if (!dirty && extensionManager != null) {
ExtensionResultHolder<Boolean> resultHolder = new ExtensionResultHolder<Boolean>();
ExtensionResultStatusType result = extensionManager.getProxy().establishDirtyState(quantityBasedRule, resultHolder);
if (ExtensionResultStatusType.NOT_HANDLED != result && resultHolder.getResult() != null) {
dirty = resultHolder.getResult();
}
}
if (dirty) {
// pre-merge (can result in a clone for enterprise)
quantityBasedRule = em.merge(quantityBasedRule);
// update the quantity based rule
quantityBasedRule.setQuantity(dto.getQuantity());
quantityBasedRule.setMatchRule(mvel);
quantityBasedRule = em.merge(quantityBasedRule);
}
updatedRules.add(quantityBasedRule);
break checkId;
}
}
throw new IllegalArgumentException("Unable to update the rule of type (" + memberType.getName() + ") because an update was requested for id (" + dto.getPk() + "), which does not exist.");
}
} else {
// Create a new Criteria
QuantityBasedRule quantityBasedRule;
try {
quantityBasedRule = (QuantityBasedRule) memberType.newInstance();
quantityBasedRule.setQuantity(dto.getQuantity());
quantityBasedRule.setMatchRule(ruleFieldExtractionUtility.convertDTOToMvelString(translator, entityKey, dto, fieldService));
if (StringUtils.isEmpty(quantityBasedRule.getMatchRule()) && !StringUtils.isEmpty(dw.getRawMvel())) {
quantityBasedRule.setMatchRule(dw.getRawMvel());
}
PropertyUtils.setNestedProperty(quantityBasedRule, mappedBy, parent);
} catch (Exception e) {
throw new RuntimeException(e);
}
em.persist(quantityBasedRule);
dto.setPk(quantityBasedRule.getId());
Object contained = findContainedRuleIfApplicable(quantityBasedRule);
if (contained != null) {
dto.setContainedPk((Long) em.unwrap(Session.class).getIdentifier(contained));
}
if (extensionManager != null) {
ExtensionResultHolder resultHolder = new ExtensionResultHolder();
extensionManager.getProxy().postAdd(quantityBasedRule, resultHolder);
if (resultHolder.getResult() != null) {
quantityBasedRule = (QuantityBasedRule) resultHolder.getResult();
}
}
if (cascadeExtensionManager != null) {
ExtensionResultHolder resultHolder = new ExtensionResultHolder();
cascadeExtensionManager.getProxy().postCascadeAdd(quantityBasedRule, dto, resultHolder);
if (resultHolder.getResult() != null) {
quantityBasedRule = (QuantityBasedRule) resultHolder.getResult();
}
}
updatedRules.add(quantityBasedRule);
dirty = true;
}
}
// if an item was not included in the comprehensive submit from the client, we can assume that the
// listing was deleted, so we remove it here.
Iterator<QuantityBasedRule> itr = criteriaList.iterator();
// Since this class explicitly removes the quantity based rule - we must also preserve the id of the element
// as the CacheInvalidationProducer will need this in order to remove each collection member cache instance as well.
BroadleafRequestContext context = BroadleafRequestContext.getBroadleafRequestContext();
context.getAdditionalProperties().put("deletedQuantityBasedRules", new HashSet<QuantityBasedRule>());
while (itr.hasNext()) {
checkForRemove: {
QuantityBasedRule original = itr.next();
for (QuantityBasedRule quantityBasedRule : updatedRules) {
Long id = sandBoxHelper.getOriginalId(quantityBasedRule);
boolean isMatch = original.getId().equals(id) || original.getId().equals(quantityBasedRule.getId());
if (isMatch) {
break checkForRemove;
}
}
((Set<QuantityBasedRule>) context.getAdditionalProperties().get("deletedQuantityBasedRules")).add(original);
em.remove(original);
itr.remove();
dirty = true;
}
}
ObjectMapper mapper = new ObjectMapper();
String json;
try {
json = mapper.writeValueAsString(dw);
} catch (Exception e) {
throw new RuntimeException(e);
}
property.setValue(json);
}
}
return dirty;
}
use of org.broadleafcommerce.common.extension.ExtensionResultHolder in project BroadleafCommerce by BroadleafCommerce.
the class RuleFieldPersistenceProvider method updateSimpleRule.
protected void updateSimpleRule(PopulateValueRequest populateValueRequest, String mvel, boolean persist, SimpleRule rule) throws IllegalAccessException, FieldNotAvailableException {
if (!persist) {
// pre-merge (can result in a clone for enterprise)
rule = populateValueRequest.getPersistenceManager().getDynamicEntityDao().merge(rule);
}
rule.setMatchRule(mvel);
if (persist) {
populateValueRequest.getPersistenceManager().getDynamicEntityDao().persist(rule);
if (extensionManager != null) {
ExtensionResultHolder resultHolder = new ExtensionResultHolder();
extensionManager.getProxy().postAdd(rule, resultHolder);
}
}
}
Aggregations