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.main.CmsException;
032import org.opencms.main.CmsLog;
033import org.opencms.main.OpenCms;
034import org.opencms.security.CmsRole;
035import org.opencms.ui.A_CmsUI;
036import org.opencms.ui.CmsCssIcon;
037import org.opencms.ui.CmsVaadinUtils;
038import org.opencms.ui.apps.Messages;
039import org.opencms.ui.components.OpenCmsTheme;
040import org.opencms.ui.contextmenu.CmsContextMenu;
041import org.opencms.ui.contextmenu.CmsMenuItemVisibilityMode;
042import org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry;
043import org.opencms.util.CmsStringUtil;
044import org.opencms.util.CmsUUID;
045
046import java.util.ArrayList;
047import java.util.Collections;
048import java.util.List;
049import java.util.Locale;
050import java.util.Set;
051
052import org.apache.commons.logging.Log;
053
054import com.vaadin.server.Resource;
055import com.vaadin.shared.MouseEventDetails.MouseButton;
056import com.vaadin.ui.themes.ValoTheme;
057import com.vaadin.v7.data.Item;
058import com.vaadin.v7.data.util.IndexedContainer;
059import com.vaadin.v7.data.util.filter.Or;
060import com.vaadin.v7.data.util.filter.SimpleStringFilter;
061import com.vaadin.v7.event.ItemClickEvent;
062import com.vaadin.v7.event.ItemClickEvent.ItemClickListener;
063import com.vaadin.v7.ui.Table;
064import com.vaadin.v7.ui.VerticalLayout;
065
066/**
067 * Table for the roles.<p>
068 */
069public class CmsRoleTable extends Table implements I_CmsFilterableTable {
070
071    /**
072     *Entry to addition info dialog.<p>
073     */
074    class EntryOpen implements I_CmsSimpleContextMenuEntry<Set<String>>, I_CmsSimpleContextMenuEntry.I_HasCssStyles {
075
076        /**
077         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#executeAction(java.lang.Object)
078         */
079        public void executeAction(Set<String> context) {
080
081            updateApp(CmsRole.valueOfId(new CmsUUID(context.iterator().next())));
082        }
083
084        /**
085         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry.I_HasCssStyles#getStyles()
086         */
087        public String getStyles() {
088
089            return ValoTheme.LABEL_BOLD;
090        }
091
092        /**
093         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getTitle(java.util.Locale)
094         */
095        public String getTitle(Locale locale) {
096
097            return CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_OPEN_0);
098        }
099
100        /**
101         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getVisibility(java.lang.Object)
102         */
103        public CmsMenuItemVisibilityMode getVisibility(Set<String> context) {
104
105            return CmsMenuItemVisibilityMode.VISIBILITY_ACTIVE;
106        }
107
108    }
109
110    /**Table properties. */
111    enum TableProperty {
112
113        /**Icon. */
114        Icon(null, Resource.class, new CmsCssIcon(OpenCmsTheme.ICON_ROLE)),
115        /**Name. */
116        Name(CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_USER_NAME_0), String.class, ""),
117        /**Description. */
118        Description(CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_USER_DESCRIPTION_0), String.class, ""),
119        /**OU. */
120        OU(CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_USER_OU_0), String.class, "");
121
122        /**Default value for column.*/
123        private Object m_defaultValue;
124
125        /**Header Message key.*/
126        private String m_headerMessage;
127
128        /**Type of column property.*/
129        private Class<?> m_type;
130
131        /**
132         * constructor.<p>
133         *
134         * @param name Name
135         * @param type type
136         * @param defaultValue value
137         */
138        TableProperty(String name, Class<?> type, Object defaultValue) {
139
140            m_headerMessage = name;
141            m_type = type;
142            m_defaultValue = defaultValue;
143        }
144
145        /**
146         * The default value.<p>
147         *
148         * @return the default value object
149         */
150        Object getDefault() {
151
152            return m_defaultValue;
153        }
154
155        /**
156         * Gets the name of the property.<p>
157         *
158         * @return a name
159         */
160        String getName() {
161
162            return m_headerMessage;
163
164        }
165
166        /**
167         * Gets the type of property.<p>
168         *
169         * @return the type
170         */
171        Class<?> getType() {
172
173            return m_type;
174        }
175
176    }
177
178    /**vaadin serial id.*/
179    private static final long serialVersionUID = 7863356514060544048L;
180
181    /** Log instance for this class. */
182    private static final Log LOG = CmsLog.getLog(CmsUserTable.class);
183
184    /**Indexed container. */
185    private IndexedContainer m_container;
186
187    /**CmsObject.*/
188    CmsObject m_cms;
189
190    /** The context menu. */
191    CmsContextMenu m_menu;
192
193    /**Name of group to show user for, or null. */
194    protected String m_group;
195
196    /** The available menu entries. */
197    private List<I_CmsSimpleContextMenuEntry<Set<String>>> m_menuEntries;
198
199    /**Parent ou. */
200    private String m_parentOU;
201
202    /**AccountsApp instance. */
203    private CmsAccountsApp m_app;
204
205    /**
206     * public constructor.<p>
207     * @param app calling app
208     *
209     * @param ou name
210     */
211    public CmsRoleTable(CmsAccountsApp app, String ou) {
212
213        try {
214            m_app = app;
215            m_parentOU = ou;
216            m_cms = getCmsObject();
217            List<CmsRole> roles = OpenCms.getRoleManager().getRoles(m_cms, ou, false);
218            init(roles);
219        } catch (CmsException e) {
220            LOG.error("Unable to read user.", e);
221        }
222    }
223
224    /**
225     * @see org.opencms.ui.apps.user.I_CmsFilterableTable#filter(java.lang.String)
226     */
227    public void filter(String data) {
228
229        m_container.removeAllContainerFilters();
230        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(data)) {
231            m_container.addContainerFilter(
232                new Or(
233                    new SimpleStringFilter(TableProperty.Name, data, true, false),
234                    new SimpleStringFilter(TableProperty.Description, data, true, false)));
235        }
236
237    }
238
239    /**
240     * @see org.opencms.ui.apps.user.I_CmsFilterableTable#getEmptyLayout()
241     */
242    public VerticalLayout getEmptyLayout() {
243
244        VerticalLayout layout = new VerticalLayout();
245        layout.setVisible(false);
246        return layout;
247    }
248
249    /**
250     * Updates app.<p>
251     * @param role to be set
252     */
253    protected void updateApp(CmsRole role) {
254
255        m_app.update(m_parentOU, CmsOuTreeType.ROLE, role.getId(), "");
256    }
257
258    /**
259     * Returns the available menu entries.<p>
260     *
261     * @return the menu entries
262     */
263    List<I_CmsSimpleContextMenuEntry<Set<String>>> getMenuEntries() {
264
265        if (m_menuEntries == null) {
266            m_menuEntries = new ArrayList<I_CmsSimpleContextMenuEntry<Set<String>>>();
267            m_menuEntries.add(new EntryOpen());
268            //            m_menuEntries.add(new EntryEditRole());
269            //            m_menuEntries.add(new EntryEditGroup());
270            //            m_menuEntries.add(new EntryShowResources());
271            //            m_menuEntries.add(new EntryAddInfos());
272            //            m_menuEntries.add(new EntrySwitchUser());
273            //            m_menuEntries.add(new EntryRemoveFromGroup());
274            //            m_menuEntries.add(new EntryDelete());
275            //            m_menuEntries.add(new EntryKillSession());
276        }
277        return m_menuEntries;
278    }
279
280    /**
281     * Gets CmsObject.<p>
282     *
283     * @return cmsobject
284     */
285    private CmsObject getCmsObject() {
286
287        CmsObject cms;
288        try {
289            cms = OpenCms.initCmsObject(A_CmsUI.getCmsObject());
290            //m_cms.getRequestContext().setSiteRoot("");
291        } catch (CmsException e) {
292            cms = A_CmsUI.getCmsObject();
293        }
294        return cms;
295    }
296
297    /**
298     * initializes table.
299     *
300     * @param roles list of user
301     */
302    private void init(List<CmsRole> roles) {
303
304        CmsRole.applySystemRoleOrder(roles);
305        m_menu = new CmsContextMenu();
306        m_menu.setAsTableContextMenu(this);
307
308        m_container = new IndexedContainer();
309
310        for (TableProperty prop : TableProperty.values()) {
311            m_container.addContainerProperty(prop, prop.getType(), prop.getDefault());
312            setColumnHeader(prop, prop.getName());
313        }
314        setContainerDataSource(m_container);
315        setItemIconPropertyId(TableProperty.Icon);
316        setRowHeaderMode(RowHeaderMode.ICON_ONLY);
317
318        setColumnWidth(null, 40);
319        setSelectable(true);
320
321        setVisibleColumns(TableProperty.Name, TableProperty.OU);
322
323        for (CmsRole role : roles) {
324
325            Item item = m_container.addItem(role);
326            item.getItemProperty(TableProperty.Name).setValue(role.getName(A_CmsUI.get().getLocale()));
327            item.getItemProperty(TableProperty.Description).setValue(role.getDescription(A_CmsUI.get().getLocale()));
328            item.getItemProperty(TableProperty.OU).setValue(role.getOuFqn());
329        }
330
331        addItemClickListener(new ItemClickListener() {
332
333            private static final long serialVersionUID = 4807195510202231174L;
334
335            public void itemClick(ItemClickEvent event) {
336
337                setValue(null);
338                select(event.getItemId());
339                if (event.getButton().equals(MouseButton.RIGHT) || (event.getPropertyId() == null)) {
340                    m_menu.setEntries(
341                        getMenuEntries(),
342                        Collections.singleton(((CmsRole)getValue()).getId().getStringValue()));
343                    m_menu.openForTable(event, event.getItemId(), event.getPropertyId(), CmsRoleTable.this);
344                } else if (event.getButton().equals(MouseButton.LEFT)
345                    && event.getPropertyId().equals(TableProperty.Name)) {
346                    updateApp((CmsRole)getValue());
347                }
348
349            }
350
351        });
352        setCellStyleGenerator(new CellStyleGenerator() {
353
354            private static final long serialVersionUID = 1L;
355
356            public String getStyle(Table source, Object itemId, Object propertyId) {
357
358                if (TableProperty.Name.equals(propertyId)) {
359                    return " " + OpenCmsTheme.HOVER_COLUMN;
360                }
361
362                return "";
363            }
364        });
365        setVisibleColumns(TableProperty.Name, TableProperty.Description, TableProperty.OU);
366    }
367}