]> git.mxchange.org Git - jjobs-ejb.git/blob - src/java/org/mxchange/jcontacts/model/contact/JobsAdminContactSessionBean.java
Please cherry-pick:
[jjobs-ejb.git] / src / java / org / mxchange / jcontacts / model / contact / JobsAdminContactSessionBean.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.model.contact;
18
19 import java.text.MessageFormat;
20 import java.util.GregorianCalendar;
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;
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 BaseJobsDatabaseBean implements AdminContactSessionBeanRemote {
35
36         /**
37          * Serial number
38          */
39         private static final long serialVersionUID = 542_145_347_916L;
40
41         /**
42          * Default constructor
43          */
44         public JobsAdminContactSessionBean () {
45                 // Call super constructor
46                 super();
47         }
48
49         @Override
50         public Contact addContact (final Contact contact) throws ContactAlreadyAddedException {
51                 // Trace message
52                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addContact: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
53
54                 // Is the instance set?
55                 if (null == contact) {
56                         // Throw NPE
57                         throw new NullPointerException("contact is null"); //NOI18N
58                 } else if (contact.getContactId() != null) {
59                         // Should be null
60                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} - is not null", contact.getContactId())); //NOI18N
61                 } else if (this.isContactFound(contact)) {
62                         // Already registered
63                         throw new ContactAlreadyAddedException(contact);
64                 }
65
66                 // Set created timestamp
67                 contact.setContactCreated(new GregorianCalendar());
68
69                 // Set all created timestamps, if instance is there
70                 this.setAllContactPhoneEntriesCreated(contact);
71
72                 // Persist it
73                 this.getEntityManager().persist(contact);
74
75                 // Trace message
76                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addContact: contact.contactId={1} after persisting - EXIT!", this.getClass().getSimpleName(), contact.getContactId())); //NOI18N
77
78                 // Return it
79                 return contact;
80         }
81
82         @Override
83         public void deleteContactData (final Contact contact) throws ContactNotFoundException {
84                 // Trace message
85                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteContactData: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
86
87                 // Is the instance set?
88                 if (null == contact) {
89                         // Throw NPE
90                         throw new NullPointerException("contact is null"); //NOI18N
91                 } else if (contact.getContactId() == null) {
92                         // Should not be null
93                         throw new NullPointerException("contact.contactId is null"); //NOI18N
94                 } else if (contact.getContactId() < 1) {
95                         // Not valid
96                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
97                 } else if (!this.isContactFound(contact)) {
98                         // Contact not found
99                         throw new ContactNotFoundException(contact.getContactId());
100                 }
101
102                 // Merge it to get a managed entity back
103                 final Contact managedContact = this.getEntityManager().getReference(contact.getClass(), contact.getContactId());
104
105                 // Remove it from database
106                 this.getEntityManager().remove(managedContact);
107
108                 // Trace message
109                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteContactData: EXIT!", this.getClass().getSimpleName())); //NOI18N
110         }
111
112         /**
113          * Determines if the given contact is found
114          * <p>
115          * @param contact Contact instance
116          * <p>
117          * @return Whether the contact has been found
118          */
119         private boolean isContactFound (final Contact contact) {
120                 // Trace message
121                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactRegistered: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
122
123                 // Create query
124                 final Query query = this.getEntityManager().createNamedQuery("SearchContact"); //NOI18N
125
126                 // Set parameter
127                 query.setParameter("contact", contact); //NOI18N
128                 query.setMaxResults(1);
129
130                 // Default is not found
131                 boolean isFound = false;
132
133                 // Try it
134                 try {
135                         // Try to find instance
136                         final Contact dummy = (Contact) query.getSingleResult();
137
138                         // Mark as found
139                         isFound = true;
140                 } catch (final NoResultException e) {
141                         // No result found
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 }