001/* Copyright (C) 2014 konik.io 002 * 003 * This file is part of the Konik library. 004 * 005 * The Konik library is free software: you can redistribute it and/or modify 006 * it under the terms of the GNU Affero General Public License as 007 * published by the Free Software Foundation, either version 3 of the 008 * License, or (at your option) any later version. 009 * 010 * The Konik library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU Affero General Public License for more details. 014 * 015 * You should have received a copy of the GNU Affero General Public License 016 * along with the Konik library. If not, see <http://www.gnu.org/licenses/>. 017 */ 018package io.konik.validation; 019 020import io.konik.validator.annotation.Basic; 021import io.konik.validator.annotation.Comfort; 022import io.konik.validator.annotation.Extended; 023import io.konik.zugferd.Invoice; 024import io.konik.zugferd.profile.ConformanceLevel; 025 026import java.util.Set; 027 028import javax.inject.Inject; 029import javax.inject.Named; 030import javax.inject.Singleton; 031import javax.validation.ConstraintViolation; 032import javax.validation.Validation; 033import javax.validation.Validator; 034import javax.validation.ValidatorFactory; 035import javax.validation.groups.Default; 036 037/** 038 * Validates the invoice against the declared invoice profile. 039 */ 040@Named 041@Singleton 042public class InvoiceValidator { 043 044 private Validator validator; 045 046 /** 047 * Instantiates a new invoice validator. 048 * 049 * @param validator the validator 050 */ 051 @Inject 052 public InvoiceValidator(Validator validator) { 053 super(); 054 this.validator = validator; 055 } 056 057 058 /** 059 * Instantiates a new default invoice validator, based on the Bean Validation provider 060 */ 061 public InvoiceValidator() { 062 ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); 063 this.validator = factory.getValidator(); 064 } 065 066 067 /** 068 * Validate the invoice 069 * 070 * @param invoice the invoice 071 * @return the sets the 072 */ 073 public Set<ConstraintViolation<Invoice>> validate(Invoice invoice) { 074 ConformanceLevel conformanceLevel = invoice.getContext().getGuideline().getConformanceLevel(); 075 Class<?>[] validationGroups = resolveIntoValidationGroups(conformanceLevel); 076 return validator.validate(invoice, validationGroups); 077 } 078 079 /** 080 * Resolve the given profile into bean validation groups. 081 * 082 * @param conformanceLevel the given profile 083 * @return the class[] list of validation group classes 084 */ 085 public static Class<?>[] resolveIntoValidationGroups(ConformanceLevel conformanceLevel) { 086 switch (conformanceLevel) { 087 case BASIC: 088 return new Class[] { Default.class}; 089 case COMFORT: 090 return new Class[] { Default.class, Comfort.class }; 091 case EXTENDED: 092 return new Class[] { Default.class, Comfort.class, Extended.class }; 093 default: 094 throw new IllegalArgumentException("Provided Profile:" + conformanceLevel + "not covered"); 095 } 096 } 097}