KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > javabb > transaction > PrivMsgTransaction


1 /*
2  * Copyright 2004 JavaFree.org
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.javabb.transaction;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.javabb.component.VelocityTemplate;
24 import org.javabb.dao.entity.IPrivMsgReciviedDAO;
25 import org.javabb.dao.entity.IPrivMsgSentDAO;
26 import org.javabb.infra.Configuration;
27 import org.javabb.infra.Constants;
28 import org.javabb.infra.Email;
29 import org.javabb.vo.PrivMsg;
30 import org.javabb.vo.PrivMsgRecivied;
31 import org.javabb.vo.PrivMsgSent;
32 import org.javabb.vo.User;
33
34 /**
35  * @author Lucas Teixeira - <a HREF="mailto:lucas@javabb.org">lucas@javabb.org
36  * </a>
37  */

38
39 public class PrivMsgTransaction extends Transaction {
40
41     private IPrivMsgReciviedDAO _privMsgReciviedDAO = null;
42
43     private IPrivMsgSentDAO _privMsgSentDAO = null;
44     
45     private UserTransaction _userTransaction;
46
47     public PrivMsgTransaction() {
48     }
49
50     public void setPrivMsgReciviedDAO(IPrivMsgReciviedDAO privMsgReciviedDAO) {
51         this._privMsgReciviedDAO = privMsgReciviedDAO;
52     }
53
54     public void setPrivMsgSentDAO(IPrivMsgSentDAO privMsgSentDAO) {
55         this._privMsgSentDAO = privMsgSentDAO;
56     }
57
58     public void setUserTransaction(UserTransaction userTransaction) {
59         _userTransaction = userTransaction;
60     }
61     
62     public List JavaDoc getUserInbox(User u) throws Exception JavaDoc {
63         return _privMsgReciviedDAO.retrieveUserInbox(u);
64     }
65     
66     public int countMsgByUser(User u) throws Exception JavaDoc{
67         if(u != null && u.getIdUser() != null){
68             return _privMsgReciviedDAO.countMessagesByUser(u);
69         }else {
70             return 0;
71         }
72         
73     }
74
75     public List JavaDoc getUserOutbox(User u) throws Exception JavaDoc {
76         return _privMsgSentDAO.retrieveUserOutbox(u);
77     }
78
79     public Long JavaDoc send(PrivMsg p) throws Exception JavaDoc {
80         Long JavaDoc mpId = _privMsgReciviedDAO.save(new PrivMsgRecivied(p));
81         _privMsgSentDAO.save(new PrivMsgSent(p));
82         return mpId;
83     }
84     
85     public void deleteSent(PrivMsg p) throws Exception JavaDoc {
86         _privMsgSentDAO.delete((PrivMsgSent) p);
87     }
88
89     public void deleteRecivied(PrivMsg p) throws Exception JavaDoc {
90         _privMsgReciviedDAO.delete((PrivMsgRecivied) p);
91     }
92
93     public PrivMsg loadSent(PrivMsg p) throws Exception JavaDoc {
94         return _privMsgSentDAO.load(p.getId());
95     }
96
97     public PrivMsg loadRecivied(PrivMsg p) throws Exception JavaDoc {
98         PrivMsgRecivied _p = _privMsgReciviedDAO.load(p.getId());
99         _p.setRead(new Integer JavaDoc(1));
100         return _p;
101     }
102
103     public void deleteSelectedInbox(List JavaDoc list) throws Exception JavaDoc {
104         if (list != null && list.size() > 0)
105             _privMsgReciviedDAO.delete(list);
106     }
107
108     public void deleteSelectedOutbox(List JavaDoc list) throws Exception JavaDoc {
109         if (list != null && list.size() > 0)
110             _privMsgSentDAO.delete(list);
111     }
112
113     public List JavaDoc asPrivMsgList(Long JavaDoc[] id) {
114         List JavaDoc l = new ArrayList JavaDoc();
115         for (int i = 0; i < id.length; i++)
116             l.add(new PrivMsg(id[i]));
117         return l;
118     }
119
120     public void delegateMail(String JavaDoc message_18n,Long JavaDoc idUserTo, Long JavaDoc mpId) throws Exception JavaDoc{
121         //String mailUser = p.getUserTo();
122
User usr = _userTransaction.getUser(idUserTo);
123         String JavaDoc mailUser = usr.getEmail();
124         sendMailToUser(message_18n, mpId, mailUser);
125     }
126     
127     /**
128      * Send an email to user notify about his mp
129      * @param message_18n
130      * @param pmId
131      * @param userMail
132      * @throws Exception
133      */

134     private void sendMailToUser(String JavaDoc message_18n, Long JavaDoc pmId, String JavaDoc userMail) throws Exception JavaDoc{
135         //Send mail to user
136
Configuration conf = new Configuration();
137         
138         Map JavaDoc mailMap = new HashMap JavaDoc();
139         mailMap.put("conf", conf);
140         mailMap.put("pm_message", message_18n);
141         mailMap.put("pmId", pmId);
142         
143         String JavaDoc template = VelocityTemplate.makeTemplate(mailMap, Constants.mpMailTemplate);
144
145         Email.sendMail(conf.adminMail, userMail, conf.forumName, template,
146                 true);
147     }
148
149 }
Popular Tags