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: PermissionVisibilityDescriptor.java 28609 2008-01-09 16:38:30Z sfermigier $
020 */
021
022package org.nuxeo.ecm.core.security;
023
024import java.util.ArrayList;
025import java.util.Arrays;
026import java.util.Collections;
027import java.util.LinkedList;
028import java.util.List;
029
030import org.nuxeo.common.xmap.annotation.XNode;
031import org.nuxeo.common.xmap.annotation.XNodeList;
032import org.nuxeo.common.xmap.annotation.XObject;
033import org.nuxeo.ecm.core.api.security.UserVisiblePermission;
034
035@XObject("visibility")
036public class PermissionVisibilityDescriptor {
037
038    @XNode("@type")
039    private String typeName = "";
040
041    private final List<PermissionUIItemDescriptor> items = new ArrayList<PermissionUIItemDescriptor>();
042
043    private String[] sortedPermissionNames;
044
045    public PermissionVisibilityDescriptor() {
046    }
047
048    public PermissionVisibilityDescriptor(PermissionVisibilityDescriptor pvd) {
049        typeName = pvd.typeName;
050        for (PermissionUIItemDescriptor pid : pvd.items) {
051            items.add(new PermissionUIItemDescriptor(pid));
052        }
053    }
054
055    @XNodeList(value = "item", type = PermissionUIItemDescriptor[].class, componentType = PermissionUIItemDescriptor.class)
056    protected void setPermissionUIItems(PermissionUIItemDescriptor[] items) {
057        this.items.clear();
058        this.items.addAll(Arrays.asList(items));
059        sortedPermissionNames = null;
060    }
061
062    public String getTypeName() {
063        return typeName;
064    }
065
066    public List<PermissionUIItemDescriptor> getPermissionUIItems() {
067        return items;
068    }
069
070    public void merge(PermissionVisibilityDescriptor other) {
071        List<PermissionUIItemDescriptor> otherItems = new ArrayList<PermissionUIItemDescriptor>(other.items);
072        List<PermissionUIItemDescriptor> mergedItems = new LinkedList<PermissionUIItemDescriptor>();
073
074        // merge items with common permission names
075        for (PermissionUIItemDescriptor item : items) {
076            for (PermissionUIItemDescriptor otherItem : otherItems) {
077                if (item.getPermission().equals(otherItem.getPermission())) {
078                    item.merge(otherItem);
079                    mergedItems.add(otherItem);
080                }
081            }
082            otherItems.removeAll(mergedItems);
083            mergedItems.clear();
084        }
085        // add items for new permission names
086        items.addAll(otherItems);
087        sortedPermissionNames = null;
088    }
089
090    public String[] getSortedItems() {
091        if (sortedPermissionNames == null) {
092            Collections.sort(items, new PermissionUIItemComparator());
093            List<String> filteredPermissions = new LinkedList<String>();
094            for (PermissionUIItemDescriptor pid : items) {
095                if (pid.isShown()) {
096                    filteredPermissions.add(pid.getPermission());
097                }
098            }
099            sortedPermissionNames = filteredPermissions.toArray(new String[filteredPermissions.size()]);
100        }
101        return sortedPermissionNames;
102    }
103
104    public List<UserVisiblePermission> getSortedUIPermissionDescriptor() {
105        Collections.sort(items, new PermissionUIItemComparator());
106        List<UserVisiblePermission> result = new ArrayList<UserVisiblePermission>();
107        for (PermissionUIItemDescriptor pid : items) {
108            if (pid.isShown()) {
109                result.add(new UserVisiblePermission(pid.getId(), pid.getPermission(), pid.getDenyPermission()));
110            }
111        }
112        return result;
113    }
114
115    @Override
116    public boolean equals(Object other) {
117        if (other instanceof PermissionVisibilityDescriptor) {
118            PermissionVisibilityDescriptor otherPvd = (PermissionVisibilityDescriptor) other;
119            if (!typeName.equals(otherPvd.typeName)) {
120                return false;
121            }
122            if (!items.equals(otherPvd.items)) {
123                return false;
124            }
125            return true;
126        }
127        return false;
128    }
129
130    @Override
131    public String toString() {
132        return String.format("PermissionVisibilityDescriptor[%s]", typeName);
133    }
134
135}