use of org.broadleafcommerce.openadmin.dto.PersistencePackage in project BroadleafCommerce by BroadleafCommerce.
the class PersistencePackageFactoryImpl method create.
@Override
public PersistencePackage create(PersistencePackageRequest request) {
PersistencePerspective persistencePerspective = new PersistencePerspective();
persistencePerspective.setAdditionalForeignKeys(request.getAdditionalForeignKeys());
persistencePerspective.setAdditionalNonPersistentProperties(new String[] {});
if (request.getForeignKey() != null) {
persistencePerspective.addPersistencePerspectiveItem(PersistencePerspectiveItemType.FOREIGNKEY, request.getForeignKey());
}
switch(request.getType()) {
case STANDARD:
persistencePerspective.setOperationTypes(getDefaultOperationTypes());
break;
case ADORNED:
if (request.getAdornedList() == null) {
throw new IllegalArgumentException("ADORNED type requires the adornedList to be set");
}
persistencePerspective.setOperationTypes(getOperationTypes(OperationType.ADORNEDTARGETLIST));
persistencePerspective.addPersistencePerspectiveItem(PersistencePerspectiveItemType.ADORNEDTARGETLIST, request.getAdornedList());
break;
case MAP:
if (request.getMapStructure() == null) {
throw new IllegalArgumentException("MAP type requires the mapStructure to be set");
}
persistencePerspective.setOperationTypes(getOperationTypes(OperationType.MAP));
persistencePerspective.addPersistencePerspectiveItem(PersistencePerspectiveItemType.MAPSTRUCTURE, request.getMapStructure());
break;
}
if (request.getOperationTypesOverride() != null) {
persistencePerspective.setOperationTypes(request.getOperationTypesOverride());
}
PersistencePackage pp = new PersistencePackage();
pp.setCeilingEntityFullyQualifiedClassname(request.getCeilingEntityClassname());
if (!StringUtils.isEmpty(request.getSecurityCeilingEntityClassname())) {
pp.setSecurityCeilingEntityFullyQualifiedClassname(request.getSecurityCeilingEntityClassname());
}
if (!ArrayUtils.isEmpty(request.getSectionCrumbs())) {
SectionCrumb[] converted = new SectionCrumb[request.getSectionCrumbs().length];
int index = 0;
for (SectionCrumb crumb : request.getSectionCrumbs()) {
SectionCrumb temp = new SectionCrumb();
String originalSectionIdentifier = crumb.getSectionIdentifier();
String sectionAsClassName;
try {
sectionAsClassName = getClassNameForSection(crumb.getSectionIdentifier());
} catch (Exception e) {
sectionAsClassName = request.getCeilingEntityClassname();
}
if (sectionAsClassName != null && !sectionAsClassName.equals(originalSectionIdentifier)) {
temp.setOriginalSectionIdentifier(originalSectionIdentifier);
}
temp.setSectionIdentifier(sectionAsClassName);
temp.setSectionId(crumb.getSectionId());
converted[index] = temp;
index++;
}
pp.setSectionCrumbs(converted);
} else {
pp.setSectionCrumbs(new SectionCrumb[0]);
}
pp.setSectionEntityField(request.getSectionEntityField());
pp.setFetchTypeFullyQualifiedClassname(null);
pp.setPersistencePerspective(persistencePerspective);
pp.setCustomCriteria(request.getCustomCriteria());
pp.setCsrfToken(null);
pp.setRequestingEntityName(request.getRequestingEntityName());
pp.setValidateUnsubmittedProperties(request.isValidateUnsubmittedProperties());
pp.setIsTreeCollection(request.isTreeCollection());
pp.setAddOperationInspect(request.isAddOperationInspect());
if (request.getEntity() != null) {
pp.setEntity(request.getEntity());
}
for (Map.Entry<String, PersistencePackageRequest> subRequest : request.getSubRequests().entrySet()) {
pp.getSubPackages().put(subRequest.getKey(), create(subRequest.getValue()));
}
return pp;
}
use of org.broadleafcommerce.openadmin.dto.PersistencePackage in project BroadleafCommerce by BroadleafCommerce.
the class PersistenceManagerImpl method update.
@Override
public PersistenceResponse update(PersistencePackage persistencePackage) throws ServiceException {
for (PersistenceManagerEventHandler handler : persistenceManagerEventHandlers) {
PersistenceManagerEventHandlerResponse response = handler.preUpdate(this, persistencePackage);
if (PersistenceManagerEventHandlerResponse.PersistenceManagerEventHandlerResponseStatus.HANDLED_BREAK == response.getStatus()) {
break;
}
}
// check to see if there is a custom handler registered
// execute the root PersistencePackage
Entity response;
try {
checkRoot: {
for (CustomPersistenceHandler handler : getCustomPersistenceHandlers()) {
if (handler.canHandleUpdate(persistencePackage)) {
if (!handler.willHandleSecurity(persistencePackage)) {
adminRemoteSecurityService.securityCheck(persistencePackage, EntityOperationType.UPDATE);
}
response = handler.update(persistencePackage, dynamicEntityDao, (RecordHelper) getCompatibleModule(OperationType.BASIC));
break checkRoot;
}
}
adminRemoteSecurityService.securityCheck(persistencePackage, EntityOperationType.UPDATE);
PersistenceModule myModule = getCompatibleModule(persistencePackage.getPersistencePerspective().getOperationTypes().getUpdateType());
response = myModule.update(persistencePackage);
}
} catch (ValidationException e) {
response = e.getEntity();
} catch (ServiceException e) {
if (e.getCause() instanceof ValidationException) {
response = ((ValidationException) e.getCause()).getEntity();
} else {
throw e;
}
}
Map<String, List<String>> subPackageValidationErrors = new HashMap<>();
for (Map.Entry<String, PersistencePackage> subPackage : persistencePackage.getSubPackages().entrySet()) {
try {
// Run through any subPackages -- add up any validation errors
checkHandler: {
for (CustomPersistenceHandler handler : getCustomPersistenceHandlers()) {
if (handler.canHandleUpdate(subPackage.getValue())) {
Entity subResponse = handler.update(subPackage.getValue(), dynamicEntityDao, (RecordHelper) getCompatibleModule(OperationType.BASIC));
subPackage.getValue().setEntity(subResponse);
break checkHandler;
}
}
PersistenceModule subModule = getCompatibleModule(subPackage.getValue().getPersistencePerspective().getOperationTypes().getUpdateType());
Entity subResponse = subModule.update(persistencePackage);
subPackage.getValue().setEntity(subResponse);
}
} catch (ValidationException e) {
subPackage.getValue().setEntity(e.getEntity());
} catch (ServiceException e) {
if (e.getCause() instanceof ValidationException) {
response = ((ValidationException) e.getCause()).getEntity();
} else {
throw e;
}
}
}
// Build up validation errors in all of the subpackages, even those that might not have thrown ValidationExceptions
for (Map.Entry<String, PersistencePackage> subPackage : persistencePackage.getSubPackages().entrySet()) {
for (Map.Entry<String, List<String>> error : subPackage.getValue().getEntity().getPropertyValidationErrors().entrySet()) {
subPackageValidationErrors.put(subPackage.getKey() + DynamicEntityFormInfo.FIELD_SEPARATOR + error.getKey(), error.getValue());
}
}
response.getPropertyValidationErrors().putAll(subPackageValidationErrors);
if (response.isValidationFailure()) {
PersistenceResponse validationResponse = executeValidationProcessors(persistencePackage, new PersistenceResponse().withEntity(response));
Entity entity = validationResponse.getEntity();
String message = ValidationUtil.buildErrorMessage(entity.getPropertyValidationErrors(), entity.getGlobalValidationErrors());
throw new ValidationException(entity, message);
}
return executePostUpdateHandlers(persistencePackage, new PersistenceResponse().withEntity(response));
}
use of org.broadleafcommerce.openadmin.dto.PersistencePackage in project BroadleafCommerce by BroadleafCommerce.
the class PersistenceManagerImpl method add.
@Override
public PersistenceResponse add(PersistencePackage persistencePackage) throws ServiceException {
for (PersistenceManagerEventHandler handler : persistenceManagerEventHandlers) {
PersistenceManagerEventHandlerResponse response = handler.preAdd(this, persistencePackage);
if (PersistenceManagerEventHandlerResponse.PersistenceManagerEventHandlerResponseStatus.HANDLED_BREAK == response.getStatus()) {
break;
}
}
// check to see if there is a custom handler registered
// execute the root PersistencePackage
Entity response;
try {
checkRoot: {
// entity to execute the subPackage code later
for (CustomPersistenceHandler handler : getCustomPersistenceHandlers()) {
if (handler.canHandleAdd(persistencePackage)) {
if (!handler.willHandleSecurity(persistencePackage)) {
adminRemoteSecurityService.securityCheck(persistencePackage, EntityOperationType.ADD);
}
response = handler.add(persistencePackage, dynamicEntityDao, (RecordHelper) getCompatibleModule(OperationType.BASIC));
break checkRoot;
}
}
adminRemoteSecurityService.securityCheck(persistencePackage, EntityOperationType.ADD);
PersistenceModule myModule = getCompatibleModule(persistencePackage.getPersistencePerspective().getOperationTypes().getAddType());
response = myModule.add(persistencePackage);
}
} catch (ValidationException e) {
response = e.getEntity();
} catch (ServiceException e) {
if (e.getCause() instanceof ValidationException) {
response = ((ValidationException) e.getCause()).getEntity();
} else {
throw e;
}
}
if (!MapUtils.isEmpty(persistencePackage.getSubPackages())) {
// Once the entity has been saved, we can utilize its id for the subsequent dynamic forms
Class<?> entityClass;
try {
entityClass = Class.forName(response.getType()[0]);
} catch (ClassNotFoundException e) {
throw new ServiceException(e);
}
Map<String, Object> idMetadata = getDynamicEntityDao().getIdMetadata(entityClass);
String idProperty = (String) idMetadata.get("name");
String idVal = response.findProperty(idProperty).getValue();
Map<String, List<String>> subPackageValidationErrors = new HashMap<>();
for (Map.Entry<String, PersistencePackage> subPackage : persistencePackage.getSubPackages().entrySet()) {
Entity subResponse;
try {
subPackage.getValue().getCustomCriteria()[1] = idVal;
// Run through any subPackages -- add up any validation errors
checkHandler: {
for (CustomPersistenceHandler handler : getCustomPersistenceHandlers()) {
if (handler.canHandleAdd(subPackage.getValue())) {
subResponse = handler.add(subPackage.getValue(), dynamicEntityDao, (RecordHelper) getCompatibleModule(OperationType.BASIC));
subPackage.getValue().setEntity(subResponse);
break checkHandler;
}
}
PersistenceModule subModule = getCompatibleModule(subPackage.getValue().getPersistencePerspective().getOperationTypes().getAddType());
subResponse = subModule.add(persistencePackage);
subPackage.getValue().setEntity(subResponse);
}
} catch (ValidationException e) {
subPackage.getValue().setEntity(e.getEntity());
} catch (ServiceException e) {
if (e.getCause() instanceof ValidationException) {
response = ((ValidationException) e.getCause()).getEntity();
} else {
throw e;
}
}
}
// Build up validation errors in all of the subpackages, even those that might not have thrown ValidationExceptions
for (Map.Entry<String, PersistencePackage> subPackage : persistencePackage.getSubPackages().entrySet()) {
for (Map.Entry<String, List<String>> error : subPackage.getValue().getEntity().getPropertyValidationErrors().entrySet()) {
subPackageValidationErrors.put(subPackage.getKey() + DynamicEntityFormInfo.FIELD_SEPARATOR + error.getKey(), error.getValue());
}
}
response.getPropertyValidationErrors().putAll(subPackageValidationErrors);
}
if (response.isValidationFailure()) {
PersistenceResponse validationResponse = executeValidationProcessors(persistencePackage, new PersistenceResponse().withEntity(response));
Entity entity = validationResponse.getEntity();
String message = ValidationUtil.buildErrorMessage(entity.getPropertyValidationErrors(), entity.getGlobalValidationErrors());
throw new ValidationException(entity, message);
}
return executePostAddHandlers(persistencePackage, new PersistenceResponse().withEntity(response));
}
use of org.broadleafcommerce.openadmin.dto.PersistencePackage in project BroadleafCommerce by BroadleafCommerce.
the class AdminEntityServiceImpl method fetch.
@Override
public PersistenceResponse fetch(PersistencePackageRequest request) throws ServiceException {
PersistencePackage pkg = persistencePackageFactory.create(request);
CriteriaTransferObject cto = getDefaultCto();
if (request.getFilterAndSortCriteria() != null) {
cto.addAll(Arrays.asList(request.getFilterAndSortCriteria()));
}
if (request.getMaxResults() != null) {
cto.setMaxResults(request.getMaxResults());
}
if (request.getStartIndex() == null) {
cto.setFirstResult(0);
} else {
cto.setFirstResult(request.getStartIndex());
}
if (request.getMaxIndex() != null) {
Integer startIndex = request.getStartIndex() != null ? request.getStartIndex() : 0;
int requestedMaxResults = request.getMaxIndex() - startIndex + 1;
if (requestedMaxResults >= 0 && requestedMaxResults < cto.getMaxResults()) {
cto.setMaxResults(requestedMaxResults);
}
}
cto.setLastId(request.getLastId());
cto.setFirstId(request.getFirstId());
cto.setUpperCount(request.getUpperCount());
cto.setLowerCount(request.getLowerCount());
if (request.getPageSize() != null) {
cto.setMaxResults(request.getPageSize());
}
cto.setPresentationFetch(request.getPresentationFetch());
return service.fetch(pkg, cto);
}
Aggregations