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

Java tutorial

Introduction

Here is the source code for net.groupbuy.dao.impl.DeliveryTemplateDaoImpl.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 net.groupbuy.dao.DeliveryTemplateDao;
import net.groupbuy.entity.DeliveryTemplate;

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

/**
 * Dao - ??
 * 
 * @author SHOP++ Team
 * @version 3.0
 */
@Repository("deliveryTemplateDaoImpl")
public class DeliveryTemplateDaoImpl extends BaseDaoImpl<DeliveryTemplate, Long> implements DeliveryTemplateDao {

    public DeliveryTemplate findDefault() {
        try {
            String jpql = "select deliveryTemplate from DeliveryTemplate deliveryTemplate where deliveryTemplate.isDefault = true";
            return entityManager.createQuery(jpql, DeliveryTemplate.class).setFlushMode(FlushModeType.COMMIT)
                    .getSingleResult();
        } catch (NoResultException e) {
            return null;
        }
    }

    /**
     * ??
     * 
     * @param deliveryTemplate
     *            ??
     */
    @Override
    public void persist(DeliveryTemplate deliveryTemplate) {
        Assert.notNull(deliveryTemplate);
        if (deliveryTemplate.getIsDefault()) {
            String jpql = "update DeliveryTemplate deliveryTemplate set deliveryTemplate.isDefault = false where deliveryTemplate.isDefault = true";
            entityManager.createQuery(jpql).setFlushMode(FlushModeType.COMMIT).executeUpdate();
        }
        super.persist(deliveryTemplate);
    }

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

}