01: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
02: *
03: * Licensed under the Apache License, Version 2.0 (the "License");
04: * you may not use this file except in compliance with the License.
05: * You may obtain a copy of the License at
06: *
07: * http://www.apache.org/licenses/LICENSE-2.0
08: *
09: * Unless required by applicable law or agreed to in writing, software
10: * distributed under the License is distributed on an "AS IS" BASIS,
11: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: * See the License for the specific language governing permissions and
13: * limitations under the License.
14: */
15: package sample.contact;
16:
17: import org.acegisecurity.acls.domain.BasePermission;
18:
19: import org.springframework.validation.Errors;
20: import org.springframework.validation.ValidationUtils;
21: import org.springframework.validation.Validator;
22:
23: /**
24: * Validates {@link AddPermission}.
25: *
26: * @author Ben Alex
27: * @version $Id: AddPermissionValidator.java 1754 2006-11-17 02:01:21Z benalex $
28: */
29: public class AddPermissionValidator implements Validator {
30: //~ Methods ========================================================================================================
31:
32: public boolean supports(Class clazz) {
33: return clazz.equals(AddPermission.class);
34: }
35:
36: public void validate(Object obj, Errors errors) {
37: AddPermission addPermission = (AddPermission) obj;
38:
39: ValidationUtils.rejectIfEmptyOrWhitespace(errors, "permission",
40: "err.permission", "Permission is required. *");
41: ValidationUtils.rejectIfEmptyOrWhitespace(errors, "recipient",
42: "err.recipient", "Recipient is required. *");
43:
44: if (addPermission.getPermission() != null) {
45: int permission = addPermission.getPermission().intValue();
46:
47: if ((permission != BasePermission.ADMINISTRATION.getMask())
48: && (permission != BasePermission.READ.getMask())
49: && (permission != BasePermission.DELETE.getMask())) {
50: errors.rejectValue("permission",
51: "err.permission.invalid",
52: "The indicated permission is invalid. *");
53: }
54: }
55:
56: if (addPermission.getRecipient() != null) {
57: if (addPermission.getRecipient().length() > 100) {
58: errors
59: .rejectValue("recipient",
60: "err.recipient.length",
61: "The recipient is too long (maximum 100 characters). *");
62: }
63: }
64: }
65: }
|