Project

General

Profile

Revision 5ba148ae

ID5ba148aedfd3285ffb39b04fe91a73f659e32e88
Parent 1b557cbf
Child 1a3d66bf

Added by Andreas Kohlbecker over 2 years ago

ref #6169 generic AbstractCdmEditorPresenter wihout LazyInitializationExeption problems

View differences:

src/main/java/eu/etaxonomy/cdm/vaadin/view/LoginPresenter.java
92 92
     * {@inheritDoc}
93 93
     */
94 94
    @Override
95
    public void onViewEnter() {
96
        super.onViewEnter();
95
    public void handleViewEntered() {
97 96
        // attempt to auto login
98 97
        if(StringUtils.isNotEmpty(System.getProperty(PROPNAME_USER)) && StringUtils.isNotEmpty(System.getProperty(PROPNAME_PASSWORD))){
99 98
            log.warn("Performing autologin with user " + System.getProperty(PROPNAME_USER));
src/main/java/eu/etaxonomy/cdm/vaadin/view/reference/ReferenceEditorPresenter.java
8 8
*/
9 9
package eu.etaxonomy.cdm.vaadin.view.reference;
10 10

  
11
import org.hibernate.SessionFactory;
12
import org.springframework.beans.factory.annotation.Autowired;
13
import org.springframework.transaction.TransactionStatus;
14

  
15 11
import com.vaadin.spring.annotation.SpringComponent;
16 12
import com.vaadin.spring.annotation.ViewScope;
17 13

  
18 14
import eu.etaxonomy.cdm.model.reference.Reference;
19
import eu.etaxonomy.vaadin.mvp.AbstractEditorPresenter;
15
import eu.etaxonomy.vaadin.mvp.AbstractCdmEditorPresenter;
20 16

  
21 17
/**
22 18
 * @author a.kohlbecker
......
25 21
 */
26 22
@SpringComponent
27 23
@ViewScope
28
public class ReferenceEditorPresenter extends AbstractEditorPresenter<Reference> {
24
public class ReferenceEditorPresenter extends AbstractCdmEditorPresenter<Reference> {
29 25

  
30 26

  
31 27
    private static final long serialVersionUID = -7926116447719010837L;
32 28

  
33
    @Autowired
34
    private SessionFactory factory;
35

  
36
    /**
37
     * {@inheritDoc}
38
     */
39
    @Override
40
    protected void saveBean(Reference bean) {
41
        TransactionStatus tx = getRepo().startTransaction();
42
        Reference persistedBean = getRepo().getReferenceService().merge(bean);
43
        getRepo().getReferenceService().saveOrUpdate(persistedBean);
44
        getRepo().commitTransaction(tx);
45
    }
46

  
47 29
}
src/main/java/eu/etaxonomy/cdm/vaadin/view/reference/ReferencePopupEditor.java
24 24
import eu.etaxonomy.cdm.model.reference.ReferenceType;
25 25
import eu.etaxonomy.cdm.vaadin.component.TimePeriodField;
26 26
import eu.etaxonomy.cdm.vaadin.security.AccessRestrictedView;
27
import eu.etaxonomy.vaadin.mvp.AbstractPopupEditor;
27
import eu.etaxonomy.vaadin.mvp.AbstractCdmPopupEditor;
28 28

  
29 29
/**
30 30
 * @author a.kohlbecker
......
34 34

  
35 35
@SpringComponent
36 36
@Scope("prototype")
37
public class ReferencePopupEditor extends AbstractPopupEditor<Reference, ReferenceEditorPresenter> implements ReferencePopupEditorView, AccessRestrictedView {
37
public class ReferencePopupEditor extends AbstractCdmPopupEditor<Reference, ReferenceEditorPresenter> implements ReferencePopupEditorView, AccessRestrictedView {
38 38

  
39 39
    private static final long serialVersionUID = -4347633563800758815L;
40 40

  
src/main/java/eu/etaxonomy/cdm/vaadin/view/registration/ListPresenter.java
13 13
import org.springframework.beans.factory.annotation.Autowired;
14 14
import org.springframework.beans.factory.annotation.Qualifier;
15 15
import org.springframework.context.event.EventListener;
16
import org.springframework.transaction.TransactionStatus;
16 17

  
17 18
import com.vaadin.spring.annotation.SpringComponent;
18 19
import com.vaadin.spring.annotation.ViewScope;
......
37 38
    private IRegistrationWorkingSetService workingSetService;
38 39

  
39 40
    @Override
40
    public void onViewEnter() {
41
        super.onViewEnter();
41
    public void handleViewEntered() {
42
        TransactionStatus tx = getRepo().startTransaction();
42 43
        getView().populate(listRegistrations());
44
        getRepo().commitTransaction(tx);
43 45
    }
44 46

  
45 47
    /**
src/main/java/eu/etaxonomy/vaadin/mvp/AbstractCdmEditorPresenter.java
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.mvp;
10

  
11
import org.hibernate.Session;
12
import org.springframework.context.event.EventListener;
13
import org.springframework.transaction.TransactionStatus;
14

  
15
import com.vaadin.data.fieldgroup.BeanFieldGroup;
16
import com.vaadin.data.fieldgroup.FieldGroup.CommitEvent;
17
import com.vaadin.data.util.BeanItem;
18

  
19
import eu.etaxonomy.cdm.model.common.CdmBase;
20

  
21
/**
22
 * @author a.kohlbecker
23
 * @since Apr 5, 2017
24
 *
25
 */
26
public abstract class AbstractCdmEditorPresenter<DTO extends CdmBase> extends AbstractEditorPresenter<DTO> {
27

  
28
    private static final long serialVersionUID = 2218185546277084261L;
29

  
30
    TransactionStatus tx = null;
31

  
32
    @Override
33
    @EventListener
34
    public void onEditorPreSaveEvent(EditorPreSaveEvent preSaveEvent){
35
        tx = getRepo().startTransaction(true);
36
        // merge the bean and update the fieldGroup with the merged bean, so that updating
37
        // of field values in turn of the commit are can not cause LazyInitializytionExeptions
38
        // the bean still has the original values at this point
39
        mergedBean(preSaveEvent.getCommitEvent());
40

  
41
    }
42

  
43
    @Override
44
    @EventListener
45
    public void onEditorSaveEvent(EditorSaveEvent saveEvent){
46
        // the bean is now updated with the changes made by the user
47
        // merge the bean into the session, ...
48
        DTO bean = mergedBean(saveEvent.getCommitEvent());
49
        getRepo().getCommonService().saveOrUpdate(bean);
50
        getSession().flush();
51
        getRepo().commitTransaction(tx);
52
        tx = null;
53
    }
54

  
55
    /**
56
     * Obtains the bean from the fieldGroup, merges the bean into the session and
57
     * updates the fieldGroup with the merged bean.
58
     *
59
     * @param CommitEvent
60
     * @return The bean merged to the session
61
     */
62
    private DTO mergedBean(CommitEvent commitEvent) {
63
        // using just some service to get hold of the session
64
        Session session = getSession();
65
        @SuppressWarnings("unchecked")
66
        BeanItem<DTO> itemDataSource = ((BeanFieldGroup<DTO>)commitEvent.getFieldBinder()).getItemDataSource();
67
        DTO bean = itemDataSource.getBean();
68
        @SuppressWarnings("unchecked")
69
        DTO mergedBean = (DTO) session.merge(bean);
70
        itemDataSource.setBean(mergedBean);
71
        return mergedBean;
72
    }
73

  
74
    /**
75
     * @return
76
     */
77
    private Session getSession() {
78
        return getRepo().getUserService().getSession();
79
    }
80

  
81
    @Override
82
    protected final void saveBean(DTO bean){
83
        // blank implementation, since this is not needed in this or any sub class
84
    }
85

  
86
}
src/main/java/eu/etaxonomy/vaadin/mvp/AbstractCdmPopupEditor.java
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.mvp;
10

  
11
import com.vaadin.ui.Layout;
12

  
13
import eu.etaxonomy.cdm.model.common.CdmBase;
14

  
15
/**
16
 * @author a.kohlbecker
17
 * @since May 5, 2017
18
 *
19
 */
20
public abstract class AbstractCdmPopupEditor<DTO extends CdmBase, P extends AbstractEditorPresenter<DTO>>
21
    extends AbstractPopupEditor<DTO, P> {
22

  
23
    private static final long serialVersionUID = -5025937489746256070L;
24

  
25
    /**
26
     * @param layout
27
     * @param dtoType
28
     */
29
    public AbstractCdmPopupEditor(Layout layout, Class<DTO> dtoType) {
30
        super(layout, dtoType);
31
    }
32

  
33
}
src/main/java/eu/etaxonomy/vaadin/mvp/AbstractEditorPresenter.java
19 19
 */
20 20
public abstract class AbstractEditorPresenter<DTO extends Object> extends AbstractPresenter {
21 21

  
22
    @SuppressWarnings("unchecked")
22

  
23
    private static final long serialVersionUID = -6677074110764145236L;
24

  
25
    @EventListener
26
    public void onEditorPreSaveEvent(EditorPreSaveEvent preSaveEvent){
27

  
28
    }
29

  
30
    /**
31
     *
32
     * @param saveEvent
33
     */
23 34
    @EventListener
24 35
    public void onEditorSaveEvent(EditorSaveEvent saveEvent){
25
        // casting to BeanFieldGroup<DTO> must be possible here!
26 36
        DTO bean = ((BeanFieldGroup<DTO>)saveEvent.getCommitEvent().getFieldBinder()).getItemDataSource().getBean();
27 37
        saveBean(bean);
28 38
    }
src/main/java/eu/etaxonomy/vaadin/mvp/AbstractPopupEditor.java
141 141

  
142 142
        @Override
143 143
        public void preCommit(CommitEvent commitEvent) throws CommitException {
144
            logger.debug("preCommit");
145
            // notify the presenter to start a transaction
146
            eventBus.publishEvent(new EditorPreSaveEvent(commitEvent));
144 147
        }
145 148

  
146 149
        @Override
147 150
        public void postCommit(CommitEvent commitEvent) throws CommitException {
148 151
            try {
149
                // notify the presenter to persist the bean
152
                // notify the presenter to persist the bean and to commit the transaction
150 153
                eventBus.publishEvent(new EditorSaveEvent(commitEvent));
151 154

  
152 155
                // notify the NavigationManagerBean to close the window and to dispose the view
......
171 174
        try {
172 175
            fieldGroup.commit();
173 176
        } catch (CommitException e) {
174
            fieldGroup.getFields().forEach(f -> ((AbstractField)f).setValidationVisible(true));
177
            fieldGroup.getFields().forEach(f -> ((AbstractField<?>)f).setValidationVisible(true));
175 178
            if(e.getCause() != null && e.getCause() instanceof FieldGroupInvalidValueException){
176 179
                FieldGroupInvalidValueException invalidValueException = (FieldGroupInvalidValueException)e.getCause();
177 180
                updateFieldNotifications(invalidValueException.getInvalidFields());
src/main/java/eu/etaxonomy/vaadin/mvp/AbstractPresenter.java
5 5
import org.apache.log4j.Logger;
6 6
import org.springframework.beans.factory.annotation.Autowired;
7 7
import org.springframework.beans.factory.annotation.Qualifier;
8
import org.springframework.transaction.TransactionStatus;
8 9

  
9 10
import com.vaadin.spring.annotation.SpringComponent;
10 11
import com.vaadin.spring.annotation.ViewScope;
......
75 76
	    logger.trace("Presenter ready");
76 77
	}
77 78

  
79
	public final void onViewEnter() {
80
	    logger.trace("View entered");
81
	    TransactionStatus tx = getRepo().startTransaction();
82
	    handleViewEntered();
83
	    getRepo().commitTransaction(tx);
84
	}
85

  
86
	public final void onViewExit() {
87
	    handleViewExit();
88
	}
89

  
78 90
	/**
79 91
	 * Extending classes should overwrite this method to react to the event when
80 92
	 * user has navigated into the view that this presenter governs.
81 93
	 */
82
	public void onViewEnter() {
83
	    logger.trace("View entered");
94
	public void handleViewEntered() {
84 95
	}
85 96

  
86
	public void onViewExit() {
87

  
88
	}
97
    /**
98
     * Extending classes may overwrite this method to react to
99
     * the event when user leaves the view that this presenter
100
     * governs.
101
     */
102
    public void handleViewExit() {
103
    }
89 104

  
90 105
    /**
91 106
     * @return the navigationManager
src/main/java/eu/etaxonomy/vaadin/mvp/EditorPreSaveEvent.java
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.mvp;
10

  
11
import com.vaadin.data.fieldgroup.FieldGroup.CommitEvent;
12

  
13
/**
14
 * Usually a notification to a presenter to
15
 * prepare for a save operation by starting a transaction
16
 *
17
 * @author a.kohlbecker
18
 * @since Apr 5, 2017
19
 *
20
 */
21
public class EditorPreSaveEvent {
22

  
23
    private CommitEvent commitEvent;
24

  
25
    /**
26
     * @param commitEvent
27
     */
28
    public EditorPreSaveEvent(CommitEvent commitEvent) {
29
        this.commitEvent = commitEvent;
30
    }
31

  
32
    public CommitEvent getCommitEvent(){
33
        return commitEvent;
34
    }
35

  
36
}
src/main/java/eu/etaxonomy/vaadin/mvp/EditorSaveEvent.java
11 11
import com.vaadin.data.fieldgroup.FieldGroup.CommitEvent;
12 12

  
13 13
/**
14
 * Usually a notification to a presenter to
15
 * perform a save operation and to commit the transaction
16
 *
14 17
 * @author a.kohlbecker
15 18
 * @since Apr 5, 2017
16 19
 *

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)