]> git.mxchange.org Git - addressbook-war.git/blob - src/java/org/mxchange/addressbook/beans/contact/mobile/AddressbookContactMobileWebRequestBean.java
96638dc241cc0ec67cbd6009e2a1b510fb011e04
[addressbook-war.git] / src / java / org / mxchange / addressbook / beans / contact / mobile / AddressbookContactMobileWebRequestBean.java
1 /*
2  * Copyright (C) 2016 - 2022 Free Software Foundation
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.addressbook.beans.contact.mobile;
18
19 import java.text.MessageFormat;
20 import java.util.LinkedList;
21 import java.util.List;
22 import java.util.Objects;
23 import javax.enterprise.context.RequestScoped;
24 import javax.enterprise.event.Observes;
25 import javax.inject.Inject;
26 import javax.inject.Named;
27 import org.mxchange.addressbook.beans.BaseAddressbookBean;
28 import org.mxchange.addressbook.beans.contact.list.AddressbookContactListWebViewController;
29 import org.mxchange.jcontacts.events.contact.add.ObservableAdminAddedContactEvent;
30 import org.mxchange.jcontacts.events.contact.update.ObservableAdminUpdatedContactEvent;
31 import org.mxchange.jcontacts.events.contact.update.ObservableUpdatedContactEvent;
32 import org.mxchange.jcontacts.model.contact.Contact;
33 import org.mxchange.jphone.events.mobile.created.ObservableCreatedMobileNumberEvent;
34 import org.mxchange.jphone.model.phonenumbers.mobile.DialableMobileNumber;
35 import org.mxchange.jusercore.events.user.add.ObservableAdminAddedUserEvent;
36
37 /**
38  * A general contact bean (controller)
39  * <p>
40  * @author Roland Häder<roland@mxchange.org>
41  */
42 @Named ("contactMobileController")
43 @RequestScoped
44 public class AddressbookContactMobileWebRequestBean extends BaseAddressbookBean implements AddressbookContactMobileWebRequestController {
45
46         /**
47          * Serial number
48          */
49         private static final long serialVersionUID = 542_145_353_001L;
50
51         /**
52          * An instance of a contact-list controller
53          */
54         @Inject
55         private AddressbookContactListWebViewController contactListController;
56
57         /**
58          * Chosen mobile number
59          */
60         private DialableMobileNumber mobileNumber;
61
62         /**
63          * Default constructor
64          */
65         public AddressbookContactMobileWebRequestBean () {
66                 // Call super constructor
67                 super();
68         }
69
70         /**
71          * Observes events being fired when an administrator has added a new
72          * contact.
73          * <p>
74          * @param event Event being fired
75          */
76         public void afterAdminAddedContactEvent (@Observes final ObservableAdminAddedContactEvent event) {
77                 // The event must be valid
78                 if (null == event) {
79                         // Throw NPE
80                         throw new NullPointerException("event is null"); //NOI18N
81                 } else if (event.getAddedContact() == null) {
82                         // Throw again ...
83                         throw new NullPointerException("event.addedContact is null"); //NOI18N
84                 } else if (event.getAddedContact().getContactId() == null) {
85                         // ... and again
86                         throw new NullPointerException("event.addedContact.contactId is null"); //NOI18N
87                 } else if (event.getAddedContact().getContactId() < 1) {
88                         // Not valid
89                         throw new IllegalArgumentException(MessageFormat.format("event.addedContact.contactId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N
90                 }
91
92                 // Clear this bean
93                 this.clear();
94         }
95
96         /**
97          * Event observer for newly added users by administrator
98          * <p>
99          * @param event Event being fired
100          */
101         public void afterAdminAddedUserEvent (@Observes final ObservableAdminAddedUserEvent event) {
102                 // Event and contained entity instance should not be null
103                 if (null == event) {
104                         // Throw NPE
105                         throw new NullPointerException("event is null"); //NOI18N
106                 } else if (event.getAddedUser() == null) {
107                         // Throw NPE again
108                         throw new NullPointerException("event.addedUser is null"); //NOI18N
109                 } else if (event.getAddedUser().getUserId() == null) {
110                         // userId is null
111                         throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
112                 } else if (event.getAddedUser().getUserId() < 1) {
113                         // Not avalid id
114                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
115                 }
116
117                 // Clear all data
118                 this.clear();
119         }
120
121         /**
122          * Event observer for updated contact data by administrators
123          * <p>
124          * @param event Updated contact data event
125          */
126         public void afterAdminUpdatedContactDataEvent (@Observes final ObservableAdminUpdatedContactEvent event) {
127                 // Event and contained entity instance should not be null
128                 if (null == event) {
129                         // Throw NPE
130                         throw new NullPointerException("event is null"); //NOI18N
131                 } else if (event.getUpdatedContact() == null) {
132                         // Throw NPE again
133                         throw new NullPointerException("event.updatedContact is null"); //NOI18N
134                 } else if (event.getUpdatedContact().getContactId() == null) {
135                         // userId is null
136                         throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
137                 } else if (event.getUpdatedContact().getContactId() < 1) {
138                         // Not avalid id
139                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
140                 }
141
142                 // Clear all data
143                 this.clear();
144         }
145
146         /**
147          * Observes events being fired when a bean helper has successfully created a
148          * mobile number instance.
149          * <p>
150          * @param event Event being fired
151          */
152         public void afterCreatedMobileNumberEvent (@Observes final ObservableCreatedMobileNumberEvent event) {
153                 // The event instance must be valid
154                 if (null == event) {
155                         // Throw NPE
156                         throw new NullPointerException("event is null"); //NOI18N
157                 } else if (event.getMobileNumber() == null) {
158                         // Throw NPE again
159                         throw new NullPointerException("event.mobileNumber is null"); //NOI18N
160                 } else if (event.getMobileNumber().getMobileId() == null) {
161                         // Throw NPE yet again
162                         throw new NullPointerException("event.mobileNumber.mobileId is null"); //NOI18N
163                 } else if (event.getMobileNumber().getMobileId() < 1) {
164                         // Throw NPE yet again
165                         throw new NullPointerException(MessageFormat.format("event.mobileNumber.mobileId={0} is invalid", event.getMobileNumber().getMobileId())); //NOI18N
166                 }
167
168                 // Set it here
169                 this.setMobileNumber(event.getMobileNumber());
170         }
171
172         /**
173          * Event observer for updated contact data by the user
174          * <p>
175          * @param event Updated contact data event
176          */
177         public void afterUpdatedContactDataEvent (@Observes final ObservableUpdatedContactEvent event) {
178                 // Event and contained entity instance should not be null
179                 if (null == event) {
180                         // Throw NPE
181                         throw new NullPointerException("event is null"); //NOI18N
182                 } else if (event.getUpdatedContact() == null) {
183                         // Throw NPE again
184                         throw new NullPointerException("event.updatedContact is null"); //NOI18N
185                 } else if (event.getUpdatedContact().getContactId() == null) {
186                         // userId is null
187                         throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
188                 } else if (event.getUpdatedContact().getContactId() < 1) {
189                         // Not avalid id
190                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
191                 }
192
193                 // Clear all data
194                 this.clear();
195         }
196
197         /**
198          * Getter for all contacts having current mobile number linked
199          * <p>
200          * @return List of all linked contacts
201          */
202         public List<Contact> allCurrentMobileNumberContacts () {
203                 // Get id
204                 final DialableMobileNumber dialableMobileNumber = this.getMobileNumber();
205
206                 // Init list
207                 final List<Contact> contacts = new LinkedList<>();
208
209                 // "Walk" through all contacts
210                 for (final Contact contact : this.contactListController.getAllContacts()) {
211                         // Is mobile instance the same?
212                         if (Objects.equals(contact.getContactMobileNumber(), dialableMobileNumber)) {
213                                 // Found one
214                                 contacts.add(contact);
215                         }
216                 }
217
218                 // Return now-cached list
219                 return contacts;
220         }
221
222         /**
223          * Getter for chosen mobile number
224          * <p>
225          * @return mobile number
226          */
227         public DialableMobileNumber getMobileNumber () {
228                 return this.mobileNumber;
229         }
230
231         /**
232          * Setter for chosen mobile number
233          * <p>
234          * @param mobileNumber mobile number
235          */
236         public void setMobileNumber (final DialableMobileNumber mobileNumber) {
237                 this.mobileNumber = mobileNumber;
238         }
239
240         /**
241          * Clears this bean
242          */
243         private void clear () {
244                 // Clear all data
245         }
246
247 }