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.view.reference;
|
10
|
|
11
|
import java.util.ArrayList;
|
12
|
import java.util.Arrays;
|
13
|
import java.util.List;
|
14
|
import java.util.UUID;
|
15
|
|
16
|
import org.apache.log4j.Logger;
|
17
|
import org.springframework.context.annotation.Scope;
|
18
|
import org.vaadin.spring.events.annotation.EventBusListenerMethod;
|
19
|
import org.vaadin.viritin.fields.CaptionGenerator;
|
20
|
|
21
|
import com.vaadin.spring.annotation.SpringComponent;
|
22
|
|
23
|
import eu.etaxonomy.cdm.api.service.IService;
|
24
|
import eu.etaxonomy.cdm.model.agent.AgentBase;
|
25
|
import eu.etaxonomy.cdm.model.agent.Institution;
|
26
|
import eu.etaxonomy.cdm.model.agent.Person;
|
27
|
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
|
28
|
import eu.etaxonomy.cdm.model.common.Annotation;
|
29
|
import eu.etaxonomy.cdm.model.common.AnnotationType;
|
30
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
31
|
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
|
32
|
import eu.etaxonomy.cdm.service.CdmFilterablePagingProvider;
|
33
|
import eu.etaxonomy.cdm.service.UserHelperAccess;
|
34
|
import eu.etaxonomy.cdm.vaadin.component.CdmBeanItemContainerFactory;
|
35
|
import eu.etaxonomy.cdm.vaadin.event.EditorActionTypeFilter;
|
36
|
import eu.etaxonomy.cdm.vaadin.event.EntityChangeEvent;
|
37
|
import eu.etaxonomy.cdm.vaadin.event.InstitutionEditorAction;
|
38
|
import eu.etaxonomy.cdm.vaadin.event.ReferenceEditorAction;
|
39
|
import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityButtonUpdater;
|
40
|
import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityReloader;
|
41
|
import eu.etaxonomy.cdm.vaadin.view.common.InstitutionPopupEditor;
|
42
|
import eu.etaxonomy.vaadin.component.ToOneRelatedEntityField;
|
43
|
import eu.etaxonomy.vaadin.mvp.AbstractCdmEditorPresenter;
|
44
|
import eu.etaxonomy.vaadin.mvp.BeanInstantiator;
|
45
|
import eu.etaxonomy.vaadin.mvp.BoundField;
|
46
|
import eu.etaxonomy.vaadin.ui.view.PopupView;
|
47
|
|
48
|
/**
|
49
|
* @author a.kohlbecker
|
50
|
* @since Apr 5, 2017
|
51
|
*
|
52
|
*/
|
53
|
@SpringComponent
|
54
|
@Scope("prototype")
|
55
|
public class ReferenceEditorPresenter extends AbstractCdmEditorPresenter<Reference, ReferencePopupEditorView> {
|
56
|
|
57
|
private static final long serialVersionUID = -7926116447719010837L;
|
58
|
|
59
|
private static final Logger logger = Logger.getLogger(ReferenceEditorPresenter.class);
|
60
|
|
61
|
ReferencePopupEditor inReferencePopup = null;
|
62
|
|
63
|
public ReferenceEditorPresenter() {
|
64
|
|
65
|
}
|
66
|
|
67
|
|
68
|
/**
|
69
|
* {@inheritDoc}
|
70
|
*/
|
71
|
@Override
|
72
|
public void handleViewEntered() {
|
73
|
super.handleViewEntered();
|
74
|
|
75
|
getView().getInReferenceCombobox().getSelect().setCaptionGenerator(new CaptionGenerator<Reference>(){
|
76
|
|
77
|
@Override
|
78
|
public String getCaption(Reference option) {
|
79
|
return option.getTitleCache();
|
80
|
}
|
81
|
|
82
|
});
|
83
|
|
84
|
CdmBeanItemContainerFactory selectFieldFactory = new CdmBeanItemContainerFactory(getRepo());
|
85
|
CdmFilterablePagingProvider<Reference, Reference> collectionPagingProvider = pagingProviderFactory.referencePagingProvider();
|
86
|
getView().getInReferenceCombobox().loadFrom(collectionPagingProvider, collectionPagingProvider, collectionPagingProvider.getPageSize());
|
87
|
getView().getInReferenceCombobox().setNestedButtonStateUpdater(new ToOneRelatedEntityButtonUpdater<Reference>(getView().getInReferenceCombobox()));
|
88
|
getView().getInReferenceCombobox().getSelect().addValueChangeListener(new ToOneRelatedEntityReloader<Reference>(getView().getInReferenceCombobox(),this));
|
89
|
|
90
|
CdmFilterablePagingProvider<AgentBase, TeamOrPersonBase> teamOrPersonPagingProvider = new CdmFilterablePagingProvider<AgentBase, TeamOrPersonBase>(getRepo().getAgentService(), TeamOrPersonBase.class);
|
91
|
CdmFilterablePagingProvider<AgentBase, Person> personPagingProvider = new CdmFilterablePagingProvider<AgentBase, Person>(getRepo().getAgentService(), Person.class);
|
92
|
getView().getAuthorshipField().setFilterableTeamPagingProvider(teamOrPersonPagingProvider, this);
|
93
|
getView().getAuthorshipField().setFilterablePersonPagingProvider(personPagingProvider, this);
|
94
|
|
95
|
CdmFilterablePagingProvider<AgentBase, Institution> institutionPagingProvider = new CdmFilterablePagingProvider<AgentBase, Institution>(getRepo().getAgentService(), Institution.class);
|
96
|
getView().getInstitutionCombobox().getSelect().loadFrom(institutionPagingProvider, institutionPagingProvider, institutionPagingProvider.getPageSize());
|
97
|
getView().getInstitutionCombobox().getSelect().addValueChangeListener(new ToOneRelatedEntityReloader<Institution>(getView().getInstitutionCombobox(), this));
|
98
|
|
99
|
getView().getSchoolCombobox().getSelect().loadFrom(institutionPagingProvider, institutionPagingProvider, institutionPagingProvider.getPageSize());
|
100
|
getView().getSchoolCombobox().getSelect().addValueChangeListener(new ToOneRelatedEntityReloader<Institution>(getView().getSchoolCombobox(), this));
|
101
|
|
102
|
getView().getAnnotationsField().setAnnotationTypeItemContainer(selectFieldFactory.buildTermItemContainer(
|
103
|
AnnotationType.EDITORIAL().getUuid(), AnnotationType.TECHNICAL().getUuid()));
|
104
|
}
|
105
|
|
106
|
|
107
|
protected static BeanInstantiator<Reference> defaultBeanInstantiator = new BeanInstantiator<Reference>() {
|
108
|
|
109
|
@Override
|
110
|
public Reference createNewBean() {
|
111
|
return ReferenceFactory.newGeneric();
|
112
|
}
|
113
|
};
|
114
|
|
115
|
|
116
|
@Override
|
117
|
protected BeanInstantiator<Reference> defaultBeanInstantiator(){
|
118
|
return defaultBeanInstantiator;
|
119
|
}
|
120
|
|
121
|
|
122
|
/**
|
123
|
* {@inheritDoc}
|
124
|
*/
|
125
|
@Override
|
126
|
protected Reference loadCdmEntity(UUID identifier) {
|
127
|
|
128
|
List<String> initStrategy = Arrays.asList(new String []{
|
129
|
"$",
|
130
|
"annotations.*", // needed as log as we are using a table in FilterableAnnotationsField
|
131
|
}
|
132
|
);
|
133
|
|
134
|
Reference reference;
|
135
|
if(identifier != null){
|
136
|
reference = getRepo().getReferenceService().load(identifier, initStrategy);
|
137
|
} else {
|
138
|
reference = createNewBean();
|
139
|
}
|
140
|
return reference;
|
141
|
}
|
142
|
|
143
|
|
144
|
/**
|
145
|
* {@inheritDoc}
|
146
|
*/
|
147
|
@Override
|
148
|
protected void guaranteePerEntityCRUDPermissions(UUID identifier) {
|
149
|
if(crud != null){
|
150
|
newAuthorityCreated = UserHelperAccess.userHelper().createAuthorityForCurrentUser(Reference.class, identifier, crud, null);
|
151
|
}
|
152
|
|
153
|
}
|
154
|
|
155
|
/**
|
156
|
* {@inheritDoc}
|
157
|
*/
|
158
|
@Override
|
159
|
protected void guaranteePerEntityCRUDPermissions(Reference bean) {
|
160
|
if(crud != null){
|
161
|
newAuthorityCreated = UserHelperAccess.userHelper().createAuthorityForCurrentUser(bean, crud, null);
|
162
|
}
|
163
|
}
|
164
|
|
165
|
/**
|
166
|
*
|
167
|
* @param editorAction
|
168
|
* @throws EditorEntityBeanException
|
169
|
*/
|
170
|
@EventBusListenerMethod
|
171
|
public void onReferenceEditorAction(ReferenceEditorAction editorAction) {
|
172
|
|
173
|
if(!isFromOwnView(editorAction) || editorAction.getTarget() == null){
|
174
|
return;
|
175
|
}
|
176
|
|
177
|
if(ToOneRelatedEntityField.class.isAssignableFrom(editorAction.getTarget().getClass())){
|
178
|
if(editorAction.isAddAction()){
|
179
|
inReferencePopup = openPopupEditor(ReferencePopupEditor.class, editorAction);
|
180
|
inReferencePopup.loadInEditor(null);
|
181
|
}
|
182
|
if(editorAction.isEditAction()){
|
183
|
inReferencePopup = openPopupEditor(ReferencePopupEditor.class, editorAction);
|
184
|
inReferencePopup.withDeleteButton(true);
|
185
|
inReferencePopup.loadInEditor(editorAction.getEntityUuid());
|
186
|
}
|
187
|
}
|
188
|
}
|
189
|
|
190
|
@EventBusListenerMethod(filter = EditorActionTypeFilter.Edit.class)
|
191
|
public void onInstitutionEditorActionEdit(InstitutionEditorAction event) {
|
192
|
|
193
|
if(!checkFromOwnView(event)){
|
194
|
return;
|
195
|
}
|
196
|
|
197
|
InstitutionPopupEditor institutionPopuEditor = openPopupEditor(InstitutionPopupEditor.class, event);
|
198
|
|
199
|
institutionPopuEditor.grantToCurrentUser(this.crud);
|
200
|
institutionPopuEditor.withDeleteButton(true);
|
201
|
institutionPopuEditor.loadInEditor(event.getEntityUuid());
|
202
|
}
|
203
|
|
204
|
@EventBusListenerMethod(filter = EditorActionTypeFilter.Add.class)
|
205
|
public void onInstitutionEditorActionAdd(InstitutionEditorAction event) {
|
206
|
|
207
|
if(!checkFromOwnView(event)){
|
208
|
return;
|
209
|
}
|
210
|
|
211
|
InstitutionPopupEditor institutionPopuEditor = openPopupEditor(InstitutionPopupEditor.class, event);
|
212
|
|
213
|
institutionPopuEditor.grantToCurrentUser(this.crud);
|
214
|
institutionPopuEditor.withDeleteButton(false);
|
215
|
institutionPopuEditor.loadInEditor(null);
|
216
|
}
|
217
|
|
218
|
@EventBusListenerMethod
|
219
|
public void onEntityChangeEvent(EntityChangeEvent<?> event){
|
220
|
|
221
|
BoundField boundTargetField = boundTargetField((PopupView) event.getSourceView());
|
222
|
|
223
|
if(boundTargetField != null){
|
224
|
if(boundTargetField.matchesPropertyIdPath("inReference")){
|
225
|
if(event.isCreateOrModifiedType()){
|
226
|
getCache().load(event.getEntity());
|
227
|
getView().getInReferenceCombobox().reload();
|
228
|
}
|
229
|
if(event.isRemovedType()){
|
230
|
getView().getInReferenceCombobox().selectNewItem(null);
|
231
|
}
|
232
|
inReferencePopup = null;
|
233
|
}
|
234
|
else if(boundTargetField.matchesPropertyIdPath("institute")){
|
235
|
if(event.isCreateOrModifiedType()){
|
236
|
Institution newInstitution = (Institution) event.getEntity();
|
237
|
getCache().load(newInstitution);
|
238
|
if(event.isCreatedType()){
|
239
|
getView().getInstitutionCombobox().setValue(newInstitution);
|
240
|
} else {
|
241
|
getView().getInstitutionCombobox().reload();
|
242
|
}
|
243
|
}
|
244
|
} else if(boundTargetField.matchesPropertyIdPath("school")){
|
245
|
if(event.isCreateOrModifiedType()){
|
246
|
Institution newInstitution = (Institution) event.getEntity();
|
247
|
getCache().load(newInstitution);
|
248
|
if(event.isCreatedType()){
|
249
|
getView().getSchoolCombobox().setValue(newInstitution);
|
250
|
} else {
|
251
|
getView().getSchoolCombobox().reload();
|
252
|
}
|
253
|
}
|
254
|
}
|
255
|
}
|
256
|
|
257
|
}
|
258
|
|
259
|
/**
|
260
|
* {@inheritDoc}
|
261
|
*/
|
262
|
@Override
|
263
|
protected IService<Reference> getService() {
|
264
|
return getRepo().getReferenceService();
|
265
|
}
|
266
|
|
267
|
|
268
|
/**
|
269
|
* {@inheritDoc}
|
270
|
*/
|
271
|
@Override
|
272
|
protected Reference preSaveBean(Reference bean) {
|
273
|
|
274
|
// handle annotation changes
|
275
|
List<Annotation> annotations = getView().getAnnotationsField().getValue();
|
276
|
List<Annotation> currentAnnotations = new ArrayList<>(bean.getAnnotations());
|
277
|
List<Annotation> annotationsSeen = new ArrayList<>();
|
278
|
for(Annotation a : annotations){
|
279
|
if(a == null){
|
280
|
continue;
|
281
|
}
|
282
|
if(!currentAnnotations.contains(a)){
|
283
|
bean.addAnnotation(a);
|
284
|
}
|
285
|
annotationsSeen.add(a);
|
286
|
}
|
287
|
for(Annotation a : currentAnnotations){
|
288
|
if(!annotationsSeen.contains(a)){
|
289
|
bean.removeAnnotation(a);
|
290
|
}
|
291
|
}
|
292
|
|
293
|
|
294
|
return bean;
|
295
|
}
|
296
|
|
297
|
|
298
|
|
299
|
|
300
|
}
|