1 | |
package org.jtheque.films.services.impl.utils.file.jt.writer; |
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.file.IFileManager; |
21 | |
import org.jtheque.core.managers.file.able.BasicDataSource; |
22 | |
import org.jtheque.core.managers.log.ILoggingManager; |
23 | |
import org.jtheque.core.utils.file.jt.JTFileWriter; |
24 | |
import org.jtheque.films.persistence.od.able.Film; |
25 | |
import org.jtheque.films.services.impl.utils.file.jt.sources.JTFDataSource; |
26 | |
import org.jtheque.films.utils.Constants; |
27 | |
import org.jtheque.primary.od.able.Country; |
28 | |
import org.jtheque.primary.od.able.Kind; |
29 | |
import org.jtheque.primary.od.able.Person; |
30 | |
import org.jtheque.utils.bean.IntDate; |
31 | |
import org.jtheque.utils.io.FileUtils; |
32 | |
|
33 | |
import java.io.DataOutputStream; |
34 | |
import java.io.IOException; |
35 | |
import java.util.Collection; |
36 | |
import java.util.HashSet; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | 0 | public final class JTFFileWriter extends JTFileWriter { |
44 | |
@Override |
45 | |
public void writeFile(DataOutputStream stream, BasicDataSource source) { |
46 | 0 | JTFDataSource dataSource = (JTFDataSource) source; |
47 | |
|
48 | |
try { |
49 | 0 | writeHeader(stream, dataSource); |
50 | |
|
51 | 0 | Film film = dataSource.getFilm(); |
52 | |
|
53 | 0 | stream.writeUTF(Managers.getManager(IFileManager.class).formatUTFToWrite(film.getTitle())); |
54 | 0 | stream.writeInt(film.getYear()); |
55 | 0 | stream.writeInt(film.getDuration()); |
56 | 0 | stream.writeInt(film.getNote().getValue().intValue()); |
57 | 0 | stream.writeUTF(Managers.getManager(IFileManager.class).formatUTFToWrite(film.getResume())); |
58 | 0 | stream.writeUTF(Managers.getManager(IFileManager.class).formatUTFToWrite(film.getComment())); |
59 | |
|
60 | 0 | stream.writeLong(Constants.Files.JT.JTCATEGORYSEPARATOR); |
61 | |
|
62 | 0 | Collection<Country> countries = new HashSet<Country>(10); |
63 | |
|
64 | 0 | writeActors(stream, film, countries); |
65 | 0 | writeRealizer(stream, film, countries); |
66 | 0 | writeLanguage(stream, film); |
67 | 0 | writeKinds(stream, film); |
68 | 0 | writeType(stream, film); |
69 | 0 | writeCountries(stream, countries); |
70 | 0 | } catch (IOException e) { |
71 | 0 | Managers.getManager(ILoggingManager.class).getLogger(getClass()).error(e); |
72 | |
} finally { |
73 | 0 | FileUtils.close(stream); |
74 | 0 | } |
75 | 0 | } |
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
private static void writeHeader(DataOutputStream stream, BasicDataSource dataSource) throws IOException { |
85 | 0 | stream.writeUTF(FileUtils.encryptKey(Constants.Files.JT.JTKEY)); |
86 | 0 | stream.writeUTF(dataSource.getVersion().getVersion()); |
87 | 0 | stream.writeInt(dataSource.getFileVersion()); |
88 | 0 | stream.writeInt(IntDate.today().intValue()); |
89 | |
|
90 | 0 | stream.writeLong(Constants.Files.JT.JTCATEGORYSEPARATOR); |
91 | 0 | } |
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
private static void writeActors(DataOutputStream stream, Film film, Collection<Country> countries) throws IOException { |
102 | 0 | if (film.hasActors()) { |
103 | 0 | stream.writeInt(Constants.Files.JT.NO_CONTENT); |
104 | |
} else { |
105 | 0 | stream.writeInt(Constants.Files.JT.CONTENT); |
106 | |
|
107 | 0 | boolean first = true; |
108 | |
|
109 | 0 | for (Person actor : film.getActors()) { |
110 | 0 | if (first) { |
111 | 0 | first = false; |
112 | |
} else { |
113 | 0 | stream.writeLong(Constants.Files.JT.JTINTERNSEPARATOR); |
114 | |
} |
115 | |
|
116 | 0 | stream.writeUTF(Managers.getManager(IFileManager.class).formatUTFToWrite(actor.getName())); |
117 | 0 | stream.writeUTF(Managers.getManager(IFileManager.class).formatUTFToWrite(actor.getFirstName())); |
118 | 0 | stream.writeInt(actor.getTheCountry().getId()); |
119 | 0 | stream.writeInt(actor.getNote().getValue().intValue()); |
120 | |
|
121 | 0 | countries.add(actor.getTheCountry()); |
122 | |
} |
123 | |
} |
124 | |
|
125 | 0 | stream.writeLong(Constants.Files.JT.JTCATEGORYSEPARATOR); |
126 | 0 | } |
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
private static void writeRealizer(DataOutputStream stream, Film film, Collection<Country> countries) throws IOException { |
137 | 0 | if (film.hasRealizer()) { |
138 | 0 | stream.writeInt(Constants.Files.JT.NO_CONTENT); |
139 | |
} else { |
140 | 0 | stream.writeInt(Constants.Files.JT.CONTENT); |
141 | |
|
142 | 0 | Person realizer = film.getTheRealizer(); |
143 | |
|
144 | 0 | stream.writeUTF(Managers.getManager(IFileManager.class).formatUTFToWrite(realizer.getName())); |
145 | 0 | stream.writeUTF(Managers.getManager(IFileManager.class).formatUTFToWrite(realizer.getFirstName())); |
146 | 0 | stream.writeInt(realizer.getTheCountry().getId()); |
147 | 0 | stream.writeInt(realizer.getNote().getValue().intValue()); |
148 | |
|
149 | 0 | if (realizer.hasCountry()) { |
150 | 0 | countries.add(realizer.getTheCountry()); |
151 | |
} |
152 | |
} |
153 | |
|
154 | 0 | stream.writeLong(Constants.Files.JT.JTCATEGORYSEPARATOR); |
155 | 0 | } |
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
private static void writeLanguage(DataOutputStream stream, Film film) throws IOException { |
165 | 0 | if (film.hasLanguage()) { |
166 | 0 | stream.writeInt(Constants.Files.JT.NO_CONTENT); |
167 | |
} else { |
168 | 0 | stream.writeInt(Constants.Files.JT.CONTENT); |
169 | |
|
170 | 0 | stream.writeUTF(Managers.getManager(IFileManager.class).formatUTFToWrite(film.getTheLanguage().getName())); |
171 | |
} |
172 | |
|
173 | 0 | stream.writeLong(Constants.Files.JT.JTCATEGORYSEPARATOR); |
174 | 0 | } |
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
private static void writeKinds(DataOutputStream stream, Film film) throws IOException { |
184 | 0 | if (film.hasKinds()) { |
185 | 0 | stream.writeInt(Constants.Files.JT.NO_CONTENT); |
186 | |
} else { |
187 | 0 | stream.writeInt(Constants.Files.JT.CONTENT); |
188 | |
|
189 | 0 | boolean first = true; |
190 | |
|
191 | 0 | for (Kind kind : film.getKinds()) { |
192 | 0 | if (first) { |
193 | 0 | first = false; |
194 | |
} else { |
195 | 0 | stream.writeLong(Constants.Files.JT.JTINTERNSEPARATOR); |
196 | |
} |
197 | |
|
198 | 0 | stream.writeUTF(Managers.getManager(IFileManager.class).formatUTFToWrite(kind.getName())); |
199 | |
} |
200 | |
} |
201 | |
|
202 | 0 | stream.writeLong(Constants.Files.JT.JTCATEGORYSEPARATOR); |
203 | 0 | } |
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
private static void writeType(DataOutputStream stream, Film film) throws IOException { |
213 | 0 | if (film.getTheType() == null) { |
214 | 0 | stream.writeInt(Constants.Files.JT.NO_CONTENT); |
215 | |
} else { |
216 | 0 | stream.writeInt(Constants.Files.JT.CONTENT); |
217 | |
|
218 | 0 | stream.writeUTF(Managers.getManager(IFileManager.class).formatUTFToWrite(film.getTheType().getName())); |
219 | |
} |
220 | |
|
221 | 0 | stream.writeLong(Constants.Files.JT.JTCATEGORYSEPARATOR); |
222 | 0 | } |
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
private static void writeCountries(DataOutputStream stream, Collection<Country> countries) throws IOException { |
232 | 0 | if (countries.isEmpty()) { |
233 | 0 | stream.writeInt(Constants.Files.JT.NO_CONTENT); |
234 | |
} else { |
235 | 0 | stream.writeInt(Constants.Files.JT.CONTENT); |
236 | |
|
237 | 0 | boolean first = true; |
238 | |
|
239 | 0 | for (Country country : countries) { |
240 | 0 | if (first) { |
241 | 0 | first = false; |
242 | |
} else { |
243 | 0 | stream.writeLong(Constants.Files.JT.JTINTERNSEPARATOR); |
244 | |
} |
245 | |
|
246 | 0 | stream.writeInt(country.getId()); |
247 | 0 | stream.writeUTF(Managers.getManager(IFileManager.class).formatUTFToWrite(country.getName())); |
248 | |
} |
249 | |
} |
250 | |
|
251 | 0 | stream.writeLong(Constants.Files.JT.JTCATEGORYSEPARATOR); |
252 | 0 | } |
253 | |
} |