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

Java tutorial

Introduction

Here is the source code for net.groupbuy.dao.impl.AttributeDaoImpl.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 java.util.List;

import javax.persistence.FlushModeType;

import net.groupbuy.dao.AttributeDao;
import net.groupbuy.entity.Attribute;
import net.groupbuy.entity.Product;

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

/**
 * Dao - 
 * 
 * @author SHOP++ Team
 * @version 3.0
 */
@Repository("attributeDaoImpl")
public class AttributeDaoImpl extends BaseDaoImpl<Attribute, Long> implements AttributeDao {

    /**
     * propertyIndex?
     * 
     * @param attribute
     *            
     */
    @Override
    public void persist(Attribute attribute) {
        Assert.notNull(attribute);
        String jpql = "select attribute.propertyIndex from Attribute attribute where attribute.productCategory = :productCategory";
        List<Integer> propertyIndexs = entityManager.createQuery(jpql, Integer.class)
                .setFlushMode(FlushModeType.COMMIT).setParameter("productCategory", attribute.getProductCategory())
                .getResultList();
        for (int i = 0; i < Product.ATTRIBUTE_VALUE_PROPERTY_COUNT; i++) {
            if (!propertyIndexs.contains(i)) {
                attribute.setPropertyIndex(i);
                super.persist(attribute);
                break;
            }
        }
    }

    /**
     * ?
     * 
     * @param attribute
     *            
     */
    @Override
    public void remove(Attribute attribute) {
        if (attribute != null) {
            String propertyName = Product.ATTRIBUTE_VALUE_PROPERTY_NAME_PREFIX + attribute.getPropertyIndex();
            String jpql = "update Product product set product." + propertyName
                    + " = null where product.productCategory = :productCategory";
            entityManager.createQuery(jpql).setFlushMode(FlushModeType.COMMIT)
                    .setParameter("productCategory", attribute.getProductCategory()).executeUpdate();
            super.remove(attribute);
        }
    }

}