Removed all DTO where possible
[pithos] / src / gr / ebs / gss / server / domain / FileBody.java
1 /*\r
2  * Copyright 2007, 2008, 2009 Electronic Business Systems Ltd.\r
3  *\r
4  * This file is part of GSS.\r
5  *\r
6  * GSS is free software: you can redistribute it and/or modify\r
7  * it under the terms of the GNU General Public License as published by\r
8  * the Free Software Foundation, either version 3 of the License, or\r
9  * (at your option) any later version.\r
10  *\r
11  * GSS is distributed in the hope that it will be useful,\r
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14  * GNU General Public License for more details.\r
15  *\r
16  * You should have received a copy of the GNU General Public License\r
17  * along with GSS.  If not, see <http://www.gnu.org/licenses/>.\r
18  */\r
19 package gr.ebs.gss.server.domain;\r
20 \r
21 import gr.ebs.gss.server.domain.dto.FileBodyDTO;\r
22 \r
23 import java.io.Serializable;\r
24 import java.io.UnsupportedEncodingException;\r
25 import java.net.URLEncoder;\r
26 \r
27 import javax.persistence.Embedded;\r
28 import javax.persistence.Entity;\r
29 import javax.persistence.GeneratedValue;\r
30 import javax.persistence.Id;\r
31 import javax.persistence.ManyToOne;\r
32 import javax.persistence.Version;\r
33 \r
34 import org.apache.commons.logging.Log;\r
35 import org.apache.commons.logging.LogFactory;\r
36 import org.hibernate.annotations.Cache;\r
37 import org.hibernate.annotations.CacheConcurrencyStrategy;\r
38 \r
39 /**\r
40  * The mutable part of the structure of a file on the GSS service.\r
41  */\r
42 @Entity\r
43 @Cache(usage=CacheConcurrencyStrategy.READ_WRITE)\r
44 public final class FileBody  implements Serializable{\r
45         /**\r
46          * The logger.\r
47          */\r
48         private static Log logger = LogFactory.getLog(FileBody.class);\r
49 \r
50         /**\r
51          * The persistence ID of the object.\r
52          */\r
53         @Id\r
54         @GeneratedValue\r
55         private Long id;\r
56 \r
57         /**\r
58          * Version field for optimistic locking. Renamed to avoid conflict with file\r
59          * body version.\r
60          */\r
61         @SuppressWarnings("unused")\r
62         @Version\r
63         private int dbVersion;\r
64 \r
65         /**\r
66          * The audit information.\r
67          */\r
68         @Embedded\r
69         private AuditInfo auditInfo;\r
70 \r
71         /**\r
72          * The version of the file, not the JPA version field!\r
73          */\r
74         private int version;\r
75 \r
76         /**\r
77          * The header of the file.\r
78          */\r
79         @ManyToOne\r
80         private FileHeader header;\r
81 \r
82         /**\r
83          * The MIME type of this file.\r
84          */\r
85         private String mimeType;\r
86 \r
87         /**\r
88          * The original filename (with which file was uploaded)\r
89          */\r
90         private String originalFilename;\r
91 \r
92         /**\r
93          * The full file path (path+filename) under which file is currently stored\r
94          * in local file system\r
95          */\r
96         private String storedFilePath;\r
97 \r
98         /**\r
99          * The file size in bytes\r
100          */\r
101         private long fileSize;\r
102 \r
103         \r
104         public Long getId() {\r
105                 return id;\r
106         }\r
107 \r
108         /**\r
109          * Returns the version\r
110          *\r
111          * @return int\r
112          */\r
113         public int getVersion() {\r
114                 return version;\r
115         }\r
116 \r
117         /**\r
118          * Sets a new version\r
119          *\r
120          * @param newVersion\r
121          */\r
122         public void setVersion(final int newVersion) {\r
123                 version = newVersion;\r
124         }\r
125 \r
126         /**\r
127          * Retrieve the file header.\r
128          *\r
129          * @return the file header\r
130          */\r
131         public FileHeader getHeader() {\r
132                 return header;\r
133         }\r
134 \r
135         /**\r
136          * Modify the file header.\r
137          *\r
138          * @param newHeader the new header\r
139          */\r
140         public void setHeader(final FileHeader newHeader) {\r
141                 header = newHeader;\r
142         }\r
143 \r
144         \r
145         public AuditInfo getAuditInfo() {\r
146                 return auditInfo;\r
147         }\r
148 \r
149         /**\r
150          * Retrieve the MIME type.\r
151          *\r
152          * @return the MIME type\r
153          */\r
154         public String getMimeType() {\r
155                 return mimeType;\r
156         }\r
157 \r
158         /**\r
159          * Modify the MIME type.\r
160          *\r
161          * @param newMimeType the new MIME type\r
162          */\r
163         public void setMimeType(final String newMimeType) {\r
164                 mimeType = newMimeType;\r
165         }\r
166 \r
167         /**\r
168          * Retrieve the original filename.\r
169          *\r
170          * @return the original filename\r
171          */\r
172         public String getOriginalFilename() {\r
173                 return originalFilename;\r
174         }\r
175 \r
176         /**\r
177          * Modify the original filename.\r
178          *\r
179          * @param newOriginalFilename the new original filename\r
180          */\r
181         public void setOriginalFilename(final String newOriginalFilename) {\r
182                 originalFilename = newOriginalFilename;\r
183         }\r
184 \r
185         /**\r
186          * Retrieve the stored file path.\r
187          *\r
188          * @return the file path\r
189          */\r
190         public String getStoredFilePath() {\r
191                 return storedFilePath;\r
192         }\r
193 \r
194         /**\r
195          * Modify the stored file path.\r
196          *\r
197          * @param newStoredFilePath the new file path\r
198          */\r
199         public void setStoredFilePath(final String newStoredFilePath) {\r
200                 storedFilePath = newStoredFilePath;\r
201         }\r
202 \r
203         /**\r
204          * Retrieve the file size.\r
205          *\r
206          * @return the file size\r
207          */\r
208         public long getFileSize() {\r
209                 return fileSize;\r
210         }\r
211 \r
212         /**\r
213          * Modify the file size.\r
214          *\r
215          * @param newFileSize the new file size\r
216          */\r
217         public void setFileSize(final long newFileSize) {\r
218                 fileSize = newFileSize;\r
219         }\r
220 \r
221         /**\r
222          * Modify the audit info.\r
223          *\r
224          * @param newAuditInfo the new audit info\r
225          */\r
226         public void setAuditInfo(final AuditInfo newAuditInfo) {\r
227                 auditInfo = newAuditInfo;\r
228         }\r
229 \r
230         /**\r
231          * Return the original filename URL-encoded.\r
232          *\r
233          * @return the original filename URL-encoded.\r
234          */\r
235         public String getOriginalFilenameEncoded() {\r
236                 try {\r
237                         return URLEncoder.encode(getOriginalFilename(), "UTF-8");\r
238                 } catch (UnsupportedEncodingException e) {\r
239                         logger.error("", e);\r
240                         return getOriginalFilename();\r
241                 }\r
242         }\r
243 \r
244         public FileBodyDTO getDTO(){\r
245                 FileBodyDTO dto = new FileBodyDTO();\r
246                 dto.setId(id);\r
247                 dto.setVersion(version);\r
248                 dto.setFileHeaderId(getHeader().getId());\r
249                 dto.setFileSize(fileSize);\r
250                 dto.setMimeType(mimeType);\r
251                 dto.setOriginalFilename(originalFilename);\r
252                 dto.setOriginalFilenameEncoded(getOriginalFilenameEncoded());\r
253                 dto.setAuditInfo(auditInfo.getDTO());\r
254                 return dto;\r
255 \r
256         }\r
257 }\r