]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/contact/phone/JobsContactPhoneWebSessionBean.java
removed double //NOI18N (please cherry-pick)
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / contact / phone / JobsContactPhoneWebSessionBean.java
1 /*
2  * Copyright (C) 2016 Cho-Time GmbH
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.contact.phone;
18
19 import de.chotime.landingpage.beans.BaseLandingController;
20 import de.chotime.landingpage.beans.contact.LandingContactWebSessionController;
21 import de.chotime.landingpage.beans.helper.LandingWebRequestController;
22 import java.text.MessageFormat;
23 import java.util.HashMap;
24 import java.util.LinkedList;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Objects;
28 import javax.enterprise.context.SessionScoped;
29 import javax.enterprise.event.Observes;
30 import javax.inject.Inject;
31 import javax.inject.Named;
32 import org.mxchange.jcontacts.contact.Contact;
33 import org.mxchange.jcontacts.events.cellphone.unlinked.AdminUnlinkedCellphoneNumberEvent;
34 import org.mxchange.jcontacts.events.contact.add.AdminAddedContactEvent;
35 import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent;
36 import org.mxchange.jphone.phonenumbers.DialableNumber;
37 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
38 import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent;
39
40 /**
41  * An administrative contact phone controller (bean)
42  * <p>
43  * @author Roland Haeder<rhaeder@cho-time.de>
44  */
45 @Named ("contactPhoneController")
46 @SessionScoped
47 public class JobsContactPhoneWebSessionBean extends BaseLandingController implements JobsContactPhoneWebSessionController {
48
49         /**
50          * Serial number
51          */
52         private static final long serialVersionUID = 542_145_347_916L;
53
54         /**
55          * Admin helper instance
56          */
57         @Inject
58         private LandingWebRequestController beanHelper;
59
60         /**
61          * General contact controller
62          */
63         @Inject
64         private LandingContactWebSessionController contactController;
65
66         /**
67          * "Cache" for contact's cellphone, land-line and fax numbers. Currently one
68          * one per each type is supported. Maybe later this will change into a
69          * OneToMany relationship (one contact, many numbers).
70          */
71         private final Map<DialableNumber, List<Contact>> contacts;
72
73         /**
74          * Default constructor
75          */
76         public JobsContactPhoneWebSessionBean () {
77                 // Init lists/maps
78                 this.contacts = new HashMap<>(10);
79         }
80
81         @Override
82         public void afterAdminAddedContact (@Observes final AdminAddedContactEvent event) {
83                 // The event must be valid
84                 if (null == event) {
85                         // Throw NPE
86                         throw new NullPointerException("event is null"); //NOI18N
87                 } else if (event.getAddedContact() == null) {
88                         // Throw again ...
89                         throw new NullPointerException("event.addedContact is null"); //NOI18N
90                 } else if (event.getAddedContact().getContactId() == null) {
91                         // ... and again
92                         throw new NullPointerException("event.addedContact.contactId is null"); //NOI18N
93                 } else if (event.getAddedContact().getContactId() < 1) {
94                         // Not valid
95                         throw new IllegalArgumentException(MessageFormat.format("event.addedContact.contactId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N
96                 }
97
98                 // Clear this bean
99                 this.clear();
100         }
101
102         @Override
103         public void afterAdminAddedUserEvent (@Observes final AdminAddedUserEvent event) {
104                 // event should not be null
105                 if (null == event) {
106                         // Throw NPE
107                         throw new NullPointerException("event is null"); //NOI18N
108                 } else if (event.getAddedUser() == null) {
109                         // Throw NPE again
110                         throw new NullPointerException("event.addedUser is null"); //NOI18N
111                 } else if (event.getAddedUser().getUserId() == null) {
112                         // userId is null
113                         throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
114                 } else if (event.getAddedUser().getUserId() < 1) {
115                         // Not avalid id
116                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
117                 }
118
119                 // Clear all data
120                 this.clear();
121         }
122
123         @Override
124         public void afterAdminUnlinkedCellphoneContactDataEvent (@Observes final AdminUnlinkedCellphoneNumberEvent event) {
125                 // event should not be null
126                 if (null == event) {
127                         // Throw NPE
128                         throw new NullPointerException("event is null"); //NOI18N
129                 } else if (event.getUnlinkedCellphoneNumber() == null) {
130                         // Throw NPE again
131                         throw new NullPointerException("event.unlinkedCellphoneNumber is null"); //NOI18N
132                 } else if (event.getUnlinkedCellphoneNumber().getPhoneId() == null) {
133                         // userId is null
134                         throw new NullPointerException("event.unlinkedCellphoneNumber.contactId is null"); //NOI18N
135                 } else if (event.getUnlinkedCellphoneNumber().getPhoneId() < 1) {
136                         // Not avalid id
137                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUnlinkedCellphoneNumber(), event.getUnlinkedCellphoneNumber().getPhoneId())); //NOI18N
138                 }
139
140                 // Remove it from list
141                 this.contacts.remove(event.getUnlinkedCellphoneNumber());
142
143                 // Clear all data
144                 this.clear();
145         }
146
147         @Override
148         public void afterAdminUpdatedContactDataEvent (@Observes final AdminUpdatedContactEvent event) {
149                 // event should not be null
150                 if (null == event) {
151                         // Throw NPE
152                         throw new NullPointerException("event is null"); //NOI18N
153                 } else if (event.getUpdatedContact() == null) {
154                         // Throw NPE again
155                         throw new NullPointerException("event.updatedContact is null"); //NOI18N
156                 } else if (event.getUpdatedContact().getContactId() == null) {
157                         // userId is null
158                         throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
159                 } else if (event.getUpdatedContact().getContactId() < 1) {
160                         // Not avalid id
161                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
162                 }
163
164                 // Clear all data
165                 this.clear();
166         }
167
168         @Override
169         public List<Contact> allCellphoneContacts () {
170                 // Get id
171                 DialableCellphoneNumber phone = this.beanHelper.getCellPhoneNumber();
172
173                 // Is cache there?
174                 if (this.contacts.containsKey(phone)) {
175                         // Return cached version
176                         return this.contacts.get(phone);
177                 } else {
178                         // Ask bean
179                         List<Contact> list = new LinkedList<>();
180
181                         // "Walk" through all contacts
182                         for (final Contact contact : this.contactController.allContacts()) {
183                                 // Is cellphone instance the same?
184                                 if (Objects.equals(contact.getContactCellphoneNumber(), this.beanHelper.getCellPhoneNumber())) {
185                                         // Found one
186                                         list.add(contact);
187                                 }
188                         }
189
190                         // Store result in cache
191                         this.contacts.put(phone, list);
192
193                         // Return now-cached list
194                         return list;
195                 }
196         }
197
198         /**
199          * Clears this bean
200          */
201         private void clear () {
202                 // Clear all data
203         }
204
205 }