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);
}
}
}
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 } };
}
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);
}
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);
}
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);
}
Aggregations