1 | // Copyright 2004-2007 Jean-Francois Poilpret |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | package net.sourceforge.hiveboard.dao; |
16 | |
17 | import java.sql.SQLException; |
18 | import java.util.Date; |
19 | import java.util.HashSet; |
20 | import java.util.List; |
21 | import java.util.Map; |
22 | import java.util.Set; |
23 | |
24 | import net.sourceforge.hiveboard.Board; |
25 | import net.sourceforge.hiveboard.StaleObjectException; |
26 | import net.sourceforge.hivetranse.transaction.ibatis.AbstractSqlMapClientDAO; |
27 | |
28 | import com.ibatis.sqlmap.client.SqlMapClient; |
29 | |
30 | public class BoardDAOImpl extends AbstractSqlMapClientDAO |
31 | { |
32 | public BoardDAOImpl(SqlMapClient client) |
33 | { |
34 | super(client); |
35 | } |
36 | |
37 | public Map<Integer, Board> selectActiveBoards() throws SQLException |
38 | { |
39 | Map<Integer, Board> boards = queryForMap("SelectActiveBoards", null, "id"); |
40 | setBoardsParticipants(boards); |
41 | return boards; |
42 | } |
43 | |
44 | public Map<Integer, Board> selectAllBoards() throws SQLException |
45 | { |
46 | Map<Integer, Board> boards = queryForMap("SelectAllBoards", null, "id"); |
47 | setBoardsParticipants(boards); |
48 | return boards; |
49 | } |
50 | |
51 | public int selectMaxDiscriminator() throws SQLException |
52 | { |
53 | Integer max = (Integer) _client.queryForObject("SelectMaxBoardDiscriminator", null); |
54 | if (max != null) |
55 | { |
56 | return max.intValue(); |
57 | } |
58 | else |
59 | { |
60 | return 0; |
61 | } |
62 | } |
63 | |
64 | private void setBoardsParticipants(Map<Integer, Board> boards) throws SQLException |
65 | { |
66 | List<ParticipantDTO> participants = queryForList("SelectAllParticipants", null); |
67 | for (ParticipantDTO dto: participants) |
68 | { |
69 | Board board = boards.get(dto.getBoard()); |
70 | if (board != null) |
71 | { |
72 | board.getParticipants().add(dto.getAccount()); |
73 | } |
74 | } |
75 | } |
76 | |
77 | public void insertBoard(Board board) throws SQLException |
78 | { |
79 | _client.insert("InsertBoard", board); |
80 | // Insert all participants (in batch mode) |
81 | _client.startBatch(); |
82 | for (int id: board.getParticipants()) |
83 | { |
84 | insertParticipant(board.getId(), id); |
85 | } |
86 | _client.executeBatch(); |
87 | } |
88 | |
89 | public void updateBoard(Board board) throws SQLException |
90 | { |
91 | if (_client.update("UpdateBoard", board) == 1) |
92 | { |
93 | board.setVersion(board.getVersion() + 1); |
94 | } |
95 | else |
96 | { |
97 | throw new StaleObjectException( |
98 | "Board has been modified by another system [" + board + "]"); |
99 | } |
100 | |
101 | // Use batch mode |
102 | _client.startBatch(); |
103 | |
104 | // Update participants |
105 | // - select all participants for board in DB |
106 | List<Integer> list = queryForList("SelectParticipants", board.getId()); |
107 | Set<Integer> dbParticipants = new HashSet<Integer>(list); |
108 | Set<Integer> boardParticipants = board.getParticipants(); |
109 | // - calculate difference Memory \ DB => insert |
110 | Set<Integer> insertedParticipants = new HashSet<Integer>(boardParticipants); |
111 | insertedParticipants.removeAll(dbParticipants); |
112 | for (int participant: insertedParticipants) |
113 | { |
114 | insertParticipant(board.getId(), participant); |
115 | } |
116 | // - calculate DB \ Memory => delete |
117 | Set<Integer> deletedParticipants = new HashSet<Integer>(dbParticipants); |
118 | deletedParticipants.removeAll(boardParticipants); |
119 | for (int participant: deletedParticipants) |
120 | { |
121 | deleteParticipant(board.getId(), participant); |
122 | } |
123 | _client.executeBatch(); |
124 | } |
125 | |
126 | public void insertParticipant(int board, int account) throws SQLException |
127 | { |
128 | _client.insert("InsertParticipant", getParticipantDTO(board, account)); |
129 | } |
130 | |
131 | public void deleteParticipant(int board, int account) throws SQLException |
132 | { |
133 | _client.delete("DeleteParticipant", getParticipantDTO(board, account)); |
134 | } |
135 | |
136 | public void deleteParticipant(int account) throws SQLException |
137 | { |
138 | _client.delete("DeleteAccountParticipant", account); |
139 | } |
140 | |
141 | private ParticipantDTO getParticipantDTO(int board, int account) |
142 | throws SQLException |
143 | { |
144 | ParticipantDTO dto = new ParticipantDTO(); |
145 | dto.setAccount(account); |
146 | dto.setBoard(board); |
147 | return dto; |
148 | } |
149 | |
150 | public void insertComment(int board, int author, String comment, Date date) |
151 | throws SQLException |
152 | { |
153 | CommentDTO dto = new CommentDTO(); |
154 | dto.setBoard(board); |
155 | dto.setAuthor(author); |
156 | dto.setComment(comment); |
157 | dto.setDate(date); |
158 | _client.insert("InsertComment", dto); |
159 | } |
160 | |
161 | public List<CommentDTO> selectComments(int board, Date after, Date until) |
162 | throws SQLException |
163 | { |
164 | CommentSearchCriteria criteria = new CommentSearchCriteria(); |
165 | criteria.setBoard(board); |
166 | criteria.setAfter(after); |
167 | criteria.setUntil(until); |
168 | return queryForList("SelectComments", criteria); |
169 | } |
170 | |
171 | public int selectCurrentSnapshot(int board) throws SQLException |
172 | { |
173 | Integer id = (Integer) _client.queryForObject("SelectCurrentSnapshot", board); |
174 | if (id == null) |
175 | { |
176 | return -1; |
177 | } |
178 | else |
179 | { |
180 | return id.intValue(); |
181 | } |
182 | } |
183 | |
184 | public int insertSnapshot(int board, boolean current, byte[] content) |
185 | throws SQLException |
186 | { |
187 | SnapshotDTO dto = new SnapshotDTO(); |
188 | dto.setBoard(board); |
189 | dto.setCurrent(current); |
190 | dto.setDate(new Date()); |
191 | dto.setContent(content); |
192 | _client.insert("InsertSnapshot", dto); |
193 | _client.insert("InsertSnapshotContent", dto); |
194 | return dto.getId(); |
195 | } |
196 | |
197 | public void updateSnapshot(int id, byte[] content) throws SQLException |
198 | { |
199 | SnapshotDTO dto = new SnapshotDTO(); |
200 | dto.setId(id); |
201 | dto.setDate(new Date()); |
202 | dto.setContent(content); |
203 | _client.update("UpdateSnapshot", dto); |
204 | _client.update("UpdateSnapshotContent", dto); |
205 | } |
206 | |
207 | public SnapshotDTO selectSnapshot(int idSnapshot) throws SQLException |
208 | { |
209 | return (SnapshotDTO) _client.queryForObject("SelectSnapshot", idSnapshot); |
210 | } |
211 | |
212 | |
213 | public void deleteSnapshots(int board) throws SQLException |
214 | { |
215 | _client.delete("DeleteSnapshotsContents", board); |
216 | _client.delete("DeleteSnapshots", board); |
217 | } |
218 | |
219 | public List<SnapshotDTO> selectSnapshots(int board) throws SQLException |
220 | { |
221 | return queryForList("SelectSnapshots", board); |
222 | } |
223 | } |