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