1
2
3
4
5
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
24
25
26
27
28
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
38
39
40 public void setPicMan(PicManFacade pmf) {
41 this.pmf = pmf;
42 }
43
44
45
46
47
48 public int getPicProcessing() {
49 return picProcessing;
50 }
51
52
53
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
62
63 pmf.addJobToLog(jle);
64 Pic pic;
65 File file;
66 while (iter.hasNext()) {
67 picProcessing++;
68 pic = iter.next();
69
70
71
72
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 }