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