2 * Copyright (C) 2017 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.vaadin
.ui
.view
;
11 import java
.io
.Serializable
;
12 import java
.lang
.reflect
.Field
;
13 import java
.lang
.reflect
.InvocationTargetException
;
14 import java
.lang
.reflect
.Method
;
15 import java
.lang
.reflect
.ParameterizedType
;
16 import java
.lang
.reflect
.Type
;
18 import javax
.sql
.DataSource
;
20 import org
.hibernate
.SessionFactory
;
21 import org
.springframework
.beans
.factory
.annotation
.Autowired
;
22 import org
.springframework
.beans
.factory
.annotation
.Qualifier
;
23 import org
.springframework
.context
.ApplicationEventPublisher
;
24 import org
.springframework
.context
.annotation
.Lazy
;
25 import org
.springframework
.transaction
.PlatformTransactionManager
;
27 import com
.vaadin
.spring
.annotation
.SpringComponent
;
28 import com
.vaadin
.spring
.annotation
.UIScope
;
30 import eu
.etaxonomy
.cdm
.api
.application
.CdmRepository
;
31 import eu
.etaxonomy
.cdm
.service
.IRegistrationWorkingSetService
;
32 import eu
.etaxonomy
.cdm
.service
.ISpecimenTypeDesignationWorkingSetService
;
33 import eu
.etaxonomy
.cdm
.vaadin
.view
.name
.NameTypeDesignationPresenter
;
34 import eu
.etaxonomy
.cdm
.vaadin
.view
.name
.SpecimenTypeDesignationWorkingsetEditorPresenter
;
35 import eu
.etaxonomy
.vaadin
.mvp
.AbstractEditorPresenter
;
36 import eu
.etaxonomy
.vaadin
.mvp
.AbstractPopupEditor
;
37 import eu
.etaxonomy
.vaadin
.mvp
.AbstractPresenter
;
38 import eu
.etaxonomy
.vaadin
.mvp
.AbstractView
;
39 import eu
.etaxonomy
.vaadin
.ui
.navigation
.NavigationManager
;
42 * @author a.kohlbecker
48 public class PopupEditorFactory
implements Serializable
{
51 private static final long serialVersionUID
= -3371003970671610677L;
54 protected ApplicationEventPublisher eventBus
;
57 @Qualifier("cdmRepository")
58 private CdmRepository repo
;
61 private SessionFactory sessionFactory
;
64 private DataSource dataSource
;
67 private PlatformTransactionManager transactionManager
;
70 private ISpecimenTypeDesignationWorkingSetService specimenTypeDesignationWorkingSetService
;
73 private IRegistrationWorkingSetService registrationWorkingSetService
;
77 private NavigationManager navigationManager
;
79 private Field presenterRepoField
;
80 private Field presenterNavigationManagerField
;
81 private Field presenterEventBusField
;
83 private Field viewEventBusField
;
84 private Field specimenTypeDesignationWorkingSetServiceField
;
85 private Method viewInjectPresenterMethod
;
89 private Method viewInitMethod
;
91 public PopupEditorFactory(){
99 private void initFieldsAccess() {
102 presenterRepoField
= AbstractPresenter
.class.getDeclaredField("repo");
103 presenterRepoField
.setAccessible(true);
105 presenterNavigationManagerField
= AbstractPresenter
.class.getDeclaredField("navigationManager");
106 presenterNavigationManagerField
.setAccessible(true);
108 presenterEventBusField
= AbstractEditorPresenter
.class.getDeclaredField("eventBus");
109 presenterEventBusField
.setAccessible(true);
111 viewEventBusField
= AbstractView
.class.getDeclaredField("eventBus");
112 viewEventBusField
.setAccessible(true);
114 viewInjectPresenterMethod
= AbstractView
.class.getDeclaredMethod("injectPresenter", AbstractPresenter
.class);
115 viewInjectPresenterMethod
.setAccessible(true);
117 viewInitMethod
= AbstractView
.class.getDeclaredMethod("init");
118 viewInitMethod
.setAccessible(true);
120 specimenTypeDesignationWorkingSetServiceField
= SpecimenTypeDesignationWorkingsetEditorPresenter
.class.getDeclaredField("specimenTypeDesignationWorkingSetService");
121 specimenTypeDesignationWorkingSetServiceField
.setAccessible(true);
123 Field
= NameTypeDesignationPresenter
.class.getDeclaredField("registrationWorkingSetService");
124 Field
.setAccessible(true);
126 } catch (NoSuchFieldException
| SecurityException
| NoSuchMethodException e
) {
127 throw new RuntimeException("Severe error during initialization. Please check the classes AbstractPresenter, AbstractEditorPresenter, AbstractView for modificactions.", e
);
134 * @param popupViewClass
137 public <V
extends PopupView
, P
extends AbstractPresenter
> PopupView
newPopupView(Class
<V
> popupViewClass
) {
139 Class
<?
extends AbstractPresenter
<?
>> presenterClass
= findGenericPresenterType(popupViewClass
);
142 P presenter
= (P
) presenterClass
.newInstance();
144 injectPresenterBeans(presenterClass
, presenter
);
146 PopupView view
= popupViewClass
.newInstance();
147 if(AbstractView
.class.isAssignableFrom(popupViewClass
)){
148 AbstractView
<P
> abstractView
= (AbstractView
<P
>)view
;
149 viewEventBusField
.set(abstractView
, eventBus
);
150 viewInjectPresenterMethod
.invoke(abstractView
, presenter
);
151 // invoke the @PostConstruct method
153 viewInitMethod
.invoke(abstractView
);
156 } catch (InstantiationException
| IllegalAccessException
| IllegalArgumentException
| InvocationTargetException e
) {
157 throw new RuntimeException(String
.format("Error creating the view class '%s' with presenter class '%s'", popupViewClass
, presenterClass
), e
);
163 * @param presenterClass
165 * @throws IllegalAccessException
166 * @throws InvocationTargetException
167 * @throws IllegalArgumentException
169 public <P
extends AbstractPresenter
> void injectPresenterBeans(
170 Class
<?
extends AbstractPresenter
<?
>> presenterClass
, P presenter
) throws IllegalAccessException
, IllegalArgumentException
, InvocationTargetException
{
171 presenterRepoField
.set(presenter
, repo
);
172 presenterNavigationManagerField
.set(presenter
, navigationManager
);
174 if(AbstractEditorPresenter
.class.isAssignableFrom(presenterClass
)){
175 presenterEventBusField
.set(presenter
, eventBus
);
177 if(SpecimenTypeDesignationWorkingsetEditorPresenter
.class.equals(presenterClass
)){
178 specimenTypeDesignationWorkingSetServiceField
.set(presenter
, specimenTypeDesignationWorkingSetService
);
180 if(NameTypeDesignationPresenter
.class.equals(presenterClass
)){
181 Field
.set(presenter
, registrationWorkingSetService
);
186 * @param popupViewClass
189 @SuppressWarnings("unchecked")
190 private Class
<?
extends AbstractPresenter
<?
>> findGenericPresenterType(Class
<?
extends PopupView
> popupViewClass
) {
192 ParameterizedType genericSuperClass
= (ParameterizedType
)popupViewClass
.getGenericSuperclass();
193 Type
[] typeArgs
= genericSuperClass
.getActualTypeArguments();
194 if(AbstractPopupEditor
.class.isAssignableFrom(popupViewClass
)){
195 return (Class
<?
extends AbstractPresenter
<?
>>) typeArgs
[1];
197 return (Class
<?
extends AbstractPresenter
<?
>>) typeArgs
[0];