Fixed bug: Uploading a new version of a versioned file with no quota left would leave...
[pithos] / src / gr / ebs / gss / server / domain / FileTag.java
1 /*
2  * Copyright 2007, 2008, 2009 Electronic Business Systems Ltd.
3  *
4  * This file is part of GSS.
5  *
6  * GSS is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * GSS is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 package gr.ebs.gss.server.domain;
20
21 import java.io.Serializable;
22 import java.util.Date;
23
24 import javax.persistence.Column;
25 import javax.persistence.Embeddable;
26 import javax.persistence.Embedded;
27 import javax.persistence.EmbeddedId;
28 import javax.persistence.Entity;
29 import javax.persistence.JoinColumn;
30 import javax.persistence.ManyToOne;
31
32 /**
33  * A file tag. This is basically a many-to-many relationship between User and
34  * FileHeader, with an extra string which is the tag. For implementation
35  * details, see Java Persistence With Hibernate, p. 303.
36  *
37  * @author droutsis
38  */
39 @Entity
40 public class FileTag  implements Serializable{
41
42         /**
43          * The composite ID of the object.
44          */
45         @Embeddable
46         public static class FileTagId implements Serializable {
47
48                 /**
49                  *
50                  */
51                 private static final long serialVersionUID = 1L;
52
53                 /**
54                  * The ID of the user.
55                  */
56                 @Column(name = "userId")
57                 private Long userId;
58
59                 /**
60                  * The ID of the file header.
61                  */
62                 @Column(name = "fileId")
63                 private Long fileId;
64
65                 /**
66                  * The actual tag.
67                  */
68                 @Column(name = "tag")
69                 private String tag;
70
71                 @Override
72                 public boolean equals(final Object o) {
73                         if (o != null && o instanceof FileTagId) {
74                                 final FileTagId that = (FileTagId) o;
75                                 return userId.equals(that.userId) && fileId.equals(that.fileId) && tag.equals(that.tag);
76                         }
77                         return false;
78                 }
79
80                 @Override
81                 public int hashCode() {
82                         return userId.hashCode() + fileId.hashCode() + tag.hashCode();
83                 }
84         }
85
86         /**
87          * Database id. Not to be set by user.
88          */
89         @EmbeddedId
90         private FileTagId id;
91
92         /*
93          * No version field: immutable object.
94          */
95
96         /**
97          * The audit information.
98          */
99         @SuppressWarnings("unused")
100         @Embedded
101         private AuditInfo auditInfo;
102
103         /**
104          * The user who added this tag.
105          */
106         @ManyToOne
107         @JoinColumn(name = "userId", nullable = false, insertable=false, updatable=false)
108         private User user;
109
110         /**
111          * The file to which this tag belongs.
112          */
113         @ManyToOne(optional = true)
114         @JoinColumn(name = "fileId", nullable = false, insertable=false, updatable=false)
115         private FileHeader file;
116
117         /**
118          * The name of the tag.
119          */
120         @Column(nullable=false, insertable=false, updatable=false)
121         private String tag;
122
123         /**
124          * A default constructor.
125          */
126         public FileTag() {
127                 // Empty.
128         }
129
130         /**
131          * The proper constructor to call, which guarantees referential integrity.
132          *
133          * @param aUser The user who adds the tag
134          * @param aFile The file to which the tag is added
135          * @param aTag The tag itself
136          */
137         public FileTag(final User aUser, final FileHeader aFile, final String aTag) {
138                 // Set fields
139                 user = aUser;
140                 file = aFile;
141                 tag = aTag;
142
143                 // Set identifier values
144                 id = new FileTagId();
145                 id.userId = aUser.getId();
146                 id.fileId = aFile.getId();
147                 id.tag = aTag;
148
149                 // Guarantee referential integrity
150                 aUser.getFileTags().add(this);
151                 aFile.getFileTags().add(this);
152
153                 final AuditInfo ai = new AuditInfo();
154                 ai.setCreatedBy(aUser);
155                 ai.setModifiedBy(aUser);
156                 final Date now = new Date();
157                 ai.setCreationDate(now);
158                 ai.setModificationDate(now);
159                 auditInfo = ai;
160         }
161
162         /**
163          * Retrieve the FileTagId.
164          *
165          * @return the ID
166          */
167         public FileTagId getId() {
168                 return id;
169         }
170
171         /**
172          * Retrieve the user.
173          *
174          * @return the user
175          */
176         public User getUser() {
177                 return user;
178         }
179
180         /**
181          * Retrieve the file header.
182          *
183          * @return the fileheader
184          */
185         public FileHeader getFile() {
186                 return file;
187         }
188
189         /**
190          * Retrieve the actual tag content.
191          *
192          * @return the tag
193          */
194         public String getTag() {
195                 return tag;
196         }
197
198         /**
199          * Modify the audit info.
200          *
201          * @param newAuditInfo the new audit info
202          */
203         public void setAuditInfo(final AuditInfo newAuditInfo) {
204                 auditInfo = newAuditInfo;
205         }
206
207
208         /**
209          * Modify the user.
210          *
211          * @param newUser the user to set
212          */
213         public void setUser(User newUser) {
214                 user = newUser;
215         }
216
217
218         /**
219          * Modify the file.
220          *
221          * @param newFile the file to set
222          */
223         public void setFile(FileHeader newFile) {
224                 file = newFile;
225         }
226 }