/* ==================================================================
* Created [2009-4-27 下午11:32:55] by Jon.King
* ==================================================================
* TSS
* ==================================================================
* mailTo:jinpujun@hotmail.com
* Copyright (c) Jon.King, 2009-2012
* ==================================================================
*/
package com.jinhe.tss.cms.dao.impl;
import java.util.List;
import javax.persistence.Query;
import com.jinhe.tss.cms.CMSConstants;
import com.jinhe.tss.cms.dao.IChannelDao;
import com.jinhe.tss.cms.entity.Channel;
import com.jinhe.tss.component.support.persistence.TreeSupportDao;
import com.jinhe.tss.core.exception.BusinessException;
import com.jinhe.tss.core.sso.Environment;
import com.jinhe.tss.core.util.EasyUtils;
/**
* Channel的Dao层,负责处理Channel相关的数据库操作
*/
public class ChannelDao extends TreeSupportDao<Channel> implements IChannelDao {
public ChannelDao() {
super(Channel.class);
}
public void moveChannel(Channel channel) {
update(channel);
}
public Channel getSiteByChannel(Long channelId){
Channel channel = (Channel) getEntity(Channel.class, channelId);
if(channel.isSite()) {
return channel;
}
return (Channel) getEntity(Channel.class, channel.getSiteId());
}
@SuppressWarnings("unchecked")
public List<Long> getSiteChannelIDsByOperationId(String operationId){
String hql = "select distinct t.id from Channel t, RoleUserMapping r, ChannelPermissionsFull v"
+ " where v.roleId = r.id.roleId and r.id.userId = ?"
+ " and v.operationId = ? and v.resourceId = t.id order by t.id";
return (List<Long>) getEntities(hql, Environment.getOperatorId(), operationId);
}
public List<?> getParentChannel4CanAdd(){
String hql = "select distinct o from Channel o, Channel t, Temp temp "
+ " where t.id = temp.id and t.decode like o.decode ||'%' order by o.decode";
return getEntities(hql);
}
public List<?> getAllSiteChannelList() {
return getEntities("from Channel c order by c.decode");
}
public List<?> getAllStartedSiteChannelList() {
return getEntities("from Channel c where (c.disabled is null or c.disabled <> 1) order by c.decode");
}
public List<?> getChannelsBySiteIdNoPermission(Long siteId) {
Channel site = getEntity(siteId);
if(site == null){
throw new BusinessException("ID为:" + siteId + " 的站点不存在或者已经被删除");
}
return getEntities("from Channel c where c.isSite = 0 and c.decode like ?", site.getDecode() + "%");
}
public List<?> getChildChannels(Long parentId) {
String hql = " from Channel t where t.parentId = ? and t.isSite = ?";
return getEntities(hql, parentId, CMSConstants.FALSE);
}
public List<?> getChannelSourceByChannelId(Long channelId) {
String hql = "select distinct c from Channel c, ChannelDistribute cd"
+ " where c.id = cd.id.distributeToId and cd.id.distributeFromId = ?" ;
return getEntities(hql, channelId);
}
public List<?> getChannelDestinationByChannelId(Long channelId) {
String hql = "select distinct c from Channel c, ChannelDistribute cd "
+ " where c.id = cd.id.distributeFromId and cd.id.distributeToId = ?";
return getEntities(hql, channelId);
}
public List<?> getDistributeFromByChannelId(Long channelId) {
String hql = " from ChannelDistribute t where t.id.distributeFromId = ? ";
return getEntities(hql, channelId);
}
public List<?> getDistributeToByChannelId(Long channelId) {
String hql = " from ChannelDistribute t where t.id.distributeToId = ? ";
return getEntities(hql, channelId);
}
private Channel getChannelById(Long channelId){
Channel channel = getEntity(channelId);
if(channel == null){
throw new BusinessException("ID为:" + channelId + " 的栏目不存在或者已经被删除");
}
return channel;
}
public List<Channel> getChildrenById(Long channelId, String operationId) {
return getChildrenById(channelId);
}
public List<Channel> getParentsById(Long channelId, String operationId) {
return getParentsById(channelId);
}
public List<?> getChannelTreeUpNoPermission(Long channelId) {
String hql = "from Channel t where t.isSite = ? and ? like t.decode || '%'";
return getEntities(hql, CMSConstants.FALSE, getChannelById(channelId).getDecode());
}
public boolean checkBrowsePermission(Long channelId) {
String hql = "select distinct v from RoleUserMapping r, ChannelPermissionsFull v " +
" where v.id.resourceId= ? and v.id.roleId = r.id.roleId and r.id.userId = ? and v.id.operationId = ?";
List<?> list = getEntities(hql, channelId, Environment.getOperatorId(), CMSConstants.OPERATION_BROWSE);
return list.size() > 0 ;
}
//-------------------------------------------------- 文章发布相关 -----------------------------------
public int getTotalRows4Publish(Long channelId, String category ) {
String decode = getChannelById(channelId).getDecode();
String hql = "select count(*) from Article a, ChannelArticle ca, Channel c"
+ " where ca.articleOrigin <> ? and c.id = ca.id.channelId and ca.id.articleId = a.id "
+ " and c.decode like ? and ";
// 取可发布的文章总数
List<?> list;
if (CMSConstants.PUBLISH_ALL.equals(category)) { //完全发布的话已经发布的也重新发布
hql += " ( a.status = ? or a.status = ? ) ";
list = getEntities(hql, CMSConstants.ARTICLE_DISTRIBUTE, decode + "%", CMSConstants.TOPUBLISH_STATUS, CMSConstants.XML_STATUS);
}
else {
hql += " ( a.status = ? ) ";
list = getEntities(hql, CMSConstants.ARTICLE_DISTRIBUTE, decode + "%", CMSConstants.TOPUBLISH_STATUS);
}
return EasyUtils.convertObject2Integer(list.get(0));
}
public List<?> getPageArticleList4Publish(Long channelId, String category, int pageNum, int pageSize) {
String decode = getChannelById(channelId).getDecode();
String hql = "select a.id, c.id, c.name, at.name, at.publishArticleClassName "
+ " from Article a, ChannelArticle ca, Channel c, ArticleType at"
+ " where a.articleTypeId = at.id and ca.articleOrigin <> ? and c.id = ca.id.channelId and"
+ " ca.id.articleId = a.id and c.decode like ? and ";
Query query;
if (CMSConstants.PUBLISH_ALL.equals(category)) {
hql += " ( a.status = ? or a.status = ? ) ";
query = em.createQuery(hql);
query.setParameter(4, CMSConstants.XML_STATUS);
}
else {
hql += " ( a.status = ? ) ";
query = em.createQuery(hql);
}
query.setParameter(1, CMSConstants.ARTICLE_DISTRIBUTE);
query.setParameter(2, decode + "%");
query.setParameter(3, CMSConstants.TOPUBLISH_STATUS);
query.setFirstResult(pageSize * (pageNum - 1));
query.setMaxResults(pageSize);
return query.getResultList();
}
public Integer getPublishableArticleCount(Long channelId) {
String hql = "select count(*) from Article a, ChannelArticle ca "
+ " where ca.id.articleId = a.id and ca.id.channelId = ? and ca.articleOrigin <> ? and a.status = ?";
List<?> list = getEntities(hql, CMSConstants.ARTICLE_DISTRIBUTE, channelId, CMSConstants.TOPUBLISH_STATUS);
return EasyUtils.convertObject2Integer(list.get(0));
}
public List<?> getPagePublishableArticleList(Long channelId, int currentPageNum, int pageSize) {
String hql = "select a.id, c.id, c.name, at.name, at.publishArticleClassName "
+ " from Article a, ChannelArticle ca, Channel c, ArticleType at "
+ " where a.id = ca.id.articleId and ca.id.channelId = c.id and c.id = ? "
+ " and a.articleTypeId = at.id and ca.articleOrigin <> ? and a.status = ? ";
Query query = em.createQuery(hql);
query.setParameter(1, channelId);
query.setParameter(2, CMSConstants.ARTICLE_DISTRIBUTE);
query.setParameter(3, CMSConstants.TOPUBLISH_STATUS);
query.setFirstResult( pageSize * (currentPageNum - 1) );
query.setMaxResults( pageSize );
return query.getResultList();
}
}