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
) {
74 Progress pgImport
= new Progress();
75 Progress pgGet
= new Progress();
78 pg
.addProgress(pgImport
, 3);
79 pg
.addProgress(pgGet
, 1);
81 if (!isCached(luid
)) {
83 cacheLib
.imprt(lib
, luid
, pgImport
);
84 updateInfo(cacheLib
.getInfo(luid
));
86 } catch (IOException e
) {
87 Instance
.getTraceHandler().error(e
);
94 String type
= cacheLib
.getOutputType(meta
.isImageDocument());
95 MetaData cachedMeta
= meta
.clone();
96 cachedMeta
.setType(type
);
98 return cacheLib
.getStory(luid
, cachedMeta
, pg
);
102 public synchronized File
getFile(final String luid
, Progress pg
) {
107 Progress pgGet
= new Progress();
108 Progress pgRecall
= new Progress();
111 pg
.addProgress(pgGet
, 4);
112 pg
.addProgress(pgRecall
, 1);
114 if (!isCached(luid
)) {
115 getStory(luid
, pgGet
);
119 File file
= cacheLib
.getFile(luid
, pgRecall
);
127 public Image
getCover(final String luid
) {
128 if (isCached(luid
)) {
129 return cacheLib
.getCover(luid
);
132 // We could update the cache here, but it's not easy
133 return lib
.getCover(luid
);
137 public Image
getSourceCover(String source
) {
138 Image custom
= getCustomSourceCover(source
);
139 if (custom
!= null) {
143 Image cached
= cacheLib
.getSourceCover(source
);
144 if (cached
!= null) {
148 return lib
.getSourceCover(source
);
152 public Image
getAuthorCover(String author
) {
153 Image custom
= getCustomAuthorCover(author
);
154 if (custom
!= null) {
158 Image cached
= cacheLib
.getAuthorCover(author
);
159 if (cached
!= null) {
163 return lib
.getAuthorCover(author
);
167 public Image
getCustomSourceCover(String source
) {
168 Image custom
= cacheLib
.getCustomSourceCover(source
);
169 if (custom
== null) {
170 custom
= lib
.getCustomSourceCover(source
);
171 if (custom
!= null) {
172 cacheLib
.setSourceCover(source
, custom
);
180 public Image
getCustomAuthorCover(String author
) {
181 Image custom
= cacheLib
.getCustomAuthorCover(author
);
182 if (custom
== null) {
183 custom
= lib
.getCustomAuthorCover(author
);
184 if (custom
!= null) {
185 cacheLib
.setAuthorCover(author
, custom
);
193 public void setSourceCover(String source
, String luid
) {
194 lib
.setSourceCover(source
, luid
);
195 cacheLib
.setSourceCover(source
, getCover(luid
));
199 public void setAuthorCover(String author
, String luid
) {
200 lib
.setAuthorCover(author
, luid
);
201 cacheLib
.setAuthorCover(author
, getCover(luid
));
205 protected void updateInfo(MetaData meta
) {
206 if (meta
!= null && metas
!= null) {
207 for (int i
= 0; i
< metas
.size(); i
++) {
208 if (metas
.get(i
).getLuid().equals(meta
.getLuid())) {
214 cacheLib
.updateInfo(meta
);
215 lib
.updateInfo(meta
);
219 protected void invalidateInfo(String luid
) {
222 } else if (metas
!= null) {
223 for (int i
= 0; i
< metas
.size(); i
++) {
224 if (metas
.get(i
).getLuid().equals(luid
)) {
230 cacheLib
.invalidateInfo(luid
);
231 lib
.invalidateInfo(luid
);
235 public synchronized Story
save(Story story
, String luid
, Progress pg
)
237 Progress pgLib
= new Progress();
238 Progress pgCacheLib
= new Progress();
245 pg
.addProgress(pgLib
, 1);
246 pg
.addProgress(pgCacheLib
, 1);
248 story
= lib
.save(story
, luid
, pgLib
);
249 story
= cacheLib
.save(story
, story
.getMeta().getLuid(), pgCacheLib
);
251 updateInfo(story
.getMeta());
257 public synchronized void delete(String luid
) throws IOException
{
258 if (isCached(luid
)) {
259 cacheLib
.delete(luid
);
263 MetaData meta
= getInfo(luid
);
270 protected synchronized void changeSTA(String luid
, String newSource
,
271 String newTitle
, String newAuthor
, Progress pg
) throws IOException
{
276 Progress pgCache
= new Progress();
277 Progress pgOrig
= new Progress();
279 pg
.addProgress(pgCache
, 1);
280 pg
.addProgress(pgOrig
, 1);
282 MetaData meta
= getInfo(luid
);
284 throw new IOException("Story not found: " + luid
);
287 if (isCached(luid
)) {
288 cacheLib
.changeSTA(luid
, newSource
, newTitle
, newAuthor
, pgCache
);
292 lib
.changeSTA(luid
, newSource
, newTitle
, newAuthor
, pgOrig
);
295 meta
.setSource(newSource
);
296 meta
.setTitle(newTitle
);
297 meta
.setAuthor(newAuthor
);
302 * Check if the {@link Story} denoted by this Library UID is present in the
308 * @return TRUE if it is
310 public boolean isCached(String luid
) {
311 return cacheLib
.getInfo(luid
) != null;
315 * Clear the {@link Story} from the cache.
317 * The next time we try to retrieve the {@link Story}, it may be required to
323 * @throws IOException
324 * in case of I/O error
326 public void clearFromCache(String luid
) throws IOException
{
327 if (isCached(luid
)) {
328 cacheLib
.delete(luid
);
333 public Story
imprt(URL url
, Progress pg
) throws IOException
{
338 Progress pgImprt
= new Progress();
339 Progress pgCache
= new Progress();
341 pg
.addProgress(pgImprt
, 7);
342 pg
.addProgress(pgCache
, 3);
344 Story story
= lib
.imprt(url
, pgImprt
);
345 cacheLib
.save(story
, story
.getMeta().getLuid(), pgCache
);
347 updateInfo(story
.getMeta());
353 // All the following methods are only used by Save and Delete in
357 protected int getNextId() {
358 throw new java
.lang
.InternalError("Should not have been called");
362 protected void doDelete(String luid
) throws IOException
{
363 throw new java
.lang
.InternalError("Should not have been called");
367 protected Story
doSave(Story story
, Progress pg
) throws IOException
{
368 throw new java
.lang
.InternalError("Should not have been called");