net.shopxx.service.impl.MerchantServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for net.shopxx.service.impl.MerchantServiceImpl.java

Source

/*
 * Copyright 2005-2013 shopxx.net. All rights reserved.
 * Support: http://www.shopxx.net
 * License: http://www.shopxx.net/license
 */
package net.shopxx.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.LockModeType;
import javax.servlet.http.HttpServletRequest;

import net.shopxx.Principal;
import net.shopxx.Setting;
import net.shopxx.dao.DepositDao;
import net.shopxx.dao.MerchantDao;
import net.shopxx.entity.Admin;
import net.shopxx.entity.Deposit;
import net.shopxx.entity.Merchant;
import net.shopxx.service.MerchantService;
import net.shopxx.util.SettingUtils;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * Service - 
 * 
 * @author SHOP++ Team
 * @version 3.0
 */
@Service("merchantServiceImpl")
public class MerchantServiceImpl extends BaseServiceImpl<Merchant, Long> implements MerchantService {

    @Resource(name = "merchantDaoImpl")
    private MerchantDao merchantDao;
    @Resource(name = "depositDaoImpl")
    private DepositDao depositDao;

    @Resource(name = "merchantDaoImpl")
    public void setBaseDao(MerchantDao merchantDao) {
        super.setBaseDao(merchantDao);
    }

    @Transactional(readOnly = true)
    public boolean usernameExists(String username) {
        return merchantDao.usernameExists(username);
    }

    @Transactional(readOnly = true)
    public boolean usernameDisabled(String username) {
        Assert.hasText(username);
        Setting setting = SettingUtils.get();
        if (setting.getDisabledUsernames() != null) {
            for (String disabledUsername : setting.getDisabledUsernames()) {
                if (StringUtils.containsIgnoreCase(username, disabledUsername)) {
                    return true;
                }
            }
        }
        return false;
    }

    @Transactional(readOnly = true)
    public boolean emailExists(String email) {
        return merchantDao.emailExists(email);
    }

    @Transactional(readOnly = true)
    public boolean emailUnique(String previousEmail, String currentEmail) {
        if (StringUtils.equalsIgnoreCase(previousEmail, currentEmail)) {
            return true;
        } else {
            if (merchantDao.emailExists(currentEmail)) {
                return false;
            } else {
                return true;
            }
        }
    }

    public void save(Merchant merchant, Admin operator) {
        Assert.notNull(merchant);
        merchantDao.persist(merchant);
        if (merchant.getBalance().compareTo(new BigDecimal(0)) > 0) {
            Deposit deposit = new Deposit();
            //         deposit.setType(operator != null ? Deposit.Type.adminRecharge : Deposit.Type.merchantRecharge);
            deposit.setCredit(merchant.getBalance());
            deposit.setDebit(new BigDecimal(0));
            deposit.setBalance(merchant.getBalance());
            deposit.setOperator(operator != null ? operator.getUsername() : null);
            deposit.setMerchant(merchant);
            depositDao.persist(deposit);
        }
    }

    public void update(Merchant merchant, Integer modifyPoint, BigDecimal modifyBalance, String depositMemo,
            Admin operator) {
        Assert.notNull(merchant);

        merchantDao.lock(merchant, LockModeType.PESSIMISTIC_WRITE);

        if (modifyPoint != null && modifyPoint != 0 && merchant.getPoint() + modifyPoint >= 0) {
            merchant.setPoint(merchant.getPoint() + modifyPoint);
        }

        if (modifyBalance != null && modifyBalance.compareTo(new BigDecimal(0)) != 0
                && merchant.getBalance().add(modifyBalance).compareTo(new BigDecimal(0)) >= 0) {
            merchant.setBalance(merchant.getBalance().add(modifyBalance));
            Deposit deposit = new Deposit();
            if (modifyBalance.compareTo(new BigDecimal(0)) > 0) {
                //            deposit.setType(operator != null ? Deposit.Type.adminRecharge : Deposit.Type.merchantRecharge);
                deposit.setCredit(modifyBalance);
                deposit.setDebit(new BigDecimal(0));
            } else {
                //            deposit.setType(operator != null ? Deposit.Type.adminChargeback : Deposit.Type.merchantPayment);
                deposit.setCredit(new BigDecimal(0));
                deposit.setDebit(modifyBalance);
            }
            deposit.setBalance(merchant.getBalance());
            deposit.setOperator(operator != null ? operator.getUsername() : null);
            deposit.setMemo(depositMemo);
            deposit.setMerchant(merchant);
            depositDao.persist(deposit);
        }
        merchantDao.merge(merchant);
    }

    @Transactional(readOnly = true)
    public Merchant findByUsername(String username) {
        return merchantDao.findByUsername(username);
    }

    @Transactional(readOnly = true)
    public List<Merchant> findListByEmail(String email) {
        return merchantDao.findListByEmail(email);
    }

    @Transactional(readOnly = true)
    public List<Object[]> findPurchaseList(Date beginDate, Date endDate, Integer count) {
        return merchantDao.findPurchaseList(beginDate, endDate, count);
    }

    @Transactional(readOnly = true)
    public boolean isAuthenticated() {
        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        if (requestAttributes != null) {
            HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
            Principal principal = (Principal) request.getSession().getAttribute(Merchant.PRINCIPAL_ATTRIBUTE_NAME);
            if (principal != null) {
                return true;
            }
        }
        return false;
    }

    @Transactional(readOnly = true)
    public Merchant getCurrent() {
        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        if (requestAttributes != null) {
            HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
            Principal principal = (Principal) request.getSession().getAttribute(Merchant.PRINCIPAL_ATTRIBUTE_NAME);
            if (principal != null) {
                return merchantDao.find(principal.getId());
            }
        }
        return null;
    }

    @Transactional(readOnly = true)
    public String getCurrentUsername() {
        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        if (requestAttributes != null) {
            HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
            Principal principal = (Principal) request.getSession().getAttribute(Merchant.PRINCIPAL_ATTRIBUTE_NAME);
            if (principal != null) {
                return principal.getUsername();
            }
        }
        return null;
    }

}