2 * Copyright (C) 2016 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.jaddressbookcore.model.addressbook.shared;
19 import java.text.MessageFormat;
20 import java.util.Calendar;
21 import java.util.Objects;
22 import javax.persistence.Basic;
23 import javax.persistence.CascadeType;
24 import javax.persistence.Column;
25 import javax.persistence.Entity;
26 import javax.persistence.GeneratedValue;
27 import javax.persistence.GenerationType;
28 import javax.persistence.Id;
29 import javax.persistence.JoinColumn;
30 import javax.persistence.NamedQueries;
31 import javax.persistence.NamedQuery;
32 import javax.persistence.OneToOne;
33 import javax.persistence.Table;
34 import javax.persistence.Temporal;
35 import javax.persistence.TemporalType;
36 import org.mxchange.jaddressbookcore.model.addressbook.Addressbook;
37 import org.mxchange.jaddressbookcore.model.addressbook.UserAddressbook;
38 import org.mxchange.jusercore.model.user.User;
41 * A POJO for sharing address books with other users
43 * @author Roland Haeder<roland@mxchange.org>
45 @Entity (name = "addressbook_shares")
46 @Table (name = "addressbook_shares")
50 name = "SearchUserSharedAddressbooks",
51 query = "SELECT s FROM addressbook_shares AS s WHERE s.shareUserOwner = :user ORDER BY s.shareId ASC"
54 name = "SearchShareeAddressbookShare",
55 query = "SELECT s FROM addressbook_shares AS s WHERE s.shareAddressbook = :addressbook AND s.shareUserSharee = :sharee"
59 public class AddressbookShare implements ShareableAddressbook {
64 private static final long serialVersionUID = 167_889_678_177_691_690L;
67 * Address book this share is for
69 @JoinColumn (name = "share_addressbook_id", nullable = false, updatable = false)
70 @OneToOne (targetEntity = UserAddressbook.class, cascade = CascadeType.REFRESH, optional = false)
71 private Addressbook shareAddressbook;
74 * When this share has been created
76 @Basic (optional = false)
77 @Temporal (TemporalType.TIMESTAMP)
78 @Column (name = "share_created", nullable = false, updatable = false)
79 private Calendar shareCreated;
85 @GeneratedValue (strategy = GenerationType.IDENTITY)
86 @Column (name = "share_id", nullable = false, updatable = false)
90 * User who is owning the share
92 @JoinColumn (name = "share_owner_id", nullable = false, updatable = false)
93 @OneToOne (targetEntity = User.class, cascade = CascadeType.REFRESH, optional = false)
94 private User shareUserOwner;
97 * User the address book is shared with
99 @JoinColumn (name = "share_sharee_id", nullable = false, updatable = false)
100 @OneToOne (targetEntity = User.class, cascade = CascadeType.REFRESH, optional = false)
101 private User shareUserSharee;
104 * Default constructor
106 public AddressbookShare () {
110 * Constructor with address book and sharee instance. Both parameters must
111 * not be null, their id numbers must be set and the adress book's user
112 * instance must be set and have a valid id set.
114 * @param addressbook Address book instance
115 * @param sharee User sharee instance
117 public AddressbookShare (final Addressbook addressbook, final User sharee) {
118 // Call default constructor
121 // Check all conditions
122 if (null == sharee) {
124 throw new NullPointerException("sharee is null"); //NOI18N
125 } else if (sharee.getUserId() == null) {
127 throw new NullPointerException("sharee.userId is null"); //NOI18N
128 } else if (sharee.getUserId() < 1) {
130 throw new IllegalStateException(MessageFormat.format("sharee.userId={0} is invalid", sharee.getUserId())); //NOI18N
131 } else if (null == addressbook) {
133 throw new NullPointerException("addressbook is null"); //NOI18N
134 } else if (addressbook.getAddressbookId() == null) {
136 throw new NullPointerException("addressbook.addressbookId is null"); //NOI18N
137 } else if (addressbook.getAddressbookId() < 1) {
139 throw new IllegalArgumentException(MessageFormat.format("addressbook.addressbookId={0} is invalid.", addressbook.getAddressbookId())); //NOI18N
140 } else if (Objects.equals(addressbook.getAddressbookUser(), sharee)) {
141 // Sharing with yourself!
142 throw new IllegalStateException("User tries to share with himself."); //NOI18N
146 this.shareAddressbook = addressbook;
147 this.shareUserOwner = addressbook.getAddressbookUser();
148 this.shareUserSharee = sharee;
152 public boolean equals (final Object object) {
153 if (null == object) {
155 } else if (this.getClass() != object.getClass()) {
159 final ShareableAddressbook other = (ShareableAddressbook) object;
161 if (!Objects.equals(this.getShareAddressbook(), other.getShareAddressbook())) {
163 } else if (!Objects.equals(this.getShareUserOwner(), other.getShareUserOwner())) {
165 } else if (!Objects.equals(this.getShareUserOwner(), other.getShareUserOwner())) {
169 return Objects.equals(this.getShareUserSharee(), other.getShareUserSharee());
173 public int hashCode () {
175 hash = 19 * hash + Objects.hashCode(this.getShareAddressbook());
176 hash = 19 * hash + Objects.hashCode(this.getShareUserOwner());
177 hash = 19 * hash + Objects.hashCode(this.getShareUserSharee());
182 public Addressbook getShareAddressbook () {
183 return this.shareAddressbook;
187 public void setShareAddressbook (final Addressbook shareAddressbook) {
188 this.shareAddressbook = shareAddressbook;
192 public Calendar getShareCreated () {
193 return this.shareCreated;
197 public void setShareCreated (final Calendar shareCreated) {
198 this.shareCreated = shareCreated;
202 public Long getShareId () {
207 public void setShareId (final Long shareId) {
208 this.shareId = shareId;
212 public User getShareUserOwner () {
213 return this.shareUserOwner;
217 public void setShareUserOwner (final User shareUserOwner) {
218 this.shareUserOwner = shareUserOwner;
222 public User getShareUserSharee () {
223 return this.shareUserSharee;
227 public void setShareUserSharee (final User shareUserSharee) {
228 this.shareUserSharee = shareUserSharee;