/
AbstractConstraint.cfc
99 lines (82 loc) · 2.22 KB
/
AbstractConstraint.cfc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* Abstract component for constraints to extend
*
* @author Daniel Vega
*/
component accessors="true" {
property name="clazz";
property name="constraintName";
property name="constraintParamater";
public boolean function validate(any target, any property, any value){
return processValidate(argumentCollection = arguments);
}
/**
* I am the full class path of the constraint
*
* @param target the constraint object
*/
public void function setConstraintClass(any target){
variables.constraintClass = getMetaData(arguments.target).name;
}
/**
* I will return the entity name of an orm class or an empty string if it doesn't exist
*/
public String function getEntityName(Any target){
var meta = getMetadata(arguments.target);
var entity = "";
if( structKeyExists(meta,"persistent") && meta.persistent ){
if(structKeyExists(meta,"entity")){
entity = meta.entity;
} else if(structKeyExists(meta,"entityName")) {
entity = meta.entityName;
} else {
if(structKeyExists(meta,"fullname")){
entity = listLast(meta.fullname,".");
}
}
}
return entity;
}
public String function getPropertyName(Any target,String key){
var meta = getMetadata(arguments.target);
var property = arguments.key;
for(var x=1; x <= arrayLen(meta.properties); ++x){
if( findNoCase(arguments.key,meta.properties[x].name) ){
return meta.properties[x].name;
}
}
return property;
}
/**
* There are special variables that you can use in your validation rules
* This method will replace the variable with the actual value
*/
public any function getSpecialVar(any param){
switch(arguments.param){
case "$now" : {
return now();
break;
}
}
}
public boolean function isSpecialVar(param){
return listFindNoCase("$now",param);
}
public boolean function passOnNULL(){
return true;
}
/**
* I will return the id of an entity
*/
public any function getFieldTypeId(any entity){
var meta = getMetaData(arguments.entity);
var id = 0;
for( var x=1; x <= arrayLen(meta.properties); x++){
if( structKeyExists(meta.properties[x],"fieldtype") && meta.properties[x].fieldtype == "id"){
id = meta.properties[x].name;
break;
}
}
return id;
}
}