KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.javabb.transaction;
2
3 import java.io.File JavaDoc;
4 import java.io.FileInputStream JavaDoc;
5 import java.io.FileOutputStream JavaDoc;
6 import java.io.IOException JavaDoc;
7 import java.util.ArrayList JavaDoc;
8 import java.util.LinkedList JavaDoc;
9 import java.util.List JavaDoc;
10 import java.util.Properties JavaDoc;
11
12 import org.javabb.dao.entity.ICategoryDAO;
13 import org.javabb.dao.entity.IForumDAO;
14 import org.javabb.dao.entity.IPostDAO;
15 import org.javabb.dao.entity.IRefreshStatsDAO;
16 import org.javabb.dao.entity.ITopicDAO;
17 import org.javabb.dao.entity.IUserRankDAO;
18 import org.javabb.infra.Configuration;
19 import org.javabb.infra.ConfigurationFactory;
20 import org.javabb.vh.ForumConfigView;
21 import org.javabb.vo.Category;
22 import org.javabb.vo.Forum;
23 import org.javabb.vo.Post;
24 import org.javabb.vo.Topic;
25
26 /*
27  * Copyright 2004 JavaFree.org
28  *
29  * Licensed under the Apache License, Version 2.0 (the "License");
30  * you may not use this file except in compliance with the License.
31  * You may obtain a copy of the License at
32  *
33  * http://www.apache.org/licenses/LICENSE-2.0
34  *
35  * Unless required by applicable law or agreed to in writing, software
36  * distributed under the License is distributed on an "AS IS" BASIS,
37  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
38  * See the License for the specific language governing permissions and
39  * limitations under the License.
40  */

41
42 /**
43  * $Id: ForumTransaction.java,v 1.38.2.2.2.2.2.3 2006/04/17 17:46:47 daltoncamargo Exp $
44  *
45  * @author Dalton Camargo - <a HREF="mailto:dalton@javabb.org">dalton@javabb.org
46  * </a>
47  */

48 public class ForumTransaction extends Transaction {
49
50     private IForumDAO _forumDAO;
51
52     private ICategoryDAO _categoryDAO;
53
54     private UserTransaction _userTransaction;
55     
56     private IRefreshStatsDAO _refreshStatsDAO;
57     
58     private IUserRankDAO _userRankDAO;
59     
60     private ITopicDAO _topicDAO;
61     
62     private IPostDAO _postDAO;
63
64     public void setForumDAO(IForumDAO forumDAO) {
65         this._forumDAO = forumDAO;
66     }
67
68     public void setCategoryDAO(ICategoryDAO categoryDAO) {
69         this._categoryDAO = categoryDAO;
70     }
71
72     public void setUserTransaction(UserTransaction userTransaction) {
73         _userTransaction = userTransaction;
74     }
75
76     public void setUserRankDAO(IUserRankDAO rankDAO) {
77         _userRankDAO = rankDAO;
78     }
79     
80     /**
81      * @param refreshStatsDAO The refreshStatsDAO to set.
82      */

83     public void setRefreshStatsDAO(IRefreshStatsDAO refreshStatsDAO) {
84         _refreshStatsDAO = refreshStatsDAO;
85     }
86
87     public void setTopicDAO(ITopicDAO topicDAO) {
88         this._topicDAO = topicDAO;
89     }
90     
91     public void setPostDAO(IPostDAO postDAO) {
92         this._postDAO = postDAO;
93     }
94     
95     /**
96      * @param id
97      * @return forum
98      */

99     public Forum loadForum(Long JavaDoc id) {
100         return _forumDAO.load(id);
101     }
102
103     /**
104      * Obtém todos fórums e seus últimos posts
105      *
106      * @return .
107      * @throws Exception
108      */

109     public List JavaDoc findAll() throws Exception JavaDoc {
110         return _forumDAO.findAll();
111     }
112
113     /**
114      * Obtém todos fórums e seus últimos posts
115      *
116      * @param category -
117      * categoria do forum
118      * @return - Lista contendo os foruns de uma categoria
119      * @throws Exception
120      */

121     public List JavaDoc findAll(Category category) throws Exception JavaDoc {
122         return _forumDAO.findByCategory(category.getId());
123     }
124
125     /**
126      * @return .
127      * @throws Exception
128      */

129     public Long JavaDoc findNroTotalForuns() throws Exception JavaDoc {
130         return new Long JavaDoc(_forumDAO.countAllForums());
131     }
132
133     /**
134      * Deleta posts, tópicos e fórum
135      *
136      * @param forum
137      * @throws Exception
138      */

139     public void deleteForum(Forum forum) throws Exception JavaDoc {
140         _forumDAO.deleteForum(forum);
141     }
142
143     /**
144      * @param forum
145      * @param forumTo
146      * @throws Exception
147      */

148     public void transferForum(Forum forum, int forumTo) throws Exception JavaDoc {
149         _forumDAO.transferForum(forum, forumTo);
150     }
151
152     /**
153      * Refresh the information of Forum
154      *
155      * @param forumId
156      */

157     public void refreshForum(Long JavaDoc forumId) {
158         if(forumId != null){
159             _refreshStatsDAO.refreshForum(forumId);
160         }
161     }
162
163     /**
164      * Refresh the information of Topic count at forum
165      *
166      * @param topicId
167      */

168     public void refreshTopic(Long JavaDoc topicId) {
169         if (topicId != null) {
170             _refreshStatsDAO.refreshTopic(topicId);
171
172         }
173     }
174
175     /**
176      * Refresh the information of Post count at forum
177      *
178      * @param postId
179      */

180     public void refreshPost(Long JavaDoc postId) {
181         if (postId != null) {
182             _refreshStatsDAO.refreshPost(postId);
183         }
184     }
185
186     /**
187      * @param forum
188      */

189     public void update(Long JavaDoc forumId, Forum forum) {
190         Forum forumToUpdate = this.loadForum(forumId);
191         forumToUpdate.setDescricao(forum.getDescricao());
192         forumToUpdate.setCategory(forum.getCategory());
193         forumToUpdate.setNome(forum.getNome());
194     }
195
196     /**
197      * Insert forum
198      *
199      * @param forum
200      * @return
201      */

202     public Forum insertForum(Forum forum) {
203         Integer JavaDoc orderForum = new Integer JavaDoc(_forumDAO.countAllForums() + 1);
204         forum.setForumOrder(orderForum);
205         forum.setForumStatus(new Integer JavaDoc(0));
206         forum = _forumDAO.insertForum(forum);
207
208         return forum;
209     }
210
211     /**
212      * List all buttons languages available
213      *
214      * @return
215      */

216     public List JavaDoc listButtons() {
217         List JavaDoc languages = new ArrayList JavaDoc();
218
219         String JavaDoc btnPath = Configuration.realPath + File.separator + "forum"
220                 + File.separator + "images" + File.separator + "buttons";
221
222         File JavaDoc[] btnDir = new File JavaDoc(btnPath).listFiles();
223         for (int i = 0; i < btnDir.length; i++) {
224             String JavaDoc btnDirName = btnDir[i].getName();
225             if (!"CVS".equalsIgnoreCase(btnDirName)) {
226                 languages.add(btnDirName);
227             }
228         }
229
230         return languages;
231     }
232
233     /**
234      * Save the JavaBB basic configurations through of Adm Painel
235      *
236      * @param forum
237      */

238     public void saveConfigForum(ForumConfigView forum) {
239         try {
240             Properties JavaDoc properties = new Properties JavaDoc();
241
242             String JavaDoc javabbProperties = Configuration.realPath + File.separator
243                     + "WEB-INF" + File.separator + "appconf" + File.separator
244                     + "javabb.properties";
245
246             properties.load(new FileInputStream JavaDoc(javabbProperties));
247             FileOutputStream JavaDoc out = new FileOutputStream JavaDoc(javabbProperties);
248
249             //Update the property file
250

251             String JavaDoc url = forum.getDomain();
252             if ("/".equals(url.substring(url.length() - 1, url.length()))) {
253                 properties.put("config.forum.domain", forum.getDomain());
254             } else {
255                 properties.put("config.forum.domain", forum.getDomain() + "/");
256             }
257             properties.put("config.forum.forum.name", forum.getForumName());
258             properties.put("config.forum.date.format", forum.getDateFormat());
259             properties.put("config.forum.time.format", forum.getTimeFormat());
260             properties.put("config.forum.topics.page", forum.getTopicsPage());
261             properties.put("config.forum.posts.page", forum.getPostsPage());
262             properties.put("config.forum.button.lang", forum.getButtonLang());
263
264             properties.put("config.forum.admin.mail", forum.getAdminMail());
265             properties.put("config.email.notify.topic", forum.getNotifyTopic());
266             properties.put("config.forum.smtp.server.host", forum.getSmtpHost());
267             properties.put("config.forum.smtp.server.user", forum.getSmtpUser());
268             properties.put("config.forum.smtp.server.senha", forum.getSmtpPassword());
269             properties.put("config.forum.flood_control", forum.getFloodControl());
270             properties.put("config.forum.posts.announce.text", forum.getForumAnnounceText());
271             
272             properties.store(out, "JavaBB Property File");
273             out.close();
274
275             //Refreshing the static instance of Configuration
276
ConfigurationFactory.refreshConfig();
277
278         } catch (IOException JavaDoc e) {
279             e.printStackTrace();
280         }
281     }
282
283     /**
284      * Sort foruns
285      *
286      * @param idCategory -
287      * Id of category that forum has
288      * @param destOrder -
289      * Position destination of forum
290      * @param position -
291      * Current position of fórum
292      * @throws Exception
293      */

294     public void sortForuns(Long JavaDoc idCategory, Integer JavaDoc destOrder, Integer JavaDoc position)
295             throws Exception JavaDoc {
296
297         List JavaDoc foruns = _forumDAO.findByCategoryOrderAsc(idCategory);
298         LinkedList JavaDoc sortForum = new LinkedList JavaDoc();
299         for (int i = 0; i < foruns.size(); i++) {
300             Forum forumAdd = (Forum) foruns.get(i);
301             forumAdd.setForumOrder(new Integer JavaDoc(i));
302             sortForum.add(forumAdd);
303         }
304
305         //GC helping
306
foruns.clear();
307         foruns = null;
308
309         Forum t = (Forum) sortForum.get(position.intValue());
310
311         sortForum.remove(position.intValue());
312         sortForum.add(destOrder.intValue(), t);
313
314         Forum forumUpd = null;
315         for (int i = 0; i < sortForum.size(); i++) {
316             Forum forum = (Forum) sortForum.get(i);
317             //New instance of this object to reflect the update on the
318
//transaction of spring
319
forumUpd = new Forum();
320             forumUpd = _forumDAO.load(forum.getIdForum());
321             forumUpd.setForumOrder(new Integer JavaDoc(i));
322         }
323
324         //GC helping
325
sortForum.clear();
326         sortForum = null;
327     }
328
329     /**
330      * Set at UserContext all forum ids
331      */

332     public void setUnreadForumIds() throws Exception JavaDoc {
333         /*Set topics = UserContext.getContext().getReadTopicIds();
334         Date lastVisit = UserContext.getContext().getLastVisitTimestamp();
335         Long userId = new Long(0);
336         if (UserContext.getContext().getUser() != null
337                 && UserContext.getContext().getUser().getIdUser() != null) {
338             userId = UserContext.getContext().getUser().getIdUser();
339         }
340
341         List unreadForumIds = _forumDAO.obtainUnreadForuns(topics, lastVisit,
342                 userId);
343         UserContext.getContext().setUnreadForumIds(unreadForumIds);*/

344     }
345     
346     
347     /**
348      * Refresh all userRank table of all forums
349      * @throws Exception
350      */

351     public void refreshForumUserRank() throws Exception JavaDoc{
352         _userRankDAO.cleanAllUserRank();
353         List JavaDoc forums = findAll();
354         for(int i=0; i<forums.size(); i++){
355             Forum forum = (Forum) forums.get(i);
356             _userRankDAO.refreshUserRankByForum(forum.getId());
357         }
358     }
359
360 }
Popular Tags