1 package be
.nikiroo
.fanfix
.library
;
4 import java
.io
.IOException
;
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
;
16 * This library will cache another pre-existing {@link BasicLibrary}.
20 public class CacheLibrary
extends BasicLibrary
{
21 private List
<MetaData
> metas
;
22 private BasicLibrary lib
;
23 private LocalLibrary cacheLib
;
26 * Create a cache library around the given one.
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.
32 * the cache directory where to save the files to disk
34 * the original library to wrap
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
),
45 public String
getLibraryName() {
46 return lib
.getLibraryName();
50 public Status
getStatus() {
51 return lib
.getStatus();
55 protected List
<MetaData
> getMetas(Progress pg
) {
61 metas
= lib
.getMetas(pg
);
69 public synchronized MetaData
getInfo(String luid
) {
70 MetaData info
= cacheLib
.getInfo(luid
);
72 info
= lib
.getInfo(luid
);
79 public synchronized Story
getStory(String luid
, MetaData meta
, Progress pg
) {
84 Progress pgImport
= new Progress();
85 Progress pgGet
= new Progress();
88 pg
.addProgress(pgImport
, 3);
89 pg
.addProgress(pgGet
, 1);
91 if (!isCached(luid
)) {
93 cacheLib
.imprt(lib
, luid
, pgImport
);
94 updateInfo(cacheLib
.getInfo(luid
));
96 } catch (IOException e
) {
97 Instance
.getTraceHandler().error(e
);
104 String type
= cacheLib
.getOutputType(meta
.isImageDocument());
105 MetaData cachedMeta
= meta
.clone();
106 cachedMeta
.setType(type
);
108 return cacheLib
.getStory(luid
, cachedMeta
, pg
);
112 public synchronized File
getFile(final String luid
, Progress pg
) {
117 Progress pgGet
= new Progress();
118 Progress pgRecall
= new Progress();
121 pg
.addProgress(pgGet
, 4);
122 pg
.addProgress(pgRecall
, 1);
124 if (!isCached(luid
)) {
125 getStory(luid
, pgGet
);
129 File file
= cacheLib
.getFile(luid
, pgRecall
);
137 public Image
getCover(final String luid
) {
138 if (isCached(luid
)) {
139 return cacheLib
.getCover(luid
);
142 // We could update the cache here, but it's not easy
143 return lib
.getCover(luid
);
147 public Image
getSourceCover(String source
) {
148 Image custom
= getCustomSourceCover(source
);
149 if (custom
!= null) {
153 Image cached
= cacheLib
.getSourceCover(source
);
154 if (cached
!= null) {
158 return lib
.getSourceCover(source
);
162 public Image
getAuthorCover(String author
) {
163 Image custom
= getCustomAuthorCover(author
);
164 if (custom
!= null) {
168 Image cached
= cacheLib
.getAuthorCover(author
);
169 if (cached
!= null) {
173 return lib
.getAuthorCover(author
);
177 public Image
getCustomSourceCover(String source
) {
178 Image custom
= cacheLib
.getCustomSourceCover(source
);
179 if (custom
== null) {
180 custom
= lib
.getCustomSourceCover(source
);
181 if (custom
!= null) {
182 cacheLib
.setSourceCover(source
, custom
);
190 public Image
getCustomAuthorCover(String author
) {
191 Image custom
= cacheLib
.getCustomAuthorCover(author
);
192 if (custom
== null) {
193 custom
= lib
.getCustomAuthorCover(author
);
194 if (custom
!= null) {
195 cacheLib
.setAuthorCover(author
, custom
);
203 public void setSourceCover(String source
, String luid
) {
204 lib
.setSourceCover(source
, luid
);
205 cacheLib
.setSourceCover(source
, getCover(luid
));
209 public void setAuthorCover(String author
, String luid
) {
210 lib
.setAuthorCover(author
, luid
);
211 cacheLib
.setAuthorCover(author
, getCover(luid
));
215 protected void updateInfo(MetaData meta
) {
216 if (meta
!= null && metas
!= null) {
217 for (int i
= 0; i
< metas
.size(); i
++) {
218 if (metas
.get(i
).getLuid().equals(meta
.getLuid())) {
224 cacheLib
.updateInfo(meta
);
225 lib
.updateInfo(meta
);
229 protected void invalidateInfo(String luid
) {
232 } else if (metas
!= null) {
233 for (int i
= 0; i
< metas
.size(); i
++) {
234 if (metas
.get(i
).getLuid().equals(luid
)) {
240 cacheLib
.invalidateInfo(luid
);
241 lib
.invalidateInfo(luid
);
245 public synchronized Story
save(Story story
, String luid
, Progress pg
)
247 Progress pgLib
= new Progress();
248 Progress pgCacheLib
= new Progress();
255 pg
.addProgress(pgLib
, 1);
256 pg
.addProgress(pgCacheLib
, 1);
258 story
= lib
.save(story
, luid
, pgLib
);
259 story
= cacheLib
.save(story
, story
.getMeta().getLuid(), pgCacheLib
);
261 updateInfo(story
.getMeta());
267 public synchronized void delete(String luid
) throws IOException
{
268 if (isCached(luid
)) {
269 cacheLib
.delete(luid
);
273 invalidateInfo(luid
);
277 protected synchronized void changeSTA(String luid
, String newSource
,
278 String newTitle
, String newAuthor
, Progress pg
) throws IOException
{
283 Progress pgCache
= new Progress();
284 Progress pgOrig
= new Progress();
286 pg
.addProgress(pgCache
, 1);
287 pg
.addProgress(pgOrig
, 1);
289 MetaData meta
= getInfo(luid
);
291 throw new IOException("Story not found: " + luid
);
294 if (isCached(luid
)) {
295 cacheLib
.changeSTA(luid
, newSource
, newTitle
, newAuthor
, pgCache
);
299 lib
.changeSTA(luid
, newSource
, newTitle
, newAuthor
, pgOrig
);
302 meta
.setSource(newSource
);
303 meta
.setTitle(newTitle
);
304 meta
.setAuthor(newAuthor
);
307 invalidateInfo(luid
);
311 * Check if the {@link Story} denoted by this Library UID is present in the
317 * @return TRUE if it is
319 public boolean isCached(String luid
) {
320 return cacheLib
.getInfo(luid
) != null;
324 * Clear the {@link Story} from the cache.
326 * The next time we try to retrieve the {@link Story}, it may be required to
332 * @throws IOException
333 * in case of I/O error
335 public void clearFromCache(String luid
) throws IOException
{
336 if (isCached(luid
)) {
337 cacheLib
.delete(luid
);
342 public Story
imprt(URL url
, Progress pg
) throws IOException
{
347 Progress pgImprt
= new Progress();
348 Progress pgCache
= new Progress();
350 pg
.addProgress(pgImprt
, 7);
351 pg
.addProgress(pgCache
, 3);
353 Story story
= lib
.imprt(url
, pgImprt
);
354 cacheLib
.save(story
, story
.getMeta().getLuid(), pgCache
);
356 updateInfo(story
.getMeta());
362 // All the following methods are only used by Save and Delete in
366 protected int getNextId() {
367 throw new java
.lang
.InternalError("Should not have been called");
371 protected void doDelete(String luid
) throws IOException
{
372 throw new java
.lang
.InternalError("Should not have been called");
376 protected Story
doSave(Story story
, Progress pg
) throws IOException
{
377 throw new java
.lang
.InternalError("Should not have been called");