Skip to content

Commit

Permalink
Refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
alexisszmundy committed Apr 25, 2024
1 parent d30a4e6 commit cf3978c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.XMLStreamException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -117,13 +118,14 @@ public ResponseEntity<Object> saveResponsesFromXmlCampaignFolder(@RequestParam("
@PutMapping(path = "/save/lunatic-xml/all-campaigns")
public ResponseEntity<Object> saveResponsesFromAllCampaignFolders(){
List<GenesisError> errors = new ArrayList<>();
List<String> campaignFolders = fileUtils.listAllSpecsFolders();
List<File> campaignFolders = fileUtils.listAllSpecsFolders();

if (campaignFolders.isEmpty()) {
return ResponseEntity.ok("No campaign to save");
}

for (String campaignName : campaignFolders) {
for (File campaignFolder: campaignFolders) {
String campaignName = campaignFolder.getName();
log.info("Try to import data for campaign : {}", campaignName);

try {
Expand Down Expand Up @@ -176,7 +178,7 @@ public ResponseEntity<Path> findAllResponsesByQuestionnaire(@RequestParam("idQue
fileUtils.writeSuUpdatesInFile(filepath, responsesStream);
} catch (IOException e) {
log.error("Error while writing file", e);
return new ResponseEntity<>(filepath, HttpStatus.INTERNAL_SERVER_ERROR);
return ResponseEntity.internalServerError().body(filepath);
}
log.info("End of extraction, responses extracted : {}", idUEsResponses.size());
return ResponseEntity.ok(filepath);
Expand Down Expand Up @@ -267,12 +269,12 @@ private void treatCampaignWithMode(String campaignName, Mode mode, List<GenesisE
log.info("Numbers of files to load in folder {} : {}", dataFolder, dataFiles.size());
if (dataFiles.isEmpty()) {
errors.add(new NoDataError("No data file found", Mode.getEnumFromModeName(mode.getModeName())));
log.info("No data file found in folder " + dataFolder);
log.info("No data file found in folder {}", dataFolder);
}
VariablesMap variablesMap;
try {
Path ddiFilePath = fileUtils.findDDIFile(campaignName, mode.getModeName());
variablesMap = DDIReader.getVariablesFromDDI(ddiFilePath.toFile().toURI().toURL());
variablesMap = DDIReader.getVariablesFromDDI(ddiFilePath.toUri().toURL());
} catch (Exception e) {
errors.add(new GenesisError(e.getMessage()));
return;
Expand All @@ -287,7 +289,7 @@ private void treatCampaignWithMode(String campaignName, Mode mode, List<GenesisE
} else {
treatXmlFileSequentially(filepath, mode, variablesMap);
}
log.info("File {} saved", fileName);
log.debug("File {} saved", fileName);
fileUtils.moveDataFile(campaignName, mode.getFolder(), fileName);
}
}
Expand All @@ -308,12 +310,12 @@ private ResponseEntity<Object> treatXmlFileWithMemory(Path filepath, Mode modeSp
}
surveyUnitQualityService.verifySurveyUnits(suDtos, variablesMap);

log.info("Saving {} survey units updates", suDtos.size());
log.debug("Saving {} survey units updates", suDtos.size());
surveyUnitService.saveSurveyUnits(suDtos);
log.info("Survey units updates saved");
log.debug("Survey units updates saved");

log.info("File {} treated", filepath.getFileName());
return new ResponseEntity<>("Test", HttpStatus.OK);
log.info("File {} treated with {} survey units", filepath.getFileName(), suDtos.size());
return ResponseEntity.ok().build();
}

private ResponseEntity<Object> treatXmlFileSequentially(Path filepath, Mode modeSpecified, VariablesMap variablesMap) throws IOException, XMLStreamException {
Expand Down Expand Up @@ -341,6 +343,6 @@ private ResponseEntity<Object> treatXmlFileSequentially(Path filepath, Mode mode
}

log.info("File {} treated", filepath.getFileName());
return new ResponseEntity<>("Test", HttpStatus.OK);
return ResponseEntity.ok().build();
}
}
21 changes: 9 additions & 12 deletions src/main/java/fr/insee/genesis/infrastructure/utils/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;
Expand Down Expand Up @@ -219,17 +220,13 @@ public void writeSuUpdatesInFile(Path filePath, Stream<SurveyUnitUpdateDto> resp
* List all folders in the specs folder
* @return List of specs folders
*/
public List<String> listAllSpecsFolders() {
List<String> folders = new ArrayList<>();
File[] objs = new File(getSpecFolder()).listFiles();
if (objs == null) {
return List.of();
}
for (File file : objs) {
if (file.isDirectory()) {
folders.add(file.getName());
}
}
return folders;
public List<File> listAllSpecsFolders() {
File[] objs = new File(getSpecFolder()).listFiles();
if (objs == null) {
return List.of();
}
return Arrays.stream(objs)
.filter(File::isDirectory)
.toList();
}
}

0 comments on commit cf3978c

Please sign in to comment.