View Javadoc

1   package de.tivsource.page.common.image;
2   
3   import java.io.BufferedReader;
4   import java.io.File;
5   import java.io.FileInputStream;
6   import java.io.IOException;
7   import java.io.InputStreamReader;
8   import java.io.Serializable;
9   import java.util.Date;
10  import java.util.UUID;
11  
12  import javax.persistence.Basic;
13  import javax.persistence.MappedSuperclass;
14  import javax.persistence.Temporal;
15  import javax.persistence.Transient;
16  
17  import org.apache.logging.log4j.LogManager;
18  import org.apache.logging.log4j.Logger;
19  import org.apache.tools.ant.util.FileUtils;
20  import org.hibernate.annotations.Type;
21  
22  import de.tivsource.page.common.file.FileActions;
23  
24  /**
25   * Die Klasse Image dient dazu Bilder zu verwalten
26   *
27   * @author Marc Michele
28   *
29   */
30  @MappedSuperclass
31  public class ImageUntouched implements Comparable<ImageUntouched>, Serializable {
32  
33      /**
34       * 
35       */
36      private static final long serialVersionUID = 7403879687974877269L;
37  
38      /**
39       * Statischer Logger der Klasse Image für die Ausgabe mit log4j. Es gibt nur
40       * Ausgaben mit dem Log-Level "TRACE".
41       */
42      private static final Logger logger = LogManager.getLogger(ImageUntouched.class);
43  
44      protected static String uploadPath = "/srv/tiv-page/upload/";
45  
46      /**
47       * Lokaler Pfad der Orginal-Datei.
48       */
49      private String original;
50  
51      /**
52       * Lokaler Pfad der kleinen Abbildung mit 1000 x 1000 Pixeln.
53       */
54      private String large;
55  
56      /**
57       * Lokaler Pfad der normalen Abbildung mit 650 x 650 Pixeln.
58       */
59      private String normal;
60      
61      /**
62       * Lokaler Pfad der kleinen Abbildung mit 350 x 350 Pixeln.
63       */
64      private String small;
65  
66      /**
67       * Lokaler Pfad der Abbildung für Übersichtsseiten mit 148 x 148 Pixeln.
68       */
69      private String thumbnail;
70  
71      /**
72       * Lokaler Pfad der kleinen Abbildung mit 50 x 50 Pixeln.
73       */
74      private String micro;
75  
76      /**
77       * Boolean ob das Bild ein Default Bild ist (Ja/Nein).
78       */
79      @Basic
80      @Type(type = "yes_no")
81      private Boolean standard;
82  
83      /**
84       * Datei, die aus einem Formular hochgeladen wird.
85       */
86      @Transient
87      private File uploadFile;
88  
89      @Temporal(javax.persistence.TemporalType.TIMESTAMP)
90      private Date created;
91  
92      @Temporal(javax.persistence.TemporalType.TIMESTAMP)
93      private Date modified;
94  
95      private String modifiedBy;
96  
97      private String modifiedAddress;
98  
99      public ImageUntouched() {
100         super();
101     }
102 
103     /**
104      * @return the orginal
105      */
106     public String getOriginal() {
107         return original;
108     }
109 
110     /**
111      * @param original the orginal to set
112      */
113     public void setOriginal(String original) {
114         this.original = original;
115     }
116 
117     public String getLarge() {
118         return large;
119     }
120 
121     public void setLarge(String large) {
122         this.large = large;
123     }
124 
125     public String getNormal() {
126         return normal;
127     }
128 
129     public void setNormal(String normal) {
130         this.normal = normal;
131     }
132 
133     public String getSmall() {
134         return small;
135     }
136 
137     public void setSmall(String small) {
138         this.small = small;
139     }
140 
141     public String getThumbnail() {
142         return thumbnail;
143     }
144 
145     public void setThumbnail(String thumbnail) {
146         this.thumbnail = thumbnail;
147     }
148 
149     public String getMicro() {
150         return micro;
151     }
152 
153     public void setMicro(String micro) {
154         this.micro = micro;
155     }
156 
157     public Boolean isStandard() {
158         return standard;
159     }
160 
161     public void setStandard(Boolean standard) {
162         this.standard = standard;
163     }
164 
165     /**
166      * @return the created
167      */
168     public Date getCreated() {
169         return created;
170     }
171 
172     /**
173      * @param created the created to set
174      */
175     public void setCreated(Date created) {
176         this.created = created;
177     }
178 
179     /**
180      * @return the modified
181      */
182     public Date getModified() {
183         return modified;
184     }
185 
186     /**
187      * @param modified the modified to set
188      */
189     public void setModified(Date modified) {
190         this.modified = modified;
191     }
192 
193     /**
194      * @return the modifiedBy
195      */
196     public String getModifiedBy() {
197         return modifiedBy;
198     }
199 
200     /**
201      * @param modifiedBy the modifiedBy to set
202      */
203     public void setModifiedBy(String modifiedBy) {
204         this.modifiedBy = modifiedBy;
205     }
206 
207     /**
208      * @return the modifiedAddress
209      */
210     public String getModifiedAddress() {
211         return modifiedAddress;
212     }
213 
214     /**
215      * @param modifiedAddress the modifiedAddress to set
216      */
217     public void setModifiedAddress(String modifiedAddress) {
218         this.modifiedAddress = modifiedAddress;
219     }
220 
221     public FileInputStream getOriginalFileInputStream() {
222         try {
223             return new FileInputStream(new File(this.original));
224         } catch (Exception e) {
225             e.printStackTrace();
226             return null;
227         }
228     }
229 
230     public FileInputStream getLargeFileInputStream() {
231         try {
232             return new FileInputStream(new File(this.large));
233         } catch (Exception e) {
234             e.printStackTrace();
235             return null;
236         }
237     }
238 
239     public FileInputStream getNormalFileInputStream() {
240         try {
241             return new FileInputStream(new File(this.normal));
242         } catch (Exception e) {
243             e.printStackTrace();
244             return null;
245         }
246     }
247 
248     public FileInputStream getSmallFileInputStream() {
249         try {
250             return new FileInputStream(new File(this.small));
251         } catch (Exception e) {
252             e.printStackTrace();
253             return null;
254         }
255     }
256 
257     public FileInputStream getThumbnailFileInputStream() {
258         try {
259             return new FileInputStream(new File(this.thumbnail));
260         } catch (Exception e) {
261             e.printStackTrace();
262             return null;
263         }
264     }
265 
266     public FileInputStream getMicroFileInputStream() {
267         try {
268             return new FileInputStream(new File(this.micro));
269         } catch (Exception e) {
270             e.printStackTrace();
271             return null;
272         }
273     }
274 
275     /**
276      * @return the uploadFile
277      */
278     public File getUploadFile() {
279         return uploadFile;
280     }
281 
282     /**
283      * @param uploadFile
284      *            the uploadFile to set
285      */
286     public void setUploadFile(File uploadFile) {
287         this.uploadFile = uploadFile;
288     }
289 
290     public void generate() {
291 
292         // Generiere UUID für den Dateinamen
293         String pictureSaveName = UUID.randomUUID().toString();
294 
295         try {
296             // Datei die erstellt werden soll
297             File fileToCreate = new File(uploadPath, pictureSaveName + ".org.png");
298             logger.debug("Absoluter Pfad der neuen Picture-Datei : "
299                     + fileToCreate.getAbsolutePath());
300 
301             // Wenn die Datei noch nicht existiert wird Sie erstellt.
302             if (!fileToCreate.exists()) {
303                 FileActions.savePictureFile(this.getUploadFile(), fileToCreate);
304             }// Ende if
305         } // Ende try
306         catch (Exception e) {
307             logger.error(e.getMessage());
308         }// Ende Catch
309 
310         String s = null;
311 
312         String file = uploadPath + pictureSaveName + ".org.png";
313         String largeFile = uploadPath + pictureSaveName + ".large.png";
314         String normalFile = uploadPath + pictureSaveName + ".normal.png";
315         String smallFile = uploadPath + pictureSaveName + ".small.png";
316         String thumbFile = uploadPath + pictureSaveName + ".thumb.png";
317         String microFile = uploadPath + pictureSaveName + ".micro.png";
318         try {
319 
320             Process process = Runtime.getRuntime().exec(
321                     "/usr/bin/convert -resize 1000x1000 -quality 85 " + file + " "+ largeFile);
322             BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
323             BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
324 
325             while ((s = stdInput.readLine()) != null) {
326                 System.out.println(s);
327             }
328 
329             while ((s = stdError.readLine()) != null) {
330                 System.out.println(s);
331             }
332 
333             process = Runtime.getRuntime().exec(
334                     "/usr/bin/convert -resize 650x650 -quality 85 " + file + " "+ normalFile);
335             stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
336             stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
337 
338             while ((s = stdInput.readLine()) != null) {
339                 System.out.println(s);
340             }
341 
342             while ((s = stdError.readLine()) != null) {
343                 System.out.println(s);
344             }
345 
346             process = Runtime.getRuntime().exec(
347                     "/usr/bin/convert -resize 350x350 -quality 85 " + file + " "+ smallFile);
348             stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
349             stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
350 
351             while ((s = stdInput.readLine()) != null) {
352                 System.out.println(s);
353             }
354 
355             while ((s = stdError.readLine()) != null) {
356                 System.out.println(s);
357             }
358 
359             process = Runtime.getRuntime().exec(
360                     "/usr/bin/convert -resize 148x148 -quality 85 " + file + " " + thumbFile);
361             stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
362             stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
363 
364             while ((s = stdInput.readLine()) != null) {
365                 System.out.println(s);
366             }
367 
368             while ((s = stdError.readLine()) != null) {
369                 System.out.println(s);
370             }
371 
372             process = Runtime.getRuntime().exec(
373                     "/usr/bin/convert -resize 50x50 -quality 85 " + file + " " + microFile);
374             stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
375             stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
376 
377             while ((s = stdInput.readLine()) != null) {
378                 System.out.println(s);
379             }
380 
381             while ((s = stdError.readLine()) != null) {
382                 System.out.println(s);
383             }
384 
385         } catch (IOException e) {
386             System.out.println("exception happened - here's what I know: ");
387             e.printStackTrace();
388             System.exit(-1);
389         }
390 
391         this.setOriginal(file);
392         this.setLarge(largeFile);
393         this.setNormal(normalFile);
394         this.setSmall(smallFile);
395         this.setThumbnail(thumbFile);
396         this.setMicro(microFile);
397         this.setStandard(true);
398     }
399 
400     public void delete() {
401         File microFile = new File(micro);
402         if (microFile.exists()) {
403             FileUtils.delete(microFile);
404         }// Ende if
405 
406         File thumbnailFile = new File(thumbnail);
407         if (thumbnailFile.exists()) {
408             FileUtils.delete(thumbnailFile);
409         }// Ende if
410 
411         File smallFile = new File(small);
412         if (smallFile.exists()) {
413             FileUtils.delete(smallFile);
414         }// Ende if
415 
416         File normalFile = new File(normal);
417         if (normalFile.exists()) {
418             FileUtils.delete(normalFile);
419         }// Ende if
420 
421         File largeFile = new File(large);
422         if (largeFile.exists()) {
423             FileUtils.delete(largeFile);
424         }// Ende if
425 
426         File originalFile = new File(original);
427         if (originalFile.exists()) {
428             FileUtils.delete(originalFile);
429         }// Ende if
430     }// Ende delete()
431     
432     public int compareTo(ImageUntouched image) {
433         logger.trace("compareTo() aufgerufen.");
434         return image.getMicro().compareTo(this.getMicro()) * -1;
435     }
436 
437 }// Ende class