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

org.sonar.server.qualityprofile.QProfileProjectLookup 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.qualityprofile;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import javax.annotation.CheckForNull;
import org.sonar.api.component.Component;
import org.sonar.api.ce.ComputeEngineSide;
import org.sonar.api.server.ServerSide;
import org.sonar.api.web.UserRole;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.MyBatis;
import org.sonar.db.qualityprofile.QualityProfileDto;
import org.sonar.server.user.UserSession;

@ServerSide
@ComputeEngineSide
public class QProfileProjectLookup {

  private final DbClient db;
  private final UserSession userSession;

  public QProfileProjectLookup(DbClient db, UserSession userSession) {
    this.db = db;
    this.userSession = userSession;
  }

  public List projects(int profileId) {
    DbSession session = db.openSession(false);
    try {
      QualityProfileDto qualityProfile = db.qualityProfileDao().selectById(session, profileId);
      QProfileValidations.checkProfileIsNotNull(qualityProfile);
      Map componentsByKeys = Maps.newHashMap();
      for (Component component : db.qualityProfileDao().selectProjects(qualityProfile.getName(), qualityProfile.getLanguage(), session)) {
        componentsByKeys.put(component.key(), component);
      }

      List result = Lists.newArrayList();
      Collection authorizedProjectKeys = db.authorizationDao().selectAuthorizedRootProjectsKeys(userSession.getUserId(), UserRole.USER);
      for (Map.Entry entry : componentsByKeys.entrySet()) {
        if (authorizedProjectKeys.contains(entry.getKey())) {
          result.add(entry.getValue());
        }
      }

      return result;
    } finally {
      MyBatis.closeQuietly(session);
    }
  }

  public int countProjects(QProfile profile) {
    return db.qualityProfileDao().countProjects(profile.name(), profile.language());
  }

  @CheckForNull
  public QProfile findProfileByProjectAndLanguage(long projectId, String language) {
    QualityProfileDto dto = db.qualityProfileDao().selectByProjectAndLanguage(projectId, language);
    if (dto != null) {
      return QProfile.from(dto);
    }
    return null;
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy