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 */ 020package org.sonar.server.filters; 021 022import com.google.common.collect.Lists; 023import com.google.common.collect.Sets; 024 025import org.apache.commons.lang.StringUtils; 026import org.apache.commons.lang.builder.ReflectionToStringBuilder; 027import org.apache.commons.lang.builder.ToStringStyle; 028import org.sonar.api.resources.Qualifiers; 029 030import java.util.List; 031import java.util.Set; 032 033public class Filter { 034 035 // path 036 private Integer rootSnapshotId; 037 private Integer baseSnapshotId; 038 private String baseSnapshotPath; 039 040 // filters on resources 041 private Set<String> scopes; 042 private Set<String> qualifiers; 043 private Set<String> languages; 044 private Set<Integer> favouriteIds; 045 private DateCriterion dateCriterion; 046 private String keyRegexp; 047 private String nameRegexp; 048 private boolean onDirectChildren = false; 049 050 // filters on measures 051 private List<MeasureCriterion> measureCriteria = Lists.newLinkedList(); 052 private int periodIndex = 0; 053 054 // sorting 055 private Integer sortedMetricId; 056 private Boolean sortedByMeasureVariation = Boolean.FALSE; 057 private boolean sortedByLanguage; 058 private boolean sortedByName; 059 private boolean sortedByKey; 060 private boolean sortedByDate; 061 private boolean sortedByVersion; 062 private boolean isNumericMetric = true; 063 private boolean ascendingSort = true; 064 065 public Filter setPath(Integer rootSnapshotId, Integer snapshotId, String snapshotPath) { 066 this.baseSnapshotId = snapshotId; 067 if (rootSnapshotId == null) { 068 this.rootSnapshotId = snapshotId; 069 } else { 070 this.rootSnapshotId = rootSnapshotId; 071 } 072 this.baseSnapshotPath = StringUtils.defaultString(snapshotPath, ""); //With Oracle the path can be null (see SONAR-2582) 073 return this; 074 } 075 076 public Integer getRootSnapshotId() { 077 return rootSnapshotId; 078 } 079 080 public boolean hasBaseSnapshot() { 081 return baseSnapshotId != null; 082 } 083 084 public Integer getBaseSnapshotId() { 085 return baseSnapshotId; 086 } 087 088 public String getBaseSnapshotPath() { 089 return baseSnapshotPath; 090 } 091 092 public Set<String> getScopes() { 093 return scopes; 094 } 095 096 public boolean hasScopes() { 097 return scopes != null && !scopes.isEmpty(); 098 } 099 100 public Filter setScopes(Set<String> scopes) { 101 this.scopes = scopes; 102 return this; 103 } 104 105 public Filter setScopes(String... scopes) { 106 this.scopes = Sets.newHashSet(scopes); 107 return this; 108 } 109 110 public Set<String> getQualifiers() { 111 return qualifiers; 112 } 113 114 public boolean hasQualifiers() { 115 return qualifiers != null && !qualifiers.isEmpty(); 116 } 117 118 public Filter setQualifiers(Set<String> qualifiers) { 119 this.qualifiers = qualifiers; 120 return this; 121 } 122 123 public Filter setQualifiers(String... qualifiers) { 124 this.qualifiers = Sets.newHashSet(qualifiers); 125 return this; 126 } 127 128 public Set<String> getLanguages() { 129 return languages; 130 } 131 132 public boolean hasLanguages() { 133 return languages != null && !languages.isEmpty(); 134 } 135 136 public Filter setLanguages(Set<String> languages) { 137 this.languages = languages; 138 return this; 139 } 140 141 public Filter setLanguages(String... languages) { 142 this.languages = Sets.newHashSet(languages); 143 return this; 144 } 145 146 public Set<Integer> getFavouriteIds() { 147 return favouriteIds; 148 } 149 150 public boolean hasFavouriteIds() { 151 return favouriteIds != null && !favouriteIds.isEmpty(); 152 } 153 154 public Filter setFavouriteIds(Set<Integer> favouriteIds) { 155 this.favouriteIds = favouriteIds; 156 return this; 157 } 158 159 public Filter setFavouriteIds(Integer... favouriteIds) { 160 this.favouriteIds = Sets.newHashSet(favouriteIds); 161 return this; 162 } 163 164 public Integer getSortedMetricId() { 165 return sortedMetricId; 166 } 167 168 public boolean isNumericMetric() { 169 return isNumericMetric; 170 } 171 172 public boolean isTextSort() { 173 return !isNumericMetric || sortedByLanguage || sortedByName || sortedByVersion || sortedByKey; 174 } 175 176 public Filter setSortedMetricId(Integer id, boolean isNumericValue, Boolean isVariation) { 177 unsetSorts(); 178 this.sortedMetricId = id; 179 this.isNumericMetric = isNumericValue; 180 this.sortedByMeasureVariation = isVariation; 181 return this; 182 } 183 184 public boolean isSortedByLanguage() { 185 return sortedByLanguage; 186 } 187 188 public Filter setSortedByLanguage() { 189 unsetSorts(); 190 this.sortedByLanguage = true; 191 return this; 192 } 193 194 public boolean isSortedByName() { 195 return sortedByName; 196 } 197 198 public boolean isSortedByKey() { 199 return sortedByKey; 200 } 201 202 public boolean isSortedByVersion() { 203 return sortedByVersion; 204 } 205 206 public Filter setSortedByVersion() { 207 unsetSorts(); 208 this.sortedByVersion = true; 209 return this; 210 } 211 212 public boolean isSorted() { 213 return isSortedByLanguage() || isSortedByName() || isSortedByKey() || isSortedByDate() || isSortedByVersion() || getSortedMetricId() != null; 214 } 215 216 public boolean isSortedByDate() { 217 return sortedByDate; 218 } 219 220 public Filter setSortedByDate() { 221 unsetSorts(); 222 sortedByDate = true; 223 return this; 224 } 225 226 public Filter setSortedByName() { 227 unsetSorts(); 228 this.sortedByName = true; 229 return this; 230 } 231 232 public Filter setSortedByKey() { 233 unsetSorts(); 234 this.sortedByKey = true; 235 return this; 236 } 237 238 private void unsetSorts() { 239 this.sortedByDate = false; 240 this.sortedByLanguage = false; 241 this.sortedByName = false; 242 this.sortedByKey = false; 243 this.sortedMetricId = null; 244 this.sortedByVersion = false; 245 this.isNumericMetric = true; 246 } 247 248 public List<MeasureCriterion> getMeasureCriteria() { 249 return measureCriteria; 250 } 251 252 public Filter setMeasureCriteria(List<MeasureCriterion> l) { 253 this.measureCriteria = l; 254 return this; 255 } 256 257 public Filter addMeasureCriterion(MeasureCriterion c) { 258 this.measureCriteria.add(c); 259 return this; 260 } 261 262 public Filter createMeasureCriterionOnValue(Integer metricId, String operator, Double value, Boolean variation) { 263 this.measureCriteria.add(new MeasureCriterion(metricId, operator, value, variation)); 264 return this; 265 } 266 267 public boolean hasMeasureCriteria() { 268 return !measureCriteria.isEmpty(); 269 } 270 271 protected boolean hasMeasureCriteriaOnMetric(Integer metricId) { 272 if (metricId != null) { 273 for (MeasureCriterion criterion : measureCriteria) { 274 if (metricId.equals(criterion.getMetricId())) { 275 return true; 276 } 277 } 278 } 279 return false; 280 } 281 282 public boolean mustJoinMeasuresTable() { 283 return sortedMetricId != null || hasMeasureCriteria(); 284 } 285 286 public boolean isAscendingSort() { 287 return ascendingSort; 288 } 289 290 public Filter setAscendingSort(boolean b) { 291 this.ascendingSort = b; 292 return this; 293 } 294 295 public DateCriterion getDateCriterion() { 296 return dateCriterion; 297 } 298 299 public Filter setDateCriterion(DateCriterion dc) { 300 this.dateCriterion = dc; 301 return this; 302 } 303 304 public Filter setDateCriterion(String operator, Integer daysAgo) { 305 this.dateCriterion = new DateCriterion().setOperator(operator).setDate(daysAgo); 306 return this; 307 } 308 309 public String getKeyRegexp() { 310 return keyRegexp; 311 } 312 313 public Filter setKeyRegexp(String s) { 314 this.keyRegexp = s; 315 return this; 316 } 317 318 public String getNameRegexp() { 319 return nameRegexp; 320 } 321 322 public Filter setNameRegexp(String s) { 323 this.nameRegexp = s; 324 return this; 325 } 326 327 public int getPeriodIndex() { 328 return periodIndex; 329 } 330 331 public void setPeriodIndex(int i) { 332 this.periodIndex = i; 333 } 334 335 public boolean isOnPeriod() { 336 return periodIndex > 0; 337 } 338 339 public boolean isOnDirectChildren() { 340 return onDirectChildren; 341 } 342 343 public void setOnDirectChildren(boolean b) { 344 this.onDirectChildren = b; 345 } 346 347 static String getVariationColumn(int periodIndex) { 348 switch (periodIndex) { 349 case 1: 350 return "variation_value_1"; 351 case 2: 352 return "variation_value_2"; 353 case 3: 354 return "variation_value_3"; 355 case 4: 356 return "variation_value_4"; 357 case 5: 358 return "variation_value_5"; 359 default: 360 return null; 361 } 362 } 363 364 String getColumnToSort() { 365 String col = "text_value"; 366 if (isNumericMetric()) { 367 col = (sortedByMeasureVariation == Boolean.TRUE ? getVariationColumn(periodIndex) : "value"); 368 } 369 return col; 370 } 371 372 public boolean mustReturnEmptyResult() { 373 boolean hasCriterionOnVariation = false; 374 for (MeasureCriterion criterion : measureCriteria) { 375 if (criterion.isVariation() == Boolean.TRUE) { 376 hasCriterionOnVariation = true; 377 } 378 } 379 return (hasCriterionOnVariation && !isOnPeriod()); 380 } 381 382 @Override 383 public String toString() { 384 return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE); 385 } 386 387 public static Filter createForAllQualifiers() { 388 return new Filter().setQualifiers( 389 Qualifiers.VIEW, Qualifiers.SUBVIEW, 390 Qualifiers.PROJECT, Qualifiers.MODULE, Qualifiers.DIRECTORY, Qualifiers.PACKAGE, 391 Qualifiers.FILE, Qualifiers.CLASS, Qualifiers.UNIT_TEST_FILE, Qualifiers.LIBRARY, Qualifiers.PARAGRAPH); 392 } 393}