001: /*
002: * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/workplace/tools/workplace/broadcast/A_CmsMessageDialog.java,v $
003: * Date : $Date: 2008-03-17 15:20:20 $
004: * Version: $Revision: 1.10 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software GmbH, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.workplace.tools.workplace.broadcast;
033:
034: import org.opencms.file.CmsUser;
035: import org.opencms.jsp.CmsJspActionElement;
036: import org.opencms.main.CmsException;
037: import org.opencms.main.CmsLog;
038: import org.opencms.main.CmsSessionInfo;
039: import org.opencms.main.OpenCms;
040: import org.opencms.util.CmsStringUtil;
041: import org.opencms.workplace.CmsWidgetDialog;
042: import org.opencms.workplace.CmsWorkplaceSettings;
043: import org.opencms.workplace.list.CmsHtmlList;
044:
045: import java.util.ArrayList;
046: import java.util.Iterator;
047: import java.util.List;
048:
049: import javax.servlet.http.HttpServletRequest;
050:
051: import org.apache.commons.logging.Log;
052:
053: /**
054: * Base dialog to edit a message info object.<p>
055: *
056: * @author Michael Moossen
057: *
058: * @version $Revision: 1.10 $
059: *
060: * @since 6.0.0
061: */
062: public abstract class A_CmsMessageDialog extends CmsWidgetDialog {
063:
064: /** Defines which pages are valid for this dialog. */
065: public static final String[] PAGES = { "page1" };
066:
067: /** Request parameter name for the list of session ids. */
068: public static final String PARAM_SESSIONIDS = "sessionids";
069:
070: /** The static log object for this class. */
071: private static final Log LOG = CmsLog
072: .getLog(A_CmsMessageDialog.class);
073:
074: /** Message info object. */
075: protected CmsMessageInfo m_msgInfo;
076:
077: /** Stores the value of the request parameter for the list of session ids. */
078: private String m_paramSessionids;
079:
080: /**
081: * Public constructor with JSP action element.<p>
082: *
083: * @param jsp an initialized JSP action element
084: */
085: public A_CmsMessageDialog(CmsJspActionElement jsp) {
086:
087: super (jsp);
088: }
089:
090: /**
091: * Returns the list of session ids parameter value.<p>
092: *
093: * @return the list of session ids parameter value
094: */
095: public String getParamSessionids() {
096:
097: return m_paramSessionids;
098: }
099:
100: /**
101: * Sets the list of session ids parameter value.<p>
102: *
103: * @param sessionIds the list of session ids parameter value
104: */
105: public void setParamSessionids(String sessionIds) {
106:
107: m_paramSessionids = sessionIds;
108: }
109:
110: /**
111: * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
112: */
113: protected String[] getPageArray() {
114:
115: return PAGES;
116: }
117:
118: /**
119: * Returns a semicolon separated list of user names.<p>
120: *
121: * @return a semicolon separated list of user names
122: */
123: protected String getToNames() {
124:
125: List users = new ArrayList();
126: Iterator itIds = idsList().iterator();
127: while (itIds.hasNext()) {
128: String id = itIds.next().toString();
129: CmsSessionInfo session = OpenCms.getSessionManager()
130: .getSessionInfo(id);
131: if (session != null) {
132: try {
133: String userName = getCms().readUser(
134: session.getUserId()).getFullName();
135: if (!users.contains(userName)) {
136: users.add(userName);
137: }
138: } catch (Exception e) {
139: LOG.error(e.getLocalizedMessage(), e);
140: }
141: }
142: }
143: StringBuffer result = new StringBuffer(256);
144: Iterator itUsers = users.iterator();
145: while (itUsers.hasNext()) {
146: result.append(itUsers.next().toString());
147: if (itUsers.hasNext()) {
148: result.append("; ");
149: }
150: }
151: return result.toString();
152: }
153:
154: /**
155: * Returns the list of session ids.<p>
156: *
157: * @return the list of session ids
158: */
159: protected List idsList() {
160:
161: if (!isForAll()) {
162: return CmsStringUtil.splitAsList(getParamSessionids(),
163: CmsHtmlList.ITEM_SEPARATOR);
164: }
165: List manageableUsers = new ArrayList();
166: try {
167: manageableUsers = OpenCms.getRoleManager()
168: .getManageableUsers(getCms(), "", true);
169: } catch (CmsException e) {
170: if (LOG.isErrorEnabled()) {
171: LOG.error(e.getLocalizedMessage(), e);
172: }
173: }
174: List ids = new ArrayList();
175: Iterator itSessions = OpenCms.getSessionManager()
176: .getSessionInfos().iterator();
177: while (itSessions.hasNext()) {
178: CmsSessionInfo sessionInfo = (CmsSessionInfo) itSessions
179: .next();
180: CmsUser user;
181: try {
182: user = getCms().readUser(sessionInfo.getUserId());
183: } catch (CmsException e) {
184: if (LOG.isWarnEnabled()) {
185: LOG.warn(e.getLocalizedMessage(), e);
186: }
187: continue;
188: }
189: if (!manageableUsers.contains(user)) {
190: continue;
191: }
192: ids.add(sessionInfo.getSessionId());
193: }
194: return ids;
195: }
196:
197: /**
198: * Initializes the message info object to work with depending on the dialog state and request parameters.<p>
199: */
200: protected void initMessageObject() {
201:
202: Object o = null;
203:
204: try {
205: // this is not the initial call, get the message info object from session
206: o = getDialogObject();
207: m_msgInfo = (CmsMessageInfo) o;
208: // test
209: m_msgInfo.getTo();
210: } catch (Exception e) {
211: // create a new message info object
212: m_msgInfo = new CmsMessageInfo();
213: }
214: m_msgInfo.setFrom(getCms().getRequestContext().currentUser()
215: .getFullName());
216: m_msgInfo.setTo(getToNames());
217: }
218:
219: /**
220: * @see org.opencms.workplace.CmsWorkplace#initMessages()
221: */
222: protected void initMessages() {
223:
224: // add specific dialog resource bundle
225: addMessages(Messages.get().getBundleName());
226: addMessages(org.opencms.workplace.tools.workplace.Messages
227: .get().getBundleName());
228: // add default resource bundles
229: super .initMessages();
230: }
231:
232: /**
233: * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
234: */
235: protected void initWorkplaceRequestValues(
236: CmsWorkplaceSettings settings, HttpServletRequest request) {
237:
238: // initialize parameters and dialog actions in super implementation
239: super .initWorkplaceRequestValues(settings, request);
240:
241: // save the current state of the message (may be changed because of the widget values)
242: setDialogObject(m_msgInfo);
243: }
244:
245: /**
246: * Checks if the edited message has to be sent to all sessions.<p>
247: *
248: * @return <code>true</code> if the edited message has to be sent to all sessions
249: */
250: protected boolean isForAll() {
251:
252: return CmsStringUtil
253: .isEmptyOrWhitespaceOnly(getParamSessionids());
254: }
255: }
|