use of com.salesmanager.shop.store.api.exception.OperationNotAllowedException in project shopizer by shopizer-ecommerce.
the class ProductFacadeImpl method addProductToCategory.
@Override
public ReadableProduct addProductToCategory(Category category, Product product, Language language) throws Exception {
Validate.notNull(category, "Category cannot be null");
Validate.notNull(product, "Product cannot be null");
// not alloweed if category already attached
List<Category> assigned = product.getCategories().stream().filter(cat -> cat.getId().longValue() == category.getId().longValue()).collect(Collectors.toList());
if (assigned.size() > 0) {
throw new OperationNotAllowedException("Category with id [" + category.getId() + "] already attached to product [" + product.getId() + "]");
}
product.getCategories().add(category);
productService.update(product);
ReadableProduct readableProduct = new ReadableProduct();
ReadableProductPopulator populator = new ReadableProductPopulator();
populator.setPricingService(pricingService);
populator.setimageUtils(imageUtils);
populator.populate(product, readableProduct, product.getMerchantStore(), language);
return readableProduct;
}
use of com.salesmanager.shop.store.api.exception.OperationNotAllowedException in project shopizer by shopizer-ecommerce.
the class TaxFacadeImpl method createTaxClass.
@Override
public Entity createTaxClass(PersistableTaxClass taxClass, MerchantStore store, Language language) {
Validate.notNull(taxClass, "TaxClass cannot be null");
Validate.notNull(store, "MerchantStore cannot be null");
Validate.notNull(store.getCode(), "MerchantStore code cannot be null");
try {
if (this.existsTaxClass(taxClass.getCode(), store, language)) {
throw new OperationNotAllowedException("Tax class [" + taxClass.getCode() + "] already exist for store [" + store.getCode() + "]");
}
taxClass.setStore(store.getCode());
TaxClass model = persistableTaxClassMapper.convert(taxClass, store, language);
model = taxClassService.saveOrUpdate(model);
;
Entity id = new Entity();
id.setId(model.getId());
return id;
} catch (ServiceException e) {
LOGGER.error("Error while saving taxClass [" + taxClass.getCode() + "] for store [" + store.getCode() + "]", e);
throw new ServiceRuntimeException("Error while saving taxClass [" + taxClass.getCode() + "] for store [" + store.getCode() + "]", e);
}
}
use of com.salesmanager.shop.store.api.exception.OperationNotAllowedException in project shopizer by shopizer-ecommerce.
the class TaxFacadeImpl method createTaxRate.
@Override
public Entity createTaxRate(PersistableTaxRate taxRate, MerchantStore store, Language language) {
Validate.notNull(taxRate, "TaxRate cannot be null");
Validate.notNull(taxRate.getCode(), "TaxRate code cannot be null");
Validate.notNull(store, "MerchantStore cannot be null");
Validate.notNull(store.getCode(), "MerchantStore code cannot be null");
try {
TaxRate model = taxRateService.getByCode(taxRate.getCode(), store);
if (model != null) {
throw new OperationNotAllowedException("Tax rate [" + taxRate.getCode() + "] already exist for store [" + store.getCode() + "]");
}
model = persistableTaxRateMapper.convert(taxRate, store, language);
model = taxRateService.saveOrUpdate(model);
Entity id = new Entity();
id.setId(model.getId());
return id;
} catch (ServiceException e) {
LOGGER.error("Error while saving taxRate [" + taxRate.getCode() + "] for store [" + store.getCode() + "]", e);
throw new ServiceRuntimeException("Error while saving taxRate [" + taxRate.getCode() + "] for store [" + store.getCode() + "]", e);
}
}
use of com.salesmanager.shop.store.api.exception.OperationNotAllowedException in project shopizer by shopizer-ecommerce.
the class UserFacadeImpl method update.
@Override
public ReadableUser update(Long id, String authenticatedUser, MerchantStore store, PersistableUser user) {
Validate.notNull(user, "User cannot be null");
Validate.notNull(store, "store cannot be null");
try {
User userModel = userService.getById(id);
if (userModel == null) {
throw new ServiceRuntimeException("Cannot find user [" + user.getUserName() + "]");
}
if (userModel.getId().longValue() != id.longValue()) {
throw new ServiceRuntimeException("Cannot find user [" + user.getUserName() + "] id or name does not match");
}
User auth = userService.getByUserName(authenticatedUser);
if (auth == null) {
throw new ServiceRuntimeException("Cannot find user [" + authenticatedUser + "]");
}
User adminName = getByUserName(user.getUserName());
if (adminName != null) {
if (adminName.getId().longValue() != userModel.getId().longValue()) {
throw new ServiceRuntimeException("User id [" + userModel.getId() + "] does not match [" + user.getUserName() + "]");
}
}
boolean isActive = userModel.isActive();
List<Group> originalGroups = userModel.getGroups();
Group superadmin = originalGroups.stream().filter(group -> Constants.GROUP_SUPERADMIN.equals(group.getGroupName())).findAny().orElse(null);
// i'm i editing my own profile ?
if (authenticatedUser.equals(adminName)) {
if (!userModel.getMerchantStore().getCode().equals(store.getCode())) {
throw new OperationNotAllowedException("User [" + adminName + "] cannot change owning store");
}
} else {
// i am an admin or super admin
Group adminOrSuperadmin = originalGroups.stream().filter(group -> (Constants.GROUP_SUPERADMIN.equals(group.getGroupName()) || Constants.ADMIN_USER.equals(group.getGroupName()) || Constants.ADMIN_STORE.equals(group.getGroupName()))).findAny().orElse(null);
if (!userModel.getMerchantStore().getCode().equals(store.getCode()) && adminOrSuperadmin == null) {
throw new OperationNotAllowedException("User [" + adminName + "] cannot change owning store");
}
}
userModel = converPersistabletUserToUser(store, languageService.defaultLanguage(), userModel, user);
// admin
if (superadmin != null) {
userModel.setGroups(originalGroups);
}
Group adminGroup = auth.getGroups().stream().filter((group) -> Constants.GROUP_SUPERADMIN.equals(group.getGroupName()) || Constants.GROUP_SUPERADMIN.equals(group.getGroupName())).findAny().orElse(null);
if (adminGroup == null) {
userModel.setGroups(originalGroups);
userModel.setActive(isActive);
}
user.setPassword(userModel.getAdminPassword());
userService.update(userModel);
return this.convertUserToReadableUser(languageService.defaultLanguage(), userModel);
} catch (ServiceException e) {
throw new ServiceRuntimeException("Cannot update user [" + user.getUserName() + "]", e);
}
}
use of com.salesmanager.shop.store.api.exception.OperationNotAllowedException in project shopizer by shopizer-ecommerce.
the class ProductCommonFacadeImpl method addProductToCategory.
@Override
public ReadableProduct addProductToCategory(Category category, Product product, Language language) {
Validate.notNull(category, "Category cannot be null");
Validate.notNull(product, "Product cannot be null");
// not alloweed if category already attached
List<Category> assigned = product.getCategories().stream().filter(cat -> cat.getId().longValue() == category.getId().longValue()).collect(Collectors.toList());
if (assigned.size() > 0) {
throw new OperationNotAllowedException("Category with id [" + category.getId() + "] already attached to product [" + product.getId() + "]");
}
product.getCategories().add(category);
ReadableProduct readableProduct = new ReadableProduct();
try {
productService.update(product);
ReadableProductPopulator populator = new ReadableProductPopulator();
populator.setPricingService(pricingService);
populator.setimageUtils(imageUtils);
populator.populate(product, readableProduct, product.getMerchantStore(), language);
} catch (Exception e) {
throw new RuntimeException("Exception when adding product [" + product.getId() + "] to category [" + category.getId() + "]", e);
}
return readableProduct;
}
Aggregations