]> git.mxchange.org Git - addressbook-mailer-ejb.git/blob - src/java/org/mxchange/jcontacts/contact/AddressbookAdminContactSessionBean.java
Please cherry-pick:
[addressbook-mailer-ejb.git] / src / java / org / mxchange / jcontacts / contact / AddressbookAdminContactSessionBean.java
1 /*
2  * Copyright (C) 2016, 2017 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                 // Call super constructor
43                 super();
44         }
45
46         @Override
47         public Contact addContact (final Contact contact) throws ContactAlreadyAddedException {
48                 // Trace message
49                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addContact: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
50
51                 // Is the instance set?
52                 if (null == contact) {
53                         // Throw NPE
54                         throw new NullPointerException("contact is null"); //NOI18N
55                 } else if (contact.getContactId() != null) {
56                         // Should be null
57                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} - is not null", contact.getContactId())); //NOI18N
58                 }
59
60                 // Set created timestamp
61                 contact.setContactCreated(new GregorianCalendar());
62
63                 // Set all created timestamps, if instance is there
64                 this.setAllContactPhoneEntriesCreated(contact);
65
66                 // Persist it
67                 this.getEntityManager().persist(contact);
68
69                 // Flush it to get contactId set
70                 this.getEntityManager().flush();
71
72                 // Trace message
73                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addContact: contact.contactId={1} after persisting - EXIT!", this.getClass().getSimpleName(), contact.getContactId())); //NOI18N
74
75                 // Return it
76                 return contact;
77         }
78
79         @Override
80         public void deleteContactData (final Contact contact) {
81                 // Trace message
82                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteContactData: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
83
84                 // Is the instance set?
85                 if (null == contact) {
86                         // Throw NPE
87                         throw new NullPointerException("contact is null"); //NOI18N
88                 } else if (contact.getContactId() == null) {
89                         // Should not be null
90                         throw new NullPointerException("contact.contactId is null"); //NOI18N
91                 } else if (contact.getContactId() < 1) {
92                         // Not valid
93                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
94                 }
95
96                 // Merge it to get a managed entity back
97                 Contact managedContact = this.getEntityManager().getReference(contact.getClass(), contact.getContactId());
98
99                 // Remove it from database
100                 this.getEntityManager().remove(managedContact);
101
102                 // Trace message
103                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteContactData: EXIT!", this.getClass().getSimpleName())); //NOI18N
104         }
105
106 }