2 * Copyright (C) 2017, 2018 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 public FinancialsBasicDataListWebViewBean () {
80 // Call super constructor
84 this.allBasicData = new LinkedList<>();
88 * Observers events being fired when an administrator has added company
91 * @param event Event being fired
93 public void afterAdminAddedBasicCompanyDataEvent (@Observes final ObservableAdminAddedBusinessBasicDataEvent event) {
94 // Is the parameter valid?
97 throw new NullPointerException("event is null"); //NOI18N
98 } else if (event.getBasicData() == null) {
100 throw new NullPointerException("event.basicData is null"); //NOI18N
101 } else if (event.getBasicData().getBasicDataId() == null) {
103 throw new NullPointerException("event.basicData.basicDataId is null"); //NOI18N
104 } else if (event.getBasicData().getBasicDataId() < 1) {
106 throw new IllegalArgumentException(MessageFormat.format("event.basicData.basicDataId={0} is invalid", event.getBasicData().getBasicDataId())); //NOI18N
107 } else if (event.getBasicData().getCompanyName() == null) {
109 throw new NullPointerException("event.basicData.companyName is null"); //NOI18N
110 } else if (event.getBasicData().getCompanyName().isEmpty()) {
112 throw new IllegalArgumentException("event.basicData.companyName is empty"); //NOI18N
116 this.basicDataCache.put(event.getBasicData().getBasicDataId(), event.getBasicData());
117 this.getAllBasicData().add(event.getBasicData());
121 public BasicData findBasicDataById (final Long basicDataId) throws BasicDataNotFoundException {
122 // Validate parameter
123 if (null == basicDataId) {
125 throw new NullPointerException("basicDataId is null"); //NOI18N
126 } else if (basicDataId < 1) {
128 throw new IllegalArgumentException(MessageFormat.format("basicDataId={0} is invalid", basicDataId)); //NOI18N
129 } else if (!this.basicDataCache.containsKey(basicDataId)) {
131 throw new BasicDataNotFoundException(basicDataId);
135 final BasicData basicData = this.basicDataCache.get(basicDataId);
142 * Getter for a list of all business contacts
144 * @return A list of all business contacts
146 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
147 public List<BasicData> getAllBasicData () {
148 return this.allBasicData;
152 * Getter for filtered basic company data
154 * @return Filtered basic company data
156 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
157 public List<BasicData> getFilteredBasicData () {
158 return this.filteredBasicData;
162 * Setter for filtered basic company data
164 * @param filteredBasicData Filtered basic company data
166 @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
167 public void setFilteredBasicData (final List<BasicData> filteredBasicData) {
168 this.filteredBasicData = filteredBasicData;
175 public void initializeList () {
177 if (!this.basicDataCache.iterator().hasNext()) {
179 final List<BasicData> basicDatas = this.basicDataBean.allBusinessBasicData();
182 for (final BasicData basicData : basicDatas) {
184 this.basicDataCache.put(basicData.getBasicDataId(), basicData);
188 // Is cache there and list is not full?
189 if ((this.getAllBasicData().isEmpty()) && (this.basicDataCache.iterator().hasNext())) {
191 final Iterator<Cache.Entry<Long, BasicData>> iterator = this.basicDataCache.iterator();
194 while (iterator.hasNext()) {
196 final Cache.Entry<Long, BasicData> next = iterator.next();
199 this.getAllBasicData().add(next.getValue());
203 this.getAllBasicData().sort(new Comparator<BasicData>() {
205 public int compare (final BasicData o1, final BasicData o2) {
206 return o1.getBasicDataId() > o2.getBasicDataId() ? 1 : o1.getBasicDataId() < o2.getBasicDataId() ? -1 : 0;
211 this.setFilteredBasicData(this.getAllBasicData());
216 public Boolean isCompanyNameUsed (final String companyName) {
217 // Validate parameter
218 if (null == companyName) {
220 throw new NullPointerException("companyName is null"); //NOI18N
221 } else if (companyName.isEmpty()) {
223 throw new IllegalArgumentException("companyName is empty"); //NOI18N
226 // Default is not found
227 boolean isFound = false;
230 for (final BasicData basicData : this.getAllBasicData()) {
231 // Is same company name?
232 if (Objects.equals(basicData.getCompanyName(), companyName)) {
244 public Boolean isEmailAddressRegistered (final String emailAddress) {
245 // Validate parameter
246 if (null == emailAddress) {
248 throw new NullPointerException("emailAddress is null"); //NOI18N
249 } else if (emailAddress.isEmpty()) {
251 throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
254 // Default is not found
255 boolean isFound = false;
258 for (final BasicData basicData : this.getAllBasicData()) {
259 // Is email address used?
260 if (Objects.equals(basicData.getCompanyEmailAddress(), emailAddress)) {