com.gantzgulch.sharing.domain.SharedFile.java Source code

Java tutorial

Introduction

Here is the source code for com.gantzgulch.sharing.domain.SharedFile.java

Source

/*
 * Copyright 2011 GantzGulch, Inc.
 * 
 * This file is part of GantzFileSharing.
 * 
 * GantzFileSharing is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * GantzFileSharing is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with GantzFileSharing.  If not, see <http://www.gnu.org/licenses/>. 
 */
package com.gantzgulch.sharing.domain;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

import com.gantzgulch.sharing.util.Compare;
import com.google.common.collect.Iterables;

@Entity
@Table(name = "shared_file")
public class SharedFile extends AbstractDomain {

    @Column(name = "filename")
    private String filename;

    @Column(name = "on_disk_filename")
    private String onDiskFilename;

    @Column(name = "description")
    private String description;

    @Column(name = "size")
    private long size;

    @Column(name = "upload_date")
    private Date uploadDate;

    @Column(name = "touch_date")
    private Date touchDate;

    @OneToMany(mappedBy = "sharedFile", cascade = { CascadeType.ALL })
    private Set<SharedFileLog> sharedFileLogs = new HashSet<SharedFileLog>();

    @OneToMany(mappedBy = "sharedFile", cascade = { CascadeType.REFRESH })
    private Set<SharedFileComment> comments;

    public SharedFile() {
    }

    public SharedFile(String filename, String description, long size) {
        this.filename = filename;
        this.description = description;
        this.size = size;
        this.uploadDate = new Date();
        this.touchDate = this.uploadDate;
    }

    public String getFilename() {
        return filename;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }

    public String getOnDiskFilename() {
        return onDiskFilename;
    }

    public void setOnDiskFilename(String onDiskFilename) {
        this.onDiskFilename = onDiskFilename;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public long getSize() {
        return size;
    }

    public void setSize(long size) {
        this.size = size;
    }

    public Date getUploadDate() {
        return uploadDate;
    }

    public void setUploadDate(Date uploadDate) {
        this.uploadDate = uploadDate;
    }

    public Date getTouchDate() {
        return touchDate;
    }

    public void setTouchDate(Date touchDate) {
        this.touchDate = touchDate;
    }

    public List<SharedFileLog> getSharedFileLogs() {
        List<SharedFileLog> logs = new ArrayList<SharedFileLog>(sharedFileLogs);
        Collections.sort(logs, new SharedFileLog.DateCompartor());
        return logs;
    }

    public void addSharedFileLog(SharedFileLog log) {
        log.setSharedFile(this);
        sharedFileLogs.add(log);
    }

    public List<SharedFileComment> getComments() {
        return new ArrayList<SharedFileComment>(comments);
    }

    public List<SharedFileComment> getRootComments() {
        List<SharedFileComment> rootComments = new ArrayList<SharedFileComment>();
        Iterables.addAll(rootComments, Iterables.filter(comments, new SharedFileComment.RootCommentPredicate()));
        Collections.sort(rootComments, new SharedFileComment.DateComparator());
        return rootComments;
    }

    public void addComment(SharedFileComment sharedFileComment) {
        sharedFileComment.setSharedFile(this);
        comments.add(sharedFileComment);
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE). //
                append("filename", filename). //
                append("onDiskFilename", onDiskFilename). //
                append("description", description). //
                append("size", size). //
                append("uploadDate", uploadDate). //
                append("touchDate", touchDate). //
                toString();
    }

    public static class UploadDateComparator implements Comparator<SharedFile> {

        private final int direction;

        public UploadDateComparator(boolean isReversed) {
            this.direction = isReversed ? -1 : 1;
        }

        @Override
        public int compare(SharedFile o1, SharedFile o2) {
            return direction * Compare.compare(o1.getUploadDate(), o2.getUploadDate());
        }
    }

}