]> git.mxchange.org Git - addressbook-war.git/blob
c497b6aec33e8e3e87f9aa33ced86496792e3e9d
[addressbook-war.git] /
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.user.email_address.list;
18
19 import fish.payara.cdi.jsr107.impl.NamedCache;
20 import java.util.Comparator;
21 import java.util.LinkedList;
22 import java.util.List;
23 import javax.annotation.PostConstruct;
24 import javax.cache.Cache;
25 import javax.ejb.EJB;
26 import javax.faces.view.ViewScoped;
27 import javax.inject.Inject;
28 import javax.inject.Named;
29 import org.mxchange.addressbook.beans.BaseAddressbookBean;
30 import org.mxchange.addressbook.beans.features.AddressbookFeaturesWebApplicationController;
31 import org.mxchange.jusercore.model.email_address.ChangeableEmailAddress;
32 import org.mxchange.jusercore.model.user.email_address.UserEmailChangeSessionBeanRemote;
33
34 /**
35  * A view-scoped bean for listing email address changes
36  * <p>
37  * @author Roland Häder<roland@mxchange.org>
38  */
39 @Named ("emailChangeListController")
40 @ViewScoped
41 public class AddressbookEmailChangeListWebViewBean extends BaseAddressbookBean implements AddressbookEmailChangeListWebViewController {
42
43         /**
44          * Serial number
45          */
46         private static final long serialVersionUID = 186_078_724_659_154L;
47
48         /**
49          * A list of all mobile numbers
50          */
51         private final List<ChangeableEmailAddress> allEmailAddressChanges;
52
53         /**
54          * Remote email change bean
55          */
56         @EJB (lookup = "java:global/addressbook-ejb/userEmailChange!org.mxchange.jusercore.model.user.email_address.UserEmailChangeSessionBeanRemote")
57         private UserEmailChangeSessionBeanRemote emailChangeBean;
58
59         /**
60          * Features controller
61          */
62         @Inject
63         private AddressbookFeaturesWebApplicationController featureController;
64
65         /**
66          * A list of filtered mobile numbers
67          */
68         private List<ChangeableEmailAddress> filteredEmailAddressChanges;
69
70         /**
71          * Local list of already queued email addresses
72          */
73         @Inject
74         @NamedCache (cacheName = "queuedEmailCache")
75         private transient Cache<Long, ChangeableEmailAddress> queuedEmailCache;
76
77         /**
78          * Default constructor
79          */
80         public AddressbookEmailChangeListWebViewBean () {
81                 // Call super constructor
82                 super();
83
84                 // Initialize list
85                 this.allEmailAddressChanges = new LinkedList<>();
86         }
87
88         @Override
89         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
90         public List<ChangeableEmailAddress> getAllEmailAddressChanges () {
91                 return this.allEmailAddressChanges;
92         }
93
94         /**
95          * Getter for filtered email address changed
96          * <p>
97          * @return Filtered email address changed
98          */
99         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
100         public List<ChangeableEmailAddress> getFilteredEmailAddressChanges () {
101                 return this.filteredEmailAddressChanges;
102         }
103
104         /**
105          * Setter for filtered email address changed
106          * <p>
107          * @param filteredEmailAddressChanges Filtered email address changed
108          */
109         @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
110         public void setFilteredEmailAddressChanges (final List<ChangeableEmailAddress> filteredEmailAddressChanges) {
111                 this.filteredEmailAddressChanges = filteredEmailAddressChanges;
112         }
113
114         /**
115          * Post-construction
116          */
117         @PostConstruct
118         public void initializeList () {
119                 // Is cache there?
120                 if (!this.queuedEmailCache.iterator().hasNext()) {
121                         // Add all
122                         for (final ChangeableEmailAddress currentEmailAddress : this.emailChangeBean.fetchAllQueuedAddressChanges()) {
123                                 // Add it to cache
124                                 this.queuedEmailCache.put(currentEmailAddress.getEmailChangeId(), currentEmailAddress);
125                         }
126                 }
127
128                 // Is cache filled and list is empty
129                 if ((this.queuedEmailCache.iterator().hasNext()) && (this.getAllEmailAddressChanges().isEmpty())) {
130                         // Build up list
131                         for (final Cache.Entry<Long, ChangeableEmailAddress> currentEntry : this.queuedEmailCache) {
132                                 // Add to list
133                                 this.getAllEmailAddressChanges().add(currentEntry.getValue());
134                         }
135
136                         // Sort list
137                         this.getAllEmailAddressChanges().sort(new Comparator<ChangeableEmailAddress>() {
138                                 @Override
139                                 public int compare (final ChangeableEmailAddress queuedEmail1, final ChangeableEmailAddress queuedEmail2) {
140                                         return queuedEmail1.getEmailChangeId() > queuedEmail2.getEmailChangeId() ? 1 : queuedEmail1.getEmailChangeId() < queuedEmail2.getEmailChangeId() ? -1 : 0;
141                                 }
142                         });
143
144                         // Set full list
145                         this.setFilteredEmailAddressChanges(this.getAllEmailAddressChanges());
146                 }
147         }
148
149         @Override
150         public boolean isEmailAddressQueued (final String emailAddress) {
151                 // Check if parameter is valid
152                 if (null == emailAddress) {
153                         // Throw NPE
154                         throw new NullPointerException("emailAddress is null"); //NOI18N
155                 } else if (emailAddress.isEmpty()) {
156                         // Throw IAE
157                         throw new IllegalArgumentException("emailAddress is empty."); //NOI18N
158                 }
159
160                 // Default is not found
161                 boolean isFound = false;
162
163                 // Iterate through whole list
164                 for (final ChangeableEmailAddress address : this.getAllEmailAddressChanges()) {
165                         // Does current match?
166                         if (emailAddress.equals(address.getEmailAddress())) {
167                                 // Yes, set flag and abort iteration
168                                 isFound = true;
169                                 break;
170                         }
171                 }
172
173                 // Return flag
174                 return isFound;
175         }
176
177 }