KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portlet > forums > security > ForumsModelContentProvider


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portlet.forums.security;
10
11 import java.util.ArrayList JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.List JavaDoc;
15 import java.util.Map JavaDoc;
16 import java.util.ResourceBundle JavaDoc;
17
18 import javax.servlet.ServletContext JavaDoc;
19
20 import org.jboss.portal.core.modules.ModuleException;
21 import org.jboss.portal.core.plugins.security.ModelContentProvider;
22 import org.jboss.portal.core.security.Item;
23 import org.jboss.portlet.forums.ForumsModule;
24 import org.jboss.portlet.forums.model.Category;
25 import org.jboss.portlet.forums.model.Forum;
26
27 /**
28  * @author <a HREF="mailto:theute@jboss.org">Thomas Heute </a>
29  * @version $Revision: 1.2 $
30  */

31 public class ForumsModelContentProvider implements ModelContentProvider
32 {
33     
34     private Map JavaDoc map;
35     private Item rootItem;
36     private ForumsModule forumsModule;
37     private ResourceBundle JavaDoc bundle;
38     
39     public void init(ServletContext JavaDoc servletContext) {
40         forumsModule = (ForumsModule) servletContext.getAttribute("forumsModule");
41         rootItem = new RootItemImpl();
42     }
43
44     public Item getItem(String JavaDoc[] path) {
45         updateMap();
46         if ((path == null) || (path.length == 0))
47         {
48            return rootItem;
49         }
50         else if (path.length == 1)
51         {
52            MapItem mapItem = (MapItem) map.get(path[0]);
53            return mapItem.categoryItem;
54         }
55         else if (path.length == 2)
56         {
57            MapItem mapItem = (MapItem) map.get(path[0]);
58            return (Item) mapItem.forumsMap.get(path[1]);
59         }
60         else
61         {
62            return null;
63         }
64     }
65
66     public Iterator JavaDoc getChildren(String JavaDoc[] path) {
67         updateMap();
68         if ((path == null) || (path.length == 0))
69         {
70            List JavaDoc list = new ArrayList JavaDoc();
71            Iterator JavaDoc it = map.values().iterator();
72            while (it.hasNext())
73            {
74               Item item = ((MapItem)it.next()).getCategoryItem();
75               list.add(item);
76            }
77            return list.iterator();
78         }
79         else if (path.length == 1)
80         {
81            MapItem mapItem = (MapItem) map.get(path[0]);
82            if (mapItem != null)
83            {
84               return ((Map JavaDoc) mapItem.getForumsMap()).values().iterator();
85            }
86            else
87            {
88               return null;
89            }
90         }
91         else
92         {
93            return null;
94         }
95     }
96
97     public void destroy() {
98         // TODO Auto-generated method stub
99
}
100     
101     private void updateMap()
102     {
103         try
104         {
105            List JavaDoc categories = forumsModule.findCategories();
106            map = new HashMap JavaDoc();
107            Iterator JavaDoc it = categories.iterator();
108            while (it.hasNext())
109            {
110               Map JavaDoc forumsMap = null;
111               forumsMap = new HashMap JavaDoc();
112               Category category = (Category)it.next();
113               CategoryItemImpl categoryItemImpl = new CategoryItemImpl(bundle, category.getTitle());
114               List JavaDoc forums = forumsModule.findForumsByCategoryID(category.getID());
115               Iterator JavaDoc it2 = forums.iterator();
116               while (it2.hasNext())
117               {
118                  Forum forum = (Forum)it2.next();
119                  forumsMap.put(forum.getName(), new ForumItemImpl(bundle, forum.getName()));
120               }
121               map.put(category.getTitle(), new MapItem(categoryItemImpl, forumsMap));
122            }
123         }
124         catch (ModuleException e)
125         {
126            e.printStackTrace();
127         }
128     }
129
130     public class MapItem {
131         
132         private Item categoryItem;
133         private Map JavaDoc forumsMap;
134         
135         MapItem(Item categoryItem, Map JavaDoc forumsMap)
136         {
137            this.categoryItem = categoryItem;
138            this.forumsMap = forumsMap;
139         }
140         
141         /**
142          * @return Returns the categoryItem.
143          */

144         public Item getCategoryItem()
145         {
146            return categoryItem;
147         }
148         /**
149          * @return Returns the forumsMap.
150          */

151         public Map JavaDoc getForumsMap()
152         {
153            return forumsMap;
154         }
155      }
156     
157 }
158
Popular Tags