DefaultClientInitializer.java :  » Workflow-Engines » osbl-1_0 » org » osbl » client » Java Open Source

Java Open Source » Workflow Engines » osbl 1_0 
osbl 1_0 » org » osbl » client » DefaultClientInitializer.java
package org.osbl.client;

import org.osbl.client.action.ActionProvider;
import org.osbl.client.action.StaticActionProvider;
import org.osbl.client.ClientResourceProvider;
import org.osbl.client.ClientServiceProvider;
import org.osbl.client.wings.devtools.*;
import org.osbl.client.wings.navigation.Navigation;
import org.osbl.client.wings.navigation.NavigationStructure;
import org.osbl.client.wings.concern.Overview;
import org.osbl.client.wings.shell.*;
import org.osbl.plugin.*;
import org.osbl.JTAHibernateEnvironment;
import org.osbl.ServiceProvider;
import org.osbl.persistence.*;
import org.osbl.authorization.Authorization;
import org.conform.BeanMetaProvider;
import org.conform.format.FormatFactory;
import org.conform.format.ResourceProvider;
import org.conform.wings.ComponentFactory;
import org.conform.wings.DefaultEditorFactory;
//import org.osbl.client.wings.devtools.AjaxDebugDevTool;
import org.hibernate.SessionFactory;
import org.wings.SConstants;
import org.wings.SLabel;
import org.wings.event.*;
import org.wings.session.Session;
import org.wings.session.SessionManager;

import javax.transaction.TransactionManager;
import javax.swing.*;
import java.util.*;

public class DefaultClientInitializer
    implements ClientInitializer
{
    public void preInitialization(Client client) {
        final Session session = SessionManager.getSession();

        ClientServiceProvider serviceProvider = ClientServiceProvider.getInstance();
        client.setServiceProvider(serviceProvider);

        ClientResourceProvider resourceProvider = (ClientResourceProvider)serviceProvider.getService("ClientResourceProvider");
        client.setResourceProvider(resourceProvider);

        determineLocale(resourceProvider);

        FormatFactory.getInstance().setLocale(session.getLocale());
        SessionManager.getSession().addRequestListener(new SRequestListener() {
            public void processRequest(SRequestEvent e) {
                switch (e.getType()) {
                    case SRequestEvent.REQUEST_START:
                        FormatFactory.getInstance().setLocale(session.getLocale());
                        break;
                    case SRequestEvent.REQUEST_END:
                        FormatFactory.getInstance().setLocale(null);
                        break;
                }
            }
        });

        BeanMetaProvider staticBeanMetaProvider = (BeanMetaProvider)serviceProvider.getService("StaticBeanMetaProvider");
        BeanMetaProvider webappBeanMetaProvider = new WebappBeanMetaProvider(staticBeanMetaProvider);
        BeanMetaProvider sessionMetaProvider = new SessionBeanMetaProvider(webappBeanMetaProvider);
        client.setBeanMetaProvider(sessionMetaProvider);

        ActionProvider actionProvider = new SessionActionProvider(StaticActionProvider.INSTANCE);
        client.setActionProvider(actionProvider);

        DefaultEditorFactory editorFactory = new DefaultEditorFactory();
        registerEditors(editorFactory);
        ComponentFactory.INSTANCE.setEditorFactory(editorFactory);
        ComponentFactory.INSTANCE.setResourceProvider(resourceProvider);

        try {
            JTAHibernateEnvironment environment = new JTAHibernateEnvironment();
            environment.setTransactionManager((TransactionManager) serviceProvider.getService("TransactionManager"));
            environment.setSessionFactory((SessionFactory) serviceProvider.getService("SessionFactory"));
            JTAHibernateEnvironmentRequestListener listener = new JTAHibernateEnvironmentRequestListener(environment);
            listener.processRequest(new SRequestEvent(session, SRequestEvent.REQUEST_START, null));
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void registerEditors(DefaultEditorFactory editorFactory) {
    }

    public void postInitialization(Client client) {
        SLabel label = new SLabel();
        label.setStyle(label.getStyle() + " headerlabel");
        label.setVerticalAlignment(SConstants.CENTER_ALIGN);
        client.addHeaderComponent(label);

        ActionProvider actionProvider = client.getActionProvider();
        client.addHeaderButton(actionProvider.getAction("businessobject.actions.logout"));

        Navigation navigation = new Navigation();
        navigation.setModel(NavigationStructure.getInstance().generateTree());

        client.addNavigation("navigation", client.getResourceProvider().getMessage("navigation.navigation"), null, navigation);
        client.addNavigation("overview", client.getResourceProvider().getMessage("navigation.overview"), null, new Overview());

        // Check whether the development tools are activated.
        boolean developmentToolsActivated = Boolean.valueOf((String)SessionManager.getSession().getProperty("development.tools", "false"));
        if (developmentToolsActivated) {
            DevelopmentTool developmentTool = new DevelopmentTool();

            PluginManager pluginManager = (PluginManager)ClientServiceProvider.getInstance().getService("ClientPluginManager");
            final ExtensionPoint extensionPoint = new ExtensionPoint("org.osbl.client.wings.devtools", Action.class);
            final List<Extension> list = pluginManager.getExtensions(extensionPoint);
            List<Action> actions = new ArrayList<Action>();
            for (Extension extension : list) {
                final Class<Action> actionClass = (Class<Action>)extension.getImplementation();
                try {
                    actions.add(actionClass.newInstance());
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
            }
            developmentTool.setGlobalActions(actions.toArray(new Action[actions.size()]));
            client.setDevelopmentTool(developmentTool);
        }
        client.setContextHelpTool(new ContextHelpTool());

        SessionManager.getSession().addExitListener(new ExitListener());
    }

    private String buildLabel(String lastName, String firstName, String mandatorKey) {
        StringBuilder sb = new StringBuilder();
        boolean lastNameIsNull = lastName == null || lastName.trim().length() == 0;
        boolean firstNameIsNull = firstName == null || firstName.trim().length() == 0;
        if (!lastNameIsNull)
            sb.append(lastName);

        if (!lastNameIsNull && !firstNameIsNull)
            sb.append(", ");

        if (!firstNameIsNull)
            sb.append(firstName);

        sb.append(" (").append(mandatorKey).append(")");

        return sb.toString();
    }

    private void determineLocale(ResourceProvider resourceProvider) {
        SessionManager.getSession().setSupportedLocales(resourceProvider.getSupportedLocales().toArray(new Locale[0]));
        SessionManager.getSession().setLocaleFromHeader(false);
    }

    private static class ExitListener
        implements SExitListener
    {
        private String userid;

        public ExitListener() {
            userid = SessionManager.getSession().getServletRequest().getUserPrincipal().getName();
        }

        public void prepareExit(SExitEvent e) throws ExitVetoException {
            Authorization authorization = (Authorization)ServiceProvider.getInstance().getService("AuthorizationService");
            authorization.clearCache(userid);
        }
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.