📄 messageproxy.java
字号:
/*
* 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.service.model.MessageBlacklistModel;
import com.yeqiangwei.club.service.model.MessageModel;
import com.yeqiangwei.club.service.model.UserModel;
import com.yeqiangwei.club.service.model.UserSettingsModel;
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.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();
UserModel userModel = this.getUserService().findById(userIdOfSend);
if(Validator.isEmpty(userModel)){
//MESSAGE_SERVICE.setMessage(MessageUtils.getMessage("error_notlogin"));
throw new ClubException(MessageUtils.getMessage("error_notlogin"));
}else{
userNameOfSend = userModel.getUserName();
}
if(userIdOfTake>0){
UserModel 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();
}
MessageModel message = new MessageModel();
BeanUtils.copyProperties(message,form);
message.setUserIdOfSend(userIdOfSend);
message.setUserNameOfSend(userNameOfSend);
message.setUserIdOfTake(userIdOfTake);
message.setUserNameOfTake(userNameOfTake);
if(this.create(message,true)!=null){
c = 1;
}
}
else if(!Validator.isEmpty(userNameOfTake)){
UserModel 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();
}
MessageModel message = new MessageModel();
BeanUtils.copyProperties(message,form);
message.setUserIdOfSend(userIdOfSend);
message.setUserNameOfSend(userNameOfSend);
message.setUserIdOfTake(userIdOfTake);
message.setUserNameOfTake(userNameOfTake);
if(this.create(message,true)!=null){
c = 1;
}
}
else if(!Validator.isEmpty(form.getUserNames())){
int s = 0;
int e = 0;
String userNames = form.getUserNames().replace(",",",");
List<String> list = StringHelper.stringToList(userNames,",");
if(!Validator.isEmpty(list)){
for(int i=0; i<list.size(); i++){
String username = list.get(i);
UserModel user = this.getUserService().findByUserName(username);
if(!Validator.isEmpty(user)){
MessageModel message = new MessageModel();
BeanUtils.copyProperties(message,form);
message.setUserIdOfSend(userIdOfSend);
message.setUserNameOfSend(userNameOfSend);
message.setUserIdOfTake(user.getUserId());
message.setUserNameOfTake(user.getUserName());
if(this.create(message,true)!=null){
s++;
}else{
e++;
}
}else{
e++;
}
}
//MESSAGE_SERVICE.setMessage(MessageUtils.getMessage(s,e));
}else{
//MESSAGE_SERVICE.setMessage(MessageUtils.getMessage("error"));
throw new ClubException(MessageUtils.getMessage("error"));
}
c = s;
}else{
//MESSAGE_SERVICE.setMessage(MessageUtils.getMessage("error_notfind_user"));
throw new ClubException(MessageUtils.getMessage("error_notfind_user"));
}
return c;
}
public MessageModel create(MessageModel model, boolean saveSent) throws ClubException {
this.validator(model);
return MESSAGE_SERVICE.create(model,saveSent);
}
public List<MessageModel> findByParameter(MessageParameter param) {
return MESSAGE_SERVICE.findByParameter(param);
}
public MessageModel findOnlyByParameter(MessageParameter param) {
return MESSAGE_SERVICE.findOnlyByParameter(param);
}
public int update_space(List<Integer> list, byte space){
return MESSAGE_SERVICE.update_space(list,space);
}
public int update_space(String ids, byte space){
return MESSAGE_SERVICE.update_space(ids,space);
}
public int update_isOpened(MessageModel model) {
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]);
MessageModel model = MESSAGE_SERVICE.getTake(takeId);
if(!Validator.isEmpty(model)){
MessageBlacklistModel black = new MessageBlacklistModel();
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(MessageModel 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{
UserSettingsModel settings = this.getUserSettingsService().getUserSettingsModel(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 MessageModel getTake(int takeId) {
return MESSAGE_SERVICE.getTake(takeId);
}
public MessageModel getSend(int sendId) {
return MESSAGE_SERVICE.getSend(sendId);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -