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 GmbH & Co. KG, 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.workplace.tools.workplace.broadcast;
029
030import org.opencms.file.CmsUser;
031import org.opencms.i18n.I_CmsMessageBundle;
032import org.opencms.jsp.CmsJspActionElement;
033import org.opencms.main.CmsException;
034import org.opencms.main.CmsRuntimeException;
035import org.opencms.main.CmsSessionInfo;
036import org.opencms.main.OpenCms;
037import org.opencms.security.CmsOrganizationalUnit;
038import org.opencms.util.CmsStringUtil;
039import org.opencms.util.CmsUUID;
040import org.opencms.workplace.CmsDialog;
041import org.opencms.workplace.list.A_CmsListDialog;
042import org.opencms.workplace.list.CmsListColumnAlignEnum;
043import org.opencms.workplace.list.CmsListColumnDefinition;
044import org.opencms.workplace.list.CmsListDateMacroFormatter;
045import org.opencms.workplace.list.CmsListDefaultAction;
046import org.opencms.workplace.list.CmsListDirectAction;
047import org.opencms.workplace.list.CmsListItem;
048import org.opencms.workplace.list.CmsListItemActionIconComparator;
049import org.opencms.workplace.list.CmsListItemDetails;
050import org.opencms.workplace.list.CmsListItemDetailsFormatter;
051import org.opencms.workplace.list.CmsListMetadata;
052import org.opencms.workplace.list.CmsListMultiAction;
053import org.opencms.workplace.list.CmsListOrderEnum;
054import org.opencms.workplace.list.CmsListTimeIntervalFormatter;
055
056import java.io.IOException;
057import java.util.ArrayList;
058import java.util.Date;
059import java.util.HashMap;
060import java.util.Iterator;
061import java.util.List;
062import java.util.Map;
063
064import javax.servlet.ServletException;
065import javax.servlet.http.HttpServletRequest;
066import javax.servlet.http.HttpServletResponse;
067import javax.servlet.jsp.PageContext;
068
069/**
070 * Session list for broadcasting messages.<p>
071 *
072 * @since 6.0.0
073 */
074public class CmsSessionsList extends A_CmsListDialog {
075
076    /** list action id constant. */
077    public static final String LIST_ACTION_MESSAGE = "am";
078
079    /** list action id constant. */
080    public static final String LIST_ACTION_PENDING_DISABLED = "apd";
081
082    /** list action id constant. */
083    public static final String LIST_ACTION_PENDING_ENABLED = "ape";
084
085    /** list column id constant. */
086    public static final String LIST_COLUMN_CREATION = "cc";
087
088    /** list column id constant. */
089    public static final String LIST_COLUMN_INACTIVE = "ci";
090
091    /** list column id constant. */
092    public static final String LIST_COLUMN_MESSAGE = "cm";
093
094    /** list column id constant. */
095    public static final String LIST_COLUMN_ORGUNIT = "cou";
096
097    /** list column id constant. */
098    public static final String LIST_COLUMN_PENDING = "cp";
099
100    /** list column id constant. */
101    public static final String LIST_COLUMN_PROJECT = "cj";
102
103    /** list column id constant. */
104    public static final String LIST_COLUMN_SITE = "cs";
105
106    /** list column id constant. */
107    public static final String LIST_COLUMN_USER = "cu";
108
109    /** list action id constant. */
110    public static final String LIST_DEFACTION_MESSAGE = "dm";
111
112    /** list column id constant. */
113    public static final String LIST_DETAIL_EMAIL = "de";
114
115    /** list id constant. */
116    public static final String LIST_ID = "ls";
117
118    /** list action id constant. */
119    public static final String LIST_MACTION_EMAIL = "me";
120
121    /** list multi action id. */
122    public static final String LIST_MACTION_KILL_SESSION = "LIST_MACTION_KILL_SESSION";
123
124    /** list action id constant. */
125    public static final String LIST_MACTION_MESSAGE = "mm";
126
127    /** Path to the list buttons. */
128    public static final String PATH_BUTTONS = "tools/workplace/buttons/";
129
130    /**
131     * Public constructor.<p>
132     *
133     * @param jsp an initialized JSP action element
134     */
135    public CmsSessionsList(CmsJspActionElement jsp) {
136
137        super(
138            jsp,
139            LIST_ID,
140            Messages.get().container(Messages.GUI_SESSIONS_LIST_NAME_0),
141            LIST_COLUMN_USER,
142            CmsListOrderEnum.ORDER_ASCENDING,
143            LIST_COLUMN_USER);
144    }
145
146    /**
147     * Public constructor with JSP variables.<p>
148     *
149     * @param context the JSP page context
150     * @param req the JSP request
151     * @param res the JSP response
152     */
153    public CmsSessionsList(PageContext context, HttpServletRequest req, HttpServletResponse res) {
154
155        this(new CmsJspActionElement(context, req, res));
156    }
157
158    /**
159     * @see org.opencms.workplace.list.A_CmsListDialog#executeListMultiActions()
160     */
161    @Override
162    public void executeListMultiActions() throws IOException, ServletException {
163
164        Map<String, String[]> params = new HashMap<String, String[]>();
165        params.put(A_CmsMessageDialog.PARAM_SESSIONIDS, new String[] {getParamSelItems()});
166        // set action parameter to initial dialog call
167        params.put(CmsDialog.PARAM_ACTION, new String[] {CmsDialog.DIALOG_INITIAL});
168
169        if (getParamListAction().equals(LIST_MACTION_MESSAGE)) {
170            // execute the send message multiaction
171            // forward to the edit message screen
172            getToolManager().jspForwardTool(this, "/workplace/broadcast/message", params);
173        } else if (getParamListAction().equals(LIST_MACTION_EMAIL)) {
174            // execute the send email multiaction
175            // forward to the edit email screen
176            getToolManager().jspForwardTool(this, "/workplace/broadcast/email", params);
177        } else if (getParamListAction().equals(LIST_MACTION_KILL_SESSION)) {
178            List<String> selectedItems = CmsStringUtil.splitAsList(getParamSelItems(), "|");
179            for (String selectedItem : selectedItems) {
180                try {
181                    OpenCms.getSessionManager().killSession(getCms(), new CmsUUID(selectedItem));
182                } catch (CmsException e) {
183                    throw new CmsRuntimeException(e.getMessageContainer(), e);
184                }
185            }
186        } else {
187            throwListUnsupportedActionException();
188        }
189    }
190
191    /**
192     * @see org.opencms.workplace.list.A_CmsListDialog#executeListSingleActions()
193     */
194    @Override
195    public void executeListSingleActions() throws IOException, ServletException {
196
197        Map<String, String[]> params = new HashMap<String, String[]>();
198        params.put(A_CmsMessageDialog.PARAM_SESSIONIDS, new String[] {getSelectedItem().getId()});
199        // set action parameter to initial dialog call
200        params.put(CmsDialog.PARAM_ACTION, new String[] {CmsDialog.DIALOG_INITIAL});
201
202        if (getParamListAction().equals(LIST_ACTION_MESSAGE) || getParamListAction().equals(LIST_DEFACTION_MESSAGE)) {
203            getToolManager().jspForwardTool(this, "/workplace/broadcast/message", params);
204        } else {
205            throwListUnsupportedActionException();
206        }
207        listSave();
208    }
209
210    /**
211     * @see org.opencms.workplace.list.A_CmsListDialog#fillDetails(java.lang.String)
212     */
213    @Override
214    protected void fillDetails(String detailId) {
215
216        // get content
217        List<CmsListItem> sessions = getList().getAllContent();
218        Iterator<CmsListItem> i = sessions.iterator();
219        while (i.hasNext()) {
220            CmsListItem item = i.next();
221            CmsSessionInfo session = OpenCms.getSessionManager().getSessionInfo(new CmsUUID(item.getId()));
222            StringBuffer html = new StringBuffer(32);
223            if (detailId.equals(LIST_DETAIL_EMAIL)) {
224                // email
225                try {
226                    CmsUser user = getCms().readUser(session.getUserId());
227                    html.append(user.getEmail());
228                } catch (CmsException e) {
229                    // should never happen
230                }
231            } else {
232                continue;
233            }
234            item.set(detailId, html.toString());
235        }
236    }
237
238    /**
239     * @see org.opencms.workplace.list.A_CmsListDialog#getListItems()
240     */
241    @Override
242    protected List<CmsListItem> getListItems() throws CmsException {
243
244        List<CmsListItem> ret = new ArrayList<CmsListItem>();
245        // get content
246        List<CmsSessionInfo> sessionInfos = OpenCms.getSessionManager().getSessionInfos();
247        Iterator<CmsSessionInfo> itSessions = sessionInfos.iterator();
248        List<CmsOrganizationalUnit> manageableOus = OpenCms.getRoleManager().getManageableOrgUnits(
249            getCms(),
250            "",
251            true,
252            false);
253        while (itSessions.hasNext()) {
254            CmsSessionInfo sessionInfo = itSessions.next();
255            CmsListItem item = getList().newItem(sessionInfo.getSessionId().toString());
256            CmsUser user = getCms().readUser(sessionInfo.getUserId());
257            CmsOrganizationalUnit userOu = OpenCms.getOrgUnitManager().readOrganizationalUnit(
258                getCms(),
259                user.getOuFqn());
260            if (!(manageableOus.contains(userOu) && !user.isWebuser())) {
261                continue;
262            }
263            item.set(LIST_COLUMN_USER, user.getFullName());
264            item.set(LIST_COLUMN_ORGUNIT, userOu.getDisplayName(getLocale()));
265            item.set(LIST_COLUMN_CREATION, new Date(sessionInfo.getTimeCreated()));
266            item.set(LIST_COLUMN_INACTIVE, Long.valueOf(System.currentTimeMillis() - sessionInfo.getTimeUpdated()));
267            try {
268                item.set(LIST_COLUMN_PROJECT, getCms().readProject(sessionInfo.getProject()).getName());
269            } catch (Exception e) {
270                // ignore
271            }
272            item.set(LIST_COLUMN_SITE, sessionInfo.getSiteRoot());
273            ret.add(item);
274        }
275
276        // hide ou column if only one ou exists
277        try {
278            if (OpenCms.getOrgUnitManager().getOrganizationalUnits(getCms(), "", true).isEmpty()) {
279                getList().getMetadata().getColumnDefinition(LIST_COLUMN_ORGUNIT).setVisible(false);
280                getList().getMetadata().getColumnDefinition(LIST_COLUMN_USER).setWidth("40%");
281            } else {
282                getList().getMetadata().getColumnDefinition(LIST_COLUMN_ORGUNIT).setVisible(true);
283                getList().getMetadata().getColumnDefinition(LIST_COLUMN_USER).setWidth("20%");
284            }
285        } catch (CmsException e) {
286            // noop
287        }
288
289        return ret;
290    }
291
292    /**
293     * @see org.opencms.workplace.CmsWorkplace#initMessages()
294     */
295    @Override
296    protected void initMessages() {
297
298        // add specific dialog resource bundle
299        addMessages(Messages.get().getBundleName());
300        addMessages(org.opencms.workplace.tools.workplace.Messages.get().getBundleName());
301        // add default resource bundles
302        super.initMessages();
303    }
304
305    /**
306     * @see org.opencms.workplace.list.A_CmsListDialog#setColumns(org.opencms.workplace.list.CmsListMetadata)
307     */
308    @Override
309    protected void setColumns(CmsListMetadata metadata) {
310
311        // create column for send message
312        CmsListColumnDefinition messageCol = new CmsListColumnDefinition(LIST_COLUMN_MESSAGE);
313        messageCol.setName(Messages.get().container(Messages.GUI_SESSIONS_LIST_COLS_MESSAGE_0));
314        messageCol.setHelpText(Messages.get().container(Messages.GUI_SESSIONS_LIST_COLS_MESSAGE_HELP_0));
315        messageCol.setWidth("20");
316        messageCol.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER);
317        messageCol.setSorteable(false);
318        // add send message action
319        CmsListDirectAction messageAction = new CmsListDirectAction(LIST_ACTION_MESSAGE);
320        messageAction.setName(Messages.get().container(Messages.GUI_SESSIONS_LIST_ACTION_MESSAGE_NAME_0));
321        messageAction.setHelpText(Messages.get().container(Messages.GUI_SESSIONS_LIST_ACTION_MESSAGE_HELP_0));
322        messageAction.setIconPath(PATH_BUTTONS + "send_message.png");
323        messageCol.addDirectAction(messageAction);
324        // add it to the list definition
325        metadata.addColumn(messageCol);
326
327        // create column for pending
328        CmsListColumnDefinition pendingCol = new CmsListColumnDefinition(LIST_COLUMN_PENDING);
329        pendingCol.setName(Messages.get().container(Messages.GUI_SESSIONS_LIST_COLS_PENDING_0));
330        pendingCol.setWidth("20");
331        pendingCol.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER);
332        pendingCol.setListItemComparator(new CmsListItemActionIconComparator());
333        // add pending action
334        CmsListDirectAction pendingAction = new CmsListDirectAction(LIST_ACTION_PENDING_ENABLED) {
335
336            /**
337             * @see org.opencms.workplace.tools.A_CmsHtmlIconButton#isVisible()
338             */
339            @Override
340            public boolean isVisible() {
341
342                if (getItem() != null) {
343                    return !OpenCms.getSessionManager().getBroadcastQueue(getItem().getId()).isEmpty();
344                }
345                return super.isVisible();
346            }
347        };
348        pendingAction.setName(Messages.get().container(Messages.GUI_SESSIONS_LIST_ACTION_PENDING_NAME_0));
349        pendingAction.setHelpText(Messages.get().container(Messages.GUI_SESSIONS_LIST_ACTION_PENDING_HELP_0));
350        pendingAction.setIconPath(PATH_BUTTONS + "message_pending.png");
351        pendingAction.setEnabled(false);
352        pendingCol.addDirectAction(pendingAction);
353
354        // not pending action
355        CmsListDirectAction notPendingAction = new CmsListDirectAction(LIST_ACTION_PENDING_DISABLED) {
356
357            /**
358             * @see org.opencms.workplace.tools.A_CmsHtmlIconButton#isVisible()
359             */
360            @Override
361            public boolean isVisible() {
362
363                if (getItem() != null) {
364                    return OpenCms.getSessionManager().getBroadcastQueue(getItem().getId()).isEmpty();
365                }
366                return super.isVisible();
367            }
368        };
369        notPendingAction.setName(Messages.get().container(Messages.GUI_SESSIONS_LIST_ACTION_NOTPENDING_NAME_0));
370        notPendingAction.setHelpText(Messages.get().container(Messages.GUI_SESSIONS_LIST_ACTION_NOTPENDING_HELP_0));
371        notPendingAction.setIconPath(PATH_BUTTONS + "message_notpending.png");
372        notPendingAction.setEnabled(false);
373        pendingCol.addDirectAction(notPendingAction);
374
375        // add it to the list definition
376        metadata.addColumn(pendingCol);
377
378        // create column for user name
379        CmsListColumnDefinition userCol = new CmsListColumnDefinition(LIST_COLUMN_USER);
380        userCol.setName(Messages.get().container(Messages.GUI_SESSIONS_LIST_COLS_USER_0));
381        //userCol.setWidth("20%");
382
383        // create default edit message action
384        CmsListDefaultAction messageEditAction = new CmsListDefaultAction(LIST_DEFACTION_MESSAGE);
385        messageEditAction.setName(Messages.get().container(Messages.GUI_SESSIONS_LIST_ACTION_MESSAGE_NAME_0));
386        messageEditAction.setHelpText(Messages.get().container(Messages.GUI_SESSIONS_LIST_ACTION_MESSAGE_HELP_0));
387        userCol.addDefaultAction(messageEditAction);
388        // add it to the list definition
389        metadata.addColumn(userCol);
390
391        // add column for organizational units
392        CmsListColumnDefinition ouCol = new CmsListColumnDefinition(LIST_COLUMN_ORGUNIT);
393        ouCol.setName(Messages.get().container(Messages.GUI_SESSIONS_LIST_COLS_ORGUNIT_0));
394        ouCol.setWidth("30%");
395        metadata.addColumn(ouCol);
396
397        // add column for creation date
398        CmsListColumnDefinition creationCol = new CmsListColumnDefinition(LIST_COLUMN_CREATION);
399        creationCol.setName(Messages.get().container(Messages.GUI_SESSIONS_LIST_COLS_CREATION_0));
400        creationCol.setWidth("16%");
401        creationCol.setFormatter(CmsListDateMacroFormatter.getDefaultDateFormatter());
402        metadata.addColumn(creationCol);
403
404        // add column for inactive time
405        CmsListColumnDefinition inactiveCol = new CmsListColumnDefinition(LIST_COLUMN_INACTIVE);
406        inactiveCol.setName(Messages.get().container(Messages.GUI_SESSIONS_LIST_COLS_INACTIVE_0));
407        inactiveCol.setWidth("10%");
408        inactiveCol.setFormatter(new CmsListTimeIntervalFormatter());
409        metadata.addColumn(inactiveCol);
410
411        // add column for project
412        CmsListColumnDefinition projectCol = new CmsListColumnDefinition(LIST_COLUMN_PROJECT);
413        projectCol.setName(Messages.get().container(Messages.GUI_SESSIONS_LIST_COLS_PROJECT_0));
414        projectCol.setWidth("12%");
415        metadata.addColumn(projectCol);
416
417        // add column for site
418        CmsListColumnDefinition siteCol = new CmsListColumnDefinition(LIST_COLUMN_SITE);
419        siteCol.setName(Messages.get().container(Messages.GUI_SESSIONS_LIST_COLS_SITE_0));
420        siteCol.setWidth("12%");
421        metadata.addColumn(siteCol);
422    }
423
424    /**
425     * @see org.opencms.workplace.list.A_CmsListDialog#setIndependentActions(org.opencms.workplace.list.CmsListMetadata)
426     */
427    @Override
428    protected void setIndependentActions(CmsListMetadata metadata) {
429
430        // create list item detail
431        CmsListItemDetails emailDetail = new CmsListItemDetails(LIST_DETAIL_EMAIL);
432        emailDetail.setAtColumn(LIST_COLUMN_USER);
433        emailDetail.setVisible(false);
434        emailDetail.setFormatter(
435            new CmsListItemDetailsFormatter(Messages.get().container(Messages.GUI_SESSIONS_LABEL_EMAIL_0)));
436        emailDetail.setShowActionName(Messages.get().container(Messages.GUI_SESSIONS_DETAIL_SHOW_EMAIL_NAME_0));
437        emailDetail.setShowActionHelpText(Messages.get().container(Messages.GUI_SESSIONS_DETAIL_SHOW_EMAIL_HELP_0));
438        emailDetail.setHideActionName(Messages.get().container(Messages.GUI_SESSIONS_DETAIL_HIDE_EMAIL_NAME_0));
439        emailDetail.setHideActionHelpText(Messages.get().container(Messages.GUI_SESSIONS_DETAIL_HIDE_EMAIL_HELP_0));
440        metadata.addItemDetails(emailDetail);
441    }
442
443    /**
444     * @see org.opencms.workplace.list.A_CmsListDialog#setMultiActions(org.opencms.workplace.list.CmsListMetadata)
445     */
446    @Override
447    protected void setMultiActions(CmsListMetadata metadata) {
448
449        // add message multi action
450        CmsListMultiAction messageMultiAction = new CmsListMultiAction(LIST_MACTION_MESSAGE);
451        messageMultiAction.setName(Messages.get().container(Messages.GUI_SESSIONS_LIST_MACTION_MESSAGE_NAME_0));
452        messageMultiAction.setHelpText(Messages.get().container(Messages.GUI_SESSIONS_LIST_MACTION_MESSAGE_HELP_0));
453        messageMultiAction.setConfirmationMessage(
454            Messages.get().container(Messages.GUI_SESSIONS_LIST_MACTION_MESSAGE_CONF_0));
455        messageMultiAction.setIconPath(PATH_BUTTONS + "multi_send_message.png");
456        metadata.addMultiAction(messageMultiAction);
457
458        // add email multi action
459        CmsListMultiAction emailMultiAction = new CmsListMultiAction(LIST_MACTION_EMAIL);
460        emailMultiAction.setName(Messages.get().container(Messages.GUI_SESSIONS_LIST_MACTION_EMAIL_NAME_0));
461        emailMultiAction.setHelpText(Messages.get().container(Messages.GUI_SESSIONS_LIST_MACTION_EMAIL_HELP_0));
462        emailMultiAction.setConfirmationMessage(
463            Messages.get().container(Messages.GUI_SESSIONS_LIST_MACTION_EMAIL_CONF_0));
464        emailMultiAction.setIconPath(PATH_BUTTONS + "multi_send_email.png");
465        metadata.addMultiAction(emailMultiAction);
466
467        CmsListMultiAction killMultiAction = new CmsListMultiAction(LIST_MACTION_KILL_SESSION);
468        I_CmsMessageBundle m = Messages.get();
469        killMultiAction.setName(m.container(Messages.GUI_SESSIONS_LIST_MACTION_KILL_NAME_0));
470        killMultiAction.setHelpText(m.container(Messages.GUI_SESSIONS_LIST_MACTION_KILL_HELP_0));
471        killMultiAction.setIconPath("list/delete.png");
472        metadata.addMultiAction(killMultiAction);
473    }
474
475}