Project

General

Profile

Download (6.24 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2017 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.vaadin.ui.view;
10

    
11
import java.lang.reflect.Field;
12
import java.lang.reflect.InvocationTargetException;
13
import java.lang.reflect.Method;
14
import java.lang.reflect.ParameterizedType;
15
import java.lang.reflect.Type;
16

    
17
import javax.sql.DataSource;
18

    
19
import org.hibernate.SessionFactory;
20
import org.springframework.beans.factory.annotation.Autowired;
21
import org.springframework.beans.factory.annotation.Qualifier;
22
import org.springframework.context.ApplicationEventPublisher;
23
import org.springframework.context.annotation.Lazy;
24
import org.springframework.transaction.PlatformTransactionManager;
25

    
26
import com.vaadin.spring.annotation.SpringComponent;
27
import com.vaadin.spring.annotation.UIScope;
28

    
29
import eu.etaxonomy.cdm.api.application.CdmRepository;
30
import eu.etaxonomy.cdm.vaadin.session.ViewScopeConversationHolder;
31
import eu.etaxonomy.vaadin.mvp.AbstractEditorPresenter;
32
import eu.etaxonomy.vaadin.mvp.AbstractPopupEditor;
33
import eu.etaxonomy.vaadin.mvp.AbstractPresenter;
34
import eu.etaxonomy.vaadin.mvp.AbstractView;
35
import eu.etaxonomy.vaadin.ui.navigation.NavigationManager;
36

    
37
/**
38
 * @author a.kohlbecker
39
 * @since May 30, 2017
40
 *
41
 */
42
@SpringComponent
43
@UIScope
44
public class PopupEditorFactory {
45

    
46

    
47
    @Autowired
48
    protected ApplicationEventPublisher eventBus;
49

    
50
    @Autowired
51
    @Qualifier("cdmRepository")
52
    private CdmRepository repo;
53

    
54
    @Autowired
55
    private SessionFactory sessionFactory;
56

    
57
    @Autowired
58
    private DataSource dataSource;
59

    
60
    @Autowired
61
    private PlatformTransactionManager transactionManager;
62

    
63
    @Autowired
64
    @Lazy
65
    private NavigationManager navigationManager;
66

    
67

    
68
    private Field presenterRepoField;
69
    private Field presenterNavigationManagerField;
70
    private Field presenterEventBusField;
71

    
72
    private Field viewEventBusField;
73
    private Method viewInjectPresenterMethod;
74

    
75
    private Method viewInitMethod;
76

    
77
    private Method conversationHolderMethod;
78

    
79
    public PopupEditorFactory(){
80
        initFieldsAccess();
81
    }
82

    
83

    
84
    /**
85
     *
86
     */
87
    private void initFieldsAccess() {
88

    
89
        try {
90
            presenterRepoField = AbstractPresenter.class.getDeclaredField("repo");
91
            presenterRepoField.setAccessible(true);
92

    
93
            presenterNavigationManagerField = AbstractPresenter.class.getDeclaredField("navigationManager");
94
            presenterNavigationManagerField.setAccessible(true);
95

    
96
            presenterEventBusField = AbstractEditorPresenter.class.getDeclaredField("eventBus");
97
            presenterEventBusField.setAccessible(true);
98

    
99
            conversationHolderMethod = AbstractPresenter.class.getDeclaredMethod("setConversationHolder", ViewScopeConversationHolder.class);
100
            conversationHolderMethod.setAccessible(true);
101

    
102
            viewEventBusField = AbstractView.class.getDeclaredField("eventBus");
103
            viewEventBusField.setAccessible(true);
104

    
105
            viewInjectPresenterMethod = AbstractView.class.getDeclaredMethod("injectPresenter", AbstractPresenter.class);
106
            viewInjectPresenterMethod.setAccessible(true);
107

    
108
            viewInitMethod = AbstractView.class.getDeclaredMethod("init");
109
            viewInitMethod.setAccessible(true);
110

    
111
        } catch (NoSuchFieldException | SecurityException | NoSuchMethodException  e) {
112
            throw new RuntimeException("Severe error during initialization. Please check the classes AbstractPresenter, AbstractEditorPresenter, AbstractView for modificactions.", e);
113
        }
114

    
115
    }
116

    
117

    
118
    /**
119
     * @param popupViewClass
120
     * @return
121
     */
122
    public <V extends PopupView, P extends AbstractPresenter> PopupView newPopupView(Class<V> popupViewClass) {
123

    
124
        Class<? extends AbstractPresenter<?>> presenterClass = findGenericPresenterType(popupViewClass);
125
        try {
126

    
127
            P presenter = (P) presenterClass.newInstance();
128

    
129
            injectPresenterBeans(presenterClass, presenter);
130

    
131
            PopupView view = popupViewClass.newInstance();
132
            if(AbstractView.class.isAssignableFrom(popupViewClass)){
133
                AbstractView<P> abstractView = (AbstractView<P>)view;
134
                viewEventBusField.set(abstractView, eventBus);
135
                viewInjectPresenterMethod.invoke(abstractView, presenter);
136
                // invoke the @PostConstruct method
137

    
138
                viewInitMethod.invoke(abstractView);
139
            }
140
            return view;
141
        } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
142
            throw new RuntimeException(String.format("Error creating the view class '%s' with presenter class '%s'", popupViewClass, presenterClass), e);
143
        }
144
    }
145

    
146

    
147
    /**
148
     * @param presenterClass
149
     * @param presenter
150
     * @throws IllegalAccessException
151
     * @throws InvocationTargetException
152
     * @throws IllegalArgumentException
153
     */
154
    public <P extends AbstractPresenter> void injectPresenterBeans(
155
            Class<? extends AbstractPresenter<?>> presenterClass, P presenter) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
156
        presenterRepoField.set(presenter, repo);
157
        presenterNavigationManagerField.set(presenter, navigationManager);
158
        conversationHolderMethod.invoke(presenter, new ViewScopeConversationHolder(dataSource, sessionFactory, transactionManager));
159

    
160
        if(AbstractEditorPresenter.class.isAssignableFrom(presenterClass)){
161
            presenterEventBusField.set(presenter, eventBus);
162
        }
163
    }
164

    
165
    /**
166
     * @param popupViewClass
167
     * @return
168
     */
169
    @SuppressWarnings("unchecked")
170
    private Class<? extends AbstractPresenter<?>> findGenericPresenterType(Class<?  extends PopupView> popupViewClass) {
171

    
172
        ParameterizedType genericSuperClass = (ParameterizedType)popupViewClass.getGenericSuperclass();
173
        Type[] typeArgs = genericSuperClass.getActualTypeArguments();
174
        if(AbstractPopupEditor.class.isAssignableFrom(popupViewClass)){
175
           return (Class<? extends AbstractPresenter<?>>) typeArgs[1];
176
        } else {
177
           return (Class<? extends AbstractPresenter<?>>) typeArgs[0];
178
        }
179
    }
180

    
181

    
182
}
(2-2/5)