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.cdm.vaadin.event;
|
10
|
|
11
|
import com.vaadin.data.Property.ValueChangeEvent;
|
12
|
import com.vaadin.data.Property.ValueChangeListener;
|
13
|
import com.vaadin.ui.Field;
|
14
|
|
15
|
import eu.etaxonomy.cdm.cache.EntityCache;
|
16
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
17
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
18
|
import eu.etaxonomy.cdm.vaadin.view.name.CachingPresenter;
|
19
|
|
20
|
/**
|
21
|
*
|
22
|
* @author a.kohlbecker
|
23
|
* @since 19.10.2017
|
24
|
*
|
25
|
*/
|
26
|
public class ToOneRelatedEntityReloader<CDM extends CdmBase> implements ValueChangeListener {
|
27
|
|
28
|
private static final long serialVersionUID = -1141764783149024788L;
|
29
|
|
30
|
Field<CDM> toOneRelatedEntityField;
|
31
|
|
32
|
CachingPresenter cachingPresenter;
|
33
|
|
34
|
public ToOneRelatedEntityReloader( Field<CDM> toOneRelatedEntityField, CachingPresenter entityCache){
|
35
|
this.toOneRelatedEntityField = toOneRelatedEntityField;
|
36
|
this.cachingPresenter = entityCache;
|
37
|
}
|
38
|
|
39
|
/**
|
40
|
* {@inheritDoc}
|
41
|
*/
|
42
|
@Override
|
43
|
public void valueChange(ValueChangeEvent event) {
|
44
|
|
45
|
|
46
|
CDM value = (CDM)event.getProperty().getValue();
|
47
|
if(value == null) {
|
48
|
return;
|
49
|
}
|
50
|
value = HibernateProxyHelper.deproxy(value);
|
51
|
|
52
|
EntityCache cache = cachingPresenter.getCache();
|
53
|
if(cache != null){
|
54
|
cache.update();
|
55
|
CDM cachedEntity = cache.find(value);
|
56
|
if(cachedEntity == null){
|
57
|
cache.add(value);
|
58
|
} else if(
|
59
|
// pure object comparison is not reliable since the entity may have been changed
|
60
|
cachedEntity.getId() == value.getId() && cachedEntity.getClass() == value.getClass()){
|
61
|
toOneRelatedEntityField.removeValueChangeListener(this);
|
62
|
toOneRelatedEntityField.setValue(null); // reset to trick equals check in vaadin
|
63
|
toOneRelatedEntityField.setValue(cachedEntity);
|
64
|
toOneRelatedEntityField.addValueChangeListener(this);
|
65
|
|
66
|
}
|
67
|
}
|
68
|
}
|
69
|
|
70
|
}
|