1 21 package com.jaspersoft.jasperserver.war.validation; 22 23 import java.util.List ; 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 37 public class FolderValidator implements Validator 38 { 39 public boolean supports(Class clazz) { 40 return FolderWrapper.class.isAssignableFrom(clazz); 41 } 42 43 public void validate(Object 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 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 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 text){ 96 return text.trim().length(); 97 } 98 99 } | Popular Tags |