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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
/*
* 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.batch.index;
import com.google.common.annotations.VisibleForTesting;
import javax.annotation.Nullable;
import javax.persistence.NonUniqueResultException;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.database.DatabaseSession;
import org.sonar.api.database.model.ResourceModel;
import org.sonar.api.database.model.Snapshot;
import org.sonar.api.resources.Language;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.Resource;
import org.sonar.api.resources.Scopes;
import org.sonar.api.security.ResourcePermissions;
import org.sonar.api.utils.SonarException;
import org.sonar.api.utils.internal.Uuids;
import static org.sonar.api.utils.DateUtils.dateToLong;
public class ResourcePersister implements ScanPersister {
@VisibleForTesting
static final String MODULE_UUID_PATH_SEPARATOR = ".";
private final DatabaseSession session;
private final ResourcePermissions permissions;
private final BatchComponentCache resourceCache;
public ResourcePersister(DatabaseSession session, ResourcePermissions permissions, BatchComponentCache resourceCache) {
this.session = session;
this.permissions = permissions;
this.resourceCache = resourceCache;
}
@Override
public void persist() {
for (BatchComponent resource : resourceCache.all()) {
persist(resource);
}
}
private void persist(BatchComponent batchResource) {
if (batchResource.snapshot() != null) {
// already persisted
return;
}
BatchComponent parentBatchResource = batchResource.parent();
Snapshot s;
if (parentBatchResource != null) {
persist(parentBatchResource);
s = persist(findModule(parentBatchResource), batchResource.resource(), parentBatchResource.resource());
} else {
// Root project
s = persistProject((Project) batchResource.resource(), null);
}
batchResource.setSnapshot(s);
}
private Project findModule(BatchComponent batchResource) {
if (batchResource.resource() instanceof Project) {
return (Project) batchResource.resource();
} else {
return findModule(batchResource.parent());
}
}
private Snapshot persistProject(Project project, @Nullable Project parent) {
ResourceModel model = findOrCreateModel(project, parent);
// Used by ResourceKeyMigration in order to know that a project has already being migrated
model.setDeprecatedKey(project.getKey());
// language is null for project since multi-language support
model.setLanguageKey(null);
// For views
if (project instanceof ResourceCopy) {
model.setCopyResourceId(((ResourceCopy) project).getCopyResourceId());
}
Snapshot parentSnapshot = null;
if (parent != null) {
// assume that the parent project has already been saved
parentSnapshot = resourceCache.get(parent.getEffectiveKey()).snapshot();
model.setRootId((Integer) ObjectUtils.defaultIfNull(parentSnapshot.getRootProjectId(), parentSnapshot.getResourceId()));
} else {
model.setRootId(null);
}
model = session.save(model);
project.setId(model.getId());
project.setUuid(model.getUuid());
Snapshot snapshot = new Snapshot(model, parentSnapshot);
snapshot.setVersion(project.getAnalysisVersion());
snapshot.setCreatedAtMs(dateToLong(project.getAnalysisDate()));
snapshot.setBuildDateMs(System.currentTimeMillis());
snapshot = session.save(snapshot);
session.commit();
if (parent == null && !permissions.hasRoles(project)) {
permissions.grantDefaultRoles(project);
}
return snapshot;
}
Snapshot persist(Project project, Resource resource, @Nullable Resource parent) {
Snapshot snapshot;
if (resource instanceof Project) {
// should not occur, please use the method saveProject()
snapshot = persistProject((Project) resource, (Project) parent);
} else {
snapshot = persistFileOrDirectory(project, resource, parent);
}
return snapshot;
}
/**
* Everything except project and library
*/
private Snapshot persistFileOrDirectory(Project project, Resource resource, @Nullable Resource parentReference) {
BatchComponent moduleResource = resourceCache.get(project);
Integer moduleId = moduleResource.resource().getId();
ResourceModel model = findOrCreateModel(resource, parentReference != null ? parentReference : project);
model.setRootId(moduleId);
model = session.save(model);
resource.setId(model.getId());
resource.setUuid(model.getUuid());
Snapshot parentSnapshot;
if (parentReference != null) {
parentSnapshot = resourceCache.get(parentReference).snapshot();
} else {
parentSnapshot = moduleResource.snapshot();
}
Snapshot snapshot = new Snapshot(model, parentSnapshot);
snapshot.setBuildDateMs(System.currentTimeMillis());
snapshot = session.save(snapshot);
session.commit();
return snapshot;
}
private ResourceModel findOrCreateModel(Resource resource, @Nullable Resource parentResource) {
ResourceModel model;
try {
model = session.getSingleResult(ResourceModel.class, "key", resource.getEffectiveKey());
if (model == null) {
if (StringUtils.isBlank(resource.getEffectiveKey())) {
throw new SonarException("Unable to persist resource " + resource.toString() + ". Resource effective key is blank. This may be caused by an outdated plugin.");
}
model = createModel(resource, parentResource);
} else {
mergeModel(model, resource);
}
updateUuids(resource, parentResource, model);
return model;
} catch (NonUniqueResultException e) {
throw new SonarException("The resource '" + resource.getEffectiveKey() + "' is duplicated in database.", e);
}
}
ResourceModel createModel(Resource resource, @Nullable Resource parentResource) {
ResourceModel model = new ResourceModel();
model.setEnabled(Boolean.TRUE);
model.setDescription(resource.getDescription());
model.setKey(resource.getEffectiveKey());
model.setUuid(Uuids.create());
model.setPath(resource.getPath());
Language language = resource.getLanguage();
if (language != null) {
model.setLanguageKey(language.getKey());
}
if (StringUtils.isNotBlank(resource.getName())) {
model.setName(resource.getName());
} else {
model.setName(resource.getKey());
}
model.setLongName(resource.getLongName());
model.setScope(resource.getScope());
model.setQualifier(resource.getQualifier());
return model;
}
private void updateUuids(Resource resource, Resource parentResource, ResourceModel model) {
if (parentResource == null) {
// Root module
model.setProjectUuid(model.getUuid());
model.setModuleUuidPath(MODULE_UUID_PATH_SEPARATOR + model.getUuid() + MODULE_UUID_PATH_SEPARATOR);
} else {
ResourceModel parentModel = session.getSingleResult(ResourceModel.class, "id", parentResource.getId());
model.setProjectUuid(parentModel.getProjectUuid());
if (Scopes.isProject(resource)) {
// Sub module
model.setModuleUuid(parentResource.getUuid());
String parentModuleUuidPath = parentModel.getModuleUuidPath();
model.setModuleUuidPath(parentModuleUuidPath + model.getUuid() + MODULE_UUID_PATH_SEPARATOR);
} else if (Scopes.isProject(parentResource)) {
// Directory
model.setModuleUuid(parentResource.getUuid());
String parentModuleUuidPath = parentModel.getModuleUuidPath();
model.setModuleUuidPath(parentModuleUuidPath);
} else {
// File
model.setModuleUuid(parentModel.getModuleUuid());
String parentModuleUuidPath = parentModel.getModuleUuidPath();
model.setModuleUuidPath(parentModuleUuidPath);
}
}
}
static void mergeModel(ResourceModel model, Resource resource) {
model.setEnabled(true);
model.setKey(resource.getEffectiveKey());
if (StringUtils.isNotBlank(resource.getName())) {
model.setName(resource.getName());
}
if (StringUtils.isNotBlank(resource.getLongName())) {
model.setLongName(resource.getLongName());
}
if (StringUtils.isNotBlank(resource.getDescription())) {
model.setDescription(resource.getDescription());
}
if (StringUtils.isNotBlank(resource.getPath())) {
model.setPath(resource.getPath());
}
model.setScope(resource.getScope());
model.setQualifier(resource.getQualifier());
Language language = resource.getLanguage();
if (language != null) {
model.setLanguageKey(language.getKey());
}
}
}
|