2 * Copyright (C) 2016 Roland Häder
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.contact;
19 import java.text.MessageFormat;
20 import java.util.GregorianCalendar;
21 import javax.ejb.Stateless;
22 import org.mxchange.jcontacts.exceptions.ContactAlreadyAddedException;
23 import org.mxchange.jjobs.database.BaseJobsDatabaseBean;
26 * An administrative contact EJB
28 * @author Roland Häder<roland@mxchange.org>
30 @Stateless (name = "adminContact", description = "An administrative contact EJB")
31 public class JobsAdminContactSessionBean extends BaseJobsDatabaseBean implements AdminContactSessionBeanRemote {
36 private static final long serialVersionUID = 542_145_347_916L;
41 public JobsAdminContactSessionBean () {
45 public Contact addContact (final Contact contact) throws ContactAlreadyAddedException {
47 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addContact: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
49 // Is the instance set?
50 if (null == contact) {
52 throw new NullPointerException("contact is null"); //NOI18N
53 } else if (contact.getContactId() != null) {
55 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} - is not null", contact.getContactId())); //NOI18N
58 // Set created timestamp
59 contact.setContactCreated(new GregorianCalendar());
61 // Set all created timestamps, if instance is there
62 this.setAllContactPhoneEntriesCreated(contact);
65 this.getEntityManager().persist(contact);
67 // Flush it to get contactId set
68 this.getEntityManager().flush();
71 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addContact: contact.contactId={1} after persisting - EXIT!", this.getClass().getSimpleName(), contact.getContactId())); //NOI18N
78 public void deleteContactData (final Contact contact) {
80 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteContactData: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
82 // Is the instance set?
83 if (null == contact) {
85 throw new NullPointerException("contact is null"); //NOI18N
86 } else if (contact.getContactId() == null) {
88 throw new NullPointerException("contact.contactId is null"); //NOI18N
89 } else if (contact.getContactId() < 1) {
91 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
94 // Merge it to get a managed entity back
95 Contact managedContact = this.getEntityManager().getReference(contact.getClass(), contact.getContactId());
97 // Remove it from database
98 this.getEntityManager().remove(managedContact);
101 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteContactData: EXIT!", this.getClass().getSimpleName())); //NOI18N