View Javadoc

1   /**
2    * 
3    */
4   package de.tivsource.page.common.css;
5   
6   import java.io.File;
7   import java.util.Date;
8   import java.util.SortedSet;
9   import java.util.UUID;
10  
11  import javax.persistence.CascadeType;
12  import javax.persistence.Column;
13  import javax.persistence.Entity;
14  import javax.persistence.FetchType;
15  import javax.persistence.Id;
16  import javax.persistence.JoinColumn;
17  import javax.persistence.JoinTable;
18  import javax.persistence.ManyToMany;
19  import javax.persistence.Table;
20  import javax.persistence.Temporal;
21  import javax.persistence.Transient;
22  import javax.persistence.Version;
23  
24  import org.apache.logging.log4j.LogManager;
25  import org.apache.logging.log4j.Logger;
26  import org.hibernate.annotations.SortNatural;
27  import org.hibernate.envers.Audited;
28  import org.hibernate.search.annotations.DocumentId;
29  
30  import de.tivsource.page.common.file.FileActions;
31  
32  /**
33   * @author Marc Michele
34   *
35   */
36  @Audited
37  @Entity
38  @Table(name = "CSSFile")
39  public class CSSFile implements Comparable<CSSFile> {
40  
41      /**
42       * Statischer Logger der Klasse CSSFile für die Ausgabe mit log4j.
43       */
44      private static final Logger logger = LogManager.getLogger(CSSFile.class);
45  
46      /**
47       * UUID der Klasse CSSFile, diese ID ist einmalig über alle Objekte hinweg
48       * und sollte der bevorzugte weg sein auf bestimmtes Objekte zuzugreifen.
49       */
50      @Id
51      @DocumentId
52      @Column(name = "uuid", unique = true, length = 42)
53      private String uuid;
54  
55      private String name;
56  
57      private String description;
58  
59      private Integer priority;
60  
61      private String path;
62  
63      /**
64       * Datei, die aus einem Formular hochgeladen wurde, die Datei ist temporär
65       * vorhanden und wird nur zum erstellen benötigt.
66       */
67      @Transient
68      private File uploadFile;
69  
70      /**
71       * Gruppen zu dem die CSS Datei gehört.
72       */
73      @ManyToMany(targetEntity = CSSGroup.class, cascade = { CascadeType.PERSIST }, fetch = FetchType.EAGER)
74      @JoinTable(
75              name = "CSSFile_CSSGroup", 
76              joinColumns = @JoinColumn(name = "cssFile_uuid"), 
77              inverseJoinColumns = @JoinColumn(name = "cssGroup_uuid")
78              )
79      @SortNatural
80      private SortedSet<CSSGroup> groups;
81  
82      @Temporal(javax.persistence.TemporalType.TIMESTAMP)
83      private Date created;
84  
85      @Temporal(javax.persistence.TemporalType.TIMESTAMP)
86      private Date modified;
87  
88      private String modifiedBy;
89  
90      private String modifiedAddress;
91  
92      /**
93       * Versionsnummer für die Locking Strategie: optimistic-locking.
94       */
95      @Version
96      private Integer version;
97  
98      /**
99       * @return the uuid
100      */
101     public String getUuid() {
102         return uuid;
103     }
104 
105     /**
106      * @param uuid
107      *            the uuid to set
108      */
109     public void setUuid(String uuid) {
110         this.uuid = uuid;
111     }
112 
113     /**
114      * @return the name
115      */
116     public String getName() {
117         return name;
118     }
119 
120     /**
121      * @param name
122      *            the name to set
123      */
124     public void setName(String name) {
125         this.name = name;
126     }
127 
128     /**
129      * @return the description
130      */
131     public String getDescription() {
132         return description;
133     }
134 
135     /**
136      * @param description
137      *            the description to set
138      */
139     public void setDescription(String description) {
140         this.description = description;
141     }
142 
143     /**
144      * @return the priority
145      */
146     public Integer getPriority() {
147         return priority;
148     }
149 
150     /**
151      * @param priority
152      *            the priority to set
153      */
154     public void setPriority(Integer priority) {
155         this.priority = priority;
156     }
157 
158     /**
159      * @return the path
160      */
161     public String getPath() {
162         return path;
163     }
164 
165     /**
166      * @param path
167      *            the path to set
168      */
169     public void setPath(String path) {
170         this.path = path;
171     }
172 
173     /**
174      * @return the uploadFile
175      */
176     public File getUploadFile() {
177         return uploadFile;
178     }
179 
180     /**
181      * @param uploadFile
182      *            the uploadFile to set
183      */
184     public void setUploadFile(File uploadFile) {
185         this.uploadFile = uploadFile;
186     }
187 
188     /**
189      * @return the groups
190      */
191     public SortedSet<CSSGroup> getGroups() {
192         return groups;
193     }
194 
195     /**
196      * @param groups
197      *            the groups to set
198      */
199     public void setGroups(SortedSet<CSSGroup> groups) {
200         this.groups = groups;
201     }
202 
203     /**
204      * @return the created
205      */
206     public Date getCreated() {
207         return created;
208     }
209 
210     /**
211      * @param created
212      *            the created to set
213      */
214     public void setCreated(Date created) {
215         this.created = created;
216     }
217 
218     /**
219      * @return the modified
220      */
221     public Date getModified() {
222         return modified;
223     }
224 
225     /**
226      * @param modified
227      *            the modified to set
228      */
229     public void setModified(Date modified) {
230         this.modified = modified;
231     }
232 
233     /**
234      * @return the modifiedBy
235      */
236     public String getModifiedBy() {
237         return modifiedBy;
238     }
239 
240     /**
241      * @param modifiedBy
242      *            the modifiedBy to set
243      */
244     public void setModifiedBy(String modifiedBy) {
245         this.modifiedBy = modifiedBy;
246     }
247 
248     /**
249      * @return the modifiedAddress
250      */
251     public String getModifiedAddress() {
252         return modifiedAddress;
253     }
254 
255     /**
256      * @param modifiedAddress
257      *            the modifiedAddress to set
258      */
259     public void setModifiedAddress(String modifiedAddress) {
260         this.modifiedAddress = modifiedAddress;
261     }
262 
263     /**
264      * @return the version
265      */
266     public Integer getVersion() {
267         return version;
268     }
269 
270     /**
271      * @param version
272      *            the version to set
273      */
274     public void setVersion(Integer version) {
275         this.version = version;
276     }
277 
278     public void generate() {
279         String filePath = "/srv/tiv-page/cssfile/";
280         // Generiere UUID für den Dateinamen
281         String cssSaveName = UUID.randomUUID().toString();
282         try {
283             // Datei die erstellt werden soll
284             File fileToCreate = new File(filePath, cssSaveName + ".css");
285             logger.debug("Absoluter Pfad der neuen CSS-Datei : "
286                     + fileToCreate.getAbsolutePath());
287 
288             // Wenn die Datei noch nicht existiert wird Sie erstellt.
289             if (!fileToCreate.exists()) {
290                 FileActions.savePictureFile(this.getUploadFile(), fileToCreate);
291             }// Ende if
292         } // Ende try
293         catch (Exception e) {
294             logger.error(e.getMessage());
295         }// Ende Catch
296         setPath(filePath + cssSaveName + ".css");
297     }// Ende generate()
298 
299     @Override
300     public int compareTo(CSSFile o) {
301         if (o.priority < this.priority) {
302             return 1;
303         } else if (o.priority > this.priority) {
304             return -1;
305         } else {
306             return o.uuid.compareTo(this.uuid);
307         }
308     }// Ende compareTo(CSSFile o)
309 
310 }// Ende class