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