use of org.broadleafcommerce.common.extension.ExtensionResultStatusType 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.ExtensionResultStatusType 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.ExtensionResultStatusType in project BroadleafCommerce by BroadleafCommerce.
the class AdminBasicEntityController method showAddCollectionItem.
/**
* Shows the modal dialog that is used to add an item to a given collection. There are several possible outcomes
* of this call depending on the type of the specified collection field.
*
* <ul>
* <li>
* <b>Basic Collection (Persist)</b> - Renders a blank form for the specified target entity so that the user may
* enter information and associate the record with this collection. Used by fields such as ProductAttribute.
* </li>
* <li>
* <b>Basic Collection (Lookup)</b> - Renders a list grid that allows the user to click on an entity and select it.
* Used by fields such as "allParentCategories".
* </li>
* <li>
* <b>Adorned Collection (without form)</b> - Renders a list grid that allows the user to click on an entity and
* select it. The view rendered by this is identical to basic collection (lookup), but will perform the operation
* on an adorned field, which may carry extra meta-information about the created relationship, such as order.
* </li>
* <li>
* <b>Adorned Collection (with form)</b> - Renders a list grid that allows the user to click on an entity and
* select it. Once the user selects the entity, he will be presented with an empty form based on the specified
* "maintainedAdornedTargetFields" for this field. Used by fields such as "crossSellProducts", which in addition
* to linking an entity, provide extra fields, such as a promotional message.
* </li>
* <li>
* <b>Map Collection</b> - Renders a form for the target entity that has an additional key field. This field is
* populated either from the configured map keys, or as a result of a lookup in the case of a key based on another
* entity. Used by fields such as the mediaMap on a Sku.
* </li>
*
* @param request
* @param response
* @param model
* @param pathVars
* @param id
* @param collectionField
* @param requestParams
* @return the return view path
* @throws Exception
*/
@RequestMapping(value = "/{id}/{collectionField:.*}/add", method = RequestMethod.GET)
public String showAddCollectionItem(HttpServletRequest request, HttpServletResponse response, Model model, @PathVariable Map<String, String> pathVars, @PathVariable(value = "id") String id, @PathVariable(value = "collectionField") String collectionField, @RequestParam MultiValueMap<String, String> requestParams) throws Exception {
String sectionKey = getSectionKey(pathVars);
String mainClassName = getClassNameForSection(sectionKey);
List<SectionCrumb> sectionCrumbs = getSectionCrumbs(request, sectionKey, id);
ClassMetadata mainMetadata = service.getClassMetadata(getSectionPersistencePackageRequest(mainClassName, sectionCrumbs, pathVars)).getDynamicResultSet().getClassMetaData();
Property collectionProperty = mainMetadata.getPMap().get(collectionField);
FieldMetadata md = collectionProperty.getMetadata();
PersistencePackageRequest ppr = PersistencePackageRequest.fromMetadata(md, sectionCrumbs).withFilterAndSortCriteria(getCriteria(requestParams)).withStartIndex(getStartIndex(requestParams)).withMaxIndex(getMaxIndex(requestParams)).withLastId(getLastId(requestParams)).withFirstId(getFirstId(requestParams)).withUpperCount(getUpperCount(requestParams)).withLowerCount(getLowerCount(requestParams)).withPageSize(getPageSize(requestParams)).withPresentationFetch(true);
if (md instanceof BasicCollectionMetadata) {
BasicCollectionMetadata fmd = (BasicCollectionMetadata) md;
if (fmd.getAddMethodType().equals(AddMethodType.PERSIST)) {
ClassMetadata cmd = service.getClassMetadata(ppr).getDynamicResultSet().getClassMetaData();
// If the entity type isn't specified, we need to determine if there are various polymorphic types
// for this entity.
String entityType = null;
if (requestParams.containsKey("entityType")) {
entityType = requestParams.get("entityType").get(0);
}
if (StringUtils.isBlank(entityType)) {
if (cmd.getPolymorphicEntities().getChildren().length == 0) {
entityType = cmd.getPolymorphicEntities().getFullyQualifiedClassname();
} else {
entityType = getDefaultEntityType();
}
} else {
entityType = URLDecoder.decode(entityType, "UTF-8");
}
if (StringUtils.isBlank(entityType)) {
List<ClassTree> entityTypes = getAddEntityTypes(cmd.getPolymorphicEntities());
model.addAttribute("entityTypes", entityTypes);
model.addAttribute("viewType", "modal/entityTypeSelection");
model.addAttribute("entityFriendlyName", cmd.getPolymorphicEntities().getFriendlyName());
String requestUri = request.getRequestURI();
if (!request.getContextPath().equals("/") && requestUri.startsWith(request.getContextPath())) {
requestUri = requestUri.substring(request.getContextPath().length() + 1, requestUri.length());
}
model.addAttribute("currentUri", requestUri);
model.addAttribute("modalHeaderType", ModalHeaderType.ADD_ENTITY.getType());
setModelAttributes(model, sectionKey);
return "modules/modalContainer";
} else {
ppr = ppr.withCeilingEntityClassname(entityType);
}
}
} else if (md instanceof MapMetadata) {
ExtensionResultStatusType result = extensionManager.getProxy().modifyModelForAddCollectionType(request, response, model, sectionKey, id, requestParams, (MapMetadata) md);
if (result.equals(ExtensionResultStatusType.HANDLED)) {
model.addAttribute("entityId", id);
model.addAttribute("sectionKey", sectionKey);
model.addAttribute("collectionField", collectionField);
return "modules/modalContainer";
}
}
// service.getContextSpecificRelationshipId(mainMetadata, entity, prefix);
model.addAttribute("currentParams", new ObjectMapper().writeValueAsString(requestParams));
return buildAddCollectionItemModel(request, response, model, id, collectionField, sectionKey, collectionProperty, md, ppr, null, null);
}
use of org.broadleafcommerce.common.extension.ExtensionResultStatusType 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.ExtensionResultStatusType 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;
}
Aggregations