diff --git a/src/main/java/it/inaf/oats/vospace/persistence/JobDAO.java b/src/main/java/it/inaf/oats/vospace/persistence/JobDAO.java index 03a9e05968ccfe371a310562b5ae1bbceaa39aed..c231c2c764b3e07bc9cb117b9cae51d2340058c1 100644 --- a/src/main/java/it/inaf/oats/vospace/persistence/JobDAO.java +++ b/src/main/java/it/inaf/oats/vospace/persistence/JobDAO.java @@ -51,8 +51,8 @@ public class JobDAO { public void createJob(JobSummary jobSummary) { - String sql = - "INSERT INTO job(job_id, owner_id, job_type, phase, job_info," + String sql + = "INSERT INTO job(job_id, owner_id, job_type, phase, job_info," + " error_message, error_type, error_has_detail, error_detail) " + "VALUES (?, ?, ?, ?, ?, ? ,? ,? ,?)"; @@ -63,9 +63,9 @@ public class JobDAO { ps.setObject(++i, getJobDirection(jobSummary), Types.VARCHAR); ps.setObject(++i, jobSummary.getPhase().value(), Types.OTHER); ps.setObject(++i, toJson(jobSummary.getJobInfo()), Types.OTHER); - + ErrorSummary errorSummary = jobSummary.getErrorSummary(); - if(errorSummary != null) { + if (errorSummary != null) { ps.setString(++i, errorSummary.getMessage()); ps.setObject(++i, errorSummary.getType().value(), Types.OTHER); ps.setBoolean(++i, errorSummary.isHasDetail()); @@ -125,7 +125,10 @@ public class JobDAO { jobSummary.setPhase(ExecutionPhase.fromValue(rs.getString("phase"))); jobSummary.setJobInfo(getJobPayload(rs.getString("job_info"))); jobSummary.setResults(getResults(rs.getString("results"))); - + jobSummary.setCreationTime(toXMLGregorianCalendar(rs.getTimestamp("creation_time"))); + jobSummary.setStartTime(toXMLGregorianCalendar(rs.getTimestamp("start_time"))); + jobSummary.setEndTime(toXMLGregorianCalendar(rs.getTimestamp("end_time"))); + // Retrieve error information if any String errorType = rs.getString("error_type"); if (errorType != null) { @@ -137,7 +140,7 @@ public class JobDAO { jobSummary.setErrorSummary(errorSummary); } - + return jobSummary; } @@ -263,30 +266,27 @@ public class JobDAO { public void updateJob(JobSummary job) { String sql = "UPDATE job SET (phase, results"; - + ErrorSummary errorSummary = job.getErrorSummary(); - if(errorSummary != null) - { + if (errorSummary != null) { sql += ", error_message, error_type, error_has_detail, error_detail"; - } - + } + sql += ") = (?, ?"; - - if(errorSummary != null) - { + + if (errorSummary != null) { sql += ", ?, ?, ?, ?"; } - + sql += ") WHERE job_id = ?"; jdbcTemplate.update(sql, ps -> { int i = 0; ps.setObject(++i, job.getPhase().name(), Types.OTHER); ps.setObject(++i, toJson(job.getResults()), Types.OTHER); - if(errorSummary != null) - { + if (errorSummary != null) { ps.setString(++i, errorSummary.getMessage()); - ps.setObject(++i, errorSummary.getType().value(), Types.OTHER); + ps.setObject(++i, errorSummary.getType().value(), Types.OTHER); ps.setBoolean(++i, errorSummary.isHasDetail()); ps.setString(++i, errorSummary.getDetailMessage()); } @@ -303,24 +303,28 @@ public class JobDAO { } public static XMLGregorianCalendar toXMLGregorianCalendar(Timestamp t) { - XMLGregorianCalendar cal = null; - try { - cal = DatatypeFactory.newInstance().newXMLGregorianCalendar(); - - LocalDateTime ldt = t.toLocalDateTime(); - - cal.setYear(ldt.getYear()); - cal.setMonth(ldt.getMonthValue()); - cal.setDay(ldt.getDayOfMonth()); - cal.setHour(ldt.getHour()); - cal.setMinute(ldt.getMinute()); - cal.setSecond(ldt.getSecond()); - cal.setFractionalSecond(new BigDecimal("0." + ldt.getNano())); - - } catch (Exception e) { - LOG.error("Error while generating XMLGregorianCalendar", e); + if (t != null) { + try { + XMLGregorianCalendar cal = DatatypeFactory.newInstance().newXMLGregorianCalendar(); + + LocalDateTime ldt = t.toLocalDateTime(); + + cal.setYear(ldt.getYear()); + cal.setMonth(ldt.getMonthValue()); + cal.setDay(ldt.getDayOfMonth()); + cal.setHour(ldt.getHour()); + cal.setMinute(ldt.getMinute()); + cal.setSecond(ldt.getSecond()); + cal.setFractionalSecond(new BigDecimal("0." + ldt.getNano())); + + // return calendar only if it has been fully initialized (otherwise + // toString issue could appear); return null in other cases. + return cal; + } catch (Exception e) { + LOG.error("Error while generating XMLGregorianCalendar", e); + } } - return cal; + return null; } } diff --git a/src/test/java/it/inaf/oats/vospace/persistence/JobDAOTest.java b/src/test/java/it/inaf/oats/vospace/persistence/JobDAOTest.java index a18f8d4a891fd836882b548d0b71dc4e242477e1..47e806004305f5d84c847ccdb631dade1353ce4b 100644 --- a/src/test/java/it/inaf/oats/vospace/persistence/JobDAOTest.java +++ b/src/test/java/it/inaf/oats/vospace/persistence/JobDAOTest.java @@ -29,6 +29,8 @@ import net.ivoa.xml.uws.v1.Jobs; import it.inaf.oats.vospace.exception.ErrorSummaryFactory; import it.inaf.oats.vospace.exception.PermissionDeniedException; import java.util.Arrays; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; @ExtendWith(SpringExtension.class) @ContextConfiguration(classes = {DataSourceConfig.class}) @@ -80,11 +82,16 @@ public class JobDAOTest { // uses the job retrieved from DAO to perform the update (reproduced a bug in job update) job = dao.getJob("123").get(); + assertNotNull(job.getCreationTime()); + assertNull(job.getStartTime()); job.setPhase(ExecutionPhase.EXECUTING); dao.updateJob(job); - assertEquals(ExecutionPhase.EXECUTING, dao.getJob("123").get().getPhase()); + job = dao.getJob("123").get(); + assertEquals(ExecutionPhase.EXECUTING, job.getPhase()); + assertNotNull(job.getStartTime()); + assertNull(job.getEndTime()); } @Test @@ -143,6 +150,7 @@ public class JobDAOTest { JobSummary retrievedJob = retrievedJobOpt.get(); assertEquals(ExecutionPhase.ERROR, retrievedJob.getPhase()); assertTrue(areEqual(job.getErrorSummary(), retrievedJob.getErrorSummary())); + assertNotNull(retrievedJob.getEndTime()); } @Test