log api by id

This commit is contained in:
Michele Artini 2023-12-14 14:18:34 +01:00
parent d7c220e88d
commit 8faf3ae71e
2 changed files with 28 additions and 0 deletions

View File

@ -13,6 +13,7 @@ import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import eu.dnetlib.organizations.utils.DatabaseUtils;
@ -24,6 +25,22 @@ public class PublicApiController {
@Autowired
private DatabaseUtils dbUtils;
@GetMapping("/logs")
public void findJournalById(@RequestParam final String id,
final HttpServletResponse res) throws IOException {
final String filename = String.format("%s.log", id.replaceAll(":", "_"));
res.setContentType(MediaType.TEXT_PLAIN.getType());
res.setHeader("Content-Disposition", "attachment; filename=" + filename);
try (final ServletOutputStream out = res.getOutputStream()) {
for (final String s : dbUtils.obtainLogEntries(id)) {
IOUtils.write(s, out, StandardCharsets.UTF_8);
}
}
}
@GetMapping("/logs/{year}/{month}/{country}")
public void findJournalByCountry(@PathVariable final int year,
@PathVariable final int month,

View File

@ -682,6 +682,17 @@ public class DatabaseUtils {
persistentOrganizationRepository.deleteById(id);
}
public List<String> obtainLogEntries(final String id) {
final String query = "SELECT o.id, o.name, j.operation, j.description, date(j.op_date) as op_date "
+ "FROM organizations o JOIN journal j ON o.id = j.id "
+ "WHERE o.id=? "
+ "ORDER BY date(j.op_date)";
return jdbcTemplate.queryForList(query, id)
.stream()
.map(this::asLogEntry)
.collect(Collectors.toList());
}
public List<String> obtainLogEntries(final int year, final int month, final String country) {
final String query = "SELECT o.id, o.name, j.operation, j.description, date(j.op_date) as op_date "
+ "FROM organizations o JOIN journal j ON o.id = j.id "