001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.datadictionary.validation.constraint;
017
018import org.kuali.rice.core.api.config.property.ConfigurationService;
019import org.kuali.rice.krad.service.KRADServiceLocator;
020import org.kuali.rice.krad.uif.UifConstants;
021
022import java.util.ArrayList;
023import java.util.List;
024
025/**
026 * Parent abstract class that allows additional characters to be allowed in child constraint
027 * character sets, see AlphaNumericPatternConstraint, among others for example.
028 *
029 * By setting an allow flag to true you are allowing that character as a valid character in the set.
030 * AllowAll set to true will allow all characters which have a flag to be allowed in the set.
031 *
032 * @author Kuali Rice Team (rice.collab@kuali.org)
033 */
034public abstract class AllowCharacterConstraint extends ValidCharactersPatternConstraint {
035    protected boolean allowWhitespace = false;
036    protected boolean omitNewline = false;
037    protected boolean allowUnderscore = false;
038    protected boolean allowPeriod = false;
039    protected boolean allowParenthesis = false;
040    protected boolean allowDollar = false;
041    protected boolean allowForwardSlash = false;
042    protected boolean allowDoubleQuote = false;
043    protected boolean allowApostrophe = false;
044    protected boolean allowComma = false;
045    protected boolean allowColon = false;
046    protected boolean allowSemiColon = false;
047    protected boolean allowQuestionMark = false;
048    protected boolean allowExclaimation = false;
049    protected boolean allowDash = false;
050    protected boolean allowPlus = false;
051    protected boolean allowEquals = false;
052    protected boolean allowAsterisk = false;
053    protected boolean allowAtSign = false;
054    protected boolean allowPercent = false;
055    protected boolean allowPound = false;
056    protected boolean allowGreaterThan = false;
057    protected boolean allowLessThan = false;
058    protected boolean allowBrackets = false;
059    protected boolean allowAmpersand = false;
060    protected boolean allowCurlyBraces = false;
061    protected boolean allowBackslash = false;
062    protected boolean allowAll = false;
063
064    /**
065     * @return the allowWhitespace
066     */
067    public boolean isAllowWhitespace() {
068        return this.allowWhitespace;
069    }
070
071    /**
072     * @param allowWhitespace the allowWhitespace to set
073     */
074    public void setAllowWhitespace(boolean allowWhitespace) {
075        this.allowWhitespace = allowWhitespace;
076    }
077
078    /**
079     * @return the allowUnderscore
080     */
081    public boolean isAllowUnderscore() {
082        return this.allowUnderscore;
083    }
084
085    /**
086     * @param allowUnderscore the allowUnderscore to set
087     */
088    public void setAllowUnderscore(boolean allowUnderscore) {
089        this.allowUnderscore = allowUnderscore;
090    }
091
092    /**
093     * @return the allowPeriod
094     */
095    public boolean isAllowPeriod() {
096        return this.allowPeriod;
097    }
098
099    /**
100     * @param allowPeriod the allowPeriod to set
101     */
102    public void setAllowPeriod(boolean allowPeriod) {
103        this.allowPeriod = allowPeriod;
104    }
105
106    /**
107     * @return the allowParenthesis
108     */
109    public boolean isAllowParenthesis() {
110        return this.allowParenthesis;
111    }
112
113    /**
114     * @param allowParenthesis the allowParenthesis to set
115     */
116    public void setAllowParenthesis(boolean allowParenthesis) {
117        this.allowParenthesis = allowParenthesis;
118    }
119
120    /**
121     * @return the allowDollar
122     */
123    public boolean isAllowDollar() {
124        return this.allowDollar;
125    }
126
127    /**
128     * @param allowDollar the allowDollar to set
129     */
130    public void setAllowDollar(boolean allowDollar) {
131        this.allowDollar = allowDollar;
132    }
133
134    /**
135     * @return the allowForwardSlash
136     */
137    public boolean isAllowForwardSlash() {
138        return this.allowForwardSlash;
139    }
140
141    /**
142     * @param allowForwardSlash the allowForwardSlash to set
143     */
144    public void setAllowForwardSlash(boolean allowForwardSlash) {
145        this.allowForwardSlash = allowForwardSlash;
146    }
147
148    /**
149     * @return the allowDoubleQuote
150     */
151    public boolean isAllowDoubleQuote() {
152        return this.allowDoubleQuote;
153    }
154
155    /**
156     * @param allowDoubleQuote the allowDoubleQuote to set
157     */
158    public void setAllowDoubleQuote(boolean allowDoubleQuote) {
159        this.allowDoubleQuote = allowDoubleQuote;
160    }
161
162    /**
163     * @return the allowApostrophe
164     */
165    public boolean isAllowApostrophe() {
166        return this.allowApostrophe;
167    }
168
169    /**
170     * @param allowApostrophe the allowApostrophe to set
171     */
172    public void setAllowApostrophe(boolean allowApostrophe) {
173        this.allowApostrophe = allowApostrophe;
174    }
175
176    /**
177     * @return the allowComma
178     */
179    public boolean isAllowComma() {
180        return this.allowComma;
181    }
182
183    /**
184     * @param allowComma the allowComma to set
185     */
186    public void setAllowComma(boolean allowComma) {
187        this.allowComma = allowComma;
188    }
189
190    /**
191     * @return the allowColon
192     */
193    public boolean isAllowColon() {
194        return this.allowColon;
195    }
196
197    /**
198     * @param allowColon the allowColon to set
199     */
200    public void setAllowColon(boolean allowColon) {
201        this.allowColon = allowColon;
202    }
203
204    /**
205     * @return the allowSemiColon
206     */
207    public boolean isAllowSemiColon() {
208        return this.allowSemiColon;
209    }
210
211    /**
212     * @param allowSemiColon the allowSemiColon to set
213     */
214    public void setAllowSemiColon(boolean allowSemiColon) {
215        this.allowSemiColon = allowSemiColon;
216    }
217
218    /**
219     * @return the allowQuestionMark
220     */
221    public boolean isAllowQuestionMark() {
222        return this.allowQuestionMark;
223    }
224
225    /**
226     * @param allowQuestionMark the allowQuestionMark to set
227     */
228    public void setAllowQuestionMark(boolean allowQuestionMark) {
229        this.allowQuestionMark = allowQuestionMark;
230    }
231
232    /**
233     * @return the allowExclaimation
234     */
235    public boolean isAllowExclaimation() {
236        return this.allowExclaimation;
237    }
238
239    /**
240     * @param allowExclaimation the allowExclaimation to set
241     */
242    public void setAllowExclaimation(boolean allowExclaimation) {
243        this.allowExclaimation = allowExclaimation;
244    }
245
246    /**
247     * @return the allowDash
248     */
249    public boolean isAllowDash() {
250        return this.allowDash;
251    }
252
253    /**
254     * @param allowDash the allowDash to set
255     */
256    public void setAllowDash(boolean allowDash) {
257        this.allowDash = allowDash;
258    }
259
260    /**
261     * @return the allowPlus
262     */
263    public boolean isAllowPlus() {
264        return this.allowPlus;
265    }
266
267    /**
268     * @param allowPlus the allowPlus to set
269     */
270    public void setAllowPlus(boolean allowPlus) {
271        this.allowPlus = allowPlus;
272    }
273
274    /**
275     * @return the allowEquals
276     */
277    public boolean isAllowEquals() {
278        return this.allowEquals;
279    }
280
281    /**
282     * @param allowEquals the allowEquals to set
283     */
284    public void setAllowEquals(boolean allowEquals) {
285        this.allowEquals = allowEquals;
286    }
287
288    /**
289     * @return the allowAsterisk
290     */
291    public boolean isAllowAsterisk() {
292        return this.allowAsterisk;
293    }
294
295    /**
296     * @param allowAsterisk the allowAsterisk to set
297     */
298    public void setAllowAsterisk(boolean allowAsterisk) {
299        this.allowAsterisk = allowAsterisk;
300    }
301
302    /**
303     * @return the allowAtSign
304     */
305    public boolean isAllowAtSign() {
306        return this.allowAtSign;
307    }
308
309    /**
310     * @param allowAtSign the allowAtSign to set
311     */
312    public void setAllowAtSign(boolean allowAtSign) {
313        this.allowAtSign = allowAtSign;
314    }
315
316    /**
317     * @return the allowPercent
318     */
319    public boolean isAllowPercent() {
320        return this.allowPercent;
321    }
322
323    /**
324     * @param allowPercent the allowPercent to set
325     */
326    public void setAllowPercent(boolean allowPercent) {
327        this.allowPercent = allowPercent;
328    }
329
330    /**
331     * @return the allowPound
332     */
333    public boolean isAllowPound() {
334        return this.allowPound;
335    }
336
337    /**
338     * @param allowPound the allowPound to set
339     */
340    public void setAllowPound(boolean allowPound) {
341        this.allowPound = allowPound;
342    }
343
344    public boolean isAllowGreaterThan() {
345        return allowGreaterThan;
346    }
347
348    public void setAllowGreaterThan(boolean allowGreaterThan) {
349        this.allowGreaterThan = allowGreaterThan;
350    }
351
352    public boolean isAllowLessThan() {
353        return allowLessThan;
354    }
355
356    public void setAllowLessThan(boolean allowLessThan) {
357        this.allowLessThan = allowLessThan;
358    }
359
360    public boolean isAllowBrackets() {
361        return allowBrackets;
362    }
363
364    public void setAllowBrackets(boolean allowBrackets) {
365        this.allowBrackets = allowBrackets;
366    }
367
368    public boolean isAllowAmpersand() {
369        return allowAmpersand;
370    }
371
372    public void setAllowAmpersand(boolean allowAmpersand) {
373        this.allowAmpersand = allowAmpersand;
374    }
375
376    public boolean isAllowCurlyBraces() {
377        return allowCurlyBraces;
378    }
379
380    public void setAllowCurlyBraces(boolean allowCurlyBraces) {
381        this.allowCurlyBraces = allowCurlyBraces;
382    }
383
384    public boolean isAllowBackslash() {
385        return allowBackslash;
386    }
387
388    public void setAllowBackslash(boolean allowBackslash) {
389        this.allowBackslash = allowBackslash;
390    }
391
392    /**
393     * If true, this constraint will allow all symbols that have flags for them. Equivalent to
394     * setting each flag to true separately.
395     *
396     * @return the allowAll
397     */
398    public boolean isAllowAll() {
399        return this.allowAll;
400    }
401
402    /**
403     * @param allowAll the allowAll to set
404     */
405    public void setAllowAll(boolean allowAll) {
406        this.allowAll = allowAll;
407    }
408
409    public boolean isOmitNewline() {
410        return omitNewline;
411    }
412
413    /**
414     * When set to true, omit new line characters from the set of valid characters.  This flag
415     * will only have an effect if the allowWhitespace flag is true, otherwise all whitespace
416     * including new lines characters are omitted.
417     *
418     * @param omitNewline
419     */
420    public void setOmitNewline(boolean omitNewline) {
421        this.omitNewline = omitNewline;
422    }
423
424    /**
425     * This method returns the allowed set of characters allowed by this constraint, based on the
426     * flags set. This string is intended to be placed within the or set of a regex, ie between the
427     * [ ] symbols
428     *
429     * @return
430     */
431    protected String getAllowedCharacterRegex() {
432        StringBuilder regexString = new StringBuilder("");
433        if (allowWhitespace || allowAll) {
434            regexString.append("\\t\\v\\040");
435            if (!omitNewline) {
436                regexString.append("\\f\\r\\n");
437            }
438        }
439        if (allowUnderscore || allowAll) {
440            regexString.append("_");
441        }
442        if (allowPeriod || allowAll) {
443            regexString.append(".");
444        }
445        if (allowParenthesis || allowAll) {
446            regexString.append("(");
447            regexString.append(")");
448        }
449        if (allowDollar || allowAll) {
450            regexString.append("$");
451        }
452        if (allowForwardSlash || allowAll) {
453            regexString.append("/");
454        }
455        if (allowDoubleQuote || allowAll) {
456            regexString.append("\\\"");
457        }
458        if (allowApostrophe || allowAll) {
459            regexString.append("'");
460        }
461        if (allowComma || allowAll) {
462            regexString.append(",");
463        }
464        if (allowColon || allowAll) {
465            regexString.append(":");
466        }
467        if (allowSemiColon || allowAll) {
468            regexString.append(";");
469        }
470        if (allowQuestionMark || allowAll) {
471            regexString.append("?");
472        }
473        if (allowExclaimation || allowAll) {
474            regexString.append("!");
475        }
476        if (allowDash || allowAll) {
477            regexString.append("\\-");
478        }
479        if (allowPlus || allowAll) {
480            regexString.append("+");
481        }
482        if (allowEquals || allowAll) {
483            regexString.append("=");
484        }
485        if (allowAsterisk || allowAll) {
486            regexString.append("*");
487        }
488        if (allowAtSign || allowAll) {
489            regexString.append("@");
490        }
491        if (allowPercent || allowAll) {
492            regexString.append("%");
493        }
494        if (allowPound || allowAll) {
495            regexString.append("#");
496        }
497        if (allowLessThan || allowAll) {
498            regexString.append("\\0074");
499        }
500        if (allowGreaterThan || allowAll) {
501            regexString.append("\\0076");
502        }
503        if (allowAmpersand || allowAll) {
504            regexString.append("\\0046");
505        }
506        if (allowBackslash || allowAll) {
507            regexString.append("\\0134");
508        }
509        if (allowCurlyBraces || allowAll) {
510            regexString.append("\\0173\\0175");
511        }
512        if (allowBrackets || allowAll) {
513            regexString.append("\\0133\\0135");
514        }
515        return regexString.toString();
516    }
517
518    /**
519     * Generates a comma separated string of the allowed set of characters, for the {0} parameter to
520     * be used within its validation message
521     *
522     * @return the validationMessageParams
523     */
524    public List<String> getValidationMessageParams() {
525        if (validationMessageParams == null) {
526            validationMessageParams = new ArrayList<String>();
527            ConfigurationService configService = KRADServiceLocator.getKualiConfigurationService();
528            StringBuilder paramString = new StringBuilder("");
529
530            if (allowWhitespace || allowAll) {
531                paramString.append(", " + configService.getPropertyValueAsString(
532                        UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "whitespace"));
533                if (!omitNewline) {
534                    paramString.append(", " + configService.getPropertyValueAsString(
535                            UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "newline"));
536                }
537            }
538            if (allowUnderscore || allowAll) {
539                paramString.append(", " + configService.getPropertyValueAsString(
540                        UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "underscore"));
541            }
542            if (allowPeriod || allowAll) {
543                paramString.append(", " + configService.getPropertyValueAsString(
544                        UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "period"));
545            }
546            if (allowParenthesis || allowAll) {
547                paramString.append(", " + configService.getPropertyValueAsString(
548                        UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "parenthesis"));
549            }
550            if (allowDollar || allowAll) {
551                paramString.append(", " + configService.getPropertyValueAsString(
552                        UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "dollar"));
553            }
554            if (allowForwardSlash || allowAll) {
555                paramString.append(", " + configService.getPropertyValueAsString(
556                        UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "forwardSlash"));
557            }
558            if (allowDoubleQuote || allowAll) {
559                paramString.append(", " + configService.getPropertyValueAsString(
560                        UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "doubleQuote"));
561            }
562            if (allowApostrophe || allowAll) {
563                paramString.append(", " + configService.getPropertyValueAsString(
564                        UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "apostrophe"));
565            }
566            if (allowComma || allowAll) {
567                paramString.append(", " + configService.getPropertyValueAsString(
568                        UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "comma"));
569            }
570            if (allowColon || allowAll) {
571                paramString.append(", " + configService.getPropertyValueAsString(
572                        UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "colon"));
573            }
574            if (allowSemiColon || allowAll) {
575                paramString.append(", " + configService.getPropertyValueAsString(
576                        UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "semiColon"));
577            }
578            if (allowQuestionMark || allowAll) {
579                paramString.append(", " + configService.getPropertyValueAsString(
580                        UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "questionMark"));
581            }
582            if (allowExclaimation || allowAll) {
583                paramString.append(", " + configService.getPropertyValueAsString(
584                        UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "exclaimation"));
585            }
586            if (allowDash || allowAll) {
587                paramString.append(", " + configService.getPropertyValueAsString(
588                        UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "dash"));
589            }
590            if (allowPlus || allowAll) {
591                paramString.append(", " + configService.getPropertyValueAsString(
592                        UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "plus"));
593            }
594            if (allowEquals || allowAll) {
595                paramString.append(", " + configService.getPropertyValueAsString(
596                        UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "equals"));
597            }
598            if (allowAsterisk || allowAll) {
599                paramString.append(", " + configService.getPropertyValueAsString(
600                        UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "asterisk"));
601            }
602            if (allowAtSign || allowAll) {
603                paramString.append(", " + configService.getPropertyValueAsString(
604                        UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "atSign"));
605            }
606            if (allowPercent || allowAll) {
607                paramString.append(", " + configService.getPropertyValueAsString(
608                        UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "percent"));
609            }
610            if (allowPound || allowAll) {
611                paramString.append(", " + configService.getPropertyValueAsString(
612                        UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "pound"));
613            }
614            if (allowLessThan || allowAll) {
615                paramString.append(", " + configService.getPropertyValueAsString(
616                        UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "lessThan"));
617            }
618            if (allowGreaterThan || allowAll) {
619                paramString.append(", " + configService.getPropertyValueAsString(
620                        UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "greaterThan"));
621            }
622            if (allowAmpersand || allowAll) {
623                paramString.append(", " + configService.getPropertyValueAsString(
624                        UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "ampersand"));
625            }
626            if (allowBackslash || allowAll) {
627               paramString.append(", " + configService.getPropertyValueAsString(
628                        UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "backslash"));
629            }
630            if (allowCurlyBraces || allowAll) {
631                paramString.append(", " + configService.getPropertyValueAsString(
632                        UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "curlyBraces"));
633            }
634            if (allowBrackets || allowAll) {
635                paramString.append(", " + configService.getPropertyValueAsString(
636                        UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "brackets"));
637            }
638
639            validationMessageParams.add(paramString.toString());
640        }
641        return this.validationMessageParams;
642    }
643
644}