/
PersistFileDependenciesStep.java
143 lines (126 loc) · 6.17 KB
/
PersistFileDependenciesStep.java
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
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube 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.
*
* SonarQube 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.computation.step;
import org.sonar.api.resources.Qualifiers;
import org.sonar.api.utils.System2;
import org.sonar.batch.protocol.Constants;
import org.sonar.batch.protocol.output.BatchReport;
import org.sonar.batch.protocol.output.BatchReportReader;
import org.sonar.core.design.FileDependencyDto;
import org.sonar.core.persistence.DbSession;
import org.sonar.core.persistence.MyBatis;
import org.sonar.server.computation.ComputationContext;
import org.sonar.server.computation.source.ReportIterator;
import org.sonar.server.db.DbClient;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
public class PersistFileDependenciesStep implements ComputationStep {
private final DbClient dbClient;
private final System2 system2;
public PersistFileDependenciesStep(DbClient dbClient, System2 system2) {
this.dbClient = dbClient;
this.system2 = system2;
}
@Override
public String[] supportedProjectQualifiers() {
return new String[] {Qualifiers.PROJECT};
}
@Override
public void execute(ComputationContext context) {
DbSession session = dbClient.openSession(true);
try {
FileDependenciesContext fileDependenciesContext = new FileDependenciesContext(context, session);
int rootComponentRef = context.getReportMetadata().getRootComponentRef();
recursivelyProcessComponent(fileDependenciesContext, rootComponentRef, rootComponentRef);
session.commit();
} finally {
MyBatis.closeQuietly(session);
}
}
private void recursivelyProcessComponent(FileDependenciesContext fileDependenciesContext, int componentRef, int parentModuleRef) {
BatchReportReader reportReader = fileDependenciesContext.context.getReportReader();
BatchReport.Component component = reportReader.readComponent(componentRef);
if (component.getType().equals(Constants.ComponentType.FILE)) {
processFileDependenciesReport(fileDependenciesContext, component);
}
for (Integer childRef : component.getChildRefList()) {
recursivelyProcessComponent(fileDependenciesContext, childRef, componentRef);
}
}
private void processFileDependenciesReport(FileDependenciesContext fileDependenciesContext, BatchReport.Component component){
File fileDependencyReport = fileDependenciesContext.context.getReportReader().readFileDependencies(component.getRef());
if (fileDependencyReport != null) {
ReportIterator<BatchReport.FileDependency> fileDependenciesIterator = new ReportIterator<>(fileDependencyReport, BatchReport.FileDependency.PARSER);
try {
while (fileDependenciesIterator.hasNext()) {
BatchReport.FileDependency fileDependency = fileDependenciesIterator.next();
persistFileDependency(fileDependenciesContext, fileDependency, component.getRef());
}
} finally {
fileDependenciesIterator.close();
}
}
}
private void persistFileDependency(FileDependenciesContext fileDependenciesContext, BatchReport.FileDependency fileDependency, int fromRef){
int toFileRef = fileDependency.getToFileRef();
String fromComponentUuid = fileDependenciesContext.uuidsByRef.getUuidFromRef(fromRef);
String toComponentUuid = fileDependenciesContext.uuidsByRef.getUuidFromRef(toFileRef);
dbClient.fileDependencyDao().insert(fileDependenciesContext.session, new FileDependencyDto()
.setFromComponentUuid(fileDependenciesContext.uuidsByRef.getUuidFromRef(fromRef))
.setToComponentUuid(fileDependenciesContext.uuidsByRef.getUuidFromRef(toFileRef))
.setFromParentUuid(fileDependenciesContext.parentUuidsByComponentUuid.get(fromComponentUuid))
.setToParentUuid(fileDependenciesContext.parentUuidsByComponentUuid.get(toComponentUuid))
.setRootProjectSnapshotId(fileDependenciesContext.rootSnapshotId)
.setWeight(fileDependency.getWeight())
.setCreatedAt(system2.now())
);
}
private static class FileDependenciesContext {
private final Long rootSnapshotId;
private final ComponentUuidsCache uuidsByRef;
private final ComputationContext context;
private final Map<String, String> parentUuidsByComponentUuid = new HashMap<>();
private final DbSession session;
public FileDependenciesContext(ComputationContext context, DbSession session) {
this.context = context;
this.rootSnapshotId = context.getReportMetadata().getSnapshotId();
this.session = session;
this.uuidsByRef = new ComponentUuidsCache(context.getReportReader());
int rootComponentRef = context.getReportMetadata().getRootComponentRef();
recursivelyProcessParentByComponentCache(rootComponentRef, rootComponentRef);
}
private void recursivelyProcessParentByComponentCache(int componentRef, int parentModuleRef){
BatchReportReader reportReader = context.getReportReader();
BatchReport.Component component = reportReader.readComponent(componentRef);
BatchReport.Component parent = reportReader.readComponent(parentModuleRef);
if (component.getType().equals(Constants.ComponentType.FILE)) {
parentUuidsByComponentUuid.put(component.getUuid(), parent.getUuid());
}
for (Integer childRef : component.getChildRefList()) {
recursivelyProcessParentByComponentCache(childRef, componentRef);
}
}
}
@Override
public String getDescription() {
return "Persist file dependencies";
}
}