1 21 package com.jaspersoft.jasperserver.api.engine.scheduling; 22 23 import java.util.Set ; 24 import java.util.regex.Pattern ; 25 26 import com.jaspersoft.jasperserver.api.JSException; 27 import com.jaspersoft.jasperserver.api.common.domain.ExecutionContext; 28 import com.jaspersoft.jasperserver.api.common.domain.ValidationErrors; 29 import com.jaspersoft.jasperserver.api.common.domain.impl.ValidationErrorImpl; 30 import com.jaspersoft.jasperserver.api.common.domain.impl.ValidationErrorsImpl; 31 import com.jaspersoft.jasperserver.api.engine.scheduling.domain.ReportJob; 32 import com.jaspersoft.jasperserver.api.engine.scheduling.domain.ReportJobCalendarTrigger; 33 import com.jaspersoft.jasperserver.api.engine.scheduling.domain.ReportJobMailNotification; 34 import com.jaspersoft.jasperserver.api.engine.scheduling.domain.ReportJobRepositoryDestination; 35 import com.jaspersoft.jasperserver.api.engine.scheduling.domain.ReportJobSimpleTrigger; 36 import com.jaspersoft.jasperserver.api.engine.scheduling.domain.ReportJobTrigger; 37 38 42 public class DefaultReportJobValidator implements ReportJobValidator { 43 44 private static final Pattern PATTERN_CRON_MINUTES; 45 private static final Pattern PATTERN_CRON_HOURS; 46 private static final Pattern PATTERN_CRON_MONTH_DAYS; 47 48 static { 49 String allPattern = "(\\*)"; 50 51 String minPattern = "(\\d|[0-5]\\d)"; 52 String minRangePattern = "(" + minPattern + "(\\-" + minPattern + ")?)"; 53 String minuteIncrementPattern = "(" + minPattern + "\\/\\d+)"; 54 PATTERN_CRON_MINUTES = Pattern.compile("^(" + minRangePattern + "(," + minRangePattern + ")*)|" + minuteIncrementPattern + "|" + allPattern + "$"); 55 56 String hourPattern = "(\\d|[01]\\d|2[0-3])"; 57 String hourRangePattern = "(" + hourPattern + "(\\-" + hourPattern + ")?)"; 58 String hourIncrementPattern = "(" + hourPattern + "\\/\\d+)"; 59 PATTERN_CRON_HOURS = Pattern.compile("^(" + hourRangePattern + "(," + hourRangePattern + ")*)|" + hourIncrementPattern + "|" + allPattern + "$"); 60 61 String dayPattern = "([1-9]|[012]\\d|3[01])"; 62 String dayRangePattern = "(" + dayPattern + "(\\-" + dayPattern + ")?)"; 63 String dayIncrementPattern = "(" + dayPattern + "\\/\\d+)"; 64 PATTERN_CRON_MONTH_DAYS = Pattern.compile("^(" + dayRangePattern + "(," + dayRangePattern + ")*)|" + dayIncrementPattern + "|" + allPattern + "$"); 65 } 66 67 public ValidationErrors validateJob(ExecutionContext context, ReportJob job) { 68 ValidationErrorsImpl errors = new ValidationErrorsImpl(); 69 validateJobDetails(errors, job); 70 validateJobTrigger(errors, job.getTrigger()); 71 validateJobOutput(errors, job); 72 return errors; 73 } 74 75 protected void validateJobDetails(ValidationErrorsImpl errors, ReportJob job) { 76 checkString(errors, "label", job.getLabel(), true, 100); 77 checkString(errors, "description", job.getDescription(), false, 2000); 78 } 79 80 protected void validateJobTrigger(ValidationErrorsImpl errors, ReportJobTrigger trigger) { 81 if (trigger == null) { 82 errors.add(new ValidationErrorImpl("error.report.job.no.trigger", null, "No trigger is defined for the job.", "trigger")); 83 return; 84 } 85 86 if (trigger.getStartType() == ReportJobTrigger.START_TYPE_SCHEDULE && trigger.getStartDate() == null) { 87 addNotEmpty(errors, "trigger.startDate"); 88 } 89 90 if (trigger instanceof ReportJobSimpleTrigger) { 91 validateJobSimpleTrigger(errors, (ReportJobSimpleTrigger) trigger); 92 } else if (trigger instanceof ReportJobCalendarTrigger) { 93 validateJobCalendarTrigger(errors, (ReportJobCalendarTrigger) trigger); 94 } else { 95 throw new JSException("Unknow report job trigger type \"" + trigger.getClass().getName() + "\""); 96 } 97 } 98 99 protected void validateJobSimpleTrigger(ValidationErrorsImpl errors, ReportJobSimpleTrigger trigger) { 100 if (trigger.getOccurrenceCount() > 1 || trigger.getOccurrenceCount() == ReportJobSimpleTrigger.RECUR_INDEFINITELY) { 101 if (trigger.getRecurrenceInterval() == null) { 102 addNotEmpty(errors, "trigger.recurrenceInterval"); 103 } else if (trigger.getRecurrenceInterval().intValue() <= 0) { 104 errors.add(new ValidationErrorImpl("error.positive", null, null, "trigger.recurrenceInterval")); 105 } 106 if (trigger.getRecurrenceIntervalUnit() == null) { 107 addNotEmpty(errors, "trigger.recurrenceIntervalUnit"); 108 } 109 } 110 } 111 112 protected void validateJobCalendarTrigger(ValidationErrorsImpl errors, ReportJobCalendarTrigger trigger) { 113 if (checkString(errors, "trigger.minutes", trigger.getMinutes(), true, 200)) { 114 validateCronMinutes(errors, trigger.getMinutes()); 115 } 116 117 if (checkString(errors, "trigger.hours", trigger.getHours(), true, 80)) { 118 validateCronHours(errors, trigger.getHours()); 119 } 120 121 switch (trigger.getDaysType()) { 122 case ReportJobCalendarTrigger.DAYS_TYPE_ALL: 123 break; 124 case ReportJobCalendarTrigger.DAYS_TYPE_WEEK: 125 if (trigger.getWeekDays() == null || trigger.getWeekDays().isEmpty()) { 126 errors.add(new ValidationErrorImpl("error.not.empty", null, "The value cannot be empty", "trigger.weekDays")); 127 } 128 break; 129 case ReportJobCalendarTrigger.DAYS_TYPE_MONTH: 130 if (checkString(errors, "trigger.monthDays", trigger.getMonthDays(), true, 100)) { 131 validateCronMonthDays(errors, trigger.getMonthDays()); 132 } 133 break; 134 default: 135 throw new JSException("Unknown calendar trigger days type " + trigger.getDaysType()); 136 } 137 138 if (trigger.getMonths() == null || trigger.getMonths().isEmpty()) { 139 errors.add(new ValidationErrorImpl("error.not.empty", null, "The value cannot be empty", "trigger.months")); 140 } 141 } 142 143 protected void validateCronMinutes(ValidationErrorsImpl errors, String minutes) { 144 if (!PATTERN_CRON_MINUTES.matcher(minutes).matches()) { 145 errors.add(new ValidationErrorImpl("error.pattern", null, "The minutes are not valid", "trigger.minutes")); 146 } 147 } 148 149 protected void validateCronHours(ValidationErrorsImpl errors, String hours) { 150 if (!PATTERN_CRON_HOURS.matcher(hours).matches()) { 151 errors.add(new ValidationErrorImpl("error.pattern", null, "The hours are not valid", "trigger.hours")); 152 } 153 } 154 155 protected void validateCronMonthDays(ValidationErrorsImpl errors, String days) { 156 if (!PATTERN_CRON_MONTH_DAYS.matcher(days).matches()) { 157 errors.add(new ValidationErrorImpl("error.pattern", null, "The hours are not valid", "trigger.monthDays")); 158 } 159 } 160 161 protected void validateJobOutput(ValidationErrorsImpl errors, ReportJob job) { 162 checkString(errors, "baseOutputFilename", job.getBaseOutputFilename(), true, 100); 163 164 Set outputFormats = job.getOutputFormats(); 165 if (outputFormats == null || outputFormats.isEmpty()) { 166 errors.add(new ValidationErrorImpl("error.report.job.no.output.formats", null, "No output formats are selected for the job.", "outputFormats")); 167 } 168 169 if (job.getContentRepositoryDestination() == null) { 170 errors.add(new ValidationErrorImpl("error.report.job.no.repository.output", null, "Repository output is not defined for the job.", "contentRepositoryDestination")); 171 } else { 172 validateRepositoryDestination(errors, job.getContentRepositoryDestination()); 173 } 174 175 ReportJobMailNotification mailNotification = job.getMailNotification(); 176 if (mailNotification != null && !mailNotification.isEmpty()) { 177 validateMailNotification(errors, mailNotification); 178 } 179 } 180 181 protected void validateRepositoryDestination(ValidationErrorsImpl errors, ReportJobRepositoryDestination repositoryDestination) { 182 checkString(errors, "contentRepositoryDestination.folderURI", repositoryDestination.getFolderURI(), true, 200); 183 } 184 185 protected void validateMailNotification(ValidationErrorsImpl errors, ReportJobMailNotification mailNotification) { 186 } 187 188 protected boolean checkString(ValidationErrorsImpl errors, String field, String value, boolean mandatory, int maxLength) { 189 boolean valid = true; 190 boolean empty = value == null || value.length() == 0; 191 if (empty) { 192 if (mandatory) { 193 errors.add(new ValidationErrorImpl("error.not.empty", null, "The value cannot be empty", field)); 194 valid = false; 195 } 196 } else { 197 if (value.length() > maxLength) { 198 errors.add(new ValidationErrorImpl("error.length", new Object []{new Integer (maxLength)}, "Maximum length is {0,number,integer}.", field)); 199 valid = false; 200 } 201 } 202 return valid; 203 } 204 205 protected void addNotEmpty(ValidationErrorsImpl errors, String field) { 206 errors.add(new ValidationErrorImpl("error.not.empty", null, "The value cannot be empty", field)); 207 } 208 209 } 210 | Popular Tags |