Search in sources :

Example 6 with CustomerPhone

use of org.broadleafcommerce.profile.core.domain.CustomerPhone in project BroadleafCommerce by BroadleafCommerce.

the class CustomerPhoneValidator method validate.

public void validate(Object obj, Errors errors) {
    // use regular phone
    CustomerPhone cPhone = (CustomerPhone) obj;
    if (!errors.hasErrors()) {
        // check for duplicate phone number
        List<CustomerPhone> phones = customerPhoneService.readAllCustomerPhonesByCustomerId(cPhone.getCustomer().getId());
        String phoneNum = cPhone.getPhone().getPhoneNumber();
        String phoneName = cPhone.getPhoneName();
        Long phoneId = cPhone.getPhone().getId();
        Long customerPhoneId = cPhone.getId();
        boolean foundPhoneIdForUpdate = false;
        boolean foundCustomerPhoneIdForUpdate = false;
        for (CustomerPhone existingPhone : phones) {
            // validate that the phoneId passed for an editPhone scenario exists for this user
            if (phoneId != null && !foundPhoneIdForUpdate) {
                if (existingPhone.getPhone().getId().equals(phoneId)) {
                    foundPhoneIdForUpdate = true;
                }
            }
            // validate that the customerPhoneId passed for an editPhone scenario exists for this user
            if (customerPhoneId != null && !foundCustomerPhoneIdForUpdate) {
                if (existingPhone.getId().equals(customerPhoneId)) {
                    foundCustomerPhoneIdForUpdate = true;
                }
            }
            if (existingPhone.getId().equals(cPhone.getId())) {
                continue;
            }
            if (phoneNum.equals(existingPhone.getPhone().getPhoneNumber())) {
                errors.pushNestedPath("phone");
                errors.rejectValue("phoneNumber", "phoneNumber.duplicate", null);
                errors.popNestedPath();
            }
            if (phoneName.equalsIgnoreCase(existingPhone.getPhoneName())) {
                errors.rejectValue("phoneName", "phoneName.duplicate", null);
            }
        }
        if (phoneId != null && !foundPhoneIdForUpdate) {
            errors.pushNestedPath("phone");
            errors.rejectValue("id", "phone.invalid_id", null);
            errors.popNestedPath();
        }
        if (customerPhoneId != null && !foundCustomerPhoneIdForUpdate) {
            errors.rejectValue("id", "phone.invalid_id", null);
        }
    }
}
Also used : CustomerPhone(org.broadleafcommerce.profile.core.domain.CustomerPhone)

Example 7 with CustomerPhone

use of org.broadleafcommerce.profile.core.domain.CustomerPhone in project BroadleafCommerce by BroadleafCommerce.

the class CustomerPhoneDataProvider method createCustomerPhone.

@DataProvider(name = "setupCustomerPhone")
public static Object[][] createCustomerPhone() {
    CustomerPhone cp1 = new CustomerPhoneImpl();
    Phone phone1 = new PhoneImpl();
    phone1.setPhoneNumber("111-111-1111");
    cp1.setPhone(phone1);
    cp1.setPhoneName("phone1");
    CustomerPhone cp2 = new CustomerPhoneImpl();
    Phone phone2 = new PhoneImpl();
    phone1.setPhoneNumber("222-222-2222");
    cp2.setPhone(phone2);
    cp2.setPhoneName("phone2");
    return new Object[][] { new Object[] { cp1 }, new Object[] { cp2 } };
}
Also used : CustomerPhone(org.broadleafcommerce.profile.core.domain.CustomerPhone) Phone(org.broadleafcommerce.profile.core.domain.Phone) CustomerPhone(org.broadleafcommerce.profile.core.domain.CustomerPhone) CustomerPhoneImpl(org.broadleafcommerce.profile.core.domain.CustomerPhoneImpl) CustomerPhoneImpl(org.broadleafcommerce.profile.core.domain.CustomerPhoneImpl) PhoneImpl(org.broadleafcommerce.profile.core.domain.PhoneImpl) DataProvider(org.testng.annotations.DataProvider)

Example 8 with CustomerPhone

use of org.broadleafcommerce.profile.core.domain.CustomerPhone in project BroadleafCommerce by BroadleafCommerce.

the class CustomerPhoneServiceImpl method saveCustomerPhone.

@Override
@Transactional(TransactionUtils.DEFAULT_TRANSACTION_MANAGER)
public CustomerPhone saveCustomerPhone(CustomerPhone customerPhone) {
    List<CustomerPhone> activeCustomerPhones = readActiveCustomerPhonesByCustomerId(customerPhone.getCustomer().getId());
    if (activeCustomerPhones != null && activeCustomerPhones.isEmpty()) {
        customerPhone.getPhone().setDefault(true);
    } else {
        // if parameter customerPhone is set as default, unset all other default phones
        if (customerPhone.getPhone().isDefault()) {
            for (CustomerPhone activeCustomerPhone : activeCustomerPhones) {
                if (!activeCustomerPhone.getId().equals(customerPhone.getId()) && activeCustomerPhone.getPhone().isDefault()) {
                    activeCustomerPhone.getPhone().setDefault(false);
                    customerPhoneDao.save(activeCustomerPhone);
                }
            }
        }
    }
    return customerPhoneDao.save(customerPhone);
}
Also used : CustomerPhone(org.broadleafcommerce.profile.core.domain.CustomerPhone) Transactional(org.springframework.transaction.annotation.Transactional)

Example 9 with CustomerPhone

use of org.broadleafcommerce.profile.core.domain.CustomerPhone in project BroadleafCommerce by BroadleafCommerce.

the class CustomerPhoneDaoImpl method findDefaultCustomerPhone.

@SuppressWarnings("unchecked")
public CustomerPhone findDefaultCustomerPhone(Long customerId) {
    Query query = em.createNamedQuery("BC_FIND_DEFAULT_PHONE_BY_CUSTOMER_ID");
    query.setParameter("customerId", customerId);
    List<CustomerPhone> customerPhones = query.getResultList();
    return customerPhones.isEmpty() ? null : customerPhones.get(0);
}
Also used : CustomerPhone(org.broadleafcommerce.profile.core.domain.CustomerPhone) Query(javax.persistence.Query)

Example 10 with CustomerPhone

use of org.broadleafcommerce.profile.core.domain.CustomerPhone in project BroadleafCommerce by BroadleafCommerce.

the class CustomerPhoneControllerTest method createCustomerPhoneFromController.

@Test(groups = "createCustomerPhoneFromController", dataProvider = "setupCustomerPhoneControllerData", dataProviderClass = CustomerPhoneControllerTestDataProvider.class, dependsOnGroups = "readCustomer")
@Transactional
@Commit
public void createCustomerPhoneFromController(PhoneNameForm phoneNameForm) {
    BindingResult errors = new BeanPropertyBindingResult(phoneNameForm, "phoneNameForm");
    Customer customer = customerService.readCustomerByUsername("customer1");
    request = this.getNewServletInstance();
    request.setAttribute(CustomerStateRequestProcessor.getCustomerRequestAttributeName(), customer);
    String view = customerPhoneController.savePhone(phoneNameForm, errors, request, null, null);
    assert (view.indexOf(SUCCESS) >= 0);
    List<CustomerPhone> phones = customerPhoneService.readAllCustomerPhonesByCustomerId(userId);
    boolean inPhoneList = false;
    Long id = (Long) request.getAttribute("customerPhoneId");
    assert (id != null);
    for (CustomerPhone p : phones) {
        if ((p.getPhoneName() != null) && p.getPhoneName().equals(phoneNameForm.getPhoneName())) {
            inPhoneList = true;
        }
    }
    assert (inPhoneList == true);
    createdCustomerPhoneIds.add(id);
}
Also used : CustomerPhone(org.broadleafcommerce.profile.core.domain.CustomerPhone) BindingResult(org.springframework.validation.BindingResult) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) Customer(org.broadleafcommerce.profile.core.domain.Customer) Commit(org.springframework.test.annotation.Commit) Test(org.testng.annotations.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

CustomerPhone (org.broadleafcommerce.profile.core.domain.CustomerPhone)13 Transactional (org.springframework.transaction.annotation.Transactional)5 Test (org.testng.annotations.Test)4 Customer (org.broadleafcommerce.profile.core.domain.Customer)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 Phone (org.broadleafcommerce.profile.core.domain.Phone)2 PhoneImpl (org.broadleafcommerce.profile.core.domain.PhoneImpl)2 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)2 BindingResult (org.springframework.validation.BindingResult)2 Query (javax.persistence.Query)1 CustomerPhoneImpl (org.broadleafcommerce.profile.core.domain.CustomerPhoneImpl)1 PhoneNameForm (org.broadleafcommerce.profile.web.core.model.PhoneNameForm)1 Commit (org.springframework.test.annotation.Commit)1 Rollback (org.springframework.test.annotation.Rollback)1 DataProvider (org.testng.annotations.DataProvider)1