org.javalid.external.db.annotations.validation
Annotation Type DbNumCheck


@Target(value={TYPE,METHOD,FIELD})
@Retention(value=RUNTIME)
@Documented
public @interface DbNumCheck

If a method or field is annotated with this annotation it means that its value must be checked against a database query. The query must return a single result. In general you'd best use a count query, where your return a value which determines whether the value is valid or not.

The check is valid if the query returns the expected value of this annotation. An example, both do the same, one defines the query directly. The other refers to a query defined in the xml configuration file:

 @DbNumCheck(refDsId="myDS",query="select count(*) from table x where x.id=${this}")
 @DbNumCheck(refDsId="myDS", refQueryId="myQuery")
 
Note the ${this}, refers to the current METHOD/FIELD it is annotated to (thus its value will be bound).

Finally: You can annotate field/methods with the annotation, but also on a class. If specified on class level it is applied from there (you can add multiple of these constraints in that case, if you wish).

Since:
1.1
Version:
1.0
Author:
M.Reuvers

Required Element Summary
 java.lang.String refDsId
          Id of datasource defined in the xml config file for the database extension.
 
Optional Element Summary
 java.lang.String[] applyToGroups
          Specify for which groups this annotation must be applied.
 java.lang.String customCode
          Optional custom code (property from a properties file).
 boolean globalMessage
          If this annotation's message must be added as global message instead of real validation path.
 long maxValue
          Must be set when mode() is set to MODE_BETWEEN, this represents the maximum value then (inclusive).
 long minValue
          Must be set when mode() is set to MODE_BETWEEN, this represents the minimum value then (inclusive).
 int mode
          The mode the check is in, defaults to equals.
 java.lang.String query
          Actual sql query to use.
 java.lang.String refQueryId
          Id of query defined in the xml config file for the database extension.
 long value
          The value returned by the query (must be int / long).
 

Element Detail

refDsId

public abstract java.lang.String refDsId
Id of datasource defined in the xml config file for the database extension.

refQueryId

public abstract java.lang.String refQueryId
Id of query defined in the xml config file for the database extension. Defaults to "", either this one or query is required!

Default:
""

query

public abstract java.lang.String query
Actual sql query to use. Defaults to "", either this one or refQueryId is required.

Default:
""

value

public abstract long value
The value returned by the query (must be int / long). If it is conform the mode() it is valid (e.g. equals mode, means the value must match exactly). Otherwise the check is assumed to be invalid. Defaults to 1. Note that this one is required for all MODE_ constants, except MODE_BETWEEN, you must specify minValue() and maxValue() then instead.

Default:
1L

minValue

public abstract long minValue
Must be set when mode() is set to MODE_BETWEEN, this represents the minimum value then (inclusive). Defaults to 1L.

Default:
1L

maxValue

public abstract long maxValue
Must be set when mode() is set to MODE_BETWEEN, this represents the maximum value then (inclusive). Defaults to 1L.

Default:
1L

mode

public abstract int mode
The mode the check is in, defaults to equals. Thus annotated method/field must equal the value(). Use one of the MODE_ constants to change it, note that MODE_BETWEEN requires you to set minValue() and maxValue() instead of value().

Default:
0

customCode

public abstract java.lang.String customCode
Optional custom code (property from a properties file). If set, this one is used instead of the default one specified in MessageCodes for annotated method.

This way you can override the default message if needed.

Default:
""

applyToGroups

public abstract java.lang.String[] applyToGroups
Specify for which groups this annotation must be applied. By default if you annotate without this variable, the annotation is applied for any group on that method (which is what you usually want). However by specifying the exact groups you can turn on/off a validation on a method depending on the group you validate.

Defaults to JvGroup.GROUP_APPLY_ALL

Default:
"_all_"

globalMessage

public abstract boolean globalMessage
If this annotation's message must be added as global message instead of real validation path. Defaults to false (thus will be added with validation path if invalid).

Default:
false