com.qatickets.domain.Ticket.java Source code

Java tutorial

Introduction

Here is the source code for com.qatickets.domain.Ticket.java

Source

/*
The MIT License (MIT)
    
Copyright (c) 2015 QAXML Pty Ltd
    
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
    
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
    
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package com.qatickets.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.SafeHtml;

import com.qatickets.domain.common.DateHelper;

@SuppressWarnings("serial")
@Entity
@Table(name = "TICKET")
public class Ticket implements Serializable {

    public static final int TYPE_BUG = 0;
    public static final int TYPE_TASK = 1;
    public static final int TYPE_IMPROVEMENT = 2;
    public static final int TYPE_NEW_FEATURE = 3;
    public static final int TYPE_BUSINESS_REQUREMENT = 4;

    public static final int BUSINESS_IMPACT_SHOWSTOPPER = 0;
    public static final int BUSINESS_IMPACT_MAJOR = 1;
    public static final int BUSINESS_IMPACT_MODERATE = 2;
    public static final int BUSINESS_IMPACT_MINOR = 3;

    public static final int STATUS_OPEN = 0;
    public static final int STATUS_IN_PROGRESS = 1;
    public static final int STATUS_DEV_COMPLETED = 2;
    public static final int STATUS_QA_READY = 3;
    public static final int STATUS_QA_PASSED = 4;
    public static final int STATUS_QA_REJECTED = 5;
    public static final int STATUS_CLOSED = 6;

    public static final List<Integer> OPENED_STATUSES = new ArrayList<Integer>() {
        {
            add(Ticket.STATUS_OPEN);
            add(Ticket.STATUS_IN_PROGRESS);
            add(Ticket.STATUS_DEV_COMPLETED);
            add(Ticket.STATUS_QA_READY);
            add(Ticket.STATUS_QA_PASSED);
            add(Ticket.STATUS_QA_REJECTED);
        }
    };

    @Id
    @Column(name = "TICKET_ID")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    @NotNull
    @Length(max = 64)
    @Column(name = "NUM", unique = true)
    private String num;

    @NotNull
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "PROJECT_ID")
    private Project project;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "FOUND_VERSION_ID")
    private Version foundVersion;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "FIX_VERSION_ID")
    private Version fixScheduledVersion;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "PARENT_ID")
    private Ticket parent;

    @NotNull
    @Length(max = 36)
    @Column(name = "UUID", unique = true)
    private String uuid = UUID.randomUUID().toString();

    @NotNull
    @Column(name = "STATUS", columnDefinition = "TINYINT UNSIGNED")
    private int status;

    @NotNull
    @Column(name = "TYPE", columnDefinition = "TINYINT UNSIGNED")
    private int type;

    @NotNull
    @Column(name = "BUSINESS_IMPACT", columnDefinition = "TINYINT UNSIGNED")
    private int businessImpact;

    @NotNull
    @SafeHtml
    @Length(max = 256)
    @Column(name = "SUMMARY")
    private String summary;

    @NotNull
    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name = "TICKET_BODY_ID")
    private TicketBody ticketBody;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "REPORTER_USER_ID")
    private UserProfile reporter;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "ASSIGNEE_USER_ID")
    private UserProfile assignee;

    @NotNull
    @Column(name = "CREATE_DATE")
    private Date createDate;

    @Column(name = "PROGRESS", columnDefinition = "TINYINT UNSIGNED")
    private int progress;

    @NotNull
    @Column(name = "UPDATE_DATE", columnDefinition = "TIMESTAMP")
    private Date updateDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "USER_ID")
    private UserProfile user;

    @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "ticket")
    private List<TicketWatcher> watchers;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "ticket")
    private Set<TicketFileAttachment> fileAttachments;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "ticket")
    private Set<TicketComponent> components;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "ticket")
    private Set<Comment> comments;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "ticket")
    private Set<TicketVote> votes;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "source")
    private Set<Link> linksFrom;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "destination")
    private Set<Link> linksTo;

    public Set<TicketFileAttachment> getFileAttachments() {
        return fileAttachments;
    }

    public List<TicketWatcher> getWatchers() {
        return watchers;
    }

    public void setWatchers(List<TicketWatcher> watchers) {
        this.watchers = watchers;
    }

    public void addWatcher(TicketWatcher watcher) {
        if (this.watchers == null) {
            this.watchers = new ArrayList<TicketWatcher>();
        }
        this.watchers.add(watcher);
    }

    public void setFileAttachments(Set<TicketFileAttachment> fileAttachments) {
        this.fileAttachments = fileAttachments;
    }

    public Set<Comment> getComments() {
        return comments;
    }

    public void setComments(Set<Comment> comments) {
        this.comments = comments;
    }

    public UserProfile getUser() {
        return user;
    }

    public void setUser(UserProfile user) {
        this.user = user;
    }

    public int getProgress() {
        return progress;
    }

    public void setProgress(int progress) {
        this.progress = progress;
    }

    public TicketBody getTicketBody() {
        return ticketBody;
    }

    public void setTicketBody(TicketBody ticketBody) {
        this.ticketBody = ticketBody;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getNum() {
        return num;
    }

    public void setNum(String num) {
        this.num = num;
    }

    public UserProfile getReporter() {
        return reporter;
    }

    public void setReporter(UserProfile reporter) {
        this.reporter = reporter;
    }

    public UserProfile getAssignee() {
        return assignee;
    }

    public void setAssignee(UserProfile assignee) {
        this.assignee = assignee;
    }

    public Project getProject() {
        return project;
    }

    public void setProject(Project project) {
        this.project = project;
    }

    public Version getFoundVersion() {
        return foundVersion;
    }

    public void setFoundVersion(Version foundVersion) {
        this.foundVersion = foundVersion;
    }

    public Ticket getParent() {
        return parent;
    }

    public void setParent(Ticket parent) {
        this.parent = parent;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public int getBusinessImpact() {
        return businessImpact;
    }

    public void setBusinessImpact(int businessImpact) {
        this.businessImpact = businessImpact;
    }

    public String getSummary() {
        return summary;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }

    public Date getCreateDate() {
        return createDate;
    }

    public String getCreateDateRelative() {
        return DateHelper.getTimeAgo(createDate.getTime());
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    public Date getUpdateDate() {
        return updateDate;
    }

    public void setUpdateDate(Date updateDate) {
        this.updateDate = updateDate;
    }

    public Version getFixScheduledVersion() {
        return fixScheduledVersion;
    }

    public void setFixScheduledVersion(Version fixScheduledVersion) {
        this.fixScheduledVersion = fixScheduledVersion;
    }

    public int getId() {
        return id;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + id;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        Ticket other = (Ticket) obj;
        if (id != other.getId())
            return false;
        return true;
    }

    @Override
    public String toString() {
        return "Ticket [id=" + id + ", num=" + num + ", type=" + type + ", businessImpact=" + businessImpact
                + ", summary=" + summary + ", createDate=" + createDate + ", updateDate=" + updateDate + "]";
    }

    public boolean belongsToProject(Project p) {
        return p.equals(this.project);
    }

    public static boolean isValidTypeValue(int typeToCheck) {
        return typeToCheck == TYPE_BUG || typeToCheck == TYPE_TASK || typeToCheck == TYPE_IMPROVEMENT
                || typeToCheck == TYPE_NEW_FEATURE || typeToCheck == TYPE_BUSINESS_REQUREMENT;
    }

    public static boolean isValidBusinessImpactValue(int value) {
        return value == BUSINESS_IMPACT_SHOWSTOPPER || value == BUSINESS_IMPACT_MAJOR
                || value == BUSINESS_IMPACT_MODERATE || value == BUSINESS_IMPACT_MINOR;
    }

    public static boolean isValidStatusValue(int value) {
        return value == STATUS_OPEN || value == STATUS_IN_PROGRESS || value == STATUS_DEV_COMPLETED
                || value == STATUS_QA_READY || value == STATUS_QA_PASSED || value == STATUS_QA_REJECTED
                || value == STATUS_CLOSED;
    }

    public String getTextForSearch() {
        return this.summary + " " + this.getTicketBody().getDescription() + " "
                + this.getTicketBody().getEnvironment();
    }

    public static boolean isValidStatusValues(Integer[] statuses) {

        for (int status : statuses) {
            if (false == isValidStatusValue(status)) {
                return false;
            }
        }

        return true;

    }

    public static boolean isValidTypeValues(Integer[] types) {

        for (int type : types) {
            if (false == isValidTypeValue(type)) {
                return false;
            }
        }

        return true;
    }

    public static boolean isValidBusinessImpactValues(Integer[] businessImpacts) {

        for (int impact : businessImpacts) {
            if (false == isValidBusinessImpactValue(impact)) {
                return false;
            }
        }

        return true;
    }

    public void addAttachment(TicketFileAttachment a) {

        if (this.fileAttachments == null) {
            this.fileAttachments = new HashSet<TicketFileAttachment>();
        }

        this.fileAttachments.add(a);

    }

    public Set<TicketComponent> getComponents() {
        return components;
    }

    public String getComponentsAsString() {

        if (components == null) {
            return null;
        }

        List<String> list = new ArrayList<String>(components.size());
        for (TicketComponent c : components) {
            list.add(Integer.toString(c.getComponent().getId()));
        }
        Collections.sort(list, String.CASE_INSENSITIVE_ORDER);

        return StringUtils.join(list, ",");
    }

    public void setComponents(Set<TicketComponent> components) {
        this.components = components;
    }

    public void addComponent(TicketComponent component) {
        if (this.components == null) {
            this.components = new HashSet<TicketComponent>();
        }
        this.components.add(component);
    }

    public String getSearchableText() {

        StringBuilder sb = new StringBuilder();

        sb.append(this.getSummary() == null ? "" : this.getSummary());
        sb.append(" ");

        sb.append(this.getTicketBody().getDescription() == null ? "" : this.getTicketBody().getDescription());
        sb.append(" ");

        sb.append(this.getTicketBody().getEnvironment() == null ? "" : this.getTicketBody().getEnvironment());
        sb.append(" ");

        sb.append(this.getTicketBody().getFixBuild() == null ? "" : this.getTicketBody().getFixBuild());
        sb.append(" ");

        sb.append(this.getTicketBody().getFixDetails() == null ? "" : this.getTicketBody().getFixDetails());
        sb.append(" ");

        return sb.toString();
    }

    public boolean hasFileAttachments() {
        return this.getFileAttachments() != null && this.getFileAttachments().isEmpty() == false;
    }

}