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