/modules/enterprise/gui/coregui/src/main/java/org/rhq/enterprise/gui/coregui/client/dashboard/PortletFactory.java

https://github.com/ccrouch/rhq · Java · 288 lines · 204 code · 35 blank · 49 comment · 10 complexity · b709b101bc8db68de6aec4649ba4dfbd MD5 · raw file

  1. /*
  2. * RHQ Management Platform
  3. * Copyright (C) 2005-2010 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation version 2 of the License.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. package org.rhq.enterprise.gui.coregui.client.dashboard;
  20. import java.util.HashMap;
  21. import java.util.Iterator;
  22. import java.util.LinkedHashMap;
  23. import java.util.TreeMap;
  24. import com.smartgwt.client.widgets.Canvas;
  25. import com.smartgwt.client.widgets.Label;
  26. import org.rhq.core.domain.common.EntityContext;
  27. import org.rhq.core.domain.dashboard.DashboardPortlet;
  28. import org.rhq.enterprise.gui.coregui.client.CoreGUI;
  29. import org.rhq.enterprise.gui.coregui.client.ImageManager;
  30. import org.rhq.enterprise.gui.coregui.client.dashboard.portlets.groups.GroupAlertsPortlet;
  31. import org.rhq.enterprise.gui.coregui.client.dashboard.portlets.groups.GroupBundleDeploymentsPortlet;
  32. import org.rhq.enterprise.gui.coregui.client.dashboard.portlets.groups.GroupConfigurationUpdatesPortlet;
  33. import org.rhq.enterprise.gui.coregui.client.dashboard.portlets.groups.GroupEventsPortlet;
  34. import org.rhq.enterprise.gui.coregui.client.dashboard.portlets.groups.GroupMetricsPortlet;
  35. import org.rhq.enterprise.gui.coregui.client.dashboard.portlets.groups.GroupOobsPortlet;
  36. import org.rhq.enterprise.gui.coregui.client.dashboard.portlets.groups.GroupOperationsPortlet;
  37. import org.rhq.enterprise.gui.coregui.client.dashboard.portlets.groups.GroupPkgHistoryPortlet;
  38. import org.rhq.enterprise.gui.coregui.client.dashboard.portlets.inventory.groups.graph.ResourceGroupGraphPortlet;
  39. import org.rhq.enterprise.gui.coregui.client.dashboard.portlets.inventory.queue.AutodiscoveryPortlet;
  40. import org.rhq.enterprise.gui.coregui.client.dashboard.portlets.inventory.resource.FavoriteResourcesPortlet;
  41. import org.rhq.enterprise.gui.coregui.client.dashboard.portlets.inventory.resource.graph.ResourceGraphPortlet;
  42. import org.rhq.enterprise.gui.coregui.client.dashboard.portlets.platform.PlatformSummaryPortlet;
  43. import org.rhq.enterprise.gui.coregui.client.dashboard.portlets.recent.alerts.RecentAlertsPortlet;
  44. import org.rhq.enterprise.gui.coregui.client.dashboard.portlets.recent.imported.RecentlyAddedResourcesPortlet;
  45. import org.rhq.enterprise.gui.coregui.client.dashboard.portlets.recent.operations.OperationHistoryPortlet;
  46. import org.rhq.enterprise.gui.coregui.client.dashboard.portlets.recent.operations.OperationSchedulePortlet;
  47. import org.rhq.enterprise.gui.coregui.client.dashboard.portlets.recent.problems.ProblemResourcesPortlet;
  48. import org.rhq.enterprise.gui.coregui.client.dashboard.portlets.resource.ResourceAlertsPortlet;
  49. import org.rhq.enterprise.gui.coregui.client.dashboard.portlets.resource.ResourceBundleDeploymentsPortlet;
  50. import org.rhq.enterprise.gui.coregui.client.dashboard.portlets.resource.ResourceConfigurationUpdatesPortlet;
  51. import org.rhq.enterprise.gui.coregui.client.dashboard.portlets.resource.ResourceEventsPortlet;
  52. import org.rhq.enterprise.gui.coregui.client.dashboard.portlets.resource.ResourceMetricsPortlet;
  53. import org.rhq.enterprise.gui.coregui.client.dashboard.portlets.resource.ResourceOobsPortlet;
  54. import org.rhq.enterprise.gui.coregui.client.dashboard.portlets.resource.ResourceOperationsPortlet;
  55. import org.rhq.enterprise.gui.coregui.client.dashboard.portlets.resource.ResourcePkgHistoryPortlet;
  56. import org.rhq.enterprise.gui.coregui.client.dashboard.portlets.summary.InventorySummaryPortlet;
  57. import org.rhq.enterprise.gui.coregui.client.dashboard.portlets.summary.TagCloudPortlet;
  58. import org.rhq.enterprise.gui.coregui.client.dashboard.portlets.util.MashupPortlet;
  59. import org.rhq.enterprise.gui.coregui.client.dashboard.portlets.util.MessagePortlet;
  60. import org.rhq.enterprise.gui.coregui.client.util.message.Message;
  61. import org.rhq.enterprise.gui.coregui.client.util.message.Message.Severity;
  62. /**
  63. * @author Simeon Pinder
  64. * @author Jay Shaughnessy
  65. */
  66. public class PortletFactory {
  67. private static final HashMap<String, PortletViewFactory> globalPortletFactoryMap;
  68. // although portlet names are I18N, they are assumed to be unique. This maps portlet names to portlet keys,
  69. // and the keyset is sorted for convenient display.
  70. private static final TreeMap<String, String> globalPortletNameMap;
  71. // although portlet names are I18N, they are assumed to be unique. This maps portlet keys to portlet names,
  72. // and is suitable for a sorted Menu value map.
  73. private static final LinkedHashMap<String, String> globalPortletMenuMap;
  74. //Group portlet registrations, diff from default portlets as only applicable for specific group
  75. private static final HashMap<String, PortletViewFactory> groupPortletFactoryMap;
  76. private static final TreeMap<String, String> groupPortletNameMap;
  77. private static final LinkedHashMap<String, String> groupPortletMenuMap;
  78. //Resource portlet registrations, diff from default portlets as only applicable for specific resource
  79. private static final HashMap<String, PortletViewFactory> resourcePortletFactoryMap;
  80. private static final TreeMap<String, String> resourcePortletNameMap;
  81. private static final LinkedHashMap<String, String> resourcePortletMenuMap;
  82. private static HashMap<String, String> portletIconMap;
  83. static {
  84. // GLOBAL Portlets
  85. // Map portlet keys to portlet factories
  86. globalPortletFactoryMap = new HashMap<String, PortletViewFactory>();
  87. globalPortletFactoryMap.put(InventorySummaryPortlet.KEY, InventorySummaryPortlet.Factory.INSTANCE);
  88. globalPortletFactoryMap.put(RecentlyAddedResourcesPortlet.KEY, RecentlyAddedResourcesPortlet.Factory.INSTANCE);
  89. globalPortletFactoryMap.put(PlatformSummaryPortlet.KEY, PlatformSummaryPortlet.Factory.INSTANCE);
  90. globalPortletFactoryMap.put(AutodiscoveryPortlet.KEY, AutodiscoveryPortlet.Factory.INSTANCE);
  91. globalPortletFactoryMap.put(RecentAlertsPortlet.KEY, RecentAlertsPortlet.Factory.INSTANCE);
  92. globalPortletFactoryMap.put(ResourceGraphPortlet.KEY, ResourceGraphPortlet.Factory.INSTANCE);
  93. globalPortletFactoryMap.put(ResourceGroupGraphPortlet.KEY, ResourceGroupGraphPortlet.Factory.INSTANCE);
  94. globalPortletFactoryMap.put(TagCloudPortlet.KEY, TagCloudPortlet.Factory.INSTANCE);
  95. globalPortletFactoryMap.put(FavoriteResourcesPortlet.KEY, FavoriteResourcesPortlet.Factory.INSTANCE);
  96. globalPortletFactoryMap.put(MashupPortlet.KEY, MashupPortlet.Factory.INSTANCE);
  97. globalPortletFactoryMap.put(MessagePortlet.KEY, MessagePortlet.Factory.INSTANCE);
  98. globalPortletFactoryMap.put(ProblemResourcesPortlet.KEY, ProblemResourcesPortlet.Factory.INSTANCE);
  99. globalPortletFactoryMap.put(OperationHistoryPortlet.KEY, OperationHistoryPortlet.Factory.INSTANCE);
  100. globalPortletFactoryMap.put(OperationSchedulePortlet.KEY, OperationSchedulePortlet.Factory.INSTANCE);
  101. // sorted map of portlet names to portlet keys
  102. globalPortletNameMap = new TreeMap<String, String>();
  103. globalPortletNameMap.put(InventorySummaryPortlet.NAME, InventorySummaryPortlet.KEY);
  104. globalPortletNameMap.put(RecentlyAddedResourcesPortlet.NAME, RecentlyAddedResourcesPortlet.KEY);
  105. globalPortletNameMap.put(PlatformSummaryPortlet.NAME, PlatformSummaryPortlet.KEY);
  106. globalPortletNameMap.put(AutodiscoveryPortlet.NAME, AutodiscoveryPortlet.KEY);
  107. globalPortletNameMap.put(RecentAlertsPortlet.NAME, RecentAlertsPortlet.KEY);
  108. globalPortletNameMap.put(ResourceGraphPortlet.NAME, ResourceGraphPortlet.KEY);
  109. globalPortletNameMap.put(ResourceGroupGraphPortlet.NAME, ResourceGroupGraphPortlet.KEY);
  110. globalPortletNameMap.put(TagCloudPortlet.NAME, TagCloudPortlet.KEY);
  111. globalPortletNameMap.put(FavoriteResourcesPortlet.NAME, FavoriteResourcesPortlet.KEY);
  112. globalPortletNameMap.put(MashupPortlet.NAME, MashupPortlet.KEY);
  113. globalPortletNameMap.put(MessagePortlet.NAME, MessagePortlet.KEY);
  114. globalPortletNameMap.put(ProblemResourcesPortlet.NAME, ProblemResourcesPortlet.KEY);
  115. globalPortletNameMap.put(OperationHistoryPortlet.NAME, OperationHistoryPortlet.KEY);
  116. globalPortletNameMap.put(OperationSchedulePortlet.NAME, OperationSchedulePortlet.KEY);
  117. globalPortletMenuMap = new LinkedHashMap<String, String>(globalPortletNameMap.size());
  118. for (Iterator<String> i = globalPortletNameMap.keySet().iterator(); i.hasNext();) {
  119. String portletName = i.next();
  120. globalPortletMenuMap.put(globalPortletNameMap.get(portletName), portletName);
  121. }
  122. // GROUP Portlets
  123. // Map portlet keys to portlet factories
  124. groupPortletFactoryMap = new HashMap<String, PortletViewFactory>();
  125. groupPortletFactoryMap.put(GroupAlertsPortlet.KEY, GroupAlertsPortlet.Factory.INSTANCE);
  126. groupPortletFactoryMap.put(GroupMetricsPortlet.KEY, GroupMetricsPortlet.Factory.INSTANCE);
  127. groupPortletFactoryMap.put(GroupOobsPortlet.KEY, GroupOobsPortlet.Factory.INSTANCE);
  128. groupPortletFactoryMap.put(GroupEventsPortlet.KEY, GroupEventsPortlet.Factory.INSTANCE);
  129. groupPortletFactoryMap.put(GroupOperationsPortlet.KEY, GroupOperationsPortlet.Factory.INSTANCE);
  130. groupPortletFactoryMap.put(GroupPkgHistoryPortlet.KEY, GroupPkgHistoryPortlet.Factory.INSTANCE);
  131. groupPortletFactoryMap.put(GroupBundleDeploymentsPortlet.KEY, GroupBundleDeploymentsPortlet.Factory.INSTANCE);
  132. groupPortletFactoryMap.put(GroupConfigurationUpdatesPortlet.KEY,
  133. GroupConfigurationUpdatesPortlet.Factory.INSTANCE);
  134. // sorted map of portlet names to portlet keys
  135. groupPortletNameMap = new TreeMap<String, String>();
  136. groupPortletNameMap.put(GroupAlertsPortlet.NAME, GroupAlertsPortlet.KEY);
  137. groupPortletNameMap.put(GroupMetricsPortlet.NAME, GroupMetricsPortlet.KEY);
  138. groupPortletNameMap.put(GroupOobsPortlet.NAME, GroupOobsPortlet.KEY);
  139. groupPortletNameMap.put(GroupEventsPortlet.NAME, GroupEventsPortlet.KEY);
  140. groupPortletNameMap.put(GroupOperationsPortlet.NAME, GroupOperationsPortlet.KEY);
  141. groupPortletNameMap.put(GroupPkgHistoryPortlet.NAME, GroupPkgHistoryPortlet.KEY);
  142. groupPortletNameMap.put(GroupBundleDeploymentsPortlet.NAME, GroupBundleDeploymentsPortlet.KEY);
  143. groupPortletNameMap.put(GroupConfigurationUpdatesPortlet.NAME, GroupConfigurationUpdatesPortlet.KEY);
  144. groupPortletMenuMap = new LinkedHashMap<String, String>(groupPortletNameMap.size());
  145. for (Iterator<String> i = groupPortletNameMap.keySet().iterator(); i.hasNext();) {
  146. String portletName = i.next();
  147. groupPortletMenuMap.put(groupPortletNameMap.get(portletName), portletName);
  148. }
  149. // Resource Portlets
  150. // Map portlet keys to portlet factories
  151. resourcePortletFactoryMap = new HashMap<String, PortletViewFactory>();
  152. resourcePortletFactoryMap.put(ResourceMetricsPortlet.KEY, ResourceMetricsPortlet.Factory.INSTANCE);
  153. resourcePortletFactoryMap.put(ResourceEventsPortlet.KEY, ResourceEventsPortlet.Factory.INSTANCE);
  154. resourcePortletFactoryMap.put(ResourceOobsPortlet.KEY, ResourceOobsPortlet.Factory.INSTANCE);
  155. resourcePortletFactoryMap.put(ResourceAlertsPortlet.KEY, ResourceAlertsPortlet.Factory.INSTANCE);
  156. resourcePortletFactoryMap.put(ResourceOperationsPortlet.KEY, ResourceOperationsPortlet.Factory.INSTANCE);
  157. resourcePortletFactoryMap.put(ResourcePkgHistoryPortlet.KEY, ResourcePkgHistoryPortlet.Factory.INSTANCE);
  158. resourcePortletFactoryMap.put(ResourceBundleDeploymentsPortlet.KEY,
  159. ResourceBundleDeploymentsPortlet.Factory.INSTANCE);
  160. resourcePortletFactoryMap.put(ResourceConfigurationUpdatesPortlet.KEY,
  161. ResourceConfigurationUpdatesPortlet.Factory.INSTANCE);
  162. // sorted map of portlet names to portlet keys
  163. resourcePortletNameMap = new TreeMap<String, String>();
  164. resourcePortletNameMap.put(ResourceMetricsPortlet.NAME, ResourceMetricsPortlet.KEY);
  165. resourcePortletNameMap.put(ResourceEventsPortlet.NAME, ResourceEventsPortlet.KEY);
  166. resourcePortletNameMap.put(ResourceOobsPortlet.NAME, ResourceOobsPortlet.KEY);
  167. resourcePortletNameMap.put(ResourceOperationsPortlet.NAME, ResourceOperationsPortlet.KEY);
  168. resourcePortletNameMap.put(ResourcePkgHistoryPortlet.NAME, ResourcePkgHistoryPortlet.KEY);
  169. resourcePortletNameMap.put(ResourceAlertsPortlet.NAME, ResourceAlertsPortlet.KEY);
  170. resourcePortletNameMap.put(ResourceBundleDeploymentsPortlet.NAME, ResourceBundleDeploymentsPortlet.KEY);
  171. resourcePortletNameMap.put(ResourceConfigurationUpdatesPortlet.NAME, ResourceConfigurationUpdatesPortlet.KEY);
  172. resourcePortletMenuMap = new LinkedHashMap<String, String>(resourcePortletNameMap.size());
  173. for (Iterator<String> i = resourcePortletNameMap.keySet().iterator(); i.hasNext();) {
  174. String portletName = i.next();
  175. resourcePortletMenuMap.put(resourcePortletNameMap.get(portletName), portletName);
  176. }
  177. //############## Portlet icon mappings ############################################
  178. //register portlet names
  179. portletIconMap = new HashMap<String, String>(globalPortletFactoryMap.size());
  180. portletIconMap.put(GroupAlertsPortlet.KEY, ImageManager.getAlertIcon());
  181. portletIconMap.put(ResourceAlertsPortlet.KEY, ImageManager.getAlertIcon());
  182. portletIconMap.put(GroupMetricsPortlet.KEY, ImageManager.getMonitorIcon());
  183. portletIconMap.put(ResourceMetricsPortlet.KEY, ImageManager.getMonitorIcon());
  184. portletIconMap.put(GroupOobsPortlet.KEY, ImageManager.getMonitorFailedIcon());
  185. portletIconMap.put(ResourceOobsPortlet.KEY, ImageManager.getMonitorFailedIcon());
  186. portletIconMap.put(GroupEventsPortlet.KEY, ImageManager.getEventIcon());
  187. portletIconMap.put(ResourceEventsPortlet.KEY, ImageManager.getEventIcon());
  188. portletIconMap.put(GroupOperationsPortlet.KEY, ImageManager.getOperationIcon());
  189. portletIconMap.put(ResourceOperationsPortlet.KEY, ImageManager.getOperationIcon());
  190. portletIconMap.put(GroupPkgHistoryPortlet.KEY, ImageManager.getActivityPackageIcon());
  191. portletIconMap.put(ResourcePkgHistoryPortlet.KEY, ImageManager.getActivityPackageIcon());
  192. portletIconMap.put(GroupBundleDeploymentsPortlet.KEY, ImageManager.getBundleIcon());
  193. portletIconMap.put(ResourceBundleDeploymentsPortlet.KEY, ImageManager.getBundleIcon());
  194. portletIconMap.put(GroupConfigurationUpdatesPortlet.KEY, ImageManager.getConfigureIcon());
  195. portletIconMap.put(ResourceConfigurationUpdatesPortlet.KEY, ImageManager.getConfigureIcon());
  196. }
  197. public static Portlet buildPortlet(String locatorId, PortletWindow portletWindow, DashboardPortlet storedPortlet,
  198. EntityContext context) {
  199. PortletViewFactory viewFactory = globalPortletFactoryMap.get(storedPortlet.getPortletKey());
  200. if (viewFactory == null) {//check group view factory
  201. viewFactory = groupPortletFactoryMap.get(storedPortlet.getPortletKey());
  202. if (viewFactory == null) {//check resource view factory
  203. viewFactory = resourcePortletFactoryMap.get(storedPortlet.getPortletKey());
  204. if (viewFactory == null) {
  205. Message msg = new Message("Bad portlet: " + storedPortlet, Severity.Warning);
  206. CoreGUI.getMessageCenter().notify(msg);
  207. class InvalidPortlet extends Label implements Portlet {
  208. InvalidPortlet() {
  209. super(CoreGUI.getMessages().view_portlet_factory_invalidPortlet());
  210. }
  211. @Override
  212. public Canvas getHelpCanvas() {
  213. return new Label(getContents());
  214. }
  215. @Override
  216. public void configure(PortletWindow portletWindow, DashboardPortlet storedPortlet) {
  217. }
  218. }
  219. ;
  220. return new InvalidPortlet();
  221. }
  222. }
  223. }
  224. Portlet view = viewFactory.getInstance(locatorId, context);
  225. view.configure(portletWindow, storedPortlet);
  226. //add code to initiate refresh cycle for portlets
  227. if (view instanceof AutoRefreshPortlet) {
  228. ((AutoRefreshPortlet) view).startRefreshCycle();
  229. }
  230. return view;
  231. }
  232. /**
  233. * @return Unprotected, make a copy if you need to alter the map entries.
  234. */
  235. public static LinkedHashMap<String, String> getGlobalPortletMenuMap() {
  236. return globalPortletMenuMap;
  237. }
  238. /**
  239. * @return Unprotected, make a copy if you need to alter the map entries.
  240. */
  241. public static LinkedHashMap<String, String> getGroupPortletMenuMap() {
  242. return groupPortletMenuMap;
  243. }
  244. /**
  245. * @return Unprotected, make a copy if you need to alter the map entries.
  246. */
  247. public static LinkedHashMap<String, String> getResourcePortletMenuMap() {
  248. return resourcePortletMenuMap;
  249. }
  250. public static String getRegisteredPortletIcon(String key) {
  251. return portletIconMap.get(key);
  252. }
  253. }