1
2
3
4
5
6
7
8 package com.buckosoft.PicMan.business;
9
10
11 import java.awt.Dimension;
12 import java.io.File;
13 import java.util.Date;
14 import java.util.Iterator;
15 import java.util.LinkedList;
16 import java.util.List;
17 import java.util.ListIterator;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.springframework.scheduling.quartz.CronTriggerBean;
22
23 import com.buckosoft.BSAccount.BSAccountMan;
24 import com.buckosoft.PicMan.business.common.PicManCommonImpl;
25 import com.buckosoft.PicMan.business.mosaic.MosaicMan;
26 import com.buckosoft.PicMan.db.DatabaseFacade;
27 import com.buckosoft.PicMan.domain.Chain;
28 import com.buckosoft.PicMan.domain.Filter;
29 import com.buckosoft.PicMan.domain.JobLogEntry;
30 import com.buckosoft.PicMan.domain.Pic;
31 import com.buckosoft.PicMan.domain.Set;
32 import com.buckosoft.PicMan.domain.SetSize;
33 import com.buckosoft.PicMan.domain.Thumbnail;
34 import com.buckosoft.PicMan.image.PicReader;
35 import com.buckosoft.PicMan.image.ThumbCache;
36 import com.buckosoft.PicMan.util.FilterUtil;
37 import com.buckosoft.PicMan.web.HtmlUtils;
38 import com.buckosoft.PicMan.web.PicFilterWeb;
39 import com.buckosoft.PicMan.web.SetSizeWebTable;
40
41
42
43
44
45
46 public class PicManImpl extends PicManCommonImpl implements PicManFacade {
47
48 private static final boolean INJECT_ERROR = false;
49
50 protected final Log logger = LogFactory.getLog(getClass());
51
52
53 private CronTriggerBean batchManagerTrigger;
54
55 private PicReader picReader = new PicReader();
56 private PicFilterWeb picFilterWeb = new PicFilterWeb();
57 private SetSizeWebTable setSizeWebTable = new SetSizeWebTable();
58 private HtmlUtils htmlUtils = new HtmlUtils();
59 private BatchManager batchManager = new BatchManager();
60 private ThumbCache thumbCache = new ThumbCache();
61 private ThumbCache mosaicThumbCache = null;
62 private MosaicMan mosaicMan = new MosaicMan();
63 private PicImporterImpl picImporter = null;
64 private SyncManager_Old syncManager = new SyncManager_Old();
65 private MetaSetChangeAuditor metaSetChangeAuditor = new MetaSetChangeAuditor();
66 private PosterManager posterManager = null;
67
68 private BSAccountMan bsAccountMan;
69
70 private LinkedList<JobLogEntry> jobLog = new LinkedList<JobLogEntry>();
71
72 private boolean databaseDEBUG = false;
73
74 public PicManImpl() {
75 this.picReader.setPicMan(this);
76
77
78 this.batchManager.setPicMan(this);
79
80 this.mosaicMan.setPicMan(this);
81 this.thumbCache.setPicMan(this);
82 this.syncManager.setPicMan(this);
83 this.metaSetChangeAuditor.setPicMan(this);
84
85 this.picReader.setThumbCache(this.thumbCache);
86
87 if (INJECT_ERROR) {
88 Exception e = new Exception("Test");
89 e.fillInStackTrace();
90 logger.info(e);
91 addError(e);
92 logger.info("test exception...", e);
93 JobLogEntry jle = new JobLogEntry();
94 jle.setType(JobLogEntry.DONE);
95 jle.setError();
96 jle.setEndTime(new Date());
97 this.addJobToLog(jle);
98 }
99 }
100
101 public DatabaseFacade getDB() {
102 return(this.dbf);
103 }
104
105 public void setBatchManagerTrigger(CronTriggerBean batchManagerTrigger) {
106 this.batchManagerTrigger = batchManagerTrigger;
107 }
108
109
110
111
112 public void setPicManDatabase(DatabaseFacade pmdb) {
113 super.setPicManDatabase(pmdb);
114 this.dbf = pmdb;
115 this.dbf.setPicManCommonFacade(this);
116 this.dbf.setDEBUG(this.databaseDEBUG);
117
118 this.picFilterWeb.setDatabase(dbf);
119 this.setSizeWebTable.setDatabase(dbf);
120
121 this.htmlUtils.setDatabase(dbf);
122
123
124 this.setupThumbCache();
125 }
126
127
128
129
130 public void setDEBUGBatchManager(boolean debugFlag) {
131 this.batchManager.setDEBUG(debugFlag);
132 }
133
134
135
136
137 public void setDEBUGBatchManagerEntry(boolean debugFlag) {
138 this.batchManager.setDEBUGEntry(debugFlag);
139 }
140
141 public void setDEBUGDatabaseImpl(boolean debugFlag) {
142 this.databaseDEBUG = debugFlag;
143 if (this.dbf != null)
144 this.dbf.setDEBUG(debugFlag);
145 }
146
147
148
149
150 public void setDEBUGMosaicMan(boolean debugFlag) {
151 this.mosaicMan.setDEBUG(debugFlag);
152 }
153
154
155
156
157 public void setDEBUGMosaicEngine(boolean debugFlag) {
158 this.mosaicMan.setDEBUGEngine(debugFlag);
159 }
160
161
162
163
164
165 public BSAccountMan getBSAccountMan() {
166 return bsAccountMan;
167 }
168
169
170
171
172 public void setBSAccountMan(BSAccountMan bsAccountMan) {
173 this.bsAccountMan = bsAccountMan;
174 }
175
176
177
178
179
180 public Thumbnail getThumbNail(Pic pic, int height) {
181 if (this.mosaicThumbCache != null && height <= this.mosaicThumbCache.getCachedHeight()) {
182 Thumbnail tn = this.mosaicThumbCache.getThumbNail(pic, height);
183 if (tn != null)
184 return(tn);
185 }
186 return(picReader.getThumbNail(pic, height, null));
187 }
188
189
190 public Thumbnail getThumbNail(Pic pic, int height, String label) {
191 if (label == null)
192 return(getThumbNail(pic, height));
193 if (this.mosaicThumbCache != null && height <= this.mosaicThumbCache.getCachedHeight()) {
194 Thumbnail tn = this.mosaicThumbCache.getThumbNail(pic, height);
195 if (tn != null)
196 return(tn);
197 }
198 return(picReader.getThumbNail(pic, height, label));
199 }
200
201
202 public Thumbnail getMosaicThumbNail(Pic pic, int height) {
203 if (this.mosaicThumbCache != null && height <= this.mosaicThumbCache.getCachedHeight()) {
204 Thumbnail tn = this.mosaicThumbCache.getThumbNail(pic, height);
205 if (tn != null)
206 return(tn);
207 tn = picReader.getThumbNail(pic, this.mosaicThumbCache.getCachedHeight(), null);
208 this.mosaicThumbCache.addToCache(pic, tn);
209 tn = this.mosaicThumbCache.getThumbNail(pic, height);
210 return(tn);
211 }
212
213
214 return(picReader.getThumbNail(pic, height, null));
215 }
216
217
218
219
220
221
222
223 public ThumbCache getThumbCache() {
224 return(this.thumbCache);
225 }
226
227
228
229
230
231 public void setupThumbCache() {
232 this.thumbCache.setupCache(this.dbf.getSystem().getThumbCacheDirectory(),
233 this.dbf.getSystem().getThumbHeight(),
234 this.dbf.getSystem().getThumbCacheSize());
235 }
236
237
238
239
240
241
242
243 public void setupMosaicThumbCache(int sid, int height) {
244 if (this.mosaicThumbCache != null && this.mosaicThumbCache.getCachedHeight() == height)
245 return;
246 this.mosaicThumbCache = new ThumbCache();
247 this.mosaicThumbCache.setPicMan(this);
248 this.mosaicThumbCache.setupCache(this.dbf.getSystem().getMosaicThumbCacheDir(), height, Integer.MAX_VALUE);
249 this.mosaicThumbCache.setMosaicSet(sid);
250 this.mosaicThumbCache.deleteCache();
251
252 }
253
254
255
256
257 public void releaseMosaicThumbCache() {
258 if (this.mosaicThumbCache != null)
259 this.mosaicThumbCache.release();
260 this.mosaicThumbCache = null;
261 }
262
263
264
265
266 public SyncManager_Old getSyncManager() {
267 return syncManager;
268 }
269
270
271
272
273 public LinkedList<JobLogEntry> getJobLog() {
274 return jobLog;
275 }
276
277
278
279
280 public int getJobLogMaxCount() {
281 return(10);
282 }
283
284
285
286
287 public void addJobToLog(JobLogEntry jle) {
288 ListIterator<JobLogEntry> liter = jobLog.listIterator();
289 while (liter.hasNext()) {
290 JobLogEntry ojle = liter.next();
291
292 if (jle.getSerial() == ojle.getSerial()) {
293 liter.set(jle);
294 return;
295 }
296 }
297 jobLog.addFirst(jle);
298 }
299
300
301
302
303
304
305
306
307 public String getPicFilterWebEdit(String picName) {
308 return(picFilterWeb.getFilterEdit(picName));
309 }
310
311
312
313
314 @Deprecated
315 public String getSetSizeWebTable(List<SetSize> setSizeList, boolean showUnused) {
316 return(this.setSizeWebTable.getSetSizeWebTable(setSizeList, showUnused));
317 }
318
319
320
321
322 public String getContactSelectorHtml(Chain chain, String setName, String uri) {
323 return(this.htmlUtils.getContactSelectorHtml(chain, setName, uri));
324 }
325
326
327
328
329 public String getSetSizeCheckboxesHtml(Chain chain) {
330 return(this.htmlUtils.getSetSizeCheckboxesHtml(chain));
331 }
332
333
334
335
336
337
338
339
340 public void runBatchManager() throws Exception {
341 batchManager.run();
342 }
343
344
345
346
347 public BatchManager getBatchManager() {
348 return(this.batchManager);
349 }
350
351
352
353
354 public String engineRunningShortStatus() {
355 return(batchManager.engineRunningShortStatus());
356 }
357
358
359
360
361 public void setEngineCronExpression(String expression) {
362 try {
363 batchManagerTrigger.setCronExpression(expression);
364 } catch (Exception ex) {
365 Exception e1 = new Exception("setEngineCronExpression threw exception", ex);
366 e1.fillInStackTrace();
367 logger.info(e1);
368 addError(e1);
369 }
370 }
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386 public Filter getMaskedFilter(List<Set> sets, Filter filter) {
387 Filter f = new Filter();
388 f.setPicName(filter.getPicName());
389
390 f.setTime(filter.getDate());
391
392 List<Integer> sizes = dbf.getSizes();
393 Iterator<Set> setIter = sets.iterator();
394 while (setIter.hasNext()) {
395 Set set = setIter.next();
396 Iterator<Integer> sizeIter = sizes.iterator();
397 String s = set.getName();
398 while (sizeIter.hasNext()) {
399 int size = sizeIter.next().intValue();
400 int v = filter.getFilter(s, size);
401 if (v != 0)
402 f.addFilter(s, size, v);
403 }
404 }
405 return(f);
406 }
407
408
409
410
411 public Thumbnail getTestMosaicThumbNail(Pic pic, int height, int depth) {
412 return(this.mosaicMan.getMosaicManDevelopment().getMosaicThumbNail(pic, height, depth));
413 }
414
415
416
417
418 public Dimension determinePicSize(Pic pic) {
419 return(this.picReader.determinePicSize(pic));
420 }
421
422
423
424
425 public MosaicMan getMosaicMan() {
426 return mosaicMan;
427 }
428
429
430
431
432 public PicImporter getPicImporter() {
433 if (picImporter == null) {
434 picImporter = new PicImporterImpl();
435 picImporter.setPicMan(this);
436 }
437 return(picImporter);
438 }
439
440
441
442
443 public PicReader getPicReader() {
444 return(picReader);
445 }
446
447
448
449
450 public int getFilesInDirCount(int rid, String dir) {
451 return(this.picReader.getFilesInDirCount(rid, dir));
452 }
453
454
455
456
457 public List<String> getDirsInDir(int rid, String subDir) {
458 List<String> list = new LinkedList<String>();
459 String dir = this.getDB().getRoot(rid).getPath();
460 if (subDir == null)
461 subDir = ".";
462 File file = new File(dir, subDir);
463 File[] l = file.listFiles();
464 if (l == null)
465 return(list);
466 for (int i=0; i<l.length; i++) {
467 if (l[i].isDirectory()) {
468 if (l[i].getName().startsWith("."))
469 continue;
470 list.add(l[i].getName());
471 }
472 }
473 return(list);
474 }
475
476
477
478
479
480 @Override
481 public String getFilterAsString(String picName) {
482 return(FilterUtil.getFilterAsString(this, picName));
483 }
484
485
486
487
488 public boolean isEngineOn() {
489 return(this.getDB().getSystem().isEngineOn());
490 }
491
492
493
494
495 @Override
496 public PosterManager getPosterManager() {
497 if (posterManager == null) {
498 posterManager = new PosterManager();
499 posterManager.setPicMan(this);
500 }
501 return(this.posterManager);
502 }
503
504 }