001: /*
002: * File : $Source: /usr/local/cvs/alkacon/com.alkacon.opencms.registration/src/com/alkacon/opencms/registration/CmsRegistrationForm.java,v $
003: * Date : $Date: 2008-02-28 08:16:45 $
004: * Version: $Revision: 1.2 $
005: *
006: * This file is part of the Alkacon OpenCms Add-On Module Package
007: *
008: * Copyright (c) 2007 Alkacon Software GmbH (http://www.alkacon.com)
009: *
010: * The Alkacon OpenCms Add-On Module Package is free software:
011: * you can redistribute it and/or modify
012: * it under the terms of the GNU General Public License as published by
013: * the Free Software Foundation, either version 3 of the License, or
014: * (at your option) any later version.
015: *
016: * The Alkacon OpenCms Add-On Module Package is distributed
017: * in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: * GNU General Public License for more details.
021: *
022: * You should have received a copy of the GNU General Public License
023: * along with the Alkacon OpenCms Add-On Module Package.
024: * If not, see http://www.gnu.org/licenses/.
025: *
026: * For further information about Alkacon Software GmbH, please see the
027: * company website: http://www.alkacon.com.
028: *
029: * For further information about OpenCms, please see the
030: * project website: http://www.opencms.org.
031: */
032:
033: package com.alkacon.opencms.registration;
034:
035: import com.alkacon.opencms.formgenerator.CmsEmailField;
036: import com.alkacon.opencms.formgenerator.CmsForm;
037: import com.alkacon.opencms.formgenerator.CmsFormHandler;
038: import com.alkacon.opencms.formgenerator.I_CmsField;
039:
040: import org.opencms.file.CmsFile;
041: import org.opencms.file.CmsObject;
042: import org.opencms.i18n.CmsMessages;
043: import org.opencms.main.CmsIllegalArgumentException;
044: import org.opencms.main.OpenCms;
045: import org.opencms.util.CmsStringUtil;
046: import org.opencms.xml.content.CmsXmlContent;
047: import org.opencms.xml.content.CmsXmlContentFactory;
048: import org.opencms.xml.types.CmsXmlHtmlValue;
049:
050: import java.util.ArrayList;
051: import java.util.HashMap;
052: import java.util.Locale;
053:
054: /**
055: * Represents a webuser registration form with all configured fields and options.<p>
056: *
057: * @author Michael Moossen
058: *
059: * @version $Revision: 1.2 $
060: *
061: * @since 7.0.4
062: */
063: public class CmsRegistrationForm extends CmsForm {
064:
065: /** Configuration node name for the user creation options. */
066: public static final String NODE_ACTION = "Action";
067:
068: /** Configuration node name for the activation text node. */
069: public static final String NODE_ACTIVATIONTEXT = "ActivationText";
070:
071: /** Configuration node name for the confirmation mail node. */
072: public static final String NODE_CONFIRMATIONMAIL = "ConfirmationMail";
073:
074: /** Configuration node name for the email as login option. */
075: public static final String NODE_EMAILASLOGIN = "EmailAsLogin";
076:
077: /** Configuration node name for the group. */
078: public static final String NODE_GROUP = "Group";
079:
080: /** Configuration node name for the mail alert. */
081: public static final String NODE_MAILALERT = "MailAlert";
082:
083: /** Configuration node name for the organizational unit. */
084: public static final String NODE_ORGANIZATIONALUNIT = "OrganizationalUnit";
085:
086: /** Resource type ID of XML content. */
087: private static final String TYPE_NAME = "alkacon-registration";
088:
089: /** The activation text. */
090: private String m_activationText;
091:
092: /** The use email as login option. */
093: private boolean m_emailAsLogin;
094:
095: /** The group, the users will be members of. */
096: private String m_group;
097:
098: /** The organizational unit, where to create the users. */
099: private String m_orgUnit;
100:
101: /**
102: * Default constructor which parses the configuration file.<p>
103: *
104: * @param jsp the initialized CmsJspActionElement to access the OpenCms API
105: * @param messages the localized messages
106: * @param initial if true, field values are filled with values specified in the configuration file, otherwise from the request
107: *
108: * @throws Exception if parsing the configuration fails
109: */
110: public CmsRegistrationForm(CmsRegistrationFormHandler jsp,
111: CmsMessages messages, boolean initial) throws Exception {
112:
113: super (jsp, messages, initial);
114: }
115:
116: /**
117: * Constructor which parses the configuration file using a given configuration file URI.<p>
118: *
119: * @param jsp the initialized CmsJspActionElement to access the OpenCms API
120: * @param messages the localized messages
121: * @param initial if true, field values are filled with values specified in the configuration file, otherwise from the request
122: * @param formConfigUri URI of the form configuration file, if not provided, current URI is used for configuration
123: * @param formAction the desired action submitted by the form
124: *
125: * @throws Exception if parsing the configuration fails
126: */
127: public CmsRegistrationForm(CmsRegistrationFormHandler jsp,
128: CmsMessages messages, boolean initial,
129: String formConfigUri, String formAction) throws Exception {
130:
131: super (jsp, messages, initial, formConfigUri, formAction);
132: }
133:
134: /**
135: * Returns the resource type name of XML content forms.<p>
136: *
137: * @return the resource type name of XML content forms
138: */
139: public static String getStaticType() {
140:
141: return TYPE_NAME;
142: }
143:
144: /**
145: * @see com.alkacon.opencms.formgenerator.CmsForm#getConfirmationMailField()
146: */
147: public int getConfirmationMailField() {
148:
149: if (m_fieldsByName.isEmpty() || (m_confirmationMailField >= 0)) {
150: return m_confirmationMailField;
151: }
152: for (int i = 0; i < m_fields.size(); i++) {
153: if (m_fields.get(i) instanceof CmsEmailField) {
154: return i;
155: }
156: }
157: return m_confirmationMailField;
158: }
159:
160: /**
161: * Returns the activation text.<p>
162: *
163: * @return the activation text
164: */
165: public String getFormActivationText() {
166:
167: return m_activationText;
168: }
169:
170: /**
171: * Returns the group.<p>
172: *
173: * @return the group
174: */
175: public String getGroup() {
176:
177: return m_group;
178: }
179:
180: /**
181: * Returns the org Unit.<p>
182: *
183: * @return the org Unit
184: */
185: public String getOrgUnit() {
186:
187: return m_orgUnit;
188: }
189:
190: /**
191: * Initializes the form configuration and creates the necessary form field objects.<p>
192: *
193: * @param jsp the initialized CmsJspActionElement to access the OpenCms API
194: * @param messages the localized messages
195: * @param initial if true, field values are filled with values specified in the XML configuration
196: * @param formConfigUri URI of the form configuration file, if not provided, current URI is used for configuration
197: * @param formAction the desired action submitted by the form
198: *
199: * @throws Exception if parsing the configuration fails
200: */
201: public void init(CmsFormHandler jsp, CmsMessages messages,
202: boolean initial, String formConfigUri, String formAction)
203: throws Exception {
204:
205: m_parameterMap = jsp.getParameterMap();
206: // read the form configuration file from VFS
207: if (CmsStringUtil.isEmpty(formConfigUri)) {
208: formConfigUri = jsp.getRequestContext().getUri();
209: }
210: CmsFile file = jsp.getCmsObject().readFile(formConfigUri);
211: CmsXmlContent content = CmsXmlContentFactory.unmarshal(jsp
212: .getCmsObject(), file);
213:
214: // get current Locale
215: Locale locale = jsp.getRequestContext().getLocale();
216:
217: // init member variables
218: initMembers();
219:
220: m_formAction = formAction;
221: m_fields = new ArrayList();
222: m_dynaFields = new ArrayList();
223: m_fieldsByName = new HashMap();
224:
225: // initialize general form configuration
226: setTransportDatabase(false);
227:
228: initFormGlobalConfiguration(content, jsp.getCmsObject(),
229: locale, messages);
230: initFormMailAlert(content, jsp.getCmsObject(), locale, messages);
231: initConfirmationMail(content, jsp.getCmsObject(), locale);
232: initUserCreationOptions(content, jsp.getCmsObject(), locale);
233:
234: // initialize the form input fields
235: initInputFields(content, jsp, locale, messages, initial);
236: // init. the optional captcha field
237: initCaptchaField(jsp, content, locale, initial);
238:
239: // add the captcha field to the list of all fields, if the form has no check page
240: if (captchaFieldIsOnInputPage() && (m_captchaField != null)) {
241: addField(m_captchaField);
242: }
243: }
244:
245: /**
246: * Returns the email As Login option.<p>
247: *
248: * @return the email As Login option
249: */
250: public boolean isEmailAsLogin() {
251:
252: return m_emailAsLogin;
253: }
254:
255: /**
256: * Sets the activation text.<p>
257: *
258: * @param activationText the activation text to set
259: */
260: public void setFormActivationText(String activationText) {
261:
262: m_activationText = activationText;
263: }
264:
265: /**
266: * @see com.alkacon.opencms.formgenerator.CmsForm#getField(java.lang.String)
267: */
268: protected I_CmsField getField(String fieldType) {
269:
270: if (fieldType.equals(CmsPasswordField.getStaticType())) {
271: return new CmsPasswordField();
272: }
273: return super .getField(fieldType);
274: }
275:
276: /**
277: * Initializes the general online form settings.<p>
278: *
279: * @param content the XML configuration content
280: * @param cms the CmsObject to access the content values
281: * @param locale the currently active Locale
282: * @param messages the localized messages
283: *
284: * @throws Exception if initializing the form settings fails
285: */
286: protected void initFormGlobalConfiguration(CmsXmlContent content,
287: CmsObject cms, Locale locale, CmsMessages messages)
288: throws Exception {
289:
290: // get the form text
291: String stringValue = content.getStringValue(cms, NODE_FORMTEXT,
292: locale);
293: setFormText(getConfigurationValue(stringValue, ""));
294: // get the form footer text
295: stringValue = content.getStringValue(cms, NODE_FORMFOOTERTEXT,
296: locale);
297: setFormFooterText(getConfigurationValue(stringValue, ""));
298:
299: // get the activation text
300: stringValue = content.getStringValue(cms, NODE_ACTIVATIONTEXT,
301: locale);
302: setFormActivationText(getConfigurationValue(stringValue, ""));
303:
304: // get the form confirmation text
305: stringValue = content.getStringValue(cms,
306: NODE_FORMCONFIRMATION, locale);
307: setFormConfirmationText(getConfigurationValue(stringValue, ""));
308: // get the unused target URI
309: setTargetUri("");
310:
311: // optional configuration options
312: String pathPrefix = NODE_OPTIONALCONFIGURATION + "/";
313:
314: // get the form check page flag
315: stringValue = content.getStringValue(cms, pathPrefix
316: + NODE_SHOWCHECK, locale);
317: setShowCheck(Boolean.valueOf(stringValue).booleanValue());
318: // get the check page text
319: stringValue = content.getStringValue(cms, pathPrefix
320: + NODE_FORMCHECKTEXT, locale);
321: setFormCheckText(getConfigurationValue(stringValue, ""));
322: // get the dynamic fields class
323: stringValue = content.getStringValue(cms, pathPrefix
324: + NODE_DYNAMICFIELDCLASS, locale);
325: setDynamicFieldClass(getConfigurationValue(stringValue, ""));
326: // get the show mandatory setting
327: stringValue = content.getStringValue(cms, pathPrefix
328: + NODE_SHOWMANDATORY, locale);
329: setShowMandatory(Boolean.valueOf(
330: getConfigurationValue(stringValue, Boolean.TRUE
331: .toString())).booleanValue());
332: // get the show reset button setting
333: stringValue = content.getStringValue(cms, pathPrefix
334: + NODE_SHOWRESET, locale);
335: setShowReset(Boolean.valueOf(
336: getConfigurationValue(stringValue, Boolean.TRUE
337: .toString())).booleanValue());
338: // get the form attributes
339: stringValue = content.getStringValue(cms, pathPrefix
340: + NODE_FORMATTRIBUTES, locale);
341: if (CmsStringUtil.isNotEmpty(stringValue)) {
342: setFormAttributes(" " + stringValue);
343: }
344: // get the field attributes
345: stringValue = content.getStringValue(cms, pathPrefix
346: + NODE_FORMFIELDATTRIBUTES, locale);
347: if (CmsStringUtil.isNotEmpty(stringValue)) {
348: setFormFieldAttributes(" " + stringValue);
349: } else {
350: // no field attributes specified, check default field attributes
351: String defaultAttributes = messages
352: .key("form.field.default.attributes");
353: if (CmsStringUtil.isNotEmpty(defaultAttributes)) {
354: setFormFieldAttributes(" " + defaultAttributes);
355: }
356: }
357: }
358:
359: /**
360: * Initializes the mail alert settings.<p>
361: *
362: * @param content the XML configuration content
363: * @param cms the CmsObject to access the content values
364: * @param locale the currently active Locale
365: * @param messages the localized messages
366: */
367: protected void initFormMailAlert(CmsXmlContent content,
368: CmsObject cms, Locale locale, CmsMessages messages) {
369:
370: if (content.getValue(NODE_MAILALERT, locale) == null) {
371: setTransportEmail(false);
372: return;
373: }
374: setTransportEmail(true);
375: // optional configuration options
376: String pathPrefix = NODE_MAILALERT + "/";
377:
378: String stringValue;
379: // get the mail from address
380: stringValue = content.getStringValue(cms, pathPrefix
381: + NODE_MAILFROM, locale);
382: setMailFrom(getConfigurationValue(stringValue, ""));
383: // get the mail to address(es)
384: stringValue = content.getStringValue(cms, pathPrefix
385: + NODE_MAILTO, locale);
386: setMailTo(getConfigurationValue(stringValue, ""));
387: // get the mail CC recipient(s)
388: stringValue = content.getStringValue(cms, pathPrefix
389: + NODE_MAILCC, locale);
390: setMailCC(getConfigurationValue(stringValue, ""));
391: // get the mail BCC recipient(s)
392: stringValue = content.getStringValue(cms, pathPrefix
393: + NODE_MAILBCC, locale);
394: setMailBCC(getConfigurationValue(stringValue, ""));
395: // get the mail subject
396: stringValue = content.getStringValue(cms, pathPrefix
397: + NODE_MAILSUBJECT, locale);
398: setMailSubject(getConfigurationValue(stringValue, ""));
399: // get the optional mail subject prefix from localized messages
400: stringValue = messages.key("form.mailsubject.prefix");
401: if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(stringValue)) {
402: // prefix present, set it
403: setMailSubjectPrefix(stringValue + " ");
404: } else {
405: // no prefix present
406: setMailSubjectPrefix("");
407: }
408: CmsXmlHtmlValue mailTextValue = (CmsXmlHtmlValue) content
409: .getValue(pathPrefix + NODE_MAILTEXT, locale);
410: if (mailTextValue != null) {
411: // get the mail text as plain text
412: stringValue = mailTextValue.getPlainText(cms);
413: setMailTextPlain(getConfigurationValue(stringValue, ""));
414: // get the mail text
415: stringValue = mailTextValue.getStringValue(cms);
416: setMailText(getConfigurationValue(stringValue, ""));
417: } else {
418: setMailTextPlain("");
419: setMailText("");
420: }
421: // get the mail type
422: stringValue = content.getStringValue(cms, pathPrefix
423: + NODE_MAILTYPE, locale);
424: setMailType(getConfigurationValue(stringValue, MAILTYPE_HTML));
425: }
426:
427: /**
428: * Initializes the confirmation mail settings.<p>
429: *
430: * @param content the XML configuration content
431: * @param cms the CmsObject to access the content values
432: * @param locale the currently active Locale
433: */
434: private void initConfirmationMail(CmsXmlContent content,
435: CmsObject cms, Locale locale) {
436:
437: setConfirmationMailEnabled(true);
438:
439: // get the confirmation mail node
440: if (content.getValue(NODE_CONFIRMATIONMAIL, locale) == null) {
441: // TODO: default settings
442: setConfirmationMailSubject("");
443: setConfirmationMailText("");
444: setConfirmationMailField(-1);
445: return;
446: }
447:
448: // optional confirmation mail nodes
449: String pathPrefix = NODE_CONFIRMATIONMAIL + "/";
450:
451: // get the confirmation mail subject
452: String stringValue = content.getStringValue(cms, pathPrefix
453: + NODE_CONFIRMATIONMAILSUBJECT, locale);
454: setConfirmationMailSubject(getConfigurationValue(stringValue,
455: ""));
456:
457: CmsXmlHtmlValue mailTextValue = (CmsXmlHtmlValue) content
458: .getValue(pathPrefix + NODE_CONFIRMATIONMAILTEXT,
459: locale);
460: if (mailTextValue != null) {
461: // get the confirmation mail text
462: stringValue = mailTextValue.getPlainText(cms);
463: setConfirmationMailTextPlain(getConfigurationValue(
464: stringValue, ""));
465: stringValue = mailTextValue.getStringValue(cms);
466: setConfirmationMailText(getConfigurationValue(stringValue,
467: ""));
468: } else {
469: setConfirmationMailTextPlain("");
470: setConfirmationMailText("");
471: }
472:
473: // get the confirmation mail field index number
474: stringValue = content.getStringValue(cms, pathPrefix
475: + NODE_CONFIRMATIONMAILFIELD, locale);
476: int fieldIndex = -1;
477: try {
478: fieldIndex = Integer.parseInt(getConfigurationValue(
479: stringValue, "1")) - 1;
480: } catch (Exception e) {
481: // ignore this exception, use first field
482: }
483: setConfirmationMailField(fieldIndex);
484: setMailSubjectPrefix("");
485: }
486:
487: /**
488: * Initializes the user creation settings.<p>
489: *
490: * @param content the XML configuration content
491: * @param cms the CmsObject to access the content values
492: * @param locale the currently active Locale
493: */
494: private void initUserCreationOptions(CmsXmlContent content,
495: CmsObject cms, Locale locale) {
496:
497: if (content.getValue(NODE_ACTION, locale) == null) {
498: // ignore for profile edition
499: return;
500: }
501: String path = NODE_ACTION + "/";
502:
503: String stringValue = content.getStringValue(cms, path
504: + NODE_EMAILASLOGIN, locale);
505: m_emailAsLogin = Boolean.valueOf(stringValue).booleanValue();
506:
507: stringValue = content.getStringValue(cms, path
508: + NODE_ORGANIZATIONALUNIT, locale);
509: try {
510: m_orgUnit = OpenCms.getOrgUnitManager()
511: .readOrganizationalUnit(cms, stringValue).getName();
512: } catch (Throwable e) {
513: throw new CmsIllegalArgumentException(Messages.get()
514: .container(Messages.ERR_ORGUNIT_DOESNOT_EXIST_1,
515: stringValue));
516: }
517:
518: if (content.getValue(path + NODE_GROUP, locale) == null) {
519: return;
520: }
521: stringValue = content.getStringValue(cms, path + NODE_GROUP,
522: locale);
523: if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(stringValue)) {
524: try {
525: m_group = cms.readGroup(stringValue).getName();
526: } catch (Throwable e) {
527: throw new CmsIllegalArgumentException(Messages.get()
528: .container(Messages.ERR_GROUP_DOESNOT_EXIST_1,
529: stringValue));
530: }
531: }
532: }
533: }
|