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.registration;
|
10
|
|
11
|
import java.util.Collection;
|
12
|
import java.util.EnumSet;
|
13
|
import java.util.Set;
|
14
|
import java.util.UUID;
|
15
|
import java.util.stream.Collectors;
|
16
|
|
17
|
import org.hibernate.criterion.Criterion;
|
18
|
import org.hibernate.criterion.Restrictions;
|
19
|
import org.springframework.beans.factory.annotation.Autowired;
|
20
|
import org.vaadin.spring.events.EventScope;
|
21
|
import org.vaadin.spring.events.annotation.EventBusListenerMethod;
|
22
|
import org.vaadin.viritin.fields.LazyComboBox;
|
23
|
|
24
|
import com.vaadin.server.SystemError;
|
25
|
import com.vaadin.server.UserError;
|
26
|
import com.vaadin.spring.annotation.SpringComponent;
|
27
|
import com.vaadin.spring.annotation.ViewScope;
|
28
|
|
29
|
import eu.etaxonomy.cdm.api.service.DeleteResult;
|
30
|
import eu.etaxonomy.cdm.api.service.dto.RegistrationDTO;
|
31
|
import eu.etaxonomy.cdm.format.ReferenceEllypsisFormatter;
|
32
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
33
|
import eu.etaxonomy.cdm.model.reference.ReferenceType;
|
34
|
import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
|
35
|
import eu.etaxonomy.cdm.persistence.hibernate.permission.CdmAuthority;
|
36
|
import eu.etaxonomy.cdm.persistence.hibernate.permission.Operation;
|
37
|
import eu.etaxonomy.cdm.persistence.query.MatchMode;
|
38
|
import eu.etaxonomy.cdm.ref.TypedEntityReference;
|
39
|
import eu.etaxonomy.cdm.service.CdmFilterablePagingProviderFactory;
|
40
|
import eu.etaxonomy.cdm.service.TypifiedEntityFilterablePagingProvider;
|
41
|
import eu.etaxonomy.cdm.service.UserHelperAccess;
|
42
|
import eu.etaxonomy.cdm.vaadin.event.EditorActionTypeFilter;
|
43
|
import eu.etaxonomy.cdm.vaadin.event.ReferenceEditorAction;
|
44
|
import eu.etaxonomy.cdm.vaadin.event.RegistrationEditorAction;
|
45
|
import eu.etaxonomy.cdm.vaadin.ui.RegistrationUIDefaults;
|
46
|
import eu.etaxonomy.cdm.vaadin.view.reference.ReferencePopupEditor;
|
47
|
import eu.etaxonomy.vaadin.mvp.AbstractEditorPresenter;
|
48
|
import eu.etaxonomy.vaadin.ui.navigation.NavigationEvent;
|
49
|
import eu.etaxonomy.vaadin.ui.view.DoneWithPopupEvent;
|
50
|
import eu.etaxonomy.vaadin.ui.view.DoneWithPopupEvent.Reason;
|
51
|
|
52
|
/**
|
53
|
* @author a.kohlbecker
|
54
|
* @since Jul 11, 2017
|
55
|
*
|
56
|
*/
|
57
|
@SpringComponent
|
58
|
@ViewScope
|
59
|
public class StartRegistrationPresenter extends AbstractEditorPresenter<RegistrationDTO, StartRegistrationView> {
|
60
|
|
61
|
private static final long serialVersionUID = 2283189121081612574L;
|
62
|
|
63
|
private ReferencePopupEditor newReferencePopup;
|
64
|
|
65
|
private Reference newReference;
|
66
|
|
67
|
private boolean registrationInProgress;
|
68
|
|
69
|
@Autowired
|
70
|
protected CdmFilterablePagingProviderFactory pagingProviderFactory;
|
71
|
|
72
|
private TypifiedEntityFilterablePagingProvider<Reference> referencePagingProvider;
|
73
|
|
74
|
public StartRegistrationPresenter (){
|
75
|
super();
|
76
|
}
|
77
|
|
78
|
|
79
|
/**
|
80
|
* {@inheritDoc}
|
81
|
*/
|
82
|
@Override
|
83
|
public void handleViewEntered() {
|
84
|
|
85
|
super.handleViewEntered();
|
86
|
|
87
|
referencePagingProvider = pagingProviderFactory.referenceEntityReferencePagingProvider(
|
88
|
new ReferenceEllypsisFormatter(ReferenceEllypsisFormatter.LabelType.BIBLIOGRAPHIC),
|
89
|
ReferenceEllypsisFormatter.INIT_STRATEGY
|
90
|
);
|
91
|
TypedEntityCaptionGenerator<Reference> titleCacheGenrator = new TypedEntityCaptionGenerator<Reference>();
|
92
|
// referencePagingProvider.addRestriction(new Restriction("type", Operator.AND_NOT, null, ReferenceType.Section, ReferenceType.Journal, ReferenceType.PrintSeries));
|
93
|
Criterion criterion = Restrictions.not(Restrictions.or(Restrictions.in("type", new ReferenceType[]{ReferenceType.Section, ReferenceType.Journal, ReferenceType.PrintSeries})));
|
94
|
|
95
|
if(!UserHelperAccess.userHelper().userIsAdmin()){
|
96
|
Collection<CdmAuthority> referencePermissions = UserHelperAccess.userHelper().findUserPermissions(Reference.class, Operation.UPDATE);
|
97
|
boolean generalUpdatePermission = referencePermissions.stream().anyMatch(p -> p.getTargetUUID() == null);
|
98
|
if(!generalUpdatePermission){
|
99
|
// restrict by allowed reference uuids
|
100
|
Set<UUID> allowedUuids = referencePermissions.stream().filter(p -> p.getTargetUUID() != null).map(CdmAuthority::getTargetUUID).collect(Collectors.toSet());
|
101
|
Criterion uuidRestriction = Restrictions.in("uuid", allowedUuids);
|
102
|
criterion = Restrictions.and(criterion, uuidRestriction);
|
103
|
}
|
104
|
}
|
105
|
referencePagingProvider.addCriterion(criterion);
|
106
|
getView().getReferenceCombobox().setCaptionGenerator(titleCacheGenrator);
|
107
|
getView().getReferenceCombobox().loadFrom(referencePagingProvider, referencePagingProvider, referencePagingProvider.getPageSize());
|
108
|
}
|
109
|
|
110
|
/**
|
111
|
* @param value
|
112
|
* @return
|
113
|
*/
|
114
|
public void updateReferenceSearchMode(MatchMode value) {
|
115
|
if(referencePagingProvider != null && value != null){
|
116
|
referencePagingProvider.setMatchMode(value);
|
117
|
getView().getReferenceCombobox().refresh();
|
118
|
}
|
119
|
}
|
120
|
|
121
|
/**
|
122
|
* {@inheritDoc}
|
123
|
*/
|
124
|
@Override
|
125
|
public void handleViewExit() {
|
126
|
if(!registrationInProgress && newReference != null){
|
127
|
logger.info("Deleting newly created Reference due to canceled registration");
|
128
|
getRepo().getReferenceService().delete(newReference);
|
129
|
}
|
130
|
super.handleViewExit();
|
131
|
}
|
132
|
|
133
|
|
134
|
@EventBusListenerMethod(filter = EditorActionTypeFilter.Add.class)
|
135
|
public void onReferenceEditorActionAdd(ReferenceEditorAction event) {
|
136
|
|
137
|
if(getView() == null || getView().getNewPublicationButton() != event.getSource()){
|
138
|
return;
|
139
|
}
|
140
|
|
141
|
newReferencePopup = openPopupEditor(ReferencePopupEditor.class, event);
|
142
|
EnumSet<ReferenceType> refTypes = RegistrationUIDefaults.PRINTPUB_REFERENCE_TYPES.clone();
|
143
|
refTypes.remove(ReferenceType.Section);
|
144
|
newReferencePopup.withReferenceTypes(refTypes);
|
145
|
|
146
|
newReferencePopup.grantToCurrentUser(EnumSet.of(CRUD.UPDATE, CRUD.DELETE));
|
147
|
newReferencePopup.withDeleteButton(true);
|
148
|
newReferencePopup.loadInEditor(null);
|
149
|
newReferencePopup.getTypeSelect().setValue(ReferenceType.Article);
|
150
|
}
|
151
|
|
152
|
@EventBusListenerMethod(filter = EditorActionTypeFilter.Remove.class)
|
153
|
public void onReferenceEditorActionRemove(ReferenceEditorAction event) {
|
154
|
|
155
|
if(getView().getRemoveNewPublicationButton() != event.getSource()){
|
156
|
return;
|
157
|
}
|
158
|
DeleteResult result = getRepo().getReferenceService().delete(newReference);
|
159
|
if(!result.isOk()){
|
160
|
String message = "";
|
161
|
for(Exception e : result.getExceptions()){
|
162
|
message += e.getMessage() + "\n" + e.getStackTrace().toString() + "\n";
|
163
|
}
|
164
|
getView().getRemoveNewPublicationButton().setComponentError(new SystemError(message));
|
165
|
}
|
166
|
|
167
|
getView().getReferenceCombobox().setEnabled(false);
|
168
|
|
169
|
getView().getRemoveNewPublicationButton().setVisible(false);
|
170
|
|
171
|
getView().getNewPublicationButton().setVisible(true);
|
172
|
getView().getNewPublicationLabel().setCaption(null);
|
173
|
getView().getNewPublicationLabel().setVisible(false);
|
174
|
}
|
175
|
|
176
|
@EventBusListenerMethod
|
177
|
public void onDoneWithPopupEvent(DoneWithPopupEvent event){
|
178
|
|
179
|
if(event.getPopup() == newReferencePopup){
|
180
|
if(event.getReason() == Reason.SAVE){
|
181
|
|
182
|
newReference = newReferencePopup.getBean();
|
183
|
|
184
|
// TODO the bean contained in the popup editor is not yet updated at this point.
|
185
|
// so re reload it using the uuid since new beans will not have an Id at this point.
|
186
|
newReference = getRepo().getReferenceService().find(newReference.getUuid());
|
187
|
|
188
|
getView().getReferenceCombobox().setValue(null); // deselect
|
189
|
getView().getReferenceCombobox().setEnabled(false);
|
190
|
|
191
|
getView().getContinueButton().setEnabled(true);
|
192
|
|
193
|
getView().getNewPublicationButton().setVisible(false);
|
194
|
|
195
|
getView().getRemoveNewPublicationButton().setVisible(true);
|
196
|
getView().getNewPublicationLabel().setCaption(newReference.getTitleCache());
|
197
|
getView().getNewPublicationLabel().setVisible(true);
|
198
|
}
|
199
|
|
200
|
newReferencePopup = null;
|
201
|
}
|
202
|
}
|
203
|
|
204
|
@SuppressWarnings("null")
|
205
|
@EventBusListenerMethod(filter = EditorActionTypeFilter.Add.class)
|
206
|
public void onRegistrationEditorActionAdd(RegistrationEditorAction event) {
|
207
|
|
208
|
if(getView().getContinueButton() != event.getSource()){
|
209
|
return;
|
210
|
}
|
211
|
|
212
|
UUID referenceUuid = null;
|
213
|
LazyComboBox<TypedEntityReference<Reference>> referenceCombobox = getView().getReferenceCombobox();
|
214
|
referenceCombobox.commit();
|
215
|
if(newReference != null){
|
216
|
referenceUuid = newReference.getUuid();
|
217
|
// } else if(referenceCombobox.getValue() != null) {
|
218
|
} else if ( event.getEntityUuid() != null) { // HACKED, see view implementation
|
219
|
referenceUuid = event.getEntityUuid();
|
220
|
}
|
221
|
if(referenceUuid == null){
|
222
|
getView().getContinueButton().setComponentError(new UserError("Can't continue. No Reference is chosen."));
|
223
|
getView().getContinueButton().setEnabled(false);
|
224
|
}
|
225
|
registrationInProgress = true;
|
226
|
viewEventBus.publish(EventScope.UI, this, new NavigationEvent(RegistrationWorksetViewBean.NAME, referenceUuid.toString()));
|
227
|
|
228
|
}
|
229
|
|
230
|
/**
|
231
|
* {@inheritDoc}
|
232
|
*/
|
233
|
@Override
|
234
|
protected RegistrationDTO loadBeanById(Object identifier) {
|
235
|
// not needed //
|
236
|
return null;
|
237
|
}
|
238
|
|
239
|
/**
|
240
|
* {@inheritDoc}
|
241
|
*/
|
242
|
@Override
|
243
|
protected void saveBean(RegistrationDTO bean) {
|
244
|
// not needed //
|
245
|
}
|
246
|
|
247
|
/**
|
248
|
* {@inheritDoc}
|
249
|
*/
|
250
|
@Override
|
251
|
protected void deleteBean(RegistrationDTO bean) {
|
252
|
// not needed //
|
253
|
}
|
254
|
|
255
|
}
|