Search in sources :

Example 31 with ServiceException

use of com.salesmanager.core.business.exception.ServiceException 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);
    }
}
Also used : Entity(com.salesmanager.shop.model.entity.Entity) ServiceException(com.salesmanager.core.business.exception.ServiceException) TaxClass(com.salesmanager.core.model.tax.taxclass.TaxClass) ReadableTaxClass(com.salesmanager.shop.model.tax.ReadableTaxClass) PersistableTaxClass(com.salesmanager.shop.model.tax.PersistableTaxClass) OperationNotAllowedException(com.salesmanager.shop.store.api.exception.OperationNotAllowedException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException)

Example 32 with ServiceException

use of com.salesmanager.core.business.exception.ServiceException 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);
    }
}
Also used : Entity(com.salesmanager.shop.model.entity.Entity) ServiceException(com.salesmanager.core.business.exception.ServiceException) TaxRate(com.salesmanager.core.model.tax.taxrate.TaxRate) ReadableTaxRate(com.salesmanager.shop.model.tax.ReadableTaxRate) PersistableTaxRate(com.salesmanager.shop.model.tax.PersistableTaxRate) OperationNotAllowedException(com.salesmanager.shop.store.api.exception.OperationNotAllowedException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException)

Example 33 with ServiceException

use of com.salesmanager.core.business.exception.ServiceException 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);
    }
}
Also used : ReadableGroup(com.salesmanager.shop.model.security.ReadableGroup) PersistableGroup(com.salesmanager.shop.model.security.PersistableGroup) Group(com.salesmanager.core.model.user.Group) ReadableUser(com.salesmanager.shop.model.user.ReadableUser) User(com.salesmanager.core.model.user.User) PersistableUser(com.salesmanager.shop.model.user.PersistableUser) ServiceException(com.salesmanager.core.business.exception.ServiceException) OperationNotAllowedException(com.salesmanager.shop.store.api.exception.OperationNotAllowedException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException)

Example 34 with ServiceException

use of com.salesmanager.core.business.exception.ServiceException in project shopizer by shopizer-ecommerce.

the class UserFacadeImpl method changePassword.

@Override
public void changePassword(Long userId, String authenticatedUser, UserPassword changePassword) {
    Validate.notNull(changePassword, "Change password request must not be null");
    Validate.notNull(changePassword.getPassword(), "Original password request must not be null");
    Validate.notNull(changePassword.getChangePassword(), "New password request must not be null");
    /**
     * Only admin and superadmin can change other user password
     */
    User auth = null;
    try {
        auth = userService.getByUserName(authenticatedUser);
        if (auth == null) {
            throw new ServiceRuntimeException("Cannot find user [" + authenticatedUser + "]");
        }
        User userModel = userService.getById(userId);
        if (userModel == null) {
            throw new ServiceRuntimeException("Cannot find user [" + userId + "]");
        }
        if (!securityFacade.matchPassword(userModel.getAdminPassword(), changePassword.getPassword())) {
            throw new ServiceRuntimeException("Actual password does not match for user [" + userId + "]");
        }
        /**
         * Validate new password
         */
        if (!securityFacade.validateUserPassword(changePassword.getChangePassword())) {
            throw new ServiceRuntimeException("New password does not apply to format policy");
        }
        String newPasswordEncoded = securityFacade.encodePassword(changePassword.getChangePassword());
        userModel.setAdminPassword(newPasswordEncoded);
        userService.update(userModel);
    } catch (ServiceException e) {
        LOGGER.error("Error updating password");
        throw new ServiceRuntimeException(e);
    }
}
Also used : ReadableUser(com.salesmanager.shop.model.user.ReadableUser) User(com.salesmanager.core.model.user.User) PersistableUser(com.salesmanager.shop.model.user.PersistableUser) ServiceException(com.salesmanager.core.business.exception.ServiceException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException)

Example 35 with ServiceException

use of com.salesmanager.core.business.exception.ServiceException in project shopizer by shopizer-ecommerce.

the class UserFacadeImpl method create.

@Override
public ReadableUser create(PersistableUser user, MerchantStore store) {
    Validate.notNull(store, "MerchantStore must not be null");
    Validate.notNull(user, "User must not be null");
    Validate.notNull(user.getUserName(), "Username must not be null");
    try {
        // check if user exists
        User tempUser = userService.getByUserName(user.getUserName(), store.getCode());
        if (tempUser != null) {
            throw new ServiceRuntimeException("User [" + user.getUserName() + "] already exists for store [" + store.getCode() + "]");
        }
        /**
         * validate password
         */
        if (!securityFacade.matchRawPasswords(user.getPassword(), user.getRepeatPassword())) {
            throw new ServiceRuntimeException("Passwords dos not match, make sure password and repeat password are equals");
        }
        /**
         * Validate new password
         */
        if (!securityFacade.validateUserPassword(user.getPassword())) {
            throw new ServiceRuntimeException("New password does not apply to format policy");
        }
        String newPasswordEncoded = securityFacade.encodePassword(user.getPassword());
        User userModel = new User();
        userModel = converPersistabletUserToUser(store, languageService.defaultLanguage(), userModel, user);
        if (CollectionUtils.isEmpty(userModel.getGroups())) {
            throw new ServiceRuntimeException("No valid group groups associated with user " + user.getUserName());
        }
        userModel.setAdminPassword(newPasswordEncoded);
        userService.saveOrUpdate(userModel);
        // now build returned object
        User createdUser = userService.getById(userModel.getId());
        return convertUserToReadableUser(createdUser.getDefaultLanguage(), createdUser);
    } catch (ServiceException e) {
        throw new ServiceRuntimeException("Cannot create user " + user.getUserName() + " for store " + store.getCode(), e);
    }
}
Also used : ReadableUser(com.salesmanager.shop.model.user.ReadableUser) User(com.salesmanager.core.model.user.User) PersistableUser(com.salesmanager.shop.model.user.PersistableUser) ServiceException(com.salesmanager.core.business.exception.ServiceException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException)

Aggregations

ServiceException (com.salesmanager.core.business.exception.ServiceException)230 ServiceRuntimeException (com.salesmanager.shop.store.api.exception.ServiceRuntimeException)88 ResourceNotFoundException (com.salesmanager.shop.store.api.exception.ResourceNotFoundException)54 ArrayList (java.util.ArrayList)45 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)44 Language (com.salesmanager.core.model.reference.language.Language)38 List (java.util.List)36 Collectors (java.util.stream.Collectors)30 IOException (java.io.IOException)28 InputStream (java.io.InputStream)27 IntegrationConfiguration (com.salesmanager.core.model.system.IntegrationConfiguration)22 Autowired (org.springframework.beans.factory.annotation.Autowired)21 OutputContentFile (com.salesmanager.core.model.content.OutputContentFile)20 Product (com.salesmanager.core.model.catalog.product.Product)19 HashMap (java.util.HashMap)19 Map (java.util.Map)18 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)17 InputContentFile (com.salesmanager.core.model.content.InputContentFile)17 Optional (java.util.Optional)17 IntegrationModule (com.salesmanager.core.model.system.IntegrationModule)16