|
@@ -7,10 +7,14 @@ import org.hsweb.web.dao.GenericMapper;
|
|
import org.hsweb.web.service.GenericService;
|
|
import org.hsweb.web.service.GenericService;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
|
|
+import javax.validation.ConstraintViolation;
|
|
import javax.validation.ValidationException;
|
|
import javax.validation.ValidationException;
|
|
|
|
+import javax.validation.Validator;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
+import java.util.Set;
|
|
|
|
|
|
/**
|
|
/**
|
|
* Created by 浩 on 2016-01-22 0022.
|
|
* Created by 浩 on 2016-01-22 0022.
|
|
@@ -19,6 +23,9 @@ import java.util.List;
|
|
public abstract class AbstractServiceImpl<Po, PK> implements GenericService<Po, PK> {
|
|
public abstract class AbstractServiceImpl<Po, PK> implements GenericService<Po, PK> {
|
|
protected Logger logger = LoggerFactory.getLogger(this.getClass());
|
|
protected Logger logger = LoggerFactory.getLogger(this.getClass());
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
+ protected Validator validator;
|
|
|
|
+
|
|
protected abstract GenericMapper<Po, PK> getMapper();
|
|
protected abstract GenericMapper<Po, PK> getMapper();
|
|
|
|
|
|
@Override
|
|
@Override
|
|
@@ -87,11 +94,12 @@ public abstract class AbstractServiceImpl<Po, PK> implements GenericService<Po,
|
|
}
|
|
}
|
|
|
|
|
|
protected void tryValidPo(Po data) {
|
|
protected void tryValidPo(Po data) {
|
|
- ValidResults results;
|
|
|
|
- if (data instanceof GenericPo) {
|
|
|
|
- results = ((GenericPo) data).valid();
|
|
|
|
- } else {
|
|
|
|
- results = GenericPo.valid(data);
|
|
|
|
|
|
+ Set<ConstraintViolation<Object>> set = validator.validate(data);
|
|
|
|
+ ValidResults results = new ValidResults();
|
|
|
|
+ if (set.size() != 0) {
|
|
|
|
+ for (ConstraintViolation<Object> violation : set) {
|
|
|
|
+ results.addResult(violation.getPropertyPath().toString(), violation.getMessage());
|
|
|
|
+ }
|
|
}
|
|
}
|
|
if (!results.isSuccess())
|
|
if (!results.isSuccess())
|
|
throw new ValidationException(results.toString());
|
|
throw new ValidationException(results.toString());
|