Skip to content

Commit f955e66

Browse files
author
SamuelQuetin
committed
refactor: improve CLOB processing with BufferedReader and streamline error handling
1 parent 3c4964a commit f955e66

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

src/main/java/fr/abes/sudoc/service/NoticeService.java

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
import lombok.extern.slf4j.Slf4j;
1212
import org.springframework.stereotype.Service;
1313

14+
import java.io.BufferedReader;
1415
import java.io.IOException;
1516
import java.io.Reader;
1617
import java.sql.Clob;
1718
import java.sql.SQLException;
1819
import java.util.*;
20+
import java.util.stream.Collectors;
1921

2022
@Service
2123
@Slf4j
@@ -38,30 +40,27 @@ public NoticeXml getNoticeByPpn(String ppn) throws IllegalPpnException, IOExcept
3840
throw new IllegalPpnException("Le PPN ne peut pas être null");
3941
//Optional<NoticesBibio> noticeOpt = this.noticesBibioRepository.findByPpn(ppn);
4042
Optional<NoticesBibio> noticeOpt = baseXmlFunctionsCaller.findByPpn(ppn);
41-
if (noticeOpt.isPresent()) {
42-
Clob clob = noticeOpt.get().getDataXml();
43-
44-
try (Reader reader = clob.getCharacterStream()){
45-
return xmlMapper.readValue(reader, NoticeXml.class);
46-
} catch (SQLException e) {
47-
log.error(e.getMessage());
48-
} finally {
49-
try {
50-
clob.free();
51-
} catch (SQLException e) {
52-
log.error(e.getMessage());
53-
}
54-
}
43+
if (noticeOpt.isEmpty()) {
44+
return null;
5545
}
56-
57-
/*if (noticeOpt.isPresent()) {
46+
Clob clob = noticeOpt.get().getDataXml();
47+
String xmlString = null;
48+
49+
try (Reader reader = clob.getCharacterStream()){
50+
xmlString = new BufferedReader(reader)
51+
.lines()
52+
.collect(Collectors.joining("\n"));
53+
} catch (SQLException e) {
54+
log.error(e.getMessage());
55+
} finally {
5856
try {
59-
return xmlMapper.readValue(noticeOpt.get().getDataXml().getCharacterStream(), NoticeXml.class);
57+
clob.free();
6058
} catch (SQLException e) {
6159
log.error(e.getMessage());
6260
}
63-
}*/
64-
return null;
61+
}
62+
63+
return (xmlString != null) ? xmlMapper.readValue(xmlString, NoticeXml.class) : null;
6564
}
6665

6766
public List<String> getEquivalentElectronique(NoticeXml notice) throws IOException, IllegalPpnException, ZoneNotFoundException {

0 commit comments

Comments
 (0)