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