1 | |
package org.jtheque.films.services.impl.utils.file.restore; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
import org.jtheque.core.managers.Managers; |
20 | |
import org.jtheque.core.managers.beans.IBeansManager; |
21 | |
import org.jtheque.core.managers.file.able.BackupReader; |
22 | |
import org.jtheque.core.managers.log.ILoggingManager; |
23 | |
import org.jtheque.films.persistence.od.able.Film; |
24 | |
import org.jtheque.films.services.able.IActorService; |
25 | |
import org.jtheque.films.services.able.IFilmsService; |
26 | |
import org.jtheque.films.services.able.INotesService; |
27 | |
import org.jtheque.films.services.able.IRealizersService; |
28 | |
import org.jtheque.films.services.impl.utils.file.imports.ImporterUtils; |
29 | |
import org.jtheque.primary.od.able.Country; |
30 | |
import org.jtheque.primary.od.able.Kind; |
31 | |
import org.jtheque.primary.od.able.Language; |
32 | |
import org.jtheque.primary.od.able.Lending; |
33 | |
import org.jtheque.primary.od.able.Person; |
34 | |
import org.jtheque.primary.od.able.Type; |
35 | |
import org.jtheque.primary.services.able.IBorrowersService; |
36 | |
import org.jtheque.primary.services.able.ICountriesService; |
37 | |
import org.jtheque.primary.services.able.IKindsService; |
38 | |
import org.jtheque.primary.services.able.ILanguagesService; |
39 | |
import org.jtheque.primary.services.able.ILendingsService; |
40 | |
import org.jtheque.primary.services.able.ITypesService; |
41 | |
import org.jtheque.utils.DatabaseException; |
42 | |
import org.jtheque.utils.DatabaseUtils; |
43 | |
import org.jtheque.utils.bean.IntDate; |
44 | |
|
45 | |
import javax.annotation.Resource; |
46 | |
import java.sql.Connection; |
47 | |
import java.sql.PreparedStatement; |
48 | |
import java.sql.ResultSet; |
49 | |
import java.sql.SQLException; |
50 | |
import java.sql.Statement; |
51 | |
import java.util.ArrayList; |
52 | |
import java.util.Arrays; |
53 | |
import java.util.Collection; |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
public final class DBV4BackupReader implements BackupReader { |
61 | |
@Resource |
62 | |
private INotesService notesService; |
63 | |
|
64 | |
@Resource |
65 | |
private IBorrowersService borrowersService; |
66 | |
|
67 | |
@Resource |
68 | |
private ICountriesService countriesService; |
69 | |
|
70 | |
@Resource |
71 | |
private ILendingsService lendingsService; |
72 | |
|
73 | |
@Resource |
74 | |
private ILanguagesService languagesService; |
75 | |
|
76 | |
@Resource |
77 | |
private ITypesService typesService; |
78 | |
|
79 | |
@Resource |
80 | |
private IKindsService kindsService; |
81 | |
|
82 | |
@Resource |
83 | |
private IActorService actorService; |
84 | |
|
85 | |
@Resource |
86 | |
private IFilmsService filmsService; |
87 | |
|
88 | |
@Resource |
89 | |
private IRealizersService realizersService; |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
public DBV4BackupReader() { |
95 | 0 | super(); |
96 | |
|
97 | 0 | Managers.getManager(IBeansManager.class).inject(this); |
98 | 0 | } |
99 | |
|
100 | |
@Override |
101 | |
public void readBackup(Object object) { |
102 | 0 | Connection connection = (Connection) object; |
103 | |
|
104 | |
try { |
105 | 0 | Collection<Film> films = importFilms(connection); |
106 | 0 | Collection<Person> actors = importActors(connection); |
107 | 0 | Collection<Person> realizers = importRealizers(connection); |
108 | 0 | Collection<Kind> kinds = importKinds(connection); |
109 | 0 | Collection<Type> types = importTypes(connection); |
110 | 0 | Collection<Language> languages = importLanguages(connection); |
111 | 0 | Collection<Country> countries = importCountries(connection); |
112 | 0 | Collection<Person> borrowers = importBorrowers(connection); |
113 | 0 | Collection<Lending> lendings = importLendings(connection); |
114 | |
|
115 | 0 | ImporterUtils.persistDataOfImport(films, actors, realizers, kinds, types, |
116 | |
languages, countries, borrowers, lendings); |
117 | 0 | } catch (DatabaseException e) { |
118 | 0 | Managers.getManager(ILoggingManager.class).getLogger(getClass()).error(e); |
119 | 0 | } |
120 | 0 | } |
121 | |
|
122 | |
@Override |
123 | |
public void persistTheData() { |
124 | 0 | } |
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
private Collection<Film> importFilms(Connection connection) throws DatabaseException { |
134 | 0 | Collection<Film> films = new ArrayList<Film>(25); |
135 | |
|
136 | 0 | Statement statement = null; |
137 | 0 | ResultSet result = null; |
138 | |
try { |
139 | 0 | statement = connection.createStatement(); |
140 | 0 | result = statement.executeQuery("SELECT * FROM t_films"); |
141 | |
|
142 | 0 | while (result.next()) { |
143 | 0 | Film film = filmsService.getEmptyFilm(); |
144 | |
|
145 | 0 | importValuesOfFilm(result, film); |
146 | 0 | importActorsOfFilm(connection, film); |
147 | |
|
148 | 0 | films.add(film); |
149 | 0 | } |
150 | 0 | } catch (SQLException e) { |
151 | 0 | throw new DatabaseException(e); |
152 | |
} finally { |
153 | 0 | DatabaseUtils.close(result); |
154 | 0 | DatabaseUtils.close(statement); |
155 | 0 | } |
156 | |
|
157 | 0 | return films; |
158 | |
} |
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
private void importValuesOfFilm(ResultSet result, Film film) throws SQLException { |
168 | 0 | film.getTemporaryContext().setId(result.getInt("ID")); |
169 | 0 | film.setTitle(result.getString("title")); |
170 | 0 | film.getTemporaryContext().setKinds(Arrays.asList(result.getInt("kind"))); |
171 | 0 | film.getTemporaryContext().setType(result.getInt("type")); |
172 | 0 | film.setYear(result.getInt("year")); |
173 | 0 | film.setDuration(result.getInt("duration")); |
174 | 0 | film.getTemporaryContext().setRealizer(result.getInt("realizer")); |
175 | 0 | film.getTemporaryContext().setLanguage(result.getInt("language")); |
176 | 0 | film.setNote(notesService.getDefaultNote()); |
177 | 0 | film.setResume(result.getString("resume")); |
178 | 0 | film.setComment(result.getString("comment")); |
179 | 0 | } |
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
private static void importActorsOfFilm(Connection connection, Film film) throws DatabaseException { |
189 | 0 | PreparedStatement statement2 = null; |
190 | 0 | ResultSet resultActors = null; |
191 | |
try { |
192 | 0 | statement2 = connection.prepareStatement("SELECT * FROM t_films_actors WHERE film = ?"); |
193 | |
|
194 | 0 | statement2.setInt(1, film.getTemporaryContext().getId()); |
195 | |
|
196 | 0 | resultActors = statement2.executeQuery(); |
197 | |
|
198 | 0 | film.getTemporaryContext().setActors(DatabaseUtils.getAllIntResults(resultActors, "actor")); |
199 | 0 | } catch (SQLException e) { |
200 | 0 | throw new DatabaseException(e); |
201 | |
} finally { |
202 | 0 | DatabaseUtils.close(resultActors); |
203 | 0 | DatabaseUtils.close(statement2); |
204 | 0 | } |
205 | 0 | } |
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
private Collection<Person> importActors(Connection connection) throws DatabaseException { |
215 | 0 | Collection<Person> actors = new ArrayList<Person>(50); |
216 | |
|
217 | 0 | Statement statement = null; |
218 | 0 | ResultSet result = null; |
219 | |
try { |
220 | 0 | statement = connection.createStatement(); |
221 | 0 | result = statement.executeQuery("SELECT * FROM t_actors"); |
222 | |
|
223 | 0 | while (result.next()) { |
224 | 0 | Person actor = actorService.getEmptyActor(); |
225 | |
|
226 | 0 | actor.getTemporaryContext().setId(result.getInt("ID")); |
227 | 0 | actor.setName(result.getString("name")); |
228 | 0 | actor.setFirstName(result.getString("firstname")); |
229 | 0 | actor.getTemporaryContext().setCountry(result.getInt("country")); |
230 | 0 | actor.setNote(notesService.getDefaultNote()); |
231 | |
|
232 | 0 | actors.add(actor); |
233 | 0 | } |
234 | 0 | } catch (SQLException e) { |
235 | 0 | throw new DatabaseException(e); |
236 | |
} finally { |
237 | 0 | DatabaseUtils.close(result); |
238 | 0 | DatabaseUtils.close(statement); |
239 | 0 | } |
240 | |
|
241 | 0 | return actors; |
242 | |
} |
243 | |
|
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
private Collection<Person> importRealizers(Connection connection) throws DatabaseException { |
252 | 0 | Collection<Person> realizers = new ArrayList<Person>(15); |
253 | |
|
254 | 0 | Statement statement = null; |
255 | 0 | ResultSet result = null; |
256 | |
try { |
257 | 0 | statement = connection.createStatement(); |
258 | 0 | result = statement.executeQuery("SELECT * FROM t_realizers"); |
259 | |
|
260 | 0 | while (result.next()) { |
261 | 0 | Person realizer = realizersService.getEmptyRealizer(); |
262 | |
|
263 | 0 | realizer.getTemporaryContext().setId(result.getInt("ID")); |
264 | 0 | realizer.setName(result.getString("name")); |
265 | 0 | realizer.setFirstName(result.getString("firstname")); |
266 | 0 | realizer.getTemporaryContext().setCountry(result.getInt("country")); |
267 | 0 | realizer.setNote(notesService.getDefaultNote()); |
268 | |
|
269 | 0 | realizers.add(realizer); |
270 | 0 | } |
271 | 0 | } catch (SQLException e) { |
272 | 0 | throw new DatabaseException(e); |
273 | |
} finally { |
274 | 0 | DatabaseUtils.close(result); |
275 | 0 | DatabaseUtils.close(statement); |
276 | 0 | } |
277 | |
|
278 | 0 | return realizers; |
279 | |
} |
280 | |
|
281 | |
|
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | |
|
287 | |
|
288 | |
private Collection<Kind> importKinds(Connection connection) throws DatabaseException { |
289 | 0 | Collection<Kind> kinds = new ArrayList<Kind>(10); |
290 | |
|
291 | 0 | Statement statement = null; |
292 | 0 | ResultSet result = null; |
293 | |
try { |
294 | 0 | statement = connection.createStatement(); |
295 | 0 | result = statement.executeQuery("SELECT * FROM t_kinds"); |
296 | |
|
297 | 0 | while (result.next()) { |
298 | 0 | Kind kind = kindsService.getEmptyKind(); |
299 | |
|
300 | 0 | kind.getTemporaryContext().setId(result.getInt("ID")); |
301 | 0 | kind.setName(result.getString("name")); |
302 | |
|
303 | 0 | kinds.add(kind); |
304 | 0 | } |
305 | 0 | } catch (SQLException e) { |
306 | 0 | throw new DatabaseException(e); |
307 | |
} finally { |
308 | 0 | DatabaseUtils.close(result); |
309 | 0 | DatabaseUtils.close(statement); |
310 | 0 | } |
311 | |
|
312 | 0 | return kinds; |
313 | |
} |
314 | |
|
315 | |
|
316 | |
|
317 | |
|
318 | |
|
319 | |
|
320 | |
|
321 | |
|
322 | |
private Collection<Type> importTypes(Connection connection) throws DatabaseException { |
323 | 0 | Collection<Type> types = new ArrayList<Type>(10); |
324 | |
|
325 | 0 | Statement statement = null; |
326 | 0 | ResultSet result = null; |
327 | |
try { |
328 | 0 | statement = connection.createStatement(); |
329 | 0 | result = statement.executeQuery("SELECT * FROM t_types"); |
330 | |
|
331 | 0 | while (result.next()) { |
332 | 0 | Type type = typesService.getEmptyType(); |
333 | |
|
334 | 0 | type.getTemporaryContext().setId(result.getInt("ID")); |
335 | 0 | type.setName(result.getString("name")); |
336 | |
|
337 | 0 | types.add(type); |
338 | 0 | } |
339 | 0 | } catch (SQLException e) { |
340 | 0 | throw new DatabaseException(e); |
341 | |
} finally { |
342 | 0 | DatabaseUtils.close(result); |
343 | 0 | DatabaseUtils.close(statement); |
344 | 0 | } |
345 | |
|
346 | 0 | return types; |
347 | |
} |
348 | |
|
349 | |
|
350 | |
|
351 | |
|
352 | |
|
353 | |
|
354 | |
|
355 | |
|
356 | |
private Collection<Language> importLanguages(Connection connection) throws DatabaseException { |
357 | 0 | Collection<Language> languages = new ArrayList<Language>(10); |
358 | |
|
359 | 0 | Statement statement = null; |
360 | 0 | ResultSet result = null; |
361 | |
try { |
362 | 0 | statement = connection.createStatement(); |
363 | 0 | result = statement.executeQuery("SELECT * FROM t_languages"); |
364 | |
|
365 | 0 | while (result.next()) { |
366 | 0 | Language language = languagesService.getEmptyLanguage(); |
367 | |
|
368 | 0 | language.getTemporaryContext().setId(result.getInt("ID")); |
369 | 0 | language.setName(result.getString("name")); |
370 | |
|
371 | 0 | languages.add(language); |
372 | 0 | } |
373 | 0 | } catch (SQLException e) { |
374 | 0 | throw new DatabaseException(e); |
375 | |
} finally { |
376 | 0 | DatabaseUtils.close(result); |
377 | 0 | DatabaseUtils.close(statement); |
378 | 0 | } |
379 | |
|
380 | 0 | return languages; |
381 | |
} |
382 | |
|
383 | |
|
384 | |
|
385 | |
|
386 | |
|
387 | |
|
388 | |
|
389 | |
|
390 | |
private Collection<Country> importCountries(Connection connection) throws DatabaseException { |
391 | 0 | Collection<Country> countries = new ArrayList<Country>(10); |
392 | |
|
393 | 0 | Statement statement = null; |
394 | 0 | ResultSet result = null; |
395 | |
try { |
396 | 0 | statement = connection.createStatement(); |
397 | 0 | result = statement.executeQuery("SELECT * FROM t_countries"); |
398 | |
|
399 | 0 | while (result.next()) { |
400 | 0 | Country country = countriesService.getEmptyCountry(); |
401 | |
|
402 | 0 | country.getTemporaryContext().setId(result.getInt("ID")); |
403 | 0 | country.setName(result.getString("name")); |
404 | |
|
405 | 0 | countries.add(country); |
406 | 0 | } |
407 | 0 | } catch (SQLException e) { |
408 | 0 | throw new DatabaseException(e); |
409 | |
} finally { |
410 | 0 | DatabaseUtils.close(result); |
411 | 0 | DatabaseUtils.close(statement); |
412 | 0 | } |
413 | |
|
414 | 0 | return countries; |
415 | |
} |
416 | |
|
417 | |
|
418 | |
|
419 | |
|
420 | |
|
421 | |
|
422 | |
|
423 | |
|
424 | |
private Collection<Person> importBorrowers(Connection connection) throws DatabaseException { |
425 | 0 | Collection<Person> borrowers = new ArrayList<Person>(10); |
426 | |
|
427 | 0 | Statement statement = null; |
428 | 0 | ResultSet result = null; |
429 | |
try { |
430 | 0 | statement = connection.createStatement(); |
431 | 0 | result = statement.executeQuery("SELECT * FROM t_borrowers"); |
432 | |
|
433 | 0 | while (result.next()) { |
434 | 0 | Person borrower = borrowersService.getEmptyBorrower(); |
435 | |
|
436 | 0 | borrower.setName(result.getString("name")); |
437 | 0 | borrower.setFirstName(result.getString("firstname")); |
438 | 0 | borrower.setEmail(result.getString("mail")); |
439 | |
|
440 | 0 | borrowers.add(borrower); |
441 | 0 | } |
442 | 0 | } catch (SQLException e) { |
443 | 0 | throw new DatabaseException(e); |
444 | |
} finally { |
445 | 0 | DatabaseUtils.close(result); |
446 | 0 | DatabaseUtils.close(statement); |
447 | 0 | } |
448 | |
|
449 | 0 | return borrowers; |
450 | |
} |
451 | |
|
452 | |
|
453 | |
|
454 | |
|
455 | |
|
456 | |
|
457 | |
|
458 | |
|
459 | |
private Collection<Lending> importLendings(Connection connection) throws DatabaseException { |
460 | 0 | Collection<Lending> lendings = new ArrayList<Lending>(10); |
461 | |
|
462 | 0 | Statement statement = null; |
463 | 0 | ResultSet result = null; |
464 | |
try { |
465 | 0 | statement = connection.createStatement(); |
466 | 0 | result = statement.executeQuery("SELECT * FROM t_lendings"); |
467 | |
|
468 | 0 | while (result.next()) { |
469 | 0 | Lending lending = lendingsService.getEmptyLending(); |
470 | |
|
471 | 0 | lending.setDate(new IntDate(result.getInt("date"))); |
472 | 0 | lending.getTemporaryContext().setFilm(result.getInt("film")); |
473 | 0 | lending.getTemporaryContext().setBorrower(result.getInt("borrower")); |
474 | |
|
475 | 0 | lendings.add(lending); |
476 | 0 | } |
477 | 0 | } catch (SQLException e) { |
478 | 0 | throw new DatabaseException(e); |
479 | |
} finally { |
480 | 0 | DatabaseUtils.close(result); |
481 | 0 | DatabaseUtils.close(statement); |
482 | 0 | } |
483 | |
|
484 | 0 | return lendings; |
485 | |
} |
486 | |
} |