Skip to content

Commit e943baf

Browse files
committed
refactor the method
1 parent 8747864 commit e943baf

1 file changed

Lines changed: 28 additions & 22 deletions

File tree

src/main/java/fr/insee/genesis/domain/service/contextualvariable/ContextualVariableJsonService.java

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -121,29 +121,12 @@ public SaveContextualVariablesReportDto saveContextualVariableFilesWithReport(
121121
while (it.hasNext()) {
122122
Path jsonFilePath = it.next();
123123

124-
try {
125-
Optional<String> type = processContextualVariableFileAndGetType(
126-
collectionInstrumentId,
127-
jsonFilePath
128-
);
124+
Optional<ContextualVariableFileReportDto> report =
125+
processContextualVariableFileForReport(collectionInstrumentId, jsonFilePath);
129126

130-
if (type.isPresent()) {
131-
moveFile(collectionInstrumentId, mode, fileUtils, jsonFilePath.toString());
132-
133-
files.add(new ContextualVariableFileReportDto(
134-
jsonFilePath.getFileName().toString(),
135-
type.get()
136-
));
137-
}
138-
} catch (GenesisException e) {
139-
throw new GenesisException(
140-
e.getStatus(),
141-
"Error while processing file '%s' : %s"
142-
.formatted(
143-
jsonFilePath.getFileName(),
144-
e.getMessage()
145-
)
146-
);
127+
if (report.isPresent()) {
128+
moveFile(collectionInstrumentId, mode, fileUtils, jsonFilePath.toString());
129+
files.add(report.get());
147130
}
148131
}
149132
} catch (NoSuchFileException nsfe) {
@@ -160,6 +143,29 @@ public SaveContextualVariablesReportDto saveContextualVariableFilesWithReport(
160143
);
161144
}
162145

146+
private Optional<ContextualVariableFileReportDto> processContextualVariableFileForReport(
147+
String collectionInstrumentId,
148+
Path jsonFilePath
149+
) throws GenesisException {
150+
try {
151+
Optional<String> type = processContextualVariableFileAndGetType(
152+
collectionInstrumentId,
153+
jsonFilePath
154+
);
155+
156+
return type.map(value -> new ContextualVariableFileReportDto(
157+
jsonFilePath.getFileName().toString(),
158+
value
159+
));
160+
} catch (GenesisException e) {
161+
throw new GenesisException(
162+
e.getStatus(),
163+
"Error while processing file '%s' : %s"
164+
.formatted(jsonFilePath.getFileName().toString(), e.getMessage())
165+
);
166+
}
167+
}
168+
163169
private Optional<String> processContextualVariableFileAndGetType(
164170
String collectionInstrumentId,
165171
Path jsonFilePath

0 commit comments

Comments
 (0)