Skip to content

Commit c07a9be

Browse files
Refactor exception handling and cleanup database interactions
Updated services and tests to handle `SQLException` consistently alongside `UncategorizedSQLException`. Removed unnecessary `try-with-resources` blocks, refined error messages, and enhanced exception handling in controllers and database calls.
1 parent 5219c9a commit c07a9be

File tree

8 files changed

+76
-62
lines changed

8 files changed

+76
-62
lines changed

src/main/java/fr/abes/sudoc/component/BaseXmlFunctionsCaller.java

Lines changed: 37 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -29,59 +29,50 @@ public BaseXmlFunctionsCaller(DataSource dataSource) {
2929
}
3030

3131

32-
public List<String> issnToPpn(String issn) {
32+
public List<String> issnToPpn(String issn) throws UncategorizedSQLException, SQLException {
3333
List<String> resultList = new ArrayList<>();
34-
try (
35-
Connection connection = dataSource.getConnection();
36-
PreparedStatement ps = connection.prepareStatement(requestIssn)
37-
) {
38-
ps.setString(1, issn);
3934

40-
ResultSet rs = ps.executeQuery();
41-
while (rs.next()) {
42-
resultList.add(rs.getString("ppn"));
43-
}
44-
} catch (SQLException e) {
45-
log.error(e.getMessage());
35+
Connection connection = dataSource.getConnection();
36+
PreparedStatement ps = connection.prepareStatement(requestIssn);
37+
38+
ps.setString(1, issn);
39+
40+
ResultSet rs = ps.executeQuery();
41+
while (rs.next()) {
42+
resultList.add(rs.getString("ppn"));
4643
}
44+
4745
return resultList;
4846
}
4947

5048

51-
public String isbnToPpn(String isbn) throws UncategorizedSQLException {
49+
public String isbnToPpn(String isbn) throws UncategorizedSQLException, SQLException {
5250
String result = null;
53-
try (
54-
Connection connection = dataSource.getConnection();
55-
PreparedStatement ps = connection.prepareStatement(requestIsbn)
56-
) {
57-
ps.setString(1, isbn);
51+
Connection connection = dataSource.getConnection();
52+
PreparedStatement ps = connection.prepareStatement(requestIsbn);
5853

59-
ResultSet rs = ps.executeQuery();
60-
if (rs.next()) {
61-
result = rs.getString("ppn");
62-
}
63-
} catch (SQLException e) {
64-
log.error(e.getMessage());
54+
ps.setString(1, isbn);
55+
56+
ResultSet rs = ps.executeQuery();
57+
if (rs.next()) {
58+
result = rs.getString("ppn");
6559
}
60+
6661
return result;
6762
}
6863

6964

70-
public List<String> doiToPpn(String doi) throws UncategorizedSQLException {
65+
public List<String> doiToPpn(String doi) throws UncategorizedSQLException, SQLException {
7166
List<String> resultList = new ArrayList<>();
72-
try (
73-
Connection connection = dataSource.getConnection();
74-
PreparedStatement ps = connection.prepareStatement(requestDoi)
75-
) {
76-
ps.setString(1, doi.toLowerCase());
67+
Connection connection = dataSource.getConnection();
68+
PreparedStatement ps = connection.prepareStatement(requestDoi);
69+
ps.setString(1, doi.toLowerCase());
7770

78-
ResultSet rs = ps.executeQuery();
79-
while (rs.next()) {
80-
resultList.add(rs.getString("ppn"));
81-
}
82-
} catch (SQLException e) {
83-
log.error(e.getMessage());
71+
ResultSet rs = ps.executeQuery();
72+
while (rs.next()) {
73+
resultList.add(rs.getString("ppn"));
8474
}
75+
8576
return resultList;
8677
}
8778

@@ -123,22 +114,22 @@ public List<String> datToPpn(Integer date, String auteur, String titre) throws U
123114
return resultList;
124115
}
125116

126-
public Optional<NoticesBibio> findByPpn(String ppn){
117+
public Optional<NoticesBibio> findByPpn(String ppn) {
127118
//@ColumnTransformer(read = "XMLSERIALIZE (CONTENT data_xml as CLOB)", write = "NULLSAFE_XMLTYPE(?)")
128119

129120
try (
130121
Connection connection = dataSource.getConnection();
131122
PreparedStatement ps = connection.prepareStatement("SELECT id, XMLSERIALIZE (CONTENT data_xml as CLOB) as xmlclob FROM NoticesBibio WHERE ppn=?")
132123
) {
133-
ps.setString(1, ppn);
134-
ResultSet rs = ps.executeQuery();
135-
if (rs.next()) {
136-
NoticesBibio noticesBibio = new NoticesBibio();
137-
noticesBibio.setId(rs.getInt("id"));
138-
noticesBibio.setPpn(ppn);
139-
noticesBibio.setDataXml(rs.getClob("xmlclob"));
140-
return Optional.of(noticesBibio);
141-
}
124+
ps.setString(1, ppn);
125+
ResultSet rs = ps.executeQuery();
126+
if (rs.next()) {
127+
NoticesBibio noticesBibio = new NoticesBibio();
128+
noticesBibio.setId(rs.getInt("id"));
129+
noticesBibio.setPpn(ppn);
130+
noticesBibio.setDataXml(rs.getClob("xmlclob"));
131+
return Optional.of(noticesBibio);
132+
}
142133
} catch (SQLException e) {
143134
log.error(e.getMessage());
144135
}

src/main/java/fr/abes/sudoc/controller/SudocController.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import org.springframework.http.MediaType;
1616
import org.springframework.web.bind.annotation.*;
1717

18-
1918
import java.io.IOException;
2019
import java.util.ArrayList;
2120
import java.util.List;
@@ -62,9 +61,11 @@ public ResultWsDto onlineIdentifier2Ppn(@PathVariable String type, @PathVariable
6261
}
6362
} catch (IllegalStateException ex) {
6463
throw new IllegalArgumentException("Le type " + type + " est incorrect. Les types acceptés sont : monograph, serial");
65-
} catch (IOException | IllegalPpnException ex) {
64+
} catch (IOException ex) {
6665
log.error("erreur dans la récupération de la notice correspondant à l'online identifier {}", onlineIdentifier);
6766
throw new IOException(ex);
67+
} catch (IllegalPpnException ex) {
68+
resultat.addErreur("Aucun PPN ne correspond à l'issn " + onlineIdentifier);
6869
}
6970
return resultat;
7071
}
@@ -120,16 +121,18 @@ public ResultWsDto printIdentifier2Ppn(@PathVariable String type, @PathVariable
120121
if(resultat.getResultats().isEmpty() && resultat.getErreurs().isEmpty()){
121122
resultat.addErreur("Aucun PPN ne correspond au " + printIdentifier);
122123
}
123-
return resultat;
124124
} else {
125125
throw new IllegalArgumentException("Le format de l'" + enumType.name() + " " + printIdentifier + " est incorrect");
126126
}
127127
} catch (IllegalStateException ex) {
128128
throw new IllegalArgumentException("Le type " + type + " est incorrect. Les types acceptés sont : monograph, serial");
129-
} catch (IOException | IllegalPpnException ex) {
129+
} catch (IOException ex) {
130130
log.error("erreur dans la récupération de la notice correspondant à au print identifier {}", printIdentifier);
131131
throw new IOException(ex);
132+
} catch (IllegalPpnException ex) {
133+
resultat.addErreur("Aucun PPN ne correspond à l'isbn " + printIdentifier);
132134
}
135+
return resultat;
133136
}
134137

135138

@@ -159,8 +162,7 @@ public ResultWsDto doiIdentifier2Ppn(@RequestParam(name = "doi") String doi_iden
159162
log.debug("ZoneNotFoundException : {}", e.getMessage());
160163
throw new IOException(e.getMessage());
161164
} catch (IllegalPpnException e) {
162-
log.info("Aucune notice ne correspond au doi {}", doi_identifier);
163-
//res.addErreur("Aucune notice ne correspond à l'identifiant " + doi_identifier);
165+
resultat.addErreur("Aucune notice ne correspond au doi " + doi_identifier);
164166
}
165167
return resultat;
166168
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.springframework.stereotype.Service;
77

88
import java.io.IOException;
9+
import java.sql.SQLException;
910
import java.util.ArrayList;
1011
import java.util.List;
1112
import java.util.regex.Pattern;
@@ -40,6 +41,11 @@ public List<String> getPpnFromIdentifiant(String doi) throws IOException, Illega
4041
}
4142
} catch (UncategorizedSQLException ex) {
4243
throw new IOException("Incident technique lors de l'accès à la base de données");
44+
} catch (SQLException ex) {
45+
if (ex.getMessage().contains("no ppn matched")) {
46+
throw new IllegalPpnException("Aucune notice ne correspond à la recherche");
47+
}
48+
throw new IOException(ex);
4349
}
4450
}
4551
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.springframework.stereotype.Service;
99

1010
import java.io.IOException;
11+
import java.sql.SQLException;
1112
import java.util.List;
1213
import java.util.regex.Pattern;
1314

@@ -35,13 +36,15 @@ public List<String> getPpnFromIdentifiant(String isbn) throws IOException, Illeg
3536
try{
3637
String result = caller.isbnToPpn(isbn);
3738
return Utilitaire.parseJson(result);
38-
} catch (UncategorizedSQLException ex) {
39+
} catch (SQLException ex) {
3940
if (ex.getMessage().contains("no ppn matched")) {
4041
throw new IllegalPpnException("Aucune notice ne correspond à la recherche");
4142
}
4243
throw new IOException(ex);
4344
} catch (JsonProcessingException ex) {
4445
throw new IOException("Impossible de récupérer les ppn correspondant à cet identifiant");
46+
} catch (UncategorizedSQLException e) {
47+
throw new IOException("Incident technique lors de l'accès à la base de données");
4548
}
4649
}
4750
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package fr.abes.sudoc.service;
22

33
import fr.abes.sudoc.component.BaseXmlFunctionsCaller;
4+
import fr.abes.sudoc.exception.IllegalPpnException;
45
import org.springframework.jdbc.UncategorizedSQLException;
56
import org.springframework.stereotype.Service;
67

78
import java.io.IOException;
9+
import java.sql.SQLException;
810
import java.util.List;
911
import java.util.regex.Pattern;
1012

@@ -23,11 +25,16 @@ public boolean checkFormat(String issn) {
2325
}
2426

2527
@Override
26-
public List<String> getPpnFromIdentifiant(String issn) throws IOException {
28+
public List<String> getPpnFromIdentifiant(String issn) throws IOException, IllegalPpnException {
2729
try{
2830
return caller.issnToPpn(issn.replace("-", ""));
2931
} catch (UncategorizedSQLException ex) {
3032
throw new IOException("Incident technique lors de l'accès à la base de données");
33+
} catch (SQLException ex) {
34+
if (ex.getMessage().contains("no ppn matched")) {
35+
throw new IllegalPpnException("Aucune notice ne correspond à la recherche");
36+
}
37+
throw new IOException(ex);
3138
}
3239
}
3340

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ public NoticeXml getNoticeByPpn(String ppn) throws IllegalPpnException, IOExcept
5353
}
5454
}
5555
}
56+
57+
/*if (noticeOpt.isPresent()) {
58+
try {
59+
return xmlMapper.readValue(noticeOpt.get().getDataXml().getCharacterStream(), NoticeXml.class);
60+
} catch (SQLException e) {
61+
log.error(e.getMessage());
62+
}
63+
}*/
5664
return null;
5765
}
5866

src/test/java/fr/abes/sudoc/service/IsbnServiceTest.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@
1010
import org.springframework.beans.factory.annotation.Autowired;
1111
import org.springframework.boot.test.context.SpringBootTest;
1212
import org.springframework.boot.test.mock.mockito.MockBean;
13-
import org.springframework.jdbc.UncategorizedSQLException;
1413
import org.springframework.test.context.junit.jupiter.SpringExtension;
1514

1615
import java.io.IOException;
1716
import java.sql.SQLException;
18-
import java.sql.SQLRecoverableException;
1917

2018
@ExtendWith(SpringExtension.class)
2119
@SpringBootTest(classes = {IsbnService.class, BaseXmlFunctionsCaller.class})
@@ -101,12 +99,12 @@ void checkFormatIsbn13Characters() {
10199

102100
@Test
103101
@DisplayName("getPpnFromIdentifiant with UncategorizedSQLException")
104-
void testgetPpnUncategorizedException() {
105-
UncategorizedSQLException ex = new UncategorizedSQLException("no ppn matched", "select test", new SQLException());
102+
void testgetPpnUncategorizedException() throws SQLException {
103+
SQLException ex = new SQLException("no ppn matched", "select test", new SQLException());
106104
Mockito.doThrow(ex).when(caller).isbnToPpn(Mockito.anyString());
107105
Assertions.assertThrows(IllegalPpnException.class, () -> isbnService.getPpnFromIdentifiant("1111111111"));
108106

109-
ex = new UncategorizedSQLException("trululu", "select test", new SQLException());
107+
ex = new SQLException("trululu", "select test", new SQLException());
110108
Mockito.doThrow(ex).when(caller).isbnToPpn(Mockito.anyString());
111109
Assertions.assertThrows(IOException.class, () -> isbnService.getPpnFromIdentifiant("1111111111"));
112110
}

src/test/java/fr/abes/sudoc/service/IssnServiceTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package fr.abes.sudoc.service;
22

33
import fr.abes.sudoc.component.BaseXmlFunctionsCaller;
4-
import fr.abes.sudoc.exception.IllegalPpnException;
54
import fr.abes.sudoc.utils.Utilitaire;
65
import org.junit.jupiter.api.Assertions;
76
import org.junit.jupiter.api.DisplayName;
@@ -15,7 +14,7 @@
1514
import org.springframework.test.context.junit.jupiter.SpringExtension;
1615

1716
import java.io.IOException;
18-
import java.sql.SQLRecoverableException;
17+
import java.sql.SQLException;
1918

2019
@ExtendWith(SpringExtension.class)
2120
@SpringBootTest(classes = {IssnService.class, BaseXmlFunctionsCaller.class})
@@ -95,7 +94,7 @@ void checkFormatIssn9Characters() {
9594

9695
@Test
9796
@DisplayName("getPpnFromIdentifiant with UncategorizedSQLException")
98-
void testgetPpnUncategorizedException() {
97+
void testgetPpnUncategorizedException() throws SQLException {
9998
Mockito.doThrow(UncategorizedSQLException.class).when(caller).issnToPpn(Mockito.anyString());
10099
Assertions.assertThrows(IOException.class, () -> issnService.getPpnFromIdentifiant("11111111"));
101100
}

0 commit comments

Comments
 (0)