String custEmail = request.getParameter("custEmail");
String custPassword = request.getParameter("custPassword");
String custPassword1 = request.getParameter("custPassword1");
EntityManager em = JpaConnection.getInstance().getCurrentEntityManager();
ContentBean contentBean = getContentBean(request);
Long defaultSiteDomainId = contentBean.getSiteDomain().getSite().getSiteDomainDefault().getSiteDomainId();
Language language = contentBean.getContentSessionBean().getSiteProfile().getSiteProfileClass().getLanguage();
JSONEscapeObject jsonResult = new JSONEscapeObject();
if (Format.isNullOrEmpty(custPublicName)) {
jsonResult.put("custPublicNameError", Languages.getLangTranValue(language.getLangId(), "content.error.string.required"));
}
if (Format.isNullOrEmpty(custEmail)) {
jsonResult.put("custEmailError", Languages.getLangTranValue(language.getLangId(), "content.error.string.required"));
}
if (Format.isNullOrEmpty(custPassword)) {
jsonResult.put("custPasswordError", Languages.getLangTranValue(language.getLangId(), "content.error.string.required"));
}
if (Format.isNullOrEmpty(custPassword1)) {
jsonResult.put("custPassword1Error", Languages.getLangTranValue(language.getLangId(), "content.error.string.required"));
}
if (!Format.isNullOrEmpty(custPassword) && !Format.isNullOrEmpty(custPassword1)) {
if (!custPassword.equals(custPassword1)) {
jsonResult.put("custPasswordMatch", Languages.getLangTranValue(language.getLangId(), "content.error.password.nomatch"));
}
}
if (jsonResult.length() > 0) {
jsonResult.put("status", Constants.WEBSERVICE_STATUS_FAILED);
this.streamWebService(response, jsonResult.toHtmlString());
return null;
}
char singleCheckout = contentBean.getSiteDomain().getSite().getSingleCheckout();
String sql = "from Customer customer " +
"where customer.siteDomain.siteDomainId = :siteDomainId " +
"and custPublicName = :custPublicName ";
Query query = em.createQuery(sql);
query.setParameter("custPublicName", custPublicName);
if (singleCheckout == Constants.VALUE_YES) {
query.setParameter("siteDomainId", defaultSiteDomainId);
}
else {
query.setParameter("siteDomainId", contentBean.getSiteDomain().getSiteDomainId());
}
Customer customer = null;
try {
customer = (Customer) query.getSingleResult();
}
catch (javax.persistence.NoResultException e) {}
if (customer != null) {
jsonResult.put("status", Constants.WEBSERVICE_STATUS_FAILED);
jsonResult.put("custPublicNameError", Languages.getLangTranValue(language.getLangId(), "content.error.publicName.duplicate"));
this.streamWebService(response, jsonResult.toHtmlString());
return null;
}
sql = "from CustomerClass customerClass where siteId = :siteId and customerClass.systemRecord = 'Y'";
query = em.createQuery(sql);
query.setParameter("siteId", contentBean.getSiteDomain().getSite().getSiteId());
CustomerClass customerClass = (CustomerClass) query.getSingleResult();
sql = "from Customer customer " +
"where customer.siteDomain.siteDomainId = :siteDomainId " +
"and custEmail = :custEmail ";
query = em.createQuery(sql);
query.setParameter("custEmail", custEmail);
if (singleCheckout == Constants.VALUE_YES) {
query.setParameter("siteDomainId", defaultSiteDomainId);
}
else {
query.setParameter("siteDomainId", contentBean.getSiteDomain().getSiteDomainId());
}
try {
customer = (Customer) query.getSingleResult();
}
catch (javax.persistence.NoResultException e) {}
if (customer != null) {
jsonResult.put("status", Constants.WEBSERVICE_STATUS_FAILED);
jsonResult.put("custEmailError", Languages.getLangTranValue(language.getLangId(), "content.error.email.duplicate"));
this.streamWebService(response, jsonResult.toHtmlString());
return null;
}
customer = new Customer();
customer.setCustPublicName(custPublicName);
customer.setCustEmail(custEmail);;
customer.setCustPassword(AESEncoder.getInstance().encode(custPassword));
customer.setCustSource(Constants.CUSTOMER_SOURCE_REGISTER);
customer.setCustSourceRef("");
customer.setActive(Constants.VALUE_YES);
customer.setRecUpdateBy(Constants.USERNAME_SYSTEM);
customer.setRecUpdateDatetime(new Date());
customer.setRecCreateBy(Constants.USERNAME_SYSTEM);
customer.setRecCreateDatetime(new Date());
customer.setCustomerClass(customerClass);
customer.setSite(contentBean.getContentSessionBean().getSiteDomain().getSite());
if (singleCheckout == Constants.VALUE_YES) {
SiteDomain siteDomain = (SiteDomain) em.find(SiteDomain.class, defaultSiteDomainId);
customer.setSiteDomain(siteDomain);
query.setParameter("siteDomainId", defaultSiteDomainId);
}
else {
customer.setSiteDomain(contentBean.getSiteDomain());
}
CustomerAddress customerAddress = new CustomerAddress();
customer.setCustAddress(customerAddress);
customer.getCustAddresses().add(customerAddress);