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.sessions;
029
030import org.opencms.file.CmsUser;
031import org.opencms.main.CmsException;
032import org.opencms.main.CmsLog;
033import org.opencms.main.CmsSessionInfo;
034import org.opencms.main.OpenCms;
035import org.opencms.ui.A_CmsUI;
036import org.opencms.ui.CmsVaadinUtils;
037import org.opencms.ui.FontOpenCms;
038import org.opencms.ui.apps.A_CmsWorkplaceApp;
039import org.opencms.ui.apps.Messages;
040import org.opencms.ui.apps.user.CmsAccountsApp;
041import org.opencms.ui.components.CmsBasicDialog;
042import org.opencms.ui.components.CmsInfoButton;
043import org.opencms.ui.components.CmsResourceInfo;
044import org.opencms.ui.components.CmsToolBar;
045import org.opencms.util.CmsStringUtil;
046import org.opencms.util.CmsUUID;
047
048import java.util.ArrayList;
049import java.util.Iterator;
050import java.util.LinkedHashMap;
051import java.util.List;
052import java.util.Map;
053import java.util.Set;
054
055import org.apache.commons.logging.Log;
056
057import com.vaadin.ui.Button;
058import com.vaadin.ui.Button.ClickEvent;
059import com.vaadin.ui.Button.ClickListener;
060import com.vaadin.ui.Component;
061import com.vaadin.ui.UI;
062import com.vaadin.ui.Window;
063import com.vaadin.ui.themes.ValoTheme;
064import com.vaadin.v7.data.Validator;
065import com.vaadin.v7.event.FieldEvents.TextChangeEvent;
066import com.vaadin.v7.event.FieldEvents.TextChangeListener;
067import com.vaadin.v7.ui.TextField;
068
069/**
070 * Class for the broadcast app.<p>
071 */
072public class CmsSessionsApp extends A_CmsWorkplaceApp {
073
074    /**
075     * Validator for Message.<p>
076     */
077    static class MessageValidator implements Validator {
078
079        /**vaadin serial id.*/
080        private static final long serialVersionUID = -7720843154577253852L;
081
082        /**
083         * @see com.vaadin.data.Validator#validate(java.lang.Object)
084         */
085        public void validate(Object value) throws InvalidValueException {
086
087            if (value == null) {
088                throw new InvalidValueException(
089                    CmsVaadinUtils.getMessageText(Messages.GUI_MESSAGES_LOGINMESSAGE_VAL_EMPTY_MESSAGE_0));
090            }
091            String message = (String)value;
092            if (message.isEmpty()) {
093                throw new InvalidValueException(
094                    CmsVaadinUtils.getMessageText(Messages.GUI_MESSAGES_LOGINMESSAGE_VAL_EMPTY_MESSAGE_0));
095            }
096        }
097    }
098
099    /** The logger for this class. */
100    static Log LOG = CmsLog.getLog(CmsSessionsApp.class.getName());
101
102    /**Table showing sessions.*/
103    CmsSessionsTable m_table;
104
105    /**Info button. */
106    CmsInfoButton m_infoButton;
107
108    /**
109     * Get resource info boxes for given users.<p>
110     *
111     * @param ids of user to be shown in boxed
112     * @return list of resource infos
113     */
114    protected static List<CmsResourceInfo> getUserInfos(Set<String> ids) {
115
116        List<CmsResourceInfo> ret = new ArrayList<CmsResourceInfo>();
117        try {
118            for (String id : ids) {
119                CmsUser user = A_CmsUI.getCmsObject().readUser(
120                    OpenCms.getSessionManager().getSessionInfo(new CmsUUID(id)).getUserId());
121                ret.add(CmsAccountsApp.getPrincipalInfo(user));
122            }
123        } catch (CmsException e) {
124            LOG.error("Unable to read user", e);
125        }
126        return ret;
127    }
128
129    /**
130     * Get user names as String from set of sessions.<p>
131     *
132     * @param ids to gain usernames from
133     * @param andLocalized String
134     * @return user names as string
135     */
136    protected static String getUserNames(Set<String> ids, String andLocalized) {
137
138        List<String> userNames = new ArrayList<String>();
139
140        for (String id : ids) {
141            CmsSessionInfo session = OpenCms.getSessionManager().getSessionInfo(new CmsUUID(id));
142            try {
143                String name = A_CmsUI.getCmsObject().readUser(session.getUserId()).getName();
144                if (!userNames.contains(name)) {
145                    userNames.add(name);
146                }
147            } catch (CmsException e) {
148                LOG.error("Unable to read user information", e);
149            }
150        }
151
152        Iterator<String> iterator = userNames.iterator();
153
154        String res = "";
155
156        while (iterator.hasNext()) {
157            res += iterator.next();
158            if (iterator.hasNext()) {
159                res += ", ";
160            }
161        }
162        int lastPosSeperation = res.lastIndexOf(", ");
163
164        return lastPosSeperation == -1
165        ? res
166        : res.substring(0, lastPosSeperation)
167            + " "
168            + andLocalized
169            + " "
170            + res.substring(lastPosSeperation + 2, res.length());
171    }
172
173    /**
174     * Shows dialog to send broadcast.<p>
175     *
176     * @param ids of sessions to send broadcast to
177     * @param caption of window
178     * @param table instance of table to be refreshed after sending broadcast
179     */
180    protected static void showSendBroadcastDialog(Set<String> ids, String caption, final CmsSessionsTable table) {
181
182        final Window window = CmsBasicDialog.prepareWindow();
183
184        window.setCaption(caption);
185        CmsBasicDialog dialog = new CmsSendBroadcastDialog(ids, CmsSessionsTable.getCloseRunnable(window, table));
186        window.setContent(dialog);
187        dialog.setWindowMinFullHeight(500);
188        A_CmsUI.get().addWindow(window);
189
190    }
191
192    /**
193    * @see org.opencms.ui.apps.A_CmsWorkplaceApp#getBreadCrumbForState(java.lang.String)
194    */
195    @Override
196    protected LinkedHashMap<String, String> getBreadCrumbForState(String state) {
197
198        LinkedHashMap<String, String> crumbs = new LinkedHashMap<String, String>();
199
200        //Check if state is empty -> start
201        if (CmsStringUtil.isEmptyOrWhitespaceOnly(state)) {
202            crumbs.put("", CmsVaadinUtils.getMessageText(Messages.GUI_MESSAGES_BROADCAST_ADMIN_TOOL_NAME_0));
203            return crumbs;
204        }
205        return new LinkedHashMap<String, String>();
206    }
207
208    /**
209     * @see org.opencms.ui.apps.A_CmsWorkplaceApp#getComponentForState(java.lang.String)
210     */
211    @Override
212    protected Component getComponentForState(String state) {
213
214        addToolbarButtons();
215        addInfoLayoutComponents();
216        m_rootLayout.setMainHeightFull(true);
217        if (state.isEmpty()) {
218            m_table = new CmsSessionsTable();
219            return m_table;
220        }
221
222        return null;
223    }
224
225    /**
226     * Get a map with infos for info button.<p>
227     *
228     * @return map
229     */
230    protected Map<String, String> getInfoMap() {
231
232        Map<String, String> infos = new LinkedHashMap<String, String>();
233        List<CmsSessionInfo> sessions = OpenCms.getSessionManager().getSessionInfos();
234        List<CmsUUID> user = new ArrayList<CmsUUID>();
235        for (CmsSessionInfo info : sessions) {
236            CmsUUID id = info.getUserId();
237            if (!user.contains(id)) {
238                user.add(id);
239            }
240        }
241        infos.put(
242            CmsVaadinUtils.getMessageText(Messages.GUI_MESSAGES_SESSION_COUNT_0),
243            String.valueOf(sessions.size()));
244        infos.put(CmsVaadinUtils.getMessageText(Messages.GUI_MESSAGES_USER_COUNT_0), String.valueOf(user.size()));
245        return infos;
246    }
247
248    /**
249     * @see org.opencms.ui.apps.A_CmsWorkplaceApp#getSubNavEntries(java.lang.String)
250     */
251    @Override
252    protected List<NavEntry> getSubNavEntries(String state) {
253
254        return null;
255    }
256
257    /**
258     * Opens dialog for login message settings.<p>
259     */
260    protected void openEditLoginMessageDialog() {
261
262        Window window = CmsBasicDialog.prepareWindow();
263        CmsBasicDialog dialog = new CmsEditLoginView(window);
264        window.setContent(dialog);
265        window.setCaption(CmsVaadinUtils.getMessageText(Messages.GUI_MESSAGES_LOGINMESSAGE_TOOL_NAME_0));
266        dialog.setWindowMinFullHeight(500);
267        A_CmsUI.get().addWindow(window);
268    }
269
270    /**
271     * Adds info layout components.<p>
272     */
273    private void addInfoLayoutComponents() {
274
275        TextField siteTableFilter = new TextField();
276        siteTableFilter.setIcon(FontOpenCms.FILTER);
277        siteTableFilter.setInputPrompt(
278            Messages.get().getBundle(UI.getCurrent().getLocale()).key(Messages.GUI_EXPLORER_FILTER_0));
279        siteTableFilter.addStyleName(ValoTheme.TEXTFIELD_INLINE_ICON);
280        siteTableFilter.setWidth("200px");
281        siteTableFilter.addTextChangeListener(new TextChangeListener() {
282
283            private static final long serialVersionUID = 1L;
284
285            public void textChange(TextChangeEvent event) {
286
287                m_table.filterTable(event.getText());
288            }
289        });
290        m_infoLayout.addComponent(siteTableFilter);
291
292    }
293
294    /**
295     * Adds Buttons to toolbar.<p>
296     */
297    private void addToolbarButtons() {
298
299        Button add = CmsToolBar.createButton(
300            FontOpenCms.LOGIN,
301            CmsVaadinUtils.getMessageText(Messages.GUI_MESSAGES_LOGINMESSAGE_TOOL_NAME_0));
302        add.addClickListener(new ClickListener() {
303
304            private static final long serialVersionUID = 1L;
305
306            public void buttonClick(ClickEvent event) {
307
308                openEditLoginMessageDialog();
309            }
310        });
311        m_uiContext.addToolbarButton(add);
312
313        Button broadcastToAll = CmsToolBar.createButton(
314            FontOpenCms.BROADCAST,
315            CmsVaadinUtils.getMessageText(Messages.GUI_MESSAGES_BROADCAST_TO_ALL_0));
316        broadcastToAll.addClickListener(new ClickListener() {
317
318            private static final long serialVersionUID = 1L;
319
320            public void buttonClick(ClickEvent event) {
321
322                showSendBroadcastDialog(
323                    null,
324                    CmsVaadinUtils.getMessageText(Messages.GUI_MESSAGES_BROADCAST_TO_ALL_0),
325                    m_table);
326            }
327        });
328        m_uiContext.addToolbarButton(broadcastToAll);
329        m_infoButton = getStatisticButton();
330        m_uiContext.addToolbarButton(m_infoButton);
331        Button button = CmsToolBar.createButton(
332            FontOpenCms.RESET,
333            CmsVaadinUtils.getMessageText(Messages.GUI_MESSAGES_REFRESH_0));
334        button.addClickListener(new ClickListener() {
335
336            private static final long serialVersionUID = 1L;
337
338            public void buttonClick(ClickEvent event) {
339
340                try {
341                    m_table.ini();
342                    m_infoButton.replaceData(getInfoMap());
343                } catch (CmsException e) {
344                    //
345                }
346
347            }
348        });
349        m_uiContext.addToolbarButton(button);
350    }
351
352    /**
353     * Gets label with statistics to user sessions.<p>
354     *
355     * @return vaadin component
356     */
357    private CmsInfoButton getStatisticButton() {
358
359        CmsInfoButton ret = new CmsInfoButton(getInfoMap());
360        ret.setWindowCaption(CmsVaadinUtils.getMessageText(Messages.GUI_MESSAGES_STATISTICS_CAPTION_0));
361        ret.setDescription(CmsVaadinUtils.getMessageText(Messages.GUI_MESSAGES_STATISTICS_CAPTION_0));
362        return ret;
363    }
364}