Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package it.inaf.oats.vospace.persistence;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;
import java.util.Optional;
import javax.sql.DataSource;
import net.ivoa.xml.uws.v1.ExecutionPhase;
import net.ivoa.xml.uws.v1.JobSummary;
import net.ivoa.xml.uws.v1.ResultReference;
import net.ivoa.xml.vospace.v2.Transfer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
@Repository
public class JobDAO {
private static final ObjectMapper MAPPER = new ObjectMapper();
private final JdbcTemplate jdbcTemplate;
@Autowired
public JobDAO(DataSource dataSource) {
jdbcTemplate = new JdbcTemplate(dataSource);
}
public void createJob(JobSummary jobSummary) {
Object jobPayload = getJobPayload(jobSummary);
String sql = "INSERT INTO job(job_id, owner_id, job_type, phase, job_info) VALUES (?, ?, ?, ?, ?)";
jdbcTemplate.update(sql, ps -> {
int i = 0;
ps.setString(++i, jobSummary.getJobId());
ps.setString(++i, jobSummary.getOwnerId());
ps.setObject(++i, getJobType(jobPayload), Types.OTHER);
ps.setObject(++i, jobSummary.getPhase().value(), Types.OTHER);
ps.setObject(++i, toJson(jobPayload), Types.OTHER);
});
}
private Object getJobPayload(JobSummary jobSummary) {
List<Object> payload = jobSummary.getJobInfo().getAny();
if (payload.isEmpty()) {
throw new IllegalArgumentException("Empty JobInfo");
}
if (payload.size() > 1) {
throw new UnsupportedOperationException("JobInfo as list not supported");
}
return payload.get(0);
}
private String getJobType(Object jobPayload) {
if (jobPayload instanceof Transfer) {
Transfer transfer = (Transfer) jobPayload;
return transfer.getDirection();
}
throw new UnsupportedOperationException("JobInfo of type " + jobPayload + " not supported");
}
public Optional<JobSummary> getJob(String jobId) {
String sql = "SELECT * FROM job WHERE job_id = ?";
JobSummary jobSummary = jdbcTemplate.query(sql,
ps -> {
ps.setString(1, jobId);
}, rs -> {
if (rs.next()) {
return getJobSummaryFromResultSet(rs);
}
return null;
});
return Optional.ofNullable(jobSummary);
}
private JobSummary getJobSummaryFromResultSet(ResultSet rs) throws SQLException {
JobSummary jobSummary = new JobSummary();
jobSummary.setJobId(rs.getString("job_id"));
jobSummary.setOwnerId(rs.getString("owner_id"));
jobSummary.setPhase(ExecutionPhase.fromValue(rs.getString("phase")));
String jobType = rs.getString("job_type");
Object jobPayload = getJobPayload(jobType, rs.getString("job_info"));
JobSummary.JobInfo jobInfo = new JobSummary.JobInfo();
jobInfo.getAny().add(jobPayload);
jobSummary.setJobInfo(jobInfo);
jobSummary.setResults(getResults(rs.getString("results")));
return jobSummary;
}
private Object getJobPayload(String jobType, String json) {
try {
// TODO: switch on jobType
return MAPPER.readValue(json, Transfer.class);
} catch (JsonProcessingException ex) {
throw new RuntimeException(ex);
}
}
private List<ResultReference> getResults(String json) {
if (json == null) {
return null;
}
try {
JavaType type = MAPPER.getTypeFactory().constructCollectionType(List.class, ResultReference.class);
return MAPPER.readValue(json, type);
} catch (JsonProcessingException ex) {
throw new RuntimeException(ex);
}
}
public void updateJob(JobSummary job) {
String sql = "UPDATE job SET phase = ?, results = ? 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);
ps.setString(++i, job.getJobId());
});
}
private String toJson(Object data) {
try {
return MAPPER.writeValueAsString(data);
} catch (JsonProcessingException ex) {
throw new RuntimeException(ex);
}
}
}