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 File
getFile(final String luid
, Progress pg
) {
74 Progress pgImport
= new Progress();
75 Progress pgGet
= new Progress();
76 Progress pgRecall
= new Progress();
79 pg
.addProgress(pgImport
, 3);
80 pg
.addProgress(pgGet
, 1);
81 pg
.addProgress(pgRecall
, 1);
83 if (!isCached(luid
)) {
85 cacheLib
.imprt(lib
, luid
, pgImport
);
86 updateInfo(cacheLib
.getInfo(luid
));
88 } catch (IOException e
) {
89 Instance
.getTraceHandler().error(e
);
96 File file
= cacheLib
.getFile(luid
, pgRecall
);
104 public Image
getCover(final String luid
) {
105 if (isCached(luid
)) {
106 return cacheLib
.getCover(luid
);
109 // We could update the cache here, but it's not easy
110 return lib
.getCover(luid
);
114 public Image
getSourceCover(String source
) {
115 Image custom
= getCustomSourceCover(source
);
116 if (custom
!= null) {
120 Image cached
= cacheLib
.getSourceCover(source
);
121 if (cached
!= null) {
125 return lib
.getSourceCover(source
);
129 public Image
getAuthorCover(String author
) {
130 Image custom
= getCustomAuthorCover(author
);
131 if (custom
!= null) {
135 Image cached
= cacheLib
.getAuthorCover(author
);
136 if (cached
!= null) {
140 return lib
.getAuthorCover(author
);
144 public Image
getCustomSourceCover(String source
) {
145 Image custom
= cacheLib
.getCustomSourceCover(source
);
146 if (custom
== null) {
147 custom
= lib
.getCustomSourceCover(source
);
148 if (custom
!= null) {
149 cacheLib
.setSourceCover(source
, custom
);
157 public Image
getCustomAuthorCover(String author
) {
158 Image custom
= cacheLib
.getCustomAuthorCover(author
);
159 if (custom
== null) {
160 custom
= lib
.getCustomAuthorCover(author
);
161 if (custom
!= null) {
162 cacheLib
.setAuthorCover(author
, custom
);
170 public void setSourceCover(String source
, String luid
) {
171 lib
.setSourceCover(source
, luid
);
172 cacheLib
.setSourceCover(source
, getCover(luid
));
176 public void setAuthorCover(String author
, String luid
) {
177 lib
.setAuthorCover(author
, luid
);
178 cacheLib
.setAuthorCover(author
, getCover(luid
));
182 protected void updateInfo(MetaData meta
) {
183 if (meta
!= null && metas
!= null) {
184 for (int i
= 0; i
< metas
.size(); i
++) {
185 if (metas
.get(i
).getLuid().equals(meta
.getLuid())) {
191 cacheLib
.updateInfo(meta
);
192 lib
.updateInfo(meta
);
196 protected void invalidateInfo(String luid
) {
199 } else if (metas
!= null) {
200 for (int i
= 0; i
< metas
.size(); i
++) {
201 if (metas
.get(i
).getLuid().equals(luid
)) {
207 cacheLib
.invalidateInfo(luid
);
208 lib
.invalidateInfo(luid
);
212 public synchronized Story
save(Story story
, String luid
, Progress pg
)
214 Progress pgLib
= new Progress();
215 Progress pgCacheLib
= new Progress();
222 pg
.addProgress(pgLib
, 1);
223 pg
.addProgress(pgCacheLib
, 1);
225 story
= lib
.save(story
, luid
, pgLib
);
226 story
= cacheLib
.save(story
, story
.getMeta().getLuid(), pgCacheLib
);
228 updateInfo(story
.getMeta());
234 public synchronized void delete(String luid
) throws IOException
{
235 if (isCached(luid
)) {
236 cacheLib
.delete(luid
);
240 MetaData meta
= getInfo(luid
);
247 protected synchronized void changeSTA(String luid
, String newSource
,
248 String newTitle
, String newAuthor
, Progress pg
) throws IOException
{
253 Progress pgCache
= new Progress();
254 Progress pgOrig
= new Progress();
256 pg
.addProgress(pgCache
, 1);
257 pg
.addProgress(pgOrig
, 1);
259 MetaData meta
= getInfo(luid
);
261 throw new IOException("Story not found: " + luid
);
264 if (isCached(luid
)) {
265 cacheLib
.changeSTA(luid
, newSource
, newTitle
, newAuthor
, pgCache
);
269 lib
.changeSTA(luid
, newSource
, newTitle
, newAuthor
, pgOrig
);
272 meta
.setSource(newSource
);
273 meta
.setTitle(newTitle
);
274 meta
.setAuthor(newAuthor
);
279 * Check if the {@link Story} denoted by this Library UID is present in the
285 * @return TRUE if it is
287 public boolean isCached(String luid
) {
288 return cacheLib
.getInfo(luid
) != null;
292 * Clear the {@link Story} from the cache.
294 * The next time we try to retrieve the {@link Story}, it may be required to
300 * @throws IOException
301 * in case of I/O error
303 public void clearFromCache(String luid
) throws IOException
{
304 if (isCached(luid
)) {
305 cacheLib
.delete(luid
);
310 public Story
imprt(URL url
, Progress pg
) throws IOException
{
315 Progress pgImprt
= new Progress();
316 Progress pgCache
= new Progress();
318 pg
.addProgress(pgImprt
, 7);
319 pg
.addProgress(pgCache
, 3);
321 Story story
= lib
.imprt(url
, pgImprt
);
322 cacheLib
.save(story
, story
.getMeta().getLuid(), pgCache
);
324 updateInfo(story
.getMeta());
330 // All the following methods are only used by Save and Delete in
334 protected int getNextId() {
335 throw new java
.lang
.InternalError("Should not have been called");
339 protected void doDelete(String luid
) throws IOException
{
340 throw new java
.lang
.InternalError("Should not have been called");
344 protected Story
doSave(Story story
, Progress pg
) throws IOException
{
345 throw new java
.lang
.InternalError("Should not have been called");