1 9 package org.jboss.portlet.forums.security; 10 11 import java.util.ArrayList ; 12 import java.util.HashMap ; 13 import java.util.Iterator ; 14 import java.util.List ; 15 import java.util.Map ; 16 import java.util.ResourceBundle ; 17 18 import javax.servlet.ServletContext ; 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 31 public class ForumsModelContentProvider implements ModelContentProvider 32 { 33 34 private Map map; 35 private Item rootItem; 36 private ForumsModule forumsModule; 37 private ResourceBundle bundle; 38 39 public void init(ServletContext servletContext) { 40 forumsModule = (ForumsModule) servletContext.getAttribute("forumsModule"); 41 rootItem = new RootItemImpl(); 42 } 43 44 public Item getItem(String [] 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 getChildren(String [] path) { 67 updateMap(); 68 if ((path == null) || (path.length == 0)) 69 { 70 List list = new ArrayList (); 71 Iterator 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 ) 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 } 100 101 private void updateMap() 102 { 103 try 104 { 105 List categories = forumsModule.findCategories(); 106 map = new HashMap (); 107 Iterator it = categories.iterator(); 108 while (it.hasNext()) 109 { 110 Map forumsMap = null; 111 forumsMap = new HashMap (); 112 Category category = (Category)it.next(); 113 CategoryItemImpl categoryItemImpl = new CategoryItemImpl(bundle, category.getTitle()); 114 List forums = forumsModule.findForumsByCategoryID(category.getID()); 115 Iterator 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 forumsMap; 134 135 MapItem(Item categoryItem, Map forumsMap) 136 { 137 this.categoryItem = categoryItem; 138 this.forumsMap = forumsMap; 139 } 140 141 144 public Item getCategoryItem() 145 { 146 return categoryItem; 147 } 148 151 public Map getForumsMap() 152 { 153 return forumsMap; 154 } 155 } 156 157 } 158 | Popular Tags |