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.Maps; 023 import org.apache.commons.lang.ArrayUtils; 024 import org.apache.commons.lang.StringUtils; 025 import org.apache.commons.lang.builder.CompareToBuilder; 026 import org.apache.commons.lang.builder.EqualsBuilder; 027 import org.apache.commons.lang.builder.ToStringBuilder; 028 import org.sonar.api.utils.AnnotationUtils; 029 import org.sonar.api.web.*; 030 031 import java.util.Collection; 032 import java.util.Map; 033 034 @SuppressWarnings("rawtypes") 035 public class ViewProxy<V extends View> implements Comparable<ViewProxy> { 036 037 private V view; 038 private String[] sections = {NavigationSection.HOME}; 039 private String[] userRoles = {}; 040 private String[] resourceScopes = {}; 041 private String[] resourceQualifiers = {}; 042 private String[] resourceLanguages = {}; 043 private String[] defaultForMetrics = {}; 044 private String description = ""; 045 private Map<String, WidgetProperty> widgetPropertiesByKey = Maps.newHashMap(); 046 private String[] widgetCategories = {}; 047 private WidgetLayoutType widgetLayout = WidgetLayoutType.DEFAULT; 048 private boolean isDefaultTab = false; 049 private boolean isWidget = false; 050 private String[] mandatoryMeasures = {}; 051 private String[] needOneOfMeasures = {}; 052 053 public ViewProxy(final V view) { 054 this.view = view; 055 056 initUserRoles(view); 057 initSections(view); 058 initResourceScope(view); 059 initResourceQualifier(view); 060 initResourceLanguage(view); 061 initDefaultTabInfo(view); 062 initDescription(view); 063 initWidgetProperties(view); 064 initWidgetCategory(view); 065 initWidgetLayout(view); 066 initRequiredMeasures(view); 067 068 isWidget = (view instanceof Widget); 069 } 070 071 private void initRequiredMeasures(V view) { 072 RequiredMeasures requiredMeasuresAnnotation = AnnotationUtils.getClassAnnotation(view, RequiredMeasures.class); 073 if (requiredMeasuresAnnotation != null) { 074 mandatoryMeasures = requiredMeasuresAnnotation.allOf(); 075 needOneOfMeasures = requiredMeasuresAnnotation.anyOf(); 076 } 077 } 078 079 private void initWidgetLayout(final V view) { 080 WidgetLayout layoutAnnotation = AnnotationUtils.getClassAnnotation(view, WidgetLayout.class); 081 if (layoutAnnotation != null) { 082 widgetLayout = layoutAnnotation.value(); 083 } 084 } 085 086 private void initWidgetCategory(final V view) { 087 WidgetCategory categAnnotation = AnnotationUtils.getClassAnnotation(view, WidgetCategory.class); 088 if (categAnnotation != null) { 089 widgetCategories = categAnnotation.value(); 090 } 091 } 092 093 private void initWidgetProperties(final V view) { 094 WidgetProperties propAnnotation = AnnotationUtils.getClassAnnotation(view, WidgetProperties.class); 095 if (propAnnotation != null) { 096 for (WidgetProperty property : propAnnotation.value()) { 097 widgetPropertiesByKey.put(property.key(), property); 098 } 099 } 100 } 101 102 private void initDescription(final V view) { 103 Description descriptionAnnotation = AnnotationUtils.getClassAnnotation(view, Description.class); 104 if (descriptionAnnotation != null) { 105 description = descriptionAnnotation.value(); 106 } 107 } 108 109 private void initDefaultTabInfo(final V view) { 110 DefaultTab defaultTabAnnotation = AnnotationUtils.getClassAnnotation(view, DefaultTab.class); 111 if (defaultTabAnnotation != null) { 112 if (defaultTabAnnotation.metrics().length == 0) { 113 isDefaultTab = true; 114 defaultForMetrics = new String[0]; 115 116 } else { 117 isDefaultTab = false; 118 defaultForMetrics = defaultTabAnnotation.metrics(); 119 } 120 } 121 } 122 123 private void initResourceLanguage(final V view) { 124 ResourceLanguage languageAnnotation = AnnotationUtils.getClassAnnotation(view, ResourceLanguage.class); 125 if (languageAnnotation != null) { 126 resourceLanguages = languageAnnotation.value(); 127 } 128 } 129 130 private void initResourceQualifier(final V view) { 131 ResourceQualifier qualifierAnnotation = AnnotationUtils.getClassAnnotation(view, ResourceQualifier.class); 132 if (qualifierAnnotation != null) { 133 resourceQualifiers = qualifierAnnotation.value(); 134 } 135 } 136 137 private void initResourceScope(final V view) { 138 ResourceScope scopeAnnotation = AnnotationUtils.getClassAnnotation(view, ResourceScope.class); 139 if (scopeAnnotation != null) { 140 resourceScopes = scopeAnnotation.value(); 141 } 142 } 143 144 private void initSections(final V view) { 145 NavigationSection sectionAnnotation = AnnotationUtils.getClassAnnotation(view, NavigationSection.class); 146 if (sectionAnnotation != null) { 147 sections = sectionAnnotation.value(); 148 } 149 } 150 151 private void initUserRoles(final V view) { 152 UserRole userRoleAnnotation = AnnotationUtils.getClassAnnotation(view, UserRole.class); 153 if (userRoleAnnotation != null) { 154 userRoles = userRoleAnnotation.value(); 155 } 156 } 157 158 public V getTarget() { 159 return view; 160 } 161 162 public String getId() { 163 return view.getId(); 164 } 165 166 public boolean isController() { 167 String id = view.getId(); 168 return id !=null && id.length()>0 && id.charAt(0)=='/'; 169 } 170 171 public String getTitle() { 172 return view.getTitle(); 173 } 174 175 public String getDescription() { 176 return description; 177 } 178 179 public Collection<WidgetProperty> getWidgetProperties() { 180 return widgetPropertiesByKey.values(); 181 } 182 183 public WidgetProperty getWidgetProperty(String propertyKey) { 184 return widgetPropertiesByKey.get(propertyKey); 185 } 186 187 public String[] getWidgetCategories() { 188 return widgetCategories; 189 } 190 191 public String[] getSections() { 192 return sections; 193 } 194 195 public String[] getUserRoles() { 196 return userRoles; 197 } 198 199 public String[] getResourceScopes() { 200 return resourceScopes; 201 } 202 203 public String[] getResourceQualifiers() { 204 return resourceQualifiers; 205 } 206 207 public String[] getResourceLanguages() { 208 return resourceLanguages; 209 } 210 211 public boolean isDefaultTab() { 212 return isDefaultTab; 213 } 214 215 public String[] getDefaultTabForMetrics() { 216 return defaultForMetrics; 217 } 218 219 public boolean supportsMetric(String metricKey) { 220 return ArrayUtils.contains(defaultForMetrics, metricKey); 221 } 222 223 public boolean acceptsAvailableMeasures(String[] availableMeasures) { 224 for (String mandatoryMeasure : mandatoryMeasures) { 225 if (!ArrayUtils.contains(availableMeasures, mandatoryMeasure)) { 226 return false; 227 } 228 } 229 if (needOneOfMeasures.length == 0) { 230 return true; 231 } else { 232 for (String neededMeasure : needOneOfMeasures) { 233 if (ArrayUtils.contains(availableMeasures, neededMeasure)) { 234 return true; 235 } 236 } 237 return false; 238 } 239 } 240 241 public boolean isWidget() { 242 return isWidget; 243 } 244 245 public boolean isGwt() { 246 return view instanceof GwtPage; 247 } 248 249 public WidgetLayoutType getWidgetLayout() { 250 return widgetLayout; 251 } 252 253 public boolean isEditable() { 254 return !widgetPropertiesByKey.isEmpty(); 255 } 256 257 public boolean hasRequiredProperties() { 258 boolean requires = false; 259 for (WidgetProperty property : getWidgetProperties()) { 260 if (!property.optional() && StringUtils.isEmpty(property.defaultValue())) { 261 requires = true; 262 } 263 } 264 return requires; 265 } 266 267 @Override 268 public int hashCode() { 269 return getId().hashCode(); 270 } 271 272 @Override 273 public boolean equals(Object obj) { 274 if (obj == null) { 275 return false; 276 } 277 if (obj == this) { 278 return true; 279 } 280 if (obj.getClass() != getClass()) { 281 return false; 282 } 283 ViewProxy rhs = (ViewProxy) obj; 284 return new EqualsBuilder() 285 .append(getId(), rhs.getId()) 286 .isEquals(); 287 } 288 289 @Override 290 public String toString() { 291 return new ToStringBuilder(this) 292 .append("id", view.getId()) 293 .append("sections", sections) 294 .append("userRoles", userRoles) 295 .append("scopes", resourceScopes) 296 .append("qualifiers", resourceQualifiers) 297 .append("languages", resourceLanguages) 298 .append("metrics", defaultForMetrics) 299 .toString(); 300 301 } 302 303 public int compareTo(ViewProxy other) { 304 return new CompareToBuilder() 305 .append(getTitle(), other.getTitle()) 306 .append(getId(), other.getId()) 307 .toComparison(); 308 309 } 310 }