use of org.broadleafcommerce.common.exception.ServiceException in project BroadleafCommerce by BroadleafCommerce.
the class PersistenceManagerImpl method fetch.
@Override
public PersistenceResponse fetch(PersistencePackage persistencePackage, CriteriaTransferObject cto) throws ServiceException {
for (PersistenceManagerEventHandler handler : persistenceManagerEventHandlers) {
PersistenceManagerEventHandlerResponse response = handler.preFetch(this, persistencePackage, cto);
if (PersistenceManagerEventHandlerResponse.PersistenceManagerEventHandlerResponseStatus.HANDLED_BREAK == response.getStatus()) {
break;
}
}
// check to see if there is a custom handler registered
for (CustomPersistenceHandler handler : getCustomPersistenceHandlers()) {
if (handler.canHandleFetch(persistencePackage)) {
if (!handler.willHandleSecurity(persistencePackage)) {
adminRemoteSecurityService.securityCheck(persistencePackage, EntityOperationType.FETCH);
}
DynamicResultSet results = handler.fetch(persistencePackage, cto, dynamicEntityDao, (RecordHelper) getCompatibleModule(OperationType.BASIC));
return executePostFetchHandlers(persistencePackage, cto, new PersistenceResponse().withDynamicResultSet(results));
}
}
adminRemoteSecurityService.securityCheck(persistencePackage, EntityOperationType.FETCH);
PersistenceModule myModule = getCompatibleModule(persistencePackage.getPersistencePerspective().getOperationTypes().getFetchType());
try {
DynamicResultSet results = myModule.fetch(persistencePackage, cto);
return executePostFetchHandlers(persistencePackage, cto, new PersistenceResponse().withDynamicResultSet(results));
} catch (ServiceException e) {
if (e.getCause() instanceof NoPossibleResultsException) {
DynamicResultSet drs = new DynamicResultSet(null, new Entity[] {}, 0);
return executePostFetchHandlers(persistencePackage, cto, new PersistenceResponse().withDynamicResultSet(drs));
}
throw e;
}
}
use of org.broadleafcommerce.common.exception.ServiceException 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();
}
use of org.broadleafcommerce.common.exception.ServiceException 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);
}
}
use of org.broadleafcommerce.common.exception.ServiceException in project BroadleafCommerce by BroadleafCommerce.
the class FormProcessor method getInjectedModelAndTagAttributes.
@Override
public BroadleafTemplateModelModifierDTO getInjectedModelAndTagAttributes(String rootTagName, Map<String, String> rootTagAttributes, BroadleafTemplateContext context) {
Map<String, String> formAttributes = new HashMap<>();
formAttributes.putAll(rootTagAttributes);
BroadleafTemplateModel model = context.createModel();
BroadleafTemplateModelModifierDTO dto = new BroadleafTemplateModelModifierDTO();
// We do this instead of checking for a POST because post is default if nothing is specified
if (!"GET".equalsIgnoreCase(formAttributes.get("method"))) {
try {
String csrfToken = eps.getCSRFToken();
String stateVersionToken = null;
if (spps.isEnabled()) {
stateVersionToken = spps.getStateVersionToken();
}
// detect multipart form
if ("multipart/form-data".equalsIgnoreCase(formAttributes.get("enctype"))) {
String csrfQueryParameter = "?" + eps.getCsrfTokenParameter() + "=" + csrfToken;
if (stateVersionToken != null) {
csrfQueryParameter += "&" + spps.getStateVersionTokenParameter() + "=" + stateVersionToken;
}
// Add this into the attribute map to be used for the new <form> tag. The expression has already
// been executed, don't need to treat the value as an expression
String actionValue = formAttributes.get("action");
actionValue += csrfQueryParameter;
formAttributes.put("action", actionValue);
} else {
Map<String, String> csrfAttributes = new HashMap<>();
csrfAttributes.put("type", "hidden");
csrfAttributes.put("name", eps.getCsrfTokenParameter());
csrfAttributes.put("value", csrfToken);
BroadleafTemplateElement csrfTag = context.createStandaloneElement("input", csrfAttributes, true);
model.addElement(csrfTag);
if (stateVersionToken != null) {
Map<String, String> stateVersionAttributes = new HashMap<>();
stateVersionAttributes.put("type", "hidden");
stateVersionAttributes.put("name", spps.getStateVersionTokenParameter());
stateVersionAttributes.put("value", stateVersionToken);
BroadleafTemplateElement stateVersionTag = context.createStandaloneElement("input", stateVersionAttributes, true);
model.addElement(stateVersionTag);
}
dto.setModel(model);
}
} catch (ServiceException e) {
throw new RuntimeException("Could not get a CSRF token for this session", e);
}
}
dto.setFormParameters(formAttributes);
dto.setReplacementTagName("form");
return dto;
}
use of org.broadleafcommerce.common.exception.ServiceException in project BroadleafCommerce by BroadleafCommerce.
the class ProductCustomPersistenceHandler method update.
@Override
public Entity update(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
Entity entity = persistencePackage.getEntity();
try {
PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
Map<String, FieldMetadata> adminProperties = helper.getSimpleMergedProperties(Product.class.getName(), persistencePerspective);
BasicFieldMetadata defaultCategory = ((BasicFieldMetadata) adminProperties.get("defaultCategory"));
defaultCategory.setFriendlyName("ProductImpl_Parent_Category");
if (entity.findProperty("defaultCategory") != null && !StringUtils.isEmpty(entity.findProperty("defaultCategory").getValue())) {
// Change the inherited type so that this property is disconnected from the entity and validation is temporarily skipped.
// This is useful when the defaultCategory was previously completely empty for whatever reason. Without this, such
// a case would fail the validation, even though the property was specified in the submission.
defaultCategory.setInheritedFromType(String.class.getName());
}
Object primaryKey = helper.getPrimaryKey(entity, adminProperties);
Product adminInstance = (Product) dynamicEntityDao.retrieve(Class.forName(entity.getType()[0]), primaryKey);
if (adminInstance instanceof ProductBundle) {
removeBundleFieldRestrictions((ProductBundle) adminInstance, adminProperties, entity);
}
CategoryProductXref oldDefault = getCurrentDefaultXref(adminInstance);
// so override required flag for that field during deployment
if (BroadleafRequestContext.getBroadleafRequestContext().isProductionSandBox()) {
((BasicFieldMetadata) adminProperties.get("defaultCategory")).setRequiredOverride(false);
}
adminInstance = (Product) helper.createPopulatedInstance(adminInstance, entity, adminProperties, false);
adminInstance = dynamicEntityDao.merge(adminInstance);
boolean handled = false;
if (extensionManager != null) {
ExtensionResultStatusType result = extensionManager.getProxy().manageParentCategoryForUpdate(persistencePackage, adminInstance);
handled = ExtensionResultStatusType.NOT_HANDLED != result;
extensionManager.getProxy().manageFields(persistencePackage, adminInstance);
}
if (!handled) {
setupXref(adminInstance);
removeOldDefault(adminInstance, oldDefault, entity);
}
return helper.getRecord(adminProperties, adminInstance, null, null);
} catch (Exception e) {
throw new ServiceException("Unable to update entity for " + entity.getType()[0], e);
}
}
Aggregations