2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2014 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * SonarQube is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * SonarQube is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 package org.sonar.server.permission.ws.template;
23 import com.google.common.base.Function;
24 import java.util.List;
26 import javax.annotation.Nonnull;
27 import org.sonar.api.config.Settings;
28 import org.sonar.api.resources.Qualifiers;
29 import org.sonar.api.resources.ResourceTypes;
31 import static com.google.common.collect.FluentIterable.from;
32 import static com.google.common.collect.Ordering.natural;
33 import static org.sonar.server.permission.DefaultPermissionTemplates.DEFAULT_TEMPLATE_PROPERTY;
34 import static org.sonar.server.permission.DefaultPermissionTemplates.defaultRootQualifierTemplateProperty;
35 import static org.sonar.server.permission.ws.ResourceTypeToQualifier.RESOURCE_TYPE_TO_QUALIFIER;
37 public class DefaultPermissionTemplateFinder {
38 private final Settings settings;
39 private final ResourceTypes resourceTypes;
41 public DefaultPermissionTemplateFinder(Settings settings, ResourceTypes resourceTypes) {
42 this.settings = settings;
43 this.resourceTypes = resourceTypes;
46 public Set<String> getDefaultTemplateUuids() {
47 return from(resourceTypes.getRoots())
48 .transform(RESOURCE_TYPE_TO_QUALIFIER)
49 .transform(new QualifierToDefaultTemplate(settings))
50 .toSortedSet(natural());
53 public List<TemplateUuidQualifier> getDefaultTemplatesByQualifier() {
54 return from(resourceTypes.getRoots())
55 .transform(RESOURCE_TYPE_TO_QUALIFIER)
56 .transform(new QualifierToTemplateUuidQualifier(settings))
60 public static class TemplateUuidQualifier {
61 private final String templateUuid;
62 private final String qualifier;
64 TemplateUuidQualifier(String templateUuid, String qualifier) {
65 this.templateUuid = templateUuid;
66 this.qualifier = qualifier;
69 public String getTemplateUuid() {
73 public String getQualifier() {
78 private static class QualifierToDefaultTemplate implements Function<String, String> {
79 private final Settings settings;
81 QualifierToDefaultTemplate(Settings settings) {
82 this.settings = settings;
86 public String apply(@Nonnull String qualifier) {
87 String effectiveTemplateUuid = effectiveTemplateUuid(settings, qualifier);
88 return effectiveTemplateUuid;
92 private static class QualifierToTemplateUuidQualifier implements Function<String, TemplateUuidQualifier> {
93 private final Settings settings;
95 QualifierToTemplateUuidQualifier(Settings settings) {
96 this.settings = settings;
100 public TemplateUuidQualifier apply(@Nonnull String qualifier) {
101 String effectiveTemplateUuid = effectiveTemplateUuid(settings, qualifier);
103 return new TemplateUuidQualifier(effectiveTemplateUuid, qualifier);
107 private static String effectiveTemplateUuid(Settings settings, String qualifier) {
108 String qualifierTemplateUuid = settings.getString(defaultRootQualifierTemplateProperty(qualifier));
109 String projectTemplateUuid = settings.getString(defaultRootQualifierTemplateProperty(Qualifiers.PROJECT));
110 String defaultTemplateUuid = settings.getString(DEFAULT_TEMPLATE_PROPERTY);
112 if (qualifierTemplateUuid != null) {
113 return qualifierTemplateUuid;
114 } else if (projectTemplateUuid != null) {
115 return projectTemplateUuid;
117 return defaultTemplateUuid;