2 * Copyright (C) 2016 - 2020 Free Software Foundation
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License as
6 * published by the Free Software Foundation, either version 3 of the
7 * License, or (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 Affero General Public License for more details.
14 * You should have received a copy of the GNU Affero General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.jcontacts.model.contact;
19 import java.text.MessageFormat;
20 import java.util.Date;
21 import java.util.Objects;
23 import javax.ejb.Stateless;
24 import org.mxchange.jcontacts.exceptions.ContactAlreadyAddedException;
25 import org.mxchange.jcontacts.exceptions.ContactNotFoundException;
28 * An administrative contact EJB
30 * @author Roland Häder<roland@mxchange.org>
32 @Stateless (name = "adminContact", description = "An administrative contact EJB")
33 public class AddressbookAdminContactSessionBean extends BaseAddressbookEnterpriseBean implements AdminContactSessionBeanRemote {
38 private static final long serialVersionUID = 542_145_347_916L;
41 * EJB for general contact purposes
43 @EJB (lookup = "java:global/jfinancials-ejb/contact!org.mxchange.jcontacts.model.contact.ContactSessionBeanRemote")
44 private ContactSessionBeanRemote contactBean;
49 public AddressbookAdminContactSessionBean () {
50 // Call super constructor
55 public Contact addContact (final Contact contact) throws ContactAlreadyAddedException {
57 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addContact: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
59 // Is the instance set?
60 if (null == contact) {
62 throw new NullPointerException("contact is null"); //NOI18N
63 } else if (contact.getContactId() != null) {
65 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} - is not null", contact.getContactId())); //NOI18N
66 } else if (this.isContactFound(contact)) {
68 throw new ContactAlreadyAddedException(contact);
71 // Set created timestamp
72 contact.setContactCreated(new Date());
74 // Set all created timestamps, if instance is there
75 this.setAllPhoneEntriesCreated(contact);
78 this.getEntityManager().persist(contact);
81 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addContact: contact.contactId={1} after persisting - EXIT!", this.getClass().getSimpleName(), contact.getContactId())); //NOI18N
88 public void deleteContactData (final Contact contact) throws ContactNotFoundException {
90 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteContactData: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
92 // Is the instance set?
93 if (null == contact) {
95 throw new NullPointerException("contact is null"); //NOI18N
96 } else if (contact.getContactId() == null) {
98 throw new NullPointerException("contact.contactId is null"); //NOI18N
99 } else if (contact.getContactId() < 1) {
101 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
102 } else if (!this.isContactFound(contact)) {
104 throw new ContactNotFoundException(contact.getContactId());
107 // Merge it to get a managed entity back
108 final Contact managedContact = this.getEntityManager().getReference(contact.getClass(), contact.getContactId());
110 // Remove it from database
111 this.getEntityManager().remove(managedContact);
114 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteContactData: EXIT!", this.getClass().getSimpleName())); //NOI18N
118 * Determines if the given contact is found
120 * @param contact Contact instance
122 * @return Whether the contact has been found
124 private boolean isContactFound (final Contact contact) {
126 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactRegistered: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
128 // Default is not found
129 boolean isFound = false;
131 // Fest all entries and iterate over them
132 for (final Contact currentContact : this.contactBean.allContacts()) {
134 if (Objects.equals(contact, currentContact)) {
135 // Yes, found the same
144 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactRegistered: isFound={1} - EXIT!", this.getClass().getSimpleName(), isFound)); //NOI18N