Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 25 additions & 34 deletions src/main/java/fr/abes/sudoc/component/BaseXmlFunctionsCaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,67 +20,58 @@ public class BaseXmlFunctionsCaller {

private final DataSource dataSource;

private static final String requestIssn = "SELECT distinct ppn from AUTORITES.biblio_table_fouretout where cle1='ISSN' and cle2=?";
private static final String requestIsbn = "select AUTORITES.ISBN2PPNJSON(?) from dual";
private static final String requestDoi = "SELECT distinct ppn from AUTORITES.biblio_table_fouretout where cle1='DOI' and cle2=?";
private static final String requestIssn = "SELECT DISTINCT ppn FROM AUTORITES.biblio_table_fouretout WHERE cle1='ISSN' AND cle2=?";
private static final String requestIsbn = "SELECT AUTORITES.ISBN2PPNJSON(?) AS ppn FROM dual";
private static final String requestDoi = "SELECT DISTINCT ppn FROM AUTORITES.biblio_table_fouretout WHERE cle1='DOI' AND cle2=?";

public BaseXmlFunctionsCaller(DataSource dataSource) {
this.dataSource = dataSource;
}


public List<String> issnToPpn(String issn) {
public List<String> issnToPpn(String issn) throws UncategorizedSQLException, SQLException {
List<String> resultList = new ArrayList<>();
try (
Connection connection = dataSource.getConnection();
PreparedStatement ps = connection.prepareStatement(requestIssn)
) {

try (Connection connection = dataSource.getConnection();
PreparedStatement ps = connection.prepareStatement(requestIssn)) {

ps.setString(1, issn);

ResultSet rs = ps.executeQuery();
while (rs.next()) {
resultList.add(rs.getString("ppn"));
}
} catch (SQLException e) {
log.error(e.getMessage());
}
return resultList;
}


public String isbnToPpn(String isbn) throws UncategorizedSQLException {
public String isbnToPpn(String isbn) throws UncategorizedSQLException, SQLException {
String result = null;
try (
Connection connection = dataSource.getConnection();
PreparedStatement ps = connection.prepareStatement(requestIsbn)
) {
try (Connection connection = dataSource.getConnection();
PreparedStatement ps = connection.prepareStatement(requestIsbn)) {

ps.setString(1, isbn);

ResultSet rs = ps.executeQuery();
if (rs.next()) {
result = rs.getString("ppn");
}
} catch (SQLException e) {
log.error(e.getMessage());
}
return result;
}


public List<String> doiToPpn(String doi) throws UncategorizedSQLException {
public List<String> doiToPpn(String doi) throws UncategorizedSQLException, SQLException {
List<String> resultList = new ArrayList<>();
try (
Connection connection = dataSource.getConnection();
PreparedStatement ps = connection.prepareStatement(requestDoi)
) {
try (Connection connection = dataSource.getConnection();
PreparedStatement ps = connection.prepareStatement(requestDoi)) {
ps.setString(1, doi.toLowerCase());

ResultSet rs = ps.executeQuery();
while (rs.next()) {
resultList.add(rs.getString("ppn"));
}
} catch (SQLException e) {
log.error(e.getMessage());
}
return resultList;
}
Expand Down Expand Up @@ -123,22 +114,22 @@ public List<String> datToPpn(Integer date, String auteur, String titre) throws U
return resultList;
}

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

try (
Connection connection = dataSource.getConnection();
PreparedStatement ps = connection.prepareStatement("SELECT id, XMLSERIALIZE (CONTENT data_xml as CLOB) as xmlclob FROM NoticesBibio WHERE ppn=?")
) {
ps.setString(1, ppn);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
NoticesBibio noticesBibio = new NoticesBibio();
noticesBibio.setId(rs.getInt("id"));
noticesBibio.setPpn(ppn);
noticesBibio.setDataXml(rs.getClob("xmlclob"));
return Optional.of(noticesBibio);
}
ps.setString(1, ppn);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
NoticesBibio noticesBibio = new NoticesBibio();
noticesBibio.setId(rs.getInt("id"));
noticesBibio.setPpn(ppn);
noticesBibio.setDataXml(rs.getClob("xmlclob"));
return Optional.of(noticesBibio);
}
} catch (SQLException e) {
log.error(e.getMessage());
}
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/fr/abes/sudoc/controller/SudocController.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -62,9 +61,11 @@ public ResultWsDto onlineIdentifier2Ppn(@PathVariable String type, @PathVariable
}
} catch (IllegalStateException ex) {
throw new IllegalArgumentException("Le type " + type + " est incorrect. Les types acceptés sont : monograph, serial");
} catch (IOException | IllegalPpnException ex) {
} catch (IOException ex) {
log.error("erreur dans la récupération de la notice correspondant à l'online identifier {}", onlineIdentifier);
throw new IOException(ex);
} catch (IllegalPpnException ex) {
resultat.addErreur("Aucun PPN ne correspond à l'issn " + onlineIdentifier);
}
return resultat;
}
Expand Down Expand Up @@ -120,16 +121,18 @@ public ResultWsDto printIdentifier2Ppn(@PathVariable String type, @PathVariable
if(resultat.getResultats().isEmpty() && resultat.getErreurs().isEmpty()){
resultat.addErreur("Aucun PPN ne correspond au " + printIdentifier);
}
return resultat;
} else {
throw new IllegalArgumentException("Le format de l'" + enumType.name() + " " + printIdentifier + " est incorrect");
}
} catch (IllegalStateException ex) {
throw new IllegalArgumentException("Le type " + type + " est incorrect. Les types acceptés sont : monograph, serial");
} catch (IOException | IllegalPpnException ex) {
} catch (IOException ex) {
log.error("erreur dans la récupération de la notice correspondant à au print identifier {}", printIdentifier);
throw new IOException(ex);
} catch (IllegalPpnException ex) {
resultat.addErreur("Aucun PPN ne correspond à l'isbn " + printIdentifier);
}
return resultat;
}


Expand Down Expand Up @@ -159,8 +162,7 @@ public ResultWsDto doiIdentifier2Ppn(@RequestParam(name = "doi") String doi_iden
log.debug("ZoneNotFoundException : {}", e.getMessage());
throw new IOException(e.getMessage());
} catch (IllegalPpnException e) {
log.info("Aucune notice ne correspond au doi {}", doi_identifier);
//res.addErreur("Aucune notice ne correspond à l'identifiant " + doi_identifier);
resultat.addErreur("Aucune notice ne correspond au doi " + doi_identifier);
}
return resultat;
}
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/fr/abes/sudoc/service/DoiService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -40,6 +41,11 @@ public List<String> getPpnFromIdentifiant(String doi) throws IOException, Illega
}
} catch (UncategorizedSQLException ex) {
throw new IOException("Incident technique lors de l'accès à la base de données");
} catch (SQLException ex) {
if (ex.getMessage().contains("no ppn matched")) {
throw new IllegalPpnException("Aucune notice ne correspond à la recherche");
}
throw new IOException(ex);
}
}
}
5 changes: 4 additions & 1 deletion src/main/java/fr/abes/sudoc/service/IsbnService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -35,13 +36,15 @@ public List<String> getPpnFromIdentifiant(String isbn) throws IOException, Illeg
try{
String result = caller.isbnToPpn(isbn);
return Utilitaire.parseJson(result);
} catch (UncategorizedSQLException ex) {
} catch (SQLException ex) {
if (ex.getMessage().contains("no ppn matched")) {
throw new IllegalPpnException("Aucune notice ne correspond à la recherche");
}
throw new IOException(ex);
} catch (JsonProcessingException ex) {
throw new IOException("Impossible de récupérer les ppn correspondant à cet identifiant");
} catch (UncategorizedSQLException e) {
throw new IOException("Incident technique lors de l'accès à la base de données");
}
}
}
9 changes: 8 additions & 1 deletion src/main/java/fr/abes/sudoc/service/IssnService.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package fr.abes.sudoc.service;

import fr.abes.sudoc.component.BaseXmlFunctionsCaller;
import fr.abes.sudoc.exception.IllegalPpnException;
import org.springframework.jdbc.UncategorizedSQLException;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.regex.Pattern;

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

@Override
public List<String> getPpnFromIdentifiant(String issn) throws IOException {
public List<String> getPpnFromIdentifiant(String issn) throws IOException, IllegalPpnException {
try{
return caller.issnToPpn(issn.replace("-", ""));
} catch (UncategorizedSQLException ex) {
throw new IOException("Incident technique lors de l'accès à la base de données");
} catch (SQLException ex) {
if (ex.getMessage().contains("no ppn matched")) {
throw new IllegalPpnException("Aucune notice ne correspond à la recherche");
}
throw new IOException(ex);
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/main/java/fr/abes/sudoc/service/NoticeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ public NoticeXml getNoticeByPpn(String ppn) throws IllegalPpnException, IOExcept
}
}
}

/*if (noticeOpt.isPresent()) {
try {
return xmlMapper.readValue(noticeOpt.get().getDataXml().getCharacterStream(), NoticeXml.class);
} catch (SQLException e) {
log.error(e.getMessage());
}
}*/
return null;
}

Expand Down
8 changes: 3 additions & 5 deletions src/test/java/fr/abes/sudoc/service/IsbnServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.jdbc.UncategorizedSQLException;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import java.io.IOException;
import java.sql.SQLException;
import java.sql.SQLRecoverableException;

@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = {IsbnService.class, BaseXmlFunctionsCaller.class})
Expand Down Expand Up @@ -101,12 +99,12 @@ void checkFormatIsbn13Characters() {

@Test
@DisplayName("getPpnFromIdentifiant with UncategorizedSQLException")
void testgetPpnUncategorizedException() {
UncategorizedSQLException ex = new UncategorizedSQLException("no ppn matched", "select test", new SQLException());
void testgetPpnUncategorizedException() throws SQLException {
SQLException ex = new SQLException("no ppn matched", "select test", new SQLException());
Mockito.doThrow(ex).when(caller).isbnToPpn(Mockito.anyString());
Assertions.assertThrows(IllegalPpnException.class, () -> isbnService.getPpnFromIdentifiant("1111111111"));

ex = new UncategorizedSQLException("trululu", "select test", new SQLException());
ex = new SQLException("trululu", "select test", new SQLException());
Mockito.doThrow(ex).when(caller).isbnToPpn(Mockito.anyString());
Assertions.assertThrows(IOException.class, () -> isbnService.getPpnFromIdentifiant("1111111111"));
}
Expand Down
5 changes: 2 additions & 3 deletions src/test/java/fr/abes/sudoc/service/IssnServiceTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package fr.abes.sudoc.service;

import fr.abes.sudoc.component.BaseXmlFunctionsCaller;
import fr.abes.sudoc.exception.IllegalPpnException;
import fr.abes.sudoc.utils.Utilitaire;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
Expand All @@ -15,7 +14,7 @@
import org.springframework.test.context.junit.jupiter.SpringExtension;

import java.io.IOException;
import java.sql.SQLRecoverableException;
import java.sql.SQLException;

@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = {IssnService.class, BaseXmlFunctionsCaller.class})
Expand Down Expand Up @@ -95,7 +94,7 @@ void checkFormatIssn9Characters() {

@Test
@DisplayName("getPpnFromIdentifiant with UncategorizedSQLException")
void testgetPpnUncategorizedException() {
void testgetPpnUncategorizedException() throws SQLException {
Mockito.doThrow(UncategorizedSQLException.class).when(caller).issnToPpn(Mockito.anyString());
Assertions.assertThrows(IOException.class, () -> issnService.getPpnFromIdentifiant("11111111"));
}
Expand Down