package org.yeeku.service.impl;
import org.yeeku.dao.*;
import org.yeeku.model.*;
import org.yeeku.exception.*;
import org.yeeku.service.*;
import org.yeeku.vo.*;
import java.util.List;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
/**
* @author yeeku.H.lee kongyeeku@163.com
* @version 1.0
* <br>Copyright (C), 2005-2008, yeeku.H.Lee
* <br>This program is protected by copyright laws.
* <br>Program Name:
* <br>Date:
*/
public class MessageManagerImpl implements MessageManager
{
private UserDao userDao;
private MessageDao messDao;
public void setUserDao(UserDao userDao)
{
this.userDao = userDao;
}
public void setMessDao(MessageDao messDao)
{
this.messDao = messDao;
}
/**
* 创建一条消息
* @param title 新信息的标题
* @param content 新消息的父节点
* @param userId 创建消息的用户Id
* @return 新创建消息的主键,如果创建失败,返回-1。
*/
public int createMessage(String title , String content , int userId)
throws MessageException
{
try
{
User u = userDao.get(userId);
if ( u != null)
{
Message m = new Message();
m.setTitle(title);
m.setContent(content);
m.setUser(u);
return messDao.save(m);
}
return -1;
}
catch (Exception e)
{
e.printStackTrace();
throw new MessageException("添加消息出现异常");
}
}
/**
* 创建一个用户
* @param user 新创建用户的用户名
* @param pass 新创建用户的密码
* @return 新创建用户的主键
*/
public int createUser(String user , String pass , HttpServletRequest request)
throws MessageException
{
if (userDao.findByName(user) != null)
throw new MessageException("该用户名已经存在");
try
{
User u = new User();
u.setName(user);
u.setPass(pass);
userDao.save(u);
request.getSession(true).setAttribute("user" , u.getId());
return u.getId();
}
catch (Exception e)
{
e.printStackTrace();
throw new MessageException("注册用户出现异常");
}
}
/**
* 验证用户名是否可用,本系统不允许用户名重复
* @param user 需要验证的用户名
* @return 该用户名不重复,可用,返回true,否则返回false
*/
public boolean valid(String user)
throws MessageException
{
try
{
if (userDao.findByName(user) == null)
return true;
}
catch (Exception e)
{
e.printStackTrace();
throw new MessageException("验证用户名出现异常");
}
return false;
}
/**
* 验证用户登陆是否成功
* @param user 登陆所用的用户名
* @param pass 登陆所用的密码
* @param session 登陆后需将用户名存入session
* @return 登陆成功,返回true;否则返回false
*/
public boolean login(String user , String pass , HttpServletRequest request)
throws MessageException
{
try
{
User u = userDao.findByName(user);
if (u != null && u.getPass().equals(pass))
{
request.getSession(true).setAttribute("user" , u.getId());
return true;
}
}
catch (Exception e)
{
e.printStackTrace();
throw new MessageException("处理登陆出现异常");
}
return false;
}
/**
* 当前页面的浏览者是否登陆
* @param session 判断用户是否登陆是通过跟踪session
* @return 已经登陆返回当前用户ID,否则返回-1
*/
public int isLogin(HttpServletRequest request)
throws MessageException
{
try
{
Object userId = request.getSession(true).getAttribute("user");
if (userId != null)
{
return (Integer)userId;
}
}
catch (Exception e)
{
e.printStackTrace();
throw new MessageException("验证用户是否登陆出现异常");
}
return -1;
}
/**
* 根据消息ID返回消息
* @param id 消息ID
* @return 指定ID对应的消息
*/
public MessageBean getMessage(int id)
throws MessageException
{
try
{
Message m = messDao.get(id);
if (m != null)
{
return new MessageBean(0 , m.getTitle() , m.getContent() , m.getUser().getId() , m.getUser().getName());
}
}
catch (Exception e)
{
e.printStackTrace();
throw new MessageException("获取消息内容出现异常");
}
return null;
}
/**
* 返回特定页面所有消息
* @param pageNo 指定页码
* @return 指定页的全部消息
*/
public List<MessageBean> getAllMessageByPage(int pageNo)
throws MessageException
{
try
{
List ml = messDao.findAllByPage(pageNo);
if (ml != null && ml.size() > 0)
{
List<MessageBean> result = new ArrayList<MessageBean>();
for (Object obj : ml)
{
Message me = (Message)obj;
result.add(new MessageBean(me.getId() , me.getTitle(), null , 0 , me.getUser().getName()));
}
return result;
}
}
catch (Exception e)
{
e.printStackTrace();
throw new MessageException("获取消息内容出现异常");
}
return null;
}
}