View Javadoc
1   /******************************************************************************
2    * PicManImpl.java - Implement the main logic for PicMan
3    * 
4    * PicMan - The BuckoSoft Picture Manager in Java
5    * Copyright(c) 2005 - Dick Balaska
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  /** Implement the primary API for PicMan.
42   * @author Dick Balaska
43   * @since 2005/07/25
44   * @see <a href="http://cvs.buckosoft.com/Projects/java/PicMan/PicMan/src/com/buckosoft/PicMan/business/PicManImpl.java">PicManImpl.java</a>
45   */
46  public class PicManImpl extends PicManCommonImpl implements PicManFacade {
47  	//private static final boolean DEBUG		  = false;
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  //		this.picFilterWeb.setPicMan(this);
77  //		this.setSizeWebTable.setPicMan(this);
78  		this.batchManager.setPicMan(this);
79  //		this.htmlUtils.setPicMan(this);
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 	/** AOP Injector to set the Database
110 	 * @param pmdb A reference to the database
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 //		this.picReader.setDatabase(dbf);
118 		this.picFilterWeb.setDatabase(dbf);
119 		this.setSizeWebTable.setDatabase(dbf);
120 //		this.contactManager.setDatabase(dbf);
121 		this.htmlUtils.setDatabase(dbf);
122 //		this.picReader.setDatabase(dbf);
123 
124 		this.setupThumbCache();
125 	}
126 
127 	/** Enable logger output for the BatchManager
128 	 * @param debugFlag true == turn on debugging.
129 	 */
130 	public void setDEBUGBatchManager(boolean debugFlag) {
131 		this.batchManager.setDEBUG(debugFlag);
132 	}
133 
134 	/** Enable logger output for the BatchManager Entry
135 	 * @param debugFlag true == turn on debugging.
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 	/** Enable logger output for the MosaicMan.
148 	 * @param debugFlag true == turn on debugging.
149 	 */
150 	public void setDEBUGMosaicMan(boolean debugFlag) {
151 		this.mosaicMan.setDEBUG(debugFlag);
152 	}
153 
154 	/** Enable logger output for the Mosaic Engine.
155 	 * @param debugFlag true == turn on debugging.
156 	 */
157 	public void setDEBUGMosaicEngine(boolean debugFlag) {
158 		this.mosaicMan.setDEBUGEngine(debugFlag);
159 	}
160 
161 	
162 	/**
163 	 * @return the bsAccountMan
164 	 */
165 	public BSAccountMan getBSAccountMan() {
166 		return bsAccountMan;
167 	}
168 
169 	/**
170 	 * @param bsAccountMan the bsAccountMan to set
171 	 */
172 	public void setBSAccountMan(BSAccountMan bsAccountMan) {
173 		this.bsAccountMan = bsAccountMan;
174 	}
175 
176 	///////////////////////////////////////////////////////////////////////////
177 	/* Pic reading */
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 //		if (height <= this.thumbCache.getCachedHeight())
213 //			return(picReader.getThumbNail(pic, height, null));
214 		return(picReader.getThumbNail(pic, height, null));
215 	}
216 
217 	///////////////////////////////////////////////////////////////////////////
218 	/* ThumbNail cache */
219 
220 	/* (non-Javadoc)
221 	 * @see com.buckosoft.PicMan.business.PicManFacade#getThumbCache()
222 	 */
223 	public		ThumbCache	getThumbCache() {
224 		return(this.thumbCache);
225 	}
226 
227 	
228 	/* (non-Javadoc)
229 	 * @see com.buckosoft.PicMan.business.PicManFacade#setupThumbCache()
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 	/* Mosaic ThumbNail cache */
239 
240 	/* (non-Javadoc)
241 	 * @see com.buckosoft.PicMan.business.PicManFacade#setupMosaicThumbCache(int, int)
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 		//this.mosaicThumbCache.batchBuildCache();
252 	}
253 
254 	/* (non-Javadoc)
255 	 * @see com.buckosoft.PicMan.business.PicManFacade#releaseMosaicThumbCache()
256 	 */
257 	public void releaseMosaicThumbCache() {
258 		if (this.mosaicThumbCache != null)
259 			this.mosaicThumbCache.release();
260 		this.mosaicThumbCache = null;
261 	}
262 
263 	/**
264 	 * @return the syncManager
265 	 */
266 	public SyncManager_Old getSyncManager() {
267 		return syncManager;
268 	}
269 
270 	/* (non-Javadoc)
271 	 * @see com.buckosoft.PicMan.business.PicManFacade#getJobLog()
272 	 */
273 	public LinkedList<JobLogEntry> getJobLog() {
274 		return jobLog;
275 	}
276 
277 	/* (non-Javadoc)
278 	 * @see com.buckosoft.PicMan.business.PicManFacade#getJobLogMaxCount()
279 	 */
280 	public int	getJobLogMaxCount() {
281 		return(10);
282 	}
283 
284 	/* (non-Javadoc)
285 	 * @see com.buckosoft.PicMan.business.PicManFacade#addJobToLog(com.buckosoft.PicMan.domain.JobLogEntry)
286 	 */
287 	public void addJobToLog(JobLogEntry jle) {
288 		ListIterator<JobLogEntry> liter = jobLog.listIterator();
289 		while (liter.hasNext()) {
290 			JobLogEntry ojle = liter.next();
291 			//if (jle.getStartTime().equals(ojle.getStartTime())) {
292 			if (jle.getSerial() == ojle.getSerial()) {
293 				liter.set(jle);
294 				return;
295 			}
296 		}
297 		jobLog.addFirst(jle);
298 	}
299 
300 	
301 	///////////////////////////////////////////////////////////////////////////
302 	/* Web output */
303 
304 	/* (non-Javadoc)
305 	 * @see com.buckosoft.PicMan.business.PicManFacade#getPicFilterWebEdit(java.lang.String)
306 	 */
307 	public	String		getPicFilterWebEdit(String picName) {
308 		return(picFilterWeb.getFilterEdit(picName));
309 	}
310 
311 	/* (non-Javadoc)
312 	 * @see com.buckosoft.PicMan.business.PicManFacade#getSetSizeWebTable(java.util.List)
313 	 */
314 	@Deprecated
315 	public	String		getSetSizeWebTable(List<SetSize> setSizeList, boolean showUnused) {
316 		return(this.setSizeWebTable.getSetSizeWebTable(setSizeList, showUnused));
317 	}
318 
319 	/* (non-Javadoc)
320 	 * @see com.buckosoft.PicMan.business.PicManFacade#getContactSelectorHtml(com.buckosoft.PicMan.domain.Chain, java.lang.String, java.lang.String)
321 	 */
322 	public	String		getContactSelectorHtml(Chain chain, String setName, String uri) {
323 		return(this.htmlUtils.getContactSelectorHtml(chain, setName, uri));
324 	}
325 	
326 	/* (non-Javadoc)
327 	 * @see com.buckosoft.PicMan.business.PicManFacade#getSetSizeCheckboxesHtml(com.buckosoft.PicMan.domain.Chain)
328 	 */
329 	public	String		getSetSizeCheckboxesHtml(Chain chain) {
330 		return(this.htmlUtils.getSetSizeCheckboxesHtml(chain));
331 	}
332 
333 	///////////////////////////////////////////////////////////////////////////
334 	/* Batch manager */
335 
336 	/** Run one time through the <code>BatchManager</code> loop.  Do any work if need be.
337 	 * Called by the cron trigger job that was set up in the Spring configuration.
338 	 * @see <a href="http://cvs.buckosoft.com/Projects/java/PicMan/PicMan/WebContent/WEB-INF/PicMan-servlet.xml">PicMan-servlet.xml</a>
339 	 */
340 	public	void		runBatchManager() throws Exception {
341 		batchManager.run();
342 	}
343 
344 	/* (non-Javadoc)
345 	 * @see com.buckosoft.PicMan.business.PicManFacade#getBatchManager()
346 	 */
347 	public BatchManager	getBatchManager() {
348 		return(this.batchManager);
349 	}
350 
351 	/* (non-Javadoc)
352 	 * @see com.buckosoft.PicMan.business.PicManFacade#engineRunningShortStatus()
353 	 */
354 	public	String	engineRunningShortStatus() {
355 		return(batchManager.engineRunningShortStatus());
356 	}
357 
358 	/* (non-Javadoc)
359 	 * @see com.buckosoft.PicMan.business.PicManFacade#setEngineCronExpression(java.lang.String)
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 	/* (non-Javadoc)
373 	 * @see com.buckosoft.PicMan.business.PicManFacade#getFilterColumnsAsHtml()
374 	 */
375 /*
376 	public	List<String>	getFilterColumnsAsHtml() {
377 		if (DEBUG)
378 			logger.info("getFilterColumns()");
379 		return(htmlUtils.getFilterColumnsAsHtml(this.dbf.getFilterColumns()));
380 	}
381 */
382 
383 	/* (non-Javadoc)
384 	 * @see com.buckosoft.PicMan.business.PicManFacade#getMaskedFilter(java.util.List, com.buckosoft.PicMan.domain.Filter)
385 	 */
386 	public	Filter	getMaskedFilter(List<Set> sets, Filter filter) {
387 		Filter f = new Filter();
388 		f.setPicName(filter.getPicName());
389 		//f.setPicMan(this.pmf);
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 	/* (non-Javadoc)
409 	 * @see com.buckosoft.PicMan.business.PicManFacade#getTestMosaicThumbNail(com.buckosoft.PicMan.domain.Pic, int, int)
410 	 */
411 	public Thumbnail	getTestMosaicThumbNail(Pic pic, int height, int depth) {
412 		return(this.mosaicMan.getMosaicManDevelopment().getMosaicThumbNail(pic, height, depth));
413 	}
414 
415 	/* (non-Javadoc)
416 	 * @see com.buckosoft.PicMan.business.PicManFacade#determinePicSize(com.buckosoft.PicMan.domain.Pic)
417 	 */
418 	public Dimension determinePicSize(Pic pic) {
419 		return(this.picReader.determinePicSize(pic));
420 	}
421 
422 	/* (non-Javadoc)
423 	 * @see com.buckosoft.PicMan.business.PicManFacade#getMosaicMan()
424 	 */
425 	public MosaicMan getMosaicMan() {
426 		return mosaicMan;
427 	}
428 
429 	/* (non-Javadoc)
430 	 * @see com.buckosoft.PicMan.business.PicManFacade#getPicImporter()
431 	 */
432 	public PicImporter getPicImporter() {
433 		if (picImporter == null) {
434 			picImporter = new PicImporterImpl();
435 			picImporter.setPicMan(this);
436 		}
437 		return(picImporter);
438 	}
439 
440 	/* (non-Javadoc)
441 	 * @see com.buckosoft.PicMan.business.PicManFacade#getPicReader()
442 	 */
443 	public	PicReader	getPicReader() {
444 		return(picReader);
445 	}
446 
447 	/* (non-Javadoc)
448 	 * @see com.buckosoft.PicMan.business.PicManFacade#getFilesInDirCount(int, java.lang.String)
449 	 */
450 	public int getFilesInDirCount(int rid, String dir) {
451 		return(this.picReader.getFilesInDirCount(rid, dir));
452 	}
453 
454 	/* (non-Javadoc)
455 	 * @see com.buckosoft.PicMan.business.PicManFacade#getDirsInDir(int, java.lang.String)
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 	/* (non-Javadoc)
478 	 * @see com.buckosoft.PicMan.business.PicManFacade#getFilterAsString(java.lang.String)
479 	 */
480 	@Override
481 	public String getFilterAsString(String picName) {
482 		return(FilterUtil.getFilterAsString(this, picName));
483 	}
484 
485 	/* (non-Javadoc)
486 	 * @see com.buckosoft.PicMan.business.PicManFacade#isEngineOn()
487 	 */
488 	public boolean isEngineOn() {
489 		return(this.getDB().getSystem().isEngineOn());
490 	}
491 
492 	/* (non-Javadoc)
493 	 * @see com.buckosoft.PicMan.business.PicManFacade#getPosterManager()
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 }