001: /*
002: * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/workplace/tools/workplace/broadcast/CmsSendPopupGroupsDialog.java,v $
003: * Date : $Date: 2008-03-17 15:20:20 $
004: * Version: $Revision: 1.5 $
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.CmsIllegalStateException;
038: import org.opencms.main.CmsLog;
039: import org.opencms.main.OpenCms;
040: import org.opencms.util.CmsStringUtil;
041: import org.opencms.widgets.CmsDisplayWidget;
042: import org.opencms.widgets.CmsTextareaWidget;
043: import org.opencms.workplace.CmsDialog;
044: import org.opencms.workplace.CmsWidgetDialog;
045: import org.opencms.workplace.CmsWidgetDialogParameter;
046: import org.opencms.workplace.CmsWorkplaceSettings;
047: import org.opencms.workplace.list.CmsHtmlList;
048:
049: import java.util.ArrayList;
050: import java.util.Collections;
051: import java.util.Iterator;
052: import java.util.List;
053:
054: import javax.servlet.http.HttpServletRequest;
055: import javax.servlet.http.HttpServletResponse;
056: import javax.servlet.jsp.PageContext;
057:
058: import org.apache.commons.logging.Log;
059:
060: /**
061: * Dialog to send a new email to the selected groups.<p>
062: *
063: * @author Michael Moossen
064: *
065: * @version $Revision: 1.5 $
066: *
067: * @since 6.5.6
068: */
069: public class CmsSendPopupGroupsDialog extends CmsWidgetDialog {
070:
071: /** localized messages Keys prefix. */
072: public static final String KEY_PREFIX = "message";
073:
074: /** Defines which pages are valid for this dialog. */
075: public static final String[] PAGES = { "page1" };
076:
077: /** Parameter name constant. */
078: public static final String PARAM_GROUPS = "groups";
079:
080: /** The static log object for this class. */
081: private static final Log LOG = CmsLog
082: .getLog(CmsSendPopupGroupsDialog.class);
083:
084: /** Message info object. */
085: protected CmsMessageInfo m_msgInfo;
086:
087: /** The selected groups. */
088: private String m_paramGroups;
089:
090: /** The list of all members of the selected groups. */
091: private List m_users;
092:
093: /**
094: * Public constructor with JSP action element.<p>
095: *
096: * @param jsp an initialized JSP action element
097: */
098: public CmsSendPopupGroupsDialog(CmsJspActionElement jsp) {
099:
100: super (jsp);
101: }
102:
103: /**
104: * Public constructor with JSP variables.<p>
105: *
106: * @param context the JSP page context
107: * @param req the JSP request
108: * @param res the JSP response
109: */
110: public CmsSendPopupGroupsDialog(PageContext context,
111: HttpServletRequest req, HttpServletResponse res) {
112:
113: this (new CmsJspActionElement(context, req, res));
114: }
115:
116: /**
117: * Commits the edited project to the db.<p>
118: */
119: public void actionCommit() {
120:
121: List errors = new ArrayList();
122:
123: if (getUsers().isEmpty()) {
124: setCommitErrors(Collections
125: .singletonList(new CmsIllegalStateException(
126: Messages
127: .get()
128: .container(
129: Messages.ERR_NO_SELECTED_USER_WITH_EMAIL_0))));
130: return;
131: }
132: try {
133: Iterator itUsers = getUsers().iterator();
134: while (itUsers.hasNext()) {
135: CmsUser user = (CmsUser) itUsers.next();
136: OpenCms.getSessionManager().sendBroadcast(
137: getCms().getRequestContext().currentUser(),
138: m_msgInfo.getMsg(), user);
139: }
140: } catch (Throwable t) {
141: errors.add(t);
142: }
143: // set the list of errors to display when saving failed
144: setCommitErrors(errors);
145: }
146:
147: /**
148: * Returns the selected groups.<p>
149: *
150: * @return the selected groups
151: */
152: public List getGroups() {
153:
154: return CmsStringUtil.splitAsList(getParamGroups(),
155: CmsHtmlList.ITEM_SEPARATOR);
156: }
157:
158: /**
159: * Returns the list of selected groups.<p>
160: *
161: * @return the list of selected groups
162: */
163: public String getParamGroups() {
164:
165: return m_paramGroups;
166: }
167:
168: /**
169: * Sets the list of selected groups.<p>
170: *
171: * @param paramGroups the list of selected groups to set
172: */
173: public void setParamGroups(String paramGroups) {
174:
175: m_paramGroups = paramGroups;
176: }
177:
178: /**
179: * @see org.opencms.workplace.CmsWidgetDialog#createDialogHtml(java.lang.String)
180: */
181: protected String createDialogHtml(String dialog) {
182:
183: StringBuffer result = new StringBuffer(1024);
184:
185: result.append(createWidgetTableStart());
186: // show error header once if there were validation errors
187: result.append(createWidgetErrorHeader());
188:
189: if (dialog.equals(PAGES[0])) {
190: // create the widgets for the second dialog page
191: result
192: .append(dialogBlockStart(key(Messages.GUI_MESSAGE_EDITOR_LABEL_HEADER_BLOCK_0)));
193: result.append(createWidgetTableStart());
194: result.append(createDialogRowsHtml(0, 1));
195: result.append(createWidgetTableEnd());
196: result.append(dialogBlockEnd());
197: result
198: .append(dialogBlockStart(key(Messages.GUI_MESSAGE_EDITOR_LABEL_CONTENT_BLOCK_0)));
199: result.append(createWidgetTableStart());
200: result.append(createDialogRowsHtml(2, 2));
201: result.append(createWidgetTableEnd());
202: result.append(dialogBlockEnd());
203: }
204:
205: result.append(createWidgetTableEnd());
206: return result.toString();
207: }
208:
209: /**
210: * Creates the list of widgets for this dialog.<p>
211: */
212: protected void defineWidgets() {
213:
214: // initialize the project object to use for the dialog
215: initMessageObject();
216:
217: setKeyPrefix(KEY_PREFIX);
218:
219: addWidget(new CmsWidgetDialogParameter(m_msgInfo, "from",
220: PAGES[0], new CmsDisplayWidget()));
221: addWidget(new CmsWidgetDialogParameter(m_msgInfo, "to",
222: PAGES[0], new CmsDisplayWidget()));
223: addWidget(new CmsWidgetDialogParameter(m_msgInfo, "msg",
224: PAGES[0], new CmsTextareaWidget(12)));
225: }
226:
227: /**
228: * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
229: */
230: protected String[] getPageArray() {
231:
232: return PAGES;
233: }
234:
235: /**
236: * Returns a semicolon separated list of user names.<p>
237: *
238: * @return a semicolon separated list of user names
239: */
240: protected String getToNames() {
241:
242: StringBuffer result = new StringBuffer(256);
243: Iterator itUsers = getUsers().iterator();
244: while (itUsers.hasNext()) {
245: CmsUser user = (CmsUser) itUsers.next();
246: result.append(user.getFullName());
247: if (itUsers.hasNext()) {
248: result.append("; ");
249: }
250: }
251: return result.toString();
252: }
253:
254: /**
255: * Initializes the message info object to work with depending on the dialog state and request parameters.<p>
256: */
257: protected void initMessageObject() {
258:
259: try {
260: if (CmsStringUtil.isEmpty(getParamAction())
261: || CmsDialog.DIALOG_INITIAL
262: .equals(getParamAction())) {
263: // create a new message info object
264: m_msgInfo = new CmsMessageInfo();
265: } else {
266: // this is not the initial call, get the message info object from session
267: m_msgInfo = (CmsMessageInfo) getDialogObject();
268: }
269: } catch (Exception e) {
270: // create a new message info object
271: m_msgInfo = new CmsMessageInfo();
272: }
273: m_msgInfo.setFrom(getCms().getRequestContext().currentUser()
274: .getFullName());
275: m_msgInfo.setTo(getToNames());
276: }
277:
278: /**
279: * @see org.opencms.workplace.CmsWorkplace#initMessages()
280: */
281: protected void initMessages() {
282:
283: // add specific dialog resource bundle
284: addMessages(Messages.get().getBundleName());
285: addMessages(org.opencms.workplace.tools.workplace.Messages
286: .get().getBundleName());
287: // add default resource bundles
288: super .initMessages();
289: }
290:
291: /**
292: * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
293: */
294: protected void initWorkplaceRequestValues(
295: CmsWorkplaceSettings settings, HttpServletRequest request) {
296:
297: // initialize parameters and dialog actions in super implementation
298: super .initWorkplaceRequestValues(settings, request);
299:
300: // save the current state of the message (may be changed because of the widget values)
301: setDialogObject(m_msgInfo);
302: }
303:
304: /**
305: * @see org.opencms.workplace.CmsWidgetDialog#validateParamaters()
306: */
307: protected void validateParamaters() throws Exception {
308:
309: if ((getUsers() == null) || getUsers().isEmpty()) {
310: throw new Exception();
311: }
312: }
313:
314: /**
315: * Returns a list of all members of the selected groups.<p>
316: *
317: * @return a list of user objects
318: */
319: private List getUsers() {
320:
321: if (m_users == null) {
322: m_users = new ArrayList();
323: List manageableUsers = new ArrayList();
324: try {
325: manageableUsers = OpenCms.getRoleManager()
326: .getManageableUsers(getCms(), "", true);
327: } catch (CmsException e) {
328: if (LOG.isErrorEnabled()) {
329: LOG.error(e.getLocalizedMessage(), e);
330: }
331: }
332: Iterator itGroups = getGroups().iterator();
333: while (itGroups.hasNext()) {
334: String groupName = (String) itGroups.next();
335: try {
336: Iterator itUsers = getCms().getUsersOfGroup(
337: groupName, true).iterator();
338: while (itUsers.hasNext()) {
339: CmsUser user = (CmsUser) itUsers.next();
340: if (OpenCms.getSessionManager()
341: .getSessionInfos(user.getId())
342: .isEmpty()) {
343: continue;
344: }
345: if (!manageableUsers.contains(user)) {
346: continue;
347: }
348: m_users.add(user);
349: }
350: } catch (CmsException e) {
351: // should never happen
352: }
353: }
354: }
355: return m_users;
356: }
357: }
|