com.aistor.modules.cms.service.CommentService.java Source code

Java tutorial

Introduction

Here is the source code for com.aistor.modules.cms.service.CommentService.java

Source

/**
 * Copyright &copy; 2012-2013 <a href="https://github.com/Dopas/dopas">Dopas</a> All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.aistor.modules.cms.service;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.aistor.common.persistence.Page;
import com.aistor.common.service.BaseService;
import com.aistor.modules.cms.dao.CommentDao;
import com.aistor.modules.cms.entity.Comment;

/**
 * Service
 * @author Zaric
 * @version 2013-01-15
 */
@Service
@Transactional(readOnly = true)
public class CommentService extends BaseService {

    @SuppressWarnings("unused")
    private static Logger logger = LoggerFactory.getLogger(CommentService.class);

    @Autowired
    private CommentDao commentDao;

    public Comment get(Long id) {
        return commentDao.findOne(id);
    }

    public Page<Comment> find(Page<Comment> page, Comment comment) {
        DetachedCriteria dc = commentDao.createDetachedCriteria();
        if (StringUtils.isNotEmpty(comment.getModule())) {
            dc.add(Restrictions.eq("module", comment.getModule()));
        }
        if (comment.getContentId() != null && comment.getContentId() > 0) {
            dc.add(Restrictions.eq("contentId", comment.getContentId()));
        }
        if (StringUtils.isNotEmpty(comment.getTitle())) {
            dc.add(Restrictions.like("title", "%" + comment.getTitle() + "%"));
        }
        dc.add(Restrictions.eq("status", comment.getStatus()));
        dc.addOrder(Order.desc("id"));
        return commentDao.find(page, dc);
    }

    @Transactional(readOnly = false)
    public void save(Comment comment) {
        commentDao.save(comment);
    }

    @Transactional(readOnly = false)
    public void delete(Long id, Boolean isRe) {
        commentDao.updateStatus(id, isRe != null && isRe ? Comment.STATUS_AUDIT : Comment.STATUS_DELETE);
    }

}