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.exceptions.basicdata.BasicDataNotFoundException;
34 import org.mxchange.jcontactsbusiness.model.basicdata.AdminBasicCompanyDataSessionBeanRemote;
35 import org.mxchange.jcontactsbusiness.model.basicdata.BasicCompanyDataSessionBeanRemote;
36 import org.mxchange.jcontactsbusiness.model.basicdata.BasicData;
37 import org.mxchange.jcountry.model.data.Country;
38 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
39 import org.mxchange.jfinancials.beans.user.login.FinancialsUserLoginWebSessionController;
42 * A business contact bean (controller)
44 * @author Roland Häder<roland@mxchange.org>
46 @Named ("basicCompanyDataController")
48 public class FinancialsBusinessDataWebRequestBean extends BaseFinancialsBean implements FinancialsBusinessDataWebRequestController {
53 private static final long serialVersionUID = 56_189_028_928_371L;
56 * EJB for administrative basic business data purposes
58 @EJB (lookup = "java:global/jfinancials-ejb/adminBasicCompanyData!org.mxchange.jcontactsbusiness.model.basicdata.AdminBasicCompanyDataSessionBeanRemote", description = "A stateless session bean for administrative purposes.")
59 private AdminBasicCompanyDataSessionBeanRemote adminBasicCompanyDataBean;
62 * List of all basic company data
64 private List<BasicData> allBasicData;
67 * A list of all registered companies (globally)
70 @NamedCache (cacheName = "basicDataCache")
71 private Cache<Long, BasicData> basicDataCache;
74 * EJB for general basic business data purposes
76 @EJB (lookup = "java:global/jfinancials-ejb/basicCompanyData!org.mxchange.jcontactsbusiness.model.basicdata.BasicCompanyDataSessionBeanRemote", description = "A stateless session bean for general purposes.")
77 private BasicCompanyDataSessionBeanRemote businessDataBean;
80 * Comments for this company
82 private String companyComments;
85 * Companies (main) email address (example: info@company.example)
87 private String companyEmailAddress;
92 private String companyName;
95 * Area code for fax number
97 private Integer faxAreaCode;
100 * Country for fax number
102 private Country faxCountry;
105 * Dial number for fax number
107 private Long faxNumber;
110 * List of filtered basic company data
112 private List<BasicData> filteredBasicCompanyData;
115 * Area code for land-line number
117 private Integer landLineAreaCode;
120 * Country for land-line number
122 private Country landLineCountry;
125 * Dial number for land-line number
127 private Long landLineNumber;
133 private FinancialsUserLoginWebSessionController userLoginController;
138 public FinancialsBusinessDataWebRequestBean () {
139 // Call super constructor
143 this.allBasicData = new LinkedList<>();
147 * Observers events being fired when an administrator has added company
150 * @param event Event being fired
152 public void afterAdminAddedBasicCompanyDataEvent (@Observes final ObservableAdminAddedBusinessBasicDataEvent event) {
153 // Is the parameter valid?
156 throw new NullPointerException("event is null");
157 } else if (event.getBasicData() == null) {
159 throw new NullPointerException("event.basicData is null");
160 } else if (event.getBasicData().getBasicDataId() == null) {
162 throw new NullPointerException("event.basicData.basicDataId is null");
163 } else if (event.getBasicData().getBasicDataId() < 1) {
165 throw new IllegalArgumentException(MessageFormat.format("event.basicData.basicDataId={0} is invalid", event.getBasicData().getBasicDataId()));
166 } else if (event.getBasicData().getCompanyName() == null) {
168 throw new NullPointerException("event.basicData.companyName is null");
169 } else if (event.getBasicData().getCompanyName().isEmpty()) {
171 throw new IllegalArgumentException("event.basicData.companyName is empty");
175 this.basicDataCache.put(event.getBasicData().getBasicDataId(), event.getBasicData());
176 this.allBasicData.add(event.getBasicData());
180 * Getter for a list of all business contacts
182 * @return A list of all business contacts
184 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
185 public List<BasicData> allBasicData () {
186 return this.allBasicData;
190 public BasicData findBasicDataById (final Long basicDataId) throws BasicDataNotFoundException {
191 // Validate parameter
192 if (null == basicDataId) {
194 throw new NullPointerException("basicDataId is null"); //NOI18N
195 } else if (basicDataId < 1) {
197 throw new IllegalArgumentException("basicDataId=" + basicDataId + " is invalid"); //NOI18N
198 } else if (!this.basicDataCache.containsKey(basicDataId)) {
200 throw new BasicDataNotFoundException(basicDataId);
204 final BasicData basicData = this.basicDataCache.get(basicDataId);
211 * Getter for comments
215 public String getCompanyComments () {
216 return this.companyComments;
220 * Setter for comments
222 * @param companyComments Comments
224 public void setCompanyComments (final String companyComments) {
225 this.companyComments = companyComments;
229 * Getter for company's (main) email address
231 * @return Company's (main) email address
233 public String getCompanyEmailAddress () {
234 return this.companyEmailAddress;
238 * Setter for company's (main) email address
240 * @param companyEmailAddress Company's (main) email address
242 public void setCompanyEmailAddress (final String companyEmailAddress) {
243 this.companyEmailAddress = companyEmailAddress;
247 * Getter for company cacheName
249 * @return Company cacheName
251 public String getCompanyName () {
252 return this.companyName;
256 * Setter for company cacheName
258 * @param companyName Company cacheName
260 public void setCompanyName (final String companyName) {
261 this.companyName = companyName;
265 * Getter for fax number's area code
267 * @return Fax number's area code
269 public Integer getFaxAreaCode () {
270 return this.faxAreaCode;
274 * Setter for fax number's area code
276 * @param faxAreaCode Fax number's area code
278 public void setFaxAreaCode (final Integer faxAreaCode) {
279 this.faxAreaCode = faxAreaCode;
283 * Getter for fax's country instance
285 * @return Fax' country instance
287 public Country getFaxCountry () {
288 return this.faxCountry;
292 * Setter for fax's country instance
294 * @param faxCountry Fax' country instance
296 public void setFaxCountry (final Country faxCountry) {
297 this.faxCountry = faxCountry;
301 * Getter for fax number
305 public Long getFaxNumber () {
306 return this.faxNumber;
310 * Setter for fax number
312 * @param faxNumber Fax number
314 public void setFaxNumber (final Long faxNumber) {
315 this.faxNumber = faxNumber;
319 * Getter for filtered basic company data
321 * @return Filtered basic company data
323 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
324 public List<BasicData> getFilteredBasicCompanyData () {
325 return this.filteredBasicCompanyData;
329 * Setter for filtered basic company data
331 * @param filteredBasicCompanyData Filtered basic company data
333 @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
334 public void setFilteredBasicCompanyData (final List<BasicData> filteredBasicCompanyData) {
335 this.filteredBasicCompanyData = filteredBasicCompanyData;
339 * Getter for land-line number's area code
341 * @return Land-line number's area code
343 public Integer getLandLineAreaCode () {
344 return this.landLineAreaCode;
348 * Setter for land-line number's area code
350 * @param landLineAreaCode Land-line number's area code
352 public void setLandLineAreaCode (final Integer landLineAreaCode) {
353 this.landLineAreaCode = landLineAreaCode;
357 * Getter for land-line number's country instance
359 * @return Land-line number's country instance
361 public Country getLandLineCountry () {
362 return this.landLineCountry;
366 * Setter for land-line number's country instance
368 * @param landLineCountry Land-line number's country instance
370 public void setLandLineCountry (final Country landLineCountry) {
371 this.landLineCountry = landLineCountry;
375 * Getter for land-line number
377 * @return Land-line number
379 public Long getLandLineNumber () {
380 return this.landLineNumber;
384 * Setter for land-line number
386 * @param landLineNumber Land-line number
388 public void setLandLineNumber (final Long landLineNumber) {
389 this.landLineNumber = landLineNumber;
396 public void initializeList () {
398 if (!this.basicDataCache.iterator().hasNext()) {
400 final List<BasicData> basicDatas = this.businessDataBean.allBusinessBasicData();
403 for (final BasicData basicData : basicDatas) {
405 this.basicDataCache.put(basicData.getBasicDataId(), basicData);
409 // Is cache there and list is not full?
410 if ((this.allBasicData.isEmpty()) && (this.basicDataCache.iterator().hasNext())) {
412 final Iterator<Cache.Entry<Long, BasicData>> iterator = this.basicDataCache.iterator();
415 while (iterator.hasNext()) {
417 final Cache.Entry<Long, BasicData> next = iterator.next();
420 this.allBasicData.add(next.getValue());
424 this.allBasicData.sort(new Comparator<BasicData>() {
426 public int compare (final BasicData o1, final BasicData o2) {
427 return o1.getBasicDataId() > o2.getBasicDataId() ? 1 : o1.getBasicDataId() < o2.getBasicDataId() ? -1 : 0;
434 public Boolean isCompanyNameUsed (String companyName) {
435 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
441 private void clear () {
443 this.setCompanyComments(null);
444 this.setCompanyEmailAddress(null);
445 this.setCompanyName(null);
446 this.setFaxAreaCode(null);
447 this.setFaxCountry(null);
448 this.setFaxNumber(null);
449 this.setLandLineAreaCode(null);
450 this.setLandLineCountry(null);
451 this.setLandLineNumber(null);