📄 messageserviceimpl.java
字号:
/*
* Created on 2007-4-30
* 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.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.MessageSendDAO;
import com.yeqiangwei.club.dao.MessageTakeDAO;
import com.yeqiangwei.club.param.MessageParameter;
import com.yeqiangwei.club.service.model.MessageModel;
import com.yeqiangwei.util.StringHelper;
import com.yeqiangwei.util.Validator;
import com.yeqiangwei.club.util.BeanUtils;
import com.yeqiangwei.club.util.BeanLocator;
import com.yeqiangwei.club.util.MessageUtils;
import com.yeqiangwei.club.controller.form.MessageForm;
import com.yeqiangwei.club.dao.model.MessageTake;
import com.yeqiangwei.club.dao.model.MessageSend;
import com.yeqiangwei.club.exception.ClubException;
public class MessageServiceImpl implements MessageService{
private static final Logger logger = Logger.getLogger(MessageServiceImpl.class);
private Cache cache = CacheFactory.creator(CacheRegion.MESSAGE_TAKE);
public int sends(MessageForm form) {
return 0;
}
public static void main(String args[]){
/*
com.yeqiangwei.club.dao.hibernate.ConnectionManager.init();
MessageServiceImpl service = new MessageServiceImpl();
String[] ids = new String[3];
ids[0] = "1";
ids[1] = "1";
ids[2] = "1";
//service.trash(ids,true);
List<Integer> list = new java.util.ArrayList<Integer> ();
list.add(1);
list.add(2);
//service.getMessageTakeDAO().update(list,true);
com.yeqiangwei.club.dao.hibernate.ConnectionManager.closeSession();
*/
}
public String cacheKeyNewMessage(int userId){
StringBuffer sb = new StringBuffer("new message key - userId:");
sb.append(userId);
return sb.toString();
}
public long countNewMessage(int userId) {
if(userId==0){
return 0;
}
Long n = (Long) cache.get(this.cacheKeyNewMessage(userId));
//Boolean isput = (Boolean) cache.get(this.cacheKeyNewMessage(userId)+" - isput");
if(Validator.isEmpty(n)){
MessageParameter param = new MessageParameter();
param.setUserIdOfTake(userId);
param.setIsOpened(new Boolean(false));
param.setIsDeleted(new Boolean(false));
n = new Long(this.getMessageTakeDAO().countByParameter(param));
cache.put(this.cacheKeyNewMessage(userId),n);
//cache.put(this.cacheKeyNewMessage(userId)+" - isput",new Boolean(true));
}
return n.longValue();
}
public MessageModel create(MessageModel model, boolean saveSent) {
if(Validator.isEmpty(model.getTitle())){
String temp = StringHelper.substring(model.getContent(),0,30,null);
model.setTitle(temp);
if(model.getTitle().length()==model.getContent().length()){
model.setContent("");
}
}
MessageTake take = new MessageTake();
BeanUtils.copyProperties(take,model);
this.getMessageTakeDAO().create(take);
if(take.getReplyId()==0){
take.setReplyId(take.getTakeId());
this.getMessageTakeDAO().update(take);
}
if(saveSent){ //是否需要保存到发件箱
MessageSend send = new MessageSend();
BeanUtils.copyProperties(send,model);
send.setTakeId(take.getTakeId());
this.getMessageSendDAO().create(send);
}
cache.remove(this.cacheKeyNewMessage(take.getUserIdOfTake()));
return model;
}
public List<MessageModel> findByParameter(MessageParameter param) {
if(param.getAct().equals("take")){
param.setSpace((byte)0);
return this.fromTake(param);
}
else if(param.getAct().equals("trash")){
param.setSpace((byte)1);
return this.fromTake(param);
}else{
return this.fromSend(param);
}
}
public MessageModel findOnlyByParameter(MessageParameter param) {
if(param.getAct().equals("take")&&!Validator.isEmpty(param.getTakeId())){
return this.getTake(param.getTakeId());
}
else if(param.getAct().equals("send")&&!Validator.isEmpty(param.getSendId())){
return this.getSend(param.getSendId());
}
else{
return null;
}
}
public long countByParameter(MessageParameter param) {
if(param.getAct().equals("take")){
param.setSpace((byte)0);
return this.getMessageTakeDAO().countByParameter(param);
}
if(param.getAct().equals("trash")){
param.setSpace((byte)1);
return this.getMessageTakeDAO().countByParameter(param);
}
else{
return this.getMessageSendDAO().countByParameter(param);
}
}
public int refuse(String str){
return this.trash(StringHelper.stringToIntegerList(str,"|"),true);
}
public int trashs(String str, boolean istrash) {
return this.trash(StringHelper.stringToIntegerList(str,"|"),istrash);
}
public int delete(String str, String act) throws ClubException {
return this.delete(StringHelper.stringToIntegerList(str,"|"),act);
}
public int trash(List<Integer> list, boolean isDeleted) {
int s = 0;
if(!Validator.isEmpty(list)){
byte space = 0;
if(isDeleted){
space = 1;
}else{
space = 0;
}
s = this.update_space(list,space);
}
return s;
}
public int update_space(String str, byte space){
return this.update_space(StringHelper.stringToIntegerList(str,"|"),space);
}
public int update_space(List<Integer> list, byte space){
return this.getMessageTakeDAO().update_space(list,space);
}
public int update_isOpened(MessageModel model) {
int c = this.getMessageTakeDAO().update_isOpened(model.getTakeId(), model.getIsOpened());
if(c==0){
logger.error("收件箱标记已读失败 takeId = "+model.getTakeId());
}
c = this.getMessageSendDAO().update_thatIsOpened(model.getTakeId(), model.getIsOpened());
if(c==0){
logger.error("发件箱标记已打开失败 takeId = "+model.getTakeId());
}
cache.remove(this.cacheKeyNewMessage(model.getUserIdOfTake()));
return c;
}
public int delete(List<Integer> list, String act) throws ClubException {
int s = 0;
if(!Validator.isEmpty(list)){
if(act.equals("deltake")){
s = this.getMessageTakeDAO().delete(list);
}else if(act.equals("delsend")){
s = this.getMessageSendDAO().delete(list);
}else{
logger.error("action is null");
throw new ClubException(MessageUtils.getMessage("error_system"));
}
}else{
//this.setMessage(MessageUtils.getMessage("error_delete_noid"));
throw new ClubException(MessageUtils.getMessage("error_delete_noid"));
}
return s;
}
/*
private boolean update(int id){
boolean bea = false;
MessageTake item = this.getMessageTakeDAO().findById(id);
if(!Validator.isEmpty(item)){
int c = this.getMessageTakeDAO().delete(item);
if(c>0){
bea = true;
this.setMessage(MessageUtils.getMessage("success"));
}else{
this.setMessage(MessageUtils.getMessage("error_system"));
}
}else{
this.setMessage(MessageUtils.getMessage("error_system"));
}
return bea;
}
private int delete(int id){
int c = 0;
if(id>0){
MessageTake take = new MessageTake();
take.setTakeId(id);
c = this.getMessageTakeDAO().delete(take);
}
return c;
}
*/
public MessageModel getTake(int takeId){
MessageModel model = null;
MessageTake take = this.getMessageTakeDAO().findById(takeId);
if(!Validator.isEmpty(take)){
model = new MessageModel();
BeanUtils.copyProperties(model,take);
}
return model;
}
public MessageModel getSend(int sendId){
MessageModel model = null;
MessageSend send = this.getMessageSendDAO().findById(sendId);
if(!Validator.isEmpty(send)){
model = new MessageModel();
BeanUtils.copyProperties(model,send);
}
return model;
}
private List<MessageModel> fromTake(MessageParameter param){
param.setUserIdOfTake(param.getMyUserId());
List<MessageTake> list = this.getMessageTakeDAO().findByParameter(param);
List<MessageModel> mlist = BeanUtils.copyList(list, BeanLocator.MESSAGEMODEL);
return mlist;
}
private List<MessageModel> fromSend(MessageParameter param){
param.setUserIdOfSend(param.getMyUserId());
List<MessageSend> list = this.getMessageSendDAO().findByParameter(param);
List<MessageModel> mlist = BeanUtils.copyList(list, BeanLocator.MESSAGEMODEL);
return mlist;
}
public MessageSendDAO getMessageSendDAO() {
return DAOWrapper.<MessageSendDAO>getSingletonInstance(DAOLocator.MESSAGESEND);
}
public MessageTakeDAO getMessageTakeDAO() {
return DAOWrapper.<MessageTakeDAO>getSingletonInstance(DAOLocator.MESSAGETAKE);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -