| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 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.business.contact.ContactEngine; |
| 20 | |
import com.buckosoft.PicMan.domain.Contact; |
| 21 | |
import com.buckosoft.PicMan.domain.Pic; |
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | 0 | public class QuadrantSets extends ContactEngine { |
| 31 | 0 | private boolean DEBUG = false; |
| 32 | 0 | private int quadrantRows = 1; |
| 33 | 0 | private int quadrantCols = 3; |
| 34 | 0 | private int numQuadrants = quadrantRows * quadrantCols; |
| 35 | 0 | private String[] workingQuadrantDirs = new String[numQuadrants]; |
| 36 | |
private ArrayList<LinkedList<String>> workingQuadrantFiles; |
| 37 | |
private LinkedList<String> dirSets; |
| 38 | |
private List<String> picsInContact; |
| 39 | |
private Graphics2D g; |
| 40 | |
|
| 41 | |
private Contact c; |
| 42 | |
private int[] picInRow; |
| 43 | |
private int[] colInRow; |
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
protected boolean _makeContact() { |
| 50 | |
|
| 51 | 0 | boolean success = true; |
| 52 | |
|
| 53 | 0 | logger.info("makeContact '" + cName + "'"); |
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | 0 | picsInContact = this.contactManager.getPicNames(contactParams); |
| 58 | 0 | Collections.sort(picsInContact); |
| 59 | 0 | logger.info("select from " + picsInContact.size() + " filters"); |
| 60 | 0 | if (picsInContact.size() == 0) { |
| 61 | 0 | RuntimeException e = new RuntimeException("No pics to make contact " + contactParams.getUuid()); |
| 62 | 0 | pmf.addError(e); |
| 63 | 0 | return(false); |
| 64 | |
} |
| 65 | 0 | HashMap<String, Integer> dirSetsHash = new HashMap<String, Integer>(); |
| 66 | |
Pic pic; |
| 67 | |
|
| 68 | 0 | for (String s : picsInContact) { |
| 69 | 0 | pic = pmf.getDB().getPic(s); |
| 70 | 0 | Integer x = dirSetsHash.get(pic.getLocation()); |
| 71 | 0 | if (x == null) |
| 72 | 0 | x = 0; |
| 73 | 0 | x++; |
| 74 | 0 | dirSetsHash.put(pic.getLocation(), x); |
| 75 | 0 | } |
| 76 | 0 | dirSets = randomize(new LinkedList<String>(dirSetsHash.keySet())); |
| 77 | |
|
| 78 | 0 | if (logger.isDebugEnabled()) { |
| 79 | 0 | for (String key : dirSets) { |
| 80 | 0 | logger.debug("dirSet " + key); |
| 81 | 0 | } |
| 82 | |
} |
| 83 | |
|
| 84 | 0 | workingQuadrantFiles = new ArrayList<LinkedList<String>>(); |
| 85 | 0 | for (int i=0; i<numQuadrants; i++) { |
| 86 | 0 | workingQuadrantFiles.add(null); |
| 87 | 0 | restock(i); |
| 88 | |
} |
| 89 | |
|
| 90 | 0 | BufferedImage bi = new BufferedImage(cWidth, cHeight, BufferedImage.TYPE_3BYTE_BGR); |
| 91 | 0 | g = bi.createGraphics(); |
| 92 | 0 | c = new Contact(); |
| 93 | 0 | c.setName(cUuid); |
| 94 | 0 | c.setStartTime(new Date()); |
| 95 | 0 | c.setCid(chain.getCid()); |
| 96 | 0 | int rowCount = cHeight/tHeight; |
| 97 | 0 | int quadrantRowCount = rowCount / quadrantRows; |
| 98 | 0 | colInRow = new int[rowCount]; |
| 99 | 0 | picInRow = new int[rowCount]; |
| 100 | |
try { |
| 101 | 0 | int curQuadrant = 0; |
| 102 | 0 | int colWidth = cWidth / quadrantCols; |
| 103 | 0 | for (int qr=0; qr < quadrantRows; qr++) { |
| 104 | 0 | for (int qc=0; qc<quadrantCols; qc++) { |
| 105 | 0 | if (DEBUG) |
| 106 | 0 | logger.debug("qr=" + qr + " qc=" + qc + " curQuadrant=" + curQuadrant); |
| 107 | 0 | fillQuadrant(qr*quadrantRowCount, (qr+1)*quadrantRowCount, (qc+1)*colWidth, curQuadrant); |
| 108 | 0 | curQuadrant++; |
| 109 | |
} |
| 110 | 0 | if (isAbort()) |
| 111 | 0 | break; |
| 112 | |
} |
| 113 | 0 | drawContactLabel(g); |
| 114 | 0 | } catch (Exception e) { |
| 115 | 0 | logger.error("Error drawing contact", e); |
| 116 | 0 | pmf.addError(e); |
| 117 | 0 | return(false); |
| 118 | |
|
| 119 | 0 | } |
| 120 | 0 | success = writePic(bi, false); |
| 121 | 0 | c.setEndTime(new Date()); |
| 122 | 0 | pmf.getDB().addContact(c); |
| 123 | 0 | logger.info("Done with " + cUuid); |
| 124 | 0 | return(success); |
| 125 | |
} |
| 126 | |
|
| 127 | |
private void fillQuadrant(int curRow, int lrow, int lcol, int curQuadrant) { |
| 128 | |
String tName; |
| 129 | 0 | if (DEBUG) |
| 130 | 0 | logger.debug("curRow=" + curRow + " lrow=" + lrow + " lcol=" + lcol + " curQuadrant=" + curQuadrant); |
| 131 | 0 | while (curRow < lrow) { |
| 132 | 0 | while (colInRow[curRow] < lcol) { |
| 133 | 0 | if (workingQuadrantFiles.get(curQuadrant).isEmpty()) |
| 134 | 0 | restock(curQuadrant); |
| 135 | 0 | tName = (String)workingQuadrantFiles.get(curQuadrant).pop(); |
| 136 | 0 | if (DEBUG) |
| 137 | 0 | logger.debug(tName + " col/row = " + colInRow[curRow] + "/" + curRow); |
| 138 | 0 | int widthDrawn = drawThumb(g, tName, tHeight, colInRow[curRow], curRow*tHeight); |
| 139 | 0 | c.setPic(tName, curRow, picInRow[curRow]); |
| 140 | |
|
| 141 | 0 | colInRow[curRow] += widthDrawn; |
| 142 | 0 | picInRow[curRow]++; |
| 143 | 0 | } |
| 144 | 0 | curRow++; |
| 145 | |
} |
| 146 | 0 | } |
| 147 | |
|
| 148 | |
private void restock(int i) { |
| 149 | 0 | workingQuadrantDirs[i] = dirSets.pop(); |
| 150 | 0 | dirSets.add(workingQuadrantDirs[i]); |
| 151 | 0 | workingQuadrantFiles.set(i, fillQuadrantFiles(workingQuadrantDirs[i])); |
| 152 | 0 | } |
| 153 | |
|
| 154 | |
private LinkedList<String> fillQuadrantFiles(String dir) { |
| 155 | 0 | LinkedList<String> list = new LinkedList<String>(); |
| 156 | 0 | for (String s : picsInContact) { |
| 157 | 0 | Pic pic = pmf.getDB().getPic(s); |
| 158 | 0 | if (pic.getLocation().equals(dir)) |
| 159 | 0 | list.add(pic.getName()); |
| 160 | 0 | } |
| 161 | 0 | Collections.sort(list); |
| 162 | 0 | return(list); |
| 163 | |
} |
| 164 | |
} |