001/*
002 * (C) Copyright 2006-2011 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: PermissionUIItemDescriptor.java 28609 2008-01-09 16:38:30Z sfermigier $
020 */
021
022package org.nuxeo.ecm.core.security;
023
024import java.io.Serializable;
025
026import org.nuxeo.common.xmap.annotation.XContent;
027import org.nuxeo.common.xmap.annotation.XNode;
028import org.nuxeo.common.xmap.annotation.XObject;
029import org.nuxeo.ecm.core.api.NuxeoException;
030
031@XObject("item")
032public class PermissionUIItemDescriptor implements Serializable {
033
034    private static final long serialVersionUID = 1L;
035
036    @XNode("@show")
037    private Boolean show;
038
039    @XNode("@order")
040    private Integer order;
041
042    @XNode("@denyPermission")
043    private String denyPermission;
044
045    @XNode("@id")
046    private String id;
047
048    private String permission = "";
049
050    @XContent
051    protected void setPermission(String permission) {
052        this.permission = permission.trim();
053    }
054
055    public PermissionUIItemDescriptor() {
056    }
057
058    public PermissionUIItemDescriptor(PermissionUIItemDescriptor referenceDescriptor) {
059        show = referenceDescriptor.show;
060        order = referenceDescriptor.order;
061        permission = referenceDescriptor.permission;
062        denyPermission = referenceDescriptor.denyPermission;
063        id = referenceDescriptor.id;
064    }
065
066    public int getOrder() {
067        if (order == null) {
068            // default order
069            return 0;
070        } else {
071            return order;
072        }
073    }
074
075    public boolean isShown() {
076        if (show == null) {
077            // permission items are shown by default
078            return true;
079        } else {
080            return show;
081        }
082    }
083
084    public String getPermission() {
085        return permission;
086    }
087
088    public String getDenyPermission() {
089        if (denyPermission != null) {
090            return denyPermission;
091        } else {
092            return permission;
093        }
094    }
095
096    public String getId() {
097        if (id != null) {
098            return id;
099        } else {
100            return permission;
101        }
102    }
103
104    @Override
105    public boolean equals(Object other) {
106        if (other instanceof PermissionUIItemDescriptor) {
107            PermissionUIItemDescriptor otherPid = (PermissionUIItemDescriptor) other;
108            if (!permission.equals(otherPid.permission)) {
109                return false;
110            }
111            if (show != null) {
112                if (!show.equals(otherPid.show)) {
113                    return false;
114                }
115            } else {
116                if (otherPid.show != null) {
117                    return false;
118                }
119            }
120            if (order != null) {
121                if (!order.equals(otherPid.order)) {
122                    return false;
123                }
124            } else {
125                if (otherPid.order != null) {
126                    return false;
127                }
128            }
129            if (getId() != null) {
130                if (!getId().equals(otherPid.getId())) {
131                    return false;
132                }
133            } else {
134                if (otherPid.getId() != null) {
135                    return false;
136                }
137            }
138            if (getDenyPermission() != null) {
139                if (!getDenyPermission().equals(otherPid.getDenyPermission())) {
140                    return false;
141                }
142            } else {
143                if (otherPid.getDenyPermission() != null) {
144                    return false;
145                }
146            }
147
148            return true;
149        }
150        return false;
151    }
152
153    public void merge(PermissionUIItemDescriptor pid) {
154        // sanity check
155        if (!permission.equals(pid.permission)) {
156            throw new NuxeoException(String.format("cannot merge permission item '%s' with '%s'", permission,
157                    pid.permission));
158        }
159        // do not merge unset attributes
160        show = pid.show != null ? pid.show : show;
161        order = pid.order != null ? pid.order : order;
162        id = pid.id != null ? pid.id : id;
163        denyPermission = pid.denyPermission != null ? pid.denyPermission : denyPermission;
164    }
165
166    @Override
167    public String toString() {
168        if (denyPermission != null) {
169            return String.format("PermissionUIItemDescriptor[%s (deny %s)]", permission, denyPermission);
170        } else {
171            return String.format("PermissionUIItemDescriptor[%s]", permission);
172        }
173    }
174
175}