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 for (int i
= 0; i
< metas
.size(); i
++) {
220 if (metas
.get(i
).getLuid().equals(meta
.getLuid())) {
226 cacheLib
.updateInfo(meta
);
227 lib
.updateInfo(meta
);
231 protected void invalidateInfo(String luid
) {
234 } else if (metas
!= null) {
235 for (int i
= 0; i
< metas
.size(); i
++) {
236 if (metas
.get(i
).getLuid().equals(luid
)) {
242 cacheLib
.invalidateInfo(luid
);
243 lib
.invalidateInfo(luid
);
247 public synchronized Story
save(Story story
, String luid
, Progress pg
)
249 Progress pgLib
= new Progress();
250 Progress pgCacheLib
= new Progress();
257 pg
.addProgress(pgLib
, 1);
258 pg
.addProgress(pgCacheLib
, 1);
260 story
= lib
.save(story
, luid
, pgLib
);
261 story
= cacheLib
.save(story
, story
.getMeta().getLuid(), pgCacheLib
);
263 updateInfo(story
.getMeta());
269 public synchronized void delete(String luid
) throws IOException
{
270 if (isCached(luid
)) {
271 cacheLib
.delete(luid
);
275 invalidateInfo(luid
);
279 protected synchronized void changeSTA(String luid
, String newSource
,
280 String newTitle
, String newAuthor
, Progress pg
) throws IOException
{
285 Progress pgCache
= new Progress();
286 Progress pgOrig
= new Progress();
288 pg
.addProgress(pgCache
, 1);
289 pg
.addProgress(pgOrig
, 1);
291 MetaData meta
= getInfo(luid
);
293 throw new IOException("Story not found: " + luid
);
296 if (isCached(luid
)) {
297 cacheLib
.changeSTA(luid
, newSource
, newTitle
, newAuthor
, pgCache
);
301 lib
.changeSTA(luid
, newSource
, newTitle
, newAuthor
, pgOrig
);
304 meta
.setSource(newSource
);
305 meta
.setTitle(newTitle
);
306 meta
.setAuthor(newAuthor
);
309 invalidateInfo(luid
);
313 * Check if the {@link Story} denoted by this Library UID is present in the
319 * @return TRUE if it is
321 public boolean isCached(String luid
) {
323 return cacheLib
.getInfo(luid
) != null;
324 } catch (IOException e
) {
330 * Clear the {@link Story} from the cache.
332 * The next time we try to retrieve the {@link Story}, it may be required to
338 * @throws IOException
339 * in case of I/O error
341 public void clearFromCache(String luid
) throws IOException
{
342 if (isCached(luid
)) {
343 cacheLib
.delete(luid
);
348 public MetaData
imprt(URL url
, Progress pg
) throws IOException
{
353 Progress pgImprt
= new Progress();
354 Progress pgCache
= new Progress();
356 pg
.addProgress(pgImprt
, 7);
357 pg
.addProgress(pgCache
, 3);
359 MetaData meta
= lib
.imprt(url
, pgImprt
);
362 clearFromCache(meta
.getLuid());
368 // All the following methods are only used by Save and Delete in
372 protected int getNextId() {
373 throw new java
.lang
.InternalError("Should not have been called");
377 protected void doDelete(String luid
) throws IOException
{
378 throw new java
.lang
.InternalError("Should not have been called");
382 protected Story
doSave(Story story
, Progress pg
) throws IOException
{
383 throw new java
.lang
.InternalError("Should not have been called");