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.model.contact;
19 import java.text.MessageFormat;
20 import java.util.Date;
21 import javax.ejb.Stateless;
22 import javax.persistence.NoResultException;
23 import javax.persistence.Query;
24 import org.mxchange.jcontacts.exceptions.ContactAlreadyAddedException;
25 import org.mxchange.jcontacts.exceptions.ContactNotFoundException;
26 import org.mxchange.jjobs.database.BaseJobsDatabaseBean;
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 BaseJobsDatabaseBean implements AdminContactSessionBeanRemote {
39 private static final long serialVersionUID = 542_145_347_916L;
44 public JobsAdminContactSessionBean () {
45 // Call super constructor
50 public Contact addContact (final Contact contact) throws ContactAlreadyAddedException {
52 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addContact: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
54 // Is the instance set?
55 if (null == contact) {
57 throw new NullPointerException("contact is null"); //NOI18N
58 } else if (contact.getContactId() != null) {
60 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} - is not null", contact.getContactId())); //NOI18N
61 } else if (this.isContactFound(contact)) {
63 throw new ContactAlreadyAddedException(contact);
66 // Set created timestamp
67 contact.setContactCreated(new Date());
69 // Set all created timestamps, if instance is there
70 this.setAllPhoneEntriesCreated(contact);
73 this.getEntityManager().persist(contact);
76 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addContact: contact.contactId={1} after persisting - EXIT!", this.getClass().getSimpleName(), contact.getContactId())); //NOI18N
83 public void deleteContactData (final Contact contact) throws ContactNotFoundException {
85 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteContactData: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
87 // Is the instance set?
88 if (null == contact) {
90 throw new NullPointerException("contact is null"); //NOI18N
91 } else if (contact.getContactId() == null) {
93 throw new NullPointerException("contact.contactId is null"); //NOI18N
94 } else if (contact.getContactId() < 1) {
96 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
97 } else if (!this.isContactFound(contact)) {
99 throw new ContactNotFoundException(contact.getContactId());
102 // Merge it to get a managed entity back
103 final Contact managedContact = this.getEntityManager().getReference(contact.getClass(), contact.getContactId());
105 // Remove it from database
106 this.getEntityManager().remove(managedContact);
109 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteContactData: EXIT!", this.getClass().getSimpleName())); //NOI18N
113 * Determines if the given contact is found
115 * @param contact Contact instance
117 * @return Whether the contact has been found
119 private boolean isContactFound (final Contact contact) {
121 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactRegistered: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
124 final Query query = this.getEntityManager().createNamedQuery("SearchContact"); //NOI18N
127 query.setParameter("contact", contact); //NOI18N
128 query.setMaxResults(1);
130 // Default is not found
131 boolean isFound = false;
135 // Try to find instance
136 final Contact dummy = (Contact) query.getSingleResult();
140 } catch (final NoResultException e) {
145 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactRegistered: isFound={1} - EXIT!", this.getClass().getSimpleName(), isFound)); //NOI18N