Project

General

Profile

Download (16.6 KB) Statistics
| Branch: | Tag: | Revision:
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.ArrayList;
12
import java.util.Collection;
13
import java.util.EnumSet;
14
import java.util.List;
15
import java.util.Stack;
16
import java.util.UUID;
17

    
18
import org.springframework.security.core.GrantedAuthority;
19
import org.vaadin.viritin.fields.LazyComboBox;
20

    
21
import com.vaadin.navigator.View;
22
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
23
import com.vaadin.server.FontAwesome;
24
import com.vaadin.server.Page;
25
import com.vaadin.spring.annotation.SpringView;
26
import com.vaadin.ui.Alignment;
27
import com.vaadin.ui.Button;
28
import com.vaadin.ui.Component;
29
import com.vaadin.ui.CssLayout;
30
import com.vaadin.ui.GridLayout;
31
import com.vaadin.ui.HorizontalLayout;
32
import com.vaadin.ui.Label;
33
import com.vaadin.ui.Notification;
34
import com.vaadin.ui.Panel;
35
import com.vaadin.ui.UI;
36
import com.vaadin.ui.VerticalLayout;
37
import com.vaadin.ui.Window;
38
import com.vaadin.ui.themes.ValoTheme;
39

    
40
import eu.etaxonomy.cdm.model.name.Registration;
41
import eu.etaxonomy.cdm.model.name.RegistrationStatus;
42
import eu.etaxonomy.cdm.model.name.TaxonName;
43
import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
44
import eu.etaxonomy.cdm.vaadin.component.registration.RegistrationItem;
45
import eu.etaxonomy.cdm.vaadin.component.registration.RegistrationItemEditButtonGroup;
46
import eu.etaxonomy.cdm.vaadin.component.registration.RegistrationItemEditButtonGroup.TypeDesignationWorkingSetButton;
47
import eu.etaxonomy.cdm.vaadin.component.registration.RegistrationStateLabel;
48
import eu.etaxonomy.cdm.vaadin.component.registration.RegistrationStyles;
49
import eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction.EditorActionContext;
50
import eu.etaxonomy.cdm.vaadin.event.RegistrationEditorAction;
51
import eu.etaxonomy.cdm.vaadin.event.ShowDetailsEvent;
52
import eu.etaxonomy.cdm.vaadin.event.TaxonNameEditorAction;
53
import eu.etaxonomy.cdm.vaadin.event.TypeDesignationWorkingsetEditorAction;
54
import eu.etaxonomy.cdm.vaadin.event.registration.RegistrationWorkingsetAction;
55
import eu.etaxonomy.cdm.vaadin.model.TypedEntityReference;
56
import eu.etaxonomy.cdm.vaadin.model.registration.RegistrationWorkingSet;
57
import eu.etaxonomy.cdm.vaadin.security.AccessRestrictedView;
58
import eu.etaxonomy.cdm.vaadin.security.PermissionDebugUtils;
59
import eu.etaxonomy.cdm.vaadin.security.UserHelper;
60
import eu.etaxonomy.cdm.vaadin.util.converter.TypeDesignationSetManager.TypeDesignationWorkingSetType;
61
import eu.etaxonomy.cdm.vaadin.view.AbstractPageView;
62
import eu.etaxonomy.vaadin.event.EditorActionType;
63

    
64
/**
65
 * @author a.kohlbecker
66
 * @since Mar 2, 2017
67
 *
68
 */
69
@SpringView(name=RegistrationWorksetViewBean.NAME)
70
public class RegistrationWorksetViewBean extends AbstractPageView<RegistrationWorkingsetPresenter>
71
    implements RegistrationWorkingsetView, View, AccessRestrictedView {
72

    
73
    public static final String DOM_ID_WORKINGSET = "workingset";
74

    
75
    private static final long serialVersionUID = -213040114015958970L;
76

    
77
    public static final String NAME = "workingset";
78

    
79
    public RegistrationType regType = null;
80

    
81
    private List<CssLayout> registrations = new ArrayList<>();
82

    
83
    private String headerText = "Registration Workingset Editor";
84
    private String subheaderText = "";
85

    
86
    private Integer citationID;
87

    
88
    private Button addNewNameRegistrationButton;
89

    
90
    private LazyComboBox<TaxonName> existingNameCombobox;
91

    
92
    private GridLayout registrationsGrid;
93

    
94
    private Button addExistingNameButton;
95

    
96
    private RegistrationItem workingsetHeader;
97

    
98
    private Panel registrationListPanel;
99

    
100
    public RegistrationWorksetViewBean() {
101
        super();
102
    }
103

    
104

    
105
    /**
106
     * {@inheritDoc}
107
     */
108
    @Override
109
    protected void initContent() {
110
        getLayout().setId(NAME);
111
        updateHeader();
112
        // all content is added in createRegistrationsList()
113
    }
114

    
115

    
116
    /**
117
     * {@inheritDoc}
118
     */
119
    @Override
120
    public void enter(ViewChangeEvent event) {
121
        if(event.getParameters() != null){
122
            this.citationID = Integer.valueOf(event.getParameters());
123

    
124
            getPresenter().handleViewEntered();
125
        }
126
    }
127

    
128
    /**
129
     * {@inheritDoc}
130
     */
131
    @Override
132
    public void setWorkingset(RegistrationWorkingSet workingset) {
133

    
134
        if(workingsetHeader != null){
135
            getLayout().removeComponent(workingsetHeader);
136
            getLayout().removeComponent(registrationListPanel);
137
        }
138
        workingsetHeader = new RegistrationItem(workingset, this);
139
        if(UserHelper.fromSession().userIsRegistrationCurator() || UserHelper.fromSession().userIsAdmin()){
140
            workingsetHeader.getSubmitterLabel().setVisible(true);
141
        }
142
        addContentComponent(workingsetHeader, null);
143

    
144
        registrationListPanel = createRegistrationsList(workingset);
145
        registrationListPanel.setHeight("100%");
146
        registrationListPanel.setStyleName("registration-list");
147
        registrationListPanel.setCaption("Registrations");
148
        addContentComponent(registrationListPanel, 1.0f);
149

    
150
    }
151

    
152
    /**
153
     * {@inheritDoc}
154
     */
155
    @Override
156
    public void addBlockingRegistration(RegistrationDTO blocking) {
157
        if(registrations == null) {
158
            throw new RuntimeException("A Workingset must be present prior adding blocking registrations.");
159
        }
160
        // add the blocking registration
161

    
162
    }
163

    
164
    /**
165
     * @param workingset
166
     * @return
167
     */
168
    public Panel createRegistrationsList(RegistrationWorkingSet workingset) {
169

    
170
        registrationsGrid = new GridLayout(3, 1);
171
        registrationsGrid.setWidth("100%");
172
        // allow vertical scrolling:
173
        registrationsGrid.setHeightUndefined();
174

    
175
        //registrationsGrid.setColumnExpandRatio(0, 0.1f);
176
        registrationsGrid.setColumnExpandRatio(1, 1f);
177

    
178
        int row = 0;
179
        for(RegistrationDTO dto : workingset.getRegistrationDTOs()) {
180
            putRegistrationListComponent(registrationsGrid, row++, dto);
181
        }
182

    
183
        Label addRegistrationLabel_1 = new Label("Add a new registration for a");
184
        Label addRegistrationLabel_2 = new Label("or an");
185

    
186
        addNewNameRegistrationButton = new Button("new name");
187
        addNewNameRegistrationButton.setDescription("A name which is newly published in this publication.");
188
        addNewNameRegistrationButton.addClickListener(
189
                e -> getViewEventBus().publish(this, new TaxonNameEditorAction(EditorActionType.ADD, null, addNewNameRegistrationButton, this))
190
                );
191

    
192
        addExistingNameButton = new Button("existing name:");
193
        addExistingNameButton.setDescription("A name which was previously published in a earlier publication.");
194
        addExistingNameButton.setEnabled(false);
195
        addExistingNameButton.addClickListener(
196
                e -> getViewEventBus().publish(this, new RegistrationWorkingsetAction(citationID, RegistrationWorkingsetAction.Action.start))
197
                );
198

    
199
        existingNameCombobox = new LazyComboBox<TaxonName>(TaxonName.class);
200
        existingNameCombobox.addValueChangeListener(
201
                e -> addExistingNameButton.setEnabled(e.getProperty().getValue() != null)
202
                );
203

    
204
        HorizontalLayout buttonContainer = new HorizontalLayout(addRegistrationLabel_1, addNewNameRegistrationButton, addRegistrationLabel_2, addExistingNameButton, existingNameCombobox);
205
        buttonContainer.setSpacing(true);
206
//        buttonContainer.setWidth(100, Unit.PERCENTAGE);
207
        buttonContainer.setComponentAlignment(addRegistrationLabel_1, Alignment.MIDDLE_LEFT);
208
        buttonContainer.setComponentAlignment(addRegistrationLabel_2, Alignment.MIDDLE_LEFT);
209
        registrationsGrid.addComponent(buttonContainer, 0, row, 2, row);
210
        registrationsGrid.setComponentAlignment(buttonContainer, Alignment.MIDDLE_RIGHT);
211

    
212
        Panel namesTypesPanel = new Panel(registrationsGrid);
213
        return namesTypesPanel;
214
    }
215

    
216

    
217
    /**
218
     * @param grid
219
     * @param row If null, the new row will be inserted as last registration item, that is before the button row.
220
     * @param dto
221
     */
222
    protected void putRegistrationListComponent(GridLayout grid, int row, RegistrationDTO dto) {
223

    
224
        grid.setRows(grid.getRows() + 1);
225

    
226
        CssLayout buttonGroup = new CssLayout();
227
        buttonGroup.setStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
228

    
229
        Button messageButton = new Button(FontAwesome.COMMENT);
230
        messageButton.setStyleName(ValoTheme.BUTTON_TINY); //  + " " + RegistrationStyles.STYLE_FRIENDLY_FOREGROUND);
231
        if(dto.getMessages().isEmpty()){
232
            messageButton.setEnabled(false);
233
        } else {
234
            messageButton.addClickListener(e -> getViewEventBus().publish(this,
235
                    new ShowDetailsEvent<RegistrationDTO, Integer>(
236
                        e,
237
                        RegistrationDTO.class,
238
                        dto.getId(),
239
                        "messages"
240
                        )
241
                    )
242
                );
243
        }
244
        messageButton.setCaption("<span class=\"" + RegistrationStyles.BUTTON_BADGE +"\"> " + dto.getMessages().size() + "</span>");
245
        messageButton.setCaptionAsHtml(true);
246
        buttonGroup.addComponent(messageButton);
247

    
248
        RegistrationStateLabel stateLabel = new RegistrationStateLabel().update(dto.getStatus());
249

    
250

    
251
        if(UserHelper.fromSession().userIsRegistrationCurator() || UserHelper.fromSession().userIsAdmin()) {
252
            Button editRegistrationButton = new Button(FontAwesome.COG);
253
            editRegistrationButton.setStyleName(ValoTheme.BUTTON_TINY);
254
            editRegistrationButton.setDescription("Edit registration");
255
            editRegistrationButton.addClickListener(e -> getViewEventBus().publish(this, new RegistrationEditorAction(
256
                EditorActionType.EDIT,
257
                dto.getId(),
258
                null,
259
                this
260
                )));
261
            buttonGroup.addComponent(editRegistrationButton);
262
        }
263

    
264
        PermissionDebugUtils.addGainPerEntityPermissionButton(buttonGroup, Registration.class, dto.getId(),
265
                EnumSet.of(CRUD.UPDATE), RegistrationStatus.PREPARATION.name());
266

    
267
        Component regItem;
268

    
269
        RegistrationItemEditButtonGroup editButtonGroup = new RegistrationItemEditButtonGroup(dto);
270
        Integer registrationEntityID = dto.getId();
271
        Stack<EditorActionContext> context = new Stack<EditorActionContext>();
272
        context.push(new EditorActionContext(
273
                    new TypedEntityReference<>(Registration.class, registrationEntityID),
274
                    this)
275
                    );
276

    
277
        if(editButtonGroup.getNameButton() != null){
278
            editButtonGroup.getNameButton().getButton().addClickListener(e -> {
279
                Integer nameId = editButtonGroup.getNameButton().getId();
280
                getViewEventBus().publish(this, new TaxonNameEditorAction(
281
                    EditorActionType.EDIT,
282
                    nameId,
283
                    e.getButton(),
284
                    this,
285
                    context
286
                    )
287
                );
288
            });
289
        }
290

    
291
        for(TypeDesignationWorkingSetButton workingsetButton : editButtonGroup.getTypeDesignationButtons()){
292
            workingsetButton.getButton().addClickListener(e -> {
293
                TypedEntityReference baseEntityRef = workingsetButton.getBaseEntity();
294
                TypeDesignationWorkingSetType workingsetType = workingsetButton.getType();
295
                getViewEventBus().publish(this, new TypeDesignationWorkingsetEditorAction(
296
                        EditorActionType.EDIT,
297
                        baseEntityRef,
298
                        workingsetType,
299
                        registrationEntityID,
300
                        e.getButton(),
301
                        this,
302
                        context
303
                        )
304
                    );
305
            });
306
        }
307

    
308
        editButtonGroup.getAddTypeDesignationButton().addClickListener(
309
                e -> chooseNewTypeRegistrationWorkingset(dto.getId())
310
                );
311
        regItem = editButtonGroup;
312

    
313

    
314
        grid.addComponent(stateLabel, 0, row);
315
        grid.setComponentAlignment(stateLabel, Alignment.TOP_LEFT);
316
        grid.addComponent(regItem, 1, row);
317
        grid.addComponent(buttonGroup, 2, row);
318
        grid.setComponentAlignment(buttonGroup, Alignment.TOP_LEFT);
319
    }
320

    
321
    /**
322
     * @param button
323
     * @param registrationEntityId
324
     *
325
     */
326
    @Override
327
    public void chooseNewTypeRegistrationWorkingset(Integer registrationEntityId) {
328

    
329
        Window typeDesignationTypeCooser = new Window();
330
        typeDesignationTypeCooser.setModal(true);
331
        typeDesignationTypeCooser.setResizable(false);
332
        typeDesignationTypeCooser.setCaption("Add new type designation");
333
        Label label = new Label("Please select kind of type designation to be created.");
334
        Button newSpecimenTypeDesignationButton = new Button("Specimen type designation",
335
                e -> addNewTypeDesignationWorkingset(TypeDesignationWorkingSetType.SPECIMEN_TYPE_DESIGNATION_WORKINGSET, registrationEntityId, typeDesignationTypeCooser));
336
        Button newNameTypeDesignationButton = new Button("Name type designation",
337
                e -> addNewTypeDesignationWorkingset(TypeDesignationWorkingSetType.NAME_TYPE_DESIGNATION_WORKINGSET, registrationEntityId, typeDesignationTypeCooser));
338

    
339
        VerticalLayout layout = new VerticalLayout(label, newSpecimenTypeDesignationButton, newNameTypeDesignationButton);
340
        layout.setMargin(true);
341
        layout.setSpacing(true);
342
        layout.setComponentAlignment(newSpecimenTypeDesignationButton, Alignment.MIDDLE_CENTER);
343
        layout.setComponentAlignment(newNameTypeDesignationButton, Alignment.MIDDLE_CENTER);
344
        typeDesignationTypeCooser.setContent(layout);
345
        UI.getCurrent().addWindow(typeDesignationTypeCooser);
346
    }
347

    
348
    /**
349
     * @param button
350
     *
351
     */
352
    protected void addNewTypeDesignationWorkingset(TypeDesignationWorkingSetType newWorkingsetType, Integer registrationEntityId, Window typeDesignationTypeCooser) {
353
        UI.getCurrent().removeWindow(typeDesignationTypeCooser);
354
        getViewEventBus().publish(this, new TypeDesignationWorkingsetEditorAction(
355
                EditorActionType.ADD,
356
                newWorkingsetType,
357
                registrationEntityId,
358
                null,
359
                this
360
                ));
361
    }
362

    
363
    /**
364
     * {@inheritDoc}
365
     */
366
    @Override
367
    public void openReferenceEditor(UUID referenceUuid) {
368
        // TODO Auto-generated method stub
369

    
370
    }
371

    
372
    /**
373
     * {@inheritDoc}
374
     */
375
    @Override
376
    public void openNameEditor(UUID nameUuid) {
377
        // TODO Auto-generated method stub
378

    
379
    }
380

    
381
    /**
382
     * {@inheritDoc}
383
     */
384
    @Override
385
    protected String getHeaderText() {
386
        return headerText;
387
    }
388

    
389
    /**
390
     * {@inheritDoc}
391
     */
392
    @Override
393
    public void setHeaderText(String text) {
394
        this.headerText = text;
395
        updateHeader();
396

    
397
    }
398

    
399
    /**
400
     * @return the subheaderText
401
     */
402
    public String getSubheaderText() {
403
        return subheaderText;
404
    }
405

    
406
    /**
407
     * {@inheritDoc}
408
     */
409
    @Override
410
    public void setSubheaderText(String text) {
411
        subheaderText = text;
412
        updateHeader();
413
    }
414

    
415
    /**
416
     * {@inheritDoc}
417
     */
418
    @Override
419
    protected String getSubHeaderText() {
420
        return subheaderText;
421
    }
422

    
423
    /**
424
     * {@inheritDoc}
425
     */
426
    @Override
427
    public void openDetailsPopup(String caption, List<String> messages) {
428
        StringBuffer sb = new StringBuffer();
429
        sb.append("<div class=\"details-popup-content\">");
430
        messages.forEach(s -> sb.append(s).append("</br>"));
431
        sb.append("</div>");
432
        new Notification(caption, sb.toString(), Notification.Type.HUMANIZED_MESSAGE, true).show(Page.getCurrent());
433
    }
434

    
435
    /**
436
     * {@inheritDoc}
437
     */
438
    @Override
439
    public boolean allowAnonymousAccess() {
440
        return false;
441
    }
442

    
443
    /**
444
     * {@inheritDoc}
445
     */
446
    @Override
447
    public Collection<Collection<GrantedAuthority>> allowedGrantedAuthorities() {
448
        return null;
449
    }
450

    
451
    /**
452
     * @return the addNewNameRegistrationButton
453
     */
454
    @Override
455
    public Button getAddNewNameRegistrationButton() {
456
        return addNewNameRegistrationButton;
457
    }
458

    
459
    @Override
460
    public Button getAddExistingNameRegistrationButton() {
461
        return addExistingNameButton;
462
    }
463

    
464
    @Override
465
    public LazyComboBox<TaxonName> getAddExistingNameCombobox() {
466
        return existingNameCombobox;
467
    }
468

    
469
    /**
470
     * @return the citationID
471
     */
472
    @Override
473
    public Integer getCitationID() {
474
        return citationID;
475
    }
476

    
477

    
478
}
(14-14/19)