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.jasperreports.domain.ReportUnit; 29 import com.jaspersoft.jasperserver.war.common.JasperServerConst; 30 import com.jaspersoft.jasperserver.war.common.JasperServerUtil; 31 import com.jaspersoft.jasperserver.war.dto.FileResourceWrapper; 32 import com.jaspersoft.jasperserver.war.dto.InputControlWrapper; 33 import com.jaspersoft.jasperserver.war.dto.ReportUnitWrapper; 34 35 39 public class ReportDetailsValidator implements Validator { 40 41 public boolean supports(Class clazz) { 42 return ReportUnitWrapper.class.isAssignableFrom(clazz); 43 } 44 45 public void validate(Object o, Errors errors) { 46 ReportUnitWrapper reportDetails = (ReportUnitWrapper) o; 47 validateNameLabelDesc(reportDetails, errors); validateJrxmlUpload(reportDetails, errors); validateReportAndControlForm(reportDetails, errors); } 51 52 public void validateNameLabelDesc(ReportUnitWrapper ruWrapper, Errors errors) { 53 ReportUnit ru = ruWrapper.getReportUnit(); 54 if (ru.getLabel() == null || ru.getLabel().trim().length() == 0) { 55 errors.rejectValue("reportUnit.label", null, 56 "Report label must not be empty"); 57 } else { 58 if (ruWrapper.getReportUnit().getLabel().length() > 100) { 59 errors 60 .rejectValue("reportUnit.label", null, 61 "Report label should not be longer than 100 characters"); 62 } else if (!JasperServerUtil.regExValidateLabel(ru.getLabel())) 63 errors.rejectValue("reportUnit.label", null, 64 "Report label contains invalid characters"); 65 } 66 67 if (ru.getName() == null || ru.getName().trim().length() == 0) { 68 errors.rejectValue("reportUnit.name", null, 69 "Report name must not be empty"); 70 } else { 71 if (ru.getName().length() > 100) { 72 errors.rejectValue("reportUnit.name", null, 73 "Report name should not be longer than 100 characters"); 74 } else if (!JasperServerUtil.regExValidateName(ru.getName())) 75 errors.rejectValue("reportUnit.name", null, 76 "Report name contains invalid characters"); 77 else { 78 if (ruWrapper.isNewMode() 79 && ruWrapper.getExistingResources() != null) { 80 List res = ruWrapper.getExistingResources(); 81 for (int i = 0; i < res.size(); i++) { 82 String preExtName = (String ) res.get(i); 83 if (preExtName.equalsIgnoreCase(ru.getName().trim())) { 84 errors 85 .rejectValue("reportUnit.name", null, 86 "A Resource with the chosen name already exists"); 87 break; 88 } 89 } 90 } 91 } 92 } 93 94 if (ru.getDescription() != null && ru.getDescription().length() > 250) 95 errors 96 .rejectValue("reportUnit.description", null, 97 "Report description should not be longer than 250 characters"); 98 } 99 100 public void validateURIString(ReportUnitWrapper reportUnit, Errors errors) { 101 if (reportUnit.getSource() == null) { 102 errors.rejectValue("source", null, "Select a valid JRXML source"); 103 } else { 104 if (reportUnit.getSource().equals( 105 JasperServerConst.FIELD_CHOICE_CONT_REPO)) { 106 if (reportUnit.getJrxmlUri() == null 107 || reportUnit.getJrxmlUri().length() == 0) { 108 errors.rejectValue("jrxmlUri", null, 109 "Select a resuable JRxml "); 110 } 111 } 112 } 113 114 } 115 116 public void validateJrxmlUpload(ReportUnitWrapper wrapper, Errors errors) { 117 if (wrapper.getSource() == null) { 118 errors.rejectValue("source", null, "Select a valid JRXML source"); 119 } else { 120 if (wrapper.getSource().equals( 121 JasperServerConst.FIELD_CHOICE_FILE_SYSTEM) 122 && !wrapper.isJrxmlLocated()) { 123 if (wrapper.getJrxmlData() == null 126 || wrapper.getJrxmlData().length == 0) { 127 errors.rejectValue("jrxmlData", null, 128 "Select a valid JRXML file"); 129 } 130 } 131 } 132 } 133 134 public void validateResources(ReportUnitWrapper wrapper, Errors errors) { 135 boolean allResLocated = true; 136 boolean allControlsLocated = true; 137 List sugRes = wrapper.getSuggestedResources(); 138 if (sugRes != null && !sugRes.isEmpty()) { 139 for (int i = 0; i < sugRes.size(); i++) { 140 FileResourceWrapper resWrap = (FileResourceWrapper) sugRes 141 .get(i); 142 if (!resWrap.isLocated()) { 143 allResLocated = false; 144 break; 145 } 146 } 147 } 148 List sugContr = wrapper.getSuggestedControls(); 149 if (sugContr != null && !sugContr.isEmpty()) { 150 for (int i = 0; i < sugContr.size(); i++) { 151 InputControlWrapper icWrap = (InputControlWrapper) sugContr 152 .get(i); 153 if (!icWrap.isLocated()) { 154 allControlsLocated = false; 155 break; 156 } 157 } 158 } 159 if (!allControlsLocated && !allResLocated) 160 errors.rejectValue("validationMessage", null, 161 "All suggested Resources and Controls must be located"); 162 else if (!allControlsLocated) 163 errors.rejectValue("validationMessage", null, 164 "All suggested Controls must be located"); 165 else if (!allResLocated) 166 errors.rejectValue("validationMessage", null, 167 "All suggested Resources must be located"); 168 169 } 170 171 public void validateReportAndControlForm(ReportUnitWrapper wrapper, Errors errors) { 172 if (wrapper.getReportUnit().getInputControlRenderingView() != null && 174 wrapper.getReportUnit().getInputControlRenderingView().length() > 100) { 175 errors 176 .rejectValue("reportUnit.inputControlRenderingView", null, 177 "JSP reference for input controls should not be longer than 100 characters"); 178 } 179 if (wrapper.getReportUnit().getReportRenderingView() != null && 180 wrapper.getReportUnit().getReportRenderingView().length() > 100) { 181 errors 182 .rejectValue("reportUnit.reportRenderingView", null, 183 "JSP reference for report display should not be longer than 100 characters"); 184 } 185 } 186 } | Popular Tags |