]> git.mxchange.org Git - jfinancials-mailer-ejb.git/blob - src/java/org/mxchange/jcontacts/contact/AddressbookAdminContactSessionBean.java
4ed5a4001bb1206b1bf6805d338d126922c876e2
[jfinancials-mailer-ejb.git] / src / java / org / mxchange / jcontacts / contact / AddressbookAdminContactSessionBean.java
1 /*
2  * Copyright (C) 2016 Roland Häder
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.jcontacts.contact;
18
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;
24
25 /**
26  * An administrative contact EJB
27  * <p>
28  * @author Roland Häder<roland@mxchange.org>
29  */
30 @Stateless (name = "adminContact", description = "An administrative contact EJB")
31 public class AddressbookAdminContactSessionBean extends BaseAddressbookDatabaseBean implements AdminContactSessionBeanRemote {
32
33         /**
34          * Serial number
35          */
36         private static final long serialVersionUID = 542_145_347_916L;
37
38         /**
39          * Default constructor
40          */
41         public AddressbookAdminContactSessionBean () {
42         }
43
44         @Override
45         public Contact addContact (final Contact contact) throws ContactAlreadyAddedException {
46                 // Trace message
47                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addContact: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
48
49                 // Is the instance set?
50                 if (null == contact) {
51                         // Throw NPE
52                         throw new NullPointerException("contact is null"); //NOI18N
53                 } else if (contact.getContactId() != null) {
54                         // Should be null
55                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} - is not null", contact.getContactId())); //NOI18N
56                 }
57
58                 // Set created timestamp
59                 contact.setContactCreated(new GregorianCalendar());
60
61                 // Set all created timestamps, if instance is there
62                 this.setAllContactPhoneEntriesCreated(contact);
63
64                 // Persist it
65                 this.getEntityManager().persist(contact);
66
67                 // Flush it to get contactId set
68                 this.getEntityManager().flush();
69
70                 // Trace message
71                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addContact: contact.contactId={1} after persisting - EXIT!", this.getClass().getSimpleName(), contact.getContactId())); //NOI18N
72
73                 // Return it
74                 return contact;
75         }
76
77         @Override
78         public void deleteContactData (final Contact contact) {
79                 // Trace message
80                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteContactData: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
81
82                 // Is the instance set?
83                 if (null == contact) {
84                         // Throw NPE
85                         throw new NullPointerException("contact is null"); //NOI18N
86                 } else if (contact.getContactId() == null) {
87                         // Should not be null
88                         throw new NullPointerException("contact.contactId is null"); //NOI18N
89                 } else if (contact.getContactId() < 1) {
90                         // Not valid
91                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
92                 }
93
94                 // Merge it to get a managed entity back
95                 Contact managedContact = this.getEntityManager().getReference(contact.getClass(), contact.getContactId());
96
97                 // Remove it from database
98                 this.getEntityManager().remove(managedContact);
99
100                 // Trace message
101                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteContactData: EXIT!", this.getClass().getSimpleName())); //NOI18N
102         }
103
104 }