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")
47 name = "SearchUserSharedAddressbooks",
48 query = "SELECT s FROM addressbook_shares AS s WHERE s.shareUserOwner = :user ORDER BY s.shareId ASC"
51 name = "SearchShareeAddressbookShare",
52 query = "SELECT s FROM addressbook_shares AS s WHERE s.shareAddressbook = :addressbook AND s.shareUserSharee = :sharee"
56 public class AddressbookShare implements ShareableAddressbook, Comparable<ShareableAddressbook> {
61 private static final long serialVersionUID = 167_889_678_177_691_690L;
67 @GeneratedValue (strategy = GenerationType.IDENTITY)
68 @Column (name = "share_id", length = 20, nullable = false, updatable = false)
72 * Address book this share is for
74 @JoinColumn (name = "share_addressbook_id", nullable = false, updatable = false)
75 @OneToOne (targetEntity = UserAddressbook.class, cascade = CascadeType.ALL, optional = false)
76 private Addressbook shareAddressbook;
79 * User who is giving the share (for his/her address book)
81 @JoinColumn (name = "share_owner_id", nullable = false, updatable = false)
82 @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.ALL, optional = false)
83 private User shareUserOwner;
86 * User the address book is shared with
88 @JoinColumn (name = "share_sharee_id", nullable = false, updatable = false)
89 @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.ALL, optional = false)
90 private User shareUserSharee;
93 * Default constructor for entity manager
95 protected AddressbookShare () {
99 * Constructor with address book and sharee instance. Both parameters must
100 * not be null, their id numbers must be set and the adress book's user
101 * instance must be set and have a valid id set.
103 * @param addressbook Address book instance
104 * @param sharee User sharee instance
106 public AddressbookShare (final Addressbook addressbook, final User sharee) {
107 // Call protected constructor
110 // Check all conditions
111 if (null == sharee) {
113 throw new NullPointerException("sharee is null"); //NOI18N
114 } else if (sharee.getUserId() == null) {
116 throw new NullPointerException("sharee.userId is null"); //NOI18N
117 } else if (sharee.getUserId() < 1) {
119 throw new IllegalStateException(MessageFormat.format("sharee.userId={0} is invalid", sharee.getUserId())); //NOI18N
120 } else if (null == addressbook) {
122 throw new NullPointerException("addressbook is null"); //NOI18N
123 } else if (addressbook.getAddressbookId() == null) {
125 throw new NullPointerException("addressbook.addressbookId is null"); //NOI18N
126 } else if (addressbook.getAddressbookId() < 1) {
128 throw new IllegalArgumentException(MessageFormat.format("addressbook.addressbookId={0} is invalid.", addressbook.getAddressbookId())); //NOI18N
129 } else if (Objects.equals(addressbook.getAddressbookUser(), sharee)) {
130 // Sharing with yourself!
131 throw new IllegalStateException("User tries to share with himself."); //NOI18N
135 this.shareAddressbook = addressbook;
136 this.shareUserOwner = addressbook.getAddressbookUser();
137 this.shareUserSharee = sharee;
141 public int compareTo (final ShareableAddressbook share) {
142 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
146 public boolean equals (final Object object) {
147 if (object == null) {
149 } else if (getClass() != object.getClass()) {
153 final ShareableAddressbook other = (ShareableAddressbook) object;
155 if (!Objects.equals(this.getShareAddressbook(), other.getShareAddressbook())) {
157 } else if (!Objects.equals(this.getShareUserOwner(), other.getShareUserOwner())) {
159 } else if (!Objects.equals(this.getShareUserOwner(), other.getShareUserOwner())) {
163 return Objects.equals(this.getShareUserSharee(), other.getShareUserSharee());
167 public Addressbook getShareAddressbook () {
168 return this.shareAddressbook;
172 public void setShareAddressbook (final Addressbook shareAddressbook) {
173 this.shareAddressbook = shareAddressbook;
177 public Long getShareId () {
182 public void setShareId (final Long shareId) {
183 this.shareId = shareId;
187 public User getShareUserOwner () {
188 return this.shareUserOwner;
192 public void setShareUserOwner (final User shareUserOwner) {
193 this.shareUserOwner = shareUserOwner;
197 public User getShareUserSharee () {
198 return this.shareUserSharee;
202 public void setShareUserSharee (final User shareUserSharee) {
203 this.shareUserSharee = shareUserSharee;
207 public int hashCode () {
209 hash = 19 * hash + Objects.hashCode(this.getShareAddressbook());
210 hash = 19 * hash + Objects.hashCode(this.getShareUserOwner());
211 hash = 19 * hash + Objects.hashCode(this.getShareUserSharee());