📄 forumserviceimpl.java
字号:
/*
* Created on 2007-2-16
* Last modified on 2007-8-22
* Powered by YeQiangWei.com
*/
package com.yeqiangwei.club.service.forum;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.apache.log4j.Logger;
import com.yeqiangwei.club.cache.Cache;
import com.yeqiangwei.club.cache.CacheRegion;
import com.yeqiangwei.club.cache.singleton.CacheFactory;
import com.yeqiangwei.club.cache.CacheKeys;
import com.yeqiangwei.club.dao.DAOLocator;
import com.yeqiangwei.club.dao.DAOWrapper;
import com.yeqiangwei.club.dao.ForumDAO;
import com.yeqiangwei.club.dao.model.Forum;
import com.yeqiangwei.club.exception.ClubException;
import com.yeqiangwei.club.exception.DAOException;
import com.yeqiangwei.club.param.CounterParameter;
import com.yeqiangwei.club.param.ForumParameter;
import com.yeqiangwei.club.service.ServiceLocator;
import com.yeqiangwei.club.service.ServiceWrapper;
import com.yeqiangwei.club.service.counter.CountService;
import com.yeqiangwei.club.service.model.CountersModel;
import com.yeqiangwei.club.service.model.ForumModel;
import com.yeqiangwei.club.service.model.ModelsOfForum;
import com.yeqiangwei.club.util.BeanLocator;
import com.yeqiangwei.club.util.BeanUtils;
import com.yeqiangwei.club.util.MessageUtils;
import com.yeqiangwei.util.FormatDateTime;
import com.yeqiangwei.util.Validator;
import com.yeqiangwei.util.TypeChange;
public class ForumServiceImpl extends MessageUtils implements ForumService {
private static final Logger logger = Logger.getLogger(ForumService.class);
private Cache forumCache = CacheFactory.creator(CacheRegion.FORUM);
private static final String CACHE_TOPS = "TopForums";
private static final String CACHE_TIME = "TopYmd";
private static final int CACHE_TIMEOUT = 300; //缓存超时後更新到数据库(秒数)
/* test */
public static void main(String args[]){
com.yeqiangwei.club.dao.hibernate.ConnectionManager.init();
ForumParameter param = new ForumParameter();
ForumServiceImpl f = new ForumServiceImpl();
List<ForumModel> list = f.findByParameter(param);
for(int i=0; i<list.size(); i++){
ForumModel model = list.get(i);
System.out.println(model.getForumName());
}
}
@SuppressWarnings("unchecked")
public List<ForumModel> findTops(int rows) {
List<ForumModel> mlist = null;
//int date = TypeChange.stringToInt(FormatDateTime.dateAdd(FormatDateTime.DAY_OF_MONTH,-1,"yyyyMMdd"));
long time = TypeChange.stringToLong(FormatDateTime.dateAdd(FormatDateTime.SECOND,CACHE_TIMEOUT,"yyyyMMddHHmmss"));
Long cacheTime = (Long) forumCache.get(CACHE_TIME);
Long nowTime = new Long(FormatDateTime.now());
if(cacheTime!=null && cacheTime>nowTime){
mlist = (List<ForumModel>) forumCache.get(CACHE_TOPS);
if(!Validator.isEmpty(mlist)){
logger.debug("findTops from cache");
return mlist;
}
}
logger.debug("findTops from db");
mlist = new ArrayList<ForumModel>();
ForumParameter param = new ForumParameter();
param.setType((byte)1);
List<Forum> list = this.getForumDAO().findByParameter(param);
if(Validator.isEmpty(list))return null;
int ymd = FormatDateTime.formatDateTimeToInt("yyyyMMdd");
int size = list.size();
if(rows>size){
rows = size;
}
for(int i=0; i<rows; i++){
Forum item = list.get(i);
ForumModel model = new ForumModel();
BeanUtils.copyProperties(model,item);
CounterParameter cparam = new CounterParameter();
cparam.setYmd(ymd);
cparam.setForumId(model.getForumId());
CountersModel counters = this.getCountService().findOnlyByParameter(cparam);
if(!Validator.isEmpty(counters)){
model.setSort(counters.getBoys()
+counters.getGirls()
+counters.getOthers()
+counters.getHits());
}else{
model.setSort(0);
}
mlist.add(model);
}
Collections.sort(mlist, new ForumComparator());
forumCache.put(CACHE_TOPS,mlist);
forumCache.put(CACHE_TIME,new Long(time));
return mlist;
}
public ForumModel findById(int id) {
if(id>0){
ForumModel model = (ForumModel) forumCache.get(CacheKeys.getForumKey(id));
if(Validator.isEmpty(model)){
Forum item = this.getForumDAO().findById(id);
if(!Validator.isEmpty(item)){
model = new ForumModel();
BeanUtils.copyProperties(model,item);
forumCache.put(CacheKeys.getKey(model),model);
//logger.debug("get Forum from DB "+model.getForumId()+" / "+model.getForumName());
}
}else{
//logger.debug("get Forum from Cache "+model.getForumId()+" / "+model.getForumName());
}
return model;
}else{
logger.debug("Forum findById id<1");
return null;
}
}
public void createOrUpdate(ForumModel model) throws ClubException {
if(model.getForumId()>0){
this.update(model);
}else{
this.create(model);
}
}
public void create(ForumModel model) throws ClubException {
this.setForumLayer(model); //设置上层对象
Forum item = new Forum();
BeanUtils.copyProperties(item,model); //BO->PO
try {
getForumDAO().create(item);
} catch (DAOException e) {
throw new ClubException(e.getMessage());
}
if(item.getForumId()==0){
throw new ClubException(MessageUtils.getMessage("error_system"));
}
forumCache.put(CacheKeys.getKey(model),model);
}
public void update(ForumModel model) throws ClubException {
this.setForumLayer(model); //设置上层对象
Forum item = this.getForumDAO().findById(model.getForumId());
if(Validator.isEmpty(item)){
throw new ClubException(MessageUtils.getMessage("error_system"));
}
BeanUtils.copyProperties(item,model); //BO->PO
try {
this.getForumDAO().update(item);
} catch (DAOException e) {
throw new ClubException(e.getMessage());
}
forumCache.put(CacheKeys.getKey(model),model);
}
/**
* 删除版面同时删除帖子以及更新用户发贴数
* @param item
* @return
* @throws ClubException
*/
public int delete(ForumModel model) throws ClubException {
int c = 0;
if(Validator.isEmpty(model)){
throw new ClubException(MessageUtils.getMessage("error_system"));
}
else if(model.getForumId()<1){
throw new ClubException(MessageUtils.getMessage("error_update_noid"));
}
else{
Forum item = new Forum();
BeanUtils.copyProperties(item,model); //BO->PO
try {
c = getForumDAO().delete(item);
forumCache.remove(CacheKeys.getKey(model));
} catch (DAOException e) {
throw new ClubException(e.getMessage());
}
}
return c;
}
public int delete(String[] ids) throws ClubException {
int c = 0;
if(!Validator.isEmpty(ids)){
for(int i=0; i<ids.length; i++){
int id = TypeChange.stringToInt(ids[i]);
ForumModel model = new ForumModel();
model.setForumId(id);
try {
c+=this.delete(model);
} catch (ClubException e) {
logger.error(e.toString());
}
}
}else{
throw new ClubException(MessageUtils.getMessage("error_update_noid"));
}
return c;
}
public List<ForumModel> findByParameter(ForumParameter param) {
List<Forum> list = this.getForumDAO().findByParameter(param);
List<ForumModel> flist = BeanUtils.<Forum,ForumModel>copyList(list,BeanLocator.FORUMMODEL);
return flist;
}
public long countByParameter(ForumParameter param) {
return getForumDAO().countByParameter(param);
}
private ForumModel setForumLayer(ForumModel model){
if(model.getForumIdd()>0){
ForumModel f = this.findById(model.getForumIdd());
if(Validator.isEmpty(f)){
model.setLayer(0);
logger.debug("找不到Forum上层对象,forumId:"+model.getForumId());
}else{
model.setLayer(f.getLayer()+1);
}
}else{
model.setLayer(0);
}
return model;
}
@SuppressWarnings("unchecked")
/**
* 根据版面ID获取版面以及上级分类信息
* 获取版面管理员信息
* 最后形成多个List集合
*/
public ModelsOfForum getModelsOfForum(int forumId) {
ModelsOfForum models = null;
List<ForumModel> list = new ArrayList<ForumModel>();
if(forumId>0){
models = new ModelsOfForum();
ForumModel model = this.findById(forumId); //取当前Forum
if(Validator.isEmpty(model)){
return null;
}else{
models.setForumModel(model);
models.setForumId(forumId);
if(!Validator.isEmpty(model)){
models.setForumModel(model);
list.add(model);
this.ForumModels(model,list);
}
Collections.sort(list, new ForumComparator()); //按版面分类层次从高到底排序
models.setForumModels(list);
}
}
return models;
}
/**
* 辅助 getForumModel方法 获取版面上级分类信息形成List集合
* @param item
* @param list
*/
private void ForumModels(ForumModel model, List<ForumModel> list){
if(model.getForumIdd()>0){
ForumModel forum = this.findById(model.getForumIdd());
if(!Validator.isEmpty(forum)){
list.add(forum);
if(forum.getForumIdd()>0){
this.ForumModels(forum,list);
}
}
}
}
public ForumDAO getForumDAO() {
return DAOWrapper.getSingletonInstance(DAOLocator.FORUM);
}
private CountService getCountService() {
return ServiceWrapper.<CountService>getSingletonInstance(ServiceLocator.COUNT);
}
public void cacheClear() {
forumCache.clear();
}
/* test
public static void main(String args[]){
com.yeqiangwei.club.dao.hibernate.ConnectionManager.init();
ForumServiceImpl f = new ForumServiceImpl();
ModelsOfForum m;
m = f.getModelsOfForum(7);
if(m!=null){
//System.out.print(m.getForum().getForumName());
}
List<ForumModel> list = m.getForumModels();
for(int i=0; i<list.size(); i++){
ForumModel ff = list.get(i);
System.out.print(ff.getForumName());
System.out.print(" ");
}
System.out.println();
}
*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -