@Transactional
 public Date processLastLoggedInDate(long customerId) {
   Date lastLoggedInDate = null;
   if (customerId != 0) {
     CustomerInfo info = customerInfoDao.findById(customerId);
     if (info != null) {
       lastLoggedInDate = info.getCustomerInfoDateOfLastLogon();
     } else {
       // SET it as current date if it is customer's first login.
       lastLoggedInDate = new Date();
       info = new CustomerInfo();
       info.setCustomerInfoId(customerId);
       info.setCustomerInfoDateAccountCreated(new Date());
     }
     info.setCustomerInfoDateOfLastLogon(new Date());
     info.setCustomerInfoNumberOfLogon(
         new Integer(
             ((info.getCustomerInfoNumberOfLogon() != null)
                     ? info.getCustomerInfoNumberOfLogon()
                     : 0)
                 + 1));
     customerInfoDao.saveOrUpdate(info);
   }
   return lastLoggedInDate;
 }
  @Transactional
  public void deleteCustomer(Customer customer) throws Exception {
    CustomerInfo info = customerInfoDao.findById(customer.getCustomerId());
    if (info != null) {
      customerInfoDao.delete(info);
    }

    // customer basket

    // wishlist

    customerDao.delete(customer);
  }
 @Transactional
 public CustomerInfo findCustomerInfoById(long id) {
   return customerInfoDao.findById(id);
 }
 @Transactional
 public void saveOrUpdateCustomerInfo(CustomerInfo customerInfo) {
   customerInfoDao.saveOrUpdate(customerInfo);
 }
  @Transactional(rollbackFor = {Exception.class})
  public void saveOrUpdateCustomer(Customer customer, SystemUrlEntryType entryType, Locale locale)
      throws Exception {

    MerchantService mservice =
        (MerchantService) ServiceFactory.getService(ServiceFactory.MerchantService);

    MerchantStore store = mservice.getMerchantStore(customer.getMerchantId());
    // MerchantUserInformation minfo = mservice.getMerchantUserInfo(customer
    //		.getMerchantId());

    if (entryType == null) {
      entryType = SystemUrlEntryType.WEB;
    }

    // check if email aleady exist

    boolean isNew = false;
    if (customer.getCustomerId() == 0) {
      isNew = true;
    }

    if (isNew && !customer.isCustomerAnonymous()) {

      // generate password
      PasswordGeneratorModule passwordGenerator =
          (PasswordGeneratorModule) SpringUtil.getBean("passwordgenerator");

      // encrypt
      String key = EncryptionUtil.generatekey(String.valueOf(SecurityConstants.idConstant));
      boolean found = true;

      String password = null;
      String encrypted = null;
      // validate if already exist
      while (found) {

        password = passwordGenerator.generatePassword();
        encrypted = EncryptionUtil.encrypt(key, password);
        Customer cfound =
            customerDao.findByUserNameAndPassword(customer.getCustomerNick(), encrypted);
        if (cfound == null) {
          found = false;
        }
      }

      // store in customer
      customer.setCustomerNick(customer.getCustomerEmailAddress());
      customer.setCustomerPassword(encrypted);

      // send email
      String l = config.getString("core.system.defaultlanguage", "en");
      if (!StringUtils.isBlank(customer.getCustomerLang())) {
        l = customer.getCustomerLang();
      }

      LabelUtil lhelper = LabelUtil.getInstance();
      String subject = lhelper.getText(l, "label.profile.information");
      List params = new ArrayList();
      params.add(store.getStorename());
      String greeting = lhelper.getText(locale, "label.email.customer.greeting", params);

      String username =
          lhelper.getText(l, "label.generic.customer.username") + " " + customer.getCustomerNick();
      String pass = lhelper.getText(l, "label.generic.customer.password") + " " + password;

      String info = "";
      String portalurl = "";

      if (entryType == SystemUrlEntryType.PORTAL) {
        info = lhelper.getText(l, "label.email.customer.portalinfo");
        String url =
            "<a href=\""
                + config.getProperty("core.accountmanagement.portal.url")
                + "/"
                + customer.getMerchantId()
                + "\">"
                + config.getProperty("core.accountmanagement.portal.url")
                + "/"
                + customer.getMerchantId()
                + "</a>";
        portalurl = lhelper.getText(l, "label.email.customer.portalurl") + " " + url;
      } else {
        info = lhelper.getText(l, "label.email.customer.webinfo");
        String url =
            "<a href=\""
                + ReferenceUtil.buildCatalogUri(store)
                + "/\">"
                + ReferenceUtil.buildCatalogUri(store)
                + "/landing.action?merchantId="
                + store.getMerchantId()
                + "</a>";
        portalurl = lhelper.getText(l, "label.email.customer.weburl") + " " + url;
      }

      Map emailctx = new HashMap();
      emailctx.put("EMAIL_STORE_NAME", store.getStorename());
      emailctx.put("EMAIL_CUSTOMER_FIRSTNAME", customer.getCustomerFirstname());
      emailctx.put("EMAIL_CUSTOMER_LAST", customer.getCustomerLastname());
      emailctx.put("EMAIL_CUSTOMER_USERNAME", username);
      emailctx.put("EMAIL_CUSTOMER_PASSWORD", pass);
      emailctx.put("EMAIL_GREETING", greeting);
      emailctx.put("EMAIL_CUSTOMER_PORTAL_INFO", info);
      emailctx.put("EMAIL_CUSTOMER_PORTAL_ENTRY", portalurl);
      emailctx.put("EMAIL_CONTACT_OWNER", store.getStoreemailaddress());

      CommonService cservice = new CommonService();
      cservice.sendHtmlEmail(
          customer.getCustomerEmailAddress(),
          subject,
          store,
          emailctx,
          "email_template_customer.ftl",
          customer.getCustomerLang());
    }

    customerDao.saveOrUptade(customer);

    // set CustomerInfo

    CustomerInfo customerInfo = new CustomerInfo();
    customerInfo.setCustomerInfoId(customer.getCustomerId());

    int login = customerInfo.getCustomerInfoNumberOfLogon();
    customerInfo.setCustomerInfoNumberOfLogon(login++);
    customerInfo.setCustomerInfoDateOfLastLogon(new Date());
    customerInfoDao.saveOrUpdate(customerInfo);
  }