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.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 public class ViewProxy<V extends View> implements Comparable<ViewProxy> {
035
036 private V view;
037 private String[] sections = {NavigationSection.HOME};
038 private String[] userRoles = {};
039 private String[] resourceScopes = {};
040 private String[] resourceQualifiers = {};
041 private String[] resourceLanguages = {};
042 private String[] defaultForMetrics = {};
043 private String description = "";
044 private Map<String, WidgetProperty> widgetPropertiesByKey = Maps.newHashMap();
045 private String[] widgetCategories = {};
046 private WidgetLayoutType widgetLayout = WidgetLayoutType.DEFAULT;
047 private boolean isDefaultTab = false;
048 private boolean isWidget = false;
049
050 public ViewProxy(final V view) {
051 this.view = view;
052
053 UserRole userRoleAnnotation = AnnotationUtils.getClassAnnotation(view, UserRole.class);
054 if (userRoleAnnotation != null) {
055 userRoles = userRoleAnnotation.value();
056 }
057
058 NavigationSection sectionAnnotation = AnnotationUtils.getClassAnnotation(view, NavigationSection.class);
059 if (sectionAnnotation != null) {
060 sections = sectionAnnotation.value();
061 }
062
063 ResourceScope scopeAnnotation = AnnotationUtils.getClassAnnotation(view, ResourceScope.class);
064 if (scopeAnnotation != null) {
065 resourceScopes = scopeAnnotation.value();
066 }
067
068 ResourceQualifier qualifierAnnotation = AnnotationUtils.getClassAnnotation(view, ResourceQualifier.class);
069 if (qualifierAnnotation != null) {
070 resourceQualifiers = qualifierAnnotation.value();
071 }
072
073 ResourceLanguage languageAnnotation = AnnotationUtils.getClassAnnotation(view, ResourceLanguage.class);
074 if (languageAnnotation != null) {
075 resourceLanguages = languageAnnotation.value();
076 }
077
078 DefaultTab defaultTabAnnotation = AnnotationUtils.getClassAnnotation(view, DefaultTab.class);
079 if (defaultTabAnnotation != null) {
080 if (defaultTabAnnotation.metrics().length == 0) {
081 isDefaultTab = true;
082 defaultForMetrics = new String[0];
083
084 } else {
085 isDefaultTab = false;
086 defaultForMetrics = defaultTabAnnotation.metrics();
087 }
088 }
089
090 Description descriptionAnnotation = AnnotationUtils.getClassAnnotation(view, Description.class);
091 if (descriptionAnnotation != null) {
092 description = descriptionAnnotation.value();
093 }
094
095 WidgetProperties propAnnotation = AnnotationUtils.getClassAnnotation(view, WidgetProperties.class);
096 if (propAnnotation != null) {
097 for (WidgetProperty property : propAnnotation.value()) {
098 widgetPropertiesByKey.put(property.key(), property);
099 }
100 }
101
102 WidgetCategory categAnnotation = AnnotationUtils.getClassAnnotation(view, WidgetCategory.class);
103 if (categAnnotation != null) {
104 widgetCategories = categAnnotation.value();
105 }
106
107 WidgetLayout layoutAnnotation = AnnotationUtils.getClassAnnotation(view, WidgetLayout.class);
108 if (layoutAnnotation != null) {
109 widgetLayout = layoutAnnotation.value();
110 }
111
112 isWidget = (view instanceof Widget);
113 }
114
115 public V getTarget() {
116 return view;
117 }
118
119 public String getId() {
120 return view.getId();
121 }
122
123 public String getTitle() {
124 return view.getTitle();
125 }
126
127 public String getDescription() {
128 return description;
129 }
130
131 public Collection<WidgetProperty> getWidgetProperties() {
132 return widgetPropertiesByKey.values();
133 }
134
135 public WidgetProperty getWidgetProperty(String propertyKey) {
136 return widgetPropertiesByKey.get(propertyKey);
137 }
138
139 public String[] getWidgetCategories() {
140 return widgetCategories;
141 }
142
143 public String[] getSections() {
144 return sections;
145 }
146
147 public String[] getUserRoles() {
148 return userRoles;
149 }
150
151 public String[] getResourceScopes() {
152 return resourceScopes;
153 }
154
155 public String[] getResourceQualifiers() {
156 return resourceQualifiers;
157 }
158
159 public String[] getResourceLanguages() {
160 return resourceLanguages;
161 }
162
163 public boolean isDefaultTab() {
164 return isDefaultTab;
165 }
166
167 public String[] getDefaultTabForMetrics() {
168 return defaultForMetrics;
169 }
170
171 public boolean supportsMetric(String metricKey) {
172 return ArrayUtils.contains(defaultForMetrics, metricKey);
173 }
174
175 public boolean isWidget() {
176 return isWidget;
177 }
178
179 public boolean isGwt() {
180 return view instanceof GwtPage;
181 }
182
183 public WidgetLayoutType getWidgetLayout() {
184 return widgetLayout;
185 }
186
187 public boolean isEditable() {
188 return !widgetPropertiesByKey.isEmpty();
189 }
190
191 public boolean hasRequiredProperties() {
192 boolean requires = false;
193 for (WidgetProperty property : getWidgetProperties()) {
194 if (!property.optional() && StringUtils.isEmpty(property.defaultValue())) {
195 requires = true;
196 }
197 }
198 return requires;
199 }
200
201 @Override
202 public int hashCode() {
203 return getId().hashCode();
204 }
205
206
207 @Override
208 public boolean equals(Object obj) {
209 if (obj == null) {
210 return false;
211 }
212 if (obj == this) {
213 return true;
214 }
215 if (obj.getClass() != getClass()) {
216 return false;
217 }
218 ViewProxy rhs = (ViewProxy) obj;
219 return new EqualsBuilder()
220 .append(getId(), rhs.getId())
221 .isEquals();
222 }
223
224 @Override
225 public String toString() {
226 return new ToStringBuilder(this)
227 .append("id", view.getId())
228 .append("sections", sections)
229 .append("userRoles", userRoles)
230 .append("scopes", resourceScopes)
231 .append("qualifiers", resourceQualifiers)
232 .append("languages", resourceLanguages)
233 .append("metrics", defaultForMetrics)
234 .toString();
235
236 }
237
238 public int compareTo(ViewProxy other) {
239 return new CompareToBuilder()
240 .append(getTitle(), other.getTitle())
241 .append(getId(), other.getId())
242 .toComparison();
243
244 }
245 }