001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.ui.apps.user;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsResource;
032import org.opencms.file.CmsUser;
033import org.opencms.main.CmsException;
034import org.opencms.ui.CmsCssIcon;
035import org.opencms.ui.components.CmsResourceInfo;
036import org.opencms.ui.components.OpenCmsTheme;
037import org.opencms.util.CmsUUID;
038
039import java.util.ArrayList;
040import java.util.HashSet;
041import java.util.List;
042import java.util.Set;
043
044import com.vaadin.v7.data.Item;
045import com.vaadin.v7.data.util.IndexedContainer;
046import com.vaadin.v7.ui.Table;
047
048/**
049 * Table to show resources with CmsResourceInfo elements.<p>
050 */
051public class CmsResourceInfoTable extends Table {
052
053    /**vaadin serial id. */
054    private static final long serialVersionUID = 5999721724863889097L;
055
056    /**Table column. */
057    private static String PROP_ELEMENT = "element";
058
059    /**Indexed container. */
060    IndexedContainer m_container;
061
062    /**
063     * Public constructor.<p>
064     *
065     * @param cms CmsObject
066     * @param userIDs user id
067     * @param groupIDs group id
068     */
069    public CmsResourceInfoTable(CmsObject cms, Set<CmsUUID> userIDs, Set<CmsUUID> groupIDs) {
070
071        List<CmsUser> user = new ArrayList<CmsUser>();
072        try {
073            for (CmsUUID group : groupIDs) {
074                user.addAll(cms.getUsersOfGroup(cms.readGroup(group).getName()));
075            }
076            Set<CmsUUID> principalIDs = new HashSet<CmsUUID>();
077            principalIDs.addAll(userIDs);
078            principalIDs.addAll(groupIDs);
079            Set<CmsResource> resources = new HashSet<CmsResource>();
080            for (CmsUUID id : principalIDs) {
081                resources.addAll(cms.getResourcesForPrincipal(id, null, false));
082            }
083
084            init(resources, user);
085        } catch (CmsException e) {
086            //
087        }
088    }
089
090    /**
091     * public constructor.<p>
092     *
093     * @param resources to be shown
094     * @param user list of user
095     */
096    public CmsResourceInfoTable(Set<CmsResource> resources, List<CmsUser> user) {
097
098        addStyleName("o-no-padding");
099        m_container = new IndexedContainer();
100        m_container.addContainerProperty(PROP_ELEMENT, CmsResourceInfo.class, null);
101
102        for (CmsResource res : resources) {
103            Item item = m_container.addItem(res);
104            item.getItemProperty(PROP_ELEMENT).setValue(new CmsResourceInfo(res));
105        }
106        if (user != null) {
107            for (CmsUser us : user) {
108                Item item = m_container.addItem(us);
109                item.getItemProperty(PROP_ELEMENT).setValue(
110                    new CmsResourceInfo(
111                        us.getSimpleName(),
112                        us.getDescription(),
113                        new CmsCssIcon(OpenCmsTheme.ICON_USER)));
114            }
115        }
116        setColumnHeaderMode(ColumnHeaderMode.HIDDEN);
117        setContainerDataSource(m_container);
118        setVisibleColumns(PROP_ELEMENT);
119
120    }
121
122    /**
123     * Initializes the table.<p>
124     *
125     * @param resources List of resources
126     * @param user List user
127     */
128    private void init(Set<CmsResource> resources, List<CmsUser> user) {
129
130        addStyleName("o-no-padding");
131        m_container = new IndexedContainer();
132        m_container.addContainerProperty(PROP_ELEMENT, CmsResourceInfo.class, null);
133
134        for (CmsResource res : resources) {
135            Item item = m_container.addItem(res);
136            if (item != null) { //Item is null if resource is dependency of multiple groups/user to delete
137                item.getItemProperty(PROP_ELEMENT).setValue(new CmsResourceInfo(res));
138            }
139        }
140        if (user != null) {
141            for (CmsUser us : user) {
142                Item item = m_container.addItem(us);
143                if (item != null) { //Item is null if User is dependency of multiple groups to delete
144                    item.getItemProperty(PROP_ELEMENT).setValue(
145                        new CmsResourceInfo(
146                            us.getSimpleName(),
147                            us.getDescription(),
148                            new CmsCssIcon(OpenCmsTheme.ICON_USER)));
149                }
150            }
151        }
152        setColumnHeaderMode(ColumnHeaderMode.HIDDEN);
153        setContainerDataSource(m_container);
154        setVisibleColumns(PROP_ELEMENT);
155
156    }
157}