Project

General

Profile

« Previous | Next » 

Revision efb45d70

Added by Fabian Reimeier almost 6 years ago

ref #7241 added DelegatingErrorHandler for components

View differences:

src/main/java/eu/etaxonomy/cdm/i18n/Messages.java
50 50
    public static String DistributionToolbar_LOGOUT = "DistributionToolbar_LOGOUT";
51 51
    public static String DistributionToolbar_SAVE = "DistributionToolbar_SAVE";
52 52
    public static String DistributionToolbar_STATUS = "DistributionToolbar_STATUS";
53
    public static String PermissionDeniedErrorHandler_ERROR_MSG = "PermissionDeniedErrorHandler_ERROR_MSG";
53 54
    public static String SettingsDialogWindowBase_CANCEL = "SettingsDialogWindowBase_CANCEL";
54 55
    public static String SettingsDialogWindowBase_OK = "SettingsDialogWindowBase_OK";
55 56

  
src/main/java/eu/etaxonomy/cdm/vaadin/event/error/DelegatingErrorHandler.java
1
// $Id$
2
/**
3
* Copyright (C) 2018 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10
package eu.etaxonomy.cdm.vaadin.event.error;
11

  
12
import java.util.HashMap;
13
import java.util.Map;
14

  
15
import com.vaadin.server.ErrorEvent;
16
import com.vaadin.server.ErrorHandler;
17
import com.vaadin.ui.Notification;
18

  
19
/**
20
 * @author freimeier
21
 * @date 26.02.2018
22
 *
23
 */
24
public class DelegatingErrorHandler implements ErrorHandler{
25
    Map<Class<? extends Exception>, ErrorTypeHandler<? extends Exception>> handlerMap = new HashMap<>();
26

  
27
    public <E extends Exception> void  registerHandler(Class<E> type, ErrorTypeHandler<E> handler) {
28
        handlerMap.put(type, handler);
29
    }
30

  
31
    /* (non-Javadoc)
32
     * @see com.vaadin.server.ErrorHandler#error(com.vaadin.server.ErrorEvent)
33
     */
34
    @Override
35
    public void error(ErrorEvent event) {
36
        System.out.println(event.getThrowable().getCause().getClass());
37
        Throwable cause = event.getThrowable().getCause();
38
        if(handlerMap.get(cause.getClass()) != null){
39
            handlerMap.get(cause.getClass()).error(event);
40
          } else {
41
            Notification.show(event.getThrowable().getMessage());
42
          }
43
    }
44
}
src/main/java/eu/etaxonomy/cdm/vaadin/event/error/ErrorTypeHandler.java
1
// $Id$
2
/**
3
* Copyright (C) 2018 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10
package eu.etaxonomy.cdm.vaadin.event.error;
11

  
12
import com.vaadin.server.ErrorEvent;
13
import com.vaadin.server.ErrorHandler;
14

  
15
/**
16
 * @author freimeier
17
 * @date 26.02.2018
18
 *
19
 */
20
public abstract class ErrorTypeHandler<E extends Exception> implements ErrorHandler {
21

  
22
    /* (non-Javadoc)
23
     * @see com.vaadin.server.ErrorHandler#error(com.vaadin.server.ErrorEvent)
24
     */
25
    @Override
26
    public final void error(ErrorEvent event) {
27
        handleError((E) event.getThrowable().getCause());
28

  
29
    }
30

  
31
    public abstract void handleError(E exception);
32
}
33

  
34

  
35

  
src/main/java/eu/etaxonomy/cdm/vaadin/event/error/HibernateSystemErrorHandler.java
1
// $Id$
2
/**
3
* Copyright (C) 2018 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10
package eu.etaxonomy.cdm.vaadin.event.error;
11

  
12
import org.springframework.orm.hibernate5.HibernateSystemException;
13

  
14
import com.vaadin.ui.Notification;
15

  
16
import eu.etaxonomy.cdm.database.PermissionDeniedException;
17
import eu.etaxonomy.cdm.i18n.Messages;
18

  
19
/**
20
 * @author freimeier
21
 * @date 26.02.2018
22
 *
23
 */
24
public class HibernateSystemErrorHandler extends ErrorTypeHandler<HibernateSystemException>{
25

  
26
    /* (non-Javadoc)
27
     * @see eu.etaxonomy.cdm.vaadin.util.errorhandler.ErrorTypeHandler#handleError(com.vaadin.server.ErrorEvent)
28
     */
29
    @Override
30
    public void handleError(HibernateSystemException exception) {
31
        if(exception != null) {
32
            if(exception.getCause().getClass().equals(PermissionDeniedException.class)) {
33
                Notification.show(Messages.getLocalizedString(Messages.PermissionDeniedErrorHandler_ERROR_MSG));
34
            }else {
35
                Notification.show(exception.getCause().getMessage());
36
            }
37
        }
38
    }
39

  
40
}
src/main/java/eu/etaxonomy/cdm/vaadin/view/distributionStatus/DistributionTableViewBean.java
16 16
import java.util.UUID;
17 17

  
18 18
import org.springframework.beans.factory.annotation.Autowired;
19
import org.springframework.orm.hibernate5.HibernateSystemException;
19 20
import org.springframework.security.core.GrantedAuthority;
20 21
import org.vaadin.addons.lazyquerycontainer.LazyQueryContainer;
21 22

  
......
47 48
import eu.etaxonomy.cdm.vaadin.component.DetailWindow;
48 49
import eu.etaxonomy.cdm.vaadin.component.DistributionToolbar;
49 50
import eu.etaxonomy.cdm.vaadin.container.CdmSQLContainer;
51
import eu.etaxonomy.cdm.vaadin.event.error.DelegatingErrorHandler;
52
import eu.etaxonomy.cdm.vaadin.event.error.HibernateSystemErrorHandler;
50 53
import eu.etaxonomy.cdm.vaadin.security.AccessRestrictedView;
51 54
import eu.etaxonomy.cdm.vaadin.util.CdmQueryFactory;
52 55
import eu.etaxonomy.cdm.vaadin.util.CdmSpringContextHelper;
......
167 170
                }
168 171
                //popup window
169 172
                final Window popup = new Window(Messages.getLocalizedString(Messages.DistributionTableViewBean_CHOOSE_DISTRIBUTION_STATUS));
173
                DelegatingErrorHandler errorHandler = new DelegatingErrorHandler();
174
                errorHandler.registerHandler(HibernateSystemException.class, new HibernateSystemErrorHandler());
175
                popup.setErrorHandler(errorHandler);
170 176
                final ListSelect termSelect = new ListSelect();
171 177
                termSelect.setSizeFull();
172 178
                termSelect.setContainerDataSource(getPresenter().getPresenceAbsenceTermContainer());
src/main/resources/eu/etaxonomy/cdm/i18n/messages.properties
27 27
DistributionToolbar_LOGOUT=Logout
28 28
DistributionToolbar_SAVE=Save
29 29
DistributionToolbar_STATUS=Status
30
PermissionDeniedErrorHandler_ERROR_MSG=You have no permission to do that!
30 31
SettingsDialogWindowBase_CANCEL=Cancel
31 32
SettingsDialogWindowBase_OK=OK
src/main/resources/eu/etaxonomy/cdm/i18n/messages_de.properties
27 27
DistributionToolbar_LOGOUT=Abmelden
28 28
DistributionToolbar_SAVE=Speichern
29 29
DistributionToolbar_STATUS=Status
30
PermissionDeniedErrorHandler_ERROR_MSG=Sie haben keine ausreichenden Berechtigungen, um diesen Vorgang auszuf?hren!
30 31
SettingsDialogWindowBase_CANCEL=Abbrechen
31 32
SettingsDialogWindowBase_OK=OK

Also available in: Unified diff