|
1 /* |
|
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
3 * |
|
4 * Copyright 2018 Mike Becker. All rights reserved. |
|
5 * |
|
6 * Redistribution and use in source and binary forms, with or without |
|
7 * modification, are permitted provided that the following conditions are met: |
|
8 * |
|
9 * 1. Redistributions of source code must retain the above copyright |
|
10 * notice, this list of conditions and the following disclaimer. |
|
11 * |
|
12 * 2. Redistributions in binary form must reproduce the above copyright |
|
13 * notice, this list of conditions and the following disclaimer in the |
|
14 * documentation and/or other materials provided with the distribution. |
|
15 * |
|
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
26 * POSSIBILITY OF SUCH DAMAGE. |
|
27 * |
|
28 */ |
|
29 package de.uapcore.lightpit.dao.postgres; |
|
30 |
|
31 import de.uapcore.lightpit.dao.IssueDao; |
|
32 import de.uapcore.lightpit.entities.*; |
|
33 |
|
34 import java.sql.Connection; |
|
35 import java.sql.PreparedStatement; |
|
36 import java.sql.ResultSet; |
|
37 import java.sql.SQLException; |
|
38 import java.util.ArrayList; |
|
39 import java.util.List; |
|
40 import java.util.Objects; |
|
41 |
|
42 import static de.uapcore.lightpit.dao.Functions.*; |
|
43 |
|
44 public final class PGIssueDao implements IssueDao { |
|
45 |
|
46 private final PreparedStatement insert, update, list, find; |
|
47 |
|
48 public PGIssueDao(Connection connection) throws SQLException { |
|
49 list = connection.prepareStatement( |
|
50 "select id, project, status, category, subject, description, " + |
|
51 "vplan.id, vplan.name, vdone.id, vdone.name, " + |
|
52 "created, updated, eta " + |
|
53 "from lpit_issue " + |
|
54 "left join lpit_version vplan on vplan.id = version_plan " + |
|
55 "left join lpit_version vdone on vdone.id = version_done " + |
|
56 "where project = ? "); |
|
57 |
|
58 find = connection.prepareStatement( |
|
59 "select id, project, status, category, subject, description, " + |
|
60 "vplan.id, vplan.name, vdone.id, vdone.name, " + |
|
61 "created, updated, eta " + |
|
62 "from lpit_issue " + |
|
63 "left join lpit_version vplan on vplan.id = version_plan " + |
|
64 "left join lpit_version vdone on vdone.id = version_done " + |
|
65 "where id = ? "); |
|
66 |
|
67 insert = connection.prepareStatement( |
|
68 "insert into lpit_issue (project, status, category, subject, description, version_plan, version_done, eta) " + |
|
69 "values (?, ?::issue_status, ?::issue_category, ?, ?, ?, ?, ?)" |
|
70 ); |
|
71 update = connection.prepareStatement( |
|
72 "update lpit_issue set updated = now(), status = ?::issue_status, category = ?::issue_category, " + |
|
73 "subject = ?, description = ?, version_plan = ?, version_done = ?, eta = ? where id = ?" |
|
74 ); |
|
75 } |
|
76 |
|
77 private Version obtainVersion(ResultSet result, Project project, String prefix) throws SQLException { |
|
78 final int vplan = result.getInt(prefix+"id"); |
|
79 if (vplan > 0) { |
|
80 final var ver = new Version(vplan, project); |
|
81 ver.setName(result.getString(prefix+"name")); |
|
82 return ver; |
|
83 } else { |
|
84 return null; |
|
85 } |
|
86 } |
|
87 |
|
88 public Issue mapColumns(ResultSet result) throws SQLException { |
|
89 final var project = new Project(result.getInt("project")); |
|
90 final var issue = new Issue(result.getInt("id"), project); |
|
91 issue.setStatus(IssueStatus.valueOf(result.getString("status"))); |
|
92 issue.setCategory(IssueCategory.valueOf(result.getString("category"))); |
|
93 issue.setSubject(result.getString("subject")); |
|
94 issue.setDescription(result.getString("description")); |
|
95 issue.setScheduledVersion(obtainVersion(result, project, "vplan.")); |
|
96 issue.setResolvedVersion(obtainVersion(result, project, "vdone.")); |
|
97 issue.setCreated(result.getTimestamp("created")); |
|
98 issue.setUpdated(result.getTimestamp("updated")); |
|
99 issue.setEta(result.getDate("eta")); |
|
100 return issue; |
|
101 } |
|
102 |
|
103 @Override |
|
104 public void save(Issue instance) throws SQLException { |
|
105 Objects.requireNonNull(instance.getSubject()); |
|
106 Objects.requireNonNull(instance.getProject()); |
|
107 insert.setInt(1, instance.getProject().getId()); |
|
108 insert.setString(2, instance.getStatus().name()); |
|
109 insert.setString(3, instance.getCategory().name()); |
|
110 insert.setString(4, instance.getSubject()); |
|
111 setStringOrNull(insert, 5, instance.getDescription()); |
|
112 setForeignKeyOrNull(insert, 6, instance.getScheduledVersion(), Version::getId); |
|
113 setForeignKeyOrNull(insert, 7, instance.getResolvedVersion(), Version::getId); |
|
114 setDateOrNull(insert, 8, instance.getEta()); |
|
115 insert.executeUpdate(); |
|
116 } |
|
117 |
|
118 @Override |
|
119 public boolean update(Issue instance) throws SQLException { |
|
120 Objects.requireNonNull(instance.getSubject()); |
|
121 update.setString(1, instance.getStatus().name()); |
|
122 update.setString(2, instance.getCategory().name()); |
|
123 update.setString(3, instance.getSubject()); |
|
124 setStringOrNull(update, 4, instance.getDescription()); |
|
125 setForeignKeyOrNull(update, 5, instance.getScheduledVersion(), Version::getId); |
|
126 setForeignKeyOrNull(update, 6, instance.getResolvedVersion(), Version::getId); |
|
127 setDateOrNull(update, 7, instance.getEta()); |
|
128 update.setInt(8, instance.getId()); |
|
129 return update.executeUpdate() > 0; |
|
130 } |
|
131 |
|
132 @Override |
|
133 public List<Issue> list(Project project) throws SQLException { |
|
134 list.setInt(1, project.getId()); |
|
135 List<Issue> versions = new ArrayList<>(); |
|
136 try (var result = list.executeQuery()) { |
|
137 while (result.next()) { |
|
138 versions.add(mapColumns(result)); |
|
139 } |
|
140 } |
|
141 return versions; |
|
142 } |
|
143 |
|
144 @Override |
|
145 public Issue find(int id) throws SQLException { |
|
146 find.setInt(1, id); |
|
147 try (var result = find.executeQuery()) { |
|
148 if (result.next()) { |
|
149 return mapColumns(result); |
|
150 } else { |
|
151 return null; |
|
152 } |
|
153 } |
|
154 } |
|
155 } |