From: Roland Häder Date: Sun, 22 Jan 2023 05:36:49 +0000 (+0100) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=ba4b9c17fba0ec883d197516d4b3d3260e8bdb96;p=jaddressbook-share-core.git Continued: - moved entity class and interface into one lower package - event classes' constructors now check parameter (not null, valid primary keys) --- diff --git a/src/org/mxchange/jaddressbookshare/events/sharing/EndedAddressbookSharingEvent.java b/src/org/mxchange/jaddressbookshare/events/sharing/EndedAddressbookSharingEvent.java index cde4094..54eacf9 100644 --- a/src/org/mxchange/jaddressbookshare/events/sharing/EndedAddressbookSharingEvent.java +++ b/src/org/mxchange/jaddressbookshare/events/sharing/EndedAddressbookSharingEvent.java @@ -16,8 +16,9 @@ */ package org.mxchange.jaddressbookshare.events.sharing; +import java.text.MessageFormat; import org.mxchange.jaddressbookshare.events.sharing.type.SharingType; -import org.mxchange.jaddressbookshare.model.addressbook.shared.ShareableAddressbook; +import org.mxchange.jaddressbookshare.model.shared.ShareableAddressbook; /** * An event fired when a user ends sharing address books @@ -42,6 +43,46 @@ public class EndedAddressbookSharingEvent implements ObservableAddressbookSharin * @param share Address book share entry */ public EndedAddressbookSharingEvent (final ShareableAddressbook share) { + // Check parameter + if (null == share) { + // Throw NPE + throw new NullPointerException("Parameter 'share' is null"); //NOI18N + } else if (share.getShareId() == null) { + // Throw NPE again + throw new NullPointerException("share.shareId is null"); //NOI18N + } else if (share.getShareId() < 1) { + // Throw IAE + throw new IllegalArgumentException(MessageFormat.format("share.shareId={0} is invalid", share.getShareId())); //NOI18N + } else if (share.getShareAddressbook() == null) { + // Throw NPE again + throw new NullPointerException("share.shareAddressbook is null"); //NOI18N + } else if (share.getShareAddressbook().getAddressbookId() == null) { + // ... and again + throw new NullPointerException("share.shareAddressbook.addressbookId is null"); //NOI18N + } else if (share.getShareAddressbook().getAddressbookId() < 1) { + // Throw IAE + throw new IllegalArgumentException(MessageFormat.format("share.shareAddressbook.addressbookId={0} is invalid", share.getShareAddressbook().getAddressbookId())); //NOI18N + } else if (share.getShareUserOwner() == null) { + // Throw NPE again + throw new NullPointerException("share.shareUserOwner is null"); //NOI18N + } else if (share.getShareUserOwner().getUserId() == null) { + // ... and again + throw new NullPointerException("share.shareUserOwner.userId is null"); //NOI18N + } else if (share.getShareUserOwner().getUserId() < 1) { + // Throw IAE + throw new IllegalArgumentException(MessageFormat.format("share.shareUserOwner.userId={0} is invalid", share.getShareUserOwner().getUserId())); //NOI18N + } else if (share.getShareUserSharee() == null) { + // Throw NPE again + throw new NullPointerException("share.shareUserSharee is null"); //NOI18N + } else if (share.getShareUserSharee().getUserId() == null) { + // ... and again + throw new NullPointerException("share.shareUserSharee.userId is null"); //NOI18N + } else if (share.getShareUserSharee().getUserId() < 1) { + // Throw IAE + throw new IllegalArgumentException(MessageFormat.format("share.shareUserSharee.userId={0} is invalid", share.getShareUserSharee().getUserId())); //NOI18N + } + + // Set it in class this.shareableAddressbook = share; } diff --git a/src/org/mxchange/jaddressbookshare/events/sharing/ObservableAddressbookSharingEvent.java b/src/org/mxchange/jaddressbookshare/events/sharing/ObservableAddressbookSharingEvent.java index db8931b..8fb98fa 100644 --- a/src/org/mxchange/jaddressbookshare/events/sharing/ObservableAddressbookSharingEvent.java +++ b/src/org/mxchange/jaddressbookshare/events/sharing/ObservableAddressbookSharingEvent.java @@ -18,7 +18,7 @@ package org.mxchange.jaddressbookshare.events.sharing; import java.io.Serializable; import org.mxchange.jaddressbookshare.events.sharing.type.SharingType; -import org.mxchange.jaddressbookshare.model.addressbook.shared.ShareableAddressbook; +import org.mxchange.jaddressbookshare.model.shared.ShareableAddressbook; /** * An interface for address book sharing events @@ -35,9 +35,10 @@ public interface ObservableAddressbookSharingEvent extends Serializable { ShareableAddressbook getShareableAddressbook (); /** - * Getter for sharing type enum + * Getter for sharing type enumeration *

- * @return Sharing type enum + * @return Sharing type enumeration */ SharingType getSharingType (); + } diff --git a/src/org/mxchange/jaddressbookshare/events/sharing/StartedAddressbookSharingEvent.java b/src/org/mxchange/jaddressbookshare/events/sharing/StartedAddressbookSharingEvent.java index 8748130..0b0419f 100644 --- a/src/org/mxchange/jaddressbookshare/events/sharing/StartedAddressbookSharingEvent.java +++ b/src/org/mxchange/jaddressbookshare/events/sharing/StartedAddressbookSharingEvent.java @@ -16,8 +16,9 @@ */ package org.mxchange.jaddressbookshare.events.sharing; +import java.text.MessageFormat; import org.mxchange.jaddressbookshare.events.sharing.type.SharingType; -import org.mxchange.jaddressbookshare.model.addressbook.shared.ShareableAddressbook; +import org.mxchange.jaddressbookshare.model.shared.ShareableAddressbook; /** * An event fired when a user starts sharing address books @@ -29,7 +30,7 @@ public class StartedAddressbookSharingEvent implements ObservableAddressbookShar /** * Serial number */ - private static final long serialVersionUID = 152_896_748_186_018_497L; + private static final long serialVersionUID = 152_896_748_186_018_498L; /** * Shared address book entry @@ -42,6 +43,46 @@ public class StartedAddressbookSharingEvent implements ObservableAddressbookShar * @param share Address book share entry */ public StartedAddressbookSharingEvent (final ShareableAddressbook share) { + // Check parameter + if (null == share) { + // Throw NPE + throw new NullPointerException("Parameter 'share' is null"); //NOI18N + } else if (share.getShareId() == null) { + // Throw NPE again + throw new NullPointerException("share.shareId is null"); //NOI18N + } else if (share.getShareId() < 1) { + // Throw IAE + throw new IllegalArgumentException(MessageFormat.format("share.shareId={0} is invalid", share.getShareId())); //NOI18N + } else if (share.getShareAddressbook() == null) { + // Throw NPE again + throw new NullPointerException("share.shareAddressbook is null"); //NOI18N + } else if (share.getShareAddressbook().getAddressbookId() == null) { + // ... and again + throw new NullPointerException("share.shareAddressbook.addressbookId is null"); //NOI18N + } else if (share.getShareAddressbook().getAddressbookId() < 1) { + // Throw IAE + throw new IllegalArgumentException(MessageFormat.format("share.shareAddressbook.addressbookId={0} is invalid", share.getShareAddressbook().getAddressbookId())); //NOI18N + } else if (share.getShareUserOwner() == null) { + // Throw NPE again + throw new NullPointerException("share.shareUserOwner is null"); //NOI18N + } else if (share.getShareUserOwner().getUserId() == null) { + // ... and again + throw new NullPointerException("share.shareUserOwner.userId is null"); //NOI18N + } else if (share.getShareUserOwner().getUserId() < 1) { + // Throw IAE + throw new IllegalArgumentException(MessageFormat.format("share.shareUserOwner.userId={0} is invalid", share.getShareUserOwner().getUserId())); //NOI18N + } else if (share.getShareUserSharee() == null) { + // Throw NPE again + throw new NullPointerException("share.shareUserSharee is null"); //NOI18N + } else if (share.getShareUserSharee().getUserId() == null) { + // ... and again + throw new NullPointerException("share.shareUserSharee.userId is null"); //NOI18N + } else if (share.getShareUserSharee().getUserId() < 1) { + // Throw IAE + throw new IllegalArgumentException(MessageFormat.format("share.shareUserSharee.userId={0} is invalid", share.getShareUserSharee().getUserId())); //NOI18N + } + + // Set it in class this.shareableAddressbook = share; } diff --git a/src/org/mxchange/jaddressbookshare/events/sharing/type/SharingType.java b/src/org/mxchange/jaddressbookshare/events/sharing/type/SharingType.java index 580209b..f1b7bbb 100644 --- a/src/org/mxchange/jaddressbookshare/events/sharing/type/SharingType.java +++ b/src/org/mxchange/jaddressbookshare/events/sharing/type/SharingType.java @@ -16,18 +16,19 @@ */ package org.mxchange.jaddressbookshare.events.sharing.type; +import java.io.Serializable; + /** * An enumeration for sharing types *

* @author Roland Häder */ -public enum SharingType { +public enum SharingType implements Serializable { /** * Sharing has been started */ STARTED, - /** * Sharing has been ended */ diff --git a/src/org/mxchange/jaddressbookshare/model/addressbook/shared/AddressbookShare.java b/src/org/mxchange/jaddressbookshare/model/addressbook/shared/AddressbookShare.java deleted file mode 100644 index fc23f0c..0000000 --- a/src/org/mxchange/jaddressbookshare/model/addressbook/shared/AddressbookShare.java +++ /dev/null @@ -1,289 +0,0 @@ -/* - * Copyright (C) 2016 - 2022 Free Software Foundation - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.mxchange.jaddressbookshare.model.addressbook.shared; - -import java.text.MessageFormat; -import java.util.Date; -import java.util.Objects; -import javax.persistence.Basic; -import javax.persistence.CascadeType; -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.JoinColumn; -import javax.persistence.NamedQueries; -import javax.persistence.NamedQuery; -import javax.persistence.OneToOne; -import javax.persistence.Table; -import javax.persistence.Temporal; -import javax.persistence.TemporalType; -import javax.persistence.Transient; -import org.mxchange.jaddressbook.model.addressbook.Addressbook; -import org.mxchange.jaddressbook.model.addressbook.UserAddressbook; -import org.mxchange.jcoreutils.comparable.ComparableUtils; -import org.mxchange.jusercore.model.user.LoginUser; -import org.mxchange.jusercore.model.user.User; - -/** - * A POJO for sharing address books with other users - *

- * @author Roland Häder - */ -@Entity (name = "addressbook_shares") -@Table (name = "addressbook_shares") -@NamedQueries ( - { - @NamedQuery ( - name = "SearchUserSharedAddressbooks", - query = "SELECT s FROM addressbook_shares AS s WHERE s.shareUserOwner = :user ORDER BY s.shareId ASC" - ), - @NamedQuery ( - name = "SearchShareeAddressbookShare", - query = "SELECT s FROM addressbook_shares AS s WHERE s.shareAddressbook = :addressbook AND s.shareUserSharee = :sharee" - ) - } -) -@SuppressWarnings ("PersistenceUnitPresent") -public class AddressbookShare implements ShareableAddressbook { - - /** - * Serial number - */ - @Transient - private static final long serialVersionUID = 167_889_678_177_691_690L; - - /** - * Address book this share is for - */ - @JoinColumn (name = "share_addressbook_id", nullable = false, updatable = false) - @OneToOne (targetEntity = UserAddressbook.class, cascade = CascadeType.REFRESH, optional = false) - private Addressbook shareAddressbook; - - /** - * When this share has been created - */ - @Basic (optional = false) - @Temporal (TemporalType.TIMESTAMP) - @Column (name = "share_entry_created", updatable = false, nullable = false) - private Date shareEntryCreated; - - /** - * When this share has been updated - */ - @Temporal (TemporalType.TIMESTAMP) - @Column (name = "share_entry_updated", insertable = false, nullable = false) - private Date shareEntryUpdated; - - /** - * Id number - */ - @Id - @GeneratedValue (strategy = GenerationType.IDENTITY) - @Column (name = "share_id", nullable = false, updatable = false) - private Long shareId; - - /** - * User who is owning the share - */ - @JoinColumn (name = "share_owner_id", nullable = false, updatable = false) - @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.REFRESH, optional = false) - private User shareUserOwner; - - /** - * User the address book is shared with - */ - @JoinColumn (name = "share_sharee_id", nullable = false, updatable = false) - @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.REFRESH, optional = false) - private User shareUserSharee; - - /** - * Default constructor - */ - public AddressbookShare () { - } - - /** - * Constructor with address book and sharee instance. Both parameters must - * not be null, their id numbers must be set and the address book's user - * instance must be set and have a valid id set. - *

- * @param addressbook Address book instance - * @param sharee User sharee instance - */ - public AddressbookShare (final Addressbook addressbook, final User sharee) { - // Invoke default constructor - this(); - - // Check all conditions - if (null == sharee) { - // Throw NPE - throw new NullPointerException("sharee is null"); //NOI18N - } else if (sharee.getUserId() == null) { - // Throw NPE again - throw new NullPointerException("sharee.userId is null"); //NOI18N - } else if (sharee.getUserId() < 1) { - // Invalid id number - throw new IllegalStateException(MessageFormat.format("sharee.userId={0} is invalid", sharee.getUserId())); //NOI18N - } else if (null == addressbook) { - // Throw NPE again - throw new NullPointerException("addressbook is null"); //NOI18N - } else if (addressbook.getAddressbookId() == null) { - // Throw NPE again - throw new NullPointerException("addressbook.addressbookId is null"); //NOI18N - } else if (addressbook.getAddressbookId() < 1) { - // Invalid id number - throw new IllegalArgumentException(MessageFormat.format("addressbook.addressbookId={0} is invalid.", addressbook.getAddressbookId())); //NOI18N - } else if (Objects.equals(addressbook.getAddressbookUser(), sharee)) { - // Sharing with yourself! - throw new IllegalStateException("User tries to share with himself."); //NOI18N - } - - // Set all instances - this.shareAddressbook = addressbook; - this.shareUserOwner = addressbook.getAddressbookUser(); - this.shareUserSharee = sharee; - } - - @Override - public int compareTo (final ShareableAddressbook shareableAddressbook) { - // Checkparameter and return 0 if equal - if (null == shareableAddressbook) { - // Should not happen - throw new NullPointerException("shareableAddressbook is null"); //NOI18N - } else if (shareableAddressbook.equals(this)) { - // Same object - return 0; - } - - // All comparators - final int comparators[] = { - // First address book - this.getShareAddressbook().compareTo(shareableAddressbook.getShareAddressbook()), - // ... next sharer - this.getShareUserOwner().compareTo(shareableAddressbook.getShareUserOwner()), - // ... next sharee - this.getShareUserSharee().compareTo(shareableAddressbook.getShareUserSharee()),}; - - // Check all values - final int comparison = ComparableUtils.checkAll(comparators); - - // Return value - return comparison; - } - - @Override - public boolean equals (final Object object) { - if (null == object) { - return false; - } else if (this.getClass() != object.getClass()) { - return false; - } - - final ShareableAddressbook shared = (ShareableAddressbook) object; - - if (!Objects.equals(this.getShareAddressbook(), shared.getShareAddressbook())) { - return false; - } else if (!Objects.equals(this.getShareId(), shared.getShareId())) { - return false; - } else if (!Objects.equals(this.getShareUserOwner(), shared.getShareUserOwner())) { - return false; - } else if (!Objects.equals(this.getShareUserSharee(), shared.getShareUserSharee())) { - return false; - } - - return Objects.equals(this.getShareUserSharee(), shared.getShareUserSharee()); - } - - @Override - public Addressbook getShareAddressbook () { - return this.shareAddressbook; - } - - @Override - public void setShareAddressbook (final Addressbook shareAddressbook) { - this.shareAddressbook = shareAddressbook; - } - - @Override - @SuppressWarnings ("ReturnOfDateField") - public Date getShareEntryCreated () { - return this.shareEntryCreated; - } - - @Override - @SuppressWarnings ("AssignmentToDateFieldFromParameter") - public void setShareEntryCreated (final Date shareEntryCreated) { - this.shareEntryCreated = shareEntryCreated; - } - - @Override - @SuppressWarnings ("ReturnOfDateField") - public Date getShareEntryUpdated () { - return this.shareEntryUpdated; - } - - @Override - @SuppressWarnings ("AssignmentToDateFieldFromParameter") - public void setShareEntryUpdated (final Date shareEntryUpdated) { - this.shareEntryUpdated = shareEntryUpdated; - } - - @Override - public Long getShareId () { - return this.shareId; - } - - @Override - public void setShareId (final Long shareId) { - this.shareId = shareId; - } - - @Override - public User getShareUserOwner () { - return this.shareUserOwner; - } - - @Override - public void setShareUserOwner (final User shareUserOwner) { - this.shareUserOwner = shareUserOwner; - } - - @Override - public User getShareUserSharee () { - return this.shareUserSharee; - } - - @Override - public void setShareUserSharee (final User shareUserSharee) { - this.shareUserSharee = shareUserSharee; - } - - @Override - public int hashCode () { - int hash = 7; - - hash = 19 * hash + Objects.hashCode(this.getShareAddressbook()); - hash = 19 * hash + Objects.hashCode(this.getShareId()); - hash = 19 * hash + Objects.hashCode(this.getShareUserOwner()); - hash = 19 * hash + Objects.hashCode(this.getShareUserSharee()); - - return hash; - } - -} diff --git a/src/org/mxchange/jaddressbookshare/model/addressbook/shared/ShareableAddressbook.java b/src/org/mxchange/jaddressbookshare/model/addressbook/shared/ShareableAddressbook.java deleted file mode 100644 index 6e74ac1..0000000 --- a/src/org/mxchange/jaddressbookshare/model/addressbook/shared/ShareableAddressbook.java +++ /dev/null @@ -1,120 +0,0 @@ -/* - * Copyright (C) 2016 - 2022 Free Software Foundation - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.mxchange.jaddressbookshare.model.addressbook.shared; - -import java.io.Serializable; -import java.util.Date; -import org.mxchange.jaddressbook.model.addressbook.Addressbook; -import org.mxchange.jusercore.model.user.User; - -/** - * A POJI for shared address books - *

- * @author Roland Häder - */ -public interface ShareableAddressbook extends Comparable, Serializable { - - /** - * Getter for shared address book instance - *

- * @return Shared address book instance - */ - Addressbook getShareAddressbook (); - - /** - * Setter for shared address book instance - *

- * @param shareAddressbook Shared address book instance - */ - void setShareAddressbook (final Addressbook shareAddressbook); - - /** - * Getter for id number - *

- * @return Id number - */ - Long getShareId (); - - /** - * Setter for id number - *

- * @param shareId Id number - */ - void setShareId (final Long shareId); - - /** - * Getter for address book owner instance - *

- * @return Address book owner instance - */ - User getShareUserOwner (); - - /** - * Setter for address book owner instance - *

- * @param shareUserOwner Address book owner instance - */ - void setShareUserOwner (final User shareUserOwner); - - /** - * Getter for address book sharee instance - *

- * @return Address book sharee instance - */ - User getShareUserSharee (); - - /** - * Setter for address book sharee instance - *

- * @param shareUserSharer Address book sharee instance - */ - void setShareUserSharee (final User shareUserSharer); - - /** - * Getter for share created timestamp - *

- * @return Share created timestamp - */ - Date getShareEntryCreated (); - - /** - * Setter for share created timestamp - *

- * @param shareCreated Share created timestamp - */ - void setShareEntryCreated (final Date shareCreated); - - /** - * Getter for share updated timestamp - *

- * @return Share updated timestamp - */ - Date getShareEntryUpdated (); - - /** - * Setter for share updated timestamp - *

- * @param shareUpdated Share updated timestamp - */ - void setShareEntryUpdated (final Date shareUpdated); - - @Override - boolean equals (final Object object); - - @Override - int hashCode (); -} diff --git a/src/org/mxchange/jaddressbookshare/model/shared/AddressbookShare.java b/src/org/mxchange/jaddressbookshare/model/shared/AddressbookShare.java new file mode 100644 index 0000000..e0c903d --- /dev/null +++ b/src/org/mxchange/jaddressbookshare/model/shared/AddressbookShare.java @@ -0,0 +1,290 @@ +/* + * Copyright (C) 2016 - 2022 Free Software Foundation + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.jaddressbookshare.model.shared; + +import java.text.MessageFormat; +import java.util.Date; +import java.util.Objects; +import javax.persistence.Basic; +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.NamedQueries; +import javax.persistence.NamedQuery; +import javax.persistence.OneToOne; +import javax.persistence.Table; +import javax.persistence.Temporal; +import javax.persistence.TemporalType; +import javax.persistence.Transient; +import org.mxchange.jaddressbook.model.addressbook.Addressbook; +import org.mxchange.jaddressbook.model.addressbook.UserAddressbook; +import org.mxchange.jcoreutils.comparable.ComparableUtils; +import org.mxchange.jusercore.model.user.LoginUser; +import org.mxchange.jusercore.model.user.User; + +/** + * A POJO for sharing address books with other users + *

+ * @author Roland Häder + */ +@Entity (name = "addressbook_shares") +@Table (name = "addressbook_shares") +@NamedQueries ( + { + @NamedQuery ( + name = "SearchUserSharedAddressbooks", + query = "SELECT s FROM addressbook_shares AS s WHERE s.shareUserOwner = :user ORDER BY s.shareId ASC" + ), + @NamedQuery ( + name = "SearchShareeAddressbookShare", + query = "SELECT s FROM addressbook_shares AS s WHERE s.shareAddressbook = :addressbook AND s.shareUserSharee = :sharee" + ) + } +) +@SuppressWarnings ("PersistenceUnitPresent") +public class AddressbookShare implements ShareableAddressbook { + + /** + * Serial number + */ + @Transient + private static final long serialVersionUID = 167_889_678_177_691_690L; + + /** + * Address book this share is for + */ + @JoinColumn (name = "share_addressbook_id", nullable = false, updatable = false) + @OneToOne (targetEntity = UserAddressbook.class, cascade = CascadeType.REFRESH, optional = false) + private Addressbook shareAddressbook; + + /** + * When this share has been created + */ + @Basic (optional = false) + @Temporal (TemporalType.TIMESTAMP) + @Column (name = "share_entry_created", updatable = false, nullable = false) + private Date shareEntryCreated; + + /** + * When this share has been updated + */ + @Temporal (TemporalType.TIMESTAMP) + @Column (name = "share_entry_updated", insertable = false, nullable = false) + private Date shareEntryUpdated; + + /** + * Id number + */ + @Id + @GeneratedValue (strategy = GenerationType.IDENTITY) + @Column (name = "share_id", nullable = false, updatable = false) + private Long shareId; + + /** + * User who is owning the share + */ + @JoinColumn (name = "share_owner_id", nullable = false, updatable = false) + @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.REFRESH, optional = false) + private User shareUserOwner; + + /** + * User the address book is shared with + */ + @JoinColumn (name = "share_sharee_id", nullable = false, updatable = false) + @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.REFRESH, optional = false) + private User shareUserSharee; + + /** + * Default constructor + */ + public AddressbookShare () { + } + + /** + * Constructor with address book and sharee instance. Both parameters must + * not be null, their id numbers must be set and the address book's user + * instance must be set and have a valid id set. + *

+ * @param addressbook Address book instance + * @param sharee User sharee instance + */ + public AddressbookShare (final Addressbook addressbook, final User sharee) { + // Invoke default constructor + this(); + + // Check all conditions + if (null == sharee) { + // Throw NPE + throw new NullPointerException("sharee is null"); //NOI18N + } else if (sharee.getUserId() == null) { + // Throw NPE again + throw new NullPointerException("sharee.userId is null"); //NOI18N + } else if (sharee.getUserId() < 1) { + // Invalid id number + throw new IllegalStateException(MessageFormat.format("sharee.userId={0} is invalid", sharee.getUserId())); //NOI18N + } else if (null == addressbook) { + // Throw NPE again + throw new NullPointerException("addressbook is null"); //NOI18N + } else if (addressbook.getAddressbookId() == null) { + // Throw NPE again + throw new NullPointerException("addressbook.addressbookId is null"); //NOI18N + } else if (addressbook.getAddressbookId() < 1) { + // Invalid id number + throw new IllegalArgumentException(MessageFormat.format("addressbook.addressbookId={0} is invalid.", addressbook.getAddressbookId())); //NOI18N + } else if (Objects.equals(addressbook.getAddressbookUser(), sharee)) { + // Sharing with yourself! + throw new IllegalStateException("User tries to share with himself."); //NOI18N + } + + // Set all instances + this.shareAddressbook = addressbook; + this.shareUserOwner = addressbook.getAddressbookUser(); + this.shareUserSharee = sharee; + } + + @Override + public int compareTo (final ShareableAddressbook shareableAddressbook) { + // Checkparameter and return 0 if equal + if (null == shareableAddressbook) { + // Should not happen + throw new NullPointerException("shareableAddressbook is null"); //NOI18N + } else if (shareableAddressbook.equals(this)) { + // Same object + return 0; + } + + // All comparators + final int comparators[] = { + // First address book + this.getShareAddressbook().compareTo(shareableAddressbook.getShareAddressbook()), + // ... next sharer + this.getShareUserOwner().compareTo(shareableAddressbook.getShareUserOwner()), + // ... next sharee + this.getShareUserSharee().compareTo(shareableAddressbook.getShareUserSharee()) + }; + + // Check all values + final int comparison = ComparableUtils.checkAll(comparators); + + // Return value + return comparison; + } + + @Override + public boolean equals (final Object object) { + if (null == object) { + return false; + } else if (this.getClass() != object.getClass()) { + return false; + } + + final ShareableAddressbook shared = (ShareableAddressbook) object; + + if (!Objects.equals(this.getShareAddressbook(), shared.getShareAddressbook())) { + return false; + } else if (!Objects.equals(this.getShareId(), shared.getShareId())) { + return false; + } else if (!Objects.equals(this.getShareUserOwner(), shared.getShareUserOwner())) { + return false; + } else if (!Objects.equals(this.getShareUserSharee(), shared.getShareUserSharee())) { + return false; + } + + return Objects.equals(this.getShareUserSharee(), shared.getShareUserSharee()); + } + + @Override + public Addressbook getShareAddressbook () { + return this.shareAddressbook; + } + + @Override + public void setShareAddressbook (final Addressbook shareAddressbook) { + this.shareAddressbook = shareAddressbook; + } + + @Override + @SuppressWarnings ("ReturnOfDateField") + public Date getShareEntryCreated () { + return this.shareEntryCreated; + } + + @Override + @SuppressWarnings ("AssignmentToDateFieldFromParameter") + public void setShareEntryCreated (final Date shareEntryCreated) { + this.shareEntryCreated = shareEntryCreated; + } + + @Override + @SuppressWarnings ("ReturnOfDateField") + public Date getShareEntryUpdated () { + return this.shareEntryUpdated; + } + + @Override + @SuppressWarnings ("AssignmentToDateFieldFromParameter") + public void setShareEntryUpdated (final Date shareEntryUpdated) { + this.shareEntryUpdated = shareEntryUpdated; + } + + @Override + public Long getShareId () { + return this.shareId; + } + + @Override + public void setShareId (final Long shareId) { + this.shareId = shareId; + } + + @Override + public User getShareUserOwner () { + return this.shareUserOwner; + } + + @Override + public void setShareUserOwner (final User shareUserOwner) { + this.shareUserOwner = shareUserOwner; + } + + @Override + public User getShareUserSharee () { + return this.shareUserSharee; + } + + @Override + public void setShareUserSharee (final User shareUserSharee) { + this.shareUserSharee = shareUserSharee; + } + + @Override + public int hashCode () { + int hash = 7; + + hash = 19 * hash + Objects.hashCode(this.getShareAddressbook()); + hash = 19 * hash + Objects.hashCode(this.getShareId()); + hash = 19 * hash + Objects.hashCode(this.getShareUserOwner()); + hash = 19 * hash + Objects.hashCode(this.getShareUserSharee()); + + return hash; + } + +} diff --git a/src/org/mxchange/jaddressbookshare/model/shared/ShareableAddressbook.java b/src/org/mxchange/jaddressbookshare/model/shared/ShareableAddressbook.java new file mode 100644 index 0000000..1ad7eb7 --- /dev/null +++ b/src/org/mxchange/jaddressbookshare/model/shared/ShareableAddressbook.java @@ -0,0 +1,120 @@ +/* + * Copyright (C) 2016 - 2022 Free Software Foundation + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.jaddressbookshare.model.shared; + +import java.io.Serializable; +import java.util.Date; +import org.mxchange.jaddressbook.model.addressbook.Addressbook; +import org.mxchange.jusercore.model.user.User; + +/** + * A POJI for shared address books + *

+ * @author Roland Häder + */ +public interface ShareableAddressbook extends Comparable, Serializable { + + /** + * Getter for shared address book instance + *

+ * @return Shared address book instance + */ + Addressbook getShareAddressbook (); + + /** + * Setter for shared address book instance + *

+ * @param shareAddressbook Shared address book instance + */ + void setShareAddressbook (final Addressbook shareAddressbook); + + /** + * Getter for id number + *

+ * @return Id number + */ + Long getShareId (); + + /** + * Setter for id number + *

+ * @param shareId Id number + */ + void setShareId (final Long shareId); + + /** + * Getter for address book owner instance + *

+ * @return Address book owner instance + */ + User getShareUserOwner (); + + /** + * Setter for address book owner instance + *

+ * @param shareUserOwner Address book owner instance + */ + void setShareUserOwner (final User shareUserOwner); + + /** + * Getter for address book sharee instance + *

+ * @return Address book sharee instance + */ + User getShareUserSharee (); + + /** + * Setter for address book sharee instance + *

+ * @param shareUserSharer Address book sharee instance + */ + void setShareUserSharee (final User shareUserSharer); + + /** + * Getter for share created timestamp + *

+ * @return Share created timestamp + */ + Date getShareEntryCreated (); + + /** + * Setter for share created timestamp + *

+ * @param shareCreated Share created timestamp + */ + void setShareEntryCreated (final Date shareCreated); + + /** + * Getter for share updated timestamp + *

+ * @return Share updated timestamp + */ + Date getShareEntryUpdated (); + + /** + * Setter for share updated timestamp + *

+ * @param shareUpdated Share updated timestamp + */ + void setShareEntryUpdated (final Date shareUpdated); + + @Override + boolean equals (final Object object); + + @Override + int hashCode (); +}