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 Story
getStory(String luid
, MetaData meta
, Progress pg
) {
70 String normal
= Instance
.getUiConfig().getString(
71 UiConfig
.GUI_NON_IMAGES_DOCUMENT_TYPE
);
72 String images
= Instance
.getUiConfig().getString(
73 UiConfig
.GUI_IMAGES_DOCUMENT_TYPE
);
74 String type
= meta
.isImageDocument() ? images
: normal
;
76 MetaData cachedMeta
= meta
.clone();
77 cachedMeta
.setType(type
);
79 return super.getStory(luid
, cachedMeta
, pg
);
83 public synchronized File
getFile(final String luid
, Progress pg
) {
88 Progress pgImport
= new Progress();
89 Progress pgGet
= new Progress();
90 Progress pgRecall
= new Progress();
93 pg
.addProgress(pgImport
, 3);
94 pg
.addProgress(pgGet
, 1);
95 pg
.addProgress(pgRecall
, 1);
97 if (!isCached(luid
)) {
99 cacheLib
.imprt(lib
, luid
, pgImport
);
100 updateInfo(cacheLib
.getInfo(luid
));
102 } catch (IOException e
) {
103 Instance
.getTraceHandler().error(e
);
110 File file
= cacheLib
.getFile(luid
, pgRecall
);
118 public Image
getCover(final String luid
) {
119 if (isCached(luid
)) {
120 return cacheLib
.getCover(luid
);
123 // We could update the cache here, but it's not easy
124 return lib
.getCover(luid
);
128 public Image
getSourceCover(String source
) {
129 Image custom
= getCustomSourceCover(source
);
130 if (custom
!= null) {
134 Image cached
= cacheLib
.getSourceCover(source
);
135 if (cached
!= null) {
139 return lib
.getSourceCover(source
);
143 public Image
getAuthorCover(String author
) {
144 Image custom
= getCustomAuthorCover(author
);
145 if (custom
!= null) {
149 Image cached
= cacheLib
.getAuthorCover(author
);
150 if (cached
!= null) {
154 return lib
.getAuthorCover(author
);
158 public Image
getCustomSourceCover(String source
) {
159 Image custom
= cacheLib
.getCustomSourceCover(source
);
160 if (custom
== null) {
161 custom
= lib
.getCustomSourceCover(source
);
162 if (custom
!= null) {
163 cacheLib
.setSourceCover(source
, custom
);
171 public Image
getCustomAuthorCover(String author
) {
172 Image custom
= cacheLib
.getCustomAuthorCover(author
);
173 if (custom
== null) {
174 custom
= lib
.getCustomAuthorCover(author
);
175 if (custom
!= null) {
176 cacheLib
.setAuthorCover(author
, custom
);
184 public void setSourceCover(String source
, String luid
) {
185 lib
.setSourceCover(source
, luid
);
186 cacheLib
.setSourceCover(source
, getCover(luid
));
190 public void setAuthorCover(String author
, String luid
) {
191 lib
.setAuthorCover(author
, luid
);
192 cacheLib
.setAuthorCover(author
, getCover(luid
));
196 protected void updateInfo(MetaData meta
) {
197 if (meta
!= null && metas
!= null) {
198 for (int i
= 0; i
< metas
.size(); i
++) {
199 if (metas
.get(i
).getLuid().equals(meta
.getLuid())) {
205 cacheLib
.updateInfo(meta
);
206 lib
.updateInfo(meta
);
210 protected void invalidateInfo(String luid
) {
213 } else if (metas
!= null) {
214 for (int i
= 0; i
< metas
.size(); i
++) {
215 if (metas
.get(i
).getLuid().equals(luid
)) {
221 cacheLib
.invalidateInfo(luid
);
222 lib
.invalidateInfo(luid
);
226 public synchronized Story
save(Story story
, String luid
, Progress pg
)
228 Progress pgLib
= new Progress();
229 Progress pgCacheLib
= new Progress();
236 pg
.addProgress(pgLib
, 1);
237 pg
.addProgress(pgCacheLib
, 1);
239 story
= lib
.save(story
, luid
, pgLib
);
240 story
= cacheLib
.save(story
, story
.getMeta().getLuid(), pgCacheLib
);
242 updateInfo(story
.getMeta());
248 public synchronized void delete(String luid
) throws IOException
{
249 if (isCached(luid
)) {
250 cacheLib
.delete(luid
);
254 MetaData meta
= getInfo(luid
);
261 protected synchronized void changeSTA(String luid
, String newSource
,
262 String newTitle
, String newAuthor
, Progress pg
) throws IOException
{
267 Progress pgCache
= new Progress();
268 Progress pgOrig
= new Progress();
270 pg
.addProgress(pgCache
, 1);
271 pg
.addProgress(pgOrig
, 1);
273 MetaData meta
= getInfo(luid
);
275 throw new IOException("Story not found: " + luid
);
278 if (isCached(luid
)) {
279 cacheLib
.changeSTA(luid
, newSource
, newTitle
, newAuthor
, pgCache
);
283 lib
.changeSTA(luid
, newSource
, newTitle
, newAuthor
, pgOrig
);
286 meta
.setSource(newSource
);
287 meta
.setTitle(newTitle
);
288 meta
.setAuthor(newAuthor
);
293 * Check if the {@link Story} denoted by this Library UID is present in the
299 * @return TRUE if it is
301 public boolean isCached(String luid
) {
302 return cacheLib
.getInfo(luid
) != null;
306 * Clear the {@link Story} from the cache.
308 * The next time we try to retrieve the {@link Story}, it may be required to
314 * @throws IOException
315 * in case of I/O error
317 public void clearFromCache(String luid
) throws IOException
{
318 if (isCached(luid
)) {
319 cacheLib
.delete(luid
);
324 public Story
imprt(URL url
, Progress pg
) throws IOException
{
329 Progress pgImprt
= new Progress();
330 Progress pgCache
= new Progress();
332 pg
.addProgress(pgImprt
, 7);
333 pg
.addProgress(pgCache
, 3);
335 Story story
= lib
.imprt(url
, pgImprt
);
336 cacheLib
.save(story
, story
.getMeta().getLuid(), pgCache
);
338 updateInfo(story
.getMeta());
344 // All the following methods are only used by Save and Delete in
348 protected int getNextId() {
349 throw new java
.lang
.InternalError("Should not have been called");
353 protected void doDelete(String luid
) throws IOException
{
354 throw new java
.lang
.InternalError("Should not have been called");
358 protected Story
doSave(Story story
, Progress pg
) throws IOException
{
359 throw new java
.lang
.InternalError("Should not have been called");