2 * Copyright (C) 2015 Roland Haeder
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.
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.
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/>.
17 package org.mxchange.addressbook.validator.addressbook;
19 import java.text.MessageFormat;
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;
37 * A validator for address book id verification
39 * @author Roland Haeder
41 @FacesValidator (value = "AddressbookIdValidator")
42 public class AddressbookIdValidator extends BaseLongValidator {
47 private static final long serialVersionUID = 158_768_467_186_951_809L;
52 @EJB (mappedName = "ejb/stateless-addressbook")
53 private AddressbookSessionBeanRemote addressbookBean;
56 * An event for loading address book data loading
60 private Event<AddressbookLoadedEvent> loadedEvent;
63 public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException {
65 //this.getLogger().logTrace(MessageFormat.format("validate: context={0},component={1},value={2} - CALLED!", context, component, value)); //NOI18N
67 // All accepted, required fields
68 String[] requiredFileds = {"addressbookId"}; //NOI18N
70 // Pre-validation (example: not null, not a string, empty string ...)
71 super.preValidate(context, component, value, requiredFileds, false);
74 Long addressbookId = (Long) value;
76 // Is the address book id valid?
77 if (!this.addressbookBean.isAddressbookIdUsed(addressbookId)) {
79 throw new ValidatorException(new FacesMessage(MessageFormat.format("No address book found with id {0}. Please check your link.", addressbookId))); //NOI18N
83 Addressbook addressbook;
88 addressbook = this.addressbookBean.getAddressbookById(addressbookId);
89 } catch (final AddressbookNotFoundException ex) {
91 throw new ValidatorException(new FacesMessage(MessageFormat.format("Cannot find address book with id {0}", addressbookId)), ex);
95 this.loadedEvent.fire(new LoadedAddressbookEvent(addressbook));
98 //this.getLogger().logTrace("validate: EXIT!"); //NOI18N