com.ewcms.content.document.service.ArticleMainService.java Source code

Java tutorial

Introduction

Here is the source code for com.ewcms.content.document.service.ArticleMainService.java

Source

/**
 * Copyright (c)2010-2011 Enterprise Website Content Management System(EWCMS), All rights reserved.
 * EWCMS PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * http://www.ewcms.com
 */

package com.ewcms.content.document.service;

import static com.ewcms.common.lang.EmptyUtil.isNotNull;
import static com.ewcms.common.lang.EmptyUtil.isNull;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.ewcms.common.io.HtmlStringUtil;
import com.ewcms.content.document.BaseException;
import com.ewcms.content.document.dao.ArticleMainDAO;
import com.ewcms.content.document.dao.ReviewProcessDAO;
import com.ewcms.content.document.model.Article;
import com.ewcms.content.document.model.ArticleMain;
import com.ewcms.content.document.model.OperateTrack;
import com.ewcms.content.document.model.Article.Status;
import com.ewcms.content.document.model.Article.Type;
import com.ewcms.content.document.model.Content;
import com.ewcms.content.document.model.ReviewGroup;
import com.ewcms.content.document.model.ReviewProcess;
import com.ewcms.content.document.model.ReviewUser;
import com.ewcms.content.document.util.search.ExtractKeywordAndSummary;
import com.ewcms.content.history.History;
import com.ewcms.core.site.dao.ChannelDAO;
import com.ewcms.core.site.dao.TemplateDAO;
import com.ewcms.core.site.model.Channel;
import com.ewcms.core.site.model.Site;
import com.ewcms.core.site.model.SiteServer;
import com.ewcms.core.site.model.Template;
import com.ewcms.publication.PublishException;
import com.ewcms.publication.WebPublishFacable;
import com.ewcms.publication.deploy.DeployOperatorable;
import com.ewcms.security.manage.service.GroupServiceable;
import com.ewcms.security.manage.service.UserServiceable;
import com.ewcms.web.util.EwcmsContextUtil;

/**
 * 
 * @author ?
 */
@Service
public class ArticleMainService implements ArticleMainServiceable {

    @Autowired
    private ArticleMainDAO articleMainDAO;
    @Autowired
    private ReviewProcessDAO reviewProcessDAO;
    @Autowired
    private OperateTrackServiceable operateTrackService;
    @Autowired
    private WebPublishFacable webPublish;
    @Autowired
    private ChannelDAO channelDAO;
    @Autowired
    private TemplateDAO templateDAO;
    @Autowired
    private UserServiceable userService;

    @Override
    public ArticleMain findArticleMainByArticleMainAndChannel(Long articleMainId, Integer channelId) {
        return articleMainDAO.findArticleMainByArticleMainAndChannel(articleMainId, channelId);
    }

    @Override
    public void delArticleMain(Long articleMainId, Integer channelId) {
        ArticleMain articleMain = articleMainDAO.findArticleMainByArticleMainAndChannel(articleMainId, channelId);
        Assert.notNull(articleMain);
        if (isNotNull(articleMain.getReference()) && articleMain.getReference()) {
            articleMain.setArticle(null);
        }
        articleMainDAO.remove(articleMain);
        operateTrackService.delOperateTrack(articleMainId);
    }

    @Override
    public void delArticleMainToRecycleBin(Long articleMainId, Integer channelId) {
        ArticleMain articleMain = articleMainDAO.findArticleMainByArticleMainAndChannel(articleMainId, channelId);
        Assert.notNull(articleMain);
        Article article = articleMain.getArticle();
        Assert.notNull(article);
        Status oldStatus = article.getStatus();

        if (isNotNull(articleMain.getReference()) && articleMain.getReference()) {
            articleMain.setArticle(null);
            articleMainDAO.remove(articleMain);
        } else {
            if (isNotNull(articleMain.getShare()) && articleMain.getShare()) {
                List<ArticleMain> articleMains = articleMainDAO
                        .findArticleMainByArticleIdAndReference(article.getId(), true);
                if (articleMains != null && !articleMains.isEmpty()) {
                    for (ArticleMain vo : articleMains) {
                        if (isNotNull(vo.getReference()) && vo.getReference()) {
                            vo.setArticle(null);
                            articleMainDAO.remove(vo);
                        }
                    }
                }
            }

            operateTrackService.addOperateTrack(articleMainId, article.getStatusDescription(),
                    "", "");

            article.setStatus(Status.REEDIT);
            article.setDelete(true);
            articleMain.setArticle(article);
            articleMainDAO.merge(articleMain);
        }

        if (oldStatus == Status.RELEASE && !articleMain.getReference()) {
            Site site = EwcmsContextUtil.getCurrentSite();
            SiteServer siteServer = site.getSiteServer();
            DeployOperatorable output = siteServer.getOutputType().deployOperator(siteServer);
            try {
                output.delete(article.getUrl());
            } catch (PublishException e) {
            }
            try {
                associateRelease(channelId);
            } catch (PublishException e) {
            }
        }
    }

    @Override
    public void restoreArticleMain(Long articleMainId, Integer channelId) {
        ArticleMain articleMain = articleMainDAO.findArticleMainByArticleMainAndChannel(articleMainId, channelId);
        Assert.notNull(articleMain);
        Article article = articleMain.getArticle();
        Assert.notNull(article);
        article.setDelete(false);
        articleMain.setArticle(article);
        articleMainDAO.merge(articleMain);

        operateTrackService.addOperateTrack(articleMainId, article.getStatusDescription(),
                "", "");
    }

    @Override
    public void submitReviewArticleMain(List<Long> articleMainIds, Integer channelId) throws BaseException {
        for (Long articleMainId : articleMainIds) {
            ArticleMain articleMain = articleMainDAO.findArticleMainByArticleMainAndChannel(articleMainId,
                    channelId);
            Assert.notNull(articleMain);
            Article article = articleMain.getArticle();
            Assert.notNull(article);
            if (article.getStatus() == Status.DRAFT || article.getStatus() == Status.REEDIT) {
                ReviewProcess reviewProcess = reviewProcessDAO.findFirstReviewProcessByChannel(channelId);
                if (reviewProcess == null) {
                    operateTrackService.addOperateTrack(articleMainId, article.getStatusDescription(),
                            "?", "");

                    article.setStatus(Status.PRERELEASE);
                    article.setReviewProcess(null);
                } else {
                    UserDetails userDetails = EwcmsContextUtil.getUserDetails();
                    Collection<GrantedAuthority> authorities = userDetails.getAuthorities();
                    Boolean isAdmin = false;
                    for (GrantedAuthority ga : authorities) {
                        if (ga.getAuthority().toUpperCase().equals("ROLE_ADMIN")) {
                            operateTrackService.addOperateTrack(articleMainId, article.getStatusDescription(),
                                    "???", "");

                            article.setStatus(Status.PRERELEASE);
                            article.setReviewProcess(null);
                            isAdmin = true;
                        }
                    }

                    if (!isAdmin) {
                        operateTrackService.addOperateTrack(articleMainId, article.getStatusDescription(),
                                "???" + reviewProcess.getName() + "", "");

                        article.setStatus(Status.REVIEW);
                        article.setReviewProcess(reviewProcess);
                    }
                }
                if (article.getPublished() == null) {
                    article.setPublished(new Date(Calendar.getInstance().getTime().getTime()));
                }
                articleMain.setArticle(article);
                articleMainDAO.merge(articleMain);
                //         }else{
                //            throw new BaseException("","???????");
            }
        }
    }

    @Override
    public Boolean copyArticleMainToChannel(List<Long> articleMainIds, List<Integer> channelIds,
            Integer source_channelId) {
        ArticleMain articleMain = null;
        Article article = null;
        Channel channel = channelDAO.get(source_channelId);
        Assert.notNull(channel);
        String source_channelName = channel.getName();
        for (Integer target_channelId : channelIds) {
            for (Long articleMainId : articleMainIds) {
                articleMain = articleMainDAO.findArticleMainByArticleMainAndChannel(articleMainId,
                        source_channelId);
                if (isNull(articleMain))
                    continue;
                if (isNotNull(articleMain.getReference()) && articleMain.getReference())
                    continue;
                article = articleMain.getArticle();
                if (isNull(article))
                    continue;
                if (target_channelId != source_channelId) {
                    Article target_article = new Article();

                    target_article.setStatus(Status.DRAFT);
                    target_article.setPublished(null);
                    if (article.getType() == Type.TITLE) {
                        target_article.setUrl(article.getUrl());
                    } else {
                        target_article.setUrl(null);
                    }
                    target_article.setDelete(article.getDelete());

                    List<Content> contents = article.getContents();
                    List<Content> contents_target = new ArrayList<Content>();
                    for (Content content : contents) {
                        Content content_target = new Content();
                        content_target.setDetail(content.getDetail());
                        content_target.setPage(content.getPage());

                        contents_target.add(content_target);
                    }
                    target_article.setContents(contents_target);

                    target_article.setTitle(article.getTitle());
                    target_article.setShortTitle(article.getShortTitle());
                    target_article.setSubTitle(article.getSubTitle());
                    target_article.setAuthor(article.getAuthor());
                    target_article.setOrigin(article.getOrigin());
                    target_article.setKeyword(article.getKeyword());
                    target_article.setTag(article.getTag());
                    target_article.setSummary(article.getSummary());
                    target_article.setImage(article.getImage());
                    target_article.setComment(article.getComment());
                    target_article.setType(article.getType());
                    target_article.setModified(article.getModified());
                    target_article.setPublished(article.getPublished());
                    target_article.setCreateTime(article.getCreateTime());
                    target_article.setInside(article.getInside());
                    target_article.setOwner(article.getOwner());
                    target_article.setContentTotal(article.getContentTotal());

                    ArticleMain articleMain_new = new ArticleMain();
                    articleMain_new.setArticle(target_article);
                    articleMain_new.setChannelId(target_channelId);

                    articleMainDAO.persist(articleMain_new);
                    articleMainDAO.flush(articleMain_new);

                    String target_channelName = channelDAO.get(target_channelId).getName();
                    operateTrackService.addOperateTrack(articleMainId, article.getStatusDescription(),
                            "?" + target_channelName + "??", "");
                    operateTrackService.addOperateTrack(articleMain_new.getId(),
                            target_article.getStatusDescription(),
                            "" + source_channelName + "???", "");
                }
            }
        }
        return true;
    }

    public void copyArticleMainFromShare(List<Long> articleMainIds, List<Integer> channelIds) {
        ArticleMain articleMain = null;
        Article article = null;
        for (Integer target_channelId : channelIds) {
            for (Long articleMainId : articleMainIds) {
                articleMain = articleMainDAO.get(articleMainId);
                if (isNull(articleMain))
                    continue;
                if (isNotNull(articleMain.getReference()) && articleMain.getReference())
                    continue;
                article = articleMain.getArticle();
                if (isNull(article))
                    continue;
                if (articleMain.getChannelId() != null && target_channelId != articleMain.getChannelId()) {
                    Article target_article = new Article();

                    target_article.setStatus(Status.DRAFT);
                    target_article.setPublished(null);
                    if (article.getType() == Type.TITLE) {
                        target_article.setUrl(article.getUrl());
                    } else {
                        target_article.setUrl(null);
                    }
                    target_article.setDelete(article.getDelete());

                    List<Content> contents = article.getContents();
                    List<Content> contents_target = new ArrayList<Content>();
                    for (Content content : contents) {
                        Content content_target = new Content();
                        content_target.setDetail(content.getDetail());
                        content_target.setPage(content.getPage());

                        contents_target.add(content_target);
                    }
                    target_article.setContents(contents_target);

                    target_article.setTitle(article.getTitle());
                    target_article.setShortTitle(article.getShortTitle());
                    target_article.setSubTitle(article.getSubTitle());
                    target_article.setAuthor(article.getAuthor());
                    target_article.setOrigin(article.getOrigin());
                    target_article.setKeyword(article.getKeyword());
                    target_article.setTag(article.getTag());
                    target_article.setSummary(article.getSummary());
                    target_article.setImage(article.getImage());
                    target_article.setComment(article.getComment());
                    target_article.setType(article.getType());
                    target_article.setModified(article.getModified());
                    target_article.setPublished(article.getPublished());
                    target_article.setCreateTime(article.getCreateTime());
                    target_article.setInside(article.getInside());
                    target_article.setOwner(EwcmsContextUtil.getUserName());
                    target_article.setContentTotal(article.getContentTotal());

                    ArticleMain articleMain_new = new ArticleMain();
                    articleMain_new.setArticle(target_article);
                    articleMain_new.setChannelId(target_channelId);

                    articleMainDAO.persist(articleMain_new);
                    articleMainDAO.flush(articleMain_new);

                    String target_channelName = channelDAO.get(target_channelId).getName();
                    operateTrackService.addOperateTrack(articleMainId, article.getStatusDescription(),
                            "?" + target_channelName + "??", "");
                    operateTrackService.addOperateTrack(articleMain_new.getId(),
                            target_article.getStatusDescription(), "?", "");
                }
            }
        }
    }

    public void refenceArticleMainFromShare(List<Long> articleMainIds, List<Integer> channelIds) {
        ArticleMain articleMain = null;
        Article article = null;
        for (Integer target_channelId : channelIds) {
            for (Long articleMainId : articleMainIds) {
                articleMain = articleMainDAO.get(articleMainId);
                if (isNull(articleMain))
                    continue;
                if (isNotNull(articleMain.getReference()) && articleMain.getReference())
                    continue;
                article = articleMain.getArticle();
                if (isNull(article))
                    continue;
                if (articleMain.getChannelId() != null && target_channelId != articleMain.getChannelId()) {
                    ArticleMain articleMain_new = new ArticleMain();
                    articleMain_new.setArticle(article);
                    articleMain_new.setChannelId(target_channelId);
                    articleMain_new.setReference(true);
                    articleMain_new.setSort(articleMain.getSort());
                    articleMain_new.setTop(articleMain.getTop());

                    articleMainDAO.persist(articleMain_new);
                    articleMainDAO.flush(articleMain_new);

                    String target_channelName = channelDAO.get(target_channelId).getName();
                    operateTrackService.addOperateTrack(articleMainId, article.getStatusDescription(),
                            "" + target_channelName + "??", "");
                    operateTrackService.addOperateTrack(articleMain_new.getId(), article.getStatusDescription(),
                            "", "");
                }
            }
        }
    }

    @Override
    public Boolean moveArticleMainToChannel(List<Long> articleMainIds, List<Integer> channelIds,
            Integer source_channelId) {
        ArticleMain articleMain = null;
        Channel channel = channelDAO.get(source_channelId);
        Assert.notNull(channel);
        String source_channelName = channel.getName();
        for (Integer target_channelId : channelIds) {
            for (Long articleMainId : articleMainIds) {
                articleMain = articleMainDAO.findArticleMainByArticleMainAndChannel(articleMainId,
                        source_channelId);
                if (isNull(articleMain))
                    continue;
                if (target_channelId != source_channelId) {
                    Article article = articleMain.getArticle();

                    operateTrackService.addOperateTrack(articleMainId, article.getStatusDescription(),
                            "" + source_channelName + "??", "");

                    articleMain.setArticle(article);
                    articleMain.setChannelId(target_channelId);
                    articleMainDAO.merge(articleMain);
                }
            }
        }
        return true;
    }

    @Override
    public List<ArticleMain> findArticleMainByChannel(Integer channelId) {
        return articleMainDAO.findArticleMainByChannel(channelId);
    }

    @Override
    public void pubArticleMainByChannel(Integer channelId, Boolean again, Boolean children)
            throws PublishException {
        if (isNotNull(channelId)) {
            Channel channel = channelDAO.get(channelId);
            if (!channel.getPublicenable()) {
                throw new PublishException("????");
            }
            List<ArticleMain> articleMains = articleMainDAO.findArticleMainByTitlePrerelease();
            if (articleMains != null && !articleMains.isEmpty()) {
                for (ArticleMain articleMain : articleMains) {
                    Article article = articleMain.getArticle();
                    if (article != null && article.getUrl().trim().length() > 0) {
                        article.setStatus(Article.Status.RELEASE);
                        articleMain.setArticle(article);
                        articleMainDAO.merge(articleMain);
                    }
                }
            }
            webPublish.publishChannel(channelId, again, children);
        }
    }

    @Override
    public void associateRelease(Integer channelId) throws PublishException {
        if (isNotNull(channelId)) {
            pubArticleMainByChannel(channelId, false, false);

            List<Template> templates = templateDAO.getTemplatesInChannel(channelId);
            if (templates != null && !templates.isEmpty()) {
                for (Template template : templates) {
                    webPublish.publishTemplateContent(template.getId(), false);
                }
            }
            List<Channel> channelChildrens = channelDAO.getChannelChildren(channelId);
            if (channelChildrens != null && !channelChildrens.isEmpty()) {
                for (Channel channelChildren : channelChildrens) {
                    if (channelChildren.getPublicenable())
                        associateRelease(channelChildren.getId());
                }
            }

            Channel channel = channelDAO.get(channelId);
            String appChannel = channel.getAppChannel();
            if (appChannel != null && appChannel.length() > 0) {
                String[] appChannelIds = appChannel.split(",");
                for (String id : appChannelIds) {
                    try {
                        Integer tempId = Integer.valueOf(id);
                        pubArticleMainByChannel(tempId, false, false);
                    } catch (Exception e) {
                    }
                }
            }
        }
    }

    @Override
    public Boolean reviewArticleMainIsEffective(Long articleMainId, Integer channelId) {
        if (isNull(articleMainId) && isNull(channelId))
            return false;
        ArticleMain articleMain = articleMainDAO.findArticleMainByArticleMainAndChannel(articleMainId, channelId);
        if (isNull(articleMain))
            return false;
        Article article = articleMain.getArticle();
        if (isNull(article))
            return false;
        if (article.getStatus() == Status.REVIEW) {
            ReviewProcess rp = reviewProcessDAO.findReviewProcessByIdAndChannel(article.getReviewProcess().getId(),
                    channelId);
            List<ReviewUser> reviewUsers = rp.getReviewUsers();
            List<ReviewGroup> reviewGroups = rp.getReviewGroups();
            if (reviewUsers.isEmpty() && reviewGroups.isEmpty()) {
                return false;
            }
            String userName = EwcmsContextUtil.getUserName();
            if (isNull(userName) || userName.length() == 0)
                return false;
            for (ReviewUser reviewUser : reviewUsers) {
                if (reviewUser.getUserName().equals(userName)) {
                    return true;
                }
            }
            Collection<String> grouptNames = EwcmsContextUtil.getGroupnames();
            if (isNull(grouptNames))
                return false;
            for (ReviewGroup reviewGroup : reviewGroups) {
                for (String groupName : grouptNames) {
                    if (reviewGroup.getGroupName().equals(groupName)) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    @Override
    public void reviewArticleMain(Long articleMainId, Integer channelId, Integer review, String reason) {
        ArticleMain articleMain = null;
        Article article = null;
        Assert.notNull(articleMainId);
        articleMain = articleMainDAO.findArticleMainByArticleMainAndChannel(articleMainId, channelId);
        if (isNull(articleMain))
            return;
        article = articleMain.getArticle();
        if (isNull(article))
            return;
        if (article.getStatus() == Status.REVIEW) {
            ReviewProcess rp = reviewProcessDAO.findReviewProcessByIdAndChannel(article.getReviewProcess().getId(),
                    channelId);
            String currentStatus = article.getStatusDescription();
            String caption = "";
            if (review == 0) {// 
                if (rp != null) {
                    caption = "?" + rp.getName() + "<span style='color:blue;'></span>";
                    if (rp.getNextProcess() != null) {
                        article.setReviewProcess(rp.getNextProcess());
                        caption += "???" + rp.getNextProcess().getName() + "";
                    } else {
                        article.setStatus(Status.PRERELEASE);
                        article.setReviewProcess(null);
                        caption += "??";
                    }
                } else {
                    article.setStatus(Status.REVIEWBREAK);
                    caption = "???????????";
                }
            } else if (review == 1) {// ?
                if (rp != null) {
                    caption = "?" + rp.getName() + "<span style='color:red;'>?</span>";
                    if (rp.getPrevProcess() != null) {
                        article.setReviewProcess(rp.getPrevProcess());
                        caption += "?" + rp.getPrevProcess().getName()
                                + "?";
                    } else {
                        article.setStatus(Status.REEDIT);
                        article.setReviewProcess(null);
                        caption += "??";
                    }
                } else {
                    article.setStatus(Status.REVIEWBREAK);
                    caption = "???????????";
                }
            }
            articleMain.setArticle(article);
            articleMainDAO.merge(articleMain);

            operateTrackService.addOperateTrack(articleMainId, currentStatus, caption, reason);
        }
    }

    @Override
    public void moveArticleMainSort(Long articleMainId, Integer channelId, Long sort, Integer isInsert,
            Boolean isTop) {
        ArticleMain articleMain = articleMainDAO.findArticleMainByChannelAndEqualSort(channelId, sort, isTop);
        String desc = "??" + sort + "";
        if (articleMain == null) {
            articleMain = articleMainDAO.findArticleMainByArticleMainAndChannel(articleMainId, channelId);
            Assert.notNull(articleMain);
            articleMain.setSort(sort);
            articleMainDAO.merge(articleMain);
        } else {
            ArticleMain articleMain_new = articleMainDAO.findArticleMainByArticleMainAndChannel(articleMainId,
                    channelId);
            Assert.notNull(articleMain_new);
            articleMain_new.setSort(sort);
            if (isInsert == 0) {//?
                List<ArticleMain> articleMains = articleMainDAO.findArticleMainByChannelAndThanSort(channelId, sort,
                        isTop);
                if (!articleMains.isEmpty()) {
                    for (ArticleMain articleMain_old : articleMains) {
                        if (articleMain_new.getId() == articleMain_old.getId())
                            continue;
                        articleMain_old.setSort(articleMain_old.getSort() + 1);
                        articleMainDAO.merge(articleMain_old);
                    }
                }
                desc = "???" + sort + "";
                articleMainDAO.merge(articleMain_new);
            } else if (isInsert == 1) {//?
                if (articleMain_new.getId() != articleMain.getId()) {
                    articleMain.setSort(null);
                    articleMainDAO.merge(articleMain);
                    desc = "???" + sort + "";
                    articleMainDAO.merge(articleMain_new);
                }
            }
        }
        operateTrackService.addOperateTrack(articleMainId, articleMain.getArticle().getStatusDescription(), desc,
                "");
    }

    @Override
    public Boolean findArticleMainByChannelAndEqualSort(Integer channelId, Long sort, Boolean isTop) {
        ArticleMain articleMain = articleMainDAO.findArticleMainByChannelAndEqualSort(channelId, sort, isTop);
        if (articleMain == null)
            return false;
        return true;
    }

    @Override
    public void clearArticleMainSort(List<Long> articleMainIds, Integer channelId) {
        Assert.notNull(articleMainIds);
        for (Long articleMainId : articleMainIds) {
            ArticleMain articleMain = articleMainDAO.findArticleMainByArticleMainAndChannel(articleMainId,
                    channelId);
            Assert.notNull(articleMain);
            if (articleMain.getSort() != null) {
                articleMain.setSort(null);
                articleMainDAO.merge(articleMain);

                operateTrackService.addOperateTrack(articleMainId, articleMain.getArticle().getStatusDescription(),
                        "??", "");
            }
        }
    }

    @Override
    public void breakArticleMain(List<Long> articleMainIds, Integer channelId) throws BaseException {
        for (Long articleMainId : articleMainIds) {
            ArticleMain articleMain = articleMainDAO.get(articleMainId);
            Assert.notNull(articleMain);
            Article article = articleMain.getArticle();
            Assert.notNull(article);
            if (article.getStatus() == Status.PRERELEASE || article.getStatus() == Status.RELEASE
                    || article.getStatus() == Status.REVIEWBREAK) {

                operateTrackService.addOperateTrack(articleMainId, article.getStatusDescription(),
                        "??", "");

                article.setStatus(Status.REEDIT);
                articleMain.setArticle(article);
                articleMainDAO.merge(articleMain);
                //         }else{
                //            throw new BaseException("","???????");
            }
        }
    }

    @Override
    public String getArticleOperateTrack(Long trackId) {
        OperateTrack track = operateTrackService.findOperateTrackById(trackId);
        if (track == null)
            return "";
        return (track.getReason() == null) ? "" : track.getReason();
    }

    @Override
    public void delArticleMainByCrawler(Integer channelId, String userName) {
        List<ArticleMain> articleMains = articleMainDAO.findArticleMainByChannelIdAndUserName(channelId, userName);
        for (ArticleMain articleMain : articleMains) {
            if (articleMain.getArticle().getStatus() == Status.RELEASE)
                continue;
            articleMainDAO.remove(articleMain);
        }
    }

    @Override
    public void topArticleMain(List<Long> articleMainIds, Boolean top) {
        Assert.notNull(articleMainIds);
        for (Long articleMainId : articleMainIds) {
            ArticleMain articleMain = articleMainDAO.get(articleMainId);
            if (articleMain == null)
                continue;
            if (articleMain.getTop() != top) {
                articleMain.setTop(top);
                articleMainDAO.merge(articleMain);

                String desc = "";
                if (!top)
                    desc = "?";
                operateTrackService.addOperateTrack(articleMainId, articleMain.getArticle().getStatusDescription(),
                        desc, "");
            }
        }
    }

    @Override
    public Boolean findArticleIsEntityByArticleAndCategory(Long articleId, Long categoryId) {
        return articleMainDAO.findArticleIsEntityByArticleAndCategory(articleId, categoryId);
    }

    @Override
    @History(modelObjectIndex = 0)
    public Long addArticleMain(Article article, Integer channelId, Date published) {
        Assert.notNull(article);
        if (isNotNull(published)) {
            article.setPublished(published);
        }
        if (isNull(article.getOwner())) {
            article.setOwner(EwcmsContextUtil.getUserName());
        }

        article.setModified(new Date(Calendar.getInstance().getTime().getTime()));
        if (article.getType() == Type.TITLE) {
            titleArticleContentNull(article);
        } else {
            keywordAndSummary(article);
        }

        ArticleMain articleMain = new ArticleMain();
        articleMain.setArticle(article);
        articleMain.setChannelId(channelId);

        articleMainDAO.persist(articleMain);
        articleMainDAO.flush(articleMain);

        operateTrackService.addOperateTrack(articleMain.getId(), article.getStatusDescription(), "", "");

        return articleMain.getId();
    }

    @Override
    public Long addArticleMainByCrawler(Article article, Integer channelId, String userName) {
        Assert.notNull(article);
        if (articleMainDAO.findArticleTitleIsEntityByCrawler(article.getTitle(), channelId, userName))
            return null;

        article.setOwner(userName);
        article.setModified(new Date(Calendar.getInstance().getTime().getTime()));
        //keywordAndSummary(article);

        ArticleMain articleMain = new ArticleMain();

        articleMain.setArticle(article);
        articleMain.setChannelId(channelId);

        articleMainDAO.persist(articleMain);
        articleMainDAO.flush(articleMain);

        operateTrackService.addOperateTrack(articleMain.getId(), article.getStatusDescription(),
                "", "", userName, "");

        return articleMain.getId();
    }

    public Long findArticleMainCountByCrawler(Integer channelId, String userName) {
        return articleMainDAO.findArticleMainCountByCrawler(channelId, userName);
    }

    @Override
    @History(modelObjectIndex = 0)
    public Long updArticleMain(Article article, Long articleMainId, Integer channelId, Date published) {
        ArticleMain articleMain = articleMainDAO.findArticleMainByArticleMainAndChannel(articleMainId, channelId);
        Assert.notNull(articleMain);

        if (isNotNull(published)) {
            article.setPublished(published);
        }

        //      if (article.getStatus() == Status.RELEASE || article.getStatus() == Status.PRERELEASE || article.getStatus() == Status.REVIEW) {
        //         // throw new BaseException("error.document.article.notupdate","????");
        //      } else {
        Article article_old = articleMain.getArticle();
        Assert.notNull(article_old);
        if (article.getType() == Type.GENERAL) {
            article.setUrl(null);
            keywordAndSummary(article);
        } else if (article.getType() == Type.TITLE) {
            article.setKeyword("");
            article.setSummary("");
            if (article_old.getContents() != null && !article_old.getContents().isEmpty()) {
                article.setContents(article_old.getContents());
            } else {
                titleArticleContentNull(article);
            }
        }

        Date modNow = new Date(Calendar.getInstance().getTime().getTime());

        article.setOwner(article_old.getOwner());
        article.setCreateTime(article_old.getCreateTime());
        article.setModified(modNow);
        article.setStatus(Article.Status.REEDIT);

        article.setRelations(article_old.getRelations());

        articleMain.setArticle(article);
        articleMainDAO.merge(articleMain);

        operateTrackService.addOperateTrack(articleMainId, article_old.getStatusDescription(), "", "");
        //      }
        return articleMain.getId();
    }

    private void keywordAndSummary(Article article) {
        List<Content> contents = article.getContents();
        String title = article.getTitle();
        if (contents != null && !contents.isEmpty() && title != null && title.length() > 0) {
            String contentAll = "";
            for (Content content : contents) {
                contentAll += content.getDetail();
            }
            if (article.getKeyword() == null || article.getKeyword().length() == 0) {
                String keyword = HtmlStringUtil.join(ExtractKeywordAndSummary.getKeyword(title + " " + contentAll),
                        " ");
                article.setKeyword(keyword);
            }
            if (article.getSummary() == null || article.getSummary().length() == 0) {
                String summary = ExtractKeywordAndSummary.getTextAbstract(title, contentAll);
                article.setSummary(summary);
            }
        }
    }

    private void titleArticleContentNull(Article article) {
        Content content = new Content();
        content.setDetail(null);
        content.setPage(1);
        List<Content> contents = new ArrayList<Content>();
        contents.add(content);
        article.setContents(contents);
    }

    @Override
    public Map<Channel, Long> findBeApprovalArticleMain(String userName) {
        Map<Channel, Long> result = new HashMap<Channel, Long>();

        List<String> groupNames = new ArrayList<String>();
        UserDetails user = userService.loadUserByUsername(userName);
        if (user != null) {
            Collection<GrantedAuthority> authorites = user.getAuthorities();
            for (GrantedAuthority auth : authorites) {
                if (StringUtils.startsWith(auth.getAuthority(), GroupServiceable.GROUP_NAME_PERFIX)) {
                    groupNames.add(auth.getAuthority());
                }
            }
        }

        Map<Integer, Long> map = articleMainDAO.findBeApprovalArticleMain(userName, groupNames);
        if (!map.isEmpty()) {
            Set<Integer> keySets = map.keySet();
            for (Integer key : keySets) {
                Channel channel = channelDAO.get(key);
                Long count = map.get(key);
                result.put(channel, count);
            }
        }
        return result;
    }

    @Override
    public Map<Integer, Long> findCreateArticleFcfChart(Integer year, Integer siteId) {
        return articleMainDAO.findCreateArticleFcfChart(year, siteId);
    }

    @Override
    public Map<Integer, Long> findReleaseArticleFcfChart(Integer year, Integer siteId) {
        return articleMainDAO.findReleaseArticleFcfChart(year, siteId);
    }

    @Override
    public Map<String, Long> findReleaseArticlePersonFcfChart(Integer year, Integer siteId) {
        return articleMainDAO.findReleaseArticlePersonFcfChart(year, siteId);
    }

    @Override
    public ArticleMain findArticleMainById(Long articleMainId) {
        return articleMainDAO.get(articleMainId);
    }

    @Override
    public void referArticleMain(Integer channelId, Long[] articleMainIds) {
        Assert.notNull(channelId);
        Assert.notEmpty(articleMainIds);

        Channel channel = channelDAO.get(channelId);
        Channel parentChannel = channel.getParent();
        if (parentChannel.getType() == Channel.Type.LEADER) {
            for (Long articleMainId : articleMainIds) {
                ArticleMain articleMain = articleMainDAO.get(articleMainId);
                if (articleMain == null)
                    continue;
                ArticleMain refArticleMain = new ArticleMain();
                refArticleMain.setArticle(articleMain.getArticle());
                refArticleMain.setChannelId(channelId);
                refArticleMain.setReference(true);
                refArticleMain.setSort(articleMain.getSort());
                refArticleMain.setTop(articleMain.getTop());

                articleMainDAO.persist(refArticleMain);
            }
        }
    }

    @Override
    public void removeArticleMain(Long[] articleMainIds) {
        Assert.notNull(articleMainIds);
        Assert.notEmpty(articleMainIds);
        for (Long articleMainId : articleMainIds) {
            ArticleMain articleMain = articleMainDAO.get(articleMainId);
            Assert.notNull(articleMain);
            if (articleMain.getReference()) {
                articleMain.setArticle(null);
                articleMainDAO.merge(articleMain);
                articleMainDAO.remove(articleMain);
            }
        }
    }

    @Override
    public void shareArticleMain(List<Long> articleMainIds, Boolean share) {
        Assert.notNull(articleMainIds);
        for (Long articleMainId : articleMainIds) {
            ArticleMain articleMain = articleMainDAO.get(articleMainId);
            if (articleMain == null)
                continue;
            if (isNotNull(articleMain.getReference()) && articleMain.getReference())
                continue;
            if (articleMain.getShare() != share) {
                articleMain.setShare(share);
                articleMainDAO.merge(articleMain);

                String desc = "";
                if (!share)
                    desc = "?";
                operateTrackService.addOperateTrack(articleMainId, articleMain.getArticle().getStatusDescription(),
                        desc, "");
            }
        }
    }

}