wsdor/src/main/java/cz/xelfi/quoridor/webidor/User.java
changeset 256 1758a7727278
parent 178 4b78d4f028b3
child 264 d60370059c3c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/wsdor/src/main/java/cz/xelfi/quoridor/webidor/User.java	Sat Sep 11 17:04:51 2010 +0200
     1.3 @@ -0,0 +1,128 @@
     1.4 +/*
     1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 + *
     1.7 + * The contents of this file are subject to the terms of either the GNU
     1.8 + * General Public License Version 2 only ("GPL") or the Common
     1.9 + * Development and Distribution License("CDDL") (collectively, the
    1.10 + * "License"). You may not use this file except in compliance with the
    1.11 + * License. You can obtain a copy of the License at
    1.12 + * http://www.netbeans.org/cddl-gplv2.html
    1.13 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    1.14 + * specific language governing permissions and limitations under the
    1.15 + * License.  When distributing the software, include this License Header
    1.16 + * Notice in each file and include the License file at
    1.17 + * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    1.18 + * particular file as subject to the "Classpath" exception as provided
    1.19 + * by Sun in the GPL Version 2 section of the License file that
    1.20 + * accompanied this code. If applicable, add the following below the
    1.21 + * License Header, with the fields enclosed by brackets [] replaced by
    1.22 + * your own identifying information:
    1.23 + * "Portions Copyrighted [year] [name of copyright owner]"
    1.24 + *
    1.25 + * Contributor(s):
    1.26 + *
    1.27 + * Portions Copyrighted 2009 Jaroslav Tulach
    1.28 + */
    1.29 +
    1.30 +package cz.xelfi.quoridor.webidor;
    1.31 +
    1.32 +import java.util.ArrayList;
    1.33 +import java.util.List;
    1.34 +import java.util.Set;
    1.35 +import java.util.TreeSet;
    1.36 +import javax.xml.bind.annotation.XmlAccessType;
    1.37 +import javax.xml.bind.annotation.XmlAccessorType;
    1.38 +import javax.xml.bind.annotation.XmlAttribute;
    1.39 +import javax.xml.bind.annotation.XmlElement;
    1.40 +import javax.xml.bind.annotation.XmlRootElement;
    1.41 +import javax.xml.bind.annotation.XmlValue;
    1.42 +
    1.43 +/**
    1.44 + *
    1.45 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.46 + */
    1.47 +@XmlRootElement
    1.48 +@XmlAccessorType(XmlAccessType.NONE)
    1.49 +public final class User extends Object {
    1.50 +    @XmlAttribute
    1.51 +    private String id;
    1.52 +    @XmlElement(name="property")
    1.53 +    private List<Property> properties;
    1.54 +    private transient Set<String> permissions;
    1.55 +
    1.56 +    User() {
    1.57 +    }
    1.58 +
    1.59 +    public User(String id) {
    1.60 +        this.id = id;
    1.61 +    }
    1.62 +
    1.63 +    public void addProperty(String name, String value) {
    1.64 +        if (properties == null) {
    1.65 +            properties = new ArrayList<Property>();
    1.66 +        }
    1.67 +        properties.add(new Property(name, value));
    1.68 +    }
    1.69 +
    1.70 +    public void addPermission(String permission) {
    1.71 +        if (permissions == null) {
    1.72 +            permissions = new TreeSet<String>();
    1.73 +        }
    1.74 +        permissions.add(permission);
    1.75 +    }
    1.76 +
    1.77 +    public String getProperty(String name) {
    1.78 +        if (properties == null) {
    1.79 +            return null;
    1.80 +        }
    1.81 +        for (Property p : properties) {
    1.82 +            if (p.name.equals(name)) {
    1.83 +                return p.value;
    1.84 +            }
    1.85 +        }
    1.86 +        return null;
    1.87 +    }
    1.88 +
    1.89 +    public boolean hasPermission(String permission) {
    1.90 +        if (permissions == null) {
    1.91 +            return false;
    1.92 +        }
    1.93 +        return permissions.contains(permission);
    1.94 +    }
    1.95 +
    1.96 +    public static boolean canSee(GameId gId, String userId) {
    1.97 +        if (!gId.isFinished()) {
    1.98 +            return true;
    1.99 +        }
   1.100 +        if (userId.equals(gId.getWhite())) {
   1.101 +            return true;
   1.102 +        }
   1.103 +        if (userId.equals(gId.getBlack())) {
   1.104 +            return true;
   1.105 +        }
   1.106 +        return false;
   1.107 +    }
   1.108 +
   1.109 +    public boolean canSee(GameId gId){
   1.110 +        return canSee(gId, id);
   1.111 +    }
   1.112 +
   1.113 +    public String getId() {
   1.114 +        return id;
   1.115 +    }
   1.116 +
   1.117 +    public static final class Property {
   1.118 +        @XmlAttribute
   1.119 +        private String name;
   1.120 +        @XmlValue
   1.121 +        private String value;
   1.122 +
   1.123 +        private Property() {
   1.124 +        }
   1.125 +
   1.126 +        Property(String name, String value) {
   1.127 +            this.name = name;
   1.128 +            this.value = value;
   1.129 +        }
   1.130 +    }
   1.131 +}