KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > jforum > repository > PostRepository


1 /*
2  * Copyright (c) 2003, Rafael Steil
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms,
6  * with or without modification, are permitted provided
7  * that the following conditions are met:
8  *
9  * 1) Redistributions of source code must retain the above
10  * copyright notice, this list of conditions and the
11  * following disclaimer.
12  * 2) Redistributions in binary form must reproduce the
13  * above copyright notice, this list of conditions and
14  * the following disclaimer in the documentation and/or
15  * other materials provided with the distribution.
16  * 3) Neither the name of "Rafael Steil" nor
17  * the names of its contributors may be used to endorse
18  * or promote products derived from this software without
19  * specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
22  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
23  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
27  * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
32  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
34  * IN CONTRACT, STRICT LIABILITY, OR TORT
35  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
36  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
38  *
39  * This file creation date: 07/02/2005 - 10:29:14
40  * The JForum Project
41  * http://www.jforum.net
42  */

43 package net.jforum.repository;
44
45 import java.util.ArrayList JavaDoc;
46 import java.util.Collection JavaDoc;
47 import java.util.Iterator JavaDoc;
48 import java.util.LinkedHashMap JavaDoc;
49 import java.util.List JavaDoc;
50 import java.util.Map JavaDoc;
51
52 import net.jforum.cache.CacheEngine;
53 import net.jforum.cache.Cacheable;
54 import net.jforum.dao.DataAccessDriver;
55 import net.jforum.dao.PostDAO;
56 import net.jforum.entities.Post;
57 import net.jforum.util.preferences.ConfigKeys;
58 import net.jforum.util.preferences.SystemGlobals;
59 import net.jforum.view.forum.common.PostCommon;
60
61 /**
62  * Repository for the post in the top n topics for each forum.
63  *
64  * @author Sean Mitchell
65  * @author Rafael Steil
66  * @version $Id: PostRepository.java,v 1.11 2006/01/16 20:13:57 rafaelsteil Exp $
67  */

68 public class PostRepository implements Cacheable
69 {
70     private static final int CACHE_SIZE = SystemGlobals.getIntValue(ConfigKeys.POSTS_CACHE_SIZE);
71     private static final String JavaDoc FQN = "posts";
72     private static CacheEngine cache;
73     
74     /**
75      * @see net.jforum.cache.Cacheable#setCacheEngine(net.jforum.cache.CacheEngine)
76      */

77     public void setCacheEngine(CacheEngine engine)
78     {
79         cache = engine;
80     }
81     
82     public static int size()
83     {
84         Map JavaDoc m = (Map JavaDoc)cache.get(FQN);
85         return (m != null ? m.size() : 0);
86     }
87     
88     public static int size(int topicId)
89     {
90         List JavaDoc posts = (List JavaDoc)cache.get(FQN, Integer.toString(topicId));
91         return (posts == null ? 0 : posts.size());
92     }
93     
94     public static Collection JavaDoc cachedTopics()
95     {
96         Map JavaDoc m = (Map JavaDoc)cache.get(FQN);
97         if (m == null) {
98             return new ArrayList JavaDoc();
99         }
100         
101         return m.keySet();
102     }
103         
104     public static List JavaDoc selectAllByTopicByLimit(int topicId, int start, int count) throws Exception JavaDoc
105     {
106         String JavaDoc tid = Integer.toString(topicId);
107         
108         List JavaDoc posts = (List JavaDoc)cache.get(FQN, tid);
109         if (posts == null || posts.size() == 0) {
110             PostDAO pm = DataAccessDriver.getInstance().newPostDAO();
111             posts = pm.selectAllByTopic(topicId);
112             
113             for (Iterator JavaDoc iter = posts.iterator(); iter.hasNext(); ) {
114                 PostCommon.preparePostForDisplay((Post)iter.next());
115             }
116     
117             Map JavaDoc topics = (Map JavaDoc)cache.get(FQN);
118             if (topics == null || topics.size() == 0 || topics.size() < CACHE_SIZE) {
119                 cache.add(FQN, tid, posts);
120             }
121             else {
122                 if (!(topics instanceof LinkedHashMap JavaDoc)) {
123                     topics = new LinkedHashMap JavaDoc(topics) {
124                         protected boolean removeEldestEntry(java.util.Map.Entry eldest) {
125                             return this.size() > CACHE_SIZE;
126                         }
127                     };
128                 }
129                 
130                 topics.put(tid, posts);
131                 cache.add(FQN, topics);
132             }
133         }
134         
135         int size = posts.size();
136         return posts.subList(start, (size < start + count) ? size : start + count);
137    }
138     
139     public static void remove(int topicId, int postId)
140     {
141         synchronized (FQN) {
142             String JavaDoc tid = Integer.toString(topicId);
143             
144             List JavaDoc posts = (List JavaDoc)cache.get(FQN, tid);
145             
146             if (posts != null) {
147                 Post p = new Post();
148                 p.setId(postId);
149                 posts.remove(p);
150                 
151                 cache.add(FQN, tid, posts);
152             }
153         }
154     }
155     
156     public static void update(int topicId, Post p)
157     {
158         String JavaDoc tid = Integer.toString(topicId);
159         List JavaDoc posts = (List JavaDoc)cache.get(FQN, tid);
160         if (posts != null && posts.contains(p)) {
161             posts.set(posts.indexOf(p), p);
162             cache.add(FQN, tid, posts);
163         }
164     }
165     
166     public static void append(int topicId, Post p)
167     {
168         String JavaDoc tid = Integer.toString(topicId);
169         List JavaDoc posts = (List JavaDoc)cache.get(FQN, tid);
170         if (posts != null && !posts.contains(p)) {
171             posts.add(p);
172             cache.add(FQN, tid, posts);
173         }
174     }
175     
176     public static void clearCache(int topicId)
177     {
178         cache.remove(FQN, Integer.toString(topicId));
179     }
180 }
181
182
Popular Tags