    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 org.apache.wiki.api.core.Context;
    022import org.apache.wiki.api.core.Page;
    023import org.apache.wiki.api.exceptions.WikiException;
    024import org.apache.wiki.filters.FilterManager;
    025import org.apache.wiki.tasks.TasksManager;
    026import org.apache.wiki.workflow.Outcome;
    027import org.apache.wiki.workflow.Task;
    028import org.apache.wiki.workflow.WorkflowManager;
    029
    030
    031/**
    032 * Handles the page pre-save actions. If the proposed page text is the same as the current version,
    033 * the {@link #execute( Context )} method returns {@link org.apache.wiki.workflow.Outcome#STEP_ABORT}. Any
    034 * WikiExceptions thrown by page filters will be re-thrown, and the workflow will abort.
    035 */
    036public class PreSaveWikiPageTask extends Task {
    037
    038    private static final long serialVersionUID = 6304715570092804615L;
    039    private final String m_proposedText;
    040
    041    /**
    042     * Creates the task.
    043     *
    044     * @param proposedText The text that was just saved.
    045     */
    046    public PreSaveWikiPageTask( final String proposedText ) {
    047        super( TasksManager.WIKIPAGE_PRESAVE_TASK_MESSAGE_KEY );
    048        m_proposedText = proposedText;
    049    }
    050
    051    /**
    052     * {@inheritDoc}
    053     */
    054    @Override
    055    public Outcome execute( final Context context ) throws WikiException {
    056        // Get the wiki page
    057        final Page page = context.getPage();
    058        // Figure out who the author was. Prefer the author set programmatically; otherwise get from the current logged in user
    059        if( context.getPage().getAuthor() == null && context.getCurrentUser() != null ) {
    060            page.setAuthor( context.getCurrentUser().getName() );
    061        }
    062
    063        // Run the pre-save filters. If any exceptions, add error to list, abort, and redirect
    064        final String saveText = context.getEngine().getManager( FilterManager.class ).doPreSaveFiltering( context, m_proposedText );
    065
    066        // Stash the wiki context, old and new text as workflow attributes
    067        getWorkflowContext().put( WorkflowManager.WF_WP_SAVE_FACT_PROPOSED_TEXT, saveText );
    068        return Outcome.STEP_COMPLETE;
    069    }
    070
    071}