wsdor/src/main/java/cz/xelfi/quoridor/webidor/User.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 11 Sep 2010 17:04:51 +0200
changeset 256 1758a7727278
parent 178 webidor/src/main/java/cz/xelfi/quoridor/webidor/User.java@4b78d4f028b3
child 264 d60370059c3c
permissions -rw-r--r--
Splitting classes representing data types on the server into own module
jaroslav@143
     1
/*
jaroslav@143
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@143
     3
 *
jaroslav@143
     4
 * The contents of this file are subject to the terms of either the GNU
jaroslav@143
     5
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@143
     6
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@143
     7
 * "License"). You may not use this file except in compliance with the
jaroslav@143
     8
 * License. You can obtain a copy of the License at
jaroslav@143
     9
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@143
    10
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@143
    11
 * specific language governing permissions and limitations under the
jaroslav@143
    12
 * License.  When distributing the software, include this License Header
jaroslav@143
    13
 * Notice in each file and include the License file at
jaroslav@143
    14
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
jaroslav@143
    15
 * particular file as subject to the "Classpath" exception as provided
jaroslav@143
    16
 * by Sun in the GPL Version 2 section of the License file that
jaroslav@143
    17
 * accompanied this code. If applicable, add the following below the
jaroslav@143
    18
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@143
    19
 * your own identifying information:
jaroslav@143
    20
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@143
    21
 *
jaroslav@143
    22
 * Contributor(s):
jaroslav@143
    23
 *
jaroslav@143
    24
 * Portions Copyrighted 2009 Jaroslav Tulach
jaroslav@143
    25
 */
jaroslav@143
    26
jaroslav@143
    27
package cz.xelfi.quoridor.webidor;
jaroslav@143
    28
jaroslav@143
    29
import java.util.ArrayList;
jaroslav@143
    30
import java.util.List;
jaroslav@171
    31
import java.util.Set;
jaroslav@171
    32
import java.util.TreeSet;
jaroslav@143
    33
import javax.xml.bind.annotation.XmlAccessType;
jaroslav@143
    34
import javax.xml.bind.annotation.XmlAccessorType;
jaroslav@143
    35
import javax.xml.bind.annotation.XmlAttribute;
jaroslav@143
    36
import javax.xml.bind.annotation.XmlElement;
jaroslav@143
    37
import javax.xml.bind.annotation.XmlRootElement;
jaroslav@143
    38
import javax.xml.bind.annotation.XmlValue;
jaroslav@143
    39
jaroslav@143
    40
/**
jaroslav@143
    41
 *
jaroslav@143
    42
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@143
    43
 */
jaroslav@143
    44
@XmlRootElement
jaroslav@143
    45
@XmlAccessorType(XmlAccessType.NONE)
jaroslav@143
    46
public final class User extends Object {
jaroslav@143
    47
    @XmlAttribute
jaroslav@143
    48
    private String id;
jaroslav@143
    49
    @XmlElement(name="property")
jaroslav@143
    50
    private List<Property> properties;
jaroslav@171
    51
    private transient Set<String> permissions;
jaroslav@143
    52
jaroslav@143
    53
    User() {
jaroslav@143
    54
    }
jaroslav@143
    55
jaroslav@143
    56
    public User(String id) {
jaroslav@143
    57
        this.id = id;
jaroslav@143
    58
    }
jaroslav@143
    59
jaroslav@143
    60
    public void addProperty(String name, String value) {
jaroslav@143
    61
        if (properties == null) {
jaroslav@143
    62
            properties = new ArrayList<Property>();
jaroslav@143
    63
        }
jaroslav@143
    64
        properties.add(new Property(name, value));
jaroslav@143
    65
    }
jaroslav@143
    66
jaroslav@171
    67
    public void addPermission(String permission) {
jaroslav@171
    68
        if (permissions == null) {
jaroslav@171
    69
            permissions = new TreeSet<String>();
jaroslav@171
    70
        }
jaroslav@171
    71
        permissions.add(permission);
jaroslav@171
    72
    }
jaroslav@171
    73
jaroslav@143
    74
    public String getProperty(String name) {
jaroslav@143
    75
        if (properties == null) {
jaroslav@143
    76
            return null;
jaroslav@143
    77
        }
jaroslav@143
    78
        for (Property p : properties) {
jaroslav@143
    79
            if (p.name.equals(name)) {
jaroslav@143
    80
                return p.value;
jaroslav@143
    81
            }
jaroslav@143
    82
        }
jaroslav@143
    83
        return null;
jaroslav@143
    84
    }
jaroslav@143
    85
jaroslav@171
    86
    public boolean hasPermission(String permission) {
jaroslav@171
    87
        if (permissions == null) {
jaroslav@171
    88
            return false;
jaroslav@171
    89
        }
jaroslav@171
    90
        return permissions.contains(permission);
jaroslav@171
    91
    }
jaroslav@171
    92
jaroslav@178
    93
    public static boolean canSee(GameId gId, String userId) {
jaroslav@178
    94
        if (!gId.isFinished()) {
jaroslav@178
    95
            return true;
jaroslav@178
    96
        }
jaroslav@178
    97
        if (userId.equals(gId.getWhite())) {
jaroslav@178
    98
            return true;
jaroslav@178
    99
        }
jaroslav@178
   100
        if (userId.equals(gId.getBlack())) {
jaroslav@178
   101
            return true;
jaroslav@178
   102
        }
jaroslav@178
   103
        return false;
jaroslav@178
   104
    }
jaroslav@178
   105
jaroslav@178
   106
    public boolean canSee(GameId gId){
jaroslav@178
   107
        return canSee(gId, id);
jaroslav@178
   108
    }
jaroslav@178
   109
jaroslav@143
   110
    public String getId() {
jaroslav@143
   111
        return id;
jaroslav@143
   112
    }
jaroslav@143
   113
jaroslav@143
   114
    public static final class Property {
jaroslav@143
   115
        @XmlAttribute
jaroslav@143
   116
        private String name;
jaroslav@143
   117
        @XmlValue
jaroslav@143
   118
        private String value;
jaroslav@143
   119
jaroslav@143
   120
        private Property() {
jaroslav@143
   121
        }
jaroslav@143
   122
jaroslav@143
   123
        Property(String name, String value) {
jaroslav@143
   124
            this.name = name;
jaroslav@143
   125
            this.value = value;
jaroslav@143
   126
        }
jaroslav@143
   127
    }
jaroslav@143
   128
}