KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jaspersoft > jasperserver > war > validation > FolderValidator


1 /*
2  * Copyright (C) 2006 JasperSoft http://www.jaspersoft.com
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed WITHOUT ANY WARRANTY; and without the
10  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
15  * or write to:
16  *
17  * Free Software Foundation, Inc.,
18  * 59 Temple Place - Suite 330,
19  * Boston, MA USA 02111-1307
20  */

21 package com.jaspersoft.jasperserver.war.validation;
22
23 import java.util.List JavaDoc;
24
25 import org.springframework.validation.Errors;
26 import org.springframework.validation.Validator;
27
28 import com.jaspersoft.jasperserver.api.metadata.common.domain.Folder;
29 import com.jaspersoft.jasperserver.war.common.JasperServerConst;
30 import com.jaspersoft.jasperserver.war.common.JasperServerUtil;
31 import com.jaspersoft.jasperserver.war.dto.FolderWrapper;
32
33 /**
34  * @author Ionut Nedelcu (ionutned@users.sourceforge.net)
35  * @version $Id: FolderValidator.java 4144 2006-08-08 08:39:54Z saiyedm $
36  */

37 public class FolderValidator implements Validator
38 {
39     public boolean supports(Class JavaDoc clazz) {
40         return FolderWrapper.class.isAssignableFrom(clazz);
41     }
42
43     public void validate(Object JavaDoc object, Errors errors) {
44         Folder folder = ((FolderWrapper)object).getActualFolder();
45
46         if (folder.getName() == null || size(folder.getName()) == 0) {
47             errors.rejectValue("actualFolder.name", "error.not.empty");
48         } else {
49             if(!JasperServerUtil.regExValidateName(folder.getName())) {
50                 errors.rejectValue("actualFolder.name", null, "Name contains invalid characters");
51             }else {
52                 if (folder.getName().trim().length() > JasperServerConst.MAX_LENGTH_NAME) {
53                     errors.rejectValue("actualFolder.name", null,
54                             "Name should not be longer than "+
55                             JasperServerConst.MAX_LENGTH_NAME+" characters");
56                 }else{
57                     List JavaDoc allFolders=((FolderWrapper)object).getAllFolders();
58                     if(allFolders!=null && !allFolders.isEmpty()){
59                         for(int i=0;i<allFolders.size();i++){
60                             Folder preExistFolder=(Folder)allFolders.get(i);
61                             if(preExistFolder.getName().trim().equalsIgnoreCase(folder.getName().trim())){
62                                 //Folder name choosen now alredy exists
63
errors.rejectValue("actualFolder.name",
64                                         null, "A Folder with choosen name already exists");
65                                 break;
66                             }
67                         }
68                     }
69                 }
70             }
71         }
72
73         if (folder.getLabel() == null || size(folder.getLabel()) == 0) {
74             errors.rejectValue("actualFolder.label", "error.not.empty");
75         } else {
76             if(!JasperServerUtil.regExValidateLabel(folder.getLabel())) {
77                 errors.rejectValue("actualFolder.label", null, "Label contains invalid characters");
78             }else {
79                 if (folder.getLabel().trim().length() > JasperServerConst.MAX_LENGTH_LABEL) {
80                     errors
81                             .rejectValue("actualFolder.label", null,
82                                     "Label should not be longer than "+
83                                     JasperServerConst.MAX_LENGTH_LABEL+" characters");
84                 }
85             }
86         }
87         
88         if (folder.getDescription() != null && size(folder.getDescription()) > JasperServerConst.MAX_LENGTH_DESC) {
89             errors.rejectValue("actualFolder.description",
90                     null, "Description can not be longer than "+
91                     JasperServerConst.MAX_LENGTH_DESC+" characaters");
92         }
93     }
94
95     private int size(String JavaDoc text){
96         return text.trim().length();
97     }
98
99 }
Popular Tags