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