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.common.domain.ValidationDetail; 29 import com.jaspersoft.jasperserver.api.common.domain.ValidationResult; 30 31 import com.jaspersoft.jasperserver.war.common.JasperServerConst; 32 import com.jaspersoft.jasperserver.war.common.JasperServerUtil; 33 import com.jaspersoft.jasperserver.war.dto.OlapUnitWrapper; 34 35 import com.jaspersoft.jasperserver.api.metadata.olap.service.OlapConnectionService; 36 37 45 public class OlapUnitValidator implements Validator { 46 47 public boolean supports(Class clazz) { 48 return OlapUnitWrapper.class.isAssignableFrom(clazz); 49 } 50 51 public void validate(Object o, Errors errors) { 52 OlapUnitWrapper olapDetails = (OlapUnitWrapper) o; 53 validateNameLabelDesc(olapDetails, errors); 54 } 55 56 public void validateNameLabelDesc(OlapUnitWrapper ouWrapper, Errors errors) { 57 if (ouWrapper.getOlapUnitLabel() == null || 58 ouWrapper.getOlapUnitLabel().trim().length() == 0) { 59 errors.rejectValue("olapUnitLabel", null, 60 "OLAP label must not be empty"); 61 } else { 62 if (ouWrapper.getOlapUnitLabel().length() > 100) { 63 errors 64 .rejectValue("olapUnitLabel", null, 65 "OLAP label should not be longer than 100 characters"); 66 } else if (!JasperServerUtil.regExValidateLabel(ouWrapper.getOlapUnitLabel())) 67 errors.rejectValue("olapUnitLabel", null, 68 "OLAP label contains invalid characters"); 69 } 70 71 if (ouWrapper.getOlapUnitName() == null || 72 ouWrapper.getOlapUnitName().trim().length() == 0) { 73 errors.rejectValue("olapUnitName", null, 74 "OLAP name must not be empty"); 75 } else { 76 if (ouWrapper.getOlapUnitName().length() > 100) { 77 errors.rejectValue("olapUnitName", null, 78 "OLAP name should not be longer than 100 characters"); 79 } else if (!JasperServerUtil.regExValidateName(ouWrapper.getOlapUnitName())) 80 errors.rejectValue("olapUnitName", null, 81 "OLAP name contains invalid characters"); 82 else { 83 if (ouWrapper.isNewMode() 84 && ouWrapper.getExistingResources() != null) { 85 List res = ouWrapper.getExistingResources(); 86 for (int i = 0; i < res.size(); i++) { 87 String preExtName = (String ) res.get(i); 88 if (preExtName.equalsIgnoreCase(ouWrapper.getOlapUnitName().trim())) { 89 errors.rejectValue("olapUnitName", null, 90 "A Resource with the chosen name already exists"); 91 break; 92 } 93 } 94 } 95 } 96 } 97 98 if (ouWrapper.getOlapUnitDescription() != 99 null && ouWrapper.getOlapUnitDescription().length() > 300) 100 errors.rejectValue("olapUnitDescription", null, 101 "OLAP description should not be longer than 300 characters"); 102 } 103 104 public void validateMdxQuery(OlapUnitWrapper ouWrapper, Errors errors) { 105 ouWrapper.getOlapUnit(). 112 setMdxQuery(ouWrapper.getOlapUnitMdxQuery()); 113 if (ouWrapper.isNewMode()) { 114 ouWrapper.getOlapUnit(). 115 setOlapClientConnection(ouWrapper.getOlapClientConnection()); 116 } 117 118 if (ouWrapper.getOlapUnitMdxQuery() == null || 119 ouWrapper.getOlapUnitMdxQuery().trim().length() == 0) { 120 errors.rejectValue("olapUnitMdxQuery", null, 121 "OLAP MDX Query must not be empty"); 122 } 123 else if (ouWrapper.getOlapUnitMdxQuery() != 124 null && ouWrapper.getOlapUnitMdxQuery().length() > 4096) { 125 errors.rejectValue("olapUnitMdxQuery", null, 126 "OLAP MDX Query should not be longer than 4096 characters"); 127 } 128 else { 129 ValidationResult result = 130 getConnectionService().validate(null, ouWrapper.getOlapUnit()); 131 if (result.getValidationState().equals(ValidationResult.STATE_ERROR)) { 132 List details = result.getResults(); 133 String msg = "That is not a valid MDX Query: "; 134 String causeMsg = null; 135 for( int i = 0; i < details.size(); i++ ) { 136 ValidationDetail detail = (ValidationDetail)details.get(i); 137 Throwable e = detail.getException(); 138 while (e != null) { 139 causeMsg = e.getMessage(); 140 e = e.getCause(); 141 } 142 } 143 errors.rejectValue("olapUnitMdxQuery", null, msg + causeMsg); 144 } 145 } 146 } 147 148 public void validateURIString(OlapUnitWrapper ouWrapper, Errors errors) { 149 if (ouWrapper.getSource() == null) { 150 errors.rejectValue("source", null, "Select a valid schema source"); 151 } else { 152 if (ouWrapper.getSource().equals( 153 JasperServerConst.FIELD_CHOICE_CONT_REPO)) { 154 if (ouWrapper.getSchemaUri() == null 155 || ouWrapper.getSchemaUri().length() == 0) { 156 errors.rejectValue("schemaUri", null, 157 "Select a resuable schema "); 158 } 159 } 160 } 161 162 } 163 164 public void validateConnectionType(OlapUnitWrapper ouWrapper, Errors errors){ 165 if(ouWrapper.getSource() == null) 166 errors.rejectValue("source",null,"Please select a Connection type"); 167 } 168 169 public void validateConnectionSource(OlapUnitWrapper wrapper, Errors errors){ 170 if(wrapper.getType() == null) 171 errors.rejectValue("source",null,"Please select a Connection source"); 172 } 173 174 177 private OlapConnectionService mConnectionService; 178 public OlapConnectionService getConnectionService() { 179 return mConnectionService; 180 } 181 public void setConnectionService( OlapConnectionService cs ) { 182 mConnectionService = cs; 183 } 184 185 } | Popular Tags |