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