1 package be
.nikiroo
.fanfix
.library
;
3 import java
.awt
.image
.BufferedImage
;
5 import java
.io
.IOException
;
9 import be
.nikiroo
.fanfix
.Instance
;
10 import be
.nikiroo
.fanfix
.bundles
.UiConfig
;
11 import be
.nikiroo
.fanfix
.data
.MetaData
;
12 import be
.nikiroo
.fanfix
.data
.Story
;
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
);
88 } catch (IOException e
) {
89 Instance
.getTraceHandler().error(e
);
96 File file
= cacheLib
.getFile(luid
, pgRecall
);
104 public BufferedImage
getCover(final String luid
) {
105 // TODO: cache doesn't seem to work
106 if (isCached(luid
)) {
107 return cacheLib
.getCover(luid
);
110 // We could update the cache here, but it's not easy
111 return lib
.getCover(luid
);
115 public BufferedImage
getSourceCover(String source
) {
116 // no cache for the source cover
117 return lib
.getSourceCover(source
);
121 public void setSourceCover(String source
, String luid
) {
122 lib
.setSourceCover(source
, luid
);
123 cacheLib
.setSourceCover(source
, getSourceCover(source
));
127 protected void clearCache() {
129 cacheLib
.clearCache();
134 public synchronized Story
save(Story story
, String luid
, Progress pg
)
136 Progress pgLib
= new Progress();
137 Progress pgCacheLib
= new Progress();
144 pg
.addProgress(pgLib
, 1);
145 pg
.addProgress(pgCacheLib
, 1);
147 story
= lib
.save(story
, luid
, pgLib
);
148 story
= cacheLib
.save(story
, story
.getMeta().getLuid(), pgCacheLib
);
156 public synchronized void delete(String luid
) throws IOException
{
157 if (isCached(luid
)) {
158 cacheLib
.delete(luid
);
165 public synchronized void changeSource(String luid
, String newSource
,
166 Progress pg
) throws IOException
{
171 Progress pgCache
= new Progress();
172 Progress pgOrig
= new Progress();
174 pg
.addProgress(pgCache
, 1);
175 pg
.addProgress(pgOrig
, 1);
177 if (isCached(luid
)) {
178 cacheLib
.changeSource(luid
, newSource
, pgCache
);
181 lib
.changeSource(luid
, newSource
, pgOrig
);
188 * Check if the {@link Story} denoted by this Library UID is present in the
194 * @return TRUE if it is
196 public boolean isCached(String luid
) {
197 return cacheLib
.getInfo(luid
) != null;
201 * Clear the {@link Story} from the cache.
206 * @throws IOException
207 * in case of I/O error
209 public void clearFromCache(String luid
) throws IOException
{
210 if (isCached(luid
)) {
211 cacheLib
.delete(luid
);
217 public Story
imprt(URL url
, Progress pg
) throws IOException
{
222 Progress pgImprt
= new Progress();
223 Progress pgCache
= new Progress();
225 pg
.addProgress(pgImprt
, 7);
226 pg
.addProgress(pgCache
, 3);
228 Story story
= lib
.imprt(url
, pgImprt
);
229 cacheLib
.save(story
, story
.getMeta().getLuid(), pgCache
);
235 // All the following methods are only used by Save and Delete in
239 protected int getNextId() {
240 throw new java
.lang
.InternalError("Should not have been called");
244 protected void doDelete(String luid
) throws IOException
{
245 throw new java
.lang
.InternalError("Should not have been called");
249 protected Story
doSave(Story story
, Progress pg
) throws IOException
{
250 throw new java
.lang
.InternalError("Should not have been called");