]> git.mxchange.org Git - addressbook-ejb.git/commitdiff
Continued:
authorRoland Haeder <roland@mxchange.org>
Wed, 14 Oct 2015 08:17:13 +0000 (10:17 +0200)
committerRoland Haeder <roland@mxchange.org>
Wed, 14 Oct 2015 08:17:13 +0000 (10:17 +0200)
- added remote session bean for sharing address books
- updated jar(s)
Signed-off-by:Roland Häder <roland@mxchange.org>

lib/jcoreee.jar
src/java/org/mxchange/addressbook/model/shared/SharedAddressbooksSessionBean.java [new file with mode: 0644]

index c2e022d69fedac84439d9796883f1931d015a012..4cdb134dfe55c2381d746baac342af402e2a4f80 100644 (file)
Binary files a/lib/jcoreee.jar and b/lib/jcoreee.jar differ
diff --git a/src/java/org/mxchange/addressbook/model/shared/SharedAddressbooksSessionBean.java b/src/java/org/mxchange/addressbook/model/shared/SharedAddressbooksSessionBean.java
new file mode 100644 (file)
index 0000000..6759f9a
--- /dev/null
@@ -0,0 +1,111 @@
+/*
+ * 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.model.shared;
+
+import java.text.MessageFormat;
+import java.util.List;
+import javax.ejb.Stateless;
+import javax.persistence.NoResultException;
+import javax.persistence.Query;
+import org.mxchange.addressbook.model.addressbook.shared.AddressbookShare;
+import org.mxchange.addressbook.model.addressbook.shared.ShareableAddressbook;
+import org.mxchange.jcoreee.database.BaseDatabaseBean;
+import org.mxchange.jusercore.model.user.User;
+
+/**
+ * A stateless bean for handling address book sharing
+ * <p>
+ * @author Roland Haeder
+ */
+@Stateless (name = "share", mappedName = "ejb/stateless-share", description = "A stateless bean for handling shared addressbooks")
+public class SharedAddressbooksSessionBean extends BaseDatabaseBean implements SharedAddressbooksSessionBeanRemote {
+
+       /**
+        * Serial number
+        */
+       private static final long serialVersionUID = 136_984_697_285_694_710L;
+
+       /**
+        * Shared address books. If you add another share to the database, please
+        * also update this list as some methods may prefer this over asking the JPA
+        * all over again.
+        */
+       private List<ShareableAddressbook> shares;
+
+       /**
+        * Default constructor
+        */
+       public SharedAddressbooksSessionBean () {
+               // Init shares list
+               this.shares = null;
+       }
+
+       @Override
+       @SuppressWarnings ("unchecked")
+       public boolean isUserSharingAddressbooks (final User user) {
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserSharingAddressbooks: user={0} - CALLED!", user)); //NOI18N
+
+               // Is user null?
+               if (null == user) {
+                       // Throw NPE
+                       throw new NullPointerException("user is null"); //NOI18N
+               } else if (user.getUserId() == null) {
+                       // Null userId is not allowed
+                       throw new NullPointerException("user.userId is null"); //NOI18N
+               } else if (user.getUserId() < 1) {
+                       // Not allowed value
+                       throw new IllegalArgumentException(MessageFormat.format("user.UserId={0} is an invalid value", user.getUserId())); //NOI18N
+               } else if (this.shares instanceof List) {
+                       // Trace message
+                       this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserSharingAddressbooks: this.shares.isEmpty()={0} - EXIT!", this.shares.isEmpty()));
+
+                       // Is already initialized
+                       return (!this.shares.isEmpty());
+               }
+
+               // Get named query
+               Query query = this.getEntityManager().createNamedQuery("SearchUserSharedAddressbooks", AddressbookShare.class); //NOI18N
+
+               // Set parameter
+               query.setParameter("user", user); //NOI18N
+
+               // Default is not sharing
+               boolean isSharing = false;
+
+               // Try it as an exception may be thrown
+               try {
+                       // Get results
+                       this.shares = query.getResultList();
+
+                       // Debug message
+                       this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserSharingAddressbooks: shares.size()={0}", this.shares.size()));
+
+                       // Is it not empty?
+                       isSharing = (!this.shares.isEmpty());
+               } catch (final NoResultException ex) {
+                       // Not sharing anything
+                       this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserSharingAddressbooks: User {0} is not sharing address books: {1}", user, ex)); //NOI18N
+               }
+
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserSharingAddressbooks: iSharing={0} - EXIT!", isSharing)); //NOI18N
+
+               // Return it
+               return isSharing;
+       }
+}