View Javadoc
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  public class PicSizeUpdater {
31  	private	static final boolean DEBUG = true;
32  	protected final Log logger = LogFactory.getLog(getClass());
33  
34  	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  		this.pmf = pmf;
42  	}
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  		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  		List<Pic> pics = pmf.getDB().getPics();
57  		Iterator<Pic> iter = pics.iterator();
58  		picProcessing = 0;
59  		JobLogEntry jle = new JobLogEntry();
60  		jle.setType(JobLogEntry.PICSIZE);
61  		//jle.setChainId();
62  		//jle.setName();
63  		pmf.addJobToLog(jle);
64  		Pic pic;
65  		File file;
66  		while (iter.hasNext()) {
67  			picProcessing++;
68  			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  			Dimension d = pmf.determinePicSize(pic);
74  			if (d.height == -1) {
75  				logger.info("Can't determine size for pic: " + pic.getName());
76  				d.height = 0;
77  				d.width = 0;
78  			}
79  			long time = 0;
80  			String s = pic.getLocation().replace('\\', '/');
81  			String fullPath;
82  			if (pic.getRid() != 0) {
83  				fullPath = pmf.getDB().getRoot(pic.getRid()).getPath() + "/" + s
84  					+ "/" + pic.getName() + ".jpg";
85  				try {
86  					file = new File(fullPath);
87  					time = file.lastModified();
88  				} catch (Exception e) {
89  					logger.warn("Can't get date for pic" + pic.getName());
90  				}
91  			}
92  			if (d.height != pic.getHeight() || d.width != pic.getWidth()
93  			 || !s.equals(pic.getLocation())
94  			 || pic.getDate().getTime() != time) {
95  				if (DEBUG)
96  					logger.info("Updating: " + pic.getName());
97  				pic.setHeight(d.height);
98  				pic.setWidth(d.width);
99  				pic.setLocation(s);
100 				pic.getDate().setTime(time);
101 				pmf.getDB().updatePic(pic);
102 			}
103 			
104 		}
105 		jle.setEndTime(new Date());
106 	}
107 }