]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/helper/PizzaAdminWebRequestHelper.java
98f3d284ead74afd02169dcd51256c99c46f9996
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / helper / PizzaAdminWebRequestHelper.java
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.pizzaapplication.beans.helper;
18
19 import java.text.MessageFormat;
20 import javax.enterprise.context.RequestScoped;
21 import javax.inject.Inject;
22 import javax.inject.Named;
23 import org.mxchange.jcontacts.contact.Contact;
24 import org.mxchange.jusercore.model.user.User;
25 import org.mxchange.pizzaapplication.beans.contact.PizzaAdminContactWebRequestController;
26 import org.mxchange.pizzaapplication.beans.customer.PizzaAdminCustomerWebRequestController;
27 import org.mxchange.pizzaapplication.beans.customer.PizzaCustomerWebSessionController;
28 import org.mxchange.pizzaapplication.beans.user.PizzaAdminUserWebRequestController;
29 import org.mxchange.pizzaapplication.beans.user.PizzaUserWebSessionController;
30
31 /**
32  * A general helper for administrative beans
33  * <p>
34  * @author Roland Haeder<roland@mxchange.org>
35  */
36 @Named ("adminHelper")
37 @RequestScoped
38 public class PizzaAdminWebRequestHelper implements PizzaAdminWebRequestController {
39
40         /**
41          * Serial number
42          */
43         private static final long serialVersionUID = 17_258_793_567_145_701L;
44
45         /**
46          * Administrative contact controller
47          */
48         @Inject
49         private PizzaAdminContactWebRequestController adminContactController;
50
51         /**
52          * Administrative user controller
53          */
54         @Inject
55         private PizzaAdminCustomerWebRequestController adminCustomerController;
56
57         /**
58          * Administrative user controller
59          */
60         @Inject
61         private PizzaAdminUserWebRequestController adminUserController;
62
63         /**
64          * Contact instance
65          */
66         private Contact contact;
67
68         /**
69          * General user controller
70          */
71         @Inject
72         private PizzaCustomerWebSessionController customerController;
73
74         /**
75          * User instance
76          */
77         private User user;
78
79         /**
80          * General user controller
81          */
82         @Inject
83         private PizzaUserWebSessionController userController;
84
85         /**
86          * Default constructor
87          */
88         public PizzaAdminWebRequestHelper () {
89         }
90
91         @Override
92         public void copyContactToController () {
93                 // Log message
94                 //* NOISY-DEBUG: */ System.out.println("AdminHelper::copyContactToController - CALLED!"); //NOI18N
95
96                 // Validate user instance
97                 if (this.getContact() == null) {
98                         // Throw NPE
99                         throw new NullPointerException("this.contact is null"); //NOI18N
100                 } else if (this.getContact().getContactId() == null) {
101                         // Throw NPE again
102                         throw new NullPointerException("this.contact.contactId is null"); //NOI18N
103                 } else if (this.getContact().getContactId() < 1) {
104                         // Not valid
105                         throw new IllegalStateException(MessageFormat.format("this.contact.contactId={0} is not valid.", this.getContact().getContactId())); //NOI18N
106                 }
107
108                 // Set all fields: user
109                 this.adminContactController.copyContactToController(this.getContact());
110
111                 // Log message
112                 //* NOISY-DEBUG: */ System.out.println("AdminHelper::copyContactToController - EXIT!"); //NOI18N
113         }
114
115         @Override
116         public void copyUserToController () {
117                 // Log message
118                 //* NOISY-DEBUG: */ System.out.println("AdminHelper::copyUserToController - CALLED!"); //NOI18N
119
120                 // Validate user instance
121                 if (this.getUser() == null) {
122                         // Throw NPE
123                         throw new NullPointerException("this.user is null"); //NOI18N
124                 } else if (this.getUser().getUserId() == null) {
125                         // Throw NPE again
126                         throw new NullPointerException("this.user.userId is null"); //NOI18N
127                 } else if (this.getUser().getUserId() < 1) {
128                         // Not valid
129                         throw new IllegalStateException(MessageFormat.format("this.user.userId={0} is not valid.", this.getUser().getUserId())); //NOI18N
130                 }
131
132                 // Set all fields: user
133                 this.adminUserController.setUserName(this.getUser().getUserName());
134
135                 // Log message
136                 //* NOISY-DEBUG: */ System.out.println("AdminHelper::copyUserToController - EXIT!"); //NOI18N
137         }
138
139         @Override
140         public Contact getContact () {
141                 return this.contact;
142         }
143
144         @Override
145         public void setContact (final Contact contact) {
146                 this.contact = contact;
147         }
148
149         @Override
150         public User getUser () {
151                 return this.user;
152         }
153
154         @Override
155         public void setUser (final User user) {
156                 this.user = user;
157         }
158
159         @Override
160         public String getUserCustomerUsageMessageKey (final Contact contact) {
161                 // The contact must be valid
162                 if (null == contact) {
163                         // Throw NPE
164                         throw new NullPointerException("contact is null"); //NOI18N
165                 } else if (contact.getContactId() == null) {
166                         // Throw again ...
167                         throw new NullPointerException("contact.contactId is null"); //NOI18N
168                 } else if (contact.getContactId() < 1) {
169                         // Not valid
170                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
171                 }
172
173                 // Default key is "unused"
174                 String messageKey = "CONTACT_IS_UNUSED"; //NOI18N
175
176                 // Check user/customer
177                 boolean isUserContact = this.userController.isContactFound(contact);
178                 boolean isCustomerContact = this.customerController.isContactFound(contact);
179
180                 // Check user first
181                 if (isUserContact && isCustomerContact) {
182                         // Is both
183                         messageKey = "CONTACT_IS_USER_CUSTOMER"; //NOI18N
184                 } else if (isUserContact) {
185                         // Only user
186                         messageKey = "CONTACT_IS_USER"; //NOI18N
187                 } else if (isCustomerContact) {
188                         // Only customer
189                         messageKey = "CONTACT_IS_CUSTOMER"; //NOI18N
190                 }
191
192                 // Return message key
193                 return messageKey;
194         }
195
196 }