Skip to content

Commit b4b4b14

Browse files
author
Dennis Labordus
committed
Extracted method to solve code smell.
Signed-off-by: Dennis Labordus <[email protected]>
1 parent ea35a84 commit b4b4b14

File tree

1 file changed

+32
-25
lines changed

1 file changed

+32
-25
lines changed

repository-postgresql/src/main/java/org/lfenergy/compas/scl/data/repository/postgresql/CompasSclDataPostgreSQLRepository.java

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import javax.sql.DataSource;
1919
import javax.transaction.Transactional;
2020
import java.sql.Array;
21+
import java.sql.Connection;
2122
import java.sql.ResultSet;
2223
import java.sql.SQLException;
2324
import java.util.ArrayList;
@@ -253,10 +254,6 @@ public void create(SclFileType type, UUID id, String name, String scl, Version v
253254
insert into scl_file(id, major_version, minor_version, patch_version, type, name, created_by, scl_data)
254255
values (?, ?, ?, ?, ?, ?, ?, ?)
255256
""";
256-
var createLabelSQL = """
257-
insert into scl_label(scl_id, major_version, minor_version, patch_version, label_value)
258-
values (?, ?, ?, ?, ?)
259-
""";
260257

261258
try (var connection = dataSource.getConnection();
262259
var sclStmt = connection.prepareStatement(createSclSQL)) {
@@ -271,32 +268,42 @@ insert into scl_label(scl_id, major_version, minor_version, patch_version, label
271268
sclStmt.setString(8, scl);
272269
sclStmt.executeUpdate();
273270

274-
if (labels != null && !labels.isEmpty()) {
275-
// Now add the extracted labels from the header to the SCL_LABEL table (in batch)
276-
try (var labelsStmt = connection.prepareStatement(createLabelSQL)) {
277-
labels.stream().distinct().forEach(label -> {
278-
try {
279-
labelsStmt.setObject(1, id);
280-
labelsStmt.setInt(2, version.getMajorVersion());
281-
labelsStmt.setInt(3, version.getMinorVersion());
282-
labelsStmt.setInt(4, version.getPatchVersion());
283-
labelsStmt.setString(5, label);
284-
labelsStmt.addBatch();
285-
labelsStmt.clearParameters();
286-
} catch (SQLException exp) {
287-
throw new CompasSclDataServiceException(POSTGRES_INSERT_ERROR_CODE, "Error adding Label to database!", exp);
288-
}
289-
});
290-
labelsStmt.executeBatch();
291-
} catch (SQLException exp) {
292-
throw new CompasSclDataServiceException(POSTGRES_INSERT_ERROR_CODE, "Error adding Labels to database!", exp);
293-
}
294-
}
271+
// Add the label to the database, if there are any.
272+
createLabels(connection, id, version, labels);
295273
} catch (SQLException exp) {
296274
throw new CompasSclDataServiceException(POSTGRES_INSERT_ERROR_CODE, "Error adding SCL to database!", exp);
297275
}
298276
}
299277

278+
void createLabels(Connection connection, UUID id, Version version, List<String> labels) {
279+
if (labels != null && !labels.isEmpty()) {
280+
// Now add the extracted labels from the header to the SCL_LABEL table (in batch)
281+
var createLabelSQL = """
282+
insert into scl_label(scl_id, major_version, minor_version, patch_version, label_value)
283+
values (?, ?, ?, ?, ?)
284+
""";
285+
try (var labelsStmt = connection.prepareStatement(createLabelSQL)) {
286+
labels.stream().distinct().forEach(label -> {
287+
try {
288+
labelsStmt.setObject(1, id);
289+
labelsStmt.setInt(2, version.getMajorVersion());
290+
labelsStmt.setInt(3, version.getMinorVersion());
291+
labelsStmt.setInt(4, version.getPatchVersion());
292+
labelsStmt.setString(5, label);
293+
labelsStmt.addBatch();
294+
labelsStmt.clearParameters();
295+
} catch (SQLException exp) {
296+
throw new CompasSclDataServiceException(POSTGRES_INSERT_ERROR_CODE, "Error adding Label to database!", exp);
297+
}
298+
});
299+
// Execute the insert commands in batch now.
300+
labelsStmt.executeBatch();
301+
} catch (SQLException exp) {
302+
throw new CompasSclDataServiceException(POSTGRES_INSERT_ERROR_CODE, "Error adding Labels to database!", exp);
303+
}
304+
}
305+
}
306+
300307
@Override
301308
@Transactional(REQUIRED)
302309
public void delete(SclFileType type, UUID id) {

0 commit comments

Comments
 (0)