hopsworks-master/hopsworks-persistence/src/main/java/io/hops/hopsworks/persistence/entity/kafka/ProjectTopics.java
/*
 * Changes to this file committed after and not including commit-id: ccc0d2c5f9a5ac661e60e6eaf138de7889928b8b
 * are released under the following license:
 *
 * This file is part of Hopsworks
 * Copyright (C) 2018, Logical Clocks AB. All rights reserved
 *
 * Hopsworks is free software: you can redistribute it and/or modify it under the terms of
 * the GNU Affero General Public License as published by the Free Software Foundation,
 * either version 3 of the License, or (at your option) any later version.
 *
 * Hopsworks 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License along with this program.
 * If not, see <https://www.gnu.org/licenses/>.
 *
 * Changes to this file committed before and including commit-id: ccc0d2c5f9a5ac661e60e6eaf138de7889928b8b
 * are released under the following license:
 *
 * Copyright (C) 2013 - 2018, Logical Clocks AB and RISE SICS AB. All rights reserved
 *
 * 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 io.hops.hopsworks.persistence.entity.kafka;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;

import io.hops.hopsworks.persistence.entity.kafka.schemas.Subjects;
import io.hops.hopsworks.persistence.entity.project.Project;

@Entity
@Table(name = "project_topics",
        catalog = "hopsworks",
        schema = "")
@XmlRootElement
@NamedQueries({
  @NamedQuery(name = "ProjectTopics.findAll",
          query = "SELECT p FROM ProjectTopics p"),
  @NamedQuery(name = "ProjectTopics.findByTopicName",
          query = "SELECT p FROM ProjectTopics p WHERE p.topicName = :topicName"),
  @NamedQuery(name = "ProjectTopics.findByProject",
          query = "SELECT p FROM ProjectTopics p WHERE p.project = :project"),
  @NamedQuery(name = "ProjectTopics.countByProject",
        query = "SELECT count(p.topicName) FROM ProjectTopics p WHERE p.project = :project"),
  @NamedQuery(name = "ProjectTopics.findByProjectAndTopicName",
          query = "SELECT p FROM ProjectTopics p WHERE p.project = :project " +
              "AND p.topicName = :topicName"),
  @NamedQuery(name = "ProjectTopics.findBySubjectAndVersion",
          query = "SELECT p FROM ProjectTopics p WHERE p.subjects.subject = :subject " +
            "AND p.subjects.version = :version AND p.project = :project"),
  @NamedQuery(name = "ProjectTopics.findBySubject",
          query = "SELECT p FROM ProjectTopics p WHERE p.project = :project AND " +
            "p.subjects.subject = :subject")})
public class ProjectTopics implements Serializable {

  private static final long serialVersionUID = 1L;

  @Id
  @GeneratedValue(strategy = GenerationType.SEQUENCE)
  @Basic(optional = false)
  @Column(name = "id", unique = true)
  private Integer id;

  @Basic(optional = false)
  @NotNull
  @Size(min = 1, max = 255)
  @Column(name = "topic_name")
  private String topicName;

  // Number of partitions and replicas are used for backup only at this stage
  // The rest APIs rely on Zookeeper to pull the current status of the partitions and replications
  @Basic
  @Column(name = "num_partitions")
  private Integer numOfPartitions;

  @Basic
  @Column(name = "num_replicas")
  private Integer numOfReplicas;

  @JoinColumn(name = "project_id",
    referencedColumnName = "id")
  @ManyToOne(optional = false)
  private Project project;

  @JoinColumn(name = "subject_id", referencedColumnName = "id")
  @ManyToOne(optional = false)
  private Subjects subjects;

  public ProjectTopics() {
  }

  public ProjectTopics(String topicName, Integer numOfPartitions, Integer numOfReplicas,
                       Project project, Subjects subjects) {
    this.topicName = topicName;
    this.numOfPartitions = numOfPartitions;
    this.numOfReplicas = numOfReplicas;
    this.project = project;
    this.subjects = subjects;
  }

  public Integer getId() {
    return id;
  }

  public void setId(Integer id) {
    this.id = id;
  }

  @NotNull
  public String getTopicName() {
    return topicName;
  }

  public void setTopicName(@NotNull String topicName) {
    this.topicName = topicName;
  }

  public Project getProject() {
    return project;
  }

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

  public Subjects getSubjects() {
    return subjects;
  }

  public void setSubjects(Subjects subjects) {
    this.subjects = subjects;
  }

  public Integer getNumOfPartitions() {
    return numOfPartitions;
  }

  public void setNumOfPartitions(Integer numOfPartitions) {
    this.numOfPartitions = numOfPartitions;
  }

  public Integer getNumOfReplicas() {
    return numOfReplicas;
  }

  public void setNumOfReplicas(Integer numOfReplicas) {
    this.numOfReplicas = numOfReplicas;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    ProjectTopics topics = (ProjectTopics) o;

    if (id != null ? !id.equals(topics.id) : topics.id != null) return false;
    if (!topicName.equals(topics.topicName)) return false;
    if (project != null ? !project.equals(topics.project) : topics.project != null) return false;
    return subjects != null ? subjects.equals(topics.subjects) : topics.subjects == null;
  }

  @Override
  public int hashCode() {
    int result = id != null ? id.hashCode() : 0;
    result = 31 * result + (topicName != null ? topicName.hashCode() : 0);
    result = 31 * result + (numOfPartitions != null ? numOfPartitions.hashCode() : 0);
    result = 31 * result + (numOfReplicas != null ? numOfReplicas.hashCode() : 0);
    result = 31 * result + (project != null ? project.hashCode() : 0);
    result = 31 * result + (subjects != null ? subjects.hashCode() : 0);
    return result;
  }
}
