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