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.UUID;
10  
11  import javax.persistence.Basic;
12  import javax.persistence.MappedSuperclass;
13  import javax.persistence.Transient;
14  
15  import org.apache.logging.log4j.LogManager;
16  import org.apache.logging.log4j.Logger;
17  import org.apache.tools.ant.util.FileUtils;
18  import org.hibernate.annotations.Type;
19  
20  import de.tivsource.page.common.file.FileActions;
21  
22  /**
23   * Die Klasse Image dient dazu Bilder zu verwalten
24   *
25   * @author Marc Michele
26   *
27   */
28  @MappedSuperclass
29  public class ImageSquare implements Comparable<ImageSquare>, Serializable {
30  
31      /**
32       * 
33       */
34      private static final long serialVersionUID = 7403879687974877269L;
35  
36      /**
37       * Statischer Logger der Klasse Image für die Ausgabe mit log4j. Es gibt nur
38       * Ausgaben mit dem Log-Level "TRACE".
39       */
40      private static final Logger logger = LogManager.getLogger(ImageSquare.class);
41  
42      /**
43       * Lokaler Pfad der Orginal-Datei.
44       */
45      private String orginal;
46      
47      /**
48       * Lokaler Pfad der großen Abbildung mit 350 x 350 Pixeln.
49       */
50      private String picture;
51  
52      /**
53       * Lokaler Pfad der Abbildung für Übersichtsseiten mit 148 x 148 Pixeln.
54       */
55      private String thumbnail;
56  
57      /**
58       * Lokaler Pfad der kleinen Abbildung mit 50 x 50 Pixeln.
59       */
60      private String micro;
61  
62      /**
63       * Boolean ob das Bild ein Default Bild ist (Ja/Nein).
64       */
65      @Basic
66      @Type(type = "yes_no")
67      private Boolean standard;
68  
69      /**
70       * Datei, die aus einem Formular hochgeladen wird.
71       */
72      @Transient
73      private File uploadFile;
74  
75      public ImageSquare() {
76          super();
77      }
78  
79      public static Logger getLogger() {
80          return logger;
81      }
82  
83      /**
84       * @return the orginal
85       */
86      public String getOrginal() {
87          return orginal;
88      }
89  
90      /**
91       * @param orginal the orginal to set
92       */
93      public void setOrginal(String orginal) {
94          this.orginal = orginal;
95      }
96  
97      public String getPicture() {
98          return picture;
99      }
100 
101     public void setPicture(String picture) {
102         this.picture = picture;
103     }
104 
105     public String getThumbnail() {
106         return thumbnail;
107     }
108 
109     public void setThumbnail(String thumbnail) {
110         this.thumbnail = thumbnail;
111     }
112 
113     public String getMicro() {
114         return micro;
115     }
116 
117     public void setMicro(String micro) {
118         this.micro = micro;
119     }
120 
121     public Boolean isStandard() {
122         return standard;
123     }
124 
125     public void setStandard(Boolean standard) {
126         this.standard = standard;
127     }
128 
129     public FileInputStream getPictureFileInputStream() {
130         try {
131             return new FileInputStream(new File(this.picture));
132         } catch (Exception e) {
133             e.printStackTrace();
134             return null;
135         }
136     }
137 
138     public FileInputStream getThumbnailFileInputStream() {
139         try {
140             return new FileInputStream(new File(this.thumbnail));
141         } catch (Exception e) {
142             e.printStackTrace();
143             return null;
144         }
145     }
146 
147     public FileInputStream getMicroFileInputStream() {
148         try {
149             return new FileInputStream(new File(this.micro));
150         } catch (Exception e) {
151             e.printStackTrace();
152             return null;
153         }
154     }
155 
156     /**
157      * @return the uploadFile
158      */
159     public File getUploadFile() {
160         return uploadFile;
161     }
162 
163     /**
164      * @param uploadFile
165      *            the uploadFile to set
166      */
167     public void setUploadFile(File uploadFile) {
168         this.uploadFile = uploadFile;
169     }
170 
171     public void generate() {
172         String filePath = "/srv/www/tomcat-upload/";
173 
174         // Generiere UUID für den Dateinamen
175         String pictureSaveName = UUID.randomUUID().toString();
176 
177         try {
178             // Datei die erstellt werden soll
179             File fileToCreate = new File(filePath, pictureSaveName + ".png");
180             logger.debug("Absoluter Pfad der neuen Picture-Datei : "
181                     + fileToCreate.getAbsolutePath());
182 
183             // Wenn die Datei noch nicht existiert wird Sie erstellt.
184             if (!fileToCreate.exists()) {
185                 FileActions.savePictureFile(this.getUploadFile(), fileToCreate);
186             }// Ende if
187         } // Ende try
188         catch (Exception e) {
189             logger.error(e.getMessage());
190         }// Ende Catch
191 
192         String s = null;
193 
194         String file = filePath + pictureSaveName + ".png";
195         String newFile = filePath + pictureSaveName + ".org.png";
196         String thumbFile = filePath + pictureSaveName + ".thumb.png";
197         String microFile = filePath + pictureSaveName + ".micro.png";
198         try {
199 
200             Process process = Runtime.getRuntime().exec("montage -background none -geometry 350x350 -quality 100 " + file + " "+ newFile);
201             BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
202             BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
203 
204             while ((s = stdInput.readLine()) != null) {
205                 System.out.println(s);
206             }
207 
208             while ((s = stdError.readLine()) != null) {
209                 System.out.println(s);
210             }
211 
212             process = Runtime.getRuntime().exec("montage -background none -geometry 148x148 -quality 100 " + file + " " + thumbFile);
213             stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
214             stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
215 
216             while ((s = stdInput.readLine()) != null) {
217                 System.out.println(s);
218             }
219 
220             while ((s = stdError.readLine()) != null) {
221                 System.out.println(s);
222             }
223 
224             process = Runtime.getRuntime().exec("montage -background none -geometry 50x50 -quality 100 " + file + " " + microFile);
225             stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
226             stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
227 
228             while ((s = stdInput.readLine()) != null) {
229                 System.out.println(s);
230             }
231 
232             while ((s = stdError.readLine()) != null) {
233                 System.out.println(s);
234             }
235 
236         } catch (IOException e) {
237             System.out.println("exception happened - here's what I know: ");
238             e.printStackTrace();
239             System.exit(-1);
240         }
241 
242         this.setOrginal(file);
243         this.setPicture(newFile);
244         this.setThumbnail(thumbFile);
245         this.setMicro(microFile);
246         this.setStandard(true);
247     }
248 
249     public void delete() {
250         File microFile = new File(micro);
251         if (microFile.exists()) {
252             FileUtils.delete(microFile);
253         }// Ende if
254 
255         File thumbnailFile = new File(thumbnail);
256         if (thumbnailFile.exists()) {
257             FileUtils.delete(thumbnailFile);
258         }// Ende if
259 
260         File pictureFile = new File(picture);
261         if (pictureFile.exists()) {
262             FileUtils.delete(pictureFile);
263         }// Ende if
264 
265         File uploadFile = new File(orginal);
266         if (uploadFile.exists()) {
267             FileUtils.delete(uploadFile);
268         }// Ende if
269     }// Ende delete()
270     
271     public int compareTo(ImageSquare image) {
272         logger.trace("compareTo() aufgerufen.");
273         return image.getMicro().compareTo(this.getMicro()) * -1;
274     }
275 
276 }// Ende class