2 * Copyright (C) 2017, 2020 Free Software Foundation
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.
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.
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/>.
17 package org.mxchange.jfinancials.beans.business.basicdata.list;
19 import fish.payara.cdi.jsr107.impl.NamedCache;
20 import java.text.MessageFormat;
21 import java.util.Comparator;
22 import java.util.Iterator;
23 import java.util.LinkedList;
24 import java.util.List;
25 import java.util.Objects;
26 import javax.annotation.PostConstruct;
27 import javax.cache.Cache;
29 import javax.enterprise.event.Observes;
30 import javax.faces.view.ViewScoped;
31 import javax.inject.Inject;
32 import javax.inject.Named;
33 import org.mxchange.jcontactsbusiness.events.basicdata.added.ObservableAdminAddedBusinessBasicDataEvent;
34 import org.mxchange.jcontactsbusiness.exceptions.basicdata.BasicDataNotFoundException;
35 import org.mxchange.jcontactsbusiness.model.basicdata.BasicCompanyDataSessionBeanRemote;
36 import org.mxchange.jcontactsbusiness.model.basicdata.BasicData;
37 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
40 * A view-scoped bean for product lists
42 * @author Roland Haeder<roland@mxchange.org>
44 @Named ("basicDataListController")
46 public class FinancialsBasicDataListWebViewBean extends BaseFinancialsBean implements FinancialsBasicDataListWebViewController {
51 private static final long serialVersionUID = 34_869_872_672_645L;
54 * List of all basic company data
56 private final List<BasicData> allBasicData;
59 * EJB for general basic business data purposes
61 @EJB (lookup = "java:global/jfinancials-ejb/basicCompanyData!org.mxchange.jcontactsbusiness.model.basicdata.BasicCompanyDataSessionBeanRemote", description = "A stateless session bean for general purposes.")
62 private BasicCompanyDataSessionBeanRemote basicDataBean;
65 * A list of all registered companies (globally)
68 @NamedCache (cacheName = "basicDataCache")
69 private transient Cache<Long, BasicData> basicDataCache;
72 * List of filtered basic company data
74 private List<BasicData> filteredBasicData;
79 private BasicData selectedBasicData;
84 public FinancialsBasicDataListWebViewBean () {
85 // Call super constructor
89 this.allBasicData = new LinkedList<>();
93 * Observers events being fired when an administrator has added company
96 * @param event Event being fired
98 public void afterAdminAddedBasicCompanyDataEvent (@Observes final ObservableAdminAddedBusinessBasicDataEvent event) {
99 // Is the parameter valid?
102 throw new NullPointerException("event is null"); //NOI18N
103 } else if (event.getBasicData() == null) {
105 throw new NullPointerException("event.basicData is null"); //NOI18N
106 } else if (event.getBasicData().getBasicDataId() == null) {
108 throw new NullPointerException("event.basicData.basicDataId is null"); //NOI18N
109 } else if (event.getBasicData().getBasicDataId() < 1) {
111 throw new IllegalArgumentException(MessageFormat.format("event.basicData.basicDataId={0} is invalid", event.getBasicData().getBasicDataId())); //NOI18N
112 } else if (event.getBasicData().getCompanyShortName() == null) {
114 throw new NullPointerException("event.basicData.companyName is null"); //NOI18N
115 } else if (event.getBasicData().getCompanyShortName().isEmpty()) {
117 throw new IllegalArgumentException("event.basicData.companyName is empty"); //NOI18N
121 this.basicDataCache.put(event.getBasicData().getBasicDataId(), event.getBasicData());
122 this.getAllBasicData().add(event.getBasicData());
126 public BasicData findBasicDataById (final Long basicDataId) throws BasicDataNotFoundException {
127 // Validate parameter
128 if (null == basicDataId) {
130 throw new NullPointerException("basicDataId is null"); //NOI18N
131 } else if (basicDataId < 1) {
133 throw new IllegalArgumentException(MessageFormat.format("basicDataId={0} is invalid", basicDataId)); //NOI18N
134 } else if (!this.basicDataCache.containsKey(basicDataId)) {
136 throw new BasicDataNotFoundException(basicDataId);
140 final BasicData basicData = this.basicDataCache.get(basicDataId);
147 * Getter for a list of all business contacts
149 * @return A list of all business contacts
151 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
152 public List<BasicData> getAllBasicData () {
153 return this.allBasicData;
157 * Getter for filtered basic company data
159 * @return Filtered basic company data
161 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
162 public List<BasicData> getFilteredBasicData () {
163 return this.filteredBasicData;
167 * Setter for filtered basic company data
169 * @param filteredBasicData Filtered basic company data
171 @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
172 public void setFilteredBasicData (final List<BasicData> filteredBasicData) {
173 this.filteredBasicData = filteredBasicData;
177 * Getter for selected basic data
179 * @return Selected basic data
181 public BasicData getSelectedBasicData () {
182 return this.selectedBasicData;
186 * Setter for selected basic data
188 * @param selectedBasicData Selected basic data
190 public void setSelectedBasicData (final BasicData selectedBasicData) {
191 this.selectedBasicData = selectedBasicData;
198 public void initializeList () {
200 if (!this.basicDataCache.iterator().hasNext()) {
201 // Get whole list from EJB
202 final List<BasicData> basicDatas = this.basicDataBean.allBusinessBasicData();
205 for (final BasicData basicData : basicDatas) {
207 this.basicDataCache.put(basicData.getBasicDataId(), basicData);
211 // Is cache there and list is not full?
212 if ((this.getAllBasicData().isEmpty()) && (this.basicDataCache.iterator().hasNext())) {
214 final Iterator<Cache.Entry<Long, BasicData>> iterator = this.basicDataCache.iterator();
217 while (iterator.hasNext()) {
219 final Cache.Entry<Long, BasicData> next = iterator.next();
222 this.getAllBasicData().add(next.getValue());
226 this.getAllBasicData().sort(new Comparator<BasicData>() {
228 public int compare (final BasicData o1, final BasicData o2) {
229 return o1.getBasicDataId() > o2.getBasicDataId() ? 1 : o1.getBasicDataId() < o2.getBasicDataId() ? -1 : 0;
234 this.setFilteredBasicData(this.getAllBasicData());
239 public Boolean isCompanyNameUsed (final String companyName) {
240 // Validate parameter
241 if (null == companyName) {
243 throw new NullPointerException("companyName is null"); //NOI18N
244 } else if (companyName.isEmpty()) {
246 throw new IllegalArgumentException("companyName is empty"); //NOI18N
249 // Default is not found
250 boolean isFound = false;
253 for (final BasicData basicData : this.getAllBasicData()) {
254 // Is same company name?
255 if (Objects.equals(basicData.getCompanyName(), companyName)) {
267 public Boolean isCompanyShortNameUsed (final String companyShortName) {
268 // Validate parameter
269 if (null == companyShortName) {
271 throw new NullPointerException("companyShortName is null"); //NOI18N
272 } else if (companyShortName.isEmpty()) {
274 throw new IllegalArgumentException("companyShortName is empty"); //NOI18N
277 // Default is not found
278 boolean isFound = false;
281 for (final BasicData basicData : this.getAllBasicData()) {
282 // Is same company name?
283 if (Objects.equals(basicData.getCompanyShortName(), companyShortName)) {
295 public Boolean isEmailAddressRegistered (final String emailAddress) {
296 // Validate parameter
297 if (null == emailAddress) {
299 throw new NullPointerException("emailAddress is null"); //NOI18N
300 } else if (emailAddress.isEmpty()) {
302 throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
305 // Default is not found
306 boolean isFound = false;
309 for (final BasicData basicData : this.getAllBasicData()) {
310 // Is email address used?
311 if (Objects.equals(basicData.getCompanyEmailAddress(), emailAddress)) {