001: /*
002: * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/workplace/tools/accounts/CmsEditUserAddInfoDialog.java,v $
003: * Date : $Date: 2008-02-27 12:05:25 $
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.accounts;
033:
034: import org.opencms.file.CmsUser;
035: import org.opencms.jsp.CmsJspActionElement;
036: import org.opencms.main.CmsException;
037: import org.opencms.main.OpenCms;
038: import org.opencms.security.CmsRole;
039: import org.opencms.util.CmsDataTypeUtil;
040: import org.opencms.util.CmsStringUtil;
041: import org.opencms.util.CmsUUID;
042: import org.opencms.widgets.CmsDisplayWidget;
043: import org.opencms.widgets.CmsInputWidget;
044: import org.opencms.widgets.I_CmsWidget;
045: import org.opencms.workplace.CmsDialog;
046: import org.opencms.workplace.CmsWidgetDialog;
047: import org.opencms.workplace.CmsWidgetDialogParameter;
048: import org.opencms.workplace.CmsWorkplaceSettings;
049: import org.opencms.workplace.CmsWorkplaceUserInfoBlock;
050: import org.opencms.workplace.CmsWorkplaceUserInfoEntry;
051: import org.opencms.workplace.CmsWorkplaceUserInfoManager;
052: import org.opencms.workplace.tools.CmsToolManager;
053:
054: import java.util.ArrayList;
055: import java.util.HashMap;
056: import java.util.Iterator;
057: import java.util.List;
058: import java.util.Map;
059: import java.util.SortedMap;
060: import java.util.TreeMap;
061:
062: import javax.servlet.http.HttpServletRequest;
063: import javax.servlet.http.HttpServletResponse;
064: import javax.servlet.jsp.PageContext;
065:
066: /**
067: * Dialog to edit the users additional info in the administration view.<p>
068: *
069: * @author Michael Moossen
070: *
071: * @version $Revision: 1.5 $
072: *
073: * @since 6.5.6
074: */
075: public class CmsEditUserAddInfoDialog extends CmsWidgetDialog {
076:
077: /** localized messages Keys prefix. */
078: public static final String KEY_PREFIX = "userinfo";
079:
080: /** Defines which pages are valid for this dialog. */
081: public static final String[] PAGES = { "page1" };
082:
083: /** The additional information. */
084: protected List m_addInfoList;
085:
086: /** The user object that is edited on this dialog. */
087: protected CmsUser m_user;
088:
089: /** The map of editable additional info entries. */
090: private SortedMap m_addInfoEditable;
091:
092: /** The map of non-editable additional info entries. */
093: private SortedMap m_addInfoReadOnly;
094:
095: /** Stores the value of the request parameter for the edit all infos flag. */
096: private String m_paramEditall;
097:
098: /** Stores the value of the request parameter for the user id. */
099: private String m_paramUserid;
100:
101: /**
102: * Public constructor with JSP action element.<p>
103: *
104: * @param jsp an initialized JSP action element
105: */
106: public CmsEditUserAddInfoDialog(CmsJspActionElement jsp) {
107:
108: super (jsp);
109: }
110:
111: /**
112: * Public constructor with JSP variables.<p>
113: *
114: * @param context the JSP page context
115: * @param req the JSP request
116: * @param res the JSP response
117: */
118: public CmsEditUserAddInfoDialog(PageContext context,
119: HttpServletRequest req, HttpServletResponse res) {
120:
121: this (new CmsJspActionElement(context, req, res));
122: }
123:
124: /**
125: * Commits the edited user to the db.<p>
126: */
127: public void actionCommit() {
128:
129: List errors = new ArrayList();
130:
131: try {
132: if (!Boolean.valueOf(getParamEditall()).booleanValue()) {
133: // fill the values
134: Iterator it = m_addInfoList.iterator();
135: while (it.hasNext()) {
136: CmsUserAddInfoBean infoBean = (CmsUserAddInfoBean) it
137: .next();
138: if (infoBean.getValue() == null) {
139: m_user.deleteAdditionalInfo(infoBean.getName());
140: } else {
141: m_user.setAdditionalInfo(infoBean.getName(),
142: CmsDataTypeUtil
143: .parse(infoBean.getValue(),
144: infoBean.getType()));
145: }
146: }
147: } else {
148: Map readOnly = new HashMap();
149: Iterator itEntries = m_user.getAdditionalInfo()
150: .entrySet().iterator();
151: while (itEntries.hasNext()) {
152: Map.Entry entry = (Map.Entry) itEntries.next();
153: if (!CmsDataTypeUtil.isParseable(entry.getValue()
154: .getClass())) {
155: String key = entry.getKey().toString();
156: if (!entry.getValue().getClass().equals(
157: String.class)) {
158: key += "@"
159: + entry.getValue().getClass()
160: .getName();
161: }
162: if (m_addInfoReadOnly.containsKey(key)) {
163: readOnly.put(entry.getKey(), entry
164: .getValue());
165: }
166: }
167: }
168: m_user.setAdditionalInfo(readOnly);
169: itEntries = m_addInfoEditable.entrySet().iterator();
170: while (itEntries.hasNext()) {
171: Map.Entry entry = (Map.Entry) itEntries.next();
172: String key = (String) entry.getKey();
173: int pos = key.indexOf("@");
174: if (pos < 0) {
175: m_user.setAdditionalInfo(key, entry.getValue());
176: continue;
177: }
178: String className = key.substring(pos + 1);
179: key = key.substring(0, pos);
180: Class clazz;
181: try {
182: // try the class name
183: clazz = Class.forName(className);
184: } catch (Throwable e) {
185: try {
186: // try the class in the java.lang package
187: clazz = Class.forName(Integer.class
188: .getPackage().getName()
189: + "." + className);
190: } catch (Throwable e1) {
191: clazz = String.class;
192: }
193: }
194: m_user.setAdditionalInfo(key, CmsDataTypeUtil
195: .parse((String) entry.getValue(), clazz));
196: }
197: }
198:
199: // write the edited user
200: getCms().writeUser(m_user);
201: } catch (Throwable t) {
202: errors.add(t);
203: }
204:
205: if (errors.isEmpty()) {
206: if (getCurrentToolPath().endsWith(
207: "/orgunit/users/edit/addinfo/all")) {
208: // set closelink
209: Map argMap = new HashMap();
210: argMap.put(A_CmsEditUserDialog.PARAM_USERID, m_user
211: .getId());
212: argMap.put("oufqn", m_user.getOuFqn());
213: setParamCloseLink(CmsToolManager
214: .linkForToolPath(
215: getJsp(),
216: getCurrentToolPath()
217: .substring(
218: 0,
219: getCurrentToolPath()
220: .indexOf(
221: "/orgunit/users/edit/addinfo/all"))
222: + "/orgunit/users/edit/",
223: argMap));
224: }
225: }
226:
227: // set the list of errors to display when saving failed
228: setCommitErrors(errors);
229: }
230:
231: /**
232: * Returns the additional info map.<p>
233: *
234: * @return the additional info map
235: */
236: public SortedMap getInfo() {
237:
238: return m_addInfoEditable;
239: }
240:
241: /**
242: * Returns the edit all flag parameter value.<p>
243: *
244: * @return the edit all flag parameter value
245: */
246: public String getParamEditall() {
247:
248: CmsWorkplaceUserInfoManager manager = OpenCms
249: .getWorkplaceManager().getUserInfoManager();
250: if ((manager == null) || (manager.getBlocks() == null)
251: || manager.getBlocks().isEmpty()) {
252: // if the configuration is empty
253: return Boolean.TRUE.toString();
254: }
255: return m_paramEditall;
256: }
257:
258: /**
259: * Returns the user id parameter value.<p>
260: *
261: * @return the user id parameter value
262: */
263: public String getParamUserid() {
264:
265: return m_paramUserid;
266: }
267:
268: /**
269: * Returns the read only add info.<p>
270: *
271: * @return the read only add info
272: */
273: public SortedMap getReadonly() {
274:
275: return m_addInfoReadOnly;
276: }
277:
278: /**
279: * Sets the modified additional information.<p>
280: *
281: * @param addInfo the additional information to set
282: */
283: public void setInfo(SortedMap addInfo) {
284:
285: m_addInfoEditable = new TreeMap(addInfo);
286: }
287:
288: /**
289: * Sets the edit all flag parameter value.<p>
290: *
291: * @param editAll the edit all flag parameter value
292: */
293: public void setParamEditall(String editAll) {
294:
295: m_paramEditall = editAll;
296: }
297:
298: /**
299: * Sets the user id parameter value.<p>
300: *
301: * @param userId the user id parameter value to set
302: */
303: public void setParamUserid(String userId) {
304:
305: m_paramUserid = userId;
306: }
307:
308: /**
309: * Sets the read only add info.<p>
310: *
311: * @param addInfoReadOnly the read only add info to set
312: */
313: public void setReadonly(SortedMap addInfoReadOnly) {
314:
315: m_addInfoReadOnly = addInfoReadOnly;
316: }
317:
318: /**
319: * Creates the dialog HTML for all defined widgets of the named dialog (page).<p>
320: *
321: * This overwrites the method from the super class to create a layout variation for the widgets.<p>
322: *
323: * @param dialog the dialog (page) to get the HTML for
324: *
325: * @return the dialog HTML for all defined widgets of the named dialog (page)
326: */
327: protected String createDialogHtml(String dialog) {
328:
329: StringBuffer result = new StringBuffer(1024);
330:
331: result.append(createWidgetTableStart());
332: // show error header once if there were validation errors
333: result.append(createWidgetErrorHeader());
334:
335: if (dialog.equals(PAGES[0])) {
336: if (!Boolean.valueOf(getParamEditall()).booleanValue()) {
337: int pos = 0;
338: Iterator it = OpenCms.getWorkplaceManager()
339: .getUserInfoManager().getBlocks().iterator();
340: while (it.hasNext()) {
341: CmsWorkplaceUserInfoBlock block = (CmsWorkplaceUserInfoBlock) it
342: .next();
343:
344: result
345: .append(dialogBlockStart(key(block
346: .getTitle())));
347: result.append(createWidgetTableStart());
348: result.append(createDialogRowsHtml(pos, pos - 1
349: + block.getEntries().size()));
350: result.append(createWidgetTableEnd());
351: result.append(dialogBlockEnd());
352: pos += block.getEntries().size();
353: }
354: } else {
355: result
356: .append(createWidgetBlockStart(key(Messages.GUI_USER_EDITOR_LABEL_ADDITIONALINFO_BLOCK_0)));
357: result.append(createDialogRowsHtml(0, 0));
358: result.append(createWidgetBlockEnd());
359: result
360: .append(createWidgetBlockStart(key(Messages.GUI_USER_EDITOR_LABEL_READONLY_BLOCK_0)));
361: result.append(createDialogRowsHtml(1, 1));
362: result.append(createWidgetBlockEnd());
363: }
364: }
365: result.append(createWidgetTableEnd());
366: return result.toString();
367: }
368:
369: /**
370: * @see org.opencms.workplace.tools.accounts.A_CmsEditUserDialog#defineWidgets()
371: */
372: protected void defineWidgets() {
373:
374: // initialize the user object to use for the dialog
375: initUserObject();
376:
377: setKeyPrefix(KEY_PREFIX);
378:
379: int count = 0;
380: if (!Boolean.valueOf(getParamEditall()).booleanValue()) {
381: // widgets to display
382: Iterator itBlocks = OpenCms.getWorkplaceManager()
383: .getUserInfoManager().getBlocks().iterator();
384: while (itBlocks.hasNext()) {
385: CmsWorkplaceUserInfoBlock block = (CmsWorkplaceUserInfoBlock) itBlocks
386: .next();
387:
388: Iterator itEntries = block.getEntries().iterator();
389: while (itEntries.hasNext()) {
390: CmsWorkplaceUserInfoEntry entry = (CmsWorkplaceUserInfoEntry) itEntries
391: .next();
392:
393: int min = entry.isOptional() ? 0 : 1;
394: I_CmsWidget widget = entry.getWidgetObject();
395: addWidget(new CmsWidgetDialogParameter(
396: m_addInfoList.get(count), "value", entry
397: .getKey(), "", PAGES[0], widget,
398: min, 1));
399: count++;
400: }
401: }
402: } else {
403: addWidget(new CmsWidgetDialogParameter(this , "info",
404: PAGES[0], new CmsInputWidget()));
405: addWidget(new CmsWidgetDialogParameter(this , "readonly",
406: PAGES[0], new CmsDisplayWidget()));
407: }
408: }
409:
410: /**
411: * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
412: */
413: protected String[] getPageArray() {
414:
415: return PAGES;
416: }
417:
418: /**
419: * @see org.opencms.workplace.CmsWorkplace#initMessages()
420: */
421: protected void initMessages() {
422:
423: // add specific dialog resource bundle
424: addMessages(Messages.get().getBundleName());
425: // add default resource bundles
426: super .initMessages();
427: }
428:
429: /**
430: * Initializes the additional info bean to work with, depending on the dialog state and request parameters.<p>
431: */
432: protected void initUserObject() {
433:
434: try {
435: if (CmsStringUtil.isEmpty(getParamAction())
436: || CmsDialog.DIALOG_INITIAL
437: .equals(getParamAction())) {
438: // edit an existing user, get the user object from db
439: m_user = getCms().readUser(
440: new CmsUUID(getParamUserid()));
441: if (!Boolean.valueOf(getParamEditall()).booleanValue()) {
442: m_addInfoList = createAddInfoList(m_user);
443: } else {
444: setAddInfoMaps();
445: }
446: return;
447: } else {
448: // this is not the initial call, get the user object from session
449: m_user = getCms().readUser(
450: new CmsUUID(getParamUserid()));
451: if (!Boolean.valueOf(getParamEditall()).booleanValue()) {
452: m_addInfoList = (List) getDialogObject();
453: } else {
454: Map dObj = (Map) getDialogObject();
455: m_addInfoEditable = (SortedMap) dObj
456: .get("editable");
457: m_addInfoReadOnly = (SortedMap) dObj
458: .get("readonly");
459: }
460: return;
461: }
462: } catch (Exception e) {
463: // noop
464: }
465: // create a new user object
466: try {
467: m_user = getCms().readUser(new CmsUUID(getParamUserid()));
468: } catch (CmsException e) {
469: // ignore
470: }
471: if (!Boolean.valueOf(getParamEditall()).booleanValue()) {
472: m_addInfoList = createAddInfoList(m_user);
473: } else {
474: setAddInfoMaps();
475: }
476: }
477:
478: /**
479: * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
480: */
481: protected void initWorkplaceRequestValues(
482: CmsWorkplaceSettings settings, HttpServletRequest request) {
483:
484: // initialize parameters and dialog actions in super implementation
485: super .initWorkplaceRequestValues(settings, request);
486:
487: // save the current state (may be changed because of the widget values)
488: if (!Boolean.valueOf(getParamEditall()).booleanValue()) {
489: setDialogObject(m_addInfoList);
490: } else {
491: Map dObj = new HashMap();
492: dObj.put("editable", m_addInfoEditable);
493: dObj.put("readonly", m_addInfoReadOnly);
494: setDialogObject(dObj);
495: }
496: }
497:
498: /**
499: * @see org.opencms.workplace.CmsWidgetDialog#validateParamaters()
500: */
501: protected void validateParamaters() throws Exception {
502:
503: // test the needed parameters
504: String ou = getCms().readUser(new CmsUUID(getParamUserid()))
505: .getOuFqn();
506: OpenCms.getRoleManager().checkRole(getCms(),
507: CmsRole.ACCOUNT_MANAGER.forOrgUnit(ou));
508: }
509:
510: /**
511: * Creates a new additional information bean object.<p>
512: *
513: * @param user the user to create the bean for
514: *
515: * @return a new additional information bean object
516: */
517: private List createAddInfoList(CmsUser user) {
518:
519: List addInfoList = new ArrayList();
520: // add beans
521: Iterator itBlocks = OpenCms.getWorkplaceManager()
522: .getUserInfoManager().getBlocks().iterator();
523: while (itBlocks.hasNext()) {
524: CmsWorkplaceUserInfoBlock block = (CmsWorkplaceUserInfoBlock) itBlocks
525: .next();
526: Iterator itEntries = block.getEntries().iterator();
527: while (itEntries.hasNext()) {
528: CmsWorkplaceUserInfoEntry entry = (CmsWorkplaceUserInfoEntry) itEntries
529: .next();
530: Object value = user.getAdditionalInfo(entry.getKey());
531: if (value == null) {
532: value = "";
533: }
534: addInfoList.add(new CmsUserAddInfoBean(entry.getKey(),
535: value.toString(), entry.getClassType()));
536: }
537: }
538: return addInfoList;
539: }
540:
541: /**
542: * Builds the additional info maps.<p>
543: */
544: private void setAddInfoMaps() {
545:
546: m_addInfoEditable = new TreeMap();
547: m_addInfoReadOnly = new TreeMap();
548: Iterator itEntries = m_user.getAdditionalInfo().entrySet()
549: .iterator();
550: while (itEntries.hasNext()) {
551: Map.Entry entry = (Map.Entry) itEntries.next();
552: String key = entry.getKey().toString();
553: if ((entry.getValue() == null)
554: || CmsStringUtil.isEmptyOrWhitespaceOnly(entry
555: .getValue().toString())) {
556: // skip empty entries
557: continue;
558: }
559: if (!entry.getValue().getClass().equals(String.class)) {
560: // only show type different to string
561: key += "@" + entry.getValue().getClass().getName();
562: }
563: if (CmsDataTypeUtil
564: .isParseable(entry.getValue().getClass())) {
565: m_addInfoEditable.put(key, entry.getValue());
566: } else {
567: String value = entry.getValue().toString();
568: if (value.length() > (75 - key.length())) {
569: if ((75 - key.length()) > 5) {
570: value = value.substring(0, (75 - key.length()))
571: + " ...";
572: } else {
573: value = "...";
574: }
575: }
576: m_addInfoReadOnly.put(key, value);
577: }
578: }
579: }
580: }
|