1 | |
package org.jtheque.films.services.impl.utils.file.imports; |
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.utils.db.DaoNotes; |
22 | |
import org.jtheque.core.utils.db.DaoNotes.NoteType; |
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.IRealizersService; |
27 | |
import org.jtheque.films.services.impl.utils.DataUtils; |
28 | |
import org.jtheque.primary.od.able.Country; |
29 | |
import org.jtheque.primary.od.able.Kind; |
30 | |
import org.jtheque.primary.od.able.Language; |
31 | |
import org.jtheque.primary.od.able.Lending; |
32 | |
import org.jtheque.primary.od.able.Person; |
33 | |
import org.jtheque.primary.od.able.Type; |
34 | |
import org.jtheque.primary.services.able.IBorrowersService; |
35 | |
import org.jtheque.primary.services.able.ICountriesService; |
36 | |
import org.jtheque.primary.services.able.IKindsService; |
37 | |
import org.jtheque.primary.services.able.ILanguagesService; |
38 | |
import org.jtheque.primary.services.able.ILendingsService; |
39 | |
import org.jtheque.primary.services.able.ITypesService; |
40 | |
|
41 | |
import javax.annotation.Resource; |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
public final class ImporterUtils { |
49 | |
@Resource |
50 | |
private static ICountriesService countriesService; |
51 | |
|
52 | |
@Resource |
53 | |
private static ILendingsService lendingsService; |
54 | |
|
55 | |
@Resource |
56 | |
private static ITypesService typesService; |
57 | |
|
58 | |
@Resource |
59 | |
private static ILanguagesService languagesService; |
60 | |
|
61 | |
@Resource |
62 | |
private static IBorrowersService borrowersService; |
63 | |
|
64 | |
@Resource |
65 | |
private static IFilmsService filmsService; |
66 | |
|
67 | |
@Resource |
68 | |
private static IActorService actorService; |
69 | |
|
70 | |
@Resource |
71 | |
private static IRealizersService realizersService; |
72 | |
|
73 | |
@Resource |
74 | |
private static IKindsService kindsService; |
75 | |
|
76 | 0 | private static final DaoNotes DAO_NOTES = DaoNotes.getInstance(); |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
private ImporterUtils() { |
82 | 0 | super(); |
83 | |
|
84 | 0 | Managers.getManager(IBeansManager.class).inject(this); |
85 | 0 | } |
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
public static void persistDataOfImport(Iterable<Film> films, Iterable<Person> actors, |
101 | |
Iterable<Person> realizers, Iterable<Kind> kinds, |
102 | |
Iterable<Type> types, Iterable<Language> languages, |
103 | |
Iterable<Country> countries, |
104 | |
Iterable<Person> borrowers, Iterable<Lending> lendings) { |
105 | |
|
106 | 0 | countriesService.createAll(countries); |
107 | 0 | borrowersService.createAll(borrowers); |
108 | 0 | languagesService.createAll(languages); |
109 | 0 | typesService.createAll(types); |
110 | 0 | kindsService.createAll(kinds); |
111 | |
|
112 | 0 | persistRealizers(realizers, countries); |
113 | 0 | persistActors(actors, countries); |
114 | 0 | persistFilms(films, actors, realizers, kinds, types, languages); |
115 | 0 | persistLendings(lendings, films, borrowers); |
116 | 0 | } |
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
private static void persistRealizers(Iterable<Person> realizers, Iterable<Country> countries) { |
125 | 0 | for (Person realizer : realizers) { |
126 | 0 | realizer.setTheCountry(DataUtils.getDataByTemporaryId(realizer.getTemporaryContext().getCountry(), countries)); |
127 | 0 | realizer.setNote(DAO_NOTES.getNote(NoteType.getEnum(realizer.getTemporaryContext().getIntNote()))); |
128 | |
|
129 | 0 | realizersService.create(realizer); |
130 | |
} |
131 | 0 | } |
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
private static void persistActors(Iterable<Person> actors, Iterable<Country> countries) { |
140 | 0 | for (Person actor : actors) { |
141 | 0 | actor.setTheCountry(DataUtils.getDataByTemporaryId(actor.getTemporaryContext().getCountry(), countries)); |
142 | 0 | actor.setNote(DAO_NOTES.getNote(NoteType.getEnum(actor.getTemporaryContext().getIntNote()))); |
143 | |
|
144 | 0 | actorService.create(actor); |
145 | |
} |
146 | 0 | } |
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
private static void persistFilms(Iterable<Film> films, Iterable<Person> actors, Iterable<Person> realizers, Iterable<Kind> kinds, Iterable<Type> types, Iterable<Language> languages) { |
159 | 0 | for (Film film : films) { |
160 | 0 | film.setTheLanguage(DataUtils.getDataByTemporaryId(film.getTemporaryContext().getLanguage(), languages)); |
161 | 0 | film.setTheType(DataUtils.getDataByTemporaryId(film.getTemporaryContext().getType(), types)); |
162 | 0 | film.setTheRealizer(DataUtils.getDataByTemporaryId(film.getTemporaryContext().getRealizer(), realizers)); |
163 | |
|
164 | 0 | for (Integer i : film.getTemporaryContext().getActors()) { |
165 | 0 | Person actor = DataUtils.getDataByTemporaryId(i, actors); |
166 | |
|
167 | 0 | if (actor != null) { |
168 | 0 | film.addActor(actor); |
169 | |
} |
170 | 0 | } |
171 | |
|
172 | 0 | for (Integer i : film.getTemporaryContext().getKinds()) { |
173 | 0 | Kind kind = DataUtils.getDataByTemporaryId(i, kinds); |
174 | |
|
175 | 0 | if (kind != null) { |
176 | 0 | film.addKind(kind); |
177 | |
} |
178 | 0 | } |
179 | |
|
180 | 0 | filmsService.create(film); |
181 | |
} |
182 | 0 | } |
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
private static void persistLendings(Iterable<Lending> lendings, Iterable<Film> films, Iterable<Person> borrowers) { |
192 | 0 | if (lendings != null) { |
193 | 0 | for (Lending lending : lendings) { |
194 | 0 | lending.setThePerson(DataUtils.getDataByTemporaryId(lending.getTemporaryContext().getBorrower(), borrowers)); |
195 | 0 | lending.setTheOther(DataUtils.getDataByTemporaryId(lending.getTemporaryContext().getFilm(), films).getId()); |
196 | |
|
197 | 0 | lendingsService.create(lending); |
198 | |
} |
199 | |
} |
200 | 0 | } |
201 | |
} |