View Javadoc
1   /*
2    * Copyright (C) 2024 B3Partners B.V.
3    *
4    * SPDX-License-Identifier: MIT
5    */
6   
7   package org.tailormap.api.persistence;
8   
9   import jakarta.persistence.Basic;
10  import jakarta.persistence.Entity;
11  import jakarta.persistence.EntityListeners;
12  import jakarta.persistence.FetchType;
13  import jakarta.persistence.GeneratedValue;
14  import jakarta.persistence.GenerationType;
15  import jakarta.persistence.Id;
16  import jakarta.persistence.PrePersist;
17  import jakarta.persistence.PreUpdate;
18  import jakarta.validation.constraints.NotNull;
19  import java.time.OffsetDateTime;
20  import java.time.ZoneId;
21  import java.util.UUID;
22  import org.apache.commons.codec.digest.DigestUtils;
23  import org.tailormap.api.persistence.listener.EntityEventPublisher;
24  
25  @Entity
26  @EntityListeners(EntityEventPublisher.class)
27  public class Upload {
28    public static final String CATEGORY_APP_LOGO = "app-logo";
29    public static final String CATEGORY_LEGEND = "legend";
30    public static final String CATEGORY_PORTAL_IMAGE = "portal-image";
31    public static final String CATEGORY_DRAWING_STYLE = "drawing-style";
32    public static final String CATEGORY_DRAWING_STYLE_IMAGE = "drawing-style-image";
33  
34    @Id
35    @GeneratedValue(strategy = GenerationType.UUID)
36    private UUID id;
37  
38    private String category;
39  
40    private String filename;
41  
42    private String mimeType;
43  
44    private Integer imageWidth;
45  
46    private Integer imageHeight;
47  
48    private Boolean hiDpiImage;
49  
50    @NotNull private OffsetDateTime lastModified = OffsetDateTime.now(ZoneId.systemDefault());
51  
52    @Basic(fetch = FetchType.LAZY)
53    private byte[] content;
54  
55    private String hash;
56  
57    // <editor-fold desc="getters and setters">
58    public int getContentLength() {
59      return getContent() == null ? 0 : content.length;
60    }
61  
62    public UUID getId() {
63      return id;
64    }
65  
66    public Upload setId(UUID id) {
67      this.id = id;
68      return this;
69    }
70  
71    public String getCategory() {
72      return category;
73    }
74  
75    public Upload setCategory(String category) {
76      this.category = category;
77      return this;
78    }
79  
80    public String getFilename() {
81      return filename;
82    }
83  
84    public Upload setFilename(String filename) {
85      this.filename = filename;
86      return this;
87    }
88  
89    public String getMimeType() {
90      return mimeType;
91    }
92  
93    public Upload setMimeType(String mimeType) {
94      this.mimeType = mimeType;
95      return this;
96    }
97  
98    public Integer getImageWidth() {
99      return imageWidth;
100   }
101 
102   public Upload setImageWidth(Integer imageWidth) {
103     this.imageWidth = imageWidth;
104     return this;
105   }
106 
107   public Integer getImageHeight() {
108     return imageHeight;
109   }
110 
111   public Upload setImageHeight(Integer imageHeight) {
112     this.imageHeight = imageHeight;
113     return this;
114   }
115 
116   public Boolean getHiDpiImage() {
117     return hiDpiImage;
118   }
119 
120   public Upload setHiDpiImage(Boolean hiDpiImage) {
121     this.hiDpiImage = hiDpiImage;
122     return this;
123   }
124 
125   public OffsetDateTime getLastModified() {
126     return lastModified;
127   }
128 
129   public Upload setLastModified(OffsetDateTime lastModified) {
130     this.lastModified = lastModified;
131     return this;
132   }
133 
134   public byte[] getContent() {
135     return content;
136   }
137 
138   public Upload setContent(byte[] content) {
139     this.content = content;
140     return this;
141   }
142 
143   public String getHash() {
144     return hash;
145   }
146 
147   public Upload setHash(String hash) {
148     this.hash = hash;
149     return this;
150   }
151   // </editor-fold>
152 
153   @PrePersist
154   @PreUpdate
155   public void computeHash() {
156     if (content != null) {
157       this.hash = DigestUtils.sha1Hex(content);
158     } else {
159       this.hash = null;
160     }
161   }
162 }