001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2012 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 com.google.common.collect.Maps;
024    import com.google.common.collect.Sets;
025    import org.apache.commons.lang.ArrayUtils;
026    import org.sonar.api.ServerComponent;
027    import org.sonar.api.web.Page;
028    import org.sonar.api.web.View;
029    import org.sonar.api.web.Widget;
030    
031    import java.util.List;
032    import java.util.Map;
033    import java.util.Set;
034    
035    public class Views implements ServerComponent {
036    
037      private Map<String, ViewProxy<Page>> pagesPerId = Maps.newHashMap();
038      private Set<ViewProxy<Page>> pages = Sets.newTreeSet();
039    
040      private Map<String, ViewProxy<Widget>> widgetsPerId = Maps.newHashMap();
041      private Set<ViewProxy<Widget>> widgets = Sets.newTreeSet();
042    
043      public Views() {
044      }
045    
046      public Views(View[] views) {
047        for (View view : DefaultPages.getPages()) {
048          register(view);
049        }
050        for (View view : views) {
051          register(view);
052        }
053      }
054    
055      private void register(View view) {
056        ViewProxy proxy = new ViewProxy(view);
057        if (view instanceof Widget) {
058          widgets.add(proxy);
059          widgetsPerId.put(proxy.getId(), proxy);
060    
061        } else if (view instanceof Page) {
062          pagesPerId.put(proxy.getId(), proxy);
063          pages.add(proxy);
064        }
065      }
066    
067    
068      public ViewProxy<Page> getPage(String id) {
069        return pagesPerId.get(id);
070      }
071    
072      public List<ViewProxy<Page>> getPages(String section) {
073        return getPages(section, null, null, null, null);
074      }
075    
076      public List<ViewProxy<Page>> getPages(String section, String resourceScope, String resourceQualifier, String resourceLanguage, String[] availableMeasures) {
077        List<ViewProxy<Page>> result = Lists.newArrayList();
078        for (ViewProxy<Page> proxy : pages) {
079          if (accept(proxy, section, resourceScope, resourceQualifier, resourceLanguage, availableMeasures)) {
080            result.add(proxy);
081          }
082        }
083        return result;
084      }
085    
086      public List<ViewProxy<Page>> getPagesForMetric(String section, String resourceScope, String resourceQualifier, String resourceLanguage,
087                                                     String[] availableMeasures, String metric) {
088        List<ViewProxy<Page>> result = Lists.newArrayList();
089        for (ViewProxy<Page> proxy : pages) {
090          if (accept(proxy, section, resourceScope, resourceQualifier, resourceLanguage, availableMeasures) && proxy.supportsMetric(metric)) {
091            result.add(proxy);
092          }
093        }
094        return result;
095      }
096    
097      public ViewProxy<Widget> getWidget(String id) {
098        return widgetsPerId.get(id);
099      }
100    
101      public List<ViewProxy<Widget>> getWidgets(String resourceScope, String resourceQualifier, String resourceLanguage, String[] availableMeasures) {
102        List<ViewProxy<Widget>> result = Lists.newArrayList();
103        for (ViewProxy<Widget> proxy : widgets) {
104          if (accept(proxy, null, resourceScope, resourceQualifier, resourceLanguage, availableMeasures)) {
105            result.add(proxy);
106          }
107        }
108        return result;
109      }
110    
111      public List<ViewProxy<Widget>> getWidgets() {
112        return Lists.newArrayList(widgets);
113      }
114    
115      protected static boolean accept(ViewProxy proxy, String section, String resourceScope, String resourceQualifier, String resourceLanguage, String[] availableMeasures) {
116        return acceptNavigationSection(proxy, section)
117          && acceptResourceScope(proxy, resourceScope)
118          && acceptResourceQualifier(proxy, resourceQualifier)
119          && acceptResourceLanguage(proxy, resourceLanguage)
120          && acceptAvailableMeasures(proxy, availableMeasures);
121      }
122    
123      protected static boolean acceptResourceLanguage(ViewProxy proxy, String resourceLanguage) {
124        return resourceLanguage == null || ArrayUtils.isEmpty(proxy.getResourceLanguages()) || ArrayUtils.contains(proxy.getResourceLanguages(), resourceLanguage);
125      }
126    
127      protected static boolean acceptResourceScope(ViewProxy proxy, String resourceScope) {
128        return resourceScope == null || ArrayUtils.isEmpty(proxy.getResourceScopes()) || ArrayUtils.contains(proxy.getResourceScopes(), resourceScope);
129      }
130    
131      protected static boolean acceptResourceQualifier(ViewProxy proxy, String resourceQualifier) {
132        return resourceQualifier == null || ArrayUtils.isEmpty(proxy.getResourceQualifiers()) || ArrayUtils.contains(proxy.getResourceQualifiers(), resourceQualifier);
133      }
134    
135      protected static boolean acceptNavigationSection(ViewProxy proxy, String section) {
136        return proxy.isWidget() || section == null || ArrayUtils.contains(proxy.getSections(), section);
137      }
138    
139      protected static boolean acceptAvailableMeasures(ViewProxy proxy, String[] availableMeasures) {
140        return availableMeasures == null || proxy.acceptsAvailableMeasures(availableMeasures);
141      }
142    }