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; 021 022import java.util.Collections; 023import java.util.Map; 024import java.util.TreeMap; 025 026/** 027 * Thrown when model data violate the rules with JSR Bean Validation 028 * 029 * @author Alisson Gomes 030 * @since 0.6.0 031 */ 032public class ConstraintException extends RepositoryException 033{ 034 private static final long serialVersionUID = -1607171467309827392L; 035 036 private final Map<String, String> violations; 037 038 /** 039 * Constructor for ConstraintException without message detail 040 * @param param the name of parameter with error 041 * @param message the user message 042 */ 043 public ConstraintException(String param, String message) 044 { 045 super(message); 046 this.violations = new TreeMap<String, String>(); 047 this.violations.put(param, message); 048 } 049 050 /** 051 * Constructor for ConstraintException with a set of messages 052 * @param violations violated constraints from model, with pair of values field and message 053 */ 054 public ConstraintException(Map<String, String> violations) 055 { 056 super(); 057 this.violations = violations; 058 } 059 060 /** 061 * Map with violations where key is {@code javax.validation.ConstraintViolation.getPropertyPath()} name with 062 * violation message. 063 * @return set of violation messages from Bean Validation 064 */ 065 public Map<String, String> getViolations() 066 { 067 return Collections.unmodifiableMap(violations); 068 } 069 070 @Override 071 public String getMessage() 072 { 073 StringBuilder sb = new StringBuilder(getClass().getName()+": "); 074 sb.append(super.getMessage() == null ? "There " + 075 (violations.size() > 1 ? "are " : "is ") + violations.size() + 076 " data violations" : super.getMessage()); 077 for(String k : violations.keySet()) 078 sb.append("\n\t["+k+"]="+violations.get(k)); 079 return sb.toString(); 080 } 081 082 @Override 083 public String toString() 084 { 085 return getMessage(); 086 } 087}