001/*
002 * (C) Copyright 2006-2009 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.platform.api.ws;
023
024import java.util.ArrayList;
025import java.util.List;
026
027import org.nuxeo.ecm.core.api.security.ACE;
028
029public class WsACE {
030
031    private String username;
032
033    private String permission;
034
035    private boolean isGranted;
036
037    public WsACE(String username, String permission, boolean isGranted) {
038        this.username = username;
039        this.permission = permission;
040        this.isGranted = isGranted;
041    }
042
043    public WsACE() {
044        this(null, null, false);
045    }
046
047    public WsACE(ACE ace) {
048        this(ace.getUsername(), ace.getPermission(), ace.isGranted());
049    }
050
051    public String getUsername() {
052        return username;
053    }
054
055    public String getPermission() {
056        return permission;
057    }
058
059    public boolean isGranted() {
060        return isGranted;
061    }
062
063    public void setGranted(boolean isGranted) {
064        this.isGranted = isGranted;
065    }
066
067    public void setUsername(String username) {
068        this.username = username;
069    }
070
071    public void setPermission(String permission) {
072        this.permission = permission;
073    }
074
075    public static WsACE[] wrap(ACE[] aces) {
076        List<WsACE> result = new ArrayList<WsACE>();
077
078        for (ACE ace : aces) {
079            result.add(new WsACE(ace));
080        }
081        return result.toArray(new WsACE[result.size()]);
082    }
083
084    @Override
085    public int hashCode() {
086        final int prime = 31;
087        int result = 1;
088        result = prime * result + (isGranted ? 1231 : 1237);
089        result = prime * result + ((permission == null) ? 0 : permission.hashCode());
090        result = prime * result + ((username == null) ? 0 : username.hashCode());
091        return result;
092    }
093
094    @Override
095    public boolean equals(Object obj) {
096        if (this == obj) {
097            return true;
098        }
099        if (obj == null) {
100            return false;
101        }
102        if (!(obj instanceof WsACE)) {
103            return false;
104        }
105        WsACE other = (WsACE) obj;
106        if (isGranted != other.isGranted) {
107            return false;
108        }
109        if (permission == null) {
110            if (other.permission != null) {
111                return false;
112            }
113        } else if (!permission.equals(other.permission)) {
114            return false;
115        }
116        if (username == null) {
117            if (other.username != null) {
118                return false;
119            }
120        } else if (!username.equals(other.username)) {
121            return false;
122        }
123        return true;
124    }
125
126}