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.Version;
\r
44 * The immutable part of the structure of a file on the GSS service.
\r
47 public final class FileHeader implements Serializable{
\r
50 * The persistence ID of the object.
\r
57 * Version field for optimistic locking.
\r
59 @SuppressWarnings("unused")
\r
61 private int version;
\r
64 * The audit information.
\r
67 private AuditInfo auditInfo;
\r
70 * The icon filename.
\r
72 private String icon;
\r
77 private String name;
\r
80 * The parent folder of this file.
\r
82 @ManyToOne(optional=false)
\r
83 @JoinColumn(nullable=false)
\r
84 private Folder folder;
\r
87 * Is this a versioned file?
\r
89 private boolean versioned = false;
\r
91 * Is this file temporarily deleted?
\r
92 * XXX: the columnDefinition is postgres specific, if deployment database is changed this shall be changed too
\r
94 @Column(columnDefinition=" boolean DEFAULT false")
\r
95 private boolean deleted=false;
\r
98 * Can this file be read by anyone?
\r
99 * XXX: the columnDefinition is postgres specific, if deployment database is changed this shall be changed too
\r
101 @Column(columnDefinition=" boolean DEFAULT false")
\r
102 private boolean readForAll=false;
\r
105 * The owner of this file.
\r
107 @ManyToOne(optional=false)
\r
108 @JoinColumn(nullable=false)
\r
109 private User owner;
\r
112 * The bodies of this file. (A single one if not versioned.) A List so we
\r
115 @OneToMany(cascade = CascadeType.ALL, mappedBy = "header")
\r
116 @OrderBy("version")
\r
117 private List<FileBody> bodies;
\r
120 * The current (most recent) body of this file. The single one if not
\r
124 private FileBody currentBody;
\r
127 * The list of all tags this file has specified from all Users.
\r
129 @OneToMany(cascade = CascadeType.ALL, mappedBy = "file")
\r
131 private List<FileTag> fileTags = new ArrayList<FileTag>();
\r
134 * Set of Permission objects: The permissions (User and Group) for this
\r
137 @OneToMany(cascade = CascadeType.ALL)
\r
138 private Set<Permission> permissions = new HashSet<Permission>();
\r
145 public Long getId() {
\r
150 * Retrieve the icon.
\r
154 public String getIcon() {
\r
161 * @param newIcon the new icon
\r
163 public void setIcon(final String newIcon) {
\r
168 * Retrieve the name.
\r
172 public String getName() {
\r
179 * @param newName the new name
\r
181 public void setName(final String newName) {
\r
186 * Retrieve the folder.
\r
188 * @return the folder object
\r
190 public Folder getFolder() {
\r
195 * Modify the folder.
\r
197 * @param newFolder the new folder
\r
199 public void setFolder(final Folder newFolder) {
\r
200 folder = newFolder;
\r
204 * Determine whether this file is versioned or not.
\r
206 * @return true if this file is versioned
\r
208 public boolean isVersioned() {
\r
213 * Modify the versioning status of this file.
\r
215 * @param newStatus the new versioning status
\r
217 public void setVersioned(final boolean newStatus) {
\r
218 versioned = newStatus;
\r
223 * Is this file deleted or not?.
\r
225 * @return the deleted
\r
227 public boolean isDeleted() {
\r
233 * Set whether this file is deleted .
\r
235 * @param newDeleted the deletedFlag to set
\r
237 public void setDeleted(boolean newDeleted) {
\r
238 deleted = newDeleted;
\r
242 * Retrieve the owner.
\r
244 * @return the owner
\r
246 public User getOwner() {
\r
253 * @param newOwner the new owner
\r
255 public void setOwner(final User newOwner) {
\r
260 * Retrieve the list of bodies.
\r
262 * @return the list of bodies
\r
264 public List<FileBody> getBodies() {
\r
269 * Replace the list of bodies.
\r
271 * @param newBodies the new list of bodies
\r
273 public void setBodies(final List<FileBody> newBodies) {
\r
274 bodies = newBodies;
\r
278 * Retrieve the current body.
\r
280 * @return the current body
\r
282 public FileBody getCurrentBody() {
\r
283 return currentBody;
\r
287 * Set another body as the current one.
\r
289 * @param newCurrentBody the new current body
\r
291 public void setCurrentBody(final FileBody newCurrentBody) {
\r
292 currentBody = newCurrentBody;
\r
296 * Retrieve the audit info.
\r
298 * @return the audit info object
\r
300 public AuditInfo getAuditInfo() {
\r
305 * Modify the audit info.
\r
307 * @param newAuditInfo the new audit info
\r
309 public void setAuditInfo(final AuditInfo newAuditInfo) {
\r
310 auditInfo = newAuditInfo;
\r
314 * Retrieve the file tags.
\r
316 * @return the list of file tags
\r
318 public List<FileTag> getFileTags() {
\r
323 * Replace the list of file tags.
\r
325 * @param newFileTags the new file tags list
\r
327 public void setFileTags(final List<FileTag> newFileTags) {
\r
328 fileTags = newFileTags;
\r
332 * Retrieve the set of permissions.
\r
334 * @return the permission set
\r
336 public Set<Permission> getPermissions() {
\r
337 return permissions;
\r
341 * Replace the permission set.
\r
343 * @param newPermissions the new permission set
\r
345 public void setPermissions(final Set<Permission> newPermissions) {
\r
346 permissions = newPermissions;
\r
350 * Add a body to list of bodies.
\r
352 * @param body InfoItemBody The body to add.
\r
354 public void addBody(final FileBody body) {
\r
356 throw new IllegalArgumentException("Can't add a null FileBody.");
\r
357 // Remove from old header
\r
358 if (body.getHeader() != null)
\r
359 throw new IllegalArgumentException("Trying to add a FileBody that already belongs to a FileHeader.");
\r
361 // Set parent in child
\r
362 body.setHeader(this);
\r
364 // Update version number
\r
365 if (currentBody == null)
\r
366 body.setVersion(1);
\r
368 body.setVersion(currentBody.getVersion() + 1);
\r
370 currentBody = body;
\r
375 * Retrieve the readForAll.
\r
377 * @return the readForAll
\r
379 public boolean isReadForAll() {
\r
385 * Modify the readForAll.
\r
387 * @param readForAll the readForAll to set
\r
389 public void setReadForAll(boolean readForAll) {
\r
390 this.readForAll = readForAll;
\r
394 * Returns current body version number formatted for display. Returns "-"
\r
395 * for non-versioned file.
\r
397 * @return the current version
\r
399 public String getCurrentVersionString() {
\r
401 return String.valueOf(currentBody.getVersion());
\r
406 * Adds a permission to this FileHeader.
\r
408 * @param permission Permission to add
\r
409 * @throws IllegalArgumentException if permission is null
\r
411 public void addPermission(final Permission permission) {
\r
412 if (permission == null)
\r
413 throw new IllegalArgumentException("Can't add a null Permission.");
\r
414 getPermissions().add(permission);
\r
418 * Constructs and returns a DTO for this instance for use by remote clients
\r
420 * @return FileHeaderDTO
\r
422 public FileHeaderDTO getDTO() {
\r
423 final FileHeaderDTO f = new FileHeaderDTO();
\r
426 f.setPath(getPath());
\r
427 f.setFolder(folder.getDTO());
\r
428 f.setVersioned(versioned);
\r
429 f.setVersion(currentBody.getVersion());
\r
430 f.setOwner(owner.getDTO());
\r
431 f.setFileSize(currentBody.getFileSize());
\r
432 f.setOriginalFilename(currentBody.getOriginalFilename());
\r
433 f.setOriginalFilenameEncoded(currentBody.getOriginalFilenameEncoded());
\r
434 f.setMimeType(currentBody.getMimeType());
\r
435 f.setDeleted(deleted);
\r
436 f.setReadForAll(readForAll);
\r
437 List<String> tags = new ArrayList<String>();
\r
438 for (FileTag tag : fileTags)
\r
439 tags.add(tag.getTag());
\r
441 f.setAuditInfo(auditInfo.getDTO());
\r
446 * Checks if the specified user has permission to delete this file, by
\r
447 * checking if the user has write permission to this object.
\r
449 * @param user the specified User
\r
450 * @return true if the user has permission to delete the file, false
\r
453 public boolean hasDeletePermission(final User user) {
\r
454 if (hasWritePermission(user))
\r
460 * Checks if the specified user has permission to modify this file.
\r
462 * @param user the specified User
\r
463 * @return true if the user has permission to modify the file, false
\r
466 public boolean hasWritePermission(final User user) {
\r
467 for (final Permission p : permissions)
\r
468 if (p.getUser() != null) {
\r
469 if (p.getUser().equals(user) && p.getWrite())
\r
471 } else if (p.getGroup().contains(user) && p.getWrite())
\r
477 * Checks if the specified user has permission to read this file.
\r
479 * @param user the specified User
\r
480 * @return true if the user has permission to read the file, false
\r
483 public boolean hasReadPermission(final User user) {
\r
486 for (final Permission p : permissions)
\r
487 if (p.getUser() != null) {
\r
488 if (p.getUser().equals(user) && p.getRead())
\r
490 } else if (p.getGroup().contains(user) && p.getRead())
\r
496 * Checks if the specified user has permission to modify the ACL of this file.
\r
498 * @param user the specified User
\r
499 * @return true if the user has permission to modify the ACL of the file, false
\r
502 public boolean hasModifyACLPermission(final User user) {
\r
503 for (final Permission p : permissions)
\r
504 if (p.getUser() != null) {
\r
505 if (p.getUser().equals(user) && p.getModifyACL())
\r
507 } else if (p.getGroup().contains(user) && p.getModifyACL())
\r
513 * Retrieve the full path of the file, URL-encoded in the form:
\r
514 * /parent1/parent2/parent3/name
\r
516 * @return the full path from the root of the files namespace
\r
518 public String getPath() {
\r
520 return folder.getPath() + URLEncoder.encode(name, "UTF-8");
\r
521 } catch (UnsupportedEncodingException e) {
\r
522 throw new RuntimeException(e);
\r