]> git.mxchange.org Git - jfinancials-lib.git/blob - src/org/mxchange/addressbook/validator/addressbook/AddressbookIdValidator.java
Continued:
[jfinancials-lib.git] / src / org / mxchange / addressbook / validator / addressbook / AddressbookIdValidator.java
1 /*
2  * Copyright (C) 2015 Roland Haeder
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
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 General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.addressbook.validator.addressbook;
18
19 import java.text.MessageFormat;
20 import javax.ejb.EJB;
21 import javax.enterprise.event.Event;
22 import javax.enterprise.inject.Any;
23 import javax.faces.application.FacesMessage;
24 import javax.faces.component.UIComponent;
25 import javax.faces.context.FacesContext;
26 import javax.faces.validator.FacesValidator;
27 import javax.faces.validator.ValidatorException;
28 import javax.inject.Inject;
29 import org.mxchange.addressbook.events.AddressbookLoadedEvent;
30 import org.mxchange.addressbook.events.LoadedAddressbookEvent;
31 import org.mxchange.addressbook.exceptions.AddressbookNotFoundException;
32 import org.mxchange.addressbook.model.addressbook.Addressbook;
33 import org.mxchange.addressbook.model.addressbook.AddressbookSessionBeanRemote;
34 import org.mxchange.jcoreee.validator.number.BaseLongValidator;
35
36 /**
37  * A validator for address book id verification
38  * <p>
39  * @author Roland Haeder
40  */
41 @FacesValidator (value = "AddressbookIdValidator")
42 public class AddressbookIdValidator extends BaseLongValidator {
43
44         /**
45          * Serial number
46          */
47         private static final long serialVersionUID = 158_768_467_186_951_809L;
48
49         /**
50          * Remote bean
51          */
52         @EJB (mappedName = "ejb/stateless-addressbook")
53         private AddressbookSessionBeanRemote addressbookBean;
54
55         /**
56          * An event for loading address book data loading
57          */
58         @Inject
59         @Any
60         private Event<AddressbookLoadedEvent> loadedEvent;
61
62         @Override
63         public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException {
64                 // Trace message
65                 //this.getLogger().logTrace(MessageFormat.format("validate: context={0},component={1},value={2} - CALLED!", context, component, value)); //NOI18N
66
67                 // All accepted, required fields
68                 String[] requiredFileds = {"addressbookId"}; //NOI18N
69
70                 // Pre-validation (example: not null, not a string, empty string ...)
71                 super.preValidate(context, component, value, requiredFileds, false);
72
73                 // Cast to long
74                 Long addressbookId = (Long) value;
75
76                 // Is the address book id valid?
77                 if (!this.addressbookBean.isAddressbookIdUsed(addressbookId)) {
78                         // Is not valid
79                         throw new ValidatorException(new FacesMessage(MessageFormat.format("No address book found with id {0}. Please check your link.", addressbookId))); //NOI18N
80                 }
81
82                 // Init variable
83                 Addressbook addressbook;
84
85                 // Try it
86                 try {
87                         // Get full data
88                         addressbook = this.addressbookBean.getAddressbookById(addressbookId);
89                 } catch (final AddressbookNotFoundException ex) {
90                         // Continue to throw
91                         throw new ValidatorException(new FacesMessage(MessageFormat.format("Cannot find address book with id {0}", addressbookId)), ex);
92                 }
93
94                 // Fire event
95                 this.loadedEvent.fire(new LoadedAddressbookEvent(addressbook));
96
97                 // Trace message
98                 //this.getLogger().logTrace("validate: EXIT!"); //NOI18N
99         }
100 }