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