src/main/java/de/uapcore/lightpit/dao/postgres/PGIssueDao.java

changeset 134
f47e82cd6077
parent 128
947d0f6a6a83
child 135
bafc315294fd
equal deleted inserted replaced
133:ef075cd7ce55 134:f47e82cd6077
29 package de.uapcore.lightpit.dao.postgres; 29 package de.uapcore.lightpit.dao.postgres;
30 30
31 import de.uapcore.lightpit.dao.IssueDao; 31 import de.uapcore.lightpit.dao.IssueDao;
32 import de.uapcore.lightpit.entities.*; 32 import de.uapcore.lightpit.entities.*;
33 33
34 import java.sql.Connection; 34 import java.sql.*;
35 import java.sql.PreparedStatement;
36 import java.sql.ResultSet;
37 import java.sql.SQLException;
38 import java.util.ArrayList; 35 import java.util.ArrayList;
39 import java.util.List; 36 import java.util.List;
40 import java.util.Objects; 37 import java.util.Objects;
38 import java.util.Optional;
41 39
42 import static de.uapcore.lightpit.dao.Functions.*; 40 import static de.uapcore.lightpit.dao.Functions.*;
43 41
44 public final class PGIssueDao implements IssueDao { 42 public final class PGIssueDao implements IssueDao {
45 43
49 private final PreparedStatement insertAffected, insertResolved; 47 private final PreparedStatement insertAffected, insertResolved;
50 private final PreparedStatement insertComment, updateComment, listComments; 48 private final PreparedStatement insertComment, updateComment, listComments;
51 49
52 public PGIssueDao(Connection connection) throws SQLException { 50 public PGIssueDao(Connection connection) throws SQLException {
53 list = connection.prepareStatement( 51 list = connection.prepareStatement(
54 "select issueid, project, p.name as projectname, status, category, subject, i.description, " + 52 "select issueid, i.project, p.name as projectname, component, c.name as componentname, " +
53 "status, category, subject, i.description, " +
55 "userid, username, givenname, lastname, mail, " + 54 "userid, username, givenname, lastname, mail, " +
56 "created, updated, eta " + 55 "created, updated, eta " +
57 "from lpit_issue i " + 56 "from lpit_issue i " +
58 "join lpit_project p on project = projectid " + 57 "join lpit_project p on i.project = projectid " +
58 "left join lpit_component c on component = c.id " +
59 "left join lpit_user on userid = assignee " + 59 "left join lpit_user on userid = assignee " +
60 "where project = ? "); 60 "where i.project = ? and coalesce(component, -1) = coalesce(?, component, -1)");
61 61
62 listForVersion = connection.prepareStatement( 62 listForVersion = connection.prepareStatement(
63 "with issue_version as ( "+ 63 "with issue_version as ( "+
64 "select issueid, versionid from lpit_issue_affected_version union "+ 64 "select issueid, versionid from lpit_issue_affected_version union "+
65 "select issueid, versionid from lpit_issue_resolved_version) "+ 65 "select issueid, versionid from lpit_issue_resolved_version) "+
66 "select issueid, project, p.name as projectname, status, category, subject, i.description, " + 66 "select issueid, i.project, p.name as projectname, component, c.name as componentname, " +
67 "status, category, subject, i.description, " +
67 "userid, username, givenname, lastname, mail, " + 68 "userid, username, givenname, lastname, mail, " +
68 "created, updated, eta " + 69 "created, updated, eta " +
69 "from lpit_issue i " + 70 "from lpit_issue i " +
70 "join lpit_project p on project = projectid " + 71 "join lpit_project p on i.project = projectid " +
72 "left join lpit_component c on component = c.id " +
71 "left join issue_version using (issueid) "+ 73 "left join issue_version using (issueid) "+
72 "left join lpit_user on userid = assignee " + 74 "left join lpit_user on userid = assignee " +
73 "where coalesce(versionid,-1) = ? " 75 "where coalesce(versionid,-1) = ? and coalesce(component, -1) = coalesce(?, component, -1)"
74 ); 76 );
75 77
76 find = connection.prepareStatement( 78 find = connection.prepareStatement(
77 "select issueid, project, p.name as projectname, status, category, subject, i.description, " + 79 "select issueid, i.project, p.name as projectname, component, c.name as componentname, " +
80 "status, category, subject, i.description, " +
78 "userid, username, givenname, lastname, mail, " + 81 "userid, username, givenname, lastname, mail, " +
79 "created, updated, eta " + 82 "created, updated, eta " +
80 "from lpit_issue i " + 83 "from lpit_issue i " +
81 "left join lpit_project p on project = projectid " + 84 "join lpit_project p on i.project = projectid " +
85 "left join lpit_component c on component = c.id " +
82 "left join lpit_user on userid = assignee " + 86 "left join lpit_user on userid = assignee " +
83 "where issueid = ? "); 87 "where issueid = ? ");
84 88
85 insert = connection.prepareStatement( 89 insert = connection.prepareStatement(
86 "insert into lpit_issue (project, status, category, subject, description, assignee, eta) " + 90 "insert into lpit_issue (project, component, status, category, subject, description, assignee, eta) " +
87 "values (?, ?::issue_status, ?::issue_category, ?, ?, ?, ?) returning issueid" 91 "values (?, ?::issue_status, ?::issue_category, ?, ?, ?, ?) returning issueid"
88 ); 92 );
89 update = connection.prepareStatement( 93 update = connection.prepareStatement(
90 "update lpit_issue set updated = now(), status = ?::issue_status, category = ?::issue_category, " + 94 "update lpit_issue set " +
95 "updated = now(), component = ?, status = ?::issue_status, category = ?::issue_category, " +
91 "subject = ?, description = ?, assignee = ?, eta = ? where issueid = ?" 96 "subject = ?, description = ?, assignee = ?, eta = ? where issueid = ?"
92 ); 97 );
93 98
94 affectedVersions = connection.prepareStatement( 99 affectedVersions = connection.prepareStatement(
95 "select versionid, name, status, ordinal " + 100 "select versionid, name, status, ordinal " +
121 } 126 }
122 127
123 private Issue mapColumns(ResultSet result) throws SQLException { 128 private Issue mapColumns(ResultSet result) throws SQLException {
124 final var project = new Project(result.getInt("project")); 129 final var project = new Project(result.getInt("project"));
125 project.setName(result.getString("projectname")); 130 project.setName(result.getString("projectname"));
131 var component = new Component(result.getInt("component"));
132 if (result.wasNull()) {
133 component = null;
134 } else {
135 component.setName(result.getString("componentname"));
136 }
126 final var issue = new Issue(result.getInt("issueid")); 137 final var issue = new Issue(result.getInt("issueid"));
127 issue.setProject(project); 138 issue.setProject(project);
139 issue.setComponent(component);
128 issue.setStatus(IssueStatus.valueOf(result.getString("status"))); 140 issue.setStatus(IssueStatus.valueOf(result.getString("status")));
129 issue.setCategory(IssueCategory.valueOf(result.getString("category"))); 141 issue.setCategory(IssueCategory.valueOf(result.getString("category")));
130 issue.setSubject(result.getString("subject")); 142 issue.setSubject(result.getString("subject"));
131 issue.setDescription(result.getString("description")); 143 issue.setDescription(result.getString("description"));
132 issue.setAssignee(PGUserDao.mapColumns(result)); 144 issue.setAssignee(PGUserDao.mapColumns(result));
159 insertResolved.setInt(2, v.getId()); 171 insertResolved.setInt(2, v.getId());
160 insertResolved.executeUpdate(); 172 insertResolved.executeUpdate();
161 } 173 }
162 } 174 }
163 175
176 private int setData(PreparedStatement stmt, int column, Issue instance) throws SQLException {
177 setForeignKeyOrNull(stmt, ++column, instance.getComponent(), Component::getId);
178 stmt.setString(++column, instance.getStatus().name());
179 stmt.setString(++column, instance.getCategory().name());
180 stmt.setString(++column, instance.getSubject());
181 setStringOrNull(stmt, ++column, instance.getDescription());
182 setForeignKeyOrNull(stmt, ++column, instance.getAssignee(), User::getId);
183 setDateOrNull(stmt, ++column, instance.getEta());
184 return column;
185 }
186
164 @Override 187 @Override
165 public void save(Issue instance, Project project) throws SQLException { 188 public void save(Issue instance, Project project) throws SQLException {
166 Objects.requireNonNull(instance.getSubject()); 189 Objects.requireNonNull(instance.getSubject());
167 instance.setProject(project); 190 instance.setProject(project);
168 insert.setInt(1, instance.getProject().getId()); 191 int column = 0;
169 insert.setString(2, instance.getStatus().name()); 192 insert.setInt(++column, instance.getProject().getId());
170 insert.setString(3, instance.getCategory().name()); 193 setData(insert, column, instance);
171 insert.setString(4, instance.getSubject());
172 setStringOrNull(insert, 5, instance.getDescription());
173 setForeignKeyOrNull(insert, 6, instance.getAssignee(), User::getId);
174 setDateOrNull(insert, 7, instance.getEta());
175 // insert and retrieve the ID 194 // insert and retrieve the ID
176 final var rs = insert.executeQuery(); 195 final var rs = insert.executeQuery();
177 rs.next(); 196 rs.next();
178 instance.setId(rs.getInt(1)); 197 instance.setId(rs.getInt(1));
179 updateVersionLists(instance); 198 updateVersionLists(instance);
181 200
182 @Override 201 @Override
183 public boolean update(Issue instance) throws SQLException { 202 public boolean update(Issue instance) throws SQLException {
184 if (instance.getId() < 0) return false; 203 if (instance.getId() < 0) return false;
185 Objects.requireNonNull(instance.getSubject()); 204 Objects.requireNonNull(instance.getSubject());
186 update.setString(1, instance.getStatus().name()); 205 int column = setData(update, 0, instance);
187 update.setString(2, instance.getCategory().name()); 206 update.setInt(++column, instance.getId());
188 update.setString(3, instance.getSubject());
189 setStringOrNull(update, 4, instance.getDescription());
190 setForeignKeyOrNull(update, 5, instance.getAssignee(), User::getId);
191 setDateOrNull(update, 6, instance.getEta());
192 update.setInt(7, instance.getId());
193 boolean success = update.executeUpdate() > 0; 207 boolean success = update.executeUpdate() > 0;
194 if (success) { 208 if (success) {
195 updateVersionLists(instance); 209 updateVersionLists(instance);
196 return true; 210 return true;
197 } else { 211 } else {
198 return false; 212 return false;
199 } 213 }
200 } 214 }
201 215
202 private List<Issue> list(PreparedStatement query, int arg) throws SQLException { 216 private List<Issue> executeQuery(PreparedStatement query) throws SQLException {
203 query.setInt(1, arg);
204 List<Issue> issues = new ArrayList<>(); 217 List<Issue> issues = new ArrayList<>();
205 try (var result = query.executeQuery()) { 218 try (var result = query.executeQuery()) {
206 while (result.next()) { 219 while (result.next()) {
207 issues.add(mapColumns(result)); 220 issues.add(mapColumns(result));
208 } 221 }
210 return issues; 223 return issues;
211 } 224 }
212 225
213 @Override 226 @Override
214 public List<Issue> list(Project project) throws SQLException { 227 public List<Issue> list(Project project) throws SQLException {
215 return list(list, project.getId()); 228 list.setInt(1, project.getId());
216 } 229 list.setNull(2, Types.INTEGER);
217 230 return executeQuery(list);
218 @Override 231 }
219 public List<Issue> list(Version version) throws SQLException { 232
220 return list(listForVersion, version == null ? -1 : version.getId()); 233 @Override
234 public List<Issue> list(Project project, Component component, Version version) throws SQLException {
235 listForVersion.setInt(1, Optional.ofNullable(version).map(Version::getId).orElse(-1));
236 listForVersion.setInt(2, Optional.ofNullable(component).map(Component::getId).orElse(-1));
237 return executeQuery(listForVersion);
238 }
239
240 @Override
241 public List<Issue> list(Project project, Version version) throws SQLException {
242 listForVersion.setInt(1, Optional.ofNullable(version).map(Version::getId).orElse(-1));
243 listForVersion.setNull(2, Types.INTEGER);
244 return executeQuery(listForVersion);
245 }
246
247 @Override
248 public List<Issue> list(Project project, Component component) throws SQLException {
249 list.setInt(1, project.getId());
250 list.setInt(2, Optional.ofNullable(component).map(Component::getId).orElse(-1));
251 return executeQuery(list);
221 } 252 }
222 253
223 @Override 254 @Override
224 public Issue find(int id) throws SQLException { 255 public Issue find(int id) throws SQLException {
225 find.setInt(1, id); 256 find.setInt(1, id);

mercurial