KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > geinuke > bizlogic > PMessageBL


1 /*
2 -- GeiNuke --
3 Copyright (c) 2005 by Roberto Sidoti [geinuke@users.sourceforge.net]
4 http://www.hostingjava.it/-geinuke/
5
6 This file is part of GeiNuke.
7
8    GeiNuke is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    GeiNuke is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with GeiNuke; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */

22 package com.geinuke.bizlogic;
23 import java.sql.SQLException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Hashtable JavaDoc;
26 import java.util.List JavaDoc;
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 JavaDoc 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 JavaDoc cache=new Hashtable JavaDoc();
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 JavaDoc getReceivedPMessagesByUId(int uid) throws BLException {
70         ArrayList JavaDoc ll=null;
71         try{
72             PMessageDAO dao=new PMessageDAO();
73             List JavaDoc list=dao.getReceivedPMessagesByUId(uid);
74             
75             ll=new ArrayList JavaDoc(list);
76             this.fillMessages(ll,this.FILL_FROM,uid);
77         }catch(SQLException JavaDoc sqle){
78             throw new DBException(sqle.getMessage());
79         }catch(Throwable JavaDoc t){
80             throw new BLException(t.getMessage());
81         }
82         
83         return ll;
84     }
85
86     public ArrayList JavaDoc getSentPMessagesByUId(int uid) throws BLException {
87         ArrayList JavaDoc ll=null;
88         try{
89             PMessageDAO dao=new PMessageDAO();
90             List JavaDoc list=dao.getSentPMessagesByUId(uid);
91             
92             ll=new ArrayList JavaDoc(list);
93             this.fillMessages(ll,this.FILL_TO,uid);
94         }catch(SQLException JavaDoc sqle){
95             throw new DBException(sqle.getMessage());
96         }catch(Throwable JavaDoc 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 JavaDoc sqle){
110             throw new DBException(sqle.getMessage());
111         }catch(Throwable JavaDoc 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 JavaDoc sqle){
125             throw new DBException(sqle.getMessage());
126         }catch(Throwable JavaDoc 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 JavaDoc sqle){
140             throw new DBException(sqle.getMessage());
141         }catch(Throwable JavaDoc t){
142             throw new BLException(t.getMessage());
143         }
144         
145     }
146
147
148     public void delReceivedMessages(String JavaDoc[] 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 JavaDoc sqle){
167             throw new DBException(sqle.getMessage());
168         }catch(Throwable JavaDoc t){
169             throw new BLException(t.getMessage());
170         }
171         
172     }
173
174     public void delSentMessages(String JavaDoc[] 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 JavaDoc sqle){
194             throw new DBException(sqle.getMessage());
195         }catch(Throwable JavaDoc t){
196             throw new BLException(t.getMessage());
197         }
198     }
199
200 }
201
Popular Tags