-/*
- * Copyright (C) 2016 Roland Haeder
- *
- * This program 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.
- *
- * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
- */
-package org.mxchange.addressbook.model.addressbook.shared;
-
-import java.text.MessageFormat;
-import java.util.Calendar;
-import java.util.Objects;
-import javax.persistence.Basic;
-import javax.persistence.CascadeType;
-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.NamedQueries;
-import javax.persistence.NamedQuery;
-import javax.persistence.OneToOne;
-import javax.persistence.Table;
-import javax.persistence.Temporal;
-import javax.persistence.TemporalType;
-import org.mxchange.addressbook.model.addressbook.Addressbook;
-import org.mxchange.addressbook.model.addressbook.UserAddressbook;
-import org.mxchange.jusercore.model.user.LoginUser;
-import org.mxchange.jusercore.model.user.User;
-
-/**
- * A POJO for sharing address books with other users
- * <p>
- * @author Roland Haeder<roland@mxchange.org>
- */
-@Entity (name = "addressbook_shares")
-@Table (name = "addressbook_shares")
-@NamedQueries (
- {
- @NamedQuery (
- name = "SearchUserSharedAddressbooks",
- query = "SELECT s FROM addressbook_shares AS s WHERE s.shareUserOwner = :user ORDER BY s.shareId ASC"
- ),
- @NamedQuery (
- name = "SearchShareeAddressbookShare",
- query = "SELECT s FROM addressbook_shares AS s WHERE s.shareAddressbook = :addressbook AND s.shareUserSharee = :sharee"
- )
- }
-)
-public class AddressbookShare implements ShareableAddressbook {
-
- /**
- * Serial number
- */
- private static final long serialVersionUID = 167_889_678_177_691_690L;
-
- /**
- * Address book this share is for
- */
- @JoinColumn (name = "share_addressbook_id", nullable = false, updatable = false)
- @OneToOne (targetEntity = UserAddressbook.class, cascade = CascadeType.REFRESH, optional = false)
- private Addressbook shareAddressbook;
-
- /**
- * When this share has been created
- */
- @Basic (optional = false)
- @Temporal (TemporalType.TIMESTAMP)
- @Column (name = "share_created", nullable = false, updatable = false)
- private Calendar shareCreated;
-
- /**
- * Id number
- */
- @Id
- @GeneratedValue (strategy = GenerationType.IDENTITY)
- @Column (name = "share_id", nullable = false, updatable = false)
- private Long shareId;
-
- /**
- * User who is owning the share
- */
- @JoinColumn (name = "share_owner_id", nullable = false, updatable = false)
- @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.REFRESH, optional = false)
- private User shareUserOwner;
-
- /**
- * User the address book is shared with
- */
- @JoinColumn (name = "share_sharee_id", nullable = false, updatable = false)
- @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.REFRESH, optional = false)
- private User shareUserSharee;
-
- /**
- * Default constructor
- */
- public AddressbookShare () {
- }
-
- /**
- * Constructor with address book and sharee instance. Both parameters must
- * not be null, their id numbers must be set and the adress book's user
- * instance must be set and have a valid id set.
- * <p>
- * @param addressbook Address book instance
- * @param sharee User sharee instance
- */
- public AddressbookShare (final Addressbook addressbook, final User sharee) {
- // Call default constructor
- this();
-
- // Check all conditions
- if (null == sharee) {
- // Throw NPE
- throw new NullPointerException("sharee is null"); //NOI18N
- } else if (sharee.getUserId() == null) {
- // Throw NPE again
- throw new NullPointerException("sharee.userId is null"); //NOI18N
- } else if (sharee.getUserId() < 1) {
- // Invalid id number
- throw new IllegalStateException(MessageFormat.format("sharee.userId={0} is invalid", sharee.getUserId())); //NOI18N
- } else if (null == addressbook) {
- // Throw NPE again
- throw new NullPointerException("addressbook is null"); //NOI18N
- } else if (addressbook.getAddressbookId() == null) {
- // Throw NPE again
- throw new NullPointerException("addressbook.addressbookId is null"); //NOI18N
- } else if (addressbook.getAddressbookId() < 1) {
- // Invalid id number
- throw new IllegalArgumentException(MessageFormat.format("addressbook.addressbookId={0} is invalid.", addressbook.getAddressbookId())); //NOI18N
- } else if (Objects.equals(addressbook.getAddressbookUser(), sharee)) {
- // Sharing with yourself!
- throw new IllegalStateException("User tries to share with himself."); //NOI18N
- }
-
- // Set all instances
- this.shareAddressbook = addressbook;
- this.shareUserOwner = addressbook.getAddressbookUser();
- this.shareUserSharee = sharee;
- }
-
- @Override
- public boolean equals (final Object object) {
- if (null == object) {
- return false;
- } else if (this.getClass() != object.getClass()) {
- return false;
- }
-
- final ShareableAddressbook other = (ShareableAddressbook) object;
-
- if (!Objects.equals(this.getShareAddressbook(), other.getShareAddressbook())) {
- return false;
- } else if (!Objects.equals(this.getShareUserOwner(), other.getShareUserOwner())) {
- return false;
- } else if (!Objects.equals(this.getShareUserOwner(), other.getShareUserOwner())) {
- return false;
- }
-
- return Objects.equals(this.getShareUserSharee(), other.getShareUserSharee());
- }
-
- @Override
- public int hashCode () {
- int hash = 7;
- hash = 19 * hash + Objects.hashCode(this.getShareAddressbook());
- hash = 19 * hash + Objects.hashCode(this.getShareUserOwner());
- hash = 19 * hash + Objects.hashCode(this.getShareUserSharee());
- return hash;
- }
-
- @Override
- public Addressbook getShareAddressbook () {
- return this.shareAddressbook;
- }
-
- @Override
- public void setShareAddressbook (final Addressbook shareAddressbook) {
- this.shareAddressbook = shareAddressbook;
- }
-
- @Override
- public Calendar getShareCreated () {
- return this.shareCreated;
- }
-
- @Override
- public void setShareCreated (final Calendar shareCreated) {
- this.shareCreated = shareCreated;
- }
-
- @Override
- public Long getShareId () {
- return this.shareId;
- }
-
- @Override
- public void setShareId (final Long shareId) {
- this.shareId = shareId;
- }
-
- @Override
- public User getShareUserOwner () {
- return this.shareUserOwner;
- }
-
- @Override
- public void setShareUserOwner (final User shareUserOwner) {
- this.shareUserOwner = shareUserOwner;
- }
-
- @Override
- public User getShareUserSharee () {
- return this.shareUserSharee;
- }
-
- @Override
- public void setShareUserSharee (final User shareUserSharee) {
- this.shareUserSharee = shareUserSharee;
- }
-
-}