001/* 
002    Licensed to the Apache Software Foundation (ASF) under one
003    or more contributor license agreements.  See the NOTICE file
004    distributed with this work for additional information
005    regarding copyright ownership.  The ASF licenses this file
006    to you under the Apache License, Version 2.0 (the
007    "License"); you may not use this file except in compliance
008    with the License.  You may obtain a copy of the License at
009
010       http://www.apache.org/licenses/LICENSE-2.0
011
012    Unless required by applicable law or agreed to in writing,
013    software distributed under the License is distributed on an
014    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015    KIND, either express or implied.  See the License for the
016    specific language governing permissions and limitations
017    under the License.  
018 */
019package org.apache.wiki.tasks.pages;
020
021import java.security.Principal;
022
023import org.apache.wiki.WikiContext;
024import org.apache.wiki.WikiEngine;
025import org.apache.wiki.WikiPage;
026import org.apache.wiki.api.exceptions.WikiException;
027import org.apache.wiki.tasks.TasksManager;
028import org.apache.wiki.workflow.Outcome;
029import org.apache.wiki.workflow.Task;
030import org.apache.wiki.workflow.Workflow;
031import org.apache.wiki.workflow.WorkflowManager;
032
033
034/**
035 * Handles the page pre-save actions. If the proposed page text is the same as the current version, 
036 * the {@link #execute()} method returns {@link org.apache.wiki.workflow.Outcome#STEP_ABORT}. Any
037 * WikiExceptions thrown by page filters will be re-thrown, and the workflow will abort.
038 */
039public class PreSaveWikiPageTask extends Task {
040
041    private static final long serialVersionUID = 6304715570092804615L;
042    private final WikiContext m_context;
043    private final String m_proposedText;
044
045    /**
046     * Creates the task.
047     *
048     * @param context The WikiContext
049     * @param proposedText The text that was just saved.
050     */
051    public PreSaveWikiPageTask( final WikiContext context, final String proposedText ) {
052        super( TasksManager.WIKIPAGE_PRESAVE_TASK_MESSAGE_KEY );
053        m_context = context;
054        m_proposedText = proposedText;
055    }
056
057    /**
058     * {@inheritDoc}
059     */
060    @Override
061    public Outcome execute() throws WikiException {
062        // Retrieve attributes
063        WikiEngine engine = m_context.getEngine();
064        Workflow workflow = getWorkflow();
065
066        // Get the wiki page
067        WikiPage page = m_context.getPage();
068
069        // Figure out who the author was. Prefer the author set programmatically; otherwise 
070        // get from the current logged in user
071        if (page.getAuthor() == null) {
072            Principal wup = m_context.getCurrentUser();
073
074            if (wup != null) {
075                page.setAuthor(wup.getName());
076            }
077        }
078
079        // Run the pre-save filters. If any exceptions, add error to list, abort, and redirect
080        String saveText = engine.getFilterManager().doPreSaveFiltering(m_context, m_proposedText);
081
082        // Stash the wiki context, old and new text as workflow attributes
083        workflow.setAttribute( WorkflowManager.WF_WP_SAVE_ATTR_PRESAVE_WIKI_CONTEXT, m_context );
084        workflow.setAttribute( WorkflowManager.WF_WP_SAVE_FACT_PROPOSED_TEXT, saveText );
085        return Outcome.STEP_COMPLETE;
086    }
087
088}