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

Java tutorial

Introduction

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

Source

/*
 * Copyright 2005-2015 shopxx.net. All rights reserved.
 * Support: http://3936242.01p.com/
 * License: http://3936242.01p.com/license
 */
package net.shopxx.service.impl;

import java.util.Collections;
import java.util.List;

import javax.annotation.Resource;

import net.shopxx.Filter;
import net.shopxx.Order;
import net.shopxx.dao.AttributeDao;
import net.shopxx.dao.GoodsDao;
import net.shopxx.dao.ProductCategoryDao;
import net.shopxx.entity.Attribute;
import net.shopxx.entity.ProductCategory;
import net.shopxx.service.AttributeService;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

@Service("attributeServiceImpl")
public class AttributeServiceImpl extends BaseServiceImpl<Attribute, Long> implements AttributeService {

    @Resource(name = "attributeDaoImpl")
    private AttributeDao attributeDao;
    @Resource(name = "productCategoryDaoImpl")
    private ProductCategoryDao productCategoryDao;
    @Resource(name = "goodsDaoImpl")
    private GoodsDao goodsDao;

    @Transactional(readOnly = true)
    public Integer findUnusedPropertyIndex(ProductCategory productCategory) {
        return attributeDao.findUnusedPropertyIndex(productCategory);
    }

    @Transactional(readOnly = true)
    public List<Attribute> findList(ProductCategory productCategory, Integer count, List<Filter> filters,
            List<Order> orders) {
        return attributeDao.findList(productCategory, count, filters, orders);
    }

    @Transactional(readOnly = true)
    @Cacheable(value = "attribute", condition = "#useCache")
    public List<Attribute> findList(Long productCategoryId, Integer count, List<Filter> filters, List<Order> orders,
            boolean useCache) {
        ProductCategory productCategory = productCategoryDao.find(productCategoryId);
        if (productCategoryId != null && productCategory == null) {
            return Collections.emptyList();
        }
        return attributeDao.findList(productCategory, count, filters, orders);
    }

    @Transactional(readOnly = true)
    public String toAttributeValue(Attribute attribute, String value) {
        Assert.notNull(attribute);

        if (StringUtils.isEmpty(value) || CollectionUtils.isEmpty(attribute.getOptions())
                || !attribute.getOptions().contains(value)) {
            return null;
        }
        return value;
    }

    @Override
    @Transactional
    @CacheEvict(value = "attribute", allEntries = true)
    public Attribute save(Attribute attribute) {
        Assert.notNull(attribute);

        Integer unusedPropertyIndex = attributeDao.findUnusedPropertyIndex(attribute.getProductCategory());
        Assert.notNull(unusedPropertyIndex);

        attribute.setPropertyIndex(unusedPropertyIndex);
        return super.save(attribute);
    }

    @Override
    @Transactional
    @CacheEvict(value = "attribute", allEntries = true)
    public Attribute update(Attribute attribute) {
        return super.update(attribute);
    }

    @Override
    @Transactional
    @CacheEvict(value = "attribute", allEntries = true)
    public Attribute update(Attribute attribute, String... ignoreProperties) {
        return super.update(attribute, ignoreProperties);
    }

    @Override
    @Transactional
    @CacheEvict(value = "attribute", allEntries = true)
    public void delete(Long id) {
        super.delete(id);
    }

    @Override
    @Transactional
    @CacheEvict(value = "attribute", allEntries = true)
    public void delete(Long... ids) {
        super.delete(ids);
    }

    @Override
    @Transactional
    @CacheEvict(value = "attribute", allEntries = true)
    public void delete(Attribute attribute) {
        if (attribute != null) {
            goodsDao.clearAttributeValue(attribute);
        }

        super.delete(attribute);
    }

}