001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2011 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * Sonar is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
019 */
020 package org.sonar.server.ui;
021
022 import com.google.common.collect.Lists;
023 import org.apache.commons.lang.ArrayUtils;
024 import org.sonar.api.ServerComponent;
025 import org.sonar.api.web.Page;
026 import org.sonar.api.web.View;
027 import org.sonar.api.web.Widget;
028
029 import java.util.*;
030
031 public class Views implements ServerComponent {
032
033 private Map<String, ViewProxy<Page>> pagesPerId = new HashMap<String, ViewProxy<Page>>();
034 private Set<ViewProxy<Page>> pages = new TreeSet<ViewProxy<Page>>();
035
036 private Map<String, ViewProxy<Widget>> widgetsPerId = new HashMap<String, ViewProxy<Widget>>();
037 private Set<ViewProxy<Widget>> widgets = new TreeSet<ViewProxy<Widget>>();
038
039 public Views() {
040 }
041
042 public Views(View[] views) {
043 for (View view : DefaultPages.getPages()) {
044 register(view);
045 }
046 for (View view : views) {
047 register(view);
048 }
049 }
050
051 private void register(View view) {
052 ViewProxy proxy = new ViewProxy(view);
053 if (view instanceof Widget) {
054 widgets.add(proxy);
055 widgetsPerId.put(proxy.getId(), proxy);
056
057 } else if (view instanceof Page) {
058 pagesPerId.put(proxy.getId(), proxy);
059 pages.add(proxy);
060 }
061 }
062
063
064 public ViewProxy<Page> getPage(String id) {
065 return pagesPerId.get(id);
066 }
067
068 public List<ViewProxy<Page>> getPages(String section) {
069 return getPages(section, null, null, null);
070 }
071
072 public List<ViewProxy<Page>> getPages(String section, String resourceScope, String resourceQualifier, String resourceLanguage) {
073 List<ViewProxy<Page>> result = Lists.newArrayList();
074 for (ViewProxy<Page> proxy : pages) {
075 if (accept(proxy, section, resourceScope, resourceQualifier, resourceLanguage)) {
076 result.add(proxy);
077 }
078 }
079 return result;
080 }
081
082 public List<ViewProxy<Page>> getPagesForMetric(String section, String resourceScope, String resourceQualifier, String resourceLanguage, String metric) {
083 List<ViewProxy<Page>> result = Lists.newArrayList();
084 for (ViewProxy<Page> proxy : pages) {
085 if (accept(proxy, section, resourceScope, resourceQualifier, resourceLanguage) && proxy.supportsMetric(metric)) {
086 result.add(proxy);
087 }
088 }
089 return result;
090 }
091
092 public ViewProxy<Widget> getWidget(String id) {
093 return widgetsPerId.get(id);
094 }
095
096 public List<ViewProxy<Widget>> getWidgets(String resourceScope, String resourceQualifier, String resourceLanguage) {
097 List<ViewProxy<Widget>> result = Lists.newArrayList();
098 for (ViewProxy<Widget> proxy : widgets) {
099 if (accept(proxy, null, resourceScope, resourceQualifier, resourceLanguage)) {
100 result.add(proxy);
101 }
102 }
103 return result;
104 }
105
106 public List<ViewProxy<Widget>> getWidgets() {
107 return Lists.newArrayList(widgets);
108 }
109
110 private static boolean accept(ViewProxy proxy, String section, String resourceScope, String resourceQualifier, String resourceLanguage) {
111 return acceptNavigationSection(proxy, section)
112 && acceptResourceScope(proxy, resourceScope)
113 && acceptResourceQualifier(proxy, resourceQualifier)
114 && acceptResourceLanguage(proxy, resourceLanguage);
115 }
116
117 protected static boolean acceptResourceLanguage(ViewProxy proxy, String resourceLanguage) {
118 return resourceLanguage== null || ArrayUtils.isEmpty(proxy.getResourceLanguages()) || ArrayUtils.contains(proxy.getResourceLanguages(), resourceLanguage);
119 }
120
121 protected static boolean acceptResourceScope(ViewProxy proxy, String resourceScope) {
122 return resourceScope== null || ArrayUtils.isEmpty(proxy.getResourceScopes()) || ArrayUtils.contains(proxy.getResourceScopes(), resourceScope);
123 }
124
125 protected static boolean acceptResourceQualifier(ViewProxy proxy, String resourceQualifier) {
126 return resourceQualifier== null || ArrayUtils.isEmpty(proxy.getResourceQualifiers()) || ArrayUtils.contains(proxy.getResourceQualifiers(), resourceQualifier);
127 }
128
129 protected static boolean acceptNavigationSection(ViewProxy proxy, String section) {
130 return proxy.isWidget() || section == null || ArrayUtils.contains(proxy.getSections(), section);
131 }
132 }