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.validation;
036
037import java.util.ArrayList;
038import java.util.Iterator;
039import java.util.List;
040
041import javax.validation.ConstraintViolation;
042import javax.validation.Path;
043import javax.validation.metadata.ConstraintDescriptor;
044
045/**
046 * Represents a constraint violation received from the server.
047 * 
048 * @author William DRAI
049 */
050public class ServerConstraintViolation implements ConstraintViolation<Object> {
051        
052        private InvalidValue invalidValue;
053        private Object rootBean;
054        private Object bean;
055        private Path propertyPath;
056        private String message;
057        
058        
059        /**
060         * Constructs a new <code>ServerConstraintViolation</code> instance.
061         * 
062         * @param invalidValue serialized server-side ConstraintViolation
063         * @param rootBean root bean
064         * @param bean leaf bean
065         */
066        public ServerConstraintViolation(InvalidValue invalidValue, Object rootBean, Object bean) {
067                this.rootBean = rootBean;
068                this.bean = bean;               
069                this.propertyPath = new PathImpl(invalidValue.getPath());
070                this.message = invalidValue.getMessage();
071        }
072
073
074        public InvalidValue getInvalidValue() {
075                return invalidValue;
076        }
077
078        public Object getRootBean() {
079                return rootBean;
080        }
081
082        @Override
083        public Class<Object> getRootBeanClass() {
084                return Object.class;
085        }
086
087        public Object getLeafBean() {
088                return bean;
089        }
090
091        public Path getPropertyPath() {
092                return propertyPath;
093        }
094
095        @Override
096        public String getMessage() {
097                return message;
098        }
099
100        @Override
101        public String getMessageTemplate() {
102                return message;
103        }
104
105        @Override
106        public ConstraintDescriptor<?> getConstraintDescriptor() {
107                return null;
108        }
109        
110        public class PathImpl implements Path {
111                
112                private List<Node> nodeList = new ArrayList<Node>();
113                
114                public PathImpl(final String path) {
115                        nodeList.add(new Node() {
116                                @Override
117                                public boolean isInIterable() {
118                                        return true;
119                                }
120                                
121                                @Override
122                                public String getName() {
123                                        return path;
124                                }
125                                
126                                @Override
127                                public Object getKey() {
128                                        return null;
129                                }
130                                
131                                @Override
132                                public Integer getIndex() {
133                                        return null;
134                                }
135                        });
136                }
137                
138                @Override
139                public Iterator<Node> iterator() {
140                        return nodeList.iterator();
141                }
142                
143        }
144}