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