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