001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.lenya.cms.contactform;
019:
020: import org.apache.avalon.framework.service.ServiceSelector;
021: import org.apache.lenya.ac.AccessController;
022: import org.apache.lenya.ac.AccessControllerResolver;
023: import org.apache.lenya.ac.User;
024: import org.apache.lenya.ac.UserManager;
025: import org.apache.lenya.cms.usecase.AbstractUsecase;
026: import org.apache.lenya.notification.Message;
027: import org.apache.lenya.notification.NotificationUtil;
028:
029: /**
030: * Contact form. The recipient user ID is set using the "recipient" parameter. The sender user ID is
031: * set using the "sender" parameter;
032: */
033: public class ContactForm extends AbstractUsecase {
034:
035: protected static final String RECIPIENT = "recipient";
036: protected static final String SENDER = "sender";
037: protected static final String MESSAGE = "message";
038: protected static final String NAME = "name";
039: protected static final String FROM = "email";
040:
041: protected void doCheckPreconditions() throws Exception {
042: super .doCheckPreconditions();
043: if (getParameterAsString(SENDER).equals("")) {
044: addErrorMessage("The sender is not configured!");
045: }
046: }
047:
048: protected void doCheckExecutionConditions() throws Exception {
049: super .doCheckExecutionConditions();
050:
051: if (getParameterAsString(NAME).trim().equals("")) {
052: addErrorMessage("Please enter your name!");
053: }
054: if (getParameterAsString(FROM).trim().equals("")) {
055: addErrorMessage("Please enter your e-mail address!");
056: }
057: if (getParameterAsString(MESSAGE).trim().equals("")) {
058: addErrorMessage("Please enter a message!");
059: }
060: if (getParameterAsString(RECIPIENT).trim().equals("")) {
061: addErrorMessage("Please choose a recipient!");
062: }
063: }
064:
065: protected void doExecute() throws Exception {
066: super .doExecute();
067:
068: String senderUserId = getParameterAsString(SENDER);
069: String recipientUserId = getParameterAsString(RECIPIENT);
070:
071: User sender = getUser(senderUserId);
072: User recipient = getUser(recipientUserId);
073: User[] recipients = { recipient };
074:
075: String name = getParameterAsString(NAME);
076: String body = getParameterAsString(MESSAGE);
077: String from = getParameterAsString(FROM);
078:
079: Message message = new Message("Contact form submitted by "
080: + name + " (" + from + ")", new String[0], body,
081: new String[0], sender, recipients);
082:
083: NotificationUtil.notify(this .manager, message);
084:
085: setDefaultTargetURL(getSourceURL() + "?sent=true");
086: }
087:
088: protected User getUser(String userId) throws Exception {
089: User user;
090: ServiceSelector selector = null;
091: AccessControllerResolver acResolver = null;
092: AccessController accessController = null;
093: try {
094: selector = (ServiceSelector) this .manager
095: .lookup(AccessControllerResolver.ROLE + "Selector");
096: acResolver = (AccessControllerResolver) selector
097: .select(AccessControllerResolver.DEFAULT_RESOLVER);
098: accessController = acResolver
099: .resolveAccessController(getSourceURL());
100:
101: UserManager userManager = accessController
102: .getAccreditableManager().getUserManager();
103: user = userManager.getUser(userId);
104:
105: } finally {
106: if (selector != null) {
107: if (acResolver != null) {
108: if (accessController != null) {
109: acResolver.release(accessController);
110: }
111: selector.release(acResolver);
112: }
113: this.manager.release(selector);
114: }
115: }
116: return user;
117: }
118:
119: }
|