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