]> git.mxchange.org Git - pizzaservice-mailer-ejb.git/blob
6c6297ac4541a11e84692bbb3f7779906c576c3c
[pizzaservice-mailer-ejb.git] /
1 /*
2  * Copyright (C) 2016 Roland Haeder
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.jphone.phonenumbers.mobileprovider;
18
19 import java.text.MessageFormat;
20 import java.util.GregorianCalendar;
21 import javax.ejb.Stateless;
22 import org.mxchange.jcoreee.database.BaseDatabaseBean;
23 import org.mxchange.jphone.exceptions.MobileProviderAlreadyAddedException;
24
25 /**
26  * An administrative singleton EJB for mobile provider informations
27  * <p>
28  * @author Roland Haeder<roland@mxchange.org>
29  */
30 @Stateless (name = "adminmobileprovider", description = "A singleton session bean for mobile provider informations, admin-edition")
31 public class AddressbookAdminMobileProviderSessionBean extends BaseDatabaseBean implements AdminMobileProviderSessionBeanRemote {
32
33         /**
34          * Serial number
35          */
36         private static final long serialVersionUID = 15_846_983_298_691_207L;
37
38         @Override
39         public MobileProvider addMobileProvider (final MobileProvider mobileProvider) throws MobileProviderAlreadyAddedException {
40                 // Log trace message
41                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("addMobileProvider: mobileProvider={0} - CALLED!", mobileProvider)); //NOI18N
42
43                 // Is the instance valid?
44                 if (null == mobileProvider) {
45                         // Throw NPE
46                         throw new NullPointerException("mobileProvider is null"); //NOI18N
47                 } else if (mobileProvider.getProviderDialPrefix() == null) {
48                         // Throw NPE again
49                         throw new NullPointerException("mobileProvider.providerDialPrefix is null"); //NOI18N
50                 } else if (mobileProvider.getProviderDialPrefix() < 1) {
51                         // Not valid
52                         throw new IllegalArgumentException(MessageFormat.format("mobileProvider.providerDialPrefix={0} is not valid.", mobileProvider.getProviderDialPrefix())); //NOI18N
53                 } else if (mobileProvider.getProviderCountry() == null) {
54                         // Throw again a NPE
55                         throw new NullPointerException("mobileProvider.providerCountry is null"); //NOI18N
56                 } else if (mobileProvider.getProviderMailPattern() == null) {
57                         // ... and again ...
58                         throw new NullPointerException("mobileProvider.providerMailPattern is null"); //NOI18N
59                 } else if (mobileProvider.getProviderMailPattern().isEmpty()) {
60                         // Empty pattern set (not allowed)
61                         throw new IllegalArgumentException("mobileProvider.providerMailPattern is empty."); //NOI18N
62                 } else if (!mobileProvider.getProviderMailPattern().contains("%s")) { //NOI18N
63                         // No place-holder found
64                         throw new IllegalArgumentException(MessageFormat.format("mobileProvider.providerMailPattern={0} does not contain '%s' which is need to be replaced with the full mobile number.", mobileProvider.getProviderMailPattern())); //NOI18N
65                 } else if (mobileProvider.getProviderName() == null) {
66                         // Throw NPE again
67                         throw new NullPointerException("mobileProvider.providerName is null"); //NOI18N
68                 } else if (mobileProvider.getProviderName().isEmpty()) {
69                         // Empty name is not allowed
70                         throw new IllegalArgumentException("mobileProvider.providerName is empty"); //NOI18N
71                 }
72
73                 // Set creation timestamp
74                 mobileProvider.setProviderEntryCreated(new GregorianCalendar());
75
76                 // Persist it
77                 this.getEntityManager().persist(mobileProvider);
78
79                 // ... and flush it to get id back
80                 this.getEntityManager().flush();
81
82                 // Log trace message
83                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("addMobileProvider: mobileProvider.providerId={0} - EXIT!", mobileProvider.getProviderId())); //NOI18N
84
85                 // Return updated
86                 return mobileProvider;
87         }
88
89 }