use of com.salesmanager.core.model.customer.attribute.CustomerOptionValue in project shopizer by shopizer-ecommerce.
the class CustomerTest method createCustomer.
@Test
public void createCustomer() throws ServiceException {
Language en = languageService.getByCode("en");
MerchantStore store = merchantService.getByCode(MerchantStore.DEFAULT_STORE);
Country country = countryService.getByCode("CA");
Zone zone = zoneService.getByCode("QC");
/**
* Core customer attributes *
*/
Customer customer = new Customer();
customer.setMerchantStore(store);
customer.setEmailAddress("test@test.com");
customer.setGender(CustomerGender.M);
customer.setAnonymous(true);
customer.setCompany("ifactory");
customer.setDateOfBirth(new Date());
customer.setNick("My nick");
customer.setPassword("123456");
customer.setDefaultLanguage(store.getDefaultLanguage());
Delivery delivery = new Delivery();
delivery.setAddress("Shipping address");
delivery.setCountry(country);
delivery.setZone(zone);
Billing billing = new Billing();
billing.setFirstName("John");
billing.setLastName("Bossanova");
billing.setAddress("Billing address");
billing.setCountry(country);
billing.setZone(zone);
customer.setBilling(billing);
customer.setDelivery(delivery);
customerService.create(customer);
customer = customerService.getById(customer.getId());
// create an option value
CustomerOptionValue yes = new CustomerOptionValue();
yes.setCode("yes");
yes.setMerchantStore(store);
CustomerOptionValueDescription yesDescription = new CustomerOptionValueDescription();
yesDescription.setLanguage(en);
yesDescription.setCustomerOptionValue(yes);
CustomerOptionValueDescription yes_sir = new CustomerOptionValueDescription();
yes_sir.setCustomerOptionValue(yes);
yes_sir.setDescription("Yes sir!");
yes_sir.setName("Yes sir!");
yes_sir.setLanguage(en);
yes.getDescriptions().add(yes_sir);
// needs to be saved before using it
customerOptionValueService.create(yes);
CustomerOptionValue no = new CustomerOptionValue();
no.setCode("no");
no.setMerchantStore(store);
CustomerOptionValueDescription noDescription = new CustomerOptionValueDescription();
noDescription.setLanguage(en);
noDescription.setCustomerOptionValue(no);
CustomerOptionValueDescription no_sir = new CustomerOptionValueDescription();
no_sir.setCustomerOptionValue(no);
no_sir.setDescription("Nope!");
no_sir.setName("Nope!");
no_sir.setLanguage(en);
no.getDescriptions().add(no_sir);
// needs to be saved before using it
customerOptionValueService.create(no);
// create a customer option to be used
CustomerOption subscribedToMailingList = new CustomerOption();
subscribedToMailingList.setActive(true);
subscribedToMailingList.setPublicOption(true);
subscribedToMailingList.setCode("subscribedToMailingList");
subscribedToMailingList.setMerchantStore(store);
CustomerOptionDescription mailingListDesciption = new CustomerOptionDescription();
mailingListDesciption.setName("Subscribed to mailing list");
mailingListDesciption.setDescription("Subscribed to mailing list");
mailingListDesciption.setLanguage(en);
mailingListDesciption.setCustomerOption(subscribedToMailingList);
Set<CustomerOptionDescription> mailingListDesciptionList = new HashSet<CustomerOptionDescription>();
mailingListDesciptionList.add(mailingListDesciption);
subscribedToMailingList.setDescriptions(mailingListDesciptionList);
customerOptionService.create(subscribedToMailingList);
// create a customer option to be used
CustomerOption hasReturnedItems = new CustomerOption();
hasReturnedItems.setActive(true);
hasReturnedItems.setPublicOption(true);
hasReturnedItems.setCode("hasReturnedItems");
hasReturnedItems.setMerchantStore(store);
CustomerOptionDescription hasReturnedItemsDesciption = new CustomerOptionDescription();
hasReturnedItemsDesciption.setName("Has returned items");
hasReturnedItemsDesciption.setDescription("Has returned items");
hasReturnedItemsDesciption.setLanguage(en);
hasReturnedItemsDesciption.setCustomerOption(hasReturnedItems);
Set<CustomerOptionDescription> hasReturnedItemsList = new HashSet<CustomerOptionDescription>();
hasReturnedItemsList.add(hasReturnedItemsDesciption);
hasReturnedItems.setDescriptions(hasReturnedItemsList);
customerOptionService.create(hasReturnedItems);
subscribedToMailingList.setSortOrder(3);
customerOptionService.update(subscribedToMailingList);
// --
// now create an option set (association of a customer option with possible customer option values)
// --
// possible yes
CustomerOptionSet mailingListSetYes = new CustomerOptionSet();
mailingListSetYes.setSortOrder(0);
mailingListSetYes.setCustomerOption(subscribedToMailingList);
mailingListSetYes.setCustomerOptionValue(yes);
customerOptionSetService.create(mailingListSetYes);
// possible no
CustomerOptionSet mailingListSetNo = new CustomerOptionSet();
// mailingListSetNo.setPk(mailingListSetNoId);
mailingListSetNo.setSortOrder(1);
mailingListSetNo.setCustomerOption(subscribedToMailingList);
mailingListSetNo.setCustomerOptionValue(no);
customerOptionSetService.create(mailingListSetNo);
// possible has returned items
CustomerOptionSet hasReturnedItemsYes = new CustomerOptionSet();
hasReturnedItemsYes.setSortOrder(0);
hasReturnedItemsYes.setCustomerOption(hasReturnedItems);
hasReturnedItemsYes.setCustomerOptionValue(yes);
customerOptionSetService.create(hasReturnedItemsYes);
subscribedToMailingList.setSortOrder(2);
customerOptionService.update(subscribedToMailingList);
CustomerOption option = customerOptionService.getById(subscribedToMailingList.getId());
option.setSortOrder(4);
customerOptionService.update(option);
List<CustomerOptionSet> optionSetList = customerOptionSetService.listByStore(store, en);
// Assert.assertEquals(3, optionSetList.size());
System.out.println("Size of options : " + optionSetList.size());
/**
* Now create a customer option attribute
* A customer attribute is a selected customer option set transformed to an
* attribute for a given customer
*/
CustomerAttribute customerAttributeMailingList = new CustomerAttribute();
customerAttributeMailingList.setCustomer(customer);
customerAttributeMailingList.setCustomerOption(subscribedToMailingList);
customerAttributeMailingList.setCustomerOptionValue(no);
customer.getAttributes().add(customerAttributeMailingList);
customerService.save(customer);
customerService.delete(customer);
}
use of com.salesmanager.core.model.customer.attribute.CustomerOptionValue in project shopizer by shopizer-ecommerce.
the class CustomerPopulator method populate.
/**
* Creates a Customer entity ready to be saved
*/
@Override
public Customer populate(PersistableCustomer source, Customer target, MerchantStore store, Language language) throws ConversionException {
try {
if (source.getId() != null && source.getId() > 0) {
target.setId(source.getId());
}
if (!StringUtils.isBlank(source.getPassword())) {
target.setPassword(passwordEncoder.encode(source.getPassword()));
target.setNick(source.getUserName());
target.setAnonymous(false);
}
if (source.getBilling() != null) {
target.setBilling(new Billing());
if (!StringUtils.isEmpty(source.getFirstName())) {
target.getBilling().setFirstName(source.getFirstName());
}
if (!StringUtils.isEmpty(source.getLastName())) {
target.getBilling().setLastName(source.getLastName());
}
}
if (!StringUtils.isBlank(source.getProvider())) {
target.setProvider(source.getProvider());
}
if (!StringUtils.isBlank(source.getEmailAddress())) {
target.setEmailAddress(source.getEmailAddress());
}
if (source.getGender() != null && target.getGender() == null) {
target.setGender(com.salesmanager.core.model.customer.CustomerGender.valueOf(source.getGender()));
}
if (target.getGender() == null) {
target.setGender(com.salesmanager.core.model.customer.CustomerGender.M);
}
Map<String, Country> countries = countryService.getCountriesMap(language);
Map<String, Zone> zones = zoneService.getZones(language);
target.setMerchantStore(store);
Address sourceBilling = source.getBilling();
if (sourceBilling != null) {
Billing billing = target.getBilling();
billing.setAddress(sourceBilling.getAddress());
billing.setCity(sourceBilling.getCity());
billing.setCompany(sourceBilling.getCompany());
// billing.setCountry(country);
if (!StringUtils.isEmpty(sourceBilling.getFirstName()))
billing.setFirstName(sourceBilling.getFirstName());
if (!StringUtils.isEmpty(sourceBilling.getLastName()))
billing.setLastName(sourceBilling.getLastName());
billing.setTelephone(sourceBilling.getPhone());
billing.setPostalCode(sourceBilling.getPostalCode());
billing.setState(sourceBilling.getStateProvince());
Country billingCountry = null;
if (!StringUtils.isBlank(sourceBilling.getCountry())) {
billingCountry = countries.get(sourceBilling.getCountry());
if (billingCountry == null) {
throw new ConversionException("Unsuported country code " + sourceBilling.getCountry());
}
billing.setCountry(billingCountry);
}
if (billingCountry != null && !StringUtils.isBlank(sourceBilling.getZone())) {
Zone zone = zoneService.getByCode(sourceBilling.getZone());
if (zone == null) {
throw new ConversionException("Unsuported zone code " + sourceBilling.getZone());
}
Zone zoneDescription = zones.get(zone.getCode());
billing.setZone(zoneDescription);
}
// target.setBilling(billing);
}
if (target.getBilling() == null && source.getBilling() != null) {
LOG.info("Setting default values for billing");
Billing billing = new Billing();
Country billingCountry = null;
if (StringUtils.isNotBlank(source.getBilling().getCountry())) {
billingCountry = countries.get(source.getBilling().getCountry());
if (billingCountry == null) {
throw new ConversionException("Unsuported country code " + sourceBilling.getCountry());
}
billing.setCountry(billingCountry);
target.setBilling(billing);
}
}
Address sourceShipping = source.getDelivery();
if (sourceShipping != null) {
Delivery delivery = new Delivery();
delivery.setAddress(sourceShipping.getAddress());
delivery.setCity(sourceShipping.getCity());
delivery.setCompany(sourceShipping.getCompany());
delivery.setFirstName(sourceShipping.getFirstName());
delivery.setLastName(sourceShipping.getLastName());
delivery.setTelephone(sourceShipping.getPhone());
delivery.setPostalCode(sourceShipping.getPostalCode());
delivery.setState(sourceShipping.getStateProvince());
Country deliveryCountry = null;
if (!StringUtils.isBlank(sourceShipping.getCountry())) {
deliveryCountry = countries.get(sourceShipping.getCountry());
if (deliveryCountry == null) {
throw new ConversionException("Unsuported country code " + sourceShipping.getCountry());
}
delivery.setCountry(deliveryCountry);
}
if (deliveryCountry != null && !StringUtils.isBlank(sourceShipping.getZone())) {
Zone zone = zoneService.getByCode(sourceShipping.getZone());
if (zone == null) {
throw new ConversionException("Unsuported zone code " + sourceShipping.getZone());
}
Zone zoneDescription = zones.get(zone.getCode());
delivery.setZone(zoneDescription);
}
target.setDelivery(delivery);
}
if (source.getRating() != null && source.getRating().doubleValue() > 0) {
target.setCustomerReviewAvg(new BigDecimal(source.getRating().doubleValue()));
}
if (source.getRatingCount() > 0) {
target.setCustomerReviewCount(source.getRatingCount());
}
if (target.getDelivery() == null && source.getDelivery() != null) {
LOG.info("Setting default value for delivery");
Delivery delivery = new Delivery();
Country deliveryCountry = null;
if (StringUtils.isNotBlank(source.getDelivery().getCountry())) {
deliveryCountry = countries.get(source.getDelivery().getCountry());
if (deliveryCountry == null) {
throw new ConversionException("Unsuported country code " + sourceShipping.getCountry());
}
delivery.setCountry(deliveryCountry);
target.setDelivery(delivery);
}
}
if (source.getAttributes() != null) {
for (PersistableCustomerAttribute attr : source.getAttributes()) {
CustomerOption customerOption = customerOptionService.getById(attr.getCustomerOption().getId());
if (customerOption == null) {
throw new ConversionException("Customer option id " + attr.getCustomerOption().getId() + " does not exist");
}
CustomerOptionValue customerOptionValue = customerOptionValueService.getById(attr.getCustomerOptionValue().getId());
if (customerOptionValue == null) {
throw new ConversionException("Customer option value id " + attr.getCustomerOptionValue().getId() + " does not exist");
}
if (customerOption.getMerchantStore().getId().intValue() != store.getId().intValue()) {
throw new ConversionException("Invalid customer option id ");
}
if (customerOptionValue.getMerchantStore().getId().intValue() != store.getId().intValue()) {
throw new ConversionException("Invalid customer option value id ");
}
CustomerAttribute attribute = new CustomerAttribute();
attribute.setCustomer(target);
attribute.setCustomerOption(customerOption);
attribute.setCustomerOptionValue(customerOptionValue);
attribute.setTextValue(attr.getTextValue());
target.getAttributes().add(attribute);
}
}
if (target.getDefaultLanguage() == null) {
Language lang = source.getLanguage() == null ? language : languageService.getByCode(source.getLanguage());
target.setDefaultLanguage(lang);
}
} catch (Exception e) {
throw new ConversionException(e);
}
return target;
}
use of com.salesmanager.core.model.customer.attribute.CustomerOptionValue in project shopizer by shopizer-ecommerce.
the class CustomerOptionValueServiceImpl method delete.
public void delete(CustomerOptionValue customerOptionValue) throws ServiceException {
// remove all attributes having this option
List<CustomerAttribute> attributes = customerAttributeService.getByCustomerOptionValueId(customerOptionValue.getMerchantStore(), customerOptionValue.getId());
for (CustomerAttribute attribute : attributes) {
customerAttributeService.delete(attribute);
}
List<CustomerOptionSet> optionSets = customerOptionSetService.listByOptionValue(customerOptionValue, customerOptionValue.getMerchantStore());
for (CustomerOptionSet optionSet : optionSets) {
customerOptionSetService.delete(optionSet);
}
CustomerOptionValue option = super.getById(customerOptionValue.getId());
// remove option
super.delete(option);
}
Aggregations