/projects/wct-1.5.2/WCTCore/src-app/org/webcurator/ui/admin/validator/TemplateValidator.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus · Java · 88 lines · 56 code · 11 blank · 21 comment · 11 complexity · 1ab9f683880af30a3240fe7e4b43a3ef MD5 · raw file

  1. /*
  2. * Copyright 2006 The National Library of New Zealand
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.webcurator.ui.admin.validator;
  17. import org.apache.commons.logging.Log;
  18. import org.apache.commons.logging.LogFactory;
  19. import org.springframework.validation.Errors;
  20. import org.springframework.validation.ValidationUtils;
  21. import org.webcurator.ui.admin.command.AgencyCommand;
  22. import org.webcurator.ui.admin.command.TemplateCommand;
  23. import org.webcurator.ui.admin.controller.TemplateController;
  24. import org.webcurator.ui.common.validation.AbstractBaseValidator;
  25. import org.webcurator.ui.common.validation.ValidatorUtil;
  26. import org.webcurator.domain.model.core.PermissionTemplate;
  27. /**
  28. * Validates the Template Creation action, and checks all the fields used in creating a Permission Template
  29. * @author bprice
  30. */
  31. public class TemplateValidator extends AbstractBaseValidator {
  32. /** @see org.springframework.validation.Validator#supports(Class).*/
  33. public boolean supports(Class aClass) {
  34. return aClass.equals(TemplateCommand.class);
  35. }
  36. /** @see org.springframework.validation.Validator#validate(Object, Errors). */
  37. public void validate(Object aCmd, Errors aErrors) {
  38. TemplateCommand tempCmd = (TemplateCommand) aCmd;
  39. ValidationUtils.rejectIfEmptyOrWhitespace(aErrors, TemplateCommand.PARAM_ACTION, "required", getObjectArrayForLabel(TemplateCommand.PARAM_ACTION), "Action command is a required field.");
  40. if (AgencyCommand.ACTION_SAVE.equals(tempCmd.getAction())) {
  41. ValidationUtils.rejectIfEmptyOrWhitespace(aErrors, TemplateCommand.PARAM_AGENCY_OID, "required", getObjectArrayForLabel(TemplateCommand.PARAM_AGENCY_OID), "A specified Agency is a required field.");
  42. ValidationUtils.rejectIfEmptyOrWhitespace(aErrors, TemplateCommand.PARAM_TEMPLATE_NAME, "required", getObjectArrayForLabel(TemplateCommand.PARAM_TEMPLATE_NAME), "The template name is a required field.");
  43. ValidationUtils.rejectIfEmptyOrWhitespace(aErrors, TemplateCommand.PARAM_TEMPLATE_TYPE, "required", getObjectArrayForLabel(TemplateCommand.PARAM_TEMPLATE_TYPE), "The template type is a required field.");
  44. ValidationUtils.rejectIfEmptyOrWhitespace(aErrors, TemplateCommand.PARAM_TEMPLATE_TEXT, "required", getObjectArrayForLabel(TemplateCommand.PARAM_TEMPLATE_TEXT), "The template text is a required field.");
  45. if (tempCmd.getTemplateType().equals(PermissionTemplate.EMAIL_TYPE_TEMPLATE))
  46. {
  47. ValidationUtils.rejectIfEmptyOrWhitespace(aErrors, TemplateCommand.PARAM_TEMPLATE_SUBJECT, "required", getObjectArrayForLabel(TemplateCommand.PARAM_TEMPLATE_SUBJECT), "The template subject is a required field.");
  48. ValidationUtils.rejectIfEmptyOrWhitespace(aErrors, TemplateCommand.PARAM_TEMPLATE_OVERWRITE_FROM, "required", getObjectArrayForLabel(TemplateCommand.PARAM_TEMPLATE_OVERWRITE_FROM), "The template overwrite from is a required field.");
  49. if (tempCmd.getTemplateOverwriteFrom())
  50. ValidationUtils.rejectIfEmptyOrWhitespace(aErrors, TemplateCommand.PARAM_TEMPLATE_FROM, "required", getObjectArrayForLabel(TemplateCommand.PARAM_TEMPLATE_FROM), "The template from is a required field when the template overwrite option is selected.");
  51. if (tempCmd.getTemplateFrom() != null && tempCmd.getTemplateFrom().length() > 0) {
  52. ValidatorUtil.validateRegEx(aErrors, tempCmd.getTemplateFrom(), ValidatorUtil.EMAIL_VALIDATION_REGEX, "invalid.email",getObjectArrayForLabel(TemplateCommand.PARAM_TEMPLATE_FROM),"the template from email address is invalid" );
  53. }
  54. checkEmails(aErrors, tempCmd.getTemplateCc(), TemplateCommand.PARAM_TEMPLATE_CC, "template CC");
  55. checkEmails(aErrors, tempCmd.getTemplateBcc(), TemplateCommand.PARAM_TEMPLATE_BCC, "template BCC");
  56. }
  57. TemplateValidatorHelper validatorHelper = new TemplateValidatorHelper(tempCmd.getTemplateText(), tempCmd.getTemplateType());
  58. validatorHelper.parseForErrors(aErrors);
  59. }
  60. }
  61. private void checkEmails(Errors aErrors, String emails,
  62. String fieldConstant, String label) {
  63. if (emails != null && emails.length() > 0) {
  64. if (emails.contains(";"))
  65. {
  66. for (String email: emails.split(";"))
  67. {
  68. ValidatorUtil.validateRegEx(aErrors, email, ValidatorUtil.EMAIL_VALIDATION_REGEX, "invalid.email",getObjectArrayForLabel(fieldConstant),"the "+label+" email address is invalid" );
  69. }
  70. }
  71. else
  72. {
  73. ValidatorUtil.validateRegEx(aErrors, emails, ValidatorUtil.EMAIL_VALIDATION_REGEX, "invalid.email",getObjectArrayForLabel(fieldConstant),"the "+label+" email address is invalid" );
  74. }
  75. }
  76. }
  77. }