2 * Copyright 2007, 2008, 2009 Electronic Business Systems Ltd.
\r
4 * This file is part of GSS.
\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
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
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
19 package gr.ebs.gss.server.domain;
\r
21 import gr.ebs.gss.server.domain.dto.FileHeaderDTO;
\r
23 import java.io.Serializable;
\r
24 import java.io.UnsupportedEncodingException;
\r
25 import java.net.URLEncoder;
\r
26 import java.util.ArrayList;
\r
27 import java.util.HashSet;
\r
28 import java.util.List;
\r
29 import java.util.Set;
\r
31 import javax.persistence.CascadeType;
\r
32 import javax.persistence.Column;
\r
33 import javax.persistence.Embedded;
\r
34 import javax.persistence.Entity;
\r
35 import javax.persistence.GeneratedValue;
\r
36 import javax.persistence.Id;
\r
37 import javax.persistence.JoinColumn;
\r
38 import javax.persistence.ManyToOne;
\r
39 import javax.persistence.OneToMany;
\r
40 import javax.persistence.OrderBy;
\r
41 import javax.persistence.Table;
\r
42 import javax.persistence.UniqueConstraint;
\r
43 import javax.persistence.Version;
\r
45 import org.hibernate.annotations.Cache;
\r
46 import org.hibernate.annotations.CacheConcurrencyStrategy;
\r
49 * The immutable part of the structure of a file on the GSS service.
\r
52 @Table(name="fileheader", uniqueConstraints=@UniqueConstraint(columnNames={"folder_id", "name"}))
\r
53 @Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
\r
54 public final class FileHeader implements Serializable{
\r
57 * The persistence ID of the object.
\r
64 * Version field for optimistic locking.
\r
66 @SuppressWarnings("unused")
\r
68 private int version;
\r
71 * The audit information.
\r
74 private AuditInfo auditInfo;
\r
77 * The icon filename.
\r
79 private String icon;
\r
84 @Column(name="name")
\r
85 private String name;
\r
88 * The parent folder of this file.
\r
90 @ManyToOne(optional=false)
\r
91 @JoinColumn(name="folder_id", nullable=false)
\r
92 private Folder folder;
\r
95 * Is this a versioned file?
\r
97 private boolean versioned = false;
\r
99 * Is this file temporarily deleted?
\r
100 * XXX: the columnDefinition is postgres specific, if deployment database is changed this shall be changed too
\r
102 @Column(columnDefinition=" boolean DEFAULT false")
\r
103 private boolean deleted=false;
\r
106 * Can this file be read by anyone?
\r
107 * XXX: the columnDefinition is postgres specific, if deployment database is changed this shall be changed too
\r
109 @Column(columnDefinition=" boolean DEFAULT false")
\r
110 private boolean readForAll=false;
\r
113 * The owner of this file.
\r
115 @ManyToOne(optional=false)
\r
116 @JoinColumn(nullable=false)
\r
117 private User owner;
\r
120 * The bodies of this file. (A single one if not versioned.) A List so we
\r
123 @OneToMany(cascade = CascadeType.ALL, mappedBy = "header")
\r
124 @OrderBy("version")
\r
125 private List<FileBody> bodies = new ArrayList<FileBody>();
\r
128 * The current (most recent) body of this file. The single one if not
\r
132 private FileBody currentBody;
\r
135 * The list of all tags this file has specified from all Users.
\r
137 @OneToMany(cascade = CascadeType.ALL, mappedBy = "file")
\r
139 private List<FileTag> fileTags = new ArrayList<FileTag>();
\r
142 * Set of Permission objects: The permissions (User and Group) for this
\r
145 @OneToMany(cascade = CascadeType.ALL)
\r
146 private Set<Permission> permissions = new HashSet<Permission>();
\r
153 public Long getId() {
\r
158 * Retrieve the icon.
\r
162 public String getIcon() {
\r
169 * @param newIcon the new icon
\r
171 public void setIcon(final String newIcon) {
\r
176 * Retrieve the name.
\r
180 public String getName() {
\r
187 * @param newName the new name
\r
189 public void setName(final String newName) {
\r
194 * Retrieve the folder.
\r
196 * @return the folder object
\r
198 public Folder getFolder() {
\r
203 * Modify the folder.
\r
205 * @param newFolder the new folder
\r
207 public void setFolder(final Folder newFolder) {
\r
208 folder = newFolder;
\r
212 * Determine whether this file is versioned or not.
\r
214 * @return true if this file is versioned
\r
216 public boolean isVersioned() {
\r
221 * Modify the versioning status of this file.
\r
223 * @param newStatus the new versioning status
\r
225 public void setVersioned(final boolean newStatus) {
\r
226 versioned = newStatus;
\r
231 * Is this file deleted or not?.
\r
233 * @return the deleted
\r
235 public boolean isDeleted() {
\r
241 * Set whether this file is deleted .
\r
243 * @param newDeleted the deletedFlag to set
\r
245 public void setDeleted(boolean newDeleted) {
\r
246 deleted = newDeleted;
\r
250 * Retrieve the owner.
\r
252 * @return the owner
\r
254 public User getOwner() {
\r
261 * @param newOwner the new owner
\r
263 public void setOwner(final User newOwner) {
\r
268 * Retrieve the list of bodies.
\r
270 * @return the list of bodies
\r
272 public List<FileBody> getBodies() {
\r
277 * Replace the list of bodies.
\r
279 * @param newBodies the new list of bodies
\r
281 public void setBodies(final List<FileBody> newBodies) {
\r
282 bodies = newBodies;
\r
286 * Retrieve the current body.
\r
288 * @return the current body
\r
290 public FileBody getCurrentBody() {
\r
291 return currentBody;
\r
295 * Set another body as the current one.
\r
297 * @param newCurrentBody the new current body
\r
299 public void setCurrentBody(final FileBody newCurrentBody) {
\r
300 currentBody = newCurrentBody;
\r
304 * Retrieve the audit info.
\r
306 * @return the audit info object
\r
308 public AuditInfo getAuditInfo() {
\r
313 * Modify the audit info.
\r
315 * @param newAuditInfo the new audit info
\r
317 public void setAuditInfo(final AuditInfo newAuditInfo) {
\r
318 auditInfo = newAuditInfo;
\r
322 * Retrieve the file tags.
\r
324 * @return the list of file tags
\r
326 public List<FileTag> getFileTags() {
\r
331 * Replace the list of file tags.
\r
333 * @param newFileTags the new file tags list
\r
335 public void setFileTags(final List<FileTag> newFileTags) {
\r
336 fileTags = newFileTags;
\r
340 * Retrieve the set of permissions.
\r
342 * @return the permission set
\r
344 public Set<Permission> getPermissions() {
\r
345 return permissions;
\r
349 * Replace the permission set.
\r
351 * @param newPermissions the new permission set
\r
353 public void setPermissions(final Set<Permission> newPermissions) {
\r
354 permissions = newPermissions;
\r
358 * Add a body to list of bodies.
\r
360 * @param body InfoItemBody The body to add.
\r
362 public void addBody(final FileBody body) {
\r
364 throw new IllegalArgumentException("Can't add a null FileBody.");
\r
365 // Remove from old header
\r
366 if (body.getHeader() != null)
\r
367 throw new IllegalArgumentException("Trying to add a FileBody that already belongs to a FileHeader.");
\r
369 // Set child in parent
\r
370 getBodies().add(body);
\r
371 // Set parent in child
\r
372 body.setHeader(this);
\r
374 // Update version number
\r
375 if (currentBody == null)
\r
376 body.setVersion(1);
\r
378 body.setVersion(currentBody.getVersion() + 1);
\r
380 currentBody = body;
\r
385 * Retrieve the readForAll.
\r
387 * @return the readForAll
\r
389 public boolean isReadForAll() {
\r
395 * Modify the readForAll.
\r
397 * @param readForAll the readForAll to set
\r
399 public void setReadForAll(boolean readForAll) {
\r
400 this.readForAll = readForAll;
\r
404 * Returns current body version number formatted for display. Returns "-"
\r
405 * for non-versioned file.
\r
407 * @return the current version
\r
409 public String getCurrentVersionString() {
\r
411 return String.valueOf(currentBody.getVersion());
\r
416 * Adds a permission to this FileHeader.
\r
418 * @param permission Permission to add
\r
419 * @throws IllegalArgumentException if permission is null
\r
421 public void addPermission(final Permission permission) {
\r
422 if (permission == null)
\r
423 throw new IllegalArgumentException("Can't add a null Permission.");
\r
424 getPermissions().add(permission);
\r
428 * Constructs and returns a DTO for this instance for use by remote clients
\r
430 * @return FileHeaderDTO
\r
432 public FileHeaderDTO getDTO() {
\r
433 final FileHeaderDTO f = new FileHeaderDTO();
\r
436 f.setPath(getPath());
\r
437 f.setFolder(folder.getDTO());
\r
438 f.setVersioned(versioned);
\r
439 f.setVersion(currentBody.getVersion());
\r
440 f.setOwner(owner.getDTO());
\r
441 f.setFileSize(currentBody.getFileSize());
\r
442 f.setOriginalFilename(currentBody.getOriginalFilename());
\r
443 f.setOriginalFilenameEncoded(currentBody.getOriginalFilenameEncoded());
\r
444 f.setMimeType(currentBody.getMimeType());
\r
445 f.setDeleted(deleted);
\r
446 f.setReadForAll(readForAll);
\r
447 List<String> tags = new ArrayList<String>();
\r
448 for (FileTag tag : fileTags)
\r
449 tags.add(tag.getTag());
\r
451 f.setAuditInfo(auditInfo.getDTO());
\r
456 * Checks if the specified user has permission to delete this file, by
\r
457 * checking if the user has write permission to this object.
\r
459 * @param user the specified User
\r
460 * @return true if the user has permission to delete the file, false
\r
463 public boolean hasDeletePermission(final User user) {
\r
464 if (hasWritePermission(user))
\r
470 * Checks if the specified user has permission to modify this file.
\r
472 * @param user the specified User
\r
473 * @return true if the user has permission to modify the file, false
\r
476 public boolean hasWritePermission(final User user) {
\r
477 for (final Permission p : permissions)
\r
478 if (p.getUser() != null) {
\r
479 if (p.getUser().equals(user) && p.getWrite())
\r
481 } else if (p.getGroup().contains(user) && p.getWrite())
\r
487 * Checks if the specified user has permission to read this file.
\r
489 * @param user the specified User
\r
490 * @return true if the user has permission to read the file, false
\r
493 public boolean hasReadPermission(final User user) {
\r
496 for (final Permission p : permissions)
\r
497 if (p.getUser() != null) {
\r
498 if (p.getUser().equals(user) && p.getRead())
\r
500 } else if (p.getGroup().contains(user) && p.getRead())
\r
506 * Checks if the specified user has permission to modify the ACL of this file.
\r
508 * @param user the specified User
\r
509 * @return true if the user has permission to modify the ACL of the file, false
\r
512 public boolean hasModifyACLPermission(final User user) {
\r
513 for (final Permission p : permissions)
\r
514 if (p.getUser() != null) {
\r
515 if (p.getUser().equals(user) && p.getModifyACL())
\r
517 } else if (p.getGroup().contains(user) && p.getModifyACL())
\r
523 * Retrieve the full path of the file, URL-encoded in the form:
\r
524 * /parent1/parent2/parent3/name
\r
526 * @return the full path from the root of the files namespace
\r
528 public String getPath() {
\r
530 return folder.getPath() + URLEncoder.encode(name, "UTF-8");
\r
531 } catch (UnsupportedEncodingException e) {
\r
532 throw new RuntimeException(e);
\r
537 * Return the total space occupied by this file in storage
\r
538 * (i.e. the total size of all bodies)
\r
541 public long getTotalSize() {
\r
543 for (FileBody body: getBodies())
\r
544 total += body.getFileSize();
\r