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 type
= cacheLib
.getOutputType(meta
.isImageDocument());
71 MetaData cachedMeta
= meta
.clone();
72 cachedMeta
.setType(type
);
74 return super.getStory(luid
, cachedMeta
, pg
);
78 public synchronized File
getFile(final String luid
, Progress pg
) {
83 Progress pgImport
= new Progress();
84 Progress pgGet
= new Progress();
85 Progress pgRecall
= new Progress();
88 pg
.addProgress(pgImport
, 3);
89 pg
.addProgress(pgGet
, 1);
90 pg
.addProgress(pgRecall
, 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 File file
= cacheLib
.getFile(luid
, pgRecall
);
113 public Image
getCover(final String luid
) {
114 if (isCached(luid
)) {
115 return cacheLib
.getCover(luid
);
118 // We could update the cache here, but it's not easy
119 return lib
.getCover(luid
);
123 public Image
getSourceCover(String source
) {
124 Image custom
= getCustomSourceCover(source
);
125 if (custom
!= null) {
129 Image cached
= cacheLib
.getSourceCover(source
);
130 if (cached
!= null) {
134 return lib
.getSourceCover(source
);
138 public Image
getAuthorCover(String author
) {
139 Image custom
= getCustomAuthorCover(author
);
140 if (custom
!= null) {
144 Image cached
= cacheLib
.getAuthorCover(author
);
145 if (cached
!= null) {
149 return lib
.getAuthorCover(author
);
153 public Image
getCustomSourceCover(String source
) {
154 Image custom
= cacheLib
.getCustomSourceCover(source
);
155 if (custom
== null) {
156 custom
= lib
.getCustomSourceCover(source
);
157 if (custom
!= null) {
158 cacheLib
.setSourceCover(source
, custom
);
166 public Image
getCustomAuthorCover(String author
) {
167 Image custom
= cacheLib
.getCustomAuthorCover(author
);
168 if (custom
== null) {
169 custom
= lib
.getCustomAuthorCover(author
);
170 if (custom
!= null) {
171 cacheLib
.setAuthorCover(author
, custom
);
179 public void setSourceCover(String source
, String luid
) {
180 lib
.setSourceCover(source
, luid
);
181 cacheLib
.setSourceCover(source
, getCover(luid
));
185 public void setAuthorCover(String author
, String luid
) {
186 lib
.setAuthorCover(author
, luid
);
187 cacheLib
.setAuthorCover(author
, getCover(luid
));
191 protected void updateInfo(MetaData meta
) {
192 if (meta
!= null && metas
!= null) {
193 for (int i
= 0; i
< metas
.size(); i
++) {
194 if (metas
.get(i
).getLuid().equals(meta
.getLuid())) {
200 cacheLib
.updateInfo(meta
);
201 lib
.updateInfo(meta
);
205 protected void invalidateInfo(String luid
) {
208 } else if (metas
!= null) {
209 for (int i
= 0; i
< metas
.size(); i
++) {
210 if (metas
.get(i
).getLuid().equals(luid
)) {
216 cacheLib
.invalidateInfo(luid
);
217 lib
.invalidateInfo(luid
);
221 public synchronized Story
save(Story story
, String luid
, Progress pg
)
223 Progress pgLib
= new Progress();
224 Progress pgCacheLib
= new Progress();
231 pg
.addProgress(pgLib
, 1);
232 pg
.addProgress(pgCacheLib
, 1);
234 story
= lib
.save(story
, luid
, pgLib
);
235 story
= cacheLib
.save(story
, story
.getMeta().getLuid(), pgCacheLib
);
237 updateInfo(story
.getMeta());
243 public synchronized void delete(String luid
) throws IOException
{
244 if (isCached(luid
)) {
245 cacheLib
.delete(luid
);
249 MetaData meta
= getInfo(luid
);
256 protected synchronized void changeSTA(String luid
, String newSource
,
257 String newTitle
, String newAuthor
, Progress pg
) throws IOException
{
262 Progress pgCache
= new Progress();
263 Progress pgOrig
= new Progress();
265 pg
.addProgress(pgCache
, 1);
266 pg
.addProgress(pgOrig
, 1);
268 MetaData meta
= getInfo(luid
);
270 throw new IOException("Story not found: " + luid
);
273 if (isCached(luid
)) {
274 cacheLib
.changeSTA(luid
, newSource
, newTitle
, newAuthor
, pgCache
);
278 lib
.changeSTA(luid
, newSource
, newTitle
, newAuthor
, pgOrig
);
281 meta
.setSource(newSource
);
282 meta
.setTitle(newTitle
);
283 meta
.setAuthor(newAuthor
);
288 * Check if the {@link Story} denoted by this Library UID is present in the
294 * @return TRUE if it is
296 public boolean isCached(String luid
) {
297 return cacheLib
.getInfo(luid
) != null;
301 * Clear the {@link Story} from the cache.
303 * The next time we try to retrieve the {@link Story}, it may be required to
309 * @throws IOException
310 * in case of I/O error
312 public void clearFromCache(String luid
) throws IOException
{
313 if (isCached(luid
)) {
314 cacheLib
.delete(luid
);
319 public Story
imprt(URL url
, Progress pg
) throws IOException
{
324 Progress pgImprt
= new Progress();
325 Progress pgCache
= new Progress();
327 pg
.addProgress(pgImprt
, 7);
328 pg
.addProgress(pgCache
, 3);
330 Story story
= lib
.imprt(url
, pgImprt
);
331 cacheLib
.save(story
, story
.getMeta().getLuid(), pgCache
);
333 updateInfo(story
.getMeta());
339 // All the following methods are only used by Save and Delete in
343 protected int getNextId() {
344 throw new java
.lang
.InternalError("Should not have been called");
348 protected void doDelete(String luid
) throws IOException
{
349 throw new java
.lang
.InternalError("Should not have been called");
353 protected Story
doSave(Story story
, Progress pg
) throws IOException
{
354 throw new java
.lang
.InternalError("Should not have been called");