| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
package com.buckosoft.PicMan.business.contact; |
| 9 | |
|
| 10 | |
import java.awt.Color; |
| 11 | |
import java.awt.Graphics2D; |
| 12 | |
import java.awt.image.BufferedImage; |
| 13 | |
import java.io.File; |
| 14 | |
import java.text.DecimalFormat; |
| 15 | |
import java.util.Iterator; |
| 16 | |
import java.util.LinkedList; |
| 17 | |
import java.util.List; |
| 18 | |
|
| 19 | |
import javax.imageio.ImageIO; |
| 20 | |
import javax.imageio.ImageWriter; |
| 21 | |
import javax.imageio.stream.ImageOutputStream; |
| 22 | |
|
| 23 | |
import org.apache.commons.logging.Log; |
| 24 | |
import org.apache.commons.logging.LogFactory; |
| 25 | |
|
| 26 | |
import com.buckosoft.PicMan.business.ContactManager; |
| 27 | |
import com.buckosoft.PicMan.business.PicManFacade; |
| 28 | |
import com.buckosoft.PicMan.business.util.CopyFile; |
| 29 | |
import com.buckosoft.PicMan.db.DatabaseFacade; |
| 30 | |
import com.buckosoft.PicMan.domain.Chain; |
| 31 | |
import com.buckosoft.PicMan.domain.ContactParams; |
| 32 | |
import com.buckosoft.PicMan.domain.Pic; |
| 33 | |
import com.buckosoft.PicMan.domain.Set; |
| 34 | |
import com.buckosoft.PicMan.domain.System; |
| 35 | |
import com.buckosoft.PicMan.domain.Thumbnail; |
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | 0 | public abstract class ContactEngine { |
| 46 | |
|
| 47 | 0 | protected final Log logger = LogFactory.getLog(getClass()); |
| 48 | |
|
| 49 | |
protected PicManFacade pmf; |
| 50 | |
protected DatabaseFacade dbf; |
| 51 | |
protected System system; |
| 52 | |
protected ContactParams contactParams; |
| 53 | |
protected Set set; |
| 54 | |
protected int cWidth; |
| 55 | |
protected int cHeight; |
| 56 | |
protected String cName; |
| 57 | |
protected String cPath; |
| 58 | |
protected int tHeight; |
| 59 | |
protected String cUuid; |
| 60 | |
protected Chain chain; |
| 61 | |
protected ContactManager contactManager; |
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
public void setPicMan(PicManFacade pmf) { |
| 67 | 0 | this.pmf = pmf; |
| 68 | 0 | this.dbf = pmf.getDB(); |
| 69 | 0 | } |
| 70 | |
|
| 71 | |
public void setContactManager(ContactManager contactManager) { |
| 72 | 0 | this.contactManager = contactManager; |
| 73 | 0 | } |
| 74 | |
|
| 75 | |
protected abstract boolean _makeContact(); |
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
public boolean makeContact(ContactParams contactParams) { |
| 82 | 0 | this.contactParams = contactParams; |
| 83 | 0 | system = dbf.getSystem(); |
| 84 | 0 | chain = dbf.getChain(contactParams.getCid()); |
| 85 | 0 | cUuid = contactParams.getUuid(); |
| 86 | 0 | cWidth = chain.getContactWidth(); |
| 87 | 0 | cHeight = chain.getContactHeight(); |
| 88 | 0 | tHeight = contactParams.getSize(); |
| 89 | 0 | cName = chain.getContactPrefix() + cUuid; |
| 90 | 0 | cPath = contactParams.getOutputDirectory() + "/" + cName; |
| 91 | 0 | set = dbf.getSet(contactParams.getSetName()); |
| 92 | 0 | return(_makeContact()); |
| 93 | |
} |
| 94 | |
|
| 95 | |
|
| 96 | |
protected LinkedList<String> randomize(List<String> in) { |
| 97 | 0 | LinkedList<String> out = new LinkedList<String>(); |
| 98 | 0 | while (!in.isEmpty()) { |
| 99 | 0 | double d = Math.random() * in.size(); |
| 100 | 0 | out.add(in.remove((int)d)); |
| 101 | 0 | } |
| 102 | 0 | return(out); |
| 103 | |
} |
| 104 | |
|
| 105 | 0 | private final DecimalFormat df = new DecimalFormat("0.##"); |
| 106 | |
|
| 107 | |
protected int drawThumb(Graphics2D g, String tName, int tHeight, int x, int y) { |
| 108 | |
Pic pic; |
| 109 | |
Thumbnail tn; |
| 110 | 0 | pic = pmf.getDB().getPic(tName); |
| 111 | 0 | if (pmf.getPicReader().getQueueDepth() > 0) { |
| 112 | 0 | logger.info("Queue depth " + pmf.getPicReader().getQueueDepth() + " --- sleeping"); |
| 113 | |
try { |
| 114 | 0 | java.lang.Thread.sleep(3000); |
| 115 | 0 | } catch (InterruptedException e) { |
| 116 | 0 | } |
| 117 | |
} |
| 118 | 0 | String label = null; |
| 119 | 0 | if (system.isShowRateOnPics()) |
| 120 | 0 | label = df.format(dbf.getPicRate(tName, set, contactParams.getSize())); |
| 121 | 0 | tn = pmf.getPicReader().getThumbNail(pic, tHeight, label); |
| 122 | 0 | if (tn.isXThumb() && system.isSkipXThumbs()) |
| 123 | 0 | return(0); |
| 124 | 0 | g.drawImage(tn.getImage(), null, x, y); |
| 125 | 0 | return(tn.getImage().getWidth()); |
| 126 | |
} |
| 127 | |
|
| 128 | |
protected void drawContactLabel(Graphics2D g) { |
| 129 | 0 | int xp = 300; |
| 130 | 0 | int yp = 12; |
| 131 | 0 | String text = chain.getContactPrefix() + cUuid; |
| 132 | 0 | g.setColor(Color.BLACK); |
| 133 | 0 | g.drawString(text, xp+1, yp+1); |
| 134 | 0 | g.setColor(Color.WHITE); |
| 135 | 0 | g.drawString(text, xp, yp); |
| 136 | 0 | } |
| 137 | |
|
| 138 | |
protected boolean isAbort() { |
| 139 | 0 | return(this.contactManager.isAbort()); |
| 140 | |
} |
| 141 | |
|
| 142 | |
protected boolean writePic(BufferedImage bi, boolean makeBackup) { |
| 143 | 0 | Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg"); |
| 144 | 0 | ImageWriter writer = (ImageWriter)writers.next(); |
| 145 | 0 | File file = new File(cPath + ".jpg"); |
| 146 | 0 | logger.info("Writing jpg to '" + file.getPath() + "'"); |
| 147 | |
try { |
| 148 | 0 | ImageOutputStream oos = ImageIO.createImageOutputStream(file); |
| 149 | 0 | writer.setOutput(oos); |
| 150 | 0 | writer.write(bi); |
| 151 | 0 | oos.close(); |
| 152 | 0 | } catch (Exception e) { |
| 153 | 0 | bi = null; |
| 154 | 0 | pmf.addError(e); |
| 155 | 0 | return(false); |
| 156 | 0 | } |
| 157 | 0 | if (makeBackup) |
| 158 | 0 | CopyFile.makeBackup(pmf, file); |
| 159 | 0 | logger.info("Done with " + cUuid); |
| 160 | 0 | return(true); |
| 161 | |
} |
| 162 | |
} |