001: package org.osbl.client;
002:
003: import org.osbl.client.action.ActionProvider;
004: import org.osbl.client.action.StaticActionProvider;
005: import org.osbl.client.ClientResourceProvider;
006: import org.osbl.client.ClientServiceProvider;
007: import org.osbl.client.wings.devtools.*;
008: import org.osbl.client.wings.navigation.Navigation;
009: import org.osbl.client.wings.navigation.NavigationStructure;
010: import org.osbl.client.wings.concern.Overview;
011: import org.osbl.client.wings.shell.*;
012: import org.osbl.plugin.*;
013: import org.osbl.JTAHibernateEnvironment;
014: import org.osbl.ServiceProvider;
015: import org.osbl.persistence.*;
016: import org.osbl.authorization.Authorization;
017: import org.conform.BeanMetaProvider;
018: import org.conform.format.FormatFactory;
019: import org.conform.format.ResourceProvider;
020: import org.conform.wings.ComponentFactory;
021: import org.conform.wings.DefaultEditorFactory; //import org.osbl.client.wings.devtools.AjaxDebugDevTool;
022: import org.hibernate.SessionFactory;
023: import org.wings.SConstants;
024: import org.wings.SLabel;
025: import org.wings.event.*;
026: import org.wings.session.Session;
027: import org.wings.session.SessionManager;
028:
029: import javax.transaction.TransactionManager;
030: import javax.swing.*;
031: import java.util.*;
032:
033: public class DefaultClientInitializer implements ClientInitializer {
034: public void preInitialization(Client client) {
035: final Session session = SessionManager.getSession();
036:
037: ClientServiceProvider serviceProvider = ClientServiceProvider
038: .getInstance();
039: client.setServiceProvider(serviceProvider);
040:
041: ClientResourceProvider resourceProvider = (ClientResourceProvider) serviceProvider
042: .getService("ClientResourceProvider");
043: client.setResourceProvider(resourceProvider);
044:
045: determineLocale(resourceProvider);
046:
047: FormatFactory.getInstance().setLocale(session.getLocale());
048: SessionManager.getSession().addRequestListener(
049: new SRequestListener() {
050: public void processRequest(SRequestEvent e) {
051: switch (e.getType()) {
052: case SRequestEvent.REQUEST_START:
053: FormatFactory.getInstance().setLocale(
054: session.getLocale());
055: break;
056: case SRequestEvent.REQUEST_END:
057: FormatFactory.getInstance().setLocale(null);
058: break;
059: }
060: }
061: });
062:
063: BeanMetaProvider staticBeanMetaProvider = (BeanMetaProvider) serviceProvider
064: .getService("StaticBeanMetaProvider");
065: BeanMetaProvider webappBeanMetaProvider = new WebappBeanMetaProvider(
066: staticBeanMetaProvider);
067: BeanMetaProvider sessionMetaProvider = new SessionBeanMetaProvider(
068: webappBeanMetaProvider);
069: client.setBeanMetaProvider(sessionMetaProvider);
070:
071: ActionProvider actionProvider = new SessionActionProvider(
072: StaticActionProvider.INSTANCE);
073: client.setActionProvider(actionProvider);
074:
075: DefaultEditorFactory editorFactory = new DefaultEditorFactory();
076: registerEditors(editorFactory);
077: ComponentFactory.INSTANCE.setEditorFactory(editorFactory);
078: ComponentFactory.INSTANCE.setResourceProvider(resourceProvider);
079:
080: try {
081: JTAHibernateEnvironment environment = new JTAHibernateEnvironment();
082: environment
083: .setTransactionManager((TransactionManager) serviceProvider
084: .getService("TransactionManager"));
085: environment
086: .setSessionFactory((SessionFactory) serviceProvider
087: .getService("SessionFactory"));
088: JTAHibernateEnvironmentRequestListener listener = new JTAHibernateEnvironmentRequestListener(
089: environment);
090: listener.processRequest(new SRequestEvent(session,
091: SRequestEvent.REQUEST_START, null));
092: } catch (Exception e) {
093: e.printStackTrace();
094: }
095: }
096:
097: private void registerEditors(DefaultEditorFactory editorFactory) {
098: }
099:
100: public void postInitialization(Client client) {
101: SLabel label = new SLabel();
102: label.setStyle(label.getStyle() + " headerlabel");
103: label.setVerticalAlignment(SConstants.CENTER_ALIGN);
104: client.addHeaderComponent(label);
105:
106: ActionProvider actionProvider = client.getActionProvider();
107: client.addHeaderButton(actionProvider
108: .getAction("businessobject.actions.logout"));
109:
110: Navigation navigation = new Navigation();
111: navigation.setModel(NavigationStructure.getInstance()
112: .generateTree());
113:
114: client.addNavigation("navigation", client.getResourceProvider()
115: .getMessage("navigation.navigation"), null, navigation);
116: client.addNavigation("overview", client.getResourceProvider()
117: .getMessage("navigation.overview"), null,
118: new Overview());
119:
120: // Check whether the development tools are activated.
121: boolean developmentToolsActivated = Boolean
122: .valueOf((String) SessionManager.getSession()
123: .getProperty("development.tools", "false"));
124: if (developmentToolsActivated) {
125: DevelopmentTool developmentTool = new DevelopmentTool();
126:
127: PluginManager pluginManager = (PluginManager) ClientServiceProvider
128: .getInstance().getService("ClientPluginManager");
129: final ExtensionPoint extensionPoint = new ExtensionPoint(
130: "org.osbl.client.wings.devtools", Action.class);
131: final List<Extension> list = pluginManager
132: .getExtensions(extensionPoint);
133: List<Action> actions = new ArrayList<Action>();
134: for (Extension extension : list) {
135: final Class<Action> actionClass = (Class<Action>) extension
136: .getImplementation();
137: try {
138: actions.add(actionClass.newInstance());
139: } catch (Exception e) {
140: e.printStackTrace();
141: }
142: }
143: developmentTool.setGlobalActions(actions
144: .toArray(new Action[actions.size()]));
145: client.setDevelopmentTool(developmentTool);
146: }
147: client.setContextHelpTool(new ContextHelpTool());
148:
149: SessionManager.getSession().addExitListener(new ExitListener());
150: }
151:
152: private String buildLabel(String lastName, String firstName,
153: String mandatorKey) {
154: StringBuilder sb = new StringBuilder();
155: boolean lastNameIsNull = lastName == null
156: || lastName.trim().length() == 0;
157: boolean firstNameIsNull = firstName == null
158: || firstName.trim().length() == 0;
159: if (!lastNameIsNull)
160: sb.append(lastName);
161:
162: if (!lastNameIsNull && !firstNameIsNull)
163: sb.append(", ");
164:
165: if (!firstNameIsNull)
166: sb.append(firstName);
167:
168: sb.append(" (").append(mandatorKey).append(")");
169:
170: return sb.toString();
171: }
172:
173: private void determineLocale(ResourceProvider resourceProvider) {
174: SessionManager.getSession().setSupportedLocales(
175: resourceProvider.getSupportedLocales().toArray(
176: new Locale[0]));
177: SessionManager.getSession().setLocaleFromHeader(false);
178: }
179:
180: private static class ExitListener implements SExitListener {
181: private String userid;
182:
183: public ExitListener() {
184: userid = SessionManager.getSession().getServletRequest()
185: .getUserPrincipal().getName();
186: }
187:
188: public void prepareExit(SExitEvent e) throws ExitVetoException {
189: Authorization authorization = (Authorization) ServiceProvider
190: .getInstance().getService("AuthorizationService");
191: authorization.clearCache(userid);
192: }
193: }
194: }
|