use of com.salesmanager.core.model.customer.attribute.CustomerAttribute in project shopizer by shopizer-ecommerce.
the class CustomerAccountController method saveCustomerAttributes.
/**
* Manage the edition of customer attributes
* @param request
* @param locale
* @return
* @throws Exception
*/
@PreAuthorize("hasRole('AUTH_CUSTOMER')")
@RequestMapping(value = { "/attributes/save.html" }, method = RequestMethod.POST)
@ResponseBody
public ResponseEntity<String> saveCustomerAttributes(HttpServletRequest request, Locale locale) throws Exception {
AjaxResponse resp = new AjaxResponse();
final HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
MerchantStore store = (MerchantStore) request.getAttribute(Constants.MERCHANT_STORE);
// 1=1&2=on&3=eeee&4=on&customer=1
@SuppressWarnings("rawtypes") Enumeration parameterNames = request.getParameterNames();
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
Customer customer = null;
if (auth != null && request.isUserInRole("AUTH_CUSTOMER")) {
customer = customerFacade.getCustomerByUserName(auth.getName(), store);
}
if (customer == null) {
LOGGER.error("Customer id [customer] is not defined in the parameters");
resp.setStatus(AjaxResponse.RESPONSE_STATUS_FAIURE);
String returnString = resp.toJSONString();
return new ResponseEntity<String>(returnString, httpHeaders, HttpStatus.OK);
}
if (customer.getMerchantStore().getId().intValue() != store.getId().intValue()) {
LOGGER.error("Customer id does not belong to current store");
resp.setStatus(AjaxResponse.RESPONSE_STATUS_FAIURE);
String returnString = resp.toJSONString();
return new ResponseEntity<String>(returnString, httpHeaders, HttpStatus.OK);
}
List<CustomerAttribute> customerAttributes = customerAttributeService.getByCustomer(store, customer);
Map<Long, CustomerAttribute> customerAttributesMap = new HashMap<Long, CustomerAttribute>();
for (CustomerAttribute attr : customerAttributes) {
customerAttributesMap.put(attr.getCustomerOption().getId(), attr);
}
parameterNames = request.getParameterNames();
while (parameterNames.hasMoreElements()) {
String parameterName = (String) parameterNames.nextElement();
String parameterValue = request.getParameter(parameterName);
try {
String[] parameterKey = parameterName.split("-");
com.salesmanager.core.model.customer.attribute.CustomerOption customerOption = null;
com.salesmanager.core.model.customer.attribute.CustomerOptionValue customerOptionValue = null;
if (CUSTOMER_ID_PARAMETER.equals(parameterName)) {
continue;
}
if (parameterKey.length > 1) {
// parse key - value
String key = parameterKey[0];
String value = parameterKey[1];
// should be on
customerOption = customerOptionService.getById(new Long(key));
customerOptionValue = customerOptionValueService.getById(new Long(value));
} else {
customerOption = customerOptionService.getById(new Long(parameterName));
customerOptionValue = customerOptionValueService.getById(new Long(parameterValue));
}
// get the attribute
// CustomerAttribute attribute = customerAttributeService.getByCustomerOptionId(store, customer.getId(), customerOption.getId());
CustomerAttribute attribute = customerAttributesMap.get(customerOption.getId());
if (attribute == null) {
attribute = new CustomerAttribute();
attribute.setCustomer(customer);
attribute.setCustomerOption(customerOption);
} else {
customerAttributes.remove(attribute);
}
if (customerOption.getCustomerOptionType().equals(CustomerOptionType.Text.name())) {
if (!StringUtils.isBlank(parameterValue)) {
attribute.setCustomerOptionValue(customerOptionValue);
attribute.setTextValue(parameterValue);
} else {
attribute.setTextValue(null);
}
} else {
attribute.setCustomerOptionValue(customerOptionValue);
}
if (attribute.getId() != null && attribute.getId().longValue() > 0) {
if (attribute.getCustomerOptionValue() == null) {
customerAttributeService.delete(attribute);
} else {
customerAttributeService.update(attribute);
}
} else {
customerAttributeService.save(attribute);
}
} catch (Exception e) {
LOGGER.error("Cannot get parameter information " + parameterName, e);
}
}
// and now the remaining to be removed
for (CustomerAttribute attr : customerAttributes) {
customerAttributeService.delete(attr);
}
// refresh customer
Customer c = customerService.getById(customer.getId());
super.setSessionAttribute(Constants.CUSTOMER, c, request);
resp.setStatus(AjaxResponse.RESPONSE_STATUS_SUCCESS);
String returnString = resp.toJSONString();
return new ResponseEntity<String>(returnString, httpHeaders, HttpStatus.OK);
}
use of com.salesmanager.core.model.customer.attribute.CustomerAttribute 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.CustomerAttribute in project shopizer by shopizer-ecommerce.
the class ReadableCustomerMapper method merge.
@Override
public ReadableCustomer merge(Customer source, ReadableCustomer target, MerchantStore store, Language language) {
if (source.getId() != null && source.getId() > 0) {
target.setId(source.getId());
}
target.setEmailAddress(source.getEmailAddress());
if (StringUtils.isNotEmpty(source.getNick())) {
target.setUserName(source.getNick());
}
if (source.getDefaultLanguage() != null) {
target.setLanguage(source.getDefaultLanguage().getCode());
}
if (source.getGender() != null) {
target.setGender(source.getGender().name());
}
if (StringUtils.isNotEmpty(source.getProvider())) {
target.setProvider(source.getProvider());
}
if (source.getBilling() != null) {
Address address = new Address();
address.setAddress(source.getBilling().getAddress());
address.setCity(source.getBilling().getCity());
address.setCompany(source.getBilling().getCompany());
address.setFirstName(source.getBilling().getFirstName());
address.setLastName(source.getBilling().getLastName());
address.setPostalCode(source.getBilling().getPostalCode());
address.setPhone(source.getBilling().getTelephone());
if (source.getBilling().getCountry() != null) {
address.setCountry(source.getBilling().getCountry().getIsoCode());
}
if (source.getBilling().getZone() != null) {
address.setZone(source.getBilling().getZone().getCode());
}
if (source.getBilling().getState() != null) {
address.setStateProvince(source.getBilling().getState());
}
target.setFirstName(address.getFirstName());
target.setLastName(address.getLastName());
target.setBilling(address);
}
if (source.getCustomerReviewAvg() != null) {
target.setRating(source.getCustomerReviewAvg().doubleValue());
}
if (source.getCustomerReviewCount() != null) {
target.setRatingCount(source.getCustomerReviewCount().intValue());
}
if (source.getDelivery() != null) {
Address address = new Address();
address.setCity(source.getDelivery().getCity());
address.setAddress(source.getDelivery().getAddress());
address.setCompany(source.getDelivery().getCompany());
address.setFirstName(source.getDelivery().getFirstName());
address.setLastName(source.getDelivery().getLastName());
address.setPostalCode(source.getDelivery().getPostalCode());
address.setPhone(source.getDelivery().getTelephone());
if (source.getDelivery().getCountry() != null) {
address.setCountry(source.getDelivery().getCountry().getIsoCode());
}
if (source.getDelivery().getZone() != null) {
address.setZone(source.getDelivery().getZone().getCode());
}
if (source.getDelivery().getState() != null) {
address.setStateProvince(source.getDelivery().getState());
}
target.setDelivery(address);
} else {
target.setDelivery(target.getBilling());
}
if (source.getAttributes() != null) {
for (CustomerAttribute attribute : source.getAttributes()) {
ReadableCustomerAttribute readableAttribute = new ReadableCustomerAttribute();
readableAttribute.setId(attribute.getId());
readableAttribute.setTextValue(attribute.getTextValue());
ReadableCustomerOption option = new ReadableCustomerOption();
option.setId(attribute.getCustomerOption().getId());
option.setCode(attribute.getCustomerOption().getCode());
CustomerOptionDescription d = new CustomerOptionDescription();
d.setDescription(attribute.getCustomerOption().getDescriptionsSettoList().get(0).getDescription());
d.setName(attribute.getCustomerOption().getDescriptionsSettoList().get(0).getName());
option.setDescription(d);
readableAttribute.setCustomerOption(option);
ReadableCustomerOptionValue optionValue = new ReadableCustomerOptionValue();
optionValue.setId(attribute.getCustomerOptionValue().getId());
CustomerOptionValueDescription vd = new CustomerOptionValueDescription();
vd.setDescription(attribute.getCustomerOptionValue().getDescriptionsSettoList().get(0).getDescription());
vd.setName(attribute.getCustomerOptionValue().getDescriptionsSettoList().get(0).getName());
optionValue.setCode(attribute.getCustomerOptionValue().getCode());
optionValue.setDescription(vd);
readableAttribute.setCustomerOptionValue(optionValue);
target.getAttributes().add(readableAttribute);
}
if (source.getGroups() != null) {
for (Group group : source.getGroups()) {
ReadableGroup readableGroup = new ReadableGroup();
readableGroup.setId(group.getId().longValue());
readableGroup.setName(group.getGroupName());
readableGroup.setType(group.getGroupType().name());
target.getGroups().add(readableGroup);
}
}
}
return target;
}
use of com.salesmanager.core.model.customer.attribute.CustomerAttribute 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.CustomerAttribute 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