| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| PicSizeUpdater |
|
| 4.0;4 |
| 1 | /****************************************************************************** | |
| 2 | * PicSizeUpdater.java - Update the width/height for each pic in the database. | |
| 3 | * | |
| 4 | * PicMan - The BuckoSoft Picture Manager in Java | |
| 5 | * Copyright(c) 2007 - Dick Balaska | |
| 6 | * | |
| 7 | */ | |
| 8 | package com.buckosoft.PicMan.business.util; | |
| 9 | ||
| 10 | import java.awt.Dimension; | |
| 11 | import java.io.File; | |
| 12 | import java.util.Date; | |
| 13 | import java.util.Iterator; | |
| 14 | import java.util.List; | |
| 15 | ||
| 16 | import org.apache.commons.logging.Log; | |
| 17 | import org.apache.commons.logging.LogFactory; | |
| 18 | ||
| 19 | import com.buckosoft.PicMan.business.PicManFacade; | |
| 20 | import com.buckosoft.PicMan.domain.JobLogEntry; | |
| 21 | import com.buckosoft.PicMan.domain.Pic; | |
| 22 | ||
| 23 | /** Update the width/height for each pic in the database. <br/> | |
| 24 | * The first 12702 pictures went into the database with a 0/0 size, | |
| 25 | * and Mosaic needs to know the sizes to do it's work. <br/> | |
| 26 | * The first 15077 pictures went into the database with the incorrect date stamp from the pics. | |
| 27 | * @author Dick Balaska | |
| 28 | * @since 2007/12/13 | |
| 29 | */ | |
| 30 | 0 | public class PicSizeUpdater { |
| 31 | private static final boolean DEBUG = true; | |
| 32 | 0 | protected final Log logger = LogFactory.getLog(getClass()); |
| 33 | ||
| 34 | 0 | private int picProcessing = 0; |
| 35 | private PicManFacade pmf; | |
| 36 | ||
| 37 | /** Set the reference to the PicMan API. | |
| 38 | * @param pmf The PicManFacade | |
| 39 | */ | |
| 40 | public void setPicMan(PicManFacade pmf) { | |
| 41 | 0 | this.pmf = pmf; |
| 42 | 0 | } |
| 43 | ||
| 44 | /** Which ordinal picture are we working on? | |
| 45 | * This is just a status indicator | |
| 46 | * @return the picProcessing | |
| 47 | */ | |
| 48 | public int getPicProcessing() { | |
| 49 | 0 | return picProcessing; |
| 50 | } | |
| 51 | ||
| 52 | /** Primary entry point called from BatchManager. | |
| 53 | * Scan the entire database, read each Pic, and update it's size in the database. | |
| 54 | */ | |
| 55 | public void run() { | |
| 56 | 0 | List<Pic> pics = pmf.getDB().getPics(); |
| 57 | 0 | Iterator<Pic> iter = pics.iterator(); |
| 58 | 0 | picProcessing = 0; |
| 59 | 0 | JobLogEntry jle = new JobLogEntry(); |
| 60 | 0 | jle.setType(JobLogEntry.PICSIZE); |
| 61 | //jle.setChainId(); | |
| 62 | //jle.setName(); | |
| 63 | 0 | pmf.addJobToLog(jle); |
| 64 | Pic pic; | |
| 65 | File file; | |
| 66 | 0 | while (iter.hasNext()) { |
| 67 | 0 | picProcessing++; |
| 68 | 0 | pic = iter.next(); |
| 69 | // if (pic.getHeight() != 0 && pic.getWidth() != 0) | |
| 70 | // continue; | |
| 71 | //if (DEBUG) | |
| 72 | // logger.info("Update: " + pic.getLocation() + ":" + pic.getName()); | |
| 73 | 0 | Dimension d = pmf.determinePicSize(pic); |
| 74 | 0 | if (d.height == -1) { |
| 75 | 0 | logger.info("Can't determine size for pic: " + pic.getName()); |
| 76 | 0 | d.height = 0; |
| 77 | 0 | d.width = 0; |
| 78 | } | |
| 79 | 0 | long time = 0; |
| 80 | 0 | String s = pic.getLocation().replace('\\', '/'); |
| 81 | String fullPath; | |
| 82 | 0 | if (pic.getRid() != 0) { |
| 83 | 0 | fullPath = pmf.getDB().getRoot(pic.getRid()).getPath() + "/" + s |
| 84 | 0 | + "/" + pic.getName() + ".jpg"; |
| 85 | try { | |
| 86 | 0 | file = new File(fullPath); |
| 87 | 0 | time = file.lastModified(); |
| 88 | 0 | } catch (Exception e) { |
| 89 | 0 | logger.warn("Can't get date for pic" + pic.getName()); |
| 90 | 0 | } |
| 91 | } | |
| 92 | 0 | if (d.height != pic.getHeight() || d.width != pic.getWidth() |
| 93 | 0 | || !s.equals(pic.getLocation()) |
| 94 | 0 | || pic.getDate().getTime() != time) { |
| 95 | if (DEBUG) | |
| 96 | 0 | logger.info("Updating: " + pic.getName()); |
| 97 | 0 | pic.setHeight(d.height); |
| 98 | 0 | pic.setWidth(d.width); |
| 99 | 0 | pic.setLocation(s); |
| 100 | 0 | pic.getDate().setTime(time); |
| 101 | 0 | pmf.getDB().updatePic(pic); |
| 102 | } | |
| 103 | ||
| 104 | 0 | } |
| 105 | 0 | jle.setEndTime(new Date()); |
| 106 | 0 | } |
| 107 | } |