Package com.nexirius.framework.htmlview.application

Source Code of com.nexirius.framework.htmlview.application.HTMLApplication

//{HEADER
/**
* This class is part of jnex 'Nexirius Application Framework for Java'
* Copyright (C) Nexirius GmbH, CH-4450 Sissach, Switzerland (www.nexirius.ch)
*
* <p>This library is free software; you can redistribute it and/or<br>
* modify it under the terms of the GNU Lesser General Public<br>
* License as published by the Free Software Foundation; either<br>
* version 2.1 of the License, or (at your option) any later version.</p>
*
* <p>This library is distributed in the hope that it will be useful,<br>
* but WITHOUT ANY WARRANTY; without even the implied warranty of<br>
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU<br>
* Lesser General Public License for more details.</p>
*
* <p>You should have received a copy of the GNU Lesser General Public<br>
* License along with this library; if not, write to the Free Software<br>
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA</p>
* </blockquote>
*
* <p>
* Nexirius GmbH, hereby disclaims all copyright interest in<br>
* the library jnex' 'Nexirius Application Framework for Java' written<br>
* by Marcel Baumann.</p>
*/
//}HEADER
package com.nexirius.framework.htmlview.application;

import com.nexirius.framework.FWLog;
import com.nexirius.framework.datamodel.DataModel;
import com.nexirius.framework.htmlview.*;
import com.nexirius.framework.htmlview.function.HTMLFunction;
import com.nexirius.framework.htmlview.function.HTMLTransition;
import com.nexirius.util.statemachine.Transition;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
* This class is the base class for all JnexServlet based applications. The actual implementation should not
* hold any user specific information. For user specific information use the DataModel instance which is
* returned with createApplicationModel(). The home model and the state machine are held in the session.
*/
public abstract class HTMLApplication {
    public static int MAX_NUMBER_OF_BACKWARD_STATES = 64;
    private Map defaultTransitionMap = new HashMap();

    public HTMLSessionVariable init() throws Exception {
        HTMLState firstState = null;
        DataModel homeModel = createHomeModel();
        HTMLTransition[] htmlTransistions = getHTMLTransistions(homeModel);

        for (int i = 0; i < htmlTransistions.length; i++) {
            HTMLTransition htmlTransistion = htmlTransistions[i];
            HTMLState state = htmlTransistion.getState();
            String event = htmlTransistion.getEvent();

            if (state == null) {
                // register default command
                FWLog.debug("default command(" + event + ")");
                defaultTransitionMap.put(event, htmlTransistion);
            } else {
                FWLog.debug("Transition(" + state.getName() + ", " + event + ", " + htmlTransistion.getTargetState().getName() + ")");
                new Transition(state, event, htmlTransistion.getTargetState());

                if (htmlTransistion.getCommand() != null) {
                    defaultTransitionMap.put(state.getName() + "." + event, htmlTransistion);
                }

                if (firstState == null) {
                    firstState = state;
                }
            }
        }

        if (firstState == null) {
            throw new Exception("Empty HTMLTransition table not allowed");
        }

        return new HTMLSessionVariable(getMaxNumberOfBackwardStates(), new HTMLStateMachine(firstState, homeModel), createHTMLResolver());
    }

    protected HTMLResolver createHTMLResolver() {
        HTMLResolver htmlResolver = new HTMLResolver();

        HTMLStreamMap streamMapper = new HTMLStreamMap();
        HTMLStreamMapEntry[] streamMapperEntries = getStreamMapperEntries();

        for (int i = 0; streamMapperEntries != null && i < streamMapperEntries.length; i++) {
            streamMapper.addEntry(streamMapperEntries[i]);
        }

        HTMLTemplateMap templateMap = new HTMLTemplateMap();

        HTMLTemplateMapEntry[] templateMapEntries = getTemplateMapEntries();

        for (int i = 0; templateMapEntries != null && i < templateMapEntries.length; i++) {
            templateMap.registerTemplate(templateMapEntries[i]);
        }

        HTMLFunction[] htmlFunctions = getHTMLFunctions();

        for (int i = 0; htmlFunctions != null && i < htmlFunctions.length; i++) {
            htmlResolver.registerHTMLFunction(htmlFunctions[i]);
        }

        HTMLVariable[] varibles = getHTMLVariables();

        if (varibles.length > 0) {
            for (int i = 0; i < varibles.length; i++) {
                HTMLVariable varible = varibles[i];
                htmlResolver.getVariableStore().setVariable(varible.getName(), varible.getValue());
            }
        }

        htmlResolver.setStreamMapper(streamMapper);
        htmlResolver.setTemplateMap(templateMap);
        htmlResolver.setTranslator(new ClientResourceHTMLTranslator(getApplicationName()));
        return htmlResolver;
    }

    protected int getMaxNumberOfBackwardStates() {
        return MAX_NUMBER_OF_BACKWARD_STATES;
    }

    /**
     * Access the command which is registered with the given state and event
     * @param state
     * @param event
     * @return null or the registered HTMLCommand instance
     */
    public HTMLCommand getHTMLCommand(String state, String event) {
        HTMLTransition transition = ((HTMLTransition) defaultTransitionMap.get(state + "." + event));

        if (transition == null) {
            return null;
        }

        return transition.getCommand();
    }

    /**
     * Access the command which is registered with the given event (no specific state)
     * @param event
     * @return null or the registered HTMLCommand instance
     */
    public HTMLCommand getDefaultHTMLCommand(String event) {
        HTMLTransition transition = (HTMLTransition) defaultTransitionMap.get(event);

        if (transition == null) {
            return null;
        }

        return transition.getCommand();
    }

    public HTMLTransition getDefaultHTMLTransition(String event) {
        return (HTMLTransition) defaultTransitionMap.get(event);
    }

    /**
     * This method is called just before the init() method is called to initialize the user specific session variable instance.
     */
    public abstract void preInit();

    /**
     * Just return the name of the application. This name is used to find the application specific properties file and it is used to create an application specific name for the session variable.
     *
     * @return the name of the properties file without extension (case sensitive)
     */
    public abstract String getApplicationName();

    /**
     * Create the user specific state information which is held along with the current user session (HTMLSessionVariable)
     *
     */
    public abstract DataModel createHomeModel();

    /**
     * This method must be implemented to initialize the state machine transition of the actual application. The states and
     * transitions (actually the whole state machine instance) are held in the current user session (not shared between sessions!).
     *
     * @param homeModel the session specific instace of the home model (which has been created by createApplicationModel)
     * @return at least one transition instance
     */
    public abstract HTMLTransition[] getHTMLTransistions(DataModel homeModel);

    /**
     * Access the mapping between symbolic names and streams (streams are template files or string literals which are used as templates)
     *
     * @return an array of stream mappers or null (if none)
     */
    public abstract HTMLStreamMapEntry[] getStreamMapperEntries();


    /**
     * Map DataModel classes to associated template names. If no specific template is defined (null) then this map is used
     * to find the template which is asssciated to the actual DataModel.
     *
     * @return null or the mapping instances
     */
    public abstract HTMLTemplateMapEntry[] getTemplateMapEntries();

    /**
     * Definition of individual HTML function implementation. Each time a term like $!functionName("param1", "param2")
     * occurs in the template it will lead to a call to the function which has been registered with name 'functionName'
     *
     * @return null or a list of HTML funtions
     */
    public abstract HTMLFunction[] getHTMLFunctions();

    /**
     * this metho is called just after the initialization of the HTMLSessionVariable instance.
     *
     * @param sessionVariable
     */
    public abstract void postInit(HTMLSessionVariable sessionVariable);

    /**
     * Each time an exception is raised within a HTMLCommand then this methos is called to handle it.
     *
     * @param sessionVariable
     * @param e               the original exception raised by the HTMLCommand
     */
    public abstract void handleException(HTMLSessionVariable sessionVariable, Exception e);



    /**
     * redefine this method to introduce variables like $(VARIABLE) which will be replaced with the associated value
     * @return new HTMLVariable[] {new HTMLVariable("company", "Nexirius"} replaces each occurence of $(company) with Nexirius
     */
    protected HTMLVariable[] getHTMLVariables() {
        return new HTMLVariable[0];
    }

    /**
     * Hook to implement application specific session strategy
     * @param request
     */
    public HttpSession getSession(HttpServletRequest request) {
        return request.getSession(true);
    }
}
TOP

Related Classes of com.nexirius.framework.htmlview.application.HTMLApplication

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.