Administrator
2023-04-21 195945efc5db921a4c9eb8cf9421c172273293f5
提交 | 用户 | 时间
58d006 1 package com.mandi.common;
A 2
3 import java.util.HashSet;
4 import java.util.Iterator;
5 import java.util.Set;
6
7 import javax.validation.ConstraintViolation;
8 import javax.validation.Validation;
9 import javax.validation.Validator;
10 import javax.validation.ValidatorFactory;
11
12 import com.mandi.dao.common.ObjectResult;
13
14 /** 
15  * @author mengly 
16  * @version 创建时间:2016年6月3日 下午1:18:57 
17  * 类说明 :hibernate validator
18  */
19
20 public class ValidatorUtils {
21     private static Validator validator=null;
22     public static Validator getValidator()
23     {
24         if(validator==null)
25         {
26              ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
27              validator=factory.getValidator();
28         }
29         return validator;
30     }
31     public static <T> Set<ConstraintViolation<T>> checkCTS(T t)
32     {
33         Validator v=ValidatorUtils.getValidator();
34         Set<ConstraintViolation<T>> cts=v.validate(t);
35         return cts;
36     }
37     public static <T> ObjectResult<T> checkForMsgs(T t)
38     {
39         ObjectResult<T> objr=new ObjectResult<T>();
40         if(t==null)
41         {
42             objr.setCode(1);
43             objr.setHint("对象为空!");
44             objr.setErrmsg("对象为空!");
45             return objr;
46         }
47         Validator v=ValidatorUtils.getValidator();
48         Set<ConstraintViolation<T>> cts=v.validate(t);
49         if(cts.size()>0)
50         {
51             objr.setCode(1);
52             Iterator<ConstraintViolation<T>> it=cts.iterator();
53             StringBuffer sb=new StringBuffer();
54             while(it.hasNext())
55             {
56                 ConstraintViolation<T> ct=it.next();
57                 sb.append(ct.getMessage()+";");
58             }
59             objr.setHint(sb.toString());
60             objr.setErrmsg(sb.toString());
61         }else{
62             objr.setCode(0);
63         }
64         return objr;
65     }
66     public <T> boolean check(T t)
67     {
68         
69         Set<ConstraintViolation<T>> cts=checkCTS(t);
70         if(cts.size()>0)
71         {
72             return false;
73         }else{
74             return true;
75         }
76     }
77     
78     public static <T> ObjectResult<T> checkPro(T t,String[] Pros)
79     {
80         ObjectResult<T> objr=new ObjectResult<T>();
81         if(t==null)
82         {
83             objr.setCode(1);
84             objr.setHint("对象为空!");
85             objr.setErrmsg("对象为空!");
86             return objr;
87         }
88         Validator v=ValidatorUtils.getValidator();
89         StringBuffer sb=new StringBuffer();
90         Set<ConstraintViolation<T>> cts=new HashSet<ConstraintViolation<T>>();
91         for (String pro:Pros) {
92             cts=v.validateProperty(t,pro);
93             if(cts.size()>0)
94             {
95                 Iterator<ConstraintViolation<T>> it=cts.iterator();
96                 while(it.hasNext())
97                 {
98                     ConstraintViolation<T> ct=it.next();
99                     sb.append(ct.getMessage()+";");
100                 }
101             }
102         }
103         if(sb.length()>0)
104         {
105             objr.setCode(1);
106             objr.setErrmsg(sb.toString());
107         }else
108             objr.setCode(0);
109         return objr;
110     }
111 }