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.LinkedList;
23 import java.util.List;
24 import java.util.Objects;
25 import javax.annotation.PostConstruct;
26 import javax.cache.Cache;
28 import javax.enterprise.event.Observes;
29 import javax.faces.view.ViewScoped;
30 import javax.inject.Inject;
31 import javax.inject.Named;
32 import org.mxchange.jcontactsbusiness.events.basicdata.added.ObservableAdminAddedBusinessBasicDataEvent;
33 import org.mxchange.jcontactsbusiness.exceptions.basicdata.BasicDataNotFoundException;
34 import org.mxchange.jcontactsbusiness.model.basicdata.BasicCompanyDataSessionBeanRemote;
35 import org.mxchange.jcontactsbusiness.model.basicdata.BasicData;
36 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
39 * A view-scoped bean for product lists
41 * @author Roland Haeder<roland@mxchange.org>
43 @Named ("basicDataListController")
45 public class FinancialsBasicDataListWebViewBean extends BaseFinancialsBean implements FinancialsBasicDataListWebViewController {
50 private static final long serialVersionUID = 34_869_872_672_645L;
53 * List of all basic company data
55 private final List<BasicData> allBasicData;
58 * EJB for general basic business data purposes
60 @EJB (lookup = "java:global/jfinancials-ejb/basicCompanyData!org.mxchange.jcontactsbusiness.model.basicdata.BasicCompanyDataSessionBeanRemote", description = "A stateless session bean for general purposes.")
61 private BasicCompanyDataSessionBeanRemote basicDataBean;
64 * A list of all registered companies (globally)
67 @NamedCache (cacheName = "basicDataCache")
68 private transient Cache<Long, BasicData> basicDataCache;
71 * List of filtered basic company data
73 private List<BasicData> filteredBasicData;
78 private BasicData selectedBasicData;
83 public FinancialsBasicDataListWebViewBean () {
84 // Call super constructor
88 this.allBasicData = new LinkedList<>();
92 * Observers events being fired when an administrator has added company
95 * @param event Event being fired
97 public void afterAdminAddedBasicCompanyDataEvent (@Observes final ObservableAdminAddedBusinessBasicDataEvent event) {
98 // Is the parameter valid?
101 throw new NullPointerException("event is null"); //NOI18N
102 } else if (event.getBasicData() == null) {
104 throw new NullPointerException("event.basicData is null"); //NOI18N
105 } else if (event.getBasicData().getBasicDataId() == null) {
107 throw new NullPointerException("event.basicData.basicDataId is null"); //NOI18N
108 } else if (event.getBasicData().getBasicDataId() < 1) {
110 throw new IllegalArgumentException(MessageFormat.format("event.basicData.basicDataId={0} is invalid", event.getBasicData().getBasicDataId())); //NOI18N
111 } else if (event.getBasicData().getCompanyShortName() == null) {
113 throw new NullPointerException("event.basicData.companyName is null"); //NOI18N
114 } else if (event.getBasicData().getCompanyShortName().isEmpty()) {
116 throw new IllegalArgumentException("event.basicData.companyName is empty"); //NOI18N
120 this.basicDataCache.put(event.getBasicData().getBasicDataId(), event.getBasicData());
121 this.getAllBasicData().add(event.getBasicData());
125 public BasicData findBasicDataById (final Long basicDataId) throws BasicDataNotFoundException {
126 // Validate parameter
127 if (null == basicDataId) {
129 throw new NullPointerException("basicDataId is null"); //NOI18N
130 } else if (basicDataId < 1) {
132 throw new IllegalArgumentException(MessageFormat.format("basicDataId={0} is invalid", basicDataId)); //NOI18N
133 } else if (!this.basicDataCache.containsKey(basicDataId)) {
135 throw new BasicDataNotFoundException(basicDataId);
139 final BasicData basicData = this.basicDataCache.get(basicDataId);
146 * Getter for a list of all business contacts
148 * @return A list of all business contacts
150 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
151 public List<BasicData> getAllBasicData () {
152 return this.allBasicData;
156 * Getter for filtered basic company data
158 * @return Filtered basic company data
160 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
161 public List<BasicData> getFilteredBasicData () {
162 return this.filteredBasicData;
166 * Setter for filtered basic company data
168 * @param filteredBasicData Filtered basic company data
170 @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
171 public void setFilteredBasicData (final List<BasicData> filteredBasicData) {
172 this.filteredBasicData = filteredBasicData;
176 * Getter for selected basic data
178 * @return Selected basic data
180 public BasicData getSelectedBasicData () {
181 return this.selectedBasicData;
185 * Setter for selected basic data
187 * @param selectedBasicData Selected basic data
189 public void setSelectedBasicData (final BasicData selectedBasicData) {
190 this.selectedBasicData = selectedBasicData;
197 public void initializeList () {
199 if (!this.basicDataCache.iterator().hasNext()) {
201 for (final BasicData basicData : this.basicDataBean.fetchAllBusinessBasicData()) {
203 this.basicDataCache.put(basicData.getBasicDataId(), basicData);
207 // Is cache there and list is not full?
208 if ((this.getAllBasicData().isEmpty()) && (this.basicDataCache.iterator().hasNext())) {
210 for (final Cache.Entry<Long, BasicData> currentEntry : this.basicDataCache) {
212 this.getAllBasicData().add(currentEntry.getValue());
216 this.getAllBasicData().sort(new Comparator<BasicData>() {
218 public int compare (final BasicData basicData1, final BasicData basicData2) {
219 return basicData1.getBasicDataId() > basicData2.getBasicDataId() ? 1 : basicData1.getBasicDataId() < basicData2.getBasicDataId() ? -1 : 0;
224 this.setFilteredBasicData(this.getAllBasicData());
229 public Boolean isCompanyNameUsed (final String companyName) {
230 // Validate parameter
231 if (null == companyName) {
233 throw new NullPointerException("companyName is null"); //NOI18N
234 } else if (companyName.isEmpty()) {
236 throw new IllegalArgumentException("companyName is empty"); //NOI18N
239 // Default is not found
240 boolean isFound = false;
243 for (final BasicData basicData : this.getAllBasicData()) {
244 // Is same company name?
245 if (Objects.equals(basicData.getCompanyName(), companyName)) {
257 public boolean isCompanyRoadNumberUsed (final String companyRoadNumber) {
258 // Validate parameter
259 if (null == companyRoadNumber) {
261 throw new NullPointerException("companyRoadNumber is null"); //NOI18N
262 } else if (companyRoadNumber.isEmpty()) {
264 throw new IllegalArgumentException("companyRoadNumber is empty"); //NOI18N
267 // Default is not found
268 boolean isFound = false;
271 for (final BasicData basicData : this.getAllBasicData()) {
272 // Is same company name?
273 if (Objects.equals(basicData.getCompanyRoadNumber(), companyRoadNumber)) {
285 public Boolean isCompanyShortNameUsed (final String companyShortName) {
286 // Validate parameter
287 if (null == companyShortName) {
289 throw new NullPointerException("companyShortName is null"); //NOI18N
290 } else if (companyShortName.isEmpty()) {
292 throw new IllegalArgumentException("companyShortName is empty"); //NOI18N
295 // Default is not found
296 boolean isFound = false;
299 for (final BasicData basicData : this.getAllBasicData()) {
300 // Is same company name?
301 if (Objects.equals(basicData.getCompanyShortName(), companyShortName)) {
313 public Boolean isEmailAddressRegistered (final String emailAddress) {
314 // Validate parameter
315 if (null == emailAddress) {
317 throw new NullPointerException("emailAddress is null"); //NOI18N
318 } else if (emailAddress.isEmpty()) {
320 throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
323 // Default is not found
324 boolean isFound = false;
327 for (final BasicData basicData : this.getAllBasicData()) {
328 // Is email address used?
329 if (Objects.equals(basicData.getCompanyEmailAddress(), emailAddress)) {