aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectDao.java
blob: fcf9e17e0bb54af1ba5ce95bcea9effff1e35f3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 * SonarQube
 * Copyright (C) 2009-2025 SonarSource SA
 * mailto:info 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.db.project;

import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import javax.annotation.Nullable;
import org.sonar.api.utils.System2;
import org.sonar.db.Dao;
import org.sonar.db.DbSession;
import org.sonar.db.Pagination;
import org.sonar.db.audit.AuditPersister;
import org.sonar.db.audit.model.ProjectNewValue;

import static java.util.Collections.emptyList;
import static org.sonar.db.DatabaseUtils.executeLargeInputs;

public class ProjectDao implements Dao {
  private final System2 system2;
  private final AuditPersister auditPersister;

  public ProjectDao(System2 system2, AuditPersister auditPersister) {
    this.system2 = system2;
    this.auditPersister = auditPersister;
  }

  public void insert(DbSession session, ProjectDto project) {
    this.insert(session, project, false);
  }

  public void insert(DbSession session, ProjectDto project, boolean track) {
    if (track) {
      auditPersister.addComponent(session, new ProjectNewValue(project));
    }
    mapper(session).insert(project);
  }

  public Optional<ProjectDto> selectProjectByKey(DbSession session, String key) {
    return Optional.ofNullable(mapper(session).selectProjectByKey(key));
  }

  public Optional<ProjectDto> selectApplicationByKey(DbSession session, String key) {
    return Optional.ofNullable(mapper(session).selectApplicationByKey(key));
  }

  public Optional<ProjectDto> selectProjectOrAppByKey(DbSession session, String key) {
    return Optional.ofNullable(mapper(session).selectProjectOrAppByKey(key));
  }

  public List<ProjectDto> selectAllApplications(DbSession session) {
    return mapper(session).selectAllApplications();
  }

  public List<ProjectDto> selectProjectsByKeys(DbSession session, Collection<String> keys) {
    if (keys.isEmpty()) {
      return emptyList();
    }
    return executeLargeInputs(keys, partition -> mapper(session).selectProjectsByKeys(partition));
  }

  public List<ProjectDto> selectApplicationsByKeys(DbSession session, Set<String> keys) {
    if (keys.isEmpty()) {
      return emptyList();
    }

    return executeLargeInputs(keys, partition -> mapper(session).selectApplicationsByKeys(partition));
  }

  public Optional<ProjectDto> selectByBranchUuid(DbSession dbSession, String branchUuid) {
    return Optional.ofNullable(mapper(dbSession).selectByBranchUuid(branchUuid));
  }

  public List<ProjectDto> selectProjects(DbSession session) {
    return mapper(session).selectProjects();
  }

  public Optional<ProjectDto> selectByUuid(DbSession session, String uuid) {
    return Optional.ofNullable(mapper(session).selectByUuid(uuid));
  }

  public List<ProjectDto> selectAll(DbSession session) {
    return mapper(session).selectAll();
  }

  public List<ProjectDto> selectByUuids(DbSession session, Set<String> uuids) {
    if (uuids.isEmpty()) {
      return emptyList();
    }
    return executeLargeInputs(uuids, partition -> mapper(session).selectByUuids(partition));
  }

  public List<ProjectDto> selectByUuids(DbSession session, Set<String> uuids, Pagination pagination) {
    if (uuids.isEmpty()) {
      return emptyList();
    }
    return mapper(session).selectByUuidsWithPagination(uuids, pagination);
  }

  public void updateVisibility(DbSession session, String uuid, boolean isPrivate) {
    mapper(session).updateVisibility(uuid, isPrivate, system2.now());
  }

  public void updateContainsAiCode(DbSession session, String uuid, boolean containsAiCode) {
    mapper(session).updateContainsAiCode(uuid, containsAiCode, system2.now());
  }

  public void updateDetectedAiCode(DbSession session, String uuid, boolean detectedAiCode) {
    mapper(session).updateDetectedAiCode(uuid, detectedAiCode, system2.now());
  }

  public void updateAiCodeFixEnablementForAllProjects(DbSession dbSession, boolean featureEnablement) {
    mapper(dbSession).updateAiCodeFixEnablementForAllProjects(featureEnablement, system2.now());
  }

  public void updateTags(DbSession session, ProjectDto project) {
    mapper(session).updateTags(project);
  }

  public void update(DbSession session, ProjectDto project) {
    auditPersister.updateComponent(session, new ProjectNewValue(project));
    mapper(session).update(project);
  }

  private static ProjectMapper mapper(DbSession session) {
    return session.getMapper(ProjectMapper.class);
  }

  public void updateNcloc(DbSession dbSession, String projectUuid, long ncloc) {
    mapper(dbSession).updateNcloc(projectUuid, ncloc);
  }

  public long getNclocSum(DbSession dbSession) {
    return getNclocSum(dbSession, null);
  }

  public long getNclocSum(DbSession dbSession, @Nullable String projectUuidToExclude) {
    return Optional.ofNullable(mapper(dbSession).getNclocSum(projectUuidToExclude)).orElse(0L);
  }

  public int countIndexedProjects(DbSession session) {
    return mapper(session).countIndexedProjects();
  }

  public int countProjects(DbSession session) {
    return mapper(session).countProjects();
  }

  public int countApplications(DbSession session) {
    return mapper(session).countApplications();
  }

  public int countAiCodeFixEnabledProjects(DbSession session) {
    return mapper(session).countProjectsByAiCodeFixEnablement(true);
  }

  public int countAiCodeFixDisabledProjects(DbSession session) {
    return mapper(session).countProjectsByAiCodeFixEnablement(false);
  }

  public Set<String> selectAiCodeFixEnabledProjectKeys(DbSession session) {
    return mapper(session).selectProjectKeysByAiCodeFixEnablement(true);
  }
}