001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Nuxeo - initial API and implementation
011 *
012 * $Id: PermissionUIItemDescriptor.java 28609 2008-01-09 16:38:30Z sfermigier $
013 */
014
015package org.nuxeo.ecm.core.security;
016
017import java.io.Serializable;
018
019import org.nuxeo.common.xmap.annotation.XContent;
020import org.nuxeo.common.xmap.annotation.XNode;
021import org.nuxeo.common.xmap.annotation.XObject;
022import org.nuxeo.ecm.core.api.NuxeoException;
023
024@XObject("item")
025public class PermissionUIItemDescriptor implements Serializable {
026
027    private static final long serialVersionUID = 1L;
028
029    @XNode("@show")
030    private Boolean show;
031
032    @XNode("@order")
033    private Integer order;
034
035    @XNode("@denyPermission")
036    private String denyPermission;
037
038    @XNode("@id")
039    private String id;
040
041    private String permission = "";
042
043    @XContent
044    protected void setPermission(String permission) {
045        this.permission = permission.trim();
046    }
047
048    public PermissionUIItemDescriptor() {
049    }
050
051    public PermissionUIItemDescriptor(PermissionUIItemDescriptor referenceDescriptor) {
052        show = referenceDescriptor.show;
053        order = referenceDescriptor.order;
054        permission = referenceDescriptor.permission;
055        denyPermission = referenceDescriptor.denyPermission;
056        id = referenceDescriptor.id;
057    }
058
059    public int getOrder() {
060        if (order == null) {
061            // default order
062            return 0;
063        } else {
064            return order;
065        }
066    }
067
068    public boolean isShown() {
069        if (show == null) {
070            // permission items are shown by default
071            return true;
072        } else {
073            return show;
074        }
075    }
076
077    public String getPermission() {
078        return permission;
079    }
080
081    public String getDenyPermission() {
082        if (denyPermission != null) {
083            return denyPermission;
084        } else {
085            return permission;
086        }
087    }
088
089    public String getId() {
090        if (id != null) {
091            return id;
092        } else {
093            return permission;
094        }
095    }
096
097    @Override
098    public boolean equals(Object other) {
099        if (other instanceof PermissionUIItemDescriptor) {
100            PermissionUIItemDescriptor otherPid = (PermissionUIItemDescriptor) other;
101            if (!permission.equals(otherPid.permission)) {
102                return false;
103            }
104            if (show != null) {
105                if (!show.equals(otherPid.show)) {
106                    return false;
107                }
108            } else {
109                if (otherPid.show != null) {
110                    return false;
111                }
112            }
113            if (order != null) {
114                if (!order.equals(otherPid.order)) {
115                    return false;
116                }
117            } else {
118                if (otherPid.order != null) {
119                    return false;
120                }
121            }
122            if (getId() != null) {
123                if (!getId().equals(otherPid.getId())) {
124                    return false;
125                }
126            } else {
127                if (otherPid.getId() != null) {
128                    return false;
129                }
130            }
131            if (getDenyPermission() != null) {
132                if (!getDenyPermission().equals(otherPid.getDenyPermission())) {
133                    return false;
134                }
135            } else {
136                if (otherPid.getDenyPermission() != null) {
137                    return false;
138                }
139            }
140
141            return true;
142        }
143        return false;
144    }
145
146    public void merge(PermissionUIItemDescriptor pid) {
147        // sanity check
148        if (!permission.equals(pid.permission)) {
149            throw new NuxeoException(String.format("cannot merge permission item '%s' with '%s'", permission,
150                    pid.permission));
151        }
152        // do not merge unset attributes
153        show = pid.show != null ? pid.show : show;
154        order = pid.order != null ? pid.order : order;
155        id = pid.id != null ? pid.id : id;
156        denyPermission = pid.denyPermission != null ? pid.denyPermission : denyPermission;
157    }
158
159    @Override
160    public String toString() {
161        if (denyPermission != null) {
162            return String.format("PermissionUIItemDescriptor[%s (deny %s)]", permission, denyPermission);
163        } else {
164            return String.format("PermissionUIItemDescriptor[%s]", permission);
165        }
166    }
167
168}