2 * Copyright (C) 2016, 2017 Roland Häder
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;
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 javax.annotation.PostConstruct;
26 import javax.cache.Cache;
28 import javax.enterprise.context.RequestScoped;
29 import javax.enterprise.event.Observes;
30 import javax.inject.Inject;
31 import javax.inject.Named;
32 import org.mxchange.jcontactsbusiness.events.basicdata.added.ObservableAdminAddedBusinessBasicDataEvent;
33 import org.mxchange.jcontactsbusiness.model.basicdata.AdminBasicCompanyDataSessionBeanRemote;
34 import org.mxchange.jcontactsbusiness.model.basicdata.BasicCompanyDataSessionBeanRemote;
35 import org.mxchange.jcontactsbusiness.model.basicdata.BusinessBasicData;
36 import org.mxchange.jcountry.model.data.Country;
37 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
38 import org.mxchange.jfinancials.beans.user.login.FinancialsUserLoginWebSessionController;
41 * A business contact bean (controller)
43 * @author Roland Häder<roland@mxchange.org>
45 @Named ("basicCompanyDataController")
47 public class FinancialsBusinessDataWebRequestBean extends BaseFinancialsBean implements FinancialsBusinessDataWebRequestController {
52 private static final long serialVersionUID = 56_189_028_928_371L;
55 * EJB for administrative basic business data purposes
57 @EJB (lookup = "java:global/jfinancials-ejb/adminBasicCompanyData!org.mxchange.jcontactsbusiness.model.basicdata.AdminBasicCompanyDataSessionBeanRemote", description = "A stateless session bean for administrative purposes.")
58 private AdminBasicCompanyDataSessionBeanRemote adminBasicCompanyDataBean;
61 * List of all basic company data
63 private List<BusinessBasicData> allCompanyBasicData;
66 * A list of all registered companies (globally)
69 @NamedCache (cacheName = "basicDataCache")
70 private Cache<Long, BusinessBasicData> basicDataCache;
73 * EJB for general basic business data purposes
75 @EJB (lookup = "java:global/jfinancials-ejb/basicCompanyData!org.mxchange.jcontactsbusiness.model.basicdata.BasicCompanyDataSessionBeanRemote", description = "A stateless session bean for general purposes.")
76 private BasicCompanyDataSessionBeanRemote businessDataBean;
79 * Comments for this company
81 private String companyComments;
84 * Companies (main) email address (example: info@company.example)
86 private String companyEmailAddress;
91 private String companyName;
94 * Area code for fax number
96 private Integer faxAreaCode;
99 * Country for fax number
101 private Country faxCountry;
104 * Dial number for fax number
106 private Long faxNumber;
109 * List of filtered basic company data
111 private List<BusinessBasicData> filteredBasicCompanyData;
114 * Area code for land-line number
116 private Integer landLineAreaCode;
119 * Country for land-line number
121 private Country landLineCountry;
124 * Dial number for land-line number
126 private Long landLineNumber;
132 private FinancialsUserLoginWebSessionController userLoginController;
137 public FinancialsBusinessDataWebRequestBean () {
138 // Call super constructor
142 this.allCompanyBasicData = new LinkedList<>();
146 * Observers events being fired when an administrator has added company
149 * @param event Event being fired
151 public void afterAdminAddedBasicCompanyDataEvent (@Observes final ObservableAdminAddedBusinessBasicDataEvent event) {
152 // Is the parameter valid?
155 throw new NullPointerException("event is null");
156 } else if (event.getBasicData() == null) {
158 throw new NullPointerException("event.basicData is null");
159 } else if (event.getBasicData().getBasicDataId() == null) {
161 throw new NullPointerException("event.basicData.basicDataId is null");
162 } else if (event.getBasicData().getBasicDataId() < 1) {
164 throw new IllegalArgumentException(MessageFormat.format("event.basicData.basicDataId={0} is invalid", event.getBasicData().getBasicDataId()));
165 } else if (event.getBasicData().getCompanyName() == null) {
167 throw new NullPointerException("event.basicData.companyName is null");
168 } else if (event.getBasicData().getCompanyName().isEmpty()) {
170 throw new IllegalArgumentException("event.basicData.companyName is empty");
174 this.basicDataCache.put(event.getBasicData().getBasicDataId(), event.getBasicData());
175 this.allCompanyBasicData.add(event.getBasicData());
179 * Getter for a list of all business contacts
181 * @return A list of all business contacts
183 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
184 public List<BusinessBasicData> allCompanyBasicData () {
185 return this.allCompanyBasicData;
189 * Getter for comments
193 public String getCompanyComments () {
194 return this.companyComments;
198 * Setter for comments
200 * @param companyComments Comments
202 public void setCompanyComments (final String companyComments) {
203 this.companyComments = companyComments;
207 * Getter for company's (main) email address
209 * @return Company's (main) email address
211 public String getCompanyEmailAddress () {
212 return this.companyEmailAddress;
216 * Setter for company's (main) email address
218 * @param companyEmailAddress Company's (main) email address
220 public void setCompanyEmailAddress (final String companyEmailAddress) {
221 this.companyEmailAddress = companyEmailAddress;
225 * Getter for company cacheName
227 * @return Company cacheName
229 public String getCompanyName () {
230 return this.companyName;
234 * Setter for company cacheName
236 * @param companyName Company cacheName
238 public void setCompanyName (final String companyName) {
239 this.companyName = companyName;
243 * Getter for fax number's area code
245 * @return Fax number's area code
247 public Integer getFaxAreaCode () {
248 return this.faxAreaCode;
252 * Setter for fax number's area code
254 * @param faxAreaCode Fax number's area code
256 public void setFaxAreaCode (final Integer faxAreaCode) {
257 this.faxAreaCode = faxAreaCode;
261 * Getter for fax's country instance
263 * @return Fax' country instance
265 public Country getFaxCountry () {
266 return this.faxCountry;
270 * Setter for fax's country instance
272 * @param faxCountry Fax' country instance
274 public void setFaxCountry (final Country faxCountry) {
275 this.faxCountry = faxCountry;
279 * Getter for fax number
283 public Long getFaxNumber () {
284 return this.faxNumber;
288 * Setter for fax number
290 * @param faxNumber Fax number
292 public void setFaxNumber (final Long faxNumber) {
293 this.faxNumber = faxNumber;
297 * Getter for filtered basic company data
299 * @return Filtered basic company data
301 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
302 public List<BusinessBasicData> getFilteredBasicCompanyData () {
303 return this.filteredBasicCompanyData;
307 * Setter for filtered basic company data
309 * @param filteredBasicCompanyData Filtered basic company data
311 @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
312 public void setFilteredBasicCompanyData (final List<BusinessBasicData> filteredBasicCompanyData) {
313 this.filteredBasicCompanyData = filteredBasicCompanyData;
317 * Getter for land-line number's area code
319 * @return Land-line number's area code
321 public Integer getLandLineAreaCode () {
322 return this.landLineAreaCode;
326 * Setter for land-line number's area code
328 * @param landLineAreaCode Land-line number's area code
330 public void setLandLineAreaCode (final Integer landLineAreaCode) {
331 this.landLineAreaCode = landLineAreaCode;
335 * Getter for land-line number's country instance
337 * @return Land-line number's country instance
339 public Country getLandLineCountry () {
340 return this.landLineCountry;
344 * Setter for land-line number's country instance
346 * @param landLineCountry Land-line number's country instance
348 public void setLandLineCountry (final Country landLineCountry) {
349 this.landLineCountry = landLineCountry;
353 * Getter for land-line number
355 * @return Land-line number
357 public Long getLandLineNumber () {
358 return this.landLineNumber;
362 * Setter for land-line number
364 * @param landLineNumber Land-line number
366 public void setLandLineNumber (final Long landLineNumber) {
367 this.landLineNumber = landLineNumber;
374 public void initializeList () {
376 if (!this.basicDataCache.iterator().hasNext()) {
378 final List<BusinessBasicData> basicDatas = this.businessDataBean.allCompanyBasicData();
381 for (final BusinessBasicData basicData : basicDatas) {
383 this.basicDataCache.put(basicData.getBasicDataId(), basicData);
387 // Is cache there and list is not full?
388 if ((this.allCompanyBasicData.isEmpty()) && (this.basicDataCache.iterator().hasNext())) {
390 final Iterator<Cache.Entry<Long, BusinessBasicData>> iterator = this.basicDataCache.iterator();
393 while (iterator.hasNext()) {
395 final Cache.Entry<Long, BusinessBasicData> next = iterator.next();
398 this.allCompanyBasicData.add(next.getValue());
402 this.allCompanyBasicData.sort(new Comparator<BusinessBasicData>() {
404 public int compare (final BusinessBasicData o1, final BusinessBasicData o2) {
405 return o1.getBasicDataId() > o2.getBasicDataId() ? 1 : o1.getBasicDataId() < o2.getBasicDataId() ? -1 : 0;
414 private void clear () {
416 this.setCompanyComments(null);
417 this.setCompanyEmailAddress(null);
418 this.setCompanyName(null);
419 this.setFaxAreaCode(null);
420 this.setFaxCountry(null);
421 this.setFaxNumber(null);
422 this.setLandLineAreaCode(null);
423 this.setLandLineCountry(null);
424 this.setLandLineNumber(null);