Source Code Cross Referenced for TopicRepository.java in  » Forum » JForum-2.1.8 » net » jforum » repository » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Forum » JForum 2.1.8 » net.jforum.repository 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright (c) JForum Team
003:         * All rights reserved.
004:         * 
005:         * Redistribution and use in source and binary forms, 
006:         * with or without modification, are permitted provided 
007:         * that the following conditions are met:
008:         * 
009:         * 1) Redistributions of source code must retain the above 
010:         * copyright notice, this list of conditions and the 
011:         * following  disclaimer.
012:         * 2)  Redistributions in binary form must reproduce the 
013:         * above copyright notice, this list of conditions and 
014:         * the following disclaimer in the documentation and/or 
015:         * other materials provided with the distribution.
016:         * 3) Neither the name of "Rafael Steil" nor 
017:         * the names of its contributors may be used to endorse 
018:         * or promote products derived from this software without 
019:         * specific prior written permission.
020:         * 
021:         * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT 
022:         * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
023:         * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
024:         * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
025:         * MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
026:         * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
027:         * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 
028:         * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
029:         * EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
030:         * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
031:         * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
032:         * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
033:         * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 
034:         * IN CONTRACT, STRICT LIABILITY, OR TORT 
035:         * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
036:         * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
037:         * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038:         * 
039:         * This file creation date: 05/04/2004 - 20:11:44
040:         * The JForum Project
041:         * http://www.jforum.net
042:         */
043:        package net.jforum.repository;
044:
045:        import java.util.ArrayList;
046:        import java.util.Collections;
047:        import java.util.Comparator;
048:        import java.util.HashMap;
049:        import java.util.Iterator;
050:        import java.util.LinkedList;
051:        import java.util.List;
052:        import java.util.Map;
053:
054:        import net.jforum.cache.CacheEngine;
055:        import net.jforum.cache.Cacheable;
056:        import net.jforum.dao.DataAccessDriver;
057:        import net.jforum.dao.TopicDAO;
058:        import net.jforum.entities.Topic;
059:        import net.jforum.entities.TopicTypeComparator;
060:        import net.jforum.util.preferences.ConfigKeys;
061:        import net.jforum.util.preferences.SystemGlobals;
062:
063:        /**
064:         * Repository for the last n topics for each forum.
065:         * 
066:         * @author Rafael Steil
067:         * @author James Yong
068:         * @version $Id: TopicRepository.java,v 1.33 2007/09/05 04:00:27 rafaelsteil Exp $
069:         */
070:        public class TopicRepository implements  Cacheable {
071:            private static int maxItems = SystemGlobals
072:                    .getIntValue(ConfigKeys.TOPICS_PER_PAGE);
073:
074:            private static final String FQN = "topics";
075:            private static final String RECENT = "recent";
076:            private static final String HOTTEST = "hottest";
077:            private static final String FQN_FORUM = FQN + "/byforum";
078:            private static final String RELATION = "relation";
079:            private static final String FQN_LOADED = FQN + "/loaded";
080:            private static final Comparator TYPE_COMPARATOR = new TopicTypeComparator();
081:
082:            private static CacheEngine cache;
083:
084:            /**
085:             * @see net.jforum.cache.Cacheable#setCacheEngine(net.jforum.cache.CacheEngine)
086:             */
087:            public void setCacheEngine(CacheEngine engine) {
088:                cache = engine;
089:            }
090:
091:            public static boolean isLoaded(int forumId) {
092:                return "1".equals(cache.get(FQN_LOADED, Integer
093:                        .toString(forumId)));
094:            }
095:
096:            /**
097:             * Add topic to the FIFO stack
098:             * 
099:             * @param topic The topic to add to stack
100:             */
101:            public synchronized static void pushTopic(Topic topic) {
102:                if (SystemGlobals.getBoolValue(ConfigKeys.TOPIC_CACHE_ENABLED)) {
103:                    int limit = SystemGlobals
104:                            .getIntValue(ConfigKeys.RECENT_TOPICS);
105:
106:                    LinkedList l = (LinkedList) cache.get(FQN, RECENT);
107:                    if (l == null || l.size() == 0) {
108:                        l = new LinkedList(loadMostRecentTopics());
109:                    }
110:
111:                    l.remove(topic);
112:                    l.addFirst(topic);
113:
114:                    while (l.size() > limit) {
115:                        l.removeLast();
116:                    }
117:
118:                    cache.add(FQN, RECENT, l);
119:                }
120:            }
121:
122:            /**
123:             * Get all cached recent topics. 
124:             * 
125:             */
126:            public static List getRecentTopics() {
127:                List l = (List) cache.get(FQN, RECENT);
128:
129:                if (l == null
130:                        || l.size() == 0
131:                        || !SystemGlobals
132:                                .getBoolValue(ConfigKeys.TOPIC_CACHE_ENABLED)) {
133:                    l = loadMostRecentTopics();
134:                }
135:
136:                return new ArrayList(l);
137:            }
138:
139:            /**
140:             * Get all cached hottest topics.
141:             *
142:             */
143:            public static List getHottestTopics() {
144:                List l = (List) cache.get(FQN, HOTTEST);
145:
146:                if (l == null
147:                        || l.size() == 0
148:                        || !SystemGlobals
149:                                .getBoolValue(ConfigKeys.TOPIC_CACHE_ENABLED)) {
150:                    l = loadHottestTopics();
151:                }
152:
153:                return new ArrayList(l);
154:            }
155:
156:            /**
157:             * Add recent topics to the cache
158:             */
159:            public synchronized static List loadMostRecentTopics() {
160:                TopicDAO tm = DataAccessDriver.getInstance().newTopicDAO();
161:                int limit = SystemGlobals.getIntValue(ConfigKeys.RECENT_TOPICS);
162:
163:                List l = tm.selectRecentTopics(limit);
164:                cache.add(FQN, RECENT, new LinkedList(l));
165:
166:                return l;
167:            }
168:
169:            /**
170:             * Add hottest topics to the cache
171:             */
172:            public synchronized static List loadHottestTopics() {
173:                TopicDAO tm = DataAccessDriver.getInstance().newTopicDAO();
174:                int limit = SystemGlobals
175:                        .getIntValue(ConfigKeys.HOTTEST_TOPICS);
176:
177:                List l = tm.selectHottestTopics(limit);
178:                cache.add(FQN, HOTTEST, new LinkedList(l));
179:
180:                return l;
181:            }
182:
183:            /**
184:             * Add topics to the cache
185:             * 
186:             * @param forumId The forum id to which the topics are related
187:             * @param topics The topics to add
188:             */
189:            public static void addAll(int forumId, List topics) {
190:                if (SystemGlobals.getBoolValue(ConfigKeys.TOPIC_CACHE_ENABLED)) {
191:                    synchronized (FQN_FORUM) {
192:                        cache.add(FQN_FORUM, Integer.toString(forumId),
193:                                new LinkedList(topics));
194:
195:                        Map m = (Map) cache.get(FQN, RELATION);
196:
197:                        if (m == null) {
198:                            m = new HashMap();
199:                        }
200:
201:                        Integer fId = new Integer(forumId);
202:
203:                        for (Iterator iter = topics.iterator(); iter.hasNext();) {
204:                            Topic t = (Topic) iter.next();
205:
206:                            m.put(new Integer(t.getId()), fId);
207:                        }
208:
209:                        cache.add(FQN, RELATION, m);
210:                        cache.add(FQN_LOADED, Integer.toString(forumId), "1");
211:                    }
212:                }
213:            }
214:
215:            /**
216:             * Clears the cache
217:             * 
218:             * @param forumId The forum id to clear the cache
219:             */
220:            public static void clearCache(int forumId) {
221:                synchronized (FQN_FORUM) {
222:                    cache.add(FQN_FORUM, Integer.toString(forumId),
223:                            new LinkedList());
224:                    cache.remove(FQN, RELATION);
225:                }
226:            }
227:
228:            /**
229:             * Adds a new topic to the cache
230:             * 
231:             * @param topic The topic to add
232:             */
233:            public static void addTopic(Topic topic) {
234:                if (!SystemGlobals.getBoolValue(ConfigKeys.TOPIC_CACHE_ENABLED)) {
235:                    return;
236:                }
237:
238:                synchronized (FQN_FORUM) {
239:                    String forumId = Integer.toString(topic.getForumId());
240:                    LinkedList list = (LinkedList) cache
241:                            .get(FQN_FORUM, forumId);
242:
243:                    if (list == null) {
244:                        list = new LinkedList();
245:                        list.add(topic);
246:                    } else {
247:                        boolean contains = list.contains(topic);
248:
249:                        // If the cache is full, remove the eldest element
250:                        if (!contains && list.size() + 1 > maxItems) {
251:                            list.removeLast();
252:                        } else if (contains) {
253:                            list.remove(topic);
254:                        }
255:
256:                        list.add(topic);
257:
258:                        Collections.sort(list, TYPE_COMPARATOR);
259:                    }
260:
261:                    cache.add(FQN_FORUM, forumId, list);
262:
263:                    Map m = (Map) cache.get(FQN, RELATION);
264:
265:                    if (m == null) {
266:                        m = new HashMap();
267:                    }
268:
269:                    m.put(new Integer(topic.getId()), new Integer(forumId));
270:
271:                    cache.add(FQN, RELATION, m);
272:                }
273:            }
274:
275:            /**
276:             * Updates a cached topic
277:             * 
278:             * @param topic The topic to update
279:             */
280:            public static void updateTopic(Topic topic) {
281:                if (SystemGlobals.getBoolValue(ConfigKeys.TOPIC_CACHE_ENABLED)) {
282:                    synchronized (FQN_FORUM) {
283:                        String forumId = Integer.toString(topic.getForumId());
284:                        List l = (List) cache.get(FQN_FORUM, forumId);
285:
286:                        if (l != null) {
287:                            int index = l.indexOf(topic);
288:
289:                            if (index > -1) {
290:                                l.set(index, topic);
291:                                cache.add(FQN_FORUM, forumId, l);
292:                            }
293:                        }
294:                    }
295:                }
296:            }
297:
298:            /**
299:             * Gets a cached topic.
300:             * 
301:             * @param t The topic to try to get from the cache. The instance
302:             * passed as argument should have ae least the topicId and forumId set
303:             * @return The topic instance, if found, or <code>null</code> otherwise. 
304:             */
305:            public static Topic getTopic(Topic t) {
306:                if (!SystemGlobals.getBoolValue(ConfigKeys.TOPIC_CACHE_ENABLED)) {
307:                    return null;
308:                }
309:
310:                if (t.getForumId() == 0) {
311:                    Map m = (Map) cache.get(FQN, RELATION);
312:
313:                    if (m != null) {
314:                        Integer forumId = (Integer) m
315:                                .get(new Integer(t.getId()));
316:
317:                        if (forumId != null) {
318:                            t.setForumId(forumId.intValue());
319:                        }
320:                    }
321:
322:                    if (t.getForumId() == 0) {
323:                        return null;
324:                    }
325:                }
326:
327:                List l = (List) cache.get(FQN_FORUM, Integer.toString(t
328:                        .getForumId()));
329:
330:                int index = -1;
331:
332:                if (l != null) {
333:                    index = l.indexOf(t);
334:                }
335:
336:                return (index == -1 ? null : (Topic) l.get(index));
337:            }
338:
339:            /**
340:             * Checks if a topic is cached
341:             * 
342:             * @param topic The topic to verify
343:             * @return <code>true</code> if the topic is cached, or <code>false</code> if not.
344:             */
345:            public static boolean isTopicCached(Topic topic) {
346:                if (!SystemGlobals.getBoolValue(ConfigKeys.TOPIC_CACHE_ENABLED)) {
347:                    return false;
348:                }
349:
350:                String forumId = Integer.toString(topic.getForumId());
351:                List list = (List) cache.get(FQN_FORUM, forumId);
352:
353:                return list == null ? false : list.contains(topic);
354:            }
355:
356:            /**
357:             * Get all cached topics related to a forum. 
358:             * 
359:             * @param forumid The forum id 
360:             * @return <code>ArrayList</code> with the topics.
361:             */
362:            public static List getTopics(int forumid) {
363:                if (SystemGlobals.getBoolValue(ConfigKeys.TOPIC_CACHE_ENABLED)) {
364:                    synchronized (FQN_FORUM) {
365:                        List returnList = (List) cache.get(FQN_FORUM, Integer
366:                                .toString(forumid));
367:
368:                        if (returnList == null) {
369:                            return new ArrayList();
370:                        }
371:
372:                        return new ArrayList(returnList);
373:                    }
374:                }
375:
376:                return new ArrayList();
377:            }
378:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.