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.Calendar;
21 import java.util.GregorianCalendar;
22 import java.util.Objects;
23 import javax.persistence.Basic;
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.addressbook.model.addressbook.Addressbook;
37 import org.mxchange.addressbook.model.addressbook.UserAddressbook;
38 import org.mxchange.jusercore.model.user.LoginUser;
39 import org.mxchange.jusercore.model.user.User;
42 * A POJO for sharing address books with other users
44 * @author Roland Haeder
46 @Entity (name = "addressbook_shares")
47 @Table (name = "addressbook_shares")
51 name = "SearchUserSharedAddressbooks",
52 query = "SELECT s FROM addressbook_shares AS s WHERE s.shareUserOwner = :user ORDER BY s.shareId ASC"
55 name = "SearchShareeAddressbookShare",
56 query = "SELECT s FROM addressbook_shares AS s WHERE s.shareAddressbook = :addressbook AND s.shareUserSharee = :sharee"
60 public class AddressbookShare implements ShareableAddressbook, Comparable<ShareableAddressbook> {
65 private static final long serialVersionUID = 167_889_678_177_691_690L;
71 @GeneratedValue (strategy = GenerationType.IDENTITY)
72 @Column (name = "share_id", length = 20, nullable = false, updatable = false)
76 * Address book this share is for
78 @JoinColumn (name = "share_addressbook_id", nullable = false, updatable = false)
79 @OneToOne (targetEntity = UserAddressbook.class, optional = false)
80 private Addressbook shareAddressbook;
83 * User who is giving the share (for his/her address book)
85 @JoinColumn (name = "share_owner_id", nullable = false, updatable = false)
86 @OneToOne (targetEntity = LoginUser.class, optional = false)
87 private User shareUserOwner;
90 * User the address book is shared with
92 @JoinColumn (name = "share_sharee_id", nullable = false, updatable = false)
93 @OneToOne (targetEntity = LoginUser.class, optional = false)
94 private User shareUserSharee;
97 * When this share has been created
99 @Basic (optional = false)
100 @Temporal (TemporalType.TIMESTAMP)
101 @Column (name = "share_created", nullable = false, updatable = false)
102 private Calendar shareCreated;
105 * Default constructor for entity manager
107 protected AddressbookShare () {
111 * Constructor with address book and sharee instance. Both parameters must
112 * not be null, their id numbers must be set and the adress book's user
113 * instance must be set and have a valid id set.
115 * @param addressbook Address book instance
116 * @param sharee User sharee instance
118 public AddressbookShare (final Addressbook addressbook, final User sharee) {
119 // Call protected constructor
122 // Check all conditions
123 if (null == sharee) {
125 throw new NullPointerException("sharee is null"); //NOI18N
126 } else if (sharee.getUserId() == null) {
128 throw new NullPointerException("sharee.userId is null"); //NOI18N
129 } else if (sharee.getUserId() < 1) {
131 throw new IllegalStateException(MessageFormat.format("sharee.userId={0} is invalid", sharee.getUserId())); //NOI18N
132 } else if (null == addressbook) {
134 throw new NullPointerException("addressbook is null"); //NOI18N
135 } else if (addressbook.getAddressbookId() == null) {
137 throw new NullPointerException("addressbook.addressbookId is null"); //NOI18N
138 } else if (addressbook.getAddressbookId() < 1) {
140 throw new IllegalArgumentException(MessageFormat.format("addressbook.addressbookId={0} is invalid.", addressbook.getAddressbookId())); //NOI18N
141 } else if (Objects.equals(addressbook.getAddressbookUser(), sharee)) {
142 // Sharing with yourself!
143 throw new IllegalStateException("User tries to share with himself."); //NOI18N
147 this.shareAddressbook = addressbook;
148 this.shareUserOwner = addressbook.getAddressbookUser();
149 this.shareUserSharee = sharee;
150 this.shareCreated = new GregorianCalendar();
154 public int compareTo (final ShareableAddressbook share) {
155 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
159 public boolean equals (final Object object) {
160 if (object == null) {
162 } else if (getClass() != object.getClass()) {
166 final ShareableAddressbook other = (ShareableAddressbook) object;
168 if (!Objects.equals(this.getShareAddressbook(), other.getShareAddressbook())) {
170 } else if (!Objects.equals(this.getShareUserOwner(), other.getShareUserOwner())) {
172 } else if (!Objects.equals(this.getShareUserOwner(), other.getShareUserOwner())) {
176 return Objects.equals(this.getShareUserSharee(), other.getShareUserSharee());
180 public Addressbook getShareAddressbook () {
181 return this.shareAddressbook;
185 public void setShareAddressbook (final Addressbook shareAddressbook) {
186 this.shareAddressbook = shareAddressbook;
190 public Calendar getShareCreated () {
191 return this.shareCreated;
195 public void setShareCreated (final Calendar shareCreated) {
196 this.shareCreated = shareCreated;
200 public Long getShareId () {
205 public void setShareId (final Long shareId) {
206 this.shareId = shareId;
210 public User getShareUserOwner () {
211 return this.shareUserOwner;
215 public void setShareUserOwner (final User shareUserOwner) {
216 this.shareUserOwner = shareUserOwner;
220 public User getShareUserSharee () {
221 return this.shareUserSharee;
225 public void setShareUserSharee (final User shareUserSharee) {
226 this.shareUserSharee = shareUserSharee;
230 public int hashCode () {
232 hash = 19 * hash + Objects.hashCode(this.getShareAddressbook());
233 hash = 19 * hash + Objects.hashCode(this.getShareUserOwner());
234 hash = 19 * hash + Objects.hashCode(this.getShareUserSharee());