cover: allow custom author covers
[fanfix.git] / src / be / nikiroo / fanfix / library / CacheLibrary.java
1 package be.nikiroo.fanfix.library;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.net.URL;
6 import java.util.List;
7
8 import be.nikiroo.fanfix.Instance;
9 import be.nikiroo.fanfix.bundles.UiConfig;
10 import be.nikiroo.fanfix.data.MetaData;
11 import be.nikiroo.fanfix.data.Story;
12 import be.nikiroo.utils.Image;
13 import be.nikiroo.utils.Progress;
14
15 /**
16 * This library will cache another pre-existing {@link BasicLibrary}.
17 *
18 * @author niki
19 */
20 public class CacheLibrary extends BasicLibrary {
21 private List<MetaData> metas;
22 private BasicLibrary lib;
23 private LocalLibrary cacheLib;
24
25 /**
26 * Create a cache library around the given one.
27 * <p>
28 * It will return the same result, but those will be saved to disk at the
29 * same time to be fetched quicker the next time.
30 *
31 * @param cacheDir
32 * the cache directory where to save the files to disk
33 * @param lib
34 * the original library to wrap
35 */
36 public CacheLibrary(File cacheDir, BasicLibrary lib) {
37 this.cacheLib = new LocalLibrary(cacheDir, Instance.getUiConfig()
38 .getString(UiConfig.GUI_NON_IMAGES_DOCUMENT_TYPE), Instance
39 .getUiConfig().getString(UiConfig.GUI_IMAGES_DOCUMENT_TYPE),
40 true);
41 this.lib = lib;
42 }
43
44 @Override
45 public String getLibraryName() {
46 return lib.getLibraryName();
47 }
48
49 @Override
50 public Status getStatus() {
51 return lib.getStatus();
52 }
53
54 @Override
55 protected List<MetaData> getMetas(Progress pg) {
56 if (pg == null) {
57 pg = new Progress();
58 }
59
60 if (metas == null) {
61 metas = lib.getMetas(pg);
62 }
63
64 pg.done();
65 return metas;
66 }
67
68 @Override
69 public synchronized File getFile(final String luid, Progress pg) {
70 if (pg == null) {
71 pg = new Progress();
72 }
73
74 Progress pgImport = new Progress();
75 Progress pgGet = new Progress();
76 Progress pgRecall = new Progress();
77
78 pg.setMinMax(0, 5);
79 pg.addProgress(pgImport, 3);
80 pg.addProgress(pgGet, 1);
81 pg.addProgress(pgRecall, 1);
82
83 if (!isCached(luid)) {
84 try {
85 cacheLib.imprt(lib, luid, pgImport);
86 updateInfo(cacheLib.getInfo(luid));
87 pgImport.done();
88 } catch (IOException e) {
89 Instance.getTraceHandler().error(e);
90 }
91
92 pgImport.done();
93 pgGet.done();
94 }
95
96 File file = cacheLib.getFile(luid, pgRecall);
97 pgRecall.done();
98
99 pg.done();
100 return file;
101 }
102
103 @Override
104 public Image getCover(final String luid) {
105 if (isCached(luid)) {
106 return cacheLib.getCover(luid);
107 }
108
109 // We could update the cache here, but it's not easy
110 return lib.getCover(luid);
111 }
112
113 @Override
114 public Image getSourceCover(String source) {
115 Image custom = getCustomSourceCover(source);
116 if (custom != null) {
117 return custom;
118 }
119
120 Image cached = cacheLib.getSourceCover(source);
121 if (cached != null) {
122 return cached;
123 }
124
125 return lib.getSourceCover(source);
126 }
127
128 @Override
129 public Image getAuthorCover(String author) {
130 Image custom = getCustomSourceCover(author);
131 if (custom != null) {
132 return custom;
133 }
134
135 Image cached = cacheLib.getSourceCover(author);
136 if (cached != null) {
137 return cached;
138 }
139
140 return lib.getSourceCover(author);
141
142 }
143
144 @Override
145 public Image getCustomSourceCover(String source) {
146 Image custom = cacheLib.getCustomSourceCover(source);
147 if (custom == null) {
148 custom = lib.getCustomSourceCover(source);
149 if (custom != null) {
150 cacheLib.setSourceCover(source, custom);
151 }
152 }
153
154 return custom;
155 }
156
157 @Override
158 public Image getCustomAuthorCover(String author) {
159 Image custom = cacheLib.getCustomAuthorCover(author);
160 if (custom == null) {
161 custom = lib.getCustomAuthorCover(author);
162 if (custom != null) {
163 cacheLib.setAuthorCover(author, custom);
164 }
165 }
166
167 return custom;
168 }
169
170 @Override
171 public void setSourceCover(String source, String luid) {
172 lib.setSourceCover(source, luid);
173 cacheLib.setSourceCover(source, getCover(luid));
174 }
175
176 @Override
177 public void setAuthorCover(String author, String luid) {
178 lib.setAuthorCover(author, luid);
179 cacheLib.setAuthorCover(author, getCover(luid));
180 }
181
182 @Override
183 protected void updateInfo(MetaData meta) {
184 if (meta != null && metas != null) {
185 for (int i = 0; i < metas.size(); i++) {
186 if (metas.get(i).getLuid().equals(meta.getLuid())) {
187 metas.set(i, meta);
188 }
189 }
190 }
191
192 cacheLib.updateInfo(meta);
193 lib.updateInfo(meta);
194 }
195
196 @Override
197 protected void invalidateInfo(String luid) {
198 if (luid == null) {
199 metas = null;
200 } else if (metas != null) {
201 for (int i = 0; i < metas.size(); i++) {
202 if (metas.get(i).getLuid().equals(luid)) {
203 metas.remove(i--);
204 }
205 }
206 }
207
208 cacheLib.invalidateInfo(luid);
209 lib.invalidateInfo(luid);
210 }
211
212 @Override
213 public synchronized Story save(Story story, String luid, Progress pg)
214 throws IOException {
215 Progress pgLib = new Progress();
216 Progress pgCacheLib = new Progress();
217
218 if (pg == null) {
219 pg = new Progress();
220 }
221
222 pg.setMinMax(0, 2);
223 pg.addProgress(pgLib, 1);
224 pg.addProgress(pgCacheLib, 1);
225
226 story = lib.save(story, luid, pgLib);
227 story = cacheLib.save(story, story.getMeta().getLuid(), pgCacheLib);
228
229 updateInfo(story.getMeta());
230
231 return story;
232 }
233
234 @Override
235 public synchronized void delete(String luid) throws IOException {
236 if (isCached(luid)) {
237 cacheLib.delete(luid);
238 }
239 lib.delete(luid);
240
241 MetaData meta = getInfo(luid);
242 if (meta != null) {
243 metas.remove(meta);
244 }
245 }
246
247 @Override
248 protected synchronized void changeSTA(String luid, String newSource,
249 String newTitle, String newAuthor, Progress pg) throws IOException {
250 if (pg == null) {
251 pg = new Progress();
252 }
253
254 Progress pgCache = new Progress();
255 Progress pgOrig = new Progress();
256 pg.setMinMax(0, 2);
257 pg.addProgress(pgCache, 1);
258 pg.addProgress(pgOrig, 1);
259
260 MetaData meta = getInfo(luid);
261 if (meta == null) {
262 throw new IOException("Story not found: " + luid);
263 }
264
265 if (isCached(luid)) {
266 cacheLib.changeSTA(luid, newSource, newTitle, newAuthor, pgCache);
267 }
268 pgCache.done();
269
270 lib.changeSTA(luid, newSource, newTitle, newAuthor, pgOrig);
271 pgOrig.done();
272
273 meta.setSource(newSource);
274 meta.setTitle(newTitle);
275 meta.setAuthor(newAuthor);
276 pg.done();
277 }
278
279 /**
280 * Check if the {@link Story} denoted by this Library UID is present in the
281 * cache.
282 *
283 * @param luid
284 * the Library UID
285 *
286 * @return TRUE if it is
287 */
288 public boolean isCached(String luid) {
289 return cacheLib.getInfo(luid) != null;
290 }
291
292 /**
293 * Clear the {@link Story} from the cache.
294 * <p>
295 * The next time we try to retrieve the {@link Story}, it may be required to
296 * cache it again.
297 *
298 * @param luid
299 * the story to clear
300 *
301 * @throws IOException
302 * in case of I/O error
303 */
304 public void clearFromCache(String luid) throws IOException {
305 if (isCached(luid)) {
306 cacheLib.delete(luid);
307 }
308 }
309
310 @Override
311 public Story imprt(URL url, Progress pg) throws IOException {
312 if (pg == null) {
313 pg = new Progress();
314 }
315
316 Progress pgImprt = new Progress();
317 Progress pgCache = new Progress();
318 pg.setMinMax(0, 10);
319 pg.addProgress(pgImprt, 7);
320 pg.addProgress(pgCache, 3);
321
322 Story story = lib.imprt(url, pgImprt);
323 cacheLib.save(story, story.getMeta().getLuid(), pgCache);
324
325 updateInfo(story.getMeta());
326
327 pg.done();
328 return story;
329 }
330
331 // All the following methods are only used by Save and Delete in
332 // BasicLibrary:
333
334 @Override
335 protected int getNextId() {
336 throw new java.lang.InternalError("Should not have been called");
337 }
338
339 @Override
340 protected void doDelete(String luid) throws IOException {
341 throw new java.lang.InternalError("Should not have been called");
342 }
343
344 @Override
345 protected Story doSave(Story story, Progress pg) throws IOException {
346 throw new java.lang.InternalError("Should not have been called");
347 }
348 }