View Javadoc

1   /*
2    *  ginp - Java Web Application for Viewing Photo Collections
3    *  Copyright (C) 2004  Douglas John Culnane <doug@culnane.net>
4    *
5    *  This library is free software; you can redistribute it and/or
6    *  modify it under the terms of the GNU Lesser General Public
7    *  License as published by the Free Software Foundation; either
8    *  version 2.1 of the License, or any later version.
9    *
10   *  This library is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   *  Lesser General Public License for more details.
14   *
15   *  You should have received a copy of the GNU Lesser General Public
16   *  License along with this library; if not, write to the Free Software
17   *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18   */
19  package net.sf.ginp;
20  
21  
22  import com.drew.imaging.jpeg.*;
23  import com.drew.metadata.*;
24  
25  import java.io.File;
26  import java.text.SimpleDateFormat;
27  import java.util.Date;
28  import java.util.Iterator;
29  import java.util.Locale;
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  
33  /**
34   *  This class represents a Picture. It contains a repository of functionality
35   *  for Pictures. It is designed to be called as a tempoary object that is used
36   *  and then discarded.
37   *
38   *@author     Doug Culnane
39   *@version    $Revision: 321 $
40   */
41  public class Picture {
42  
43                      String         absPath;
44                      int            collection_id;
45                      String         collection_name;
46                      String         fileName;
47                      String         id;
48                      Date           timestamp;
49                      String         title;
50                      
51      private Log log = LogFactory.getLog(Picture.class);
52  
53      /**
54       *  Constructor for the Picture object
55       */
56      public Picture() { }
57                     
58  	/**
59  	 * Returns the value of collection_id.
60  	 */
61  	public int getCollection_id() {
62  		return collection_id;
63  	}
64  
65  	/**
66  	 * Sets the value of collection_id.
67  	 * @param collection_id The value to assign collection_id.
68  	 */
69  	public void setCollection_id(int collection_id)	{
70  		this.collection_id = collection_id;
71  	}
72  
73  	/**
74  	 * Returns the value of collection_name.
75  	 */
76  	public String getCollection_name() {
77  		return collection_name;
78  	}
79  
80  	/**
81  	 * Sets the value of collection_name.
82  	 * @param collection_name The value to assign collection_name.
83  	 */
84  	public void setCollection_name(String collection_name) {
85  		this.collection_name = collection_name;
86  	}
87  
88      /**
89       *  Constructor for the PicCollection object. The xml parameter contains the
90       *  configuration for this PicCollection.
91       *
92       *@param  setAbsPath   path to the jpeg file (can be null or empty). Path
93       *                     separator is ALWAYS "/", regardless of platform.
94       *                     A training path separator is mandatory.
95       *@param  setFileName  filename of the jpeg. It can include some path
96       *                     elements, which will be appended to the absPath.
97       */
98      public Picture(String setAbsPath, String setFileName) {
99  
100         if (setFileName.indexOf("/") != -1) {
101             // filename contains path elements, so append to abs path
102             absPath = setAbsPath + setFileName.substring(0,
103                                     setFileName.lastIndexOf("/") + 1);
104             fileName = setFileName.substring(setFileName.lastIndexOf("/") + 1);
105         }
106         else {
107             // filename is standalone, so use the given path
108             absPath = setAbsPath;
109             fileName = setFileName;
110         }
111 
112         try {
113             // try to get data from EXIF
114             File      jpegFile          = new File(absPath + fileName);
115             Metadata  metadata          = JpegMetadataReader.readMetadata(jpegFile);
116             String    dateTimeOriginal  = "";
117             String    dateTime          = "";
118 
119             // iterate through metadata directories
120             Iterator  directories       = metadata.getDirectoryIterator();
121             while (directories.hasNext()) {
122                 Directory  directory  = (Directory) directories.next();
123                 // iterate through tags and print to System.out
124                 Iterator   tags       = directory.getTagIterator();
125                 while (tags.hasNext()) {
126                     Tag  tag  = (Tag) tags.next();
127                     if (tag.getTagName().equals("Date/Time Original")) {
128                         dateTimeOriginal = tag.getDescription();
129                     } else if (tag.getTagName().equals("Date/Time")) {
130                         dateTime = tag.getDescription();
131                     }
132                 }
133             }
134             if (!setDateTime(dateTimeOriginal)) {
135                 setDateTime(dateTime);
136             }
137         } catch (Exception e) {
138             log.error("Error constructing new picture.", e);
139         }
140     }
141 
142 
143     /**
144      *  Sets the absLocation attribute of the Picture object
145      *
146      *@param  str  The new absLocation value
147      */
148     public void setAbsLocation(String str) {
149         if (str.startsWith("/")) {
150             absPath = str.substring(0, str.lastIndexOf("/") + 1);
151             fileName = str.substring(str.lastIndexOf("/") + 1);
152         }
153     }
154 
155 
156     /**
157      *  Sets the Date and Time attributes of the Picture object based on EXIF
158      *  tag value
159      *
160      *@param  str  The new Date/Time setting String value
161      *@return      True if sucessfully set Date and Time
162      */
163     public boolean setDateTime(String str) {
164         
165     	boolean done = false;
166         timestamp = null;
167         if (str != null && str.length() == 19) {
168             try {
169             	// 2002:12:22 14:47:25
170             	java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("yyyy:MM:dd hh:mm:ss");
171             	timestamp = df.parse(str);
172             	done = true;
173             } catch (Exception e) {
174                 log.error("Error seting Date Time: " + str,e);
175                 timestamp = null;
176             }
177         }
178         return done;
179     }
180 
181 
182     /**
183      *  Sets the id attribute of the Picture object
184      *
185      *@param  str  The new id value
186      */
187     public void setId(String str) {
188         id = str;
189     }
190 
191 
192     /**
193      *  Sets the timestamp attribute of the Picture object
194      *
195      *@param  d  The new timestamp value
196      */
197     public void setTimestamp(Date d) {
198         timestamp = d;
199     }
200 
201 
202     /**
203      *  Sets the title attribute of the Picture object
204      *
205      *@param  s  The new title value
206      */
207     public void setTitle(String s) {
208         title = s;
209     }
210 
211 
212     /**
213      *  Gets the absLocation attribute of the Picture object
214      *
215      *@return    The absLocation value
216      */
217     public String getAbsLocation() {
218         return absPath + fileName;
219     }
220 
221 
222     /**
223      *  Gets the date attribute of the Picture object. Formated to the Locale.
224      *
225      *@param  formatPattern  Description of the Parameter
226      *@param  locale         Prefered Locale
227      *@return                The formated date
228      */
229     public String getDate(String formatPattern, Locale locale) {
230         if (timestamp == null) {
231             return "";
232         } else {
233             if (formatPattern.equals("")) {
234                 formatPattern = "yyyy.MM.dd";
235             } 
236             SimpleDateFormat  df  = new SimpleDateFormat(formatPattern, locale);
237             return df.format(timestamp);
238         }
239 
240     }
241 
242 
243     /**
244      *  Gets the fileName attribute of the Picture object
245      *
246      *@return    The fileName value
247      */
248     public String getFileName() {
249         return fileName;
250     }
251 
252 
253     /**
254      *  Gets the id attribute of the Picture object
255      *
256      *@return    The id value
257      */
258     public String getId() {
259         return id;
260     }
261 
262 
263     /**
264      *  Gets the time attribute of the Picture object
265      *
266      *@return    The time value
267      */
268     public String getTime() {
269         if (timestamp == null) {
270             return "";
271         } else {
272             SimpleDateFormat  df  = new SimpleDateFormat("kk:mm:ss");
273             return df.format(timestamp);
274         }
275     }
276 
277 
278     /**
279      *  Gets the timestamp attribute of the Picture object
280      *
281      *@return    The timestamp value
282      */
283     public Date getTimestamp() {
284         return timestamp;
285     }
286 
287 
288     /**
289      *  Gets the title attribute of the Picture object
290      *
291      *@return    The title value
292      */
293     public String getTitle() {
294         if (title == null) {
295             return fileName.substring(0, fileName.lastIndexOf("."));
296         } else {
297             return title;
298         }
299     }
300     
301     public String getDescription(Locale loc) {
302         return "ToDo";
303     }
304     
305     
306     
307 }