]> git.mxchange.org Git - jcore-utils.git/blob - src/org/mxchange/jcoreee/validator/BaseObjectValidator.java
Harr, this logger thing does only show LoggerBean.java as caller, not the actual...
[jcore-utils.git] / src / org / mxchange / jcoreee / validator / BaseObjectValidator.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.jcoreee.validator;
18
19 import java.text.MessageFormat;
20 import java.util.Arrays;
21 import javax.ejb.EJB;
22 import javax.faces.application.FacesMessage;
23 import javax.faces.component.UIComponent;
24 import javax.faces.context.FacesContext;
25 import javax.faces.validator.Validator;
26 import javax.faces.validator.ValidatorException;
27 import org.mxchange.jcoreee.BaseEeSystem;
28 import org.mxchange.jcoreeelogger.beans.local.logger.Log;
29 import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
30
31 /**
32  * A general object validation class. Please implement javax.faces.validator.Validator
33  * (with import line!) and call preValidate(). You also may want to try out some
34  * other BaseFooValidator classes before directly inheriting from this class.
35  *
36  * @author Roland Haeder
37  */
38 public abstract class BaseObjectValidator extends BaseEeSystem implements Validator {
39
40         /**
41          * Logger instance
42          */
43         @EJB
44         @Log
45         private LoggerBeanLocal logger;
46
47         /**
48          * Needs to be implemented as the Validator interface needs to be implemented.
49          *
50          * @param context
51          * @param component
52          * @param value
53          * @throws ValidatorException 
54          */
55         @Override
56         abstract public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException;
57
58         /**
59          * The method pre-validates the given value. It makes sure that the component's id is found in
60          * requiredFields and is not null. Once the component's id has been found, it stops iteration on
61          * requiredFields (which saves execution time).
62          *
63          * @param context FacesContext instance
64          * @param component UIComponent instance
65          * @param value Value to check
66          * @param requiredFields Array of required field names (ending with)
67          * @throws ValidatorException If something more horrible went wrong
68          */
69         protected void preValidate (final FacesContext context, final UIComponent component, final Object value, final String[] requiredFields) throws ValidatorException {
70                 // Trace message
71                 this.getLogger().logTrace(MessageFormat.format("preValidate: context={0},component={1},value={2},requiredFields={3} - CALLED!", context, component, value, Arrays.toString(requiredFields))); //NOI18N
72
73                 // Init message and key
74                 FacesMessage facesMessage = null;
75                 String errKey = "error.unknown_id"; //NOI18N
76
77                 // Get client id
78                 final String clientId = component.getClientId();
79
80                 // Default is no field is valid
81                 boolean isValidField = false;
82
83                 // Check component's id against required fields and find a match
84                 for (final String field : requiredFields) {
85                         // Get logger
86                         this.getLogger().logDebug(MessageFormat.format("preValidate: field={0},clientId={1}", field, clientId)); //NOI18N
87
88                         // Is it the same?
89                         if (clientId.endsWith(field)) {
90                                 // Is valid field
91                                 isValidField = true;
92
93                                 // Is it null?
94                                 if (null == value) {
95                                         errKey = String.format("error.%s.is_null", field); //NOI18N
96
97                                         // Value it null
98                                         facesMessage = new FacesMessage(getMessageStringFromKey(errKey));
99                                 }
100
101                                 // Abort here
102                                 break;
103                         }
104                 }
105
106                 // Debug message
107                 this.getLogger().logDebug(MessageFormat.format("preValidate: isValidField={0}", isValidField)); //NOI18N
108
109                 // Valid field?
110                 if (!isValidField) {
111                         // Invalid field
112                         facesMessage = new FacesMessage(MessageFormat.format(errKey, clientId));
113                 }
114
115                 // Debug message
116                 this.getLogger().logDebug(MessageFormat.format("preValidate: facesMessage={0}", facesMessage)); //NOI18N
117
118                 // Is it not null?
119                 if (null != facesMessage) {
120                         throw new ValidatorException(facesMessage);
121                 }
122
123                 // Trace message
124                 this.getLogger().logTrace("preValidate: EXIT!"); //NOI18N
125         }
126
127         /**
128          * Getter for local bean logger
129          *
130          * @return Local bean logger
131          */
132         protected LoggerBeanLocal getLogger () {
133                 return this.logger;
134         }
135 }