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.Engine;
023import org.apache.wiki.api.core.Page;
024import org.apache.wiki.api.exceptions.WikiException;
025import org.apache.wiki.filters.FilterManager;
026import org.apache.wiki.pages.PageManager;
027import org.apache.wiki.render.RenderingManager;
028import org.apache.wiki.tasks.TasksManager;
029import org.apache.wiki.workflow.Outcome;
030import org.apache.wiki.workflow.Task;
031import org.apache.wiki.workflow.WorkflowManager;
032
033
034/**
035 * Handles the actual page save and post-save actions. 
036 */
037public class SaveWikiPageTask extends Task {
038
039    private static final long serialVersionUID = 3190559953484411420L;
040
041    final Context context;
042
043    /**
044     * Creates the Task.
045     */
046    public SaveWikiPageTask( final Context context ) {
047        super( TasksManager.WIKIPAGE_SAVE_TASK_MESSAGE_KEY );
048        this.context = context;
049    }
050
051    /**
052     * {@inheritDoc}
053     */
054    @Override
055    public Outcome execute() throws WikiException {
056        // Retrieve attributes
057        final String proposedText = ( String )getWorkflowContext().get( WorkflowManager.WF_WP_SAVE_FACT_PROPOSED_TEXT );
058
059        final Engine engine = context.getEngine();
060        final Page page = context.getPage();
061
062        // Let the rest of the engine handle actual saving.
063        engine.getManager( PageManager.class ).putPageText( page, proposedText );
064
065        // Refresh the context for post save filtering.
066        engine.getManager( PageManager.class ).getPage( page.getName() );
067        engine.getManager( RenderingManager.class ).textToHTML( context, proposedText );
068        engine.getManager( FilterManager.class ).doPostSaveFiltering( context, proposedText );
069
070        return Outcome.STEP_COMPLETE;
071    }
072
073}