Project

General

Profile

Download (6.51 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2021 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.vaadin.ui;
10

    
11

    
12
import org.apache.log4j.Logger;
13
import org.springframework.beans.factory.annotation.Autowired;
14
import org.vaadin.spring.events.EventBus.UIEventBus;
15

    
16
import com.flowingcode.vaadin.addons.errorwindow.WindowErrorHandler;
17
import com.vaadin.navigator.View;
18
import com.vaadin.navigator.ViewDisplay;
19
import com.vaadin.server.ExternalResource;
20
import com.vaadin.server.Page;
21
import com.vaadin.server.Resource;
22
import com.vaadin.server.Responsive;
23
import com.vaadin.server.VaadinRequest;
24
import com.vaadin.server.VaadinSession;
25
import com.vaadin.spring.navigator.SpringViewProvider;
26
import com.vaadin.ui.Component;
27
import com.vaadin.ui.UI;
28
import com.vaadin.ui.themes.ValoTheme;
29

    
30
import eu.etaxonomy.cdm.database.PermissionDeniedException;
31
import eu.etaxonomy.cdm.vaadin.event.error.DelegatingErrorHandler;
32
import eu.etaxonomy.cdm.vaadin.event.error.ErrorTypeErrorHandlerWrapper;
33
import eu.etaxonomy.cdm.vaadin.event.error.PermissionDeniedErrorHandler;
34
import eu.etaxonomy.cdm.vaadin.permission.ReleasableResourcesView;
35
import eu.etaxonomy.cdm.vaadin.view.RedirectToLoginView;
36
import eu.etaxonomy.vaadin.ui.UIInitializedEvent;
37
import eu.etaxonomy.vaadin.ui.navigation.NavigationManagerBean;
38

    
39
/**
40
 * @author a.kohlbecker
41
 * @since Nov 11, 2021
42
 */
43
public abstract class AbstractUI extends UI {
44

    
45
    private static final long serialVersionUID = 7430086500775997281L;
46

    
47
    private static Logger logger = Logger.getLogger(AbstractUI.class);
48

    
49
    abstract protected ViewDisplay getViewDisplay();
50

    
51
    @Autowired
52
    protected SpringViewProvider viewProvider;
53

    
54
    @Autowired
55
    final private void setNavigationManagerBean(NavigationManagerBean navigatorBean) {
56
        // logger.debug("setNavigationManagerBean()" + navigatorBean.toString());
57
        setNavigator(navigatorBean);
58
    }
59

    
60
    final private NavigationManagerBean getNavigationManagerBean() {
61
        if(getNavigator() != null) {
62
            return (NavigationManagerBean) getNavigator();
63
        }
64
        return null;
65
    }
66

    
67
    @Autowired
68
    protected UIEventBus uiEventBus;
69

    
70
    public AbstractUI() {
71
        super();
72
    }
73

    
74
    public AbstractUI(Component content) {
75
        super(content);
76
    }
77

    
78
    @Override
79
    protected void init(VaadinRequest request) {
80

    
81
        logger.debug(this.getClass().getSimpleName() + ".init()");
82
        registerErrorHandlers();
83

    
84
        configureAccessDeniedView();
85

    
86
        assert getInitialViewName() != null;
87
        assert getViewDisplay() != null;
88

    
89
        logger.debug(this.getClass().getSimpleName() + ".init() ViewDisplay: " + getViewDisplay().getClass().getSimpleName() + ", initialViewName: " + getInitialViewName());
90
        initAdditionalContent();
91

    
92
        getNavigationManagerBean().setViewDisplay(getViewDisplay());
93
        getNavigationManagerBean().setDefaultViewName(getInitialViewName());
94

    
95
        Responsive.makeResponsive(this);
96

    
97
        addDetachListener(e -> {
98
            if(getNavigator() != null) {
99
                // no point using viewProvider.getView() without the navigator
100
                for(String viewName : viewProvider.getViewNamesForCurrentUI()){
101
                    View view = viewProvider.getView(viewName);
102
                    if(view != null && view instanceof ReleasableResourcesView) {
103
                        ((ReleasableResourcesView)view).releaseResourcesOnAccessDenied();
104
                    }
105
                }
106
            }
107
        });
108

    
109
        if(getBrandName() != null) {
110
            //TODO create annotation:
111
            // @Styles(files={""}, branding="brand")
112
            //
113
            // the branding can either be specified or can be read from the properties file in .cdmLibrary/remote-webapp/{instance-name}-app.properties
114
            // See CdmUtils for appropriate methods to access this folder
115
            // the 'vaadin://' protocol refers to the VAADIN folder
116
            Resource registryCssFile = new ExternalResource("vaadin://branding/" + getBrandName() + "/css/branding.css");
117
            Page.getCurrent().getStyles().add(registryCssFile);
118
        }
119

    
120
        uiEventBus.publish(this, new UIInitializedEvent());
121
    }
122

    
123
    /**
124
     * @return The name of the initial view to show
125
     */
126
    abstract protected String getInitialViewName();
127

    
128
    /**
129
     * Branding can either be specified or can be read from the properties file
130
     * in <code>.cdmLibrary/remote-webapp/{instance-name}-app.properties</code>
131
     * See CdmUtils for appropriate methods to access this folder the
132
     * <code>'vaadin://'</code> protocol refers to the VAADIN folder.
133
     * <p>
134
     * Can be overridden by implementing classes to set a brand.
135
     *
136
     * @return <code>NULL</code> for no branding or
137
     */
138
    protected String getBrandName() {
139
        return null;
140
    }
141

    
142
    /**
143
     * Implementing classes may add additional content to the UI. This
144
     * will for example be interesting when using the {@link ValoTheme.UI_WITH_MENU}
145
     * style.
146
     */
147
    abstract protected void initAdditionalContent();
148

    
149
    protected void registerErrorHandlers() {
150
        DelegatingErrorHandler delegatingErrorHander = new DelegatingErrorHandler();
151
        WindowErrorHandler errorHandler = new WindowErrorHandler(this, RegistrationUIDefaults.ERROR_CONTACT_MESSAGE_LINE + "</br></br>"
152
                + "<i>To help analyzing the problem please describe your actions that lead to this error and provide the error details from below in your email. "
153
                + "You also might want to add a sreenshot of the browser page in error.</i>");
154
        delegatingErrorHander.registerHandler(new ErrorTypeErrorHandlerWrapper<PermissionDeniedException>(PermissionDeniedException.class, new PermissionDeniedErrorHandler(this)));
155
        delegatingErrorHander.registerHandler(new ErrorTypeErrorHandlerWrapper<Exception>(Exception.class, errorHandler));
156
        setErrorHandler(delegatingErrorHander);
157
        VaadinSession.getCurrent().setErrorHandler(delegatingErrorHander);
158
    }
159

    
160
    protected void configureAccessDeniedView() {
161
        viewProvider.setAccessDeniedViewClass(RedirectToLoginView.class);
162
    }
163

    
164
    private String pageFragmentAsState() {
165
        Page page = Page.getCurrent();
166
        String fragment = page.getUriFragment();
167
        String state = null;
168
        if(fragment != null && fragment.startsWith("!")){
169
            state = fragment.substring(1, fragment.length());
170
        }
171
        return state;
172
    }
173

    
174
}
(2-2/11)