001/** 002 * GRANITE DATA SERVICES 003 * Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S. 004 * 005 * This file is part of the Granite Data Services Platform. 006 * 007 * *** 008 * 009 * Community License: GPL 3.0 010 * 011 * This file is free software: you can redistribute it and/or modify 012 * it under the terms of the GNU General Public License as published 013 * by the Free Software Foundation, either version 3 of the License, 014 * or (at your option) any later version. 015 * 016 * This file is distributed in the hope that it will be useful, but 017 * WITHOUT ANY WARRANTY; without even the implied warranty of 018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 019 * GNU General Public License for more details. 020 * 021 * You should have received a copy of the GNU General Public License 022 * along with this program. If not, see <http://www.gnu.org/licenses/>. 023 * 024 * *** 025 * 026 * Available Commercial License: GraniteDS SLA 1.0 027 * 028 * This is the appropriate option if you are creating proprietary 029 * applications and you are not prepared to distribute and share the 030 * source code of your application under the GPL v3 license. 031 * 032 * Please visit http://www.granitedataservices.com/license for more 033 * details. 034 */ 035package org.granite.client.tide.validation; 036 037import java.util.HashSet; 038import java.util.Set; 039 040import javax.validation.ConstraintViolation; 041 042import org.granite.client.messaging.messages.responses.FaultMessage; 043import org.granite.client.messaging.messages.responses.FaultMessage.Code; 044import org.granite.client.tide.Context; 045import org.granite.client.tide.server.ExceptionHandler; 046import org.granite.client.tide.server.TideFaultEvent; 047import org.granite.client.validation.InvalidValue; 048import org.granite.client.validation.NotifyingValidatorFactory; 049import org.granite.client.validation.ServerConstraintViolation; 050 051/** 052 * @author William DRAI 053 */ 054public class ValidationExceptionHandler implements ExceptionHandler { 055 056 @Override 057 public boolean accepts(FaultMessage emsg) { 058 return emsg.getCode().equals(Code.VALIDATION_FAILED); 059 } 060 061 @Override 062 public void handle(Context context, FaultMessage emsg, TideFaultEvent faultEvent) { 063 Object[] invalidValues = emsg.getExtended() != null ? (Object[])emsg.getExtended().get("invalidValues") : null; 064 if (invalidValues != null) { 065 Set<ConstraintViolation<?>> constraintViolations = new HashSet<ConstraintViolation<?>>(); 066 067 for (Object v : invalidValues) { 068 InvalidValue iv = (InvalidValue)v; 069 Object rootBean = context.getEntityManager().getCachedObject(iv.getRootBean(), true); 070 Object leafBean = null; 071 if (iv.getBean() != null) { 072 leafBean = context.getEntityManager().getCachedObject(iv.getBean(), true); 073 if (leafBean == null) { 074 // Embedded ? 075 Object bean = rootBean; 076 String[] path = iv.getPath().split("\\."); 077 for (int i = 0; i < path.length-1; i++) 078 bean = context.getDataManager().getPropertyValue(bean, path[i]); 079 leafBean = bean; 080 } 081 } 082 083 ServerConstraintViolation violation = new ServerConstraintViolation(iv, rootBean, leafBean); 084 constraintViolations.add(violation); 085 } 086 087 NotifyingValidatorFactory notifyingValidatorFactory = context.byType(NotifyingValidatorFactory.class); 088 if (notifyingValidatorFactory == null) 089 throw new RuntimeException("No suitable validator factory defined, cannot process validation events"); 090 091 notifyingValidatorFactory.getValidator().notifyConstraintViolations(null, constraintViolations); 092 } 093 } 094 095}