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;
26 import org.mxchange.jjobs.enterprise.BaseJobsEnterpriseBean;
29 * An administrative contact EJB
31 * @author Roland Häder<roland@mxchange.org>
33 @Stateless (name = "adminContact", description = "An administrative contact EJB")
34 public class JobsAdminContactSessionBean extends BaseJobsEnterpriseBean implements AdminContactSessionBeanRemote {
39 private static final long serialVersionUID = 542_145_347_916L;
42 * EJB for general contact purposes
44 @EJB (lookup = "java:global/jfinancials-ejb/contact!org.mxchange.jcontacts.model.contact.ContactSessionBeanRemote")
45 private ContactSessionBeanRemote contactBean;
50 public JobsAdminContactSessionBean () {
51 // Call super constructor
56 public Contact addContact (final Contact contact) throws ContactAlreadyAddedException {
58 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addContact: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
60 // Is the instance set?
61 if (null == contact) {
63 throw new NullPointerException("contact is null"); //NOI18N
64 } else if (contact.getContactId() != null) {
66 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} - is not null", contact.getContactId())); //NOI18N
67 } else if (this.isContactFound(contact)) {
69 throw new ContactAlreadyAddedException(contact);
72 // Set created timestamp
73 contact.setContactEntryCreated(new Date());
75 // Set all created timestamps, if instance is there
76 this.setAllPhoneEntriesCreated(contact);
79 this.getEntityManager().persist(contact);
82 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addContact: contact.contactId={1} after persisting - EXIT!", this.getClass().getSimpleName(), contact.getContactId())); //NOI18N
89 public void deleteContactData (final Contact contact) throws ContactNotFoundException {
91 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteContactData: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
93 // Is the instance set?
94 if (null == contact) {
96 throw new NullPointerException("contact is null"); //NOI18N
97 } else if (contact.getContactId() == null) {
99 throw new NullPointerException("contact.contactId is null"); //NOI18N
100 } else if (contact.getContactId() < 1) {
102 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
103 } else if (!this.isContactFound(contact)) {
105 throw new ContactNotFoundException(contact.getContactId());
108 // Merge it to get a managed entity back
109 final Contact managedContact = this.getEntityManager().getReference(contact.getClass(), contact.getContactId());
111 // Remove it from database
112 this.getEntityManager().remove(managedContact);
115 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteContactData: EXIT!", this.getClass().getSimpleName())); //NOI18N
119 * Determines if the given contact is found
121 * @param contact Contact instance
123 * @return Whether the contact has been found
125 private boolean isContactFound (final Contact contact) {
127 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactRegistered: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
129 // Default is not found
130 boolean isFound = false;
132 // Fest all entries and iterate over them
133 for (final Contact currentContact : this.contactBean.fetchAllContacts()) {
135 if (Objects.equals(contact, currentContact)) {
136 // Yes, found the same
145 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactRegistered: isFound={1} - EXIT!", this.getClass().getSimpleName(), isFound)); //NOI18N