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

com.google.code.rees.scope.conversation.processing.DefaultInjectionConversationManager 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: DefaultInjectionConversationManager.java reesbyars $
 ******************************************************************************/
package com.google.code.rees.scope.conversation.processing;

import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.code.rees.scope.conversation.ConversationAdapter;
import com.google.code.rees.scope.conversation.ConversationUtil;
import com.google.code.rees.scope.conversation.configuration.ConversationConfiguration;
import com.google.code.rees.scope.util.ScopeUtil;

/**
 * The default implementation of the {@link InjectionConversationManager}
 * 
 * @author rees.byars
 */
public class DefaultInjectionConversationManager extends SimpleConversationManager implements InjectionConversationManager, ConversationPostProcessor {

    private static final long serialVersionUID = 8632020943340087L;
    private static final Logger LOG = LoggerFactory.getLogger(DefaultInjectionConversationManager.class);

    /**
     * {@inheritDoc}
     */
    @Override
    protected void processConversation(ConversationConfiguration conversationConfig, ConversationAdapter conversationAdapter, Object action) {

        String actionId = conversationAdapter.getActionId();
        String conversationName = conversationConfig.getConversationName();
        String conversationId = conversationAdapter.getRequestContext().get(conversationName);

        if (LOG.isDebugEnabled()) {
            LOG.debug("Processing request for " + conversationName + " and action " + actionId + " of class " + action.getClass());
        }

        if (conversationId != null) {

            if (conversationConfig.containsAction(actionId)) {

                Map conversationContext = conversationAdapter
                        .getConversationContext(conversationName,
                                conversationId);

                if (LOG.isDebugEnabled()) {
                    LOG.debug("The action is a conversation member.  Processing with context:  " + conversationContext);
                }

                if (conversationContext != null) {

                    Map actionConversationFields = conversationConfig.getFields();
                    
                    if (actionConversationFields != null) {
                        ScopeUtil.setFieldValues(action, actionConversationFields, conversationContext);
                    }
                }

                if (conversationConfig.isEndAction(actionId)) {
                    conversationAdapter.addPostProcessor(new ConversationEndProcessor(), conversationConfig, conversationId);
                } else {
                    conversationAdapter.addPostProcessor(this, conversationConfig, conversationId);
                    conversationAdapter.getViewContext().put(conversationName, conversationId);
                }
            }
        } else if (conversationConfig.isBeginAction(actionId)) {
        	
            if (LOG.isDebugEnabled()) {
                LOG.debug("Beginning new " + conversationName+ ".");
            }
            
            conversationId = ConversationUtil.generateId();
            conversationAdapter.addPostProcessor(this, conversationConfig, conversationId);
            conversationAdapter.getViewContext().put(conversationName, conversationId);
            conversationAdapter.getRequestContext().put(conversationName, conversationId);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void postProcessConversation(ConversationAdapter conversationAdapter, ConversationConfiguration conversationConfig, String conversationId) {

    	String conversationName = conversationConfig.getConversationName();
    	
    	if (LOG.isDebugEnabled()) {
            LOG.debug("Performing post-processing of  " + conversationName + " with ID of " + conversationId + "...");
        }
    	
        Object action = conversationAdapter.getAction();

        Map actionConversationFields = conversationConfig.getFields();

        if (actionConversationFields != null) {

            if (LOG.isDebugEnabled()) {
                LOG.debug("Getting conversation fields for " + conversationName + " following execution of action " + conversationAdapter.getActionId());
            }
            
            Map conversationContext = conversationAdapter.getConversationContext(conversationName, conversationId);
            conversationContext.putAll(ScopeUtil.getFieldValues(action, actionConversationFields));

        }
        
        if (LOG.isDebugEnabled()) {
            LOG.debug("...completed post-processing of  " + conversationName + " with ID of " + conversationId + ".");
        }
        
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void injectConversationFields(Object target, ConversationAdapter conversationAdapter) {
        Collection actionConversationConfigs = this.configurationProvider.getConfigurations(target.getClass());
        if (actionConversationConfigs != null) {
            for (ConversationConfiguration conversation : actionConversationConfigs) {
                String conversationName = conversation.getConversationName();
                String conversationId = conversationAdapter.getRequestContext().get(conversationName);
                if (conversationId != null) {
                    Map conversationContext = conversationAdapter.getConversationContext(conversationName, conversationId);
                    if (conversationContext != null) {
                        Map actionConversationFields = conversation.getFields();
                        if (actionConversationFields != null) {
                            ScopeUtil.setFieldValues(target, actionConversationFields, conversationContext);
                        }
                    }
                }
            }
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void extractConversationFields(Object target, ConversationAdapter conversationAdapter) {
        Collection actionConversationConfigs = this.configurationProvider.getConfigurations(target.getClass());
        if (actionConversationConfigs != null) {
            for (ConversationConfiguration conversation : actionConversationConfigs) {

                Map actionConversationFields = conversation.getFields();
                String conversationName = conversation.getConversationName();
                String conversationId = conversationAdapter.getRequestContext().get(conversationName);

                if (conversationId == null) {
                    conversationId = ConversationUtil.generateId();
                }

                if (actionConversationFields != null) {

                    Map conversationContext = conversationAdapter.getConversationContext(conversationName, conversationId);
                    conversationContext.putAll(ScopeUtil.getFieldValues(target, actionConversationFields));
                }

                conversationAdapter.getViewContext().put(conversationName, conversationId);
            }
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy