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
) throws IOException
{
61 metas
= lib
.getMetas(pg
);
69 public synchronized MetaData
getInfo(String luid
) throws IOException
{
70 MetaData info
= cacheLib
.getInfo(luid
);
72 info
= lib
.getInfo(luid
);
79 public synchronized Story
getStory(String luid
, MetaData meta
, Progress pg
)
85 Progress pgImport
= new Progress();
86 Progress pgGet
= new Progress();
89 pg
.addProgress(pgImport
, 3);
90 pg
.addProgress(pgGet
, 1);
92 if (!isCached(luid
)) {
94 cacheLib
.imprt(lib
, luid
, pgImport
);
95 updateInfo(cacheLib
.getInfo(luid
));
97 } catch (IOException e
) {
98 Instance
.getTraceHandler().error(e
);
105 String type
= cacheLib
.getOutputType(meta
.isImageDocument());
106 MetaData cachedMeta
= meta
.clone();
107 cachedMeta
.setType(type
);
109 return cacheLib
.getStory(luid
, cachedMeta
, pg
);
113 public synchronized File
getFile(final String luid
, Progress pg
)
119 Progress pgGet
= new Progress();
120 Progress pgRecall
= new Progress();
123 pg
.addProgress(pgGet
, 4);
124 pg
.addProgress(pgRecall
, 1);
126 if (!isCached(luid
)) {
127 getStory(luid
, pgGet
);
131 File file
= cacheLib
.getFile(luid
, pgRecall
);
139 public Image
getCover(final String luid
) throws IOException
{
140 if (isCached(luid
)) {
141 return cacheLib
.getCover(luid
);
144 // We could update the cache here, but it's not easy
145 return lib
.getCover(luid
);
149 public Image
getSourceCover(String source
) throws IOException
{
150 Image custom
= getCustomSourceCover(source
);
151 if (custom
!= null) {
155 Image cached
= cacheLib
.getSourceCover(source
);
156 if (cached
!= null) {
160 return lib
.getSourceCover(source
);
164 public Image
getAuthorCover(String author
) throws IOException
{
165 Image custom
= getCustomAuthorCover(author
);
166 if (custom
!= null) {
170 Image cached
= cacheLib
.getAuthorCover(author
);
171 if (cached
!= null) {
175 return lib
.getAuthorCover(author
);
179 public Image
getCustomSourceCover(String source
) throws IOException
{
180 Image custom
= cacheLib
.getCustomSourceCover(source
);
181 if (custom
== null) {
182 custom
= lib
.getCustomSourceCover(source
);
183 if (custom
!= null) {
184 cacheLib
.setSourceCover(source
, custom
);
192 public Image
getCustomAuthorCover(String author
) throws IOException
{
193 Image custom
= cacheLib
.getCustomAuthorCover(author
);
194 if (custom
== null) {
195 custom
= lib
.getCustomAuthorCover(author
);
196 if (custom
!= null) {
197 cacheLib
.setAuthorCover(author
, custom
);
205 public void setSourceCover(String source
, String luid
) throws IOException
{
206 lib
.setSourceCover(source
, luid
);
207 cacheLib
.setSourceCover(source
, getCover(luid
));
211 public void setAuthorCover(String author
, String luid
) throws IOException
{
212 lib
.setAuthorCover(author
, luid
);
213 cacheLib
.setAuthorCover(author
, getCover(luid
));
217 protected void updateInfo(MetaData meta
) throws IOException
{
218 if (meta
!= null && metas
!= null) {
219 boolean changed
= false;
220 for (int i
= 0; i
< metas
.size(); i
++) {
221 if (metas
.get(i
).getLuid().equals(meta
.getLuid())) {
232 cacheLib
.updateInfo(meta
);
233 lib
.updateInfo(meta
);
237 protected void invalidateInfo(String luid
) {
240 } else if (metas
!= null) {
241 for (int i
= 0; i
< metas
.size(); i
++) {
242 if (metas
.get(i
).getLuid().equals(luid
)) {
248 cacheLib
.invalidateInfo(luid
);
249 lib
.invalidateInfo(luid
);
253 public synchronized Story
save(Story story
, String luid
, Progress pg
)
255 Progress pgLib
= new Progress();
256 Progress pgCacheLib
= new Progress();
263 pg
.addProgress(pgLib
, 1);
264 pg
.addProgress(pgCacheLib
, 1);
266 story
= lib
.save(story
, luid
, pgLib
);
267 story
= cacheLib
.save(story
, story
.getMeta().getLuid(), pgCacheLib
);
269 updateInfo(story
.getMeta());
275 public synchronized void delete(String luid
) throws IOException
{
276 if (isCached(luid
)) {
277 cacheLib
.delete(luid
);
281 invalidateInfo(luid
);
285 protected synchronized void changeSTA(String luid
, String newSource
,
286 String newTitle
, String newAuthor
, Progress pg
) throws IOException
{
291 Progress pgCache
= new Progress();
292 Progress pgOrig
= new Progress();
294 pg
.addProgress(pgCache
, 1);
295 pg
.addProgress(pgOrig
, 1);
297 MetaData meta
= getInfo(luid
);
299 throw new IOException("Story not found: " + luid
);
302 if (isCached(luid
)) {
303 cacheLib
.changeSTA(luid
, newSource
, newTitle
, newAuthor
, pgCache
);
307 lib
.changeSTA(luid
, newSource
, newTitle
, newAuthor
, pgOrig
);
310 meta
.setSource(newSource
);
311 meta
.setTitle(newTitle
);
312 meta
.setAuthor(newAuthor
);
315 invalidateInfo(luid
);
319 * Check if the {@link Story} denoted by this Library UID is present in the
325 * @return TRUE if it is
327 public boolean isCached(String luid
) {
329 return cacheLib
.getInfo(luid
) != null;
330 } catch (IOException e
) {
336 * Clear the {@link Story} from the cache.
338 * The next time we try to retrieve the {@link Story}, it may be required to
344 * @throws IOException
345 * in case of I/O error
347 public void clearFromCache(String luid
) throws IOException
{
348 if (isCached(luid
)) {
349 cacheLib
.delete(luid
);
354 public MetaData
imprt(URL url
, Progress pg
) throws IOException
{
359 Progress pgImprt
= new Progress();
360 Progress pgCache
= new Progress();
362 pg
.addProgress(pgImprt
, 7);
363 pg
.addProgress(pgCache
, 3);
365 MetaData meta
= lib
.imprt(url
, pgImprt
);
368 clearFromCache(meta
.getLuid());
374 // All the following methods are only used by Save and Delete in
378 protected int getNextId() {
379 throw new java
.lang
.InternalError("Should not have been called");
383 protected void doDelete(String luid
) throws IOException
{
384 throw new java
.lang
.InternalError("Should not have been called");
388 protected Story
doSave(Story story
, Progress pg
) throws IOException
{
389 throw new java
.lang
.InternalError("Should not have been called");