44 import static de.uapcore.lightpit.dao.Functions.setStringOrNull; |
44 import static de.uapcore.lightpit.dao.Functions.setStringOrNull; |
45 |
45 |
46 public final class PGProjectDao implements ProjectDao { |
46 public final class PGProjectDao implements ProjectDao { |
47 |
47 |
48 private final PreparedStatement insert, update, list, find; |
48 private final PreparedStatement insert, update, list, find; |
|
49 private final PreparedStatement issue_summary; |
49 |
50 |
50 public PGProjectDao(Connection connection) throws SQLException { |
51 public PGProjectDao(Connection connection) throws SQLException { |
51 list = connection.prepareStatement( |
52 list = connection.prepareStatement( |
52 "select projectid, name, description, repourl, " + |
53 "select projectid, name, description, repourl, " + |
53 "userid, username, lastname, givenname, mail " + |
54 "userid, username, lastname, givenname, mail " + |
59 "select projectid, name, description, repourl, " + |
60 "select projectid, name, description, repourl, " + |
60 "userid, username, lastname, givenname, mail " + |
61 "userid, username, lastname, givenname, mail " + |
61 "from lpit_project " + |
62 "from lpit_project " + |
62 "left join lpit_user owner on lpit_project.owner = owner.userid " + |
63 "left join lpit_user owner on lpit_project.owner = owner.userid " + |
63 "where projectid = ?"); |
64 "where projectid = ?"); |
|
65 |
|
66 issue_summary = connection.prepareStatement( |
|
67 "select phase, count(*) as total "+ |
|
68 "from lpit_issue " + |
|
69 "join lpit_issue_phases using(status) " + |
|
70 "where project = ? "+ |
|
71 "group by phase " |
|
72 ); |
64 |
73 |
65 insert = connection.prepareStatement( |
74 insert = connection.prepareStatement( |
66 "insert into lpit_project (name, description, repourl, owner) values (?, ?, ?, ?)" |
75 "insert into lpit_project (name, description, repourl, owner) values (?, ?, ?, ?)" |
67 ); |
76 ); |
68 update = connection.prepareStatement( |
77 update = connection.prepareStatement( |
85 user.setMail(result.getString("mail")); |
94 user.setMail(result.getString("mail")); |
86 proj.setOwner(user); |
95 proj.setOwner(user); |
87 } |
96 } |
88 |
97 |
89 return proj; |
98 return proj; |
|
99 } |
|
100 |
|
101 private void mapIssueSummary(Project proj) throws SQLException { |
|
102 issue_summary.setInt(1, proj.getId()); |
|
103 final var result = issue_summary.executeQuery(); |
|
104 while (result.next()) { |
|
105 final var phase = result.getInt("phase"); |
|
106 final var total = result.getInt("total"); |
|
107 switch(phase) { |
|
108 case 0: |
|
109 proj.setOpenIssues(total); |
|
110 break; |
|
111 case 1: |
|
112 proj.setActiveIssues(total); |
|
113 break; |
|
114 case 2: |
|
115 proj.setDoneIssues(total); |
|
116 break; |
|
117 } |
|
118 } |
90 } |
119 } |
91 |
120 |
92 @Override |
121 @Override |
93 public void save(Project instance) throws SQLException { |
122 public void save(Project instance) throws SQLException { |
94 Objects.requireNonNull(instance.getName()); |
123 Objects.requireNonNull(instance.getName()); |
114 @Override |
143 @Override |
115 public List<Project> list() throws SQLException { |
144 public List<Project> list() throws SQLException { |
116 List<Project> projects = new ArrayList<>(); |
145 List<Project> projects = new ArrayList<>(); |
117 try (var result = list.executeQuery()) { |
146 try (var result = list.executeQuery()) { |
118 while (result.next()) { |
147 while (result.next()) { |
119 projects.add(mapColumns(result)); |
148 final var project = mapColumns(result); |
|
149 mapIssueSummary(project); |
|
150 projects.add(project); |
120 } |
151 } |
121 } |
152 } |
122 return projects; |
153 return projects; |
123 } |
154 } |
124 |
155 |
125 @Override |
156 @Override |
126 public Project find(int id) throws SQLException { |
157 public Project find(int id) throws SQLException { |
127 find.setInt(1, id); |
158 find.setInt(1, id); |
128 try (var result = find.executeQuery()) { |
159 try (var result = find.executeQuery()) { |
129 if (result.next()) { |
160 if (result.next()) { |
130 return mapColumns(result); |
161 final var project = mapColumns(result); |
|
162 mapIssueSummary(project); |
|
163 return project; |
131 } else { |
164 } else { |
132 return null; |
165 return null; |
133 } |
166 } |
134 } |
167 } |
135 } |
168 } |