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.inbox.usecases;
019:
020: import org.apache.lenya.ac.Identifiable;
021: import org.apache.lenya.ac.User;
022: import org.apache.lenya.ac.UserManager;
023: import org.apache.lenya.cms.usecase.AbstractUsecase;
024: import org.apache.lenya.notification.Message;
025: import org.apache.lenya.notification.NotificationUtil;
026: import org.apache.lenya.util.Assert;
027:
028: /**
029: * Show and manage an inbox.
030: */
031: public class SendMessage extends AbstractUsecase {
032:
033: protected void initParameters() {
034: super .initParameters();
035:
036: User user = getSession().getIdentity().getUser();
037: setParameter("user", user);
038:
039: try {
040: UserManager userManager = user.getAccreditableManager()
041: .getUserManager();
042: User[] users = userManager.getUsers();
043: setParameter("users", users);
044:
045: } catch (Exception e) {
046: throw new RuntimeException(e);
047: }
048:
049: }
050:
051: protected void doCheckExecutionConditions() throws Exception {
052: super .doCheckExecutionConditions();
053:
054: if (getRecipient().equals("")) {
055: addErrorMessage("Please choose a recipient.");
056: }
057:
058: if (getSubject().equals("")) {
059: addErrorMessage("Please choose a subject.");
060: }
061: }
062:
063: protected String getSubject() {
064: return getParameterAsString("subject");
065: }
066:
067: protected String getRecipient() {
068: return getParameterAsString("recipient");
069: }
070:
071: protected String getBody() {
072: return getParameterAsString("body");
073: }
074:
075: protected void doExecute() throws Exception {
076: super .doExecute();
077:
078: String recipientId = getRecipient();
079: User sender = getSession().getIdentity().getUser();
080: User recipient;
081:
082: try {
083: UserManager userManager = sender.getAccreditableManager()
084: .getUserManager();
085: recipient = userManager.getUser(recipientId);
086: Assert.notNull("user " + recipientId, recipient);
087: } catch (Exception e) {
088: throw new RuntimeException(e);
089: }
090:
091: Identifiable[] recipients = { recipient };
092: String subject = getSubject();
093: String body = getBody();
094:
095: Message message = new Message(subject, new String[0], body,
096: new String[0], sender, recipients);
097: NotificationUtil.notify(this.manager, message);
098:
099: }
100:
101: }
|