2 * Copyright (C) 2015 Roland Haeder
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.addressbook.model.addressbook.shared;
19 import java.text.MessageFormat;
20 import java.util.Objects;
21 import javax.persistence.CascadeType;
22 import javax.persistence.Column;
23 import javax.persistence.Entity;
24 import javax.persistence.GeneratedValue;
25 import javax.persistence.GenerationType;
26 import javax.persistence.Id;
27 import javax.persistence.JoinColumn;
28 import javax.persistence.NamedQueries;
29 import javax.persistence.NamedQuery;
30 import javax.persistence.OneToOne;
31 import javax.persistence.Table;
32 import org.mxchange.addressbook.model.addressbook.Addressbook;
33 import org.mxchange.addressbook.model.addressbook.UserAddressbook;
34 import org.mxchange.jusercore.model.user.LoginUser;
35 import org.mxchange.jusercore.model.user.User;
38 * A POJO for sharing address books with other users
40 * @author Roland Haeder
42 @Entity (name = "addressbook_shares")
43 @Table (name = "addressbook_shares")
45 @NamedQuery (name = "SearchUserSharedAddressbooks", query = "SELECT s FROM addressbook_shares AS s WHERE s.shareUserOwner = :user ORDER BY s.shareId ASC")
47 public class AddressbookShare implements ShareableAddressbook, Comparable<ShareableAddressbook> {
52 private static final long serialVersionUID = 167_889_678_177_691_690L;
58 @GeneratedValue (strategy = GenerationType.IDENTITY)
59 @Column (name = "share_id", length = 20, nullable = false, updatable = false)
63 * Address book this share is for
65 @JoinColumn (name = "share_addressbook_id", nullable = false, updatable = false)
66 @OneToOne (targetEntity = UserAddressbook.class, cascade = CascadeType.ALL, optional = false)
67 private Addressbook shareAddressbook;
70 * User who is giving the share (for his/her address book)
72 @JoinColumn (name = "share_owner_id", nullable = false, updatable = false)
73 @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.ALL, optional = false)
74 private User shareUserOwner;
77 * User the address book is shared with
79 @JoinColumn (name = "share_sharee_id", nullable = false, updatable = false)
80 @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.ALL, optional = false)
81 private User shareUserSharee;
84 * Default constructor for entity manager
86 protected AddressbookShare () {
90 * Constructor with address book and sharee instance. Both parameters must
91 * not be null, their id numbers must be set and the adress book's user
92 * instance must be set and have a valid id set.
94 * @param addressbook Address book instance
95 * @param sharee User sharee instance
97 public AddressbookShare (final Addressbook addressbook, final User sharee) {
98 // Call protected constructor
101 // Check all conditions
102 if (null == sharee) {
104 throw new NullPointerException("sharee is null"); //NOI18N
105 } else if (sharee.getUserId() == null) {
107 throw new NullPointerException("sharee.userId is null"); //NOI18N
108 } else if (sharee.getUserId() < 1) {
110 throw new IllegalStateException(MessageFormat.format("sharee.userId={0} is invalid", sharee.getUserId())); //NOI18N
111 } else if (null == addressbook) {
113 throw new NullPointerException("addressbook is null"); //NOI18N
114 } else if (addressbook.getAddressbookId() == null) {
116 throw new NullPointerException("addressbook.addressbookId is null"); //NOI18N
117 } else if (addressbook.getAddressbookId() < 1) {
119 throw new IllegalArgumentException(MessageFormat.format("addressbook.addressbookId={0} is invalid.", addressbook.getAddressbookId())); //NOI18N
120 } else if (Objects.equals(addressbook.getAddressbookUser(), sharee)) {
121 // Sharing with yourself!
122 throw new IllegalStateException("User tries to share with himself."); //NOI18N
126 this.shareAddressbook = addressbook;
127 this.shareUserOwner = addressbook.getAddressbookUser();
128 this.shareUserSharee = sharee;
132 public int compareTo (final ShareableAddressbook share) {
133 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
137 public boolean equals (final Object object) {
138 if (object == null) {
140 } else if (getClass() != object.getClass()) {
144 final ShareableAddressbook other = (ShareableAddressbook) object;
146 if (!Objects.equals(this.getShareAddressbook(), other.getShareAddressbook())) {
148 } else if (!Objects.equals(this.getShareUserOwner(), other.getShareUserOwner())) {
150 } else if (!Objects.equals(this.getShareUserOwner(), other.getShareUserOwner())) {
154 return Objects.equals(this.getShareUserSharee(), other.getShareUserSharee());
158 public Addressbook getShareAddressbook () {
159 return this.shareAddressbook;
163 public void setShareAddressbook (final Addressbook shareAddressbook) {
164 this.shareAddressbook = shareAddressbook;
168 public Long getShareId () {
173 public void setShareId (final Long shareId) {
174 this.shareId = shareId;
178 public User getShareUserOwner () {
179 return this.shareUserOwner;
183 public void setShareUserOwner (final User shareUserOwner) {
184 this.shareUserOwner = shareUserOwner;
188 public User getShareUserSharee () {
189 return this.shareUserSharee;
193 public void setShareUserSharee (final User shareUserSharee) {
194 this.shareUserSharee = shareUserSharee;
198 public int hashCode () {
200 hash = 19 * hash + Objects.hashCode(this.getShareAddressbook());
201 hash = 19 * hash + Objects.hashCode(this.getShareUserOwner());
202 hash = 19 * hash + Objects.hashCode(this.getShareUserSharee());