Fix cover file not sent over wire
[nikiroo-utils.git] / src / be / nikiroo / fanfix / library / CacheLibrary.java
CommitLineData
ff05b828
NR
1package be.nikiroo.fanfix.library;
2
3import java.awt.image.BufferedImage;
4import java.io.File;
5import java.io.IOException;
6import java.util.List;
7
8import be.nikiroo.fanfix.Instance;
9import be.nikiroo.fanfix.bundles.UiConfig;
10import be.nikiroo.fanfix.data.MetaData;
11import be.nikiroo.fanfix.data.Story;
12import be.nikiroo.utils.Progress;
13
14/**
15 * This library will cache another pre-existing {@link BasicLibrary}.
16 *
17 * @author niki
18 */
19public class CacheLibrary extends BasicLibrary {
20 private List<MetaData> metas;
21 private BasicLibrary lib;
22 private LocalLibrary cacheLib;
23
24 /**
25 * Create a cache library around the given one.
26 * <p>
27 * It will return the same result, but those will be saved to disk at the
28 * same time to be fetched quicker the next time.
29 *
30 * @param cacheDir
31 * the cache directory where to save the files to disk
32 * @param lib
33 * the original library to wrap
34 */
35 public CacheLibrary(File cacheDir, BasicLibrary lib) {
36 this.cacheLib = new LocalLibrary(cacheDir, Instance.getUiConfig()
37 .getString(UiConfig.GUI_NON_IMAGES_DOCUMENT_TYPE), Instance
38 .getUiConfig().getString(UiConfig.GUI_IMAGES_DOCUMENT_TYPE),
39 true);
40 this.lib = lib;
41 }
42
43 @Override
44 public String getLibraryName() {
45 return lib.getLibraryName();
46 }
47
48 @Override
49 protected List<MetaData> getMetas(Progress pg) {
50 if (pg == null) {
51 pg = new Progress();
52 }
53
54 if (metas == null) {
55 metas = lib.getMetas(pg);
56 }
57
58 pg.done();
59 return metas;
60 }
61
62 @Override
63 public synchronized File getFile(final String luid, Progress pg) {
64 if (pg == null) {
65 pg = new Progress();
66 }
67
68 Progress pgImport = new Progress();
69 Progress pgGet = new Progress();
70 Progress pgRecall = new Progress();
71
72 pg.setMinMax(0, 5);
73 pg.addProgress(pgImport, 3);
74 pg.addProgress(pgGet, 1);
75 pg.addProgress(pgRecall, 1);
76
77 if (!isCached(luid)) {
78 try {
79 cacheLib.imprt(lib, luid, pgImport);
80 pgImport.done();
085a2f9a 81 clearCache();
ff05b828 82 } catch (IOException e) {
62c63b07 83 Instance.getTraceHandler().error(e);
ff05b828
NR
84 }
85
86 pgImport.done();
87 pgGet.done();
88 }
89
90 File file = cacheLib.getFile(luid, pgRecall);
91 pgRecall.done();
92
93 pg.done();
94 return file;
95 }
96
97 @Override
98 public BufferedImage getCover(final String luid) {
ff05b828
NR
99 if (isCached(luid)) {
100 return cacheLib.getCover(luid);
101 }
102
085a2f9a 103 // We could update the cache here, but it's not easy
ff05b828
NR
104 return lib.getCover(luid);
105 }
106
085a2f9a
NR
107 @Override
108 public BufferedImage getSourceCover(String source) {
109 // no cache for the source cover
110 return lib.getSourceCover(source);
111 }
112
113 @Override
114 public void setSourceCover(String source, String luid) {
115 lib.setSourceCover(source, luid);
116 cacheLib.setSourceCover(source, getSourceCover(source));
117 }
118
ff05b828
NR
119 @Override
120 protected void clearCache() {
121 metas = null;
122 cacheLib.clearCache();
123 lib.clearCache();
124 }
125
126 @Override
127 public synchronized Story save(Story story, String luid, Progress pg)
128 throws IOException {
03c1cede
NR
129 Progress pgLib = new Progress();
130 Progress pgCacheLib = new Progress();
131
132 if (pg == null) {
133 pg = new Progress();
134 }
135
136 pg.setMinMax(0, 2);
137 pg.addProgress(pgLib, 1);
138 pg.addProgress(pgCacheLib, 1);
139
140 story = lib.save(story, luid, pgLib);
141 story = cacheLib.save(story, luid, pgCacheLib);
142
ff05b828 143 clearCache();
03c1cede 144
ff05b828
NR
145 return story;
146 }
147
148 @Override
149 public synchronized void delete(String luid) throws IOException {
085a2f9a
NR
150 if (isCached(luid)) {
151 cacheLib.delete(luid);
152 }
ff05b828
NR
153 lib.delete(luid);
154 clearCache();
155 }
156
ff05b828
NR
157 @Override
158 public synchronized void changeSource(String luid, String newSource,
159 Progress pg) throws IOException {
160 if (pg == null) {
161 pg = new Progress();
162 }
163
164 Progress pgCache = new Progress();
165 Progress pgOrig = new Progress();
166 pg.setMinMax(0, 2);
167 pg.addProgress(pgCache, 1);
168 pg.addProgress(pgOrig, 1);
169
e06632ee
NR
170 if (isCached(luid)) {
171 cacheLib.changeSource(luid, newSource, pgCache);
172 }
ff05b828
NR
173 pgCache.done();
174 lib.changeSource(luid, newSource, pgOrig);
175 pgOrig.done();
176
177 pg.done();
178 }
179
180 /**
181 * Check if the {@link Story} denoted by this Library UID is present in the
182 * cache.
183 *
184 * @param luid
185 * the Library UID
186 *
187 * @return TRUE if it is
188 */
189 public boolean isCached(String luid) {
190 return cacheLib.getInfo(luid) != null;
191 }
192
193 /**
194 * Clear the {@link Story} from the cache.
195 *
196 * @param luid
197 * the story to clear
198 *
199 * @throws IOException
200 * in case of I/O error
201 */
202 public void clearFromCache(String luid) throws IOException {
e06632ee
NR
203 if (isCached(luid)) {
204 cacheLib.delete(luid);
205 clearCache();
206 }
ff05b828
NR
207 }
208
209 // All the following methods are only used by Save and Delete in
210 // BasicLibrary:
211
212 @Override
213 protected int getNextId() {
214 throw new java.lang.InternalError("Should not have been called");
215 }
216
217 @Override
218 protected void doDelete(String luid) throws IOException {
219 throw new java.lang.InternalError("Should not have been called");
220 }
221
222 @Override
223 protected Story doSave(Story story, Progress pg) throws IOException {
224 throw new java.lang.InternalError("Should not have been called");
225 }
226}