001/* 002 * JKNIV, SQLegance keeping queries maintainable. 003 * 004 * Copyright (C) 2017, the original author or authors. 005 * 006 * This library is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 2.1 of the License. 010 * 011 * This library is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public 017 * License along with this library; if not, write to the Free Software Foundation, Inc., 018 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 019 */ 020package net.sf.jkniv.sqlegance.validation; 021 022import java.util.Map; 023 024import org.slf4j.Logger; 025import org.slf4j.LoggerFactory; 026 027import net.sf.jkniv.sqlegance.ConstraintException; 028 029public enum ValidateType 030{ 031 NONE { 032 public Class<?> getValidateGroup() { return javax.validation.groups.Default.class; } 033 Validatory getValidatory() { return ValidateType.novalidator; } 034 }, 035 ALL { 036 public Class<?> getValidateGroup() { return AllValidate.class; } 037 Validatory getValidatory() { return ValidateType.validator; } 038 }, 039 GET { 040 public Class<?> getValidateGroup() { return GetValidate.class; } 041 Validatory getValidatory() { return ValidateType.validator; } 042 }, 043 LIST { 044 public Class<?> getValidateGroup() { return ListValidate.class; } 045 Validatory getValidatory() { return ValidateType.validator; } 046 }, 047 SELECT { 048 public Class<?> getValidateGroup() { return SelectValidate.class; } 049 Validatory getValidatory() { return ValidateType.validator; } 050 }, 051 SCALAR { 052 public Class<?> getValidateGroup() { return ScalarValidate.class; } 053 Validatory getValidatory() { return ValidateType.validator; } 054 }, 055 ADD { 056 public Class<?> getValidateGroup() { return AddValidate.class; } 057 Validatory getValidatory() { return ValidateType.validator; } 058 }, 059 UPDATE { 060 public Class<?> getValidateGroup() { return UpdateValidate.class; } 061 Validatory getValidatory() { return ValidateType.validator; } 062 }, 063 ENRICH { 064 public Class<?> getValidateGroup() { return EnrichValidate.class; } 065 Validatory getValidatory() { return ValidateType.validator; } 066 }, 067 REMOVE { 068 public Class<?> getValidateGroup() { return RemoveValidate.class; } 069 Validatory getValidatory() { return ValidateType.validator; } 070 }; 071 072 public abstract Class<?> getValidateGroup(); 073 abstract Validatory getValidatory(); 074 075 private static final Validatory novalidator = new NoValidate(); 076 private static Validatory validator; 077 private static final Logger LOG = LoggerFactory.getLogger(ValidateType.class); 078 static { 079 try 080 { 081 validator = new ValidateImpl(); 082 } 083 catch (Exception e) 084 { 085 LOG.warn("Default Bean validator Factory not found, cannot apply validator"); 086 validator = new NoValidate(); 087 } 088 } 089 090 /** 091 * @param type of validate 092 * @return enum with valueof {@code type}, ValidateType.NONE if doesn't match 093 */ 094 public static ValidateType get(String type) 095 { 096 ValidateType validateType = ValidateType.NONE; 097 for (ValidateType t : ValidateType.values()) 098 { 099 if (String.valueOf(type).equalsIgnoreCase(t.name())) 100 { 101 validateType = t; 102 break; 103 } 104 } 105 return validateType; 106 } 107 108 /** 109 * Use validator (JSR 303) to perform validation against domain model, 110 * when some constraint is violated {@code ConstraintException} is throw 111 * @param params the domain model to validate 112 * @throws ConstraintException when some constraint is violated 113 */ 114 public void assertValidate(Object params) 115 { 116 getValidatory().assertValidate(params, this); 117 } 118 119 /** 120 * Use validator (JSR 303) to perform validation against domain model 121 * @param params the domain model to validate 122 * @return the pairs field and constraints violated, an empty Map is return when any constraint is violated 123 */ 124 public Map<String, String> validate(Object params) 125 { 126 return getValidatory().validate(params, this); 127 } 128 129//// 130 131 /** 132 * Use validator (JSR 303) to perform validation against domain model 133 * @param params the domain model to validate 134 * @param validateType type of validation 135 * @throws ConstraintException when some constraint is violated 136 */ 137 public static void assertValidate(Object params, ValidateType validateType) 138 { 139 validator.assertValidate(params, validateType); 140 } 141 142 /** 143 * Use validator (JSR 303) to perform validation against domain model 144 * @param params the domain model to validate 145 * @param validateGroup type of validation 146 * @throws ConstraintException when some constraint is violated 147 */ 148 public static <T> void assertValidate(Object params, Class<T> validateGroup) 149 { 150 validator.assertValidate(params, validateGroup); 151 } 152 /** 153 * Use validator (JSR 303) to perform validation against domain model 154 * @param params the domain model to validate 155 * @param validateType type of validation 156 * @return the pairs field and constraints violated, an empty Map is return when any constraint is violated 157 */ 158 public static Map<String, String> validate(Object params, ValidateType validateType) 159 { 160 return validator.validate(params, validateType); 161 } 162 163 /** 164 * Use validator (JSR 303) to perform validation against domain model 165 * @param params the domain model to validate 166 * @param validateGroup type of validation 167 * @return the pairs field and constraints violated, an empty Map is return when any constraint is violated 168 */ 169 public static <T> Map<String, String> validate(Object params, Class<T> validateGroup) 170 { 171 return validator.validate(params, validateGroup); 172 } 173}