001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://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: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://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        /**Icon. */
113        Icon(null, Resource.class, new CmsCssIcon(OpenCmsTheme.ICON_ROLE)),
114        /**Name. */
115        Name(CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_USER_NAME_0), String.class, ""),
116        /**Description. */
117        Description(CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_USER_DESCRIPTION_0), String.class, ""),
118        /**OU. */
119        OU(CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_USER_OU_0), String.class, "");
120
121        /**Default value for column.*/
122        private Object m_defaultValue;
123
124        /**Header Message key.*/
125        private String m_headerMessage;
126
127        /**Type of column property.*/
128        private Class<?> m_type;
129
130        /**
131         * constructor.<p>
132         *
133         * @param name Name
134         * @param type type
135         * @param defaultValue value
136         */
137        TableProperty(String name, Class<?> type, Object defaultValue) {
138
139            m_headerMessage = name;
140            m_type = type;
141            m_defaultValue = defaultValue;
142        }
143
144        /**
145         * The default value.<p>
146         *
147         * @return the default value object
148         */
149        Object getDefault() {
150
151            return m_defaultValue;
152        }
153
154        /**
155         * Gets the name of the property.<p>
156         *
157         * @return a name
158         */
159        String getName() {
160
161            return m_headerMessage;
162
163        }
164
165        /**
166         * Gets the type of property.<p>
167         *
168         * @return the type
169         */
170        Class<?> getType() {
171
172            return m_type;
173        }
174
175    }
176
177    /**vaadin serial id.*/
178    private static final long serialVersionUID = 7863356514060544048L;
179
180    /** Log instance for this class. */
181    private static final Log LOG = CmsLog.getLog(CmsUserTable.class);
182
183    /**Indexed container. */
184    private IndexedContainer m_container;
185
186    /**CmsObject.*/
187    CmsObject m_cms;
188
189    /** The context menu. */
190    CmsContextMenu m_menu;
191
192    /**Name of group to show user for, or null. */
193    protected String m_group;
194
195    /** The available menu entries. */
196    private List<I_CmsSimpleContextMenuEntry<Set<String>>> m_menuEntries;
197
198    /**Parent ou. */
199    private String m_parentOU;
200
201    /**AccountsApp instance. */
202    private CmsAccountsApp m_app;
203
204    /**
205     * public constructor.<p>
206     * @param app calling app
207     *
208     * @param ou name
209     */
210    public CmsRoleTable(CmsAccountsApp app, String ou) {
211
212        try {
213            m_app = app;
214            m_parentOU = ou;
215            m_cms = getCmsObject();
216            List<CmsRole> roles = OpenCms.getRoleManager().getRoles(m_cms, ou, false);
217            init(roles);
218        } catch (CmsException e) {
219            LOG.error("Unable to read user.", e);
220        }
221    }
222
223    /**
224     * @see org.opencms.ui.apps.user.I_CmsFilterableTable#filter(java.lang.String)
225     */
226    public void filter(String data) {
227
228        m_container.removeAllContainerFilters();
229        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(data)) {
230            m_container.addContainerFilter(
231                new Or(
232                    new SimpleStringFilter(TableProperty.Name, data, true, false),
233                    new SimpleStringFilter(TableProperty.Description, data, true, false)));
234        }
235
236    }
237
238    /**
239     * @see org.opencms.ui.apps.user.I_CmsFilterableTable#getEmptyLayout()
240     */
241    public VerticalLayout getEmptyLayout() {
242
243        VerticalLayout layout = new VerticalLayout();
244        layout.setVisible(false);
245        return layout;
246    }
247
248    /**
249     * Updates app.<p>
250     * @param role to be set
251     */
252    protected void updateApp(CmsRole role) {
253
254        m_app.update(m_parentOU, CmsOuTreeType.ROLE, role.getId(), "");
255    }
256
257    /**
258     * Returns the available menu entries.<p>
259     *
260     * @return the menu entries
261     */
262    List<I_CmsSimpleContextMenuEntry<Set<String>>> getMenuEntries() {
263
264        if (m_menuEntries == null) {
265            m_menuEntries = new ArrayList<I_CmsSimpleContextMenuEntry<Set<String>>>();
266            m_menuEntries.add(new EntryOpen());
267            //            m_menuEntries.add(new EntryEditRole());
268            //            m_menuEntries.add(new EntryEditGroup());
269            //            m_menuEntries.add(new EntryShowResources());
270            //            m_menuEntries.add(new EntryAddInfos());
271            //            m_menuEntries.add(new EntrySwitchUser());
272            //            m_menuEntries.add(new EntryRemoveFromGroup());
273            //            m_menuEntries.add(new EntryDelete());
274            //            m_menuEntries.add(new EntryKillSession());
275        }
276        return m_menuEntries;
277    }
278
279    /**
280     * Gets CmsObject.<p>
281     *
282     * @return cmsobject
283     */
284    private CmsObject getCmsObject() {
285
286        CmsObject cms;
287        try {
288            cms = OpenCms.initCmsObject(A_CmsUI.getCmsObject());
289            //m_cms.getRequestContext().setSiteRoot("");
290        } catch (CmsException e) {
291            cms = A_CmsUI.getCmsObject();
292        }
293        return cms;
294    }
295
296    /**
297     * initializes table.
298     *
299     * @param roles list of user
300     */
301    private void init(List<CmsRole> roles) {
302
303        CmsRole.applySystemRoleOrder(roles);
304        m_menu = new CmsContextMenu();
305        m_menu.setAsTableContextMenu(this);
306
307        m_container = new IndexedContainer();
308
309        for (TableProperty prop : TableProperty.values()) {
310            m_container.addContainerProperty(prop, prop.getType(), prop.getDefault());
311            setColumnHeader(prop, prop.getName());
312        }
313        setContainerDataSource(m_container);
314        setItemIconPropertyId(TableProperty.Icon);
315        setRowHeaderMode(RowHeaderMode.ICON_ONLY);
316
317        setColumnWidth(null, 40);
318        setSelectable(true);
319
320        setVisibleColumns(TableProperty.Name, TableProperty.OU);
321
322        for (CmsRole role : roles) {
323
324            Item item = m_container.addItem(role);
325            item.getItemProperty(TableProperty.Name).setValue(role.getName(A_CmsUI.get().getLocale()));
326            item.getItemProperty(TableProperty.Description).setValue(role.getDescription(A_CmsUI.get().getLocale()));
327            item.getItemProperty(TableProperty.OU).setValue(role.getOuFqn());
328        }
329
330        addItemClickListener(new ItemClickListener() {
331
332            private static final long serialVersionUID = 4807195510202231174L;
333
334            public void itemClick(ItemClickEvent event) {
335
336                setValue(null);
337                select(event.getItemId());
338                if (event.getButton().equals(MouseButton.RIGHT) || (event.getPropertyId() == null)) {
339                    m_menu.setEntries(
340                        getMenuEntries(),
341                        Collections.singleton(((CmsRole)getValue()).getId().getStringValue()));
342                    m_menu.openForTable(event, event.getItemId(), event.getPropertyId(), CmsRoleTable.this);
343                } else if (event.getButton().equals(MouseButton.LEFT)
344                    && event.getPropertyId().equals(TableProperty.Name)) {
345                    updateApp((CmsRole)getValue());
346                }
347
348            }
349
350        });
351        setCellStyleGenerator(new CellStyleGenerator() {
352
353            private static final long serialVersionUID = 1L;
354
355            public String getStyle(Table source, Object itemId, Object propertyId) {
356
357                if (TableProperty.Name.equals(propertyId)) {
358                    return " " + OpenCmsTheme.HOVER_COLUMN;
359                }
360
361                return "";
362            }
363        });
364        setVisibleColumns(TableProperty.Name, TableProperty.Description, TableProperty.OU);
365    }
366}