--- /dev/null
+/*
+ * Copyright (C) 2015 Roland Haeder
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.addressbook.exceptions;
+
+import java.text.MessageFormat;
+import org.mxchange.addressbook.model.addressbook.Addressbook;
+import org.mxchange.jusercore.model.user.User;
+
+/**
+ * An exception thrown whent the address book is already shared with given user
+ * <p>
+ * @author Roland Haeder
+ */
+public class UserAlreadySharingAddressbookException extends Exception {
+
+ /**
+ * Serial number
+ */
+ private static final long serialVersionUID = 58_528_177_571_976_034L;
+
+ /**
+ * Constructor with address book being already shared with sharee
+ * <p>
+ * @param addressbook Address book instance
+ * @param sharee User instance the address book is being shared with
+ */
+ public UserAlreadySharingAddressbookException (final Addressbook addressbook, final User sharee) {
+ super(MessageFormat.format("Address book id {0} owned by user {1} is already shared with user {2}", addressbook.getAddressbookId(), addressbook.getAddressbookUser().getUserId(), sharee.getUserId()));
+ }
+}
*/
package org.mxchange.addressbook.model.addressbook.shared;
+import java.text.MessageFormat;
import java.util.Objects;
import javax.persistence.CascadeType;
import javax.persistence.Column;
@OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.ALL, optional = false)
private User shareUserSharee;
+ /**
+ * Default constructor for entity manager
+ */
+ protected AddressbookShare () {
+ }
+
+ /**
+ * Constructor with address book and sharee instance. Both parameters must
+ * not be null, their id numbers must be set and the adress book's user
+ * instance must be set and have a valid id set.
+ * <p>
+ * @param addressbook Address book instance
+ * @param sharee User sharee instance
+ */
+ public AddressbookShare (final Addressbook addressbook, final User sharee) {
+ // Call protected 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 share) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
import java.io.Serializable;
import javax.ejb.Remote;
+import org.mxchange.addressbook.exceptions.UserAlreadySharingAddressbookException;
+import org.mxchange.addressbook.model.addressbook.Addressbook;
+import org.mxchange.addressbook.model.addressbook.shared.ShareableAddressbook;
import org.mxchange.jusercore.model.user.User;
/**
@Remote
public interface SharedAddressbooksSessionBeanRemote extends Serializable {
+ /**
+ * Starts an address book share between currently logged-in user and
+ * assigned user for current address book.
+ * <p>
+ * @param sharee User sharee instance
+ * @param addressbook Address book instance
+ * @return Updated share instance
+ * @throws
+ * org.mxchange.addressbook.exceptions.UserAlreadySharingAddressbookException
+ * When the user is already sharing the address book
+ */
+ ShareableAddressbook startSharing (final User sharee, final Addressbook addressbook) throws UserAlreadySharingAddressbookException;
+
/**
* Checks if the given user is sharing address books with others
* <p>