Statistics
| Branch: | Tag: | Revision:

root / src / org / gss_project / gss / server / domain / FileTag.java @ 1206:292dec4eae08

History | View | Annotate | Download (4.7 kB)

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 org.gss_project.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
import org.hibernate.annotations.Cache;
33
import org.hibernate.annotations.CacheConcurrencyStrategy;
34

    
35
/**
36
 * A file tag. This is basically a many-to-many relationship between User and
37
 * FileHeader, with an extra string which is the tag. For implementation
38
 * details, see Java Persistence With Hibernate, p. 303.
39
 *
40
 * @author droutsis
41
 */
42
@Entity
43
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
44
public class FileTag  implements Serializable{
45

    
46
        /**
47
         * The composite ID of the object.
48
         */
49
        @Embeddable
50
        public static class FileTagId implements Serializable {
51

    
52
                /**
53
                 *
54
                 */
55
                private static final long serialVersionUID = 1L;
56

    
57
                /**
58
                 * The ID of the user.
59
                 */
60
                @Column(name = "userId")
61
                private Long userId;
62

    
63
                /**
64
                 * The ID of the file header.
65
                 */
66
                @Column(name = "fileId")
67
                private Long fileId;
68

    
69
                /**
70
                 * The actual tag.
71
                 */
72
                @Column(name = "tag")
73
                private String tag;
74

    
75
                @Override
76
                public boolean equals(final Object o) {
77
                        if (o != null && o instanceof FileTagId) {
78
                                final FileTagId that = (FileTagId) o;
79
                                return userId.equals(that.userId) && fileId.equals(that.fileId) && tag.equals(that.tag);
80
                        }
81
                        return false;
82
                }
83

    
84
                @Override
85
                public int hashCode() {
86
                        return userId.hashCode() + fileId.hashCode() + tag.hashCode();
87
                }
88
        }
89

    
90
        /**
91
         * Database id. Not to be set by user.
92
         */
93
        @EmbeddedId
94
        private FileTagId id;
95

    
96
        /*
97
         * No version field: immutable object.
98
         */
99

    
100
        /**
101
         * The audit information.
102
         */
103
        @SuppressWarnings("unused")
104
        @Embedded
105
        private AuditInfo auditInfo;
106

    
107
        /**
108
         * The user who added this tag.
109
         */
110
        @ManyToOne
111
        @JoinColumn(name = "userId", nullable = false, insertable=false, updatable=false)
112
        private User user;
113

    
114
        /**
115
         * The file to which this tag belongs.
116
         */
117
        @ManyToOne(optional = true)
118
        @JoinColumn(name = "fileId", nullable = false, insertable=false, updatable=false)
119
        private FileHeader file;
120

    
121
        /**
122
         * The name of the tag.
123
         */
124
        @Column(nullable=false, insertable=false, updatable=false)
125
        private String tag;
126

    
127
        /**
128
         * A default constructor.
129
         */
130
        public FileTag() {
131
                // Empty.
132
        }
133

    
134
        /**
135
         * The proper constructor to call, which guarantees referential integrity.
136
         *
137
         * @param aUser The user who adds the tag
138
         * @param aFile The file to which the tag is added
139
         * @param aTag The tag itself
140
         */
141
        public FileTag(final User aUser, final FileHeader aFile, final String aTag) {
142
                // Set fields
143
                user = aUser;
144
                file = aFile;
145
                tag = aTag;
146

    
147
                // Set identifier values
148
                id = new FileTagId();
149
                id.userId = aUser.getId();
150
                id.fileId = aFile.getId();
151
                id.tag = aTag;
152

    
153
                // Guarantee referential integrity
154
                aUser.getFileTags().add(this);
155
                aFile.getFileTags().add(this);
156

    
157
                final AuditInfo ai = new AuditInfo();
158
                ai.setCreatedBy(aUser);
159
                ai.setModifiedBy(aUser);
160
                final Date now = new Date();
161
                ai.setCreationDate(now);
162
                ai.setModificationDate(now);
163
                auditInfo = ai;
164
        }
165

    
166
        /**
167
         * Retrieve the FileTagId.
168
         *
169
         * @return the ID
170
         */
171
        public FileTagId getId() {
172
                return id;
173
        }
174

    
175
        /**
176
         * Retrieve the user.
177
         *
178
         * @return the user
179
         */
180
        public User getUser() {
181
                return user;
182
        }
183

    
184
        /**
185
         * Retrieve the file header.
186
         *
187
         * @return the fileheader
188
         */
189
        public FileHeader getFile() {
190
                return file;
191
        }
192

    
193
        /**
194
         * Retrieve the actual tag content.
195
         *
196
         * @return the tag
197
         */
198
        public String getTag() {
199
                return tag;
200
        }
201

    
202
        /**
203
         * Modify the audit info.
204
         *
205
         * @param newAuditInfo the new audit info
206
         */
207
        public void setAuditInfo(final AuditInfo newAuditInfo) {
208
                auditInfo = newAuditInfo;
209
        }
210

    
211

    
212
        /**
213
         * Modify the user.
214
         *
215
         * @param newUser the user to set
216
         */
217
        public void setUser(User newUser) {
218
                user = newUser;
219
        }
220

    
221

    
222
        /**
223
         * Modify the file.
224
         *
225
         * @param newFile the file to set
226
         */
227
        public void setFile(FileHeader newFile) {
228
                file = newFile;
229
        }
230
}