All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.server.permission.ws.template.SearchTemplatesDataLoader Maven / Gradle / Ivy

There is a newer version: 7.2.1
Show newest version
/*
 * SonarQube
 * Copyright (C) 2009-2016 SonarSource SA
 * mailto:contact AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.server.permission.ws.template;

import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.common.collect.Table;
import com.google.common.collect.TreeBasedTable;
import java.util.List;
import javax.annotation.Nonnull;
import org.apache.ibatis.session.ResultContext;
import org.apache.ibatis.session.ResultHandler;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.permission.CountByTemplateAndPermissionDto;
import org.sonar.db.permission.PermissionTemplateDto;
import org.sonar.server.permission.ws.template.DefaultPermissionTemplateFinder.TemplateUuidQualifier;
import org.sonarqube.ws.client.permission.SearchTemplatesWsRequest;

import static org.sonar.server.permission.ws.template.SearchTemplatesData.newBuilder;

public class SearchTemplatesDataLoader {
  private final DbClient dbClient;
  private final DefaultPermissionTemplateFinder defaultPermissionTemplateFinder;

  public SearchTemplatesDataLoader(DbClient dbClient, DefaultPermissionTemplateFinder defaultPermissionTemplateFinder) {
    this.dbClient = dbClient;
    this.defaultPermissionTemplateFinder = defaultPermissionTemplateFinder;
  }

  public SearchTemplatesData load(SearchTemplatesWsRequest request) {
    DbSession dbSession = dbClient.openSession(false);
    try {
      SearchTemplatesData.Builder data = newBuilder();
      List templates = searchTemplates(dbSession, request);
      List templateIds = Lists.transform(templates, TemplateToIdFunction.INSTANCE);
      List defaultTemplates = defaultPermissionTemplateFinder.getDefaultTemplatesByQualifier();

      data.templates(templates)
        .defaultTemplates(defaultTemplates)
        .userCountByTemplateIdAndPermission(userCountByTemplateIdAndPermission(dbSession, templateIds))
        .groupCountByTemplateIdAndPermission(groupCountByTemplateIdAndPermission(dbSession, templateIds));

      return data.build();
    } finally {
      dbClient.closeSession(dbSession);
    }
  }

  private List searchTemplates(DbSession dbSession, SearchTemplatesWsRequest request) {
    String nameMatch = request.getQuery();

    return nameMatch == null ? dbClient.permissionTemplateDao().selectAll(dbSession)
      : dbClient.permissionTemplateDao().selectAll(dbSession, nameMatch);
  }

  private Table userCountByTemplateIdAndPermission(DbSession dbSession, List templateIds) {
    final Table userCountByTemplateIdAndPermission = TreeBasedTable.create();

    dbClient.permissionTemplateDao().usersCountByTemplateIdAndPermission(dbSession, templateIds, new ResultHandler() {
      @Override
      public void handleResult(ResultContext context) {
        CountByTemplateAndPermissionDto row = (CountByTemplateAndPermissionDto) context.getResultObject();
        userCountByTemplateIdAndPermission.put(row.getTemplateId(), row.getPermission(), row.getCount());
      }
    });

    return userCountByTemplateIdAndPermission;
  }

  private Table groupCountByTemplateIdAndPermission(DbSession dbSession, List templateIds) {
    final Table userCountByTemplateIdAndPermission = TreeBasedTable.create();

    dbClient.permissionTemplateDao().groupsCountByTemplateIdAndPermission(dbSession, templateIds, new ResultHandler() {
      @Override
      public void handleResult(ResultContext context) {
        CountByTemplateAndPermissionDto row = (CountByTemplateAndPermissionDto) context.getResultObject();
        userCountByTemplateIdAndPermission.put(row.getTemplateId(), row.getPermission(), row.getCount());
      }
    });

    return userCountByTemplateIdAndPermission;
  }

  private enum TemplateToIdFunction implements Function {
    INSTANCE;

    @Override
    public Long apply(@Nonnull PermissionTemplateDto template) {
      return template.getId();
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy