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