2 * Copyright (C) 2016, 2017 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.addressbook.database.BaseAddressbookDatabaseBean;
23 import org.mxchange.jcontacts.exceptions.ContactAlreadyAddedException;
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 AddressbookAdminContactSessionBean extends BaseAddressbookDatabaseBean implements AdminContactSessionBeanRemote {
36 private static final long serialVersionUID = 542_145_347_916L;
41 public AddressbookAdminContactSessionBean () {
42 // Call super constructor
47 public Contact addContact (final Contact contact) throws ContactAlreadyAddedException {
49 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addContact: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
51 // Is the instance set?
52 if (null == contact) {
54 throw new NullPointerException("contact is null"); //NOI18N
55 } else if (contact.getContactId() != null) {
57 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} - is not null", contact.getContactId())); //NOI18N
60 // Set created timestamp
61 contact.setContactCreated(new GregorianCalendar());
63 // Set all created timestamps, if instance is there
64 this.setAllContactPhoneEntriesCreated(contact);
67 this.getEntityManager().persist(contact);
69 // Flush it to get contactId set
70 this.getEntityManager().flush();
73 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addContact: contact.contactId={1} after persisting - EXIT!", this.getClass().getSimpleName(), contact.getContactId())); //NOI18N
80 public void deleteContactData (final Contact contact) {
82 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteContactData: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
84 // Is the instance set?
85 if (null == contact) {
87 throw new NullPointerException("contact is null"); //NOI18N
88 } else if (contact.getContactId() == null) {
90 throw new NullPointerException("contact.contactId is null"); //NOI18N
91 } else if (contact.getContactId() < 1) {
93 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
96 // Merge it to get a managed entity back
97 Contact managedContact = this.getEntityManager().getReference(contact.getClass(), contact.getContactId());
99 // Remove it from database
100 this.getEntityManager().remove(managedContact);
103 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteContactData: EXIT!", this.getClass().getSimpleName())); //NOI18N