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.jsp.CmsJspActionElement; 032import org.opencms.main.CmsException; 033import org.opencms.main.CmsLog; 034import org.opencms.main.CmsSessionInfo; 035import org.opencms.main.OpenCms; 036import org.opencms.util.CmsStringUtil; 037import org.opencms.workplace.CmsWidgetDialog; 038import org.opencms.workplace.CmsWorkplaceSettings; 039import org.opencms.workplace.list.CmsHtmlList; 040 041import java.util.ArrayList; 042import java.util.Iterator; 043import java.util.List; 044 045import javax.servlet.http.HttpServletRequest; 046 047import org.apache.commons.logging.Log; 048 049/** 050 * Base dialog to edit a message info object.<p> 051 * 052 * @since 6.0.0 053 */ 054public abstract class A_CmsMessageDialog extends CmsWidgetDialog { 055 056 /** Defines which pages are valid for this dialog. */ 057 public static final String[] PAGES = {"page1"}; 058 059 /** Request parameter name for the list of session ids. */ 060 public static final String PARAM_SESSIONIDS = "sessionids"; 061 062 /** The static log object for this class. */ 063 private static final Log LOG = CmsLog.getLog(A_CmsMessageDialog.class); 064 065 /** Message info object. */ 066 protected CmsMessageInfo m_msgInfo; 067 068 /** Stores the value of the request parameter for the list of session ids. */ 069 private String m_paramSessionids; 070 071 /** 072 * Public constructor with JSP action element.<p> 073 * 074 * @param jsp an initialized JSP action element 075 */ 076 public A_CmsMessageDialog(CmsJspActionElement jsp) { 077 078 super(jsp); 079 } 080 081 /** 082 * Returns the list of session ids parameter value.<p> 083 * 084 * @return the list of session ids parameter value 085 */ 086 public String getParamSessionids() { 087 088 return m_paramSessionids; 089 } 090 091 /** 092 * Sets the list of session ids parameter value.<p> 093 * 094 * @param sessionIds the list of session ids parameter value 095 */ 096 public void setParamSessionids(String sessionIds) { 097 098 m_paramSessionids = sessionIds; 099 } 100 101 /** 102 * @see org.opencms.workplace.CmsWidgetDialog#getPageArray() 103 */ 104 @Override 105 protected String[] getPageArray() { 106 107 return PAGES; 108 } 109 110 /** 111 * Returns a semicolon separated list of user names.<p> 112 * 113 * @return a semicolon separated list of user names 114 */ 115 protected String getToNames() { 116 117 List<String> users = new ArrayList<String>(); 118 Iterator<String> itIds = idsList().iterator(); 119 while (itIds.hasNext()) { 120 String id = itIds.next(); 121 CmsSessionInfo session = OpenCms.getSessionManager().getSessionInfo(id); 122 if (session != null) { 123 try { 124 String userName = getCms().readUser(session.getUserId()).getFullName(); 125 if (!users.contains(userName)) { 126 users.add(userName); 127 } 128 } catch (Exception e) { 129 LOG.error(e.getLocalizedMessage(), e); 130 } 131 } 132 } 133 StringBuffer result = new StringBuffer(256); 134 Iterator<String> itUsers = users.iterator(); 135 while (itUsers.hasNext()) { 136 result.append(itUsers.next()); 137 if (itUsers.hasNext()) { 138 result.append("; "); 139 } 140 } 141 return result.toString(); 142 } 143 144 /** 145 * Returns the list of session ids.<p> 146 * 147 * @return the list of session ids 148 */ 149 protected List<String> idsList() { 150 151 if (!isForAll()) { 152 return CmsStringUtil.splitAsList(getParamSessionids(), CmsHtmlList.ITEM_SEPARATOR); 153 } 154 List<CmsUser> manageableUsers = new ArrayList<CmsUser>(); 155 try { 156 manageableUsers = OpenCms.getRoleManager().getManageableUsers(getCms(), "", true); 157 } catch (CmsException e) { 158 if (LOG.isErrorEnabled()) { 159 LOG.error(e.getLocalizedMessage(), e); 160 } 161 } 162 List<String> ids = new ArrayList<String>(); 163 Iterator<CmsSessionInfo> itSessions = OpenCms.getSessionManager().getSessionInfos().iterator(); 164 while (itSessions.hasNext()) { 165 CmsSessionInfo sessionInfo = itSessions.next(); 166 CmsUser user; 167 try { 168 user = getCms().readUser(sessionInfo.getUserId()); 169 } catch (CmsException e) { 170 if (LOG.isWarnEnabled()) { 171 LOG.warn(e.getLocalizedMessage(), e); 172 } 173 continue; 174 } 175 if (!manageableUsers.contains(user)) { 176 continue; 177 } 178 ids.add(sessionInfo.getSessionId().toString()); 179 } 180 return ids; 181 } 182 183 /** 184 * Initializes the message info object to work with depending on the dialog state and request parameters.<p> 185 */ 186 protected void initMessageObject() { 187 188 Object o = null; 189 190 try { 191 // this is not the initial call, get the message info object from session 192 o = getDialogObject(); 193 m_msgInfo = (CmsMessageInfo)o; 194 // test 195 m_msgInfo.getTo(); 196 } catch (Exception e) { 197 // create a new message info object 198 m_msgInfo = new CmsMessageInfo(); 199 } 200 m_msgInfo.setFrom(getCms().getRequestContext().getCurrentUser().getFullName()); 201 m_msgInfo.setTo(getToNames()); 202 } 203 204 /** 205 * @see org.opencms.workplace.CmsWorkplace#initMessages() 206 */ 207 @Override 208 protected void initMessages() { 209 210 // add specific dialog resource bundle 211 addMessages(Messages.get().getBundleName()); 212 addMessages(org.opencms.workplace.tools.workplace.Messages.get().getBundleName()); 213 // add default resource bundles 214 super.initMessages(); 215 } 216 217 /** 218 * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest) 219 */ 220 @Override 221 protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) { 222 223 // initialize parameters and dialog actions in super implementation 224 super.initWorkplaceRequestValues(settings, request); 225 226 // save the current state of the message (may be changed because of the widget values) 227 setDialogObject(m_msgInfo); 228 } 229 230 /** 231 * Checks if the edited message has to be sent to all sessions.<p> 232 * 233 * @return <code>true</code> if the edited message has to be sent to all sessions 234 */ 235 protected boolean isForAll() { 236 237 return CmsStringUtil.isEmptyOrWhitespaceOnly(getParamSessionids()); 238 } 239}