use of org.broadleafcommerce.common.persistence.Status in project BroadleafCommerce by BroadleafCommerce.
the class AdminUserCustomPersistenceHandler method remove.
@Override
public void remove(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
Entity entity = persistencePackage.getEntity();
String idValue = entity.findProperty("id").getValue();
String userLoginToRemove = entity.findProperty("login") == null ? null : entity.findProperty("login").getValue();
AdminUser persistentAdminUser = adminRemoteSecurityService.getPersistentAdminUser();
if (persistentAdminUser != null && persistentAdminUser.getLogin() != null && userLoginToRemove != null) {
if (persistentAdminUser.getLogin().equals(userLoginToRemove)) {
throw new ValidationException(entity, "admin.cantDeleteCurrentUserError");
}
}
if (idValue != null) {
Long id = Long.parseLong(idValue);
AdminUser adminInstance = adminSecurityService.readAdminUserById(id);
// Check if Status was Weaved in
if (Status.class.isAssignableFrom(adminInstance.getClass())) {
((Status) adminInstance).setArchived('Y');
adminSecurityService.saveAdminUser(adminInstance);
return;
}
}
OperationType removeType = persistencePackage.getPersistencePerspective().getOperationTypes().getRemoveType();
helper.getCompatibleModule(removeType).remove(persistencePackage);
}
use of org.broadleafcommerce.common.persistence.Status in project BroadleafCommerce by BroadleafCommerce.
the class MediaFieldPersistenceProvider method populateValue.
@Override
public MetadataProviderResponse populateValue(PopulateValueRequest populateValueRequest, Serializable instance) throws PersistenceException {
if (!canHandlePersistence(populateValueRequest, instance)) {
return MetadataProviderResponse.NOT_HANDLED;
}
String prop = populateValueRequest.getProperty().getName();
if (prop.contains(FieldManager.MAPFIELDSEPARATOR)) {
Field field = populateValueRequest.getFieldManager().getField(instance.getClass(), prop.substring(0, prop.indexOf(FieldManager.MAPFIELDSEPARATOR)));
if (field.getAnnotation(OneToMany.class) == null) {
throw new UnsupportedOperationException("MediaFieldPersistenceProvider is currently only compatible with map fields when modelled using @OneToMany");
}
}
MetadataProviderResponse response = MetadataProviderResponse.HANDLED;
boolean dirty = false;
try {
setNonDisplayableValues(populateValueRequest);
Class<?> valueType = getStartingValueType(populateValueRequest);
if (Media.class.isAssignableFrom(valueType)) {
Media newMedia = mediaBuilderService.convertJsonToMedia(populateValueRequest.getProperty().getUnHtmlEncodedValue(), valueType);
boolean persist = false;
boolean noPrimary = false;
boolean update = false;
Media media;
Boolean cleared;
try {
checkExtension: {
if (extensionManager != null) {
ExtensionResultHolder<Tuple<Media, Boolean>> result = new ExtensionResultHolder<Tuple<Media, Boolean>>();
ExtensionResultStatusType statusType = extensionManager.getProxy().retrieveMedia(instance, populateValueRequest, result);
if (ExtensionResultStatusType.NOT_HANDLED != statusType) {
Tuple<Media, Boolean> tuple = result.getResult();
media = tuple.getFirst();
cleared = tuple.getSecond();
break checkExtension;
}
}
media = (Media) populateValueRequest.getFieldManager().getFieldValue(instance, populateValueRequest.getProperty().getName());
cleared = false;
}
if (newMedia == null) {
noPrimary = true;
dirty = true;
update = false;
if (!cleared && media != null) {
// remove entry in sku to media map
populateValueRequest.getFieldManager().setFieldValue(instance, populateValueRequest.getProperty().getName(), null);
populateValueRequest.getPersistenceManager().getDynamicEntityDao().remove(media);
}
} else if (media == null) {
media = (Media) valueType.newInstance();
mediaBuilderService.instantiateMediaFields(media);
Object parent = extractParent(populateValueRequest, instance);
populateValueRequest.getFieldManager().setFieldValue(media, populateValueRequest.getMetadata().getToOneParentProperty(), parent);
populateValueRequest.getFieldManager().setFieldValue(media, populateValueRequest.getMetadata().getMapKeyValueProperty(), prop.substring(prop.indexOf(FieldManager.MAPFIELDSEPARATOR) + FieldManager.MAPFIELDSEPARATOR.length(), prop.length()));
persist = true;
}
} catch (FieldNotAvailableException e) {
throw new IllegalArgumentException(e);
}
if (media != null) {
if (media instanceof Status && 'Y' == ((Status) media).getArchived()) {
persist = true;
}
populateValueRequest.getProperty().setOriginalValue(convertMediaToJson(media));
}
if (!noPrimary) {
dirty = establishDirtyState(newMedia, media);
update = dirty;
}
if (dirty) {
if (update) {
updateMedia(populateValueRequest, newMedia, persist, media);
}
response = MetadataProviderResponse.HANDLED_BREAK;
}
} else {
throw new UnsupportedOperationException("MediaFields only work with Media types.");
}
} catch (Exception e) {
throw ExceptionHelper.refineException(PersistenceException.class, PersistenceException.class, e);
}
populateValueRequest.getProperty().setIsDirty(dirty);
return response;
}
use of org.broadleafcommerce.common.persistence.Status in project BroadleafCommerce by BroadleafCommerce.
the class CustomerCustomPersistenceHandler method remove.
@Override
public void remove(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
Entity entity = persistencePackage.getEntity();
try {
Long customerId = Long.parseLong(entity.findProperty("id").getValue());
Customer customer = customerService.readCustomerById(customerId);
if (Status.class.isAssignableFrom(customer.getClass())) {
((Status) customer).setArchived('Y');
// If the customer has a conditional weave on ArchiveStatus, nothing triggers the delete so other
// normally-cascaded deletes don't happen (like CustomerAddress)
List<CustomerAddress> addressList = customer.getCustomerAddresses();
for (CustomerAddress address : addressList) {
address.setArchived('Y');
}
customer = customerService.saveCustomer(customer);
return;
}
// Remove the customer roles for the customer since it's not cascaded
roleDao.removeCustomerRolesByCustomerId(customerId);
helper.getCompatibleModule(OperationType.BASIC).remove(persistencePackage);
} catch (Exception e) {
LOG.error("Unable to execute persistence activity", e);
throw new ServiceException("Unable to remove entity for " + entity.getType()[0], e);
}
}
use of org.broadleafcommerce.common.persistence.Status in project BroadleafCommerce by BroadleafCommerce.
the class SearchFacetCustomPersistenceHandler method remove.
@Override
public void remove(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
Entity entity = persistencePackage.getEntity();
try {
SearchFacet adminInstance = getAdminInstance(persistencePackage, dynamicEntityDao, helper, entity);
if (Status.class.isAssignableFrom(adminInstance.getClass())) {
((Status) adminInstance).setArchived('Y');
dynamicEntityDao.merge(adminInstance);
}
} catch (Exception e) {
throw new ServiceException("Unable to remove entity for " + entity.getType()[0], e);
}
}
use of org.broadleafcommerce.common.persistence.Status in project BroadleafCommerce by BroadleafCommerce.
the class SearchFacetRangeCustomPersistenceHandler method remove.
@Override
public void remove(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
Entity entity = persistencePackage.getEntity();
try {
SearchFacetRange adminInstance = getAdminInstance(persistencePackage, dynamicEntityDao, helper, entity);
if (Status.class.isAssignableFrom(adminInstance.getClass())) {
((Status) adminInstance).setArchived('Y');
dynamicEntityDao.merge(adminInstance);
}
} catch (Exception e) {
throw new ServiceException("Unable to remove entity for " + entity.getType()[0], e);
}
}
Aggregations