e983806b6a8dfd0abd88d0cbeea0d5f792692143.svn-base
5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
* Copyright © 2015-2018 ODM All rights reserved.
*/
package com.thinkgem.jeesite.modules.cms.service;
import java.util.List;
import java.util.Set;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.TreeService;
import com.thinkgem.jeesite.modules.cms.dao.CategoryDao;
import com.thinkgem.jeesite.modules.cms.entity.Category;
import com.thinkgem.jeesite.modules.cms.entity.Site;
import com.thinkgem.jeesite.modules.cms.utils.CmsUtils;
import com.thinkgem.jeesite.modules.sys.entity.Office;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
/**
* 栏目Service
* @author ThinkGem
* @version 2013-5-31
*/
@Service
@Transactional(readOnly = true)
public class CategoryService extends TreeService<CategoryDao, Category> {
public static final String CACHE_CATEGORY_LIST = "categoryList";
private Category entity = new Category();
@SuppressWarnings("unchecked")
public List<Category> findByUser(boolean isCurrentSite, String module){
List<Category> list = (List<Category>)UserUtils.getCache(CACHE_CATEGORY_LIST);
if (list == null){
User user = UserUtils.getUser();
Category category = new Category();
category.setOffice(new Office());
category.getSqlMap().put("dsf", dataScopeFilter(user, "o", "u"));
category.setSite(new Site());
category.setParent(new Category());
list = dao.findList(category);
// 将没有父节点的节点,找到父节点
Set<String> parentIdSet = Sets.newHashSet();
for (Category e : list){
if (e.getParent()!=null && StringUtils.isNotBlank(e.getParent().getId())){
boolean isExistParent = false;
for (Category e2 : list){
if (e.getParent().getId().equals(e2.getId())){
isExistParent = true;
break;
}
}
if (!isExistParent){
parentIdSet.add(e.getParent().getId());
}
}
}
if (parentIdSet.size() > 0){
//FIXME 暂且注释,用于测试
// dc = dao.createDetachedCriteria();
// dc.add(Restrictions.in("id", parentIdSet));
// dc.add(Restrictions.eq("delFlag", Category.DEL_FLAG_NORMAL));
// dc.addOrder(Order.asc("site.id")).addOrder(Order.asc("sort"));
// list.addAll(0, dao.find(dc));
}
UserUtils.putCache(CACHE_CATEGORY_LIST, list);
}
if (isCurrentSite){
List<Category> categoryList = Lists.newArrayList();
for (Category e : list){
if (Category.isRoot(e.getId()) || (e.getSite()!=null && e.getSite().getId() !=null
&& e.getSite().getId().equals(Site.getCurrentSiteId()))){
if (StringUtils.isNotEmpty(module)){
if (module.equals(e.getModule()) || "".equals(e.getModule())){
categoryList.add(e);
}
}else{
categoryList.add(e);
}
}
}
return categoryList;
}
return list;
}
public List<Category> findByParentId(String parentId, String siteId){
Category parent = new Category();
parent.setId(parentId);
entity.setParent(parent);
Site site = new Site();
site.setId(siteId);
entity.setSite(site);
return dao.findByParentIdAndSiteId(entity);
}
public Page<Category> find(Page<Category> page, Category category) {
// DetachedCriteria dc = dao.createDetachedCriteria();
// if (category.getSite()!=null && StringUtils.isNotBlank(category.getSite().getId())){
// dc.createAlias("site", "site");
// dc.add(Restrictions.eq("site.id", category.getSite().getId()));
// }
// if (category.getParent()!=null && StringUtils.isNotBlank(category.getParent().getId())){
// dc.createAlias("parent", "parent");
// dc.add(Restrictions.eq("parent.id", category.getParent().getId()));
// }
// if (StringUtils.isNotBlank(category.getInMenu()) && Category.SHOW.equals(category.getInMenu())){
// dc.add(Restrictions.eq("inMenu", category.getInMenu()));
// }
// dc.add(Restrictions.eq(Category.FIELD_DEL_FLAG, Category.DEL_FLAG_NORMAL));
// dc.addOrder(Order.asc("site.id")).addOrder(Order.asc("sort"));
// return dao.find(page, dc);
// page.setSpringPage(dao.findByParentId(category.getParent().getId(), page.getSpringPage()));
// return page;
category.setPage(page);
category.setInMenu(Global.SHOW);
page.setList(dao.findModule(category));
return page;
}
@Transactional(readOnly = false)
public void save(Category category) {
category.setSite(new Site(Site.getCurrentSiteId()));
if (StringUtils.isNotBlank(category.getViewConfig())){
category.setViewConfig(StringEscapeUtils.unescapeHtml4(category.getViewConfig()));
}
super.save(category);
UserUtils.removeCache(CACHE_CATEGORY_LIST);
CmsUtils.removeCache("mainNavList_"+category.getSite().getId());
}
@Transactional(readOnly = false)
public void delete(Category category) {
super.delete(category);
UserUtils.removeCache(CACHE_CATEGORY_LIST);
CmsUtils.removeCache("mainNavList_"+category.getSite().getId());
}
/**
* 通过编号获取栏目列表
*/
public List<Category> findByIds(String ids) {
List<Category> list = Lists.newArrayList();
String[] idss = StringUtils.split(ids,",");
if (idss.length>0){
// List<Category> l = dao.findByIdIn(idss);
// for (String id : idss){
// for (Category e : l){
// if (e.getId().equals(id)){
// list.add(e);
// break;
// }
// }
// }
for(String id : idss){
Category e = dao.get(id);
if(null != e){
//System.out.println("e.id:"+e.getId()+",e.name:"+e.getName());
list.add(e);
}
//list.add(dao.get(id));
}
}
return list;
}
}