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