1 package be
.nikiroo
.fanfix
.library
;
4 import java
.io
.IOException
;
6 import java
.util
.ArrayList
;
9 import be
.nikiroo
.fanfix
.Instance
;
10 import be
.nikiroo
.fanfix
.bundles
.UiConfig
;
11 import be
.nikiroo
.fanfix
.bundles
.UiConfigBundle
;
12 import be
.nikiroo
.fanfix
.data
.MetaData
;
13 import be
.nikiroo
.fanfix
.data
.Story
;
14 import be
.nikiroo
.fanfix
.output
.BasicOutput
.OutputType
;
15 import be
.nikiroo
.utils
.Image
;
16 import be
.nikiroo
.utils
.Progress
;
19 * This library will cache another pre-existing {@link BasicLibrary}.
23 public class CacheLibrary
extends BasicLibrary
{
24 private List
<MetaData
> metas
;
25 private BasicLibrary lib
;
26 private LocalLibrary cacheLib
;
29 * Create a cache library around the given one.
31 * It will return the same result, but those will be saved to disk at the same
32 * time to be fetched quicker the next time.
34 * @param cacheDir the cache directory where to save the files to disk
35 * @param lib the original library to wrap
36 * @param config the configuration used to know which kind of default
37 * {@link OutputType} to use for images and non-images stories
39 public CacheLibrary(File cacheDir
, BasicLibrary lib
, UiConfigBundle config
) {
40 this.cacheLib
= new LocalLibrary(cacheDir
, //
41 config
.getString(UiConfig
.GUI_NON_IMAGES_DOCUMENT_TYPE
),
42 config
.getString(UiConfig
.GUI_IMAGES_DOCUMENT_TYPE
), true);
47 public String
getLibraryName() {
48 return lib
.getLibraryName();
52 public Status
getStatus() {
53 return lib
.getStatus();
57 protected List
<MetaData
> getMetas(Progress pg
) throws IOException
{
63 metas
= lib
.getMetas(pg
);
67 return new ArrayList
<MetaData
>(metas
);
71 public synchronized MetaData
getInfo(String luid
) throws IOException
{
72 MetaData info
= cacheLib
.getInfo(luid
);
74 info
= lib
.getInfo(luid
);
81 public synchronized Story
getStory(String luid
, MetaData meta
, Progress pg
)
87 Progress pgImport
= new Progress();
88 Progress pgGet
= new Progress();
91 pg
.addProgress(pgImport
, 3);
92 pg
.addProgress(pgGet
, 1);
94 if (!isCached(luid
)) {
96 cacheLib
.imprt(lib
, luid
, pgImport
);
97 updateInfo(cacheLib
.getInfo(luid
));
99 } catch (IOException e
) {
100 Instance
.getInstance().getTraceHandler().error(e
);
107 String type
= cacheLib
.getOutputType(meta
.isImageDocument());
108 MetaData cachedMeta
= meta
.clone();
109 cachedMeta
.setType(type
);
111 return cacheLib
.getStory(luid
, cachedMeta
, pg
);
115 public synchronized File
getFile(final String luid
, Progress pg
)
121 Progress pgGet
= new Progress();
122 Progress pgRecall
= new Progress();
125 pg
.addProgress(pgGet
, 4);
126 pg
.addProgress(pgRecall
, 1);
128 if (!isCached(luid
)) {
129 getStory(luid
, pgGet
);
133 File file
= cacheLib
.getFile(luid
, pgRecall
);
141 public Image
getCover(final String luid
) throws IOException
{
142 if (isCached(luid
)) {
143 return cacheLib
.getCover(luid
);
146 // We could update the cache here, but it's not easy
147 return lib
.getCover(luid
);
151 public Image
getSourceCover(String source
) throws IOException
{
152 Image custom
= getCustomSourceCover(source
);
153 if (custom
!= null) {
157 Image cached
= cacheLib
.getSourceCover(source
);
158 if (cached
!= null) {
162 return lib
.getSourceCover(source
);
166 public Image
getAuthorCover(String author
) throws IOException
{
167 Image custom
= getCustomAuthorCover(author
);
168 if (custom
!= null) {
172 Image cached
= cacheLib
.getAuthorCover(author
);
173 if (cached
!= null) {
177 return lib
.getAuthorCover(author
);
181 public Image
getCustomSourceCover(String source
) throws IOException
{
182 Image custom
= cacheLib
.getCustomSourceCover(source
);
183 if (custom
== null) {
184 custom
= lib
.getCustomSourceCover(source
);
185 if (custom
!= null) {
186 cacheLib
.setSourceCover(source
, custom
);
194 public Image
getCustomAuthorCover(String author
) throws IOException
{
195 Image custom
= cacheLib
.getCustomAuthorCover(author
);
196 if (custom
== null) {
197 custom
= lib
.getCustomAuthorCover(author
);
198 if (custom
!= null) {
199 cacheLib
.setAuthorCover(author
, custom
);
207 public void setSourceCover(String source
, String luid
) throws IOException
{
208 lib
.setSourceCover(source
, luid
);
209 cacheLib
.setSourceCover(source
, getCover(luid
));
213 public void setAuthorCover(String author
, String luid
) throws IOException
{
214 lib
.setAuthorCover(author
, luid
);
215 cacheLib
.setAuthorCover(author
, getCover(luid
));
219 protected void updateInfo(MetaData meta
) throws IOException
{
220 if (meta
!= null && metas
!= null) {
221 boolean changed
= false;
222 for (int i
= 0; i
< metas
.size(); i
++) {
223 if (metas
.get(i
).getLuid().equals(meta
.getLuid())) {
234 cacheLib
.updateInfo(meta
);
235 lib
.updateInfo(meta
);
239 protected void invalidateInfo(String luid
) {
242 } else if (metas
!= null) {
243 for (int i
= 0; i
< metas
.size(); i
++) {
244 if (metas
.get(i
).getLuid().equals(luid
)) {
250 cacheLib
.invalidateInfo(luid
);
251 lib
.invalidateInfo(luid
);
255 public synchronized Story
save(Story story
, String luid
, Progress pg
)
257 Progress pgLib
= new Progress();
258 Progress pgCacheLib
= new Progress();
265 pg
.addProgress(pgLib
, 1);
266 pg
.addProgress(pgCacheLib
, 1);
268 story
= lib
.save(story
, luid
, pgLib
);
269 story
= cacheLib
.save(story
, story
.getMeta().getLuid(), pgCacheLib
);
271 updateInfo(story
.getMeta());
277 public synchronized void delete(String luid
) throws IOException
{
278 if (isCached(luid
)) {
279 cacheLib
.delete(luid
);
283 invalidateInfo(luid
);
287 protected synchronized void changeSTA(String luid
, String newSource
,
288 String newTitle
, String newAuthor
, Progress pg
) throws IOException
{
293 Progress pgCache
= new Progress();
294 Progress pgOrig
= new Progress();
296 pg
.addProgress(pgCache
, 1);
297 pg
.addProgress(pgOrig
, 1);
299 MetaData meta
= getInfo(luid
);
301 throw new IOException("Story not found: " + luid
);
304 if (isCached(luid
)) {
305 cacheLib
.changeSTA(luid
, newSource
, newTitle
, newAuthor
, pgCache
);
309 lib
.changeSTA(luid
, newSource
, newTitle
, newAuthor
, pgOrig
);
312 meta
.setSource(newSource
);
313 meta
.setTitle(newTitle
);
314 meta
.setAuthor(newAuthor
);
317 invalidateInfo(luid
);
321 public boolean isCached(String luid
) {
323 return cacheLib
.getInfo(luid
) != null;
324 } catch (IOException e
) {
330 public void clearFromCache(String luid
) throws IOException
{
331 if (isCached(luid
)) {
332 cacheLib
.delete(luid
);
337 public MetaData
imprt(URL url
, Progress pg
) throws IOException
{
342 Progress pgImprt
= new Progress();
343 Progress pgCache
= new Progress();
345 pg
.addProgress(pgImprt
, 7);
346 pg
.addProgress(pgCache
, 3);
348 MetaData meta
= lib
.imprt(url
, pgImprt
);
351 clearFromCache(meta
.getLuid());
357 // All the following methods are only used by Save and Delete in
361 protected int getNextId() {
362 throw new java
.lang
.InternalError("Should not have been called");
366 protected void doDelete(String luid
) throws IOException
{
367 throw new java
.lang
.InternalError("Should not have been called");
371 protected Story
doSave(Story story
, Progress pg
) throws IOException
{
372 throw new java
.lang
.InternalError("Should not have been called");