Coverage Report - com.buckosoft.PicMan.business.contact.engine.SequentialSets
 
Classes in this File Line Coverage Branch Coverage Complexity
SequentialSets
0%
0/90
0%
0/32
5.75
 
 1  
 /******************************************************************************
 2  
  * SequentialSets.java - Make a list of random sets sorted by pictures within each set
 3  
  * 
 4  
  * PicMan - The BuckoSoft Picture Manager in Java
 5  
  * Copyright(c) 2011 - Dick Balaska
 6  
  * 
 7  
  */
 8  
 package com.buckosoft.PicMan.business.contact.engine;
 9  
 
 10  
 import java.awt.Graphics2D;
 11  
 import java.awt.image.BufferedImage;
 12  
 import java.util.ArrayList;
 13  
 import java.util.Collections;
 14  
 import java.util.Date;
 15  
 import java.util.HashMap;
 16  
 import java.util.LinkedList;
 17  
 import java.util.List;
 18  
 
 19  
 import com.buckosoft.PicMan.domain.Contact;
 20  
 import com.buckosoft.PicMan.domain.Pic;
 21  
 
 22  
 /** Make a contact that contains a list of random sets sorted by pictures within each set
 23  
  * @author Dick Balaska
 24  
  * @since 2011/12/22
 25  
  * @version $Revision: 1.2 $ <br> $Date: 2014/02/13 07:24:02 $
 26  
  * @see <a href="http://cvs.buckosoft.com/Projects/PicMan/PicMan/src/main/java/com/buckosoft/PicMan/business/contact/engine/SequentialSets.java">SequentialSets.java</a>
 27  
  */
 28  0
 public class SequentialSets extends QuadrantSets {
 29  0
         private        boolean        DEBUG = false;
 30  0
         private int        quadrantRows = 1;
 31  0
         private int        quadrantCols = 1;
 32  0
         private int numQuadrants = quadrantRows * quadrantCols;
 33  0
         private String[] workingQuadrantDirs = new String[numQuadrants];
 34  
         private ArrayList<LinkedList<String>> workingQuadrantFiles;
 35  
         private LinkedList<String> dirSets;
 36  
         private        List<String> picsInContact;
 37  
         private        Graphics2D g;
 38  
 //        private        Font        
 39  
         private Contact c;
 40  
         private int[] picInRow;
 41  
         private        int[] colInRow;
 42  
         
 43  
 
 44  
         /** Make a contact sheet based on the setup.
 45  
          * @return Success
 46  
          */
 47  
         protected        boolean        _makeContact() {
 48  
 
 49  0
                 boolean        success = true;
 50  
                 
 51  0
                 logger.info("makeContact '" + cName + "'");
 52  
 
 53  
 //                picsInContact = pmf.getDB().getPicNamesBySet(contactParams.getSetName(), contactParams.getSize());
 54  
 //                picsInContact = setupPicsList(contactParams, picsInContact);
 55  0
                 picsInContact = this.contactManager.getPicNames(contactParams);
 56  0
                 Collections.sort(picsInContact);
 57  0
                 logger.info("select from " + picsInContact.size() + " filters");
 58  0
                 if (picsInContact.size() == 0) {
 59  0
                         RuntimeException e = new RuntimeException("No pics to make contact " + contactParams.getUuid());
 60  0
                         pmf.addError(e);
 61  0
                         return(false);
 62  
                 }
 63  0
                 HashMap<String, Integer> dirSetsHash = new HashMap<String, Integer>();
 64  
                 Pic                        pic;
 65  
                 // figure out all of the dirSets that are available
 66  0
                 for (String s : picsInContact) {
 67  0
                         pic = pmf.getDB().getPic(s);
 68  0
                         Integer x = dirSetsHash.get(pic.getLocation());
 69  0
                         if (x == null)
 70  0
                                 x = 0;
 71  0
                         x++;
 72  0
                         dirSetsHash.put(pic.getLocation(), x);
 73  0
                 }
 74  0
                 dirSets = randomize(new LinkedList<String>(dirSetsHash.keySet()));
 75  
                 
 76  0
                 if (logger.isDebugEnabled()) {
 77  0
                         for (String key : dirSets) {
 78  0
                                 logger.debug("dirSet " + key);
 79  0
                         }
 80  
                 }
 81  
                 // pick the initial sets to work with, one for each quadrant.
 82  0
                 workingQuadrantFiles = new ArrayList<LinkedList<String>>();
 83  0
                 for (int i=0; i<numQuadrants; i++) {
 84  0
                         workingQuadrantFiles.add(null);
 85  0
                         restock(i);
 86  
                 }
 87  
 
 88  0
                 BufferedImage bi = new BufferedImage(cWidth, cHeight, BufferedImage.TYPE_3BYTE_BGR);
 89  0
                 g = bi.createGraphics();
 90  0
                 c = new Contact();
 91  0
                 c.setName(cUuid);
 92  0
                 c.setStartTime(new Date());
 93  0
                 c.setCid(chain.getCid());
 94  0
                 int        rowCount = cHeight/tHeight;
 95  0
                 int quadrantRowCount = rowCount / quadrantRows;
 96  0
                 colInRow = new int[rowCount];
 97  0
                 picInRow = new int[rowCount];
 98  
                 try {
 99  0
                         int curQuadrant = 0;
 100  0
                         int        colWidth = cWidth / quadrantCols;
 101  0
                         for (int qr=0; qr < quadrantRows; qr++) {
 102  0
                                 for (int qc=0; qc<quadrantCols; qc++) {
 103  0
                                         if (DEBUG)
 104  0
                                                 logger.debug("qr=" + qr + " qc=" + qc + " curQuadrant=" + curQuadrant);
 105  0
                                         fillQuadrant(qr*quadrantRowCount, (qr+1)*quadrantRowCount, (qc+1)*colWidth, curQuadrant);
 106  0
                                         curQuadrant++;
 107  
                                 }
 108  
                         }
 109  0
                         drawContactLabel(g);
 110  0
                 } catch (Exception e) {
 111  0
                         logger.error("Error drawing contact", e);
 112  0
                         pmf.addError(e);
 113  0
                         return(false);
 114  
                         
 115  0
                 }
 116  0
                 success = writePic(bi, false);
 117  0
                 c.setEndTime(new Date());
 118  0
                 pmf.getDB().addContact(c);
 119  0
                 logger.info("Done with " + cUuid);
 120  0
                 return(success);
 121  
         }
 122  
 
 123  
         private void fillQuadrant(int curRow, int lrow, int lcol, int curQuadrant) {
 124  
                 String                tName;
 125  0
                 if (DEBUG)
 126  0
                         logger.debug("curRow=" + curRow + " lrow=" + lrow + " lcol=" + lcol + " curQuadrant=" + curQuadrant);
 127  0
                 while (curRow < lrow) {
 128  0
                         while (colInRow[curRow] < lcol) {
 129  0
                                 if (workingQuadrantFiles.get(curQuadrant).isEmpty())
 130  0
                                         restock(curQuadrant);
 131  0
                                 tName = (String)workingQuadrantFiles.get(curQuadrant).pop();
 132  0
                                 if (DEBUG)
 133  0
                                         logger.debug(tName + " col/row = " + colInRow[curRow] + "/" + curRow);
 134  0
                                 int widthDrawn = drawThumb(g, tName, tHeight, colInRow[curRow], curRow*tHeight);
 135  0
                                 c.setPic(tName, curRow, picInRow[curRow]);
 136  
                                 
 137  0
                                 colInRow[curRow] += widthDrawn;
 138  0
                                 picInRow[curRow]++;
 139  0
                         }
 140  0
                         curRow++;
 141  
                 }
 142  0
         }
 143  
 
 144  
         private        void        restock(int i) {
 145  0
                 workingQuadrantDirs[i] = dirSets.pop();
 146  0
                 dirSets.add(workingQuadrantDirs[i]);
 147  0
                 workingQuadrantFiles.set(i, fillQuadrantFiles(workingQuadrantDirs[i]));                
 148  0
         }
 149  
 
 150  
         private        LinkedList<String> fillQuadrantFiles(String dir) {
 151  0
                 LinkedList<String> list = new LinkedList<String>();
 152  0
                 for (String s : picsInContact) {
 153  0
                         Pic pic = pmf.getDB().getPic(s);
 154  0
                         if (pic.getLocation().equals(dir))
 155  0
                                 list.add(pic.getName());
 156  0
                 }
 157  0
                 Collections.sort(list);
 158  0
                 return(list);
 159  
         }
 160  
 }