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.workflow.usecases;
019:
020: import org.apache.cocoon.components.ContextHelper;
021: import org.apache.cocoon.environment.Request;
022: import org.apache.lenya.ac.AccessControlException;
023: import org.apache.lenya.ac.Identifiable;
024: import org.apache.lenya.ac.User;
025: import org.apache.lenya.cms.ac.PolicyUtil;
026: import org.apache.lenya.cms.observation.RepositoryEvent;
027: import org.apache.lenya.cms.observation.RepositoryEventFactory;
028: import org.apache.lenya.cms.publication.Document;
029: import org.apache.lenya.cms.publication.DocumentException;
030: import org.apache.lenya.cms.publication.Proxy;
031: import org.apache.lenya.cms.publication.Publication;
032: import org.apache.lenya.cms.workflow.WorkflowUtil;
033: import org.apache.lenya.cms.workflow.usecases.InvokeWorkflow;
034: import org.apache.lenya.notification.Message;
035: import org.apache.lenya.notification.NotificationException;
036: import org.apache.lenya.notification.NotificationEventDescriptor;
037: import org.apache.lenya.workflow.Version;
038: import org.apache.lenya.workflow.Workflowable;
039:
040: /**
041: * Reject usecase handler.
042: */
043: public class Reject extends InvokeWorkflow {
044:
045: protected static final String MESSAGE_SUBJECT = "notification-message";
046: protected static final String MESSAGE_DOCUMENT_REJECTED = "document-rejected";
047: protected static final String REJECT_REASON = "rejectReason";
048: protected static final String SEND_NOTIFICATION = "sendNotification";
049:
050: /**
051: * @see org.apache.lenya.cms.usecase.AbstractUsecase#doExecute()
052: */
053: protected void doExecute() throws Exception {
054:
055: super .doExecute();
056:
057: boolean notify = Boolean.valueOf(
058: getBooleanCheckboxParameter(SEND_NOTIFICATION))
059: .booleanValue();
060: if (notify) {
061: sendNotification(getSourceDocument());
062: }
063: }
064:
065: protected void sendNotification(Document authoringDocument)
066: throws NotificationException, DocumentException,
067: AccessControlException {
068:
069: User sender = getSession().getIdentity().getUser();
070:
071: String reason = getParameterAsString(REJECT_REASON);
072: Workflowable workflowable = WorkflowUtil.getWorkflowable(
073: this .manager, getSession(), getLogger(),
074: authoringDocument);
075: Version versions[] = workflowable.getVersions();
076: // current version is reject, want originating submit
077: Version version = versions[versions.length - 2];
078:
079: // we assume that the document has been submitted, otherwise we do
080: // nothing
081: if (version.getEvent().equals("submit")) {
082:
083: String userId = version.getUserId();
084: User user = PolicyUtil.getUser(this .manager,
085: authoringDocument.getCanonicalWebappURL(), userId,
086: getLogger());
087:
088: Identifiable[] recipients = { user };
089:
090: Document authoringVersion = authoringDocument
091: .getAreaVersion(Publication.AUTHORING_AREA);
092: String url;
093:
094: Proxy proxy = authoringVersion.getPublication().getProxy(
095: authoringVersion, false);
096: if (proxy != null) {
097: url = proxy.getURL(authoringVersion);
098: } else {
099: Request request = ContextHelper
100: .getRequest(this .context);
101: final String serverUrl = "http://"
102: + request.getServerName() + ":"
103: + request.getServerPort();
104: final String webappUrl = authoringVersion
105: .getCanonicalWebappURL();
106: url = serverUrl + request.getContextPath() + webappUrl;
107: }
108: String[] params = { reason, url };
109: Message message = new Message(MESSAGE_SUBJECT,
110: new String[0], MESSAGE_DOCUMENT_REJECTED, params,
111: sender, recipients);
112:
113: NotificationEventDescriptor descriptor = new NotificationEventDescriptor(
114: message);
115: RepositoryEvent event = RepositoryEventFactory
116: .createEvent(this.manager, getSession(),
117: getLogger(), descriptor);
118: getSession().enqueueEvent(event);
119: }
120: }
121: }
|