]> git.mxchange.org Git - addressbook-ejb.git/blob - src/java/org/mxchange/jcontacts/model/contact/AddressbookAdminContactSessionBean.java
Please cherry-pick:
[addressbook-ejb.git] / src / java / org / mxchange / jcontacts / model / contact / AddressbookAdminContactSessionBean.java
1 /*
2  * Copyright (C) 2016 - 2020 Free Software Foundation
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.model.contact;
18
19 import java.text.MessageFormat;
20 import java.util.Date;
21 import java.util.Objects;
22 import javax.ejb.EJB;
23 import javax.ejb.Stateless;
24 import org.mxchange.jcontacts.exceptions.ContactAlreadyAddedException;
25 import org.mxchange.jcontacts.exceptions.ContactNotFoundException;
26
27 /**
28  * An administrative contact EJB
29  * <p>
30  * @author Roland Häder<roland@mxchange.org>
31  */
32 @Stateless (name = "adminContact", description = "An administrative contact EJB")
33 public class AddressbookAdminContactSessionBean extends BaseAddressbookEnterpriseBean implements AdminContactSessionBeanRemote {
34
35         /**
36          * Serial number
37          */
38         private static final long serialVersionUID = 542_145_347_916L;
39
40         /**
41          * EJB for general contact purposes
42          */
43         @EJB (lookup = "java:global/jfinancials-ejb/contact!org.mxchange.jcontacts.model.contact.ContactSessionBeanRemote")
44         private ContactSessionBeanRemote contactBean;
45
46         /**
47          * Default constructor
48          */
49         public AddressbookAdminContactSessionBean () {
50                 // Call super constructor
51                 super();
52         }
53
54         @Override
55         public Contact addContact (final Contact contact) throws ContactAlreadyAddedException {
56                 // Trace message
57                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addContact: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
58
59                 // Is the instance set?
60                 if (null == contact) {
61                         // Throw NPE
62                         throw new NullPointerException("contact is null"); //NOI18N
63                 } else if (contact.getContactId() != null) {
64                         // Should be null
65                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} - is not null", contact.getContactId())); //NOI18N
66                 } else if (this.isContactFound(contact)) {
67                         // Already registered
68                         throw new ContactAlreadyAddedException(contact);
69                 }
70
71                 // Set created timestamp
72                 contact.setContactCreated(new Date());
73
74                 // Set all created timestamps, if instance is there
75                 this.setAllPhoneEntriesCreated(contact);
76
77                 // Persist it
78                 this.getEntityManager().persist(contact);
79
80                 // Trace message
81                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addContact: contact.contactId={1} after persisting - EXIT!", this.getClass().getSimpleName(), contact.getContactId())); //NOI18N
82
83                 // Return it
84                 return contact;
85         }
86
87         @Override
88         public void deleteContactData (final Contact contact) throws ContactNotFoundException {
89                 // Trace message
90                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteContactData: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
91
92                 // Is the instance set?
93                 if (null == contact) {
94                         // Throw NPE
95                         throw new NullPointerException("contact is null"); //NOI18N
96                 } else if (contact.getContactId() == null) {
97                         // Should not be null
98                         throw new NullPointerException("contact.contactId is null"); //NOI18N
99                 } else if (contact.getContactId() < 1) {
100                         // Not valid
101                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
102                 } else if (!this.isContactFound(contact)) {
103                         // Contact not found
104                         throw new ContactNotFoundException(contact.getContactId());
105                 }
106
107                 // Merge it to get a managed entity back
108                 final Contact managedContact = this.getEntityManager().getReference(contact.getClass(), contact.getContactId());
109
110                 // Remove it from database
111                 this.getEntityManager().remove(managedContact);
112
113                 // Trace message
114                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteContactData: EXIT!", this.getClass().getSimpleName())); //NOI18N
115         }
116
117         /**
118          * Determines if the given contact is found
119          * <p>
120          * @param contact Contact instance
121          * <p>
122          * @return Whether the contact has been found
123          */
124         private boolean isContactFound (final Contact contact) {
125                 // Trace message
126                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactRegistered: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
127
128                 // Default is not found
129                 boolean isFound = false;
130
131                 // Fest all entries and iterate over them
132                 for (final Contact currentContact : this.contactBean.allContacts()) {
133                         // Is found?
134                         if (Objects.equals(contact, currentContact)) {
135                                 // Yes, found the same
136                                 isFound = true;
137
138                                 // Abort loop
139                                 break;
140                         }
141                 }
142
143                 // Trace message
144                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactRegistered: isFound={1} - EXIT!", this.getClass().getSimpleName(), isFound)); //NOI18N
145
146                 // Return flag
147                 return isFound;
148         }
149
150 }