All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.code.rees.scope.struts2.programmatic.ProgrammaticModelDrivenConversationSupport Maven / Gradle / Ivy

There is a newer version: 1.7.4
Show newest version
/*******************************************************************************
 * 
 *  Struts2-Conversation-Plugin - An Open Source Conversation- and Flow-Scope Solution for Struts2-based Applications
 *  =================================================================================================================
 * 
 *  Copyright (C) 2012 by Rees Byars
 *  http://code.google.com/p/struts2-conversation/
 * 
 * **********************************************************************************************************************
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 *  the License. You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 *  an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 *  specific language governing permissions and limitations under the License.
 * 
 * **********************************************************************************************************************
 * 
 *  $Id: ProgrammaticModelDrivenConversationSupport.java reesbyars $
 ******************************************************************************/
package com.google.code.rees.scope.struts2.programmatic;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import com.google.code.rees.scope.ScopeAdapterFactory;
import com.google.code.rees.scope.conversation.ConversationAdapter;
import com.google.code.rees.scope.struts2.StrutsScopeConstants;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.inject.Inject;

/**
 * This class makes it simple to manage models with conversation-scoped
 * life-cycles programmatically.
 * 
 * All access to the model is through the {@link #getModel()} and
 * {@link #setModel(Serializable)} methods so that retrieval and insertion of
 * the model from and into conversation instances
 * can be managed on behalf of inheriting classes.
 * 
 * Use of this class requires zero configuration (no interceptors, etc.).
 * 
 * @author rees.byars
 * 
 * @param 
 */
public abstract class ProgrammaticModelDrivenConversationSupport
        extends ActionSupport implements
        ProgrammaticModelDrivenConversation, Preparable {

    private static final long serialVersionUID = -3567083451289146237L;

    private T model;
    private ScopeAdapterFactory adapterFactory;

    /**
     * Sets a {@link ScopeAdapterFactory} used to create
     * {@link ConversationAdapter ConversationAdapters}.
     * 
     * @param adapterFactory
     */
    @Inject(StrutsScopeConstants.SCOPE_ADAPTER_FACTORY_KEY)
    public void setAdapterFactory(ScopeAdapterFactory adapterFactory) {
        this.adapterFactory = adapterFactory;
    }

    /**
     * {@inheritDoc}
     * 
     * The model is scoped to the conversations indicated by
     * {@link #getConversations()}
     */
    @Override
    public T getModel() {
        if (this.model == null) {
            this.model = ProgrammaticModelDrivenConversationUtil.getModel(this,
                    this.getModelName());
        }
        return this.model;
    }

    /**
     * {@inheritDoc}
     * 
     * The model is scoped to the conversations indicated by
     * {@link #getConversations()}
     */
    @Override
    public void setModel(T model) {
        ProgrammaticModelDrivenConversationUtil.setModel(model, this,
                this.getModelName());
        this.model = model;
    }

    /**
     * The name of the model used to identify it in the
     * {@link com.google.code.rees.scope.conversation.context.ConversationContext
     * ConversationContext}.
     * 
     * This can be overridden to provide the name of choice. The default is
     * this.getClass().getName().
     * 
     * @return
     */
    protected String getModelName() {
        return this.getClass().getName();
    }

    /**
     * {@inheritDoc}
     */
    public void prepare() {
        this.adapterFactory.createConversationAdapter();
        Map> stackItem = new HashMap>();
        stackItem.put(StrutsScopeConstants.CONVERSATION_ID_MAP_STACK_KEY,
                ConversationAdapter.getAdapter().getViewContext());
        ActionContext.getContext().getValueStack().push(stackItem);
    }

    /**
     * Begins new instances of this class's conversations
     */
    protected void beginConversations() {
        ProgrammaticModelDrivenConversationUtil.begin(this);
    }

    /**
     * Continues this class's conversations associated with the current request
     */
    protected void continueConversations() {
        ProgrammaticModelDrivenConversationUtil.persist(this);
    }

    /**
     * Ends this class's conversations associated with the current request
     */
    protected void endConversations() {
        ProgrammaticModelDrivenConversationUtil.end(this);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy