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
.bundles
.UiConfigBundle
;
11 import be
.nikiroo
.fanfix
.data
.MetaData
;
12 import be
.nikiroo
.fanfix
.data
.Story
;
13 import be
.nikiroo
.fanfix
.output
.BasicOutput
.OutputType
;
14 import be
.nikiroo
.utils
.Image
;
15 import be
.nikiroo
.utils
.Progress
;
18 * This library will cache another pre-existing {@link BasicLibrary}.
22 public class CacheLibrary
extends BasicLibrary
{
23 private List
<MetaData
> metas
;
24 private BasicLibrary lib
;
25 private LocalLibrary cacheLib
;
28 * Create a cache library around the given one.
30 * It will return the same result, but those will be saved to disk at the same
31 * time to be fetched quicker the next time.
33 * @param cacheDir the cache directory where to save the files to disk
34 * @param lib the original library to wrap
35 * @param config the configuration used to know which kind of default
36 * {@link OutputType} to use for images and non-images stories
38 public CacheLibrary(File cacheDir
, BasicLibrary lib
, UiConfigBundle config
) {
39 this.cacheLib
= new LocalLibrary(cacheDir
, //
40 config
.getString(UiConfig
.GUI_NON_IMAGES_DOCUMENT_TYPE
),
41 config
.getString(UiConfig
.GUI_IMAGES_DOCUMENT_TYPE
), true);
46 public String
getLibraryName() {
47 return lib
.getLibraryName();
51 public Status
getStatus() {
52 return lib
.getStatus();
56 protected List
<MetaData
> getMetas(Progress pg
) throws IOException
{
62 metas
= lib
.getMetas(pg
);
70 public synchronized MetaData
getInfo(String luid
) throws IOException
{
71 MetaData info
= cacheLib
.getInfo(luid
);
73 info
= lib
.getInfo(luid
);
80 public synchronized Story
getStory(String luid
, MetaData meta
, Progress pg
)
86 Progress pgImport
= new Progress();
87 Progress pgGet
= new Progress();
90 pg
.addProgress(pgImport
, 3);
91 pg
.addProgress(pgGet
, 1);
93 if (!isCached(luid
)) {
95 cacheLib
.imprt(lib
, luid
, pgImport
);
96 updateInfo(cacheLib
.getInfo(luid
));
98 } catch (IOException e
) {
99 Instance
.getInstance().getTraceHandler().error(e
);
106 String type
= cacheLib
.getOutputType(meta
.isImageDocument());
107 MetaData cachedMeta
= meta
.clone();
108 cachedMeta
.setType(type
);
110 return cacheLib
.getStory(luid
, cachedMeta
, pg
);
114 public synchronized File
getFile(final String luid
, Progress pg
)
120 Progress pgGet
= new Progress();
121 Progress pgRecall
= new Progress();
124 pg
.addProgress(pgGet
, 4);
125 pg
.addProgress(pgRecall
, 1);
127 if (!isCached(luid
)) {
128 getStory(luid
, pgGet
);
132 File file
= cacheLib
.getFile(luid
, pgRecall
);
140 public Image
getCover(final String luid
) throws IOException
{
141 if (isCached(luid
)) {
142 return cacheLib
.getCover(luid
);
145 // We could update the cache here, but it's not easy
146 return lib
.getCover(luid
);
150 public Image
getSourceCover(String source
) throws IOException
{
151 Image custom
= getCustomSourceCover(source
);
152 if (custom
!= null) {
156 Image cached
= cacheLib
.getSourceCover(source
);
157 if (cached
!= null) {
161 return lib
.getSourceCover(source
);
165 public Image
getAuthorCover(String author
) throws IOException
{
166 Image custom
= getCustomAuthorCover(author
);
167 if (custom
!= null) {
171 Image cached
= cacheLib
.getAuthorCover(author
);
172 if (cached
!= null) {
176 return lib
.getAuthorCover(author
);
180 public Image
getCustomSourceCover(String source
) throws IOException
{
181 Image custom
= cacheLib
.getCustomSourceCover(source
);
182 if (custom
== null) {
183 custom
= lib
.getCustomSourceCover(source
);
184 if (custom
!= null) {
185 cacheLib
.setSourceCover(source
, custom
);
193 public Image
getCustomAuthorCover(String author
) throws IOException
{
194 Image custom
= cacheLib
.getCustomAuthorCover(author
);
195 if (custom
== null) {
196 custom
= lib
.getCustomAuthorCover(author
);
197 if (custom
!= null) {
198 cacheLib
.setAuthorCover(author
, custom
);
206 public void setSourceCover(String source
, String luid
) throws IOException
{
207 lib
.setSourceCover(source
, luid
);
208 cacheLib
.setSourceCover(source
, getCover(luid
));
212 public void setAuthorCover(String author
, String luid
) throws IOException
{
213 lib
.setAuthorCover(author
, luid
);
214 cacheLib
.setAuthorCover(author
, getCover(luid
));
218 protected void updateInfo(MetaData meta
) throws IOException
{
219 if (meta
!= null && metas
!= null) {
220 boolean changed
= false;
221 for (int i
= 0; i
< metas
.size(); i
++) {
222 if (metas
.get(i
).getLuid().equals(meta
.getLuid())) {
233 cacheLib
.updateInfo(meta
);
234 lib
.updateInfo(meta
);
238 protected void invalidateInfo(String luid
) {
241 } else if (metas
!= null) {
242 for (int i
= 0; i
< metas
.size(); i
++) {
243 if (metas
.get(i
).getLuid().equals(luid
)) {
249 cacheLib
.invalidateInfo(luid
);
250 lib
.invalidateInfo(luid
);
254 public synchronized Story
save(Story story
, String luid
, Progress pg
)
256 Progress pgLib
= new Progress();
257 Progress pgCacheLib
= new Progress();
264 pg
.addProgress(pgLib
, 1);
265 pg
.addProgress(pgCacheLib
, 1);
267 story
= lib
.save(story
, luid
, pgLib
);
268 story
= cacheLib
.save(story
, story
.getMeta().getLuid(), pgCacheLib
);
270 updateInfo(story
.getMeta());
276 public synchronized void delete(String luid
) throws IOException
{
277 if (isCached(luid
)) {
278 cacheLib
.delete(luid
);
282 invalidateInfo(luid
);
286 protected synchronized void changeSTA(String luid
, String newSource
,
287 String newTitle
, String newAuthor
, Progress pg
) throws IOException
{
292 Progress pgCache
= new Progress();
293 Progress pgOrig
= new Progress();
295 pg
.addProgress(pgCache
, 1);
296 pg
.addProgress(pgOrig
, 1);
298 MetaData meta
= getInfo(luid
);
300 throw new IOException("Story not found: " + luid
);
303 if (isCached(luid
)) {
304 cacheLib
.changeSTA(luid
, newSource
, newTitle
, newAuthor
, pgCache
);
308 lib
.changeSTA(luid
, newSource
, newTitle
, newAuthor
, pgOrig
);
311 meta
.setSource(newSource
);
312 meta
.setTitle(newTitle
);
313 meta
.setAuthor(newAuthor
);
316 invalidateInfo(luid
);
320 * Check if the {@link Story} denoted by this Library UID is present in the
326 * @return TRUE if it is
328 public boolean isCached(String luid
) {
330 return cacheLib
.getInfo(luid
) != null;
331 } catch (IOException e
) {
337 * Clear the {@link Story} from the cache.
339 * The next time we try to retrieve the {@link Story}, it may be required to
345 * @throws IOException
346 * in case of I/O error
348 public void clearFromCache(String luid
) throws IOException
{
349 if (isCached(luid
)) {
350 cacheLib
.delete(luid
);
355 public MetaData
imprt(URL url
, Progress pg
) throws IOException
{
360 Progress pgImprt
= new Progress();
361 Progress pgCache
= new Progress();
363 pg
.addProgress(pgImprt
, 7);
364 pg
.addProgress(pgCache
, 3);
366 MetaData meta
= lib
.imprt(url
, pgImprt
);
369 clearFromCache(meta
.getLuid());
375 // All the following methods are only used by Save and Delete in
379 protected int getNextId() {
380 throw new java
.lang
.InternalError("Should not have been called");
384 protected void doDelete(String luid
) throws IOException
{
385 throw new java
.lang
.InternalError("Should not have been called");
389 protected Story
doSave(Story story
, Progress pg
) throws IOException
{
390 throw new java
.lang
.InternalError("Should not have been called");