|
| 1 | +package org.rest.sec.dto; |
| 2 | + |
| 3 | +import java.util.Set; |
| 4 | + |
| 5 | +import javax.xml.bind.annotation.XmlRootElement; |
| 6 | + |
| 7 | +import org.apache.commons.lang.builder.ToStringBuilder; |
| 8 | +import org.rest.common.IEntity; |
| 9 | +import org.rest.sec.model.Principal; |
| 10 | +import org.rest.sec.model.Role; |
| 11 | + |
| 12 | +import com.thoughtworks.xstream.annotations.XStreamAlias; |
| 13 | +import com.thoughtworks.xstream.annotations.XStreamAsAttribute; |
| 14 | +import com.thoughtworks.xstream.annotations.XStreamImplicit; |
| 15 | + |
| 16 | +@XmlRootElement |
| 17 | +@XStreamAlias( "user" ) |
| 18 | +public class User implements IEntity{ |
| 19 | + |
| 20 | + @XStreamAsAttribute |
| 21 | + private Long id; |
| 22 | + |
| 23 | + private String name; |
| 24 | + |
| 25 | + /* Marshalling */ |
| 26 | + // - note: this gets rid of the collection entirely |
| 27 | + @XStreamImplicit |
| 28 | + // - note: this requires: xstream.addDefaultImplementation( java.util.HashSet.class, PersistentSet.class ); |
| 29 | + // @XStreamConverter( value = HibernateCollectionConverter.class ) |
| 30 | + private Set< Role > roles; |
| 31 | + |
| 32 | + public User(){ |
| 33 | + super(); |
| 34 | + } |
| 35 | + public User( final String nameToSet ){ |
| 36 | + super(); |
| 37 | + |
| 38 | + name = nameToSet; |
| 39 | + } |
| 40 | + public User( final Principal principal ){ |
| 41 | + super(); |
| 42 | + |
| 43 | + name = principal.getName(); |
| 44 | + roles = principal.getRoles(); |
| 45 | + } |
| 46 | + |
| 47 | + // API |
| 48 | + |
| 49 | + @Override |
| 50 | + public Long getId(){ |
| 51 | + return id; |
| 52 | + } |
| 53 | + @Override |
| 54 | + public void setId( final Long idToSet ){ |
| 55 | + id = idToSet; |
| 56 | + } |
| 57 | + |
| 58 | + public String getName(){ |
| 59 | + return name; |
| 60 | + } |
| 61 | + public void setName( final String nameToSet ){ |
| 62 | + name = nameToSet; |
| 63 | + } |
| 64 | + |
| 65 | + public Set< Role > getRoles(){ |
| 66 | + return roles; |
| 67 | + } |
| 68 | + public void setRoles( final Set< Role > rolesToSet ){ |
| 69 | + roles = rolesToSet; |
| 70 | + } |
| 71 | + |
| 72 | + // |
| 73 | + |
| 74 | + @Override |
| 75 | + public int hashCode(){ |
| 76 | + final int prime = 31; |
| 77 | + int result = 1; |
| 78 | + result = prime * result + ( ( name == null ) ? 0 : name.hashCode() ); |
| 79 | + return result; |
| 80 | + } |
| 81 | + @Override |
| 82 | + public boolean equals( final Object obj ){ |
| 83 | + if( this == obj ) |
| 84 | + return true; |
| 85 | + if( obj == null ) |
| 86 | + return false; |
| 87 | + if( getClass() != obj.getClass() ) |
| 88 | + return false; |
| 89 | + final User other = (User) obj; |
| 90 | + if( name == null ){ |
| 91 | + if( other.name != null ) |
| 92 | + return false; |
| 93 | + } |
| 94 | + else if( !name.equals( other.name ) ) |
| 95 | + return false; |
| 96 | + return true; |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public String toString(){ |
| 101 | + return new ToStringBuilder( this ).append( "id", id ).append( "name", name ).toString(); |
| 102 | + } |
| 103 | + |
| 104 | +} |
0 commit comments