3 * Copyright (C) 2009-2024 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program 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 * This program 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.
20 package org.sonar.ce.task.projectexport.component;
22 import com.sonarsource.governance.projectdump.protobuf.ProjectDump;
23 import java.sql.PreparedStatement;
24 import java.sql.ResultSet;
25 import java.sql.SQLException;
26 import org.sonar.api.resources.Qualifiers;
27 import org.slf4j.LoggerFactory;
28 import org.sonar.ce.task.projectexport.steps.DumpElement;
29 import org.sonar.ce.task.projectexport.steps.DumpWriter;
30 import org.sonar.ce.task.projectexport.steps.ProjectHolder;
31 import org.sonar.ce.task.projectexport.steps.StreamWriter;
32 import org.sonar.ce.task.step.ComputationStep;
33 import org.sonar.db.DatabaseUtils;
34 import org.sonar.db.DbClient;
35 import org.sonar.db.DbSession;
37 import static java.lang.String.format;
38 import static org.apache.commons.lang.StringUtils.defaultString;
39 import static org.sonar.db.DatabaseUtils.getString;
41 public class ExportComponentsStep implements ComputationStep {
43 // Results are ordered by ascending id so that any parent is located
44 // before its children.
45 private static final String QUERY = "select" +
46 " p.uuid, p.qualifier, p.uuid_path, p.kee, p.name," +
47 " p.description, p.scope, p.language, p.long_name, p.path," +
48 " p.deprecated_kee, p.branch_uuid" +
49 " from components p" +
50 " join components pp on pp.uuid = p.branch_uuid" +
51 " join project_branches pb on pb.uuid = pp.uuid" +
52 " where pb.project_uuid=? and pb.branch_type = 'BRANCH' and pb.exclude_from_purge=? and p.enabled=?";
53 private final DbClient dbClient;
54 private final ProjectHolder projectHolder;
55 private final MutableComponentRepository componentRepository;
56 private final DumpWriter dumpWriter;
58 public ExportComponentsStep(DbClient dbClient, ProjectHolder projectHolder, MutableComponentRepository componentRepository, DumpWriter dumpWriter) {
59 this.dbClient = dbClient;
60 this.projectHolder = projectHolder;
61 this.componentRepository = componentRepository;
62 this.dumpWriter = dumpWriter;
66 public void execute(Context context) {
70 StreamWriter<ProjectDump.Component> output = dumpWriter.newStreamWriter(DumpElement.COMPONENTS);
71 DbSession dbSession = dbClient.openSession(false);
72 PreparedStatement stmt = createSelectStatement(dbSession);
73 ResultSet rs = stmt.executeQuery()) {
74 ProjectDump.Component.Builder componentBuilder = ProjectDump.Component.newBuilder();
76 String uuid = getString(rs, 1);
77 String qualifier = getString(rs, 2);
78 String uuidPath = getString(rs, 3);
79 componentBuilder.clear();
80 componentRepository.register(ref, uuid, Qualifiers.FILE.equals(qualifier));
81 ProjectDump.Component component = componentBuilder
84 .setUuidPath(uuidPath)
85 .setKey(getString(rs, 4))
86 .setName(defaultString(getString(rs, 5)))
87 .setDescription(defaultString(getString(rs, 6)))
88 .setScope(getString(rs, 7))
89 .setQualifier(qualifier)
90 .setLanguage(defaultString(getString(rs, 8)))
91 .setLongName(defaultString(getString(rs, 9)))
92 .setPath(defaultString(getString(rs, 10)))
93 .setDeprecatedKey(defaultString(getString(rs, 11)))
94 .setProjectUuid(getString(rs, 12))
96 output.write(component);
100 LoggerFactory.getLogger(getClass()).debug("{} components exported", count);
101 } catch (Exception e) {
102 throw new IllegalStateException(format("Component Export failed after processing %d components successfully", count), e);
106 private PreparedStatement createSelectStatement(DbSession dbSession) throws SQLException {
107 PreparedStatement stmt = dbClient.getMyBatis().newScrollingSelectStatement(dbSession, QUERY);
109 stmt.setString(1, projectHolder.projectDto().getUuid());
110 stmt.setBoolean(2, true);
111 stmt.setBoolean(3, true);
113 } catch (Exception t) {
114 DatabaseUtils.closeQuietly(stmt);
120 public String getDescription() {
121 return "Export components";