net.groupbuy.dao.impl.ReceiverDaoImpl.java Source code

Java tutorial

Introduction

Here is the source code for net.groupbuy.dao.impl.ReceiverDaoImpl.java

Source

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

import javax.persistence.FlushModeType;
import javax.persistence.NoResultException;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import net.groupbuy.Page;
import net.groupbuy.Pageable;
import net.groupbuy.dao.ReceiverDao;
import net.groupbuy.entity.Member;
import net.groupbuy.entity.Receiver;

import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

/**
 * Dao - ?
 * 
 * @author SHOP++ Team
 * @version 3.0
 */
@Repository("receiverDaoImpl")
public class ReceiverDaoImpl extends BaseDaoImpl<Receiver, Long> implements ReceiverDao {

    public Receiver findDefault(Member member) {
        if (member == null) {
            return null;
        }
        try {
            String jpql = "select receiver from Receiver receiver where receiver.member = :member and receiver.isDefault = true";
            return entityManager.createQuery(jpql, Receiver.class).setFlushMode(FlushModeType.COMMIT)
                    .setParameter("member", member).getSingleResult();
        } catch (NoResultException e) {
            try {
                String jpql = "select receiver from Receiver receiver where receiver.member = :member order by receiver.modifyDate desc";
                return entityManager.createQuery(jpql, Receiver.class).setFlushMode(FlushModeType.COMMIT)
                        .setParameter("member", member).setMaxResults(1).getSingleResult();
            } catch (NoResultException e1) {
                return null;
            }
        }
    }

    public Page<Receiver> findPage(Member member, Pageable pageable) {
        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Receiver> criteriaQuery = criteriaBuilder.createQuery(Receiver.class);
        Root<Receiver> root = criteriaQuery.from(Receiver.class);
        criteriaQuery.select(root);
        if (member != null) {
            criteriaQuery.where(criteriaBuilder.equal(root.get("member"), member));
        }
        return super.findPage(criteriaQuery, pageable);
    }

    /**
     * ??
     * 
     * @param receiver
     *            ?
     */
    @Override
    public void persist(Receiver receiver) {
        Assert.notNull(receiver);
        Assert.notNull(receiver.getMember());
        if (receiver.getIsDefault()) {
            String jpql = "update Receiver receiver set receiver.isDefault = false where receiver.member = :member and receiver.isDefault = true";
            entityManager.createQuery(jpql).setFlushMode(FlushModeType.COMMIT)
                    .setParameter("member", receiver.getMember()).executeUpdate();
        }
        super.persist(receiver);
    }

    /**
     * ?
     * 
     * @param receiver
     *            ?
     * @return ?
     */
    @Override
    public Receiver merge(Receiver receiver) {
        Assert.notNull(receiver);
        Assert.notNull(receiver.getMember());
        if (receiver.getIsDefault()) {
            String jpql = "update Receiver receiver set receiver.isDefault = false where receiver.member = :member and receiver.isDefault = true and receiver != :receiver";
            entityManager.createQuery(jpql).setFlushMode(FlushModeType.COMMIT)
                    .setParameter("member", receiver.getMember()).setParameter("receiver", receiver)
                    .executeUpdate();
        }
        return super.merge(receiver);
    }

}