messageproxy.java
来自「社区文章采用的是平板、树形自由选择的两种展示方式」· Java 代码 · 共 329 行
JAVA
329 行
/*
* Created on 2007-5-6
* Last modified on 2007-8-22
* Powered by YeQiangWei.com
*/
package com.yeqiangwei.club.service.message;
import java.util.List;
import org.apache.log4j.Logger;
import com.yeqiangwei.club.exception.ClubException;
import com.yeqiangwei.club.param.MessageParameter;
import com.yeqiangwei.club.service.ServiceLocator;
import com.yeqiangwei.club.service.ServiceWrapper;
import com.yeqiangwei.club.model.MessageBlacklist;
import com.yeqiangwei.club.model.Message;
import com.yeqiangwei.club.model.User;
import com.yeqiangwei.club.model.UserSettings;
import com.yeqiangwei.club.module.doing.exception.RobotException;
import com.yeqiangwei.club.module.doing.model.Robot;
import com.yeqiangwei.club.module.doing.model.UserIM;
import com.yeqiangwei.club.module.doing.param.UserIMParameter;
import com.yeqiangwei.club.module.doing.service.RobotFactory;
import com.yeqiangwei.club.module.doing.service.RobotProvider;
import com.yeqiangwei.club.module.doing.service.RobotService;
import com.yeqiangwei.club.module.doing.service.UserIMService;
import com.yeqiangwei.club.service.user.FriendService;
import com.yeqiangwei.club.service.user.UserService;
import com.yeqiangwei.club.service.user.UserSettingsService;
import com.yeqiangwei.club.service.util.BasicInfoService;
import com.yeqiangwei.club.service.util.NoAllowedCharService;
import com.yeqiangwei.club.util.BeanUtils;
import com.yeqiangwei.club.util.MessageUtils;
import com.yeqiangwei.club.controller.form.MessageForm;
import com.yeqiangwei.util.FormatDateTime;
import com.yeqiangwei.util.StringHelper;
import com.yeqiangwei.util.TypeChange;
import com.yeqiangwei.util.Validator;
public class MessageProxy implements MessageService{
private static final Logger logger = Logger.getLogger(MessageProxy.class);
private static MessageService MESSAGE_SERVICE;
static{
if(MESSAGE_SERVICE==null)MESSAGE_SERVICE = new MessageServiceImpl();
}
public long countNewMessage(int userId) {
return MESSAGE_SERVICE.countNewMessage(userId);
}
public int sends(MessageForm form) throws ClubException {
int c = 0;
int userIdOfSend = form.getUserIdOfSend();
int userIdOfTake = form.getUserIdOfTake();
String userNameOfSend = form.getUserNameOfSend();
String userNameOfTake = form.getUserNameOfTake();
User user = this.getUserService().findById(userIdOfSend);
if(Validator.isEmpty(user)){
//MESSAGE_SERVICE.setMessage(MessageUtils.getMessage("error_notlogin"));
throw new ClubException(MessageUtils.getMessage("error_notlogin"));
}else{
userNameOfSend = user.getUserName();
}
if(userIdOfTake>0){
User takeUser = this.getUserService().findById(form.getUserIdOfTake());
if(Validator.isEmpty(takeUser)){
//MESSAGE_SERVICE.setMessage(MessageUtils.getMessage("error_notfind_user"));
throw new ClubException(MessageUtils.getMessage("error_notfind_user"));
}else{
userNameOfTake = takeUser.getUserName();
}
Message message = new Message();
BeanUtils.copyProperties(message,form);
message.setUserIdOfSend(userIdOfSend);
message.setUserNameOfSend(userNameOfSend);
message.setUserIdOfTake(userIdOfTake);
message.setUserNameOfTake(userNameOfTake);
this.create(message,true);
c = 1;
}
else if(!Validator.isEmpty(userNameOfTake)){
User takeUser = this.getUserService().findByUserName(userNameOfTake);
if(Validator.isEmpty(takeUser)){
//MESSAGE_SERVICE.setMessage(MessageUtils.getMessage("error_notfind_user"));
throw new ClubException(MessageUtils.getMessage("error_notfind_user"));
}else{
userIdOfTake = takeUser.getUserId();
}
Message message = new Message();
BeanUtils.copyProperties(message,form);
message.setUserIdOfSend(userIdOfSend);
message.setUserNameOfSend(userNameOfSend);
message.setUserIdOfTake(userIdOfTake);
message.setUserNameOfTake(userNameOfTake);
this.create(message,true);
c = 1;
}
else if(!Validator.isEmpty(form.getUserNames())){
String userNames = form.getUserNames().replace(",",",");
List<String> list = StringHelper.stringToList(userNames,",");
if(!Validator.isEmpty(list)){
for(int i=0; i<list.size(); i++){
User toUser = this.getUserService().findByUserName(list.get(i));
if(!Validator.isEmpty(toUser)){
Message message = new Message();
BeanUtils.copyProperties(message,form);
message.setUserIdOfSend(userIdOfSend);
message.setUserNameOfSend(userNameOfSend);
message.setUserIdOfTake(toUser.getUserId());
message.setUserNameOfTake(toUser.getUserName());
this.create(message,true);
}
}
}else{
throw new ClubException(MessageUtils.getMessage("error"));
}
}else{
throw new ClubException(MessageUtils.getMessage("error_notfind_user"));
}
return c;
}
public void create(Message model, boolean saveSent) throws ClubException {
this.validator(model);
MESSAGE_SERVICE.create(model,saveSent);
/*********************IM提醒******************/
UserIMParameter imparam = new UserIMParameter();
imparam.setPage(1);
imparam.setRows(10);
imparam.setUserId(model.getUserIdOfTake());
List<UserIM> imlist = this.getUserIMService().findByParameter(imparam);
for(UserIM userim : imlist){
if(userim.getNewMessageHint()==0){
continue;
}
Robot robot = this.getRobotService().findById(userim.getRobotId());
if(!Validator.isEmpty(robot)){
RobotProvider robotProvider = RobotFactory.getInstance(userim.getRobotId());
if(!Validator.isEmpty(robotProvider)){
StringBuffer msg = new StringBuffer();
msg.append(getUserService().findById(model.getUserIdOfSend()).getUserName());
msg.append(" 刚刚给你发了封短消息,赶快去看看吧:\r\n");
msg.append(getBasicInfoService().findOnly().getUrl());
msg.append("/club/?url=message.jsp");
msg.append("\r\n来自:");
msg.append(getBasicInfoService().findOnly().getName());
try {
robotProvider.send(userim, msg.toString());
} catch (RobotException e) {
logger.error(e.toString());
}
}
}else{
this.getUserIMService().delete(userim);
}
}
}
public List<Message> findByParameter(MessageParameter param) {
return MESSAGE_SERVICE.findByParameter(param);
}
public Message findOnlyByParameter(MessageParameter param) {
return MESSAGE_SERVICE.findOnlyByParameter(param);
}
public int update_space(List<Integer> list, byte space) throws ClubException{
return MESSAGE_SERVICE.update_space(list,space);
}
public int update_space(String ids, byte space) throws ClubException{
return MESSAGE_SERVICE.update_space(ids,space);
}
public int update_isOpened(Message model) throws ClubException {
return MESSAGE_SERVICE.update_isOpened(model);
}
public int refuse(String str) throws ClubException{
String[] ids = StringHelper.stringToStrings(str,"|");
int s = 0;
if(!Validator.isEmpty(ids)){
s = this.trashs(str,true);
for(int i=0; i<ids.length; i++){
int takeId = TypeChange.stringToInt(ids[i]);
Message model = MESSAGE_SERVICE.getTake(takeId);
if(!Validator.isEmpty(model)){
MessageBlacklist black = new MessageBlacklist();
black.setMyUserId(model.getUserIdOfTake());
black.setUserId(model.getUserIdOfSend());
black.setUserName(model.getUserNameOfSend());
black.setCreateDateTime(FormatDateTime.now());
this.getMessageBlacklistService().create(black);
}
}
}
/*
if(s>0){
MESSAGE_SERVICE.setMessage(MessageUtils.getMessage("success"));
}else{
MESSAGE_SERVICE.setMessage(MessageUtils.getMessage("error"));
}
*/
return s;
}
public int trashs(String ids, boolean istrash) {
return MESSAGE_SERVICE.trashs(ids, istrash);
}
public int delete(String ids, String act) throws ClubException {
return MESSAGE_SERVICE.delete(ids, act);
}
private void validator(Message model) throws ClubException{
/*
* 如果是主题短信则需要填写标题
*/
if(Validator.isEmpty(model.getTitle())&&Validator.isEmpty(model.getContent()))
{
//MESSAGE_SERVICE.setMessage(MessageUtils.getMessage("error_empty"));
throw new ClubException(MessageUtils.getMessage("error_empty"));
}
else if(model.getTitle().length()>100||model.getContent().length()>2000){
//MESSAGE_SERVICE.setMessage(MessageUtils.getMessage("error_toolong"));
throw new ClubException(MessageUtils.getMessage("error_toolong"));
}
else if(this.getNoAllowedCharService().noAllowedOfMessage(model.getTitle())
|| this.getNoAllowedCharService().noAllowedOfMessage(model.getContent())
){
//MESSAGE_SERVICE.setMessage(MessageUtils.getMessage("error_noAllowedChar"));
throw new ClubException(MessageUtils.getMessage("error_noAllowedChar"));
}
/*
* 短信接收人不能为非注册用户
*/
else if(model.getUserIdOfTake()==0)
{
//MESSAGE_SERVICE.setMessage(MessageUtils.getMessage("error_notfind_user"));
throw new ClubException(MessageUtils.getMessage("error_notfind_user"));
}
/*
* 黑名单过滤
*/
else if(this.getMessageBlacklistService().findByMyUserIdAndUserId(model.getUserIdOfSend(),model.getUserIdOfTake())!=null){
//MESSAGE_SERVICE.setMessage(MessageUtils.getMessage("error"));
throw new ClubException(MessageUtils.getMessage("error_blacklist"));
}
/*
* 个性设置
*/
else{
UserSettings settings = this.getUserSettingsService().getUserSettings(model.getUserIdOfTake());
byte filter = settings.getMessageFilter();
switch(filter){
case 0: //接收任何人发送的消息
break;
case 1: //只接收会员发送的消息
break;
case 2: //只接收好友的消息
if(Validator.isEmpty(
this.getFriendService().findByMyUserIdAndFriendUserId(model.getUserIdOfTake(),model.getUserIdOfSend())
))
{
//MESSAGE_SERVICE.setMessage(MessageUtils.getMessage("error"));
logger.debug("非对方好友!");
throw new ClubException(MessageUtils.getMessage("error_message_onlyfriend"));
}
break;
case 3: //拒绝接收消息
//MESSAGE_SERVICE.setMessage(MessageUtils.getMessage("error"));
throw new ClubException(MessageUtils.getMessage("error_message_refused"));
}
if(settings.getMessageForwardUserId()>0){
model.setForwardUserId(settings.getMessageForwardUserId());
model.setForwardUserName(settings.getMessageForwardUserName());
}
}
}
public NoAllowedCharService getNoAllowedCharService() {
return ServiceWrapper.<NoAllowedCharService>getSingletonInstance(ServiceLocator.NOALLOWEDCHAR);
}
public UserSettingsService getUserSettingsService() {
return ServiceWrapper.<UserSettingsService>getSingletonInstance(ServiceLocator.USERSETTINGS);
}
public MessageBlacklistService getMessageBlacklistService() {
return ServiceWrapper.<MessageBlacklistService>getSingletonInstance(ServiceLocator.MESSAGEBLACKLIST);
}
public UserService getUserService(){
return ServiceWrapper.<UserService>getSingletonInstance(ServiceLocator.USER);
}
public FriendService getFriendService(){
return ServiceWrapper.<FriendService>getSingletonInstance(ServiceLocator.FRIEND);
}
public long countByParameter(MessageParameter param) {
return MESSAGE_SERVICE.countByParameter(param);
}
public Message getTake(int takeId) {
return MESSAGE_SERVICE.getTake(takeId);
}
public Message getSend(int sendId) {
return MESSAGE_SERVICE.getSend(sendId);
}
private RobotService getRobotService(){
return ServiceWrapper.<RobotService>getSingletonInstance(ServiceLocator.DOING_ROBOT);
}
private UserIMService getUserIMService(){
return ServiceWrapper.<UserIMService>getSingletonInstance(ServiceLocator.DOING_USERIM);
}
private BasicInfoService getBasicInfoService() {
return ServiceWrapper.<BasicInfoService>getSingletonInstance(ServiceLocator.BASICINFO);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?