📄 friendserviceimpl.java
字号:
/*
* Created on 2007-4-28
* Last modified on 2007-8-22
* Powered by YeQiangWei.com
*/
package com.yeqiangwei.club.service.user;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import com.yeqiangwei.club.cache.Cache;
import com.yeqiangwei.club.cache.CacheFactory;
import com.yeqiangwei.club.cache.CacheRegion;
import com.yeqiangwei.club.dao.DAOLocator;
import com.yeqiangwei.club.dao.DAOWrapper;
import com.yeqiangwei.club.dao.FriendDAO;
import com.yeqiangwei.club.dao.model.Friend;
import com.yeqiangwei.club.dao.model.User;
import com.yeqiangwei.club.exception.ClubException;
import com.yeqiangwei.club.param.FriendParameter;
import com.yeqiangwei.club.param.TopicParameter;
import com.yeqiangwei.club.service.ServiceLocator;
import com.yeqiangwei.club.service.ServiceWrapper;
import com.yeqiangwei.club.service.model.FriendLastPost;
import com.yeqiangwei.club.service.model.FriendModel;
import com.yeqiangwei.club.service.model.ReplyModel;
import com.yeqiangwei.club.service.model.TopicModel;
import com.yeqiangwei.club.service.model.UserModel;
import com.yeqiangwei.club.service.topic.ReplyComparator;
import com.yeqiangwei.club.service.topic.ReplyService;
import com.yeqiangwei.club.service.topic.TopicComparator;
import com.yeqiangwei.club.service.topic.TopicService;
import com.yeqiangwei.club.service.user.UserService;
import com.yeqiangwei.club.util.BeanUtils;
import com.yeqiangwei.club.util.MessageUtils;
import com.yeqiangwei.util.StringHelper;
import com.yeqiangwei.util.Validator;
/**
* 本类下只根据myUserId cache 好友列表
* @author yeqiangwei
*/
public class FriendServiceImpl implements FriendService {
private Cache cache = CacheFactory.creator(CacheRegion.FRIEND);
public FriendServiceImpl(){
}
public FriendModel findByMyUserIdAndFriendUserId(int myUserId, int friendUserId) {
FriendModel model = null;
Friend item = this.getFriendDAO().findByMyUserIdAndFriendUserId(myUserId, friendUserId);
if(!Validator.isEmpty(item)){
model = new FriendModel();
BeanUtils.copyProperties(model,item);
}
return model;
}
public FriendModel findById(int id) {
FriendModel model = null;
FriendParameter param = new FriendParameter();
param.setPage(new Integer(1));
param.setRows(new Integer(1));
List list = (List) this.getFriendDAO().find_friendJoinUser(param);
for(int i=0; i<list.size(); i++){
Object[] obj = (Object[]) list.get(i);
Friend item = (Friend) obj[0];
UserModel user = (UserModel) obj[1];
if(!Validator.isEmpty(item)&&!Validator.isEmpty(user)){
model = new FriendModel();
BeanUtils.copyProperties(model,item);
model.setUserModel(user);
cache.put(cacheKey(model,false),model);
}
}
return model;
}
public FriendModel createOrUpdate(FriendModel model) throws ClubException {
if(!Validator.isEmpty(model)&&model.getFriendId()>0){
return this.update(model);
}else{
return this.create(model);
}
}
public FriendModel create(FriendModel model) throws ClubException {
if(model.getMyUserId()==0||model.getFriendUserId()==0){
throw new ClubException(MessageUtils.getMessage("error_notfind_user"));
}
FriendParameter param = new FriendParameter();
param.setMyUserId(model.getMyUserId());
if(this.countByParameter(param)>=50){
//this.setMessage(MessageUtils.getMessage("error_friend_toomany"));
throw new ClubException(MessageUtils.getMessage("error_friend_toomany"));
}
UserModel user = this.getUserService().findById(model.getFriendUserId());
if(Validator.isEmpty(user)){
//this.setMessage(MessageUtils.getMessage("error_notfind_user"));
throw new ClubException(MessageUtils.getMessage("error_notfind_user"));
}else if(!Validator.isEmpty(
this.getFriendDAO().findByMyUserIdAndFriendUserId(model.getMyUserId(),model.getFriendUserId()))
)
{
//this.setMessage(MessageUtils.getMessage("error_friend_duplicate"));
throw new ClubException(MessageUtils.getMessage("error_friend_duplicate"));
}
Friend item = new Friend();
BeanUtils.copyProperties(item,model);
this.getFriendDAO().create(item);
model.setFriendId(item.getFriendId());
cache.put(cacheKey(model,false),model);
return model;
}
/**
*
* @param model
* @return
*/
public FriendModel update(FriendModel model) {
Friend item = this.getFriendDAO().findById(model.getFriendId());
BeanUtils.copyProperties(item,model);
this.getFriendDAO().create(item);
return model;
}
public int delete(FriendModel model) throws ClubException {
int c = 0;
if(model.getFriendId()>0){
Friend item = new Friend();
BeanUtils.copyProperties(item,model);
c = this.getFriendDAO().delete(item);
if(c>0){
cache.remove(cacheKey(model,false));
cache.remove(cacheKey(model,true));
//this.setMessage(MessageUtils.getMessage("success"));
}else{
//this.setMessage(MessageUtils.getMessage("error_system"));
throw new ClubException(MessageUtils.getMessage("error_system"));
}
}else{
throw new ClubException(MessageUtils.getMessage("error_parameter"));
}
return c;
}
public int delete(String ids) throws ClubException {
List<Integer> list = StringHelper.stringToIntegerList(ids,"|");
if(!Validator.isEmpty(list)){
return this.getFriendDAO().delete(list);
}else{
throw new ClubException(MessageUtils.getMessage("error_parameter"));
}
}
/* test
public static void main(String args[]){
com.yeqiangwei.club.dao.hibernate.ConnectionManager.init();
FriendServiceImpl f = new FriendServiceImpl();
FriendParameter param = new FriendParameter();
param.setMyUserId(95);
Object obj = f.findByParameter(param);
System.out.println(obj);
List list = (List) obj;
System.out.println(list);
com.yeqiangwei.club.dao.hibernate.ConnectionManager.closeSession();
}
*/
public List<FriendModel> findByParameter(FriendParameter param) {
List list = (List) this.getFriendDAO().find_friendJoinUser(param);
List<FriendModel> mlist = new ArrayList<FriendModel>();
if(!Validator.isEmpty(list)){
for(int i=0; i<list.size(); i++){
Object[] obj = (Object[]) list.get(i);
Friend item = (Friend) obj[0];
User user = (User) obj[1];
if(!Validator.isEmpty(item)&&!Validator.isEmpty(user)){
FriendModel model = new FriendModel();
BeanUtils.copyProperties(model,item); //PO->BO
UserModel userModel = new UserModel();
BeanUtils.copyProperties(userModel,user);
model.setUserModel(userModel);
mlist.add(model);
cache.put(cacheKey(model,false),model);
}
}
}
return mlist;
}
public long countByParameter(FriendParameter param) {
return this.getFriendDAO().countByParameter(param);
}
/**
* 限制了List上限
*/
@SuppressWarnings("unchecked")
private List<TopicModel> findTopics(List<Friend> flist, int num, int rows){
if(Validator.isEmpty(flist)){
return null;
}
List<TopicModel> alllist = new ArrayList<TopicModel>();
for(int i=0; i<flist.size(); i++){
Friend f = flist.get(i);
TopicParameter param = new TopicParameter();
param.setPage(new Integer(1));
param.setRows(3);
param.setUserId(new Integer(f.getFriendUserId()));
param.setOrderBy(new Byte((byte)2)); //order by topicId
List<TopicModel> list = this.getTopicService().findByParameter(param);
if(!Validator.isEmpty(list)){
for(int i2=0; i2<list.size(); i2++){
alllist.add(list.get(i2));
}
}
}
if(!Validator.isEmpty(alllist)&&alllist.size()>0){
Collections.sort(alllist,new TopicComparator());
}
List<TopicModel> rlist = new ArrayList<TopicModel>();
for(int i=0; i<alllist.size(); i++){
rlist.add(alllist.get(i));
if(i==rows-1){
break;
}
}
return rlist;
}
@SuppressWarnings("unchecked")
private List<ReplyModel> findReplys(List<Friend> flist, int num, int rows){
if(Validator.isEmpty(flist)){
return null;
}
List<ReplyModel> alllist = new ArrayList<ReplyModel>();
for(int i=0; i<flist.size(); i++){
Friend f = flist.get(i);
TopicParameter param = new TopicParameter();
param.setPage(new Integer(1));
param.setRows(3);
param.setUserId(new Integer(f.getFriendUserId()));
param.setOrderBy(new Byte((byte)2)); //order by topicId
List<ReplyModel> list = this.getReplyService().findByParameter(param);
//System.out.println(f.getFriendUserId());
if(!Validator.isEmpty(list)){
for(int i2=0; i2<list.size(); i2++){
alllist.add(list.get(i2));
}
}
}
if(!Validator.isEmpty(alllist)&&alllist.size()>0){
Collections.sort(alllist,new ReplyComparator());
}
List<ReplyModel> rlist = new ArrayList<ReplyModel>();
for(int i=0; i<alllist.size(); i++){
rlist.add(alllist.get(i));
if(i==rows-1){
break;
}
}
return rlist;
}
/* test */
public static void main(String args[]){
com.yeqiangwei.club.dao.hibernate.ConnectionManager.init();
FriendServiceImpl f = new FriendServiceImpl();
List<FriendLastPost> list = f.findLastPost(95, 3, 5, false);
//System.out.println(list);
if(list!=null){
for(int i=0; i<list.size(); i++){
FriendLastPost post = list.get(i);
/*System.out.println(post.getTopicModel().getCreateDateTime()
+" "+post.getTopicModel().getTitle());*/
System.out.println(post.getReplyModel().getReplyId()+"//"+post.getReplyModel().getTitle());
}
}
com.yeqiangwei.club.dao.hibernate.ConnectionManager.closeSession();
}
public List<FriendLastPost> findLastPost(int myUserId, int num, int rows, boolean isTopic) {
List<FriendLastPost> list = new ArrayList<FriendLastPost>();
FriendParameter param = new FriendParameter();
param.setMyUserId(myUserId);
param.setPage(new Integer(1));
param.setRows(new Integer(50));//总共取前50位好友的信息
param.setOrderBy((byte)3);//按关系值排列
List<Friend> flist = this.getFriendDAO().findByParameter(param);
if(isTopic){
List<TopicModel> tlist = this.findTopics(flist, num, rows);
if(tlist!=null){
for(int i=0; i<tlist.size(); i++){
FriendLastPost p = new FriendLastPost();
p.setTopicModel(tlist.get(i));
list.add(p);
}
}
}
else{
List<ReplyModel> rlist = this.findReplys(flist, num, rows);
if(rlist!=null){
for(int i=0; i<rlist.size(); i++){
FriendLastPost p = new FriendLastPost();
p.setReplyModel(rlist.get(i));
list.add(p);
}
}
}
return list;
}
/**
*
* @param model
* @param isresults true缓存List集合对象 false缓存FriendModel对象
* @return
*/
private String cacheKey(FriendModel model, boolean isresults){
StringBuffer sb = new StringBuffer();
sb.append("Friend-");
if(isresults){
sb.append("myUserId-");
sb.append(model.getMyUserId());
}else{
sb.append("friendId-");
sb.append(model.getFriendId());
}
return sb.toString();
}
public FriendDAO getFriendDAO() {
return DAOWrapper.<FriendDAO>getSingletonInstance(DAOLocator.FRIEND);
}
public UserService getUserService() {
return ServiceWrapper.getSingletonInstance(ServiceLocator.USER);
}
public ReplyService getReplyService() {
return ServiceWrapper.<ReplyService>getSingletonInstance(ServiceLocator.REPLY);
}
public TopicService getTopicService() {
return ServiceWrapper.<TopicService>getSingletonInstance(ServiceLocator.TOPIC);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -