001    /*
002      GRANITE DATA SERVICES
003      Copyright (C) 2012 GRANITE DATA SERVICES S.A.S.
004    
005      This file is part of Granite Data Services.
006    
007      Granite Data Services is free software; you can redistribute it and/or modify
008      it under the terms of the GNU Library General Public License as published by
009      the Free Software Foundation; either version 2 of the License, or (at your
010      option) any later version.
011    
012      Granite Data Services is distributed in the hope that it will be useful, but
013      WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014      FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015      for more details.
016    
017      You should have received a copy of the GNU Library General Public License
018      along with this library; if not, see <http://www.gnu.org/licenses/>.
019    */
020    
021    package org.granite.client.validation;
022    
023    import java.util.ArrayList;
024    import java.util.Iterator;
025    import java.util.List;
026    
027    import javax.validation.ConstraintViolation;
028    import javax.validation.Path;
029    import javax.validation.metadata.ConstraintDescriptor;
030    
031    /**
032     * Represents a constraint violation received from the server.
033     * 
034     * @author William DRAI
035     */
036    public class ServerConstraintViolation implements ConstraintViolation<Object> {
037            
038            private InvalidValue invalidValue;
039            private Object rootBean;
040            private Object bean;
041            private Path propertyPath;
042            private String message;
043            
044            
045            /**
046             * Constructs a new <code>ServerConstraintViolation</code> instance.
047             * 
048             * @param invalidValue serialized server-side ConstraintViolation
049             * @param rootBean root bean
050             * @param bean leaf bean
051             */
052            public ServerConstraintViolation(InvalidValue invalidValue, Object rootBean, Object bean) {
053                    this.rootBean = rootBean;
054                    this.bean = bean;               
055                    this.propertyPath = new PathImpl(invalidValue.getPath());
056                    this.message = invalidValue.getMessage();
057            }
058    
059    
060            public InvalidValue getInvalidValue() {
061                    return invalidValue;
062            }
063    
064            public Object getRootBean() {
065                    return rootBean;
066            }
067    
068            @Override
069            public Class<Object> getRootBeanClass() {
070                    return Object.class;
071            }
072    
073            public Object getLeafBean() {
074                    return bean;
075            }
076    
077            public Path getPropertyPath() {
078                    return propertyPath;
079            }
080    
081            @Override
082            public String getMessage() {
083                    return message;
084            }
085    
086            @Override
087            public String getMessageTemplate() {
088                    return message;
089            }
090    
091            @Override
092            public ConstraintDescriptor<?> getConstraintDescriptor() {
093                    return null;
094            }
095            
096            public class PathImpl implements Path {
097                    
098                    private List<Node> nodeList = new ArrayList<Node>();
099                    
100                    public PathImpl(final String path) {
101                            nodeList.add(new Node() {
102                                    @Override
103                                    public boolean isInIterable() {
104                                            return true;
105                                    }
106                                    
107                                    @Override
108                                    public String getName() {
109                                            return path;
110                                    }
111                                    
112                                    @Override
113                                    public Object getKey() {
114                                            return null;
115                                    }
116                                    
117                                    @Override
118                                    public Integer getIndex() {
119                                            return null;
120                                    }
121                            });
122                    }
123                    
124                    @Override
125                    public Iterator<Node> iterator() {
126                            return nodeList.iterator();
127                    }
128                    
129            }
130    }