blob: 4819a355341c23f61743c6c16b9ee49f373c4be1 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.lenya.cms.contactform;
import org.apache.cocoon.spring.configurator.WebAppContextUtils;
import org.apache.lenya.ac.AccessController;
import org.apache.lenya.ac.AccessControllerResolver;
import org.apache.lenya.ac.User;
import org.apache.lenya.ac.UserManager;
import org.apache.lenya.cms.usecase.AbstractUsecase;
import org.apache.lenya.notification.Message;
import org.apache.lenya.notification.Notifier;
/**
* Contact form. The recipient user ID is set using the "recipient" parameter. The sender user ID is
* set using the "sender" parameter;
*/
public class ContactForm extends AbstractUsecase {
protected static final String RECIPIENT = "recipient";
protected static final String SENDER = "sender";
protected static final String MESSAGE = "message";
protected static final String NAME = "name";
protected static final String FROM = "email";
private Notifier notifier;
protected void doCheckPreconditions() throws Exception {
super.doCheckPreconditions();
if (getParameterAsString(SENDER).equals("")) {
addErrorMessage("The sender is not configured!");
}
}
protected void doCheckExecutionConditions() throws Exception {
super.doCheckExecutionConditions();
if (getParameterAsString(NAME).trim().equals("")) {
addErrorMessage("Please enter your name!");
}
if (getParameterAsString(FROM).trim().equals("")) {
addErrorMessage("Please enter your e-mail address!");
}
if (getParameterAsString(MESSAGE).trim().equals("")) {
addErrorMessage("Please enter a message!");
}
if (getParameterAsString(RECIPIENT).trim().equals("")) {
addErrorMessage("Please choose a recipient!");
}
}
protected void doExecute() throws Exception {
super.doExecute();
String senderUserId = getParameterAsString(SENDER);
String recipientUserId = getParameterAsString(RECIPIENT);
User sender = getUser(senderUserId);
User recipient = getUser(recipientUserId);
User[] recipients = { recipient };
String name = getParameterAsString(NAME);
String body = getParameterAsString(MESSAGE);
String from = getParameterAsString(FROM);
Message message = new Message("Contact form submitted by " + name + " (" + from + ")",
new String[0], body, new String[0], sender, recipients);
getNotifier().notify(message);
setDefaultTargetURL(getSourceURL() + "?sent=true");
}
protected User getUser(String userId) throws Exception {
AccessControllerResolver acResolver = (AccessControllerResolver) WebAppContextUtils
.getCurrentWebApplicationContext().getBean(AccessControllerResolver.ROLE);
AccessController accessController = acResolver.resolveAccessController(getSourceURL());
UserManager userManager = accessController.getAccreditableManager().getUserManager();
return userManager.getUser(userId);
}
protected Notifier getNotifier() {
return notifier;
}
/**
* TODO: Bean wiring
*/
public void setNotifier(Notifier notifier) {
this.notifier = notifier;
}
}