Coverage Report - com.buckosoft.PicMan.business.PicManImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
PicManImpl
0%
0/163
0%
0/52
1.878
 
 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  0
         protected final Log logger = LogFactory.getLog(getClass());
 51  
 
 52  
 
 53  
         private        CronTriggerBean        batchManagerTrigger;
 54  
         
 55  0
         private        PicReader                                picReader = new PicReader();
 56  0
         private        PicFilterWeb                        picFilterWeb = new PicFilterWeb();
 57  0
         private        SetSizeWebTable                        setSizeWebTable = new SetSizeWebTable();
 58  0
         private        HtmlUtils                                htmlUtils = new HtmlUtils();
 59  0
         private        BatchManager                        batchManager = new BatchManager();
 60  0
         private        ThumbCache                                thumbCache = new ThumbCache();
 61  0
         private        ThumbCache                                mosaicThumbCache = null;
 62  0
         private        MosaicMan                                mosaicMan = new MosaicMan();
 63  0
         private        PicImporterImpl                        picImporter = null;
 64  0
         private        SyncManager_Old                                syncManager = new SyncManager_Old();
 65  0
         private        MetaSetChangeAuditor        metaSetChangeAuditor = new MetaSetChangeAuditor();
 66  0
         private        PosterManager                        posterManager = null;
 67  
         
 68  
         private        BSAccountMan        bsAccountMan;
 69  
 
 70  0
         private        LinkedList<JobLogEntry>                jobLog = new LinkedList<JobLogEntry>();
 71  
 
 72  0
         private        boolean                        databaseDEBUG = false;
 73  
 
 74  0
         public        PicManImpl() {
 75  0
                 this.picReader.setPicMan(this);
 76  
 //                this.picFilterWeb.setPicMan(this);
 77  
 //                this.setSizeWebTable.setPicMan(this);
 78  0
                 this.batchManager.setPicMan(this);
 79  
 //                this.htmlUtils.setPicMan(this);
 80  0
                 this.mosaicMan.setPicMan(this);
 81  0
                 this.thumbCache.setPicMan(this);
 82  0
                 this.syncManager.setPicMan(this);
 83  0
                 this.metaSetChangeAuditor.setPicMan(this);
 84  
 
 85  0
                 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  0
         }
 100  
 
 101  
         public        DatabaseFacade getDB() {
 102  0
                 return(this.dbf);
 103  
         }
 104  
 
 105  
         public void setBatchManagerTrigger(CronTriggerBean batchManagerTrigger) {
 106  0
                 this.batchManagerTrigger = batchManagerTrigger;
 107  0
         }
 108  
 
 109  
         /** AOP Injector to set the Database
 110  
          * @param pmdb A reference to the database
 111  
          */
 112  
         public void setPicManDatabase(DatabaseFacade pmdb) {
 113  0
                 super.setPicManDatabase(pmdb);
 114  0
                 this.dbf = pmdb;
 115  0
                 this.dbf.setPicManCommonFacade(this);
 116  0
                 this.dbf.setDEBUG(this.databaseDEBUG);
 117  
 //                this.picReader.setDatabase(dbf);
 118  0
                 this.picFilterWeb.setDatabase(dbf);
 119  0
                 this.setSizeWebTable.setDatabase(dbf);
 120  
 //                this.contactManager.setDatabase(dbf);
 121  0
                 this.htmlUtils.setDatabase(dbf);
 122  
 //                this.picReader.setDatabase(dbf);
 123  
 
 124  0
                 this.setupThumbCache();
 125  0
         }
 126  
 
 127  
         /** Enable logger output for the BatchManager
 128  
          * @param debugFlag true == turn on debugging.
 129  
          */
 130  
         public void setDEBUGBatchManager(boolean debugFlag) {
 131  0
                 this.batchManager.setDEBUG(debugFlag);
 132  0
         }
 133  
 
 134  
         /** Enable logger output for the BatchManager Entry
 135  
          * @param debugFlag true == turn on debugging.
 136  
          */
 137  
         public void setDEBUGBatchManagerEntry(boolean debugFlag) {
 138  0
                 this.batchManager.setDEBUGEntry(debugFlag);
 139  0
         }
 140  
 
 141  
         public void setDEBUGDatabaseImpl(boolean debugFlag) {
 142  0
                 this.databaseDEBUG = debugFlag;
 143  0
                 if (this.dbf != null)
 144  0
                         this.dbf.setDEBUG(debugFlag);
 145  0
         }
 146  
 
 147  
         /** Enable logger output for the MosaicMan.
 148  
          * @param debugFlag true == turn on debugging.
 149  
          */
 150  
         public void setDEBUGMosaicMan(boolean debugFlag) {
 151  0
                 this.mosaicMan.setDEBUG(debugFlag);
 152  0
         }
 153  
 
 154  
         /** Enable logger output for the Mosaic Engine.
 155  
          * @param debugFlag true == turn on debugging.
 156  
          */
 157  
         public void setDEBUGMosaicEngine(boolean debugFlag) {
 158  0
                 this.mosaicMan.setDEBUGEngine(debugFlag);
 159  0
         }
 160  
 
 161  
         
 162  
         /**
 163  
          * @return the bsAccountMan
 164  
          */
 165  
         public BSAccountMan getBSAccountMan() {
 166  0
                 return bsAccountMan;
 167  
         }
 168  
 
 169  
         /**
 170  
          * @param bsAccountMan the bsAccountMan to set
 171  
          */
 172  
         public void setBSAccountMan(BSAccountMan bsAccountMan) {
 173  0
                 this.bsAccountMan = bsAccountMan;
 174  0
         }
 175  
 
 176  
         ///////////////////////////////////////////////////////////////////////////
 177  
         /* Pic reading */
 178  
 
 179  
         ///////////////////////////////////////////////////////////////////////////
 180  
         public        Thumbnail        getThumbNail(Pic pic, int height) {
 181  0
                 if (this.mosaicThumbCache != null && height <= this.mosaicThumbCache.getCachedHeight()) {
 182  0
                         Thumbnail tn = this.mosaicThumbCache.getThumbNail(pic, height);
 183  0
                         if (tn != null)
 184  0
                                 return(tn);
 185  
                 }
 186  0
                 return(picReader.getThumbNail(pic, height, null));
 187  
         }
 188  
 
 189  
         ///////////////////////////////////////////////////////////////////////////
 190  
         public        Thumbnail        getThumbNail(Pic pic, int height, String label) {
 191  0
                 if (label == null)
 192  0
                         return(getThumbNail(pic, height));
 193  0
                 if (this.mosaicThumbCache != null && height <= this.mosaicThumbCache.getCachedHeight()) {
 194  0
                         Thumbnail tn = this.mosaicThumbCache.getThumbNail(pic, height);
 195  0
                         if (tn != null)
 196  0
                                 return(tn);
 197  
                 }
 198  0
                 return(picReader.getThumbNail(pic, height, label));
 199  
         }
 200  
 
 201  
         ///////////////////////////////////////////////////////////////////////////
 202  
         public        Thumbnail        getMosaicThumbNail(Pic pic, int height) {
 203  0
                 if (this.mosaicThumbCache != null && height <= this.mosaicThumbCache.getCachedHeight()) {
 204  0
                         Thumbnail tn = this.mosaicThumbCache.getThumbNail(pic, height);
 205  0
                         if (tn != null)
 206  0
                                 return(tn);
 207  0
                         tn = picReader.getThumbNail(pic, this.mosaicThumbCache.getCachedHeight(), null);
 208  0
                         this.mosaicThumbCache.addToCache(pic, tn);
 209  0
                         tn = this.mosaicThumbCache.getThumbNail(pic, height);
 210  0
                         return(tn);
 211  
                 }
 212  
 //                if (height <= this.thumbCache.getCachedHeight())
 213  
 //                        return(picReader.getThumbNail(pic, height, null));
 214  0
                 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  0
                 return(this.thumbCache);
 225  
         }
 226  
 
 227  
         
 228  
         /* (non-Javadoc)
 229  
          * @see com.buckosoft.PicMan.business.PicManFacade#setupThumbCache()
 230  
          */
 231  
         public void setupThumbCache() {
 232  0
                 this.thumbCache.setupCache(this.dbf.getSystem().getThumbCacheDirectory(),
 233  0
                                    this.dbf.getSystem().getThumbHeight(),
 234  0
                                    this.dbf.getSystem().getThumbCacheSize());
 235  0
         }
 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  0
                 if (this.mosaicThumbCache != null && this.mosaicThumbCache.getCachedHeight() == height)
 245  0
                         return;
 246  0
                 this.mosaicThumbCache = new ThumbCache();
 247  0
                 this.mosaicThumbCache.setPicMan(this);
 248  0
                 this.mosaicThumbCache.setupCache(this.dbf.getSystem().getMosaicThumbCacheDir(), height, Integer.MAX_VALUE);
 249  0
                 this.mosaicThumbCache.setMosaicSet(sid);
 250  0
                 this.mosaicThumbCache.deleteCache();
 251  
                 //this.mosaicThumbCache.batchBuildCache();
 252  0
         }
 253  
 
 254  
         /* (non-Javadoc)
 255  
          * @see com.buckosoft.PicMan.business.PicManFacade#releaseMosaicThumbCache()
 256  
          */
 257  
         public void releaseMosaicThumbCache() {
 258  0
                 if (this.mosaicThumbCache != null)
 259  0
                         this.mosaicThumbCache.release();
 260  0
                 this.mosaicThumbCache = null;
 261  0
         }
 262  
 
 263  
         /**
 264  
          * @return the syncManager
 265  
          */
 266  
         public SyncManager_Old getSyncManager() {
 267  0
                 return syncManager;
 268  
         }
 269  
 
 270  
         /* (non-Javadoc)
 271  
          * @see com.buckosoft.PicMan.business.PicManFacade#getJobLog()
 272  
          */
 273  
         public LinkedList<JobLogEntry> getJobLog() {
 274  0
                 return jobLog;
 275  
         }
 276  
 
 277  
         /* (non-Javadoc)
 278  
          * @see com.buckosoft.PicMan.business.PicManFacade#getJobLogMaxCount()
 279  
          */
 280  
         public int        getJobLogMaxCount() {
 281  0
                 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  0
                 ListIterator<JobLogEntry> liter = jobLog.listIterator();
 289  0
                 while (liter.hasNext()) {
 290  0
                         JobLogEntry ojle = liter.next();
 291  
                         //if (jle.getStartTime().equals(ojle.getStartTime())) {
 292  0
                         if (jle.getSerial() == ojle.getSerial()) {
 293  0
                                 liter.set(jle);
 294  0
                                 return;
 295  
                         }
 296  0
                 }
 297  0
                 jobLog.addFirst(jle);
 298  0
         }
 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  0
                 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  0
                 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  0
                 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  0
                 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  0
                 batchManager.run();
 342  0
         }
 343  
 
 344  
         /* (non-Javadoc)
 345  
          * @see com.buckosoft.PicMan.business.PicManFacade#getBatchManager()
 346  
          */
 347  
         public BatchManager        getBatchManager() {
 348  0
                 return(this.batchManager);
 349  
         }
 350  
 
 351  
         /* (non-Javadoc)
 352  
          * @see com.buckosoft.PicMan.business.PicManFacade#engineRunningShortStatus()
 353  
          */
 354  
         public        String        engineRunningShortStatus() {
 355  0
                 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  0
                         batchManagerTrigger.setCronExpression(expression);
 364  0
                 } catch (Exception ex) {
 365  0
                         Exception e1 = new Exception("setEngineCronExpression threw exception", ex);
 366  0
                         e1.fillInStackTrace();
 367  0
                         logger.info(e1);
 368  0
                         addError(e1);
 369  0
                 }
 370  0
         }
 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  0
                 Filter f = new Filter();
 388  0
                 f.setPicName(filter.getPicName());
 389  
                 //f.setPicMan(this.pmf);
 390  0
                 f.setTime(filter.getDate());
 391  
                 
 392  0
                 List<Integer>        sizes = dbf.getSizes();
 393  0
                 Iterator<Set> setIter = sets.iterator();
 394  0
                 while (setIter.hasNext()) {
 395  0
                         Set set = setIter.next();
 396  0
                         Iterator<Integer> sizeIter = sizes.iterator();
 397  0
                         String s = set.getName();
 398  0
                         while (sizeIter.hasNext()) {
 399  0
                                 int size = sizeIter.next().intValue();
 400  0
                                 int v = filter.getFilter(s, size);
 401  0
                                 if (v != 0)
 402  0
                                         f.addFilter(s, size, v);
 403  0
                         }
 404  0
                 }
 405  0
                 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  0
                 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  0
                 return(this.picReader.determinePicSize(pic));
 420  
         }
 421  
 
 422  
         /* (non-Javadoc)
 423  
          * @see com.buckosoft.PicMan.business.PicManFacade#getMosaicMan()
 424  
          */
 425  
         public MosaicMan getMosaicMan() {
 426  0
                 return mosaicMan;
 427  
         }
 428  
 
 429  
         /* (non-Javadoc)
 430  
          * @see com.buckosoft.PicMan.business.PicManFacade#getPicImporter()
 431  
          */
 432  
         public PicImporter getPicImporter() {
 433  0
                 if (picImporter == null) {
 434  0
                         picImporter = new PicImporterImpl();
 435  0
                         picImporter.setPicMan(this);
 436  
                 }
 437  0
                 return(picImporter);
 438  
         }
 439  
 
 440  
         /* (non-Javadoc)
 441  
          * @see com.buckosoft.PicMan.business.PicManFacade#getPicReader()
 442  
          */
 443  
         public        PicReader        getPicReader() {
 444  0
                 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  0
                 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  0
                 List<String> list = new LinkedList<String>();
 459  0
                 String dir = this.getDB().getRoot(rid).getPath();
 460  0
                 if (subDir == null)
 461  0
                         subDir = ".";
 462  0
                 File file = new File(dir, subDir);
 463  0
                 File[] l = file.listFiles();
 464  0
                 if (l == null)
 465  0
                         return(list);
 466  0
                 for (int i=0; i<l.length; i++) {
 467  0
                         if (l[i].isDirectory()) {
 468  0
                                 if (l[i].getName().startsWith("."))
 469  0
                                         continue;
 470  0
                                 list.add(l[i].getName());
 471  
                         }
 472  
                 }
 473  0
                 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  0
                 return(FilterUtil.getFilterAsString(this, picName));
 483  
         }
 484  
 
 485  
         /* (non-Javadoc)
 486  
          * @see com.buckosoft.PicMan.business.PicManFacade#isEngineOn()
 487  
          */
 488  
         public boolean isEngineOn() {
 489  0
                 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  0
                 if (posterManager == null) {
 498  0
                         posterManager = new PosterManager();
 499  0
                         posterManager.setPicMan(this);
 500  
                 }
 501  0
                 return(this.posterManager);
 502  
         }
 503  
 
 504  
 }