1 22 package com.geinuke.bizlogic; 23 import java.sql.SQLException ; 24 import java.util.ArrayList ; 25 import java.util.Hashtable ; 26 import java.util.List ; 27 import com.geinuke.dao.PMessageDAO; 28 29 import com.geinuke.middle.*; 30 import com.geinuke.util.log.*; 31 import com.geinuke.vo.PMessageVO; 32 import com.geinuke.vo.UserVO; 33 34 35 public class PMessageBL implements IPMessageBL{ 36 37 protected final int FILL_FROM=0; 38 protected final int FILL_TO=1; 39 protected ILogger logger=null; 40 41 42 public PMessageBL(){ 43 logger=LoggerFactory.getLogger(PMessageBL.class,LoggerFactory.LOG4J); 44 } 45 46 protected void fillMessage(PMessageVO pm)throws BLException{ 47 IUserBL ubl=(IUserBL)MiddleFactory.getBL("IUserBL"); 48 pm.setUserFrom(ubl.getUserFromByPID(pm.getIdPM())); 49 pm.setUserTo(ubl.getUserToByPID(pm.getIdPM())); 50 } 51 52 protected void fillMessages(ArrayList li,int who,int uid)throws BLException{ 53 IUserBL ubl=(IUserBL)MiddleFactory.getBL("IUserBL"); 54 UserVO uu=ubl.getUserByID(uid); 55 PMessageVO pm=null; 56 Hashtable cache=new Hashtable (); 57 for(int i=0;li!=null && i<li.size();i++){ 58 pm=(PMessageVO)li.get(i); 59 if(who==this.FILL_FROM){ 60 pm.setUserFrom(ubl.getUserFromByPID(pm.getIdPM())); 61 pm.setUserTo(uu); 62 }else{ 63 pm.setUserFrom(uu); 64 pm.setUserTo(ubl.getUserToByPID(pm.getIdPM())); 65 } 66 } 67 } 68 69 public ArrayList getReceivedPMessagesByUId(int uid) throws BLException { 70 ArrayList ll=null; 71 try{ 72 PMessageDAO dao=new PMessageDAO(); 73 List list=dao.getReceivedPMessagesByUId(uid); 74 75 ll=new ArrayList (list); 76 this.fillMessages(ll,this.FILL_FROM,uid); 77 }catch(SQLException sqle){ 78 throw new DBException(sqle.getMessage()); 79 }catch(Throwable t){ 80 throw new BLException(t.getMessage()); 81 } 82 83 return ll; 84 } 85 86 public ArrayList getSentPMessagesByUId(int uid) throws BLException { 87 ArrayList ll=null; 88 try{ 89 PMessageDAO dao=new PMessageDAO(); 90 List list=dao.getSentPMessagesByUId(uid); 91 92 ll=new ArrayList (list); 93 this.fillMessages(ll,this.FILL_TO,uid); 94 }catch(SQLException sqle){ 95 throw new DBException(sqle.getMessage()); 96 }catch(Throwable t){ 97 throw new BLException(t.getMessage()); 98 } 99 100 return ll; 101 } 102 103 104 public void sendPMessage(PMessageVO mes) throws BLException { 105 try{ 106 PMessageDAO dao=new PMessageDAO(); 107 dao.addPMessage(mes); 108 109 }catch(SQLException sqle){ 110 throw new DBException(sqle.getMessage()); 111 }catch(Throwable t){ 112 throw new BLException(t.getMessage()); 113 } 114 115 } 116 117 118 public PMessageVO getPMessageById(int id) throws BLException { 119 PMessageVO m=null; 120 try{ 121 PMessageDAO dao=new PMessageDAO(); 122 m=dao.getPMessageById(id); 123 this.fillMessage(m); 124 }catch(SQLException sqle){ 125 throw new DBException(sqle.getMessage()); 126 }catch(Throwable t){ 127 throw new BLException(t.getMessage()); 128 } 129 return m; 130 } 131 132 public void setSeenMessage(int pid) throws BLException { 133 PMessageVO m=null; 134 try{ 135 PMessageDAO dao=new PMessageDAO(); 136 m=this.getPMessageById(pid); 137 m.setSeen(true); 138 dao.updatePMessage(m); 139 }catch(SQLException sqle){ 140 throw new DBException(sqle.getMessage()); 141 }catch(Throwable t){ 142 throw new BLException(t.getMessage()); 143 } 144 145 } 146 147 148 public void delReceivedMessages(String [] pids) throws BLException { 149 try{ 150 PMessageDAO dao=new PMessageDAO(); 151 152 int pid=-1; 153 for(int i=0;i<pids.length;i++){ 154 pid=Integer.parseInt(pids[i]); 155 PMessageVO p=this.getPMessageById(pid); 156 logger.log(ILogger.DEBUG,"PMessageBL.delReceivedMessages(...), p.isCancelledByFrom()="+p.isCancelledByFrom()); 157 if(p.isCancelledByFrom()){ 158 logger.log(ILogger.DEBUG,"PMessageBL.delReceivedMessages(...),calling delete"); 159 dao.deletePMessageByPID(pid); 160 }else{ 161 logger.log(ILogger.DEBUG,"PMessageBL.delReceivedMessages(...),calling update"); 162 p.setCancelledByTo(true); 163 dao.updatePMessage(p); 164 } 165 } 166 }catch(SQLException sqle){ 167 throw new DBException(sqle.getMessage()); 168 }catch(Throwable t){ 169 throw new BLException(t.getMessage()); 170 } 171 172 } 173 174 public void delSentMessages(String [] pids) throws BLException { 175 try{ 176 PMessageDAO dao=new PMessageDAO(); 177 178 int pid=-1; 179 for(int i=0;i<pids.length;i++){ 180 pid=Integer.parseInt(pids[i]); 181 182 PMessageVO p=this.getPMessageById(pid); 183 System.out.println("PMessageBL.delSentMessages(...), p.isCancelledByTo()="+p.isCancelledByTo()); 184 if(p.isCancelledByTo()){ 185 System.out.println("PMessageBL.delSentMessages(...),calling delete"); 186 dao.deletePMessageByPID(pid); 187 }else{ 188 System.out.println("PMessageBL.delSentMessages(...),calling update"); 189 p.setCancelledByFrom(true); 190 dao.updatePMessage(p); 191 } 192 } 193 }catch(SQLException sqle){ 194 throw new DBException(sqle.getMessage()); 195 }catch(Throwable t){ 196 throw new BLException(t.getMessage()); 197 } 198 } 199 200 } 201 | Popular Tags |