]> git.mxchange.org Git - addressbook-lib.git/blob - src/org/mxchange/addressbook/model/addressbook/shared/AddressbookShare.java
added named query for searching user's shares
[addressbook-lib.git] / src / org / mxchange / addressbook / model / addressbook / shared / AddressbookShare.java
1 /*
2  * Copyright (C) 2015 Roland Haeder
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.addressbook.model.addressbook.shared;
18
19 import java.util.Objects;
20 import javax.persistence.CascadeType;
21 import javax.persistence.Column;
22 import javax.persistence.Entity;
23 import javax.persistence.GeneratedValue;
24 import javax.persistence.GenerationType;
25 import javax.persistence.Id;
26 import javax.persistence.JoinColumn;
27 import javax.persistence.NamedQueries;
28 import javax.persistence.NamedQuery;
29 import javax.persistence.OneToOne;
30 import javax.persistence.Table;
31 import org.mxchange.addressbook.model.addressbook.Addressbook;
32 import org.mxchange.addressbook.model.addressbook.UserAddressbook;
33 import org.mxchange.jusercore.model.user.LoginUser;
34 import org.mxchange.jusercore.model.user.User;
35
36 /**
37  * A POJO for sharing address books with other users
38  * <p>
39  * @author Roland Haeder
40  */
41 @Entity (name = "addressbook_shares")
42 @Table (name = "addressbook_shares")
43 @NamedQueries (
44                 @NamedQuery (name = "SearchUserSharedAddressbooks", query = "SELECT s FROM addressbook_shares AS s WHERE s.shareUserOwner = :user ORDER BY s.shareId ASC")
45 )
46 public class AddressbookShare implements ShareableAddressbook, Comparable<ShareableAddressbook> {
47
48         /**
49          * Serial number
50          */
51         private static final long serialVersionUID = 167_889_678_177_691_690L;
52
53         /**
54          * Id number
55          */
56         @Id
57         @GeneratedValue (strategy = GenerationType.IDENTITY)
58         @Column (name = "share_id", length = 20, nullable = false, updatable = false)
59         private Long shareId;
60
61         /**
62          * Address book this share is for
63          */
64         @JoinColumn (name = "share_addressbook_id", nullable = false, updatable = false)
65         @OneToOne (targetEntity = UserAddressbook.class, cascade = CascadeType.ALL, optional = false)
66         private Addressbook shareAddressbook;
67
68         /**
69          * User who is giving the share (for his/her address book)
70          */
71         @JoinColumn (name = "share_owner_id", nullable = false, updatable = false)
72         @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.ALL, optional = false)
73         private User shareUserOwner;
74
75         /**
76          * User the address book is shared with
77          */
78         @JoinColumn (name = "share_sharee_id", nullable = false, updatable = false)
79         @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.ALL, optional = false)
80         private User shareUserSharee;
81
82         @Override
83         public int compareTo (final ShareableAddressbook share) {
84                 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
85         }
86
87         @Override
88         public boolean equals (final Object object) {
89                 if (object == null) {
90                         return false;
91                 } else if (getClass() != object.getClass()) {
92                         return false;
93                 }
94
95                 final ShareableAddressbook other = (ShareableAddressbook) object;
96
97                 if (!Objects.equals(this.getShareAddressbook(), other.getShareAddressbook())) {
98                         return false;
99                 } else if (!Objects.equals(this.getShareUserOwner(), other.getShareUserOwner())) {
100                         return false;
101                 } else if (!Objects.equals(this.getShareUserOwner(), other.getShareUserOwner())) {
102                         return false;
103                 }
104
105                 return Objects.equals(this.getShareUserSharee(), other.getShareUserSharee());
106         }
107
108         @Override
109         public Addressbook getShareAddressbook () {
110                 return this.shareAddressbook;
111         }
112
113         @Override
114         public void setShareAddressbook (final Addressbook shareAddressbook) {
115                 this.shareAddressbook = shareAddressbook;
116         }
117
118         @Override
119         public Long getShareId () {
120                 return this.shareId;
121         }
122
123         @Override
124         public void setShareId (final Long shareId) {
125                 this.shareId = shareId;
126         }
127
128         @Override
129         public User getShareUserOwner () {
130                 return this.shareUserOwner;
131         }
132
133         @Override
134         public void setShareUserOwner (final User shareUserOwner) {
135                 this.shareUserOwner = shareUserOwner;
136         }
137
138         @Override
139         public User getShareUserSharee () {
140                 return this.shareUserSharee;
141         }
142
143         @Override
144         public void setShareUserSharee (final User shareUserSharee) {
145                 this.shareUserSharee = shareUserSharee;
146         }
147
148         @Override
149         public int hashCode () {
150                 int hash = 7;
151                 hash = 19 * hash + Objects.hashCode(this.getShareAddressbook());
152                 hash = 19 * hash + Objects.hashCode(this.getShareUserOwner());
153                 hash = 19 * hash + Objects.hashCode(this.getShareUserSharee());
154                 return hash;
155         }
156 }