Project

General

Profile

Download (21.1 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 static eu.etaxonomy.cdm.vaadin.component.registration.RegistrationStyles.LABEL_NOWRAP;
12

    
13
import java.util.ArrayList;
14
import java.util.Collection;
15
import java.util.Collections;
16
import java.util.EnumSet;
17
import java.util.HashMap;
18
import java.util.Iterator;
19
import java.util.List;
20
import java.util.Map;
21
import java.util.Set;
22
import java.util.Stack;
23
import java.util.UUID;
24

    
25
import org.springframework.security.core.GrantedAuthority;
26
import org.vaadin.viritin.fields.LazyComboBox;
27

    
28
import com.vaadin.navigator.View;
29
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
30
import com.vaadin.server.FontAwesome;
31
import com.vaadin.server.Page;
32
import com.vaadin.shared.ui.label.ContentMode;
33
import com.vaadin.spring.annotation.SpringView;
34
import com.vaadin.ui.Alignment;
35
import com.vaadin.ui.Button;
36
import com.vaadin.ui.CssLayout;
37
import com.vaadin.ui.GridLayout;
38
import com.vaadin.ui.HorizontalLayout;
39
import com.vaadin.ui.Label;
40
import com.vaadin.ui.Notification;
41
import com.vaadin.ui.Panel;
42
import com.vaadin.ui.UI;
43
import com.vaadin.ui.VerticalLayout;
44
import com.vaadin.ui.Window;
45
import com.vaadin.ui.themes.ValoTheme;
46

    
47
import eu.etaxonomy.cdm.api.service.dto.EntityReference;
48
import eu.etaxonomy.cdm.api.service.dto.RegistrationDTO;
49
import eu.etaxonomy.cdm.api.service.dto.RegistrationType;
50
import eu.etaxonomy.cdm.api.service.dto.TypedEntityReference;
51
import eu.etaxonomy.cdm.api.service.name.TypeDesignationSetManager.TypeDesignationWorkingSetType;
52
import eu.etaxonomy.cdm.model.name.Registration;
53
import eu.etaxonomy.cdm.model.name.RegistrationStatus;
54
import eu.etaxonomy.cdm.model.name.TaxonName;
55
import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
56
import eu.etaxonomy.cdm.vaadin.component.registration.RegistrationItem;
57
import eu.etaxonomy.cdm.vaadin.component.registration.RegistrationItemButtons;
58
import eu.etaxonomy.cdm.vaadin.component.registration.RegistrationItemNameAndTypeButtons;
59
import eu.etaxonomy.cdm.vaadin.component.registration.RegistrationItemNameAndTypeButtons.TypeDesignationWorkingSetButton;
60
import eu.etaxonomy.cdm.vaadin.component.registration.RegistrationItemsPanel;
61
import eu.etaxonomy.cdm.vaadin.component.registration.RegistrationStateLabel;
62
import eu.etaxonomy.cdm.vaadin.component.registration.RegistrationStyles;
63
import eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction.EditorActionContext;
64
import eu.etaxonomy.cdm.vaadin.event.RegistrationEditorAction;
65
import eu.etaxonomy.cdm.vaadin.event.ShowDetailsEvent;
66
import eu.etaxonomy.cdm.vaadin.event.TaxonNameEditorAction;
67
import eu.etaxonomy.cdm.vaadin.event.TypeDesignationWorkingsetEditorAction;
68
import eu.etaxonomy.cdm.vaadin.event.registration.RegistrationWorkingsetAction;
69
import eu.etaxonomy.cdm.vaadin.model.registration.RegistrationWorkingSet;
70
import eu.etaxonomy.cdm.vaadin.security.AccessRestrictedView;
71
import eu.etaxonomy.cdm.vaadin.security.PermissionDebugUtils;
72
import eu.etaxonomy.cdm.vaadin.security.UserHelper;
73
import eu.etaxonomy.cdm.vaadin.theme.EditValoTheme;
74
import eu.etaxonomy.cdm.vaadin.view.AbstractPageView;
75
import eu.etaxonomy.vaadin.event.EditorActionType;
76

    
77
/**
78
 * @author a.kohlbecker
79
 * @since Mar 2, 2017
80
 *
81
 */
82
@SpringView(name=RegistrationWorksetViewBean.NAME)
83
public class RegistrationWorksetViewBean extends AbstractPageView<RegistrationWorkingsetPresenter>
84
    implements RegistrationWorkingsetView, View, AccessRestrictedView {
85

    
86
    /**
87
     *
88
     */
89
    private static final int COL_INDEX_STATE_LABEL = 0;
90

    
91
    /**
92
     *
93
     */
94
    private static final int COL_INDEX_REG_ITEM = 1;
95

    
96
    /**
97
     *
98
     */
99
    private static final int COL_INDEX_BUTTON_GROUP = 2;
100

    
101
    public static final String DOM_ID_WORKINGSET = "workingset";
102

    
103
    private static final long serialVersionUID = -213040114015958970L;
104

    
105
    public static final String NAME = "workingset";
106

    
107
    public RegistrationType regType = null;
108

    
109
    private List<CssLayout> registrations = new ArrayList<>();
110

    
111
    private String headerText = "Registration Workingset Editor";
112
    private String subheaderText = "";
113

    
114
    private UUID citationUuid;
115

    
116
    private Button addNewNameRegistrationButton;
117

    
118
    private LazyComboBox<TaxonName> existingNameCombobox;
119

    
120
    private GridLayout registrationsGrid;
121

    
122
    private Button addExistingNameButton;
123

    
124
    private RegistrationItem workingsetHeader;
125

    
126
    private Panel registrationListPanel;
127

    
128
    /**
129
     * uses the registrationId as key
130
     */
131
    private Map<UUID, RegistrationDetailsItem> registrationItemMap = new HashMap<>();
132

    
133
    /**
134
     * uses the registrationId as key
135
     */
136
    private Map<UUID, EntityReference> typifiedNamesMap = new HashMap<>();
137

    
138
    public RegistrationWorksetViewBean() {
139
        super();
140
    }
141

    
142

    
143
    /**
144
     * {@inheritDoc}
145
     */
146
    @Override
147
    protected void initContent() {
148
        getLayout().setId(NAME);
149
        updateHeader();
150
        // all content is added in createRegistrationsList()
151
    }
152

    
153

    
154
    /**
155
     * {@inheritDoc}
156
     */
157
    @Override
158
    public void enter(ViewChangeEvent event) {
159
        if(event.getParameters() != null){
160
            this.citationUuid = UUID.fromString(event.getParameters());
161

    
162
            getPresenter().handleViewEntered();
163
        }
164
    }
165

    
166
    /**
167
     * {@inheritDoc}
168
     */
169
    @Override
170
    public void setWorkingset(RegistrationWorkingSet workingset) {
171

    
172
        if(workingsetHeader != null){
173
            getLayout().removeComponent(workingsetHeader);
174
            getLayout().removeComponent(registrationListPanel);
175
        }
176
        workingsetHeader = new RegistrationItem(workingset, this);
177
        addContentComponent(workingsetHeader, null);
178

    
179
        registrationListPanel = createRegistrationsList(workingset);
180
        registrationListPanel.setHeight("100%");
181
        registrationListPanel.setStyleName("registration-list");
182
        registrationListPanel.setCaption("Registrations");
183
        addContentComponent(registrationListPanel, 1.0f);
184

    
185
    }
186

    
187
    @Override
188
    public void setBlockingRegistrations(UUID registrationUuid, Set<RegistrationDTO> blockingRegDTOs) {
189

    
190
        RegistrationDetailsItem regItem = registrationItemMap.get(registrationUuid);
191

    
192
        boolean blockingRegAdded = false;
193
        for(Iterator it = regItem.itemFooter.iterator(); it.hasNext(); ){
194
            if(it.next() instanceof RegistrationItemsPanel){
195
                blockingRegAdded = true;
196
                break;
197
            }
198
        }
199
        if(!blockingRegAdded){
200
            regItem.itemFooter.addComponent(new RegistrationItemsPanel(this, "Blocked by", blockingRegDTOs));
201
        }
202
    }
203

    
204
    /**
205
     * {@inheritDoc}
206
     */
207
    @Override
208
    @Deprecated // no longer needed
209
    public void addBlockingRegistration(RegistrationDTO blocking) {
210
        if(registrations == null) {
211
            throw new RuntimeException("A Workingset must be present prior adding blocking registrations.");
212
        }
213
        // add the blocking registration
214

    
215
    }
216

    
217
    /**
218
     * @param workingset
219
     * @return
220
     */
221
    public Panel createRegistrationsList(RegistrationWorkingSet workingset) {
222

    
223
        registrationsGrid = new GridLayout(3, 1);
224
        registrationsGrid.setWidth("100%");
225
        // allow vertical scrolling:
226
        registrationsGrid.setHeightUndefined();
227

    
228
        //registrationsGrid.setColumnExpandRatio(0, 0.1f);
229
        registrationsGrid.setColumnExpandRatio(1, 1f);
230

    
231
        registrationItemMap.clear();
232
        registrationsGrid.setRows(workingset.getRegistrationDTOs().size() * 2  + 2);
233
        int row = 0;
234
        for(RegistrationDTO dto : workingset.getRegistrationDTOs()) {
235
            row = putRegistrationListComponent(row, dto);
236
        }
237

    
238
        Label addRegistrationLabel_1 = new Label("Add a new registration for a");
239
        Label addRegistrationLabel_2 = new Label("or an");
240

    
241
        addNewNameRegistrationButton = new Button("new name");
242
        addNewNameRegistrationButton.setDescription("A name which is newly published in this publication.");
243
        addNewNameRegistrationButton.addClickListener(
244
                e -> getViewEventBus().publish(this, new TaxonNameEditorAction(EditorActionType.ADD, null, addNewNameRegistrationButton, this)));
245

    
246
        addExistingNameButton = new Button("existing name:");
247
        addExistingNameButton.setDescription("A name which was previously published in a earlier publication.");
248
        addExistingNameButton.setEnabled(false);
249
        addExistingNameButton.addClickListener(
250
                e -> getViewEventBus().publish(this, new RegistrationWorkingsetAction(citationUuid, RegistrationWorkingsetAction.Action.start))
251
                );
252

    
253
        existingNameCombobox = new LazyComboBox<TaxonName>(TaxonName.class);
254
        existingNameCombobox.addValueChangeListener(
255
                e -> addExistingNameButton.setEnabled(e.getProperty().getValue() != null)
256
                );
257

    
258
        HorizontalLayout buttonContainer = new HorizontalLayout(addRegistrationLabel_1, addNewNameRegistrationButton, addRegistrationLabel_2, addExistingNameButton, existingNameCombobox);
259
        buttonContainer.setSpacing(true);
260
//        buttonContainer.setWidth(100, Unit.PERCENTAGE);
261
        buttonContainer.setComponentAlignment(addRegistrationLabel_1, Alignment.MIDDLE_LEFT);
262
        buttonContainer.setComponentAlignment(addRegistrationLabel_2, Alignment.MIDDLE_LEFT);
263

    
264
        row++;
265
        registrationsGrid.addComponent(buttonContainer, 0, row, COL_INDEX_BUTTON_GROUP, row);
266
        registrationsGrid.setComponentAlignment(buttonContainer, Alignment.MIDDLE_RIGHT);
267

    
268
        Panel namesTypesPanel = new Panel(registrationsGrid);
269
        namesTypesPanel.setStyleName(EditValoTheme.PANEL_CONTENT_PADDING_LEFT);
270
        return namesTypesPanel;
271
    }
272

    
273

    
274

    
275
    protected int putRegistrationListComponent(int row, RegistrationDTO dto) {
276

    
277
        EntityReference typifiedNameReference = dto.getTypifiedNameRef();
278
        if(typifiedNameReference == null){
279
            typifiedNameReference = dto.getNameRef();
280
        }
281
        typifiedNamesMap.put(dto.getUuid(), typifiedNameReference);
282

    
283
        RegistrationItemNameAndTypeButtons regItemButtonGroup = new RegistrationItemNameAndTypeButtons(dto);
284
        UUID registrationEntityUuid = dto.getUuid();
285

    
286
        RegistrationItemButtons regItemButtons = new RegistrationItemButtons();
287

    
288
        CssLayout footer = new CssLayout();
289
        footer.setWidth(100, Unit.PERCENTAGE);
290
        footer.setStyleName("item-footer");
291

    
292
        RegistrationDetailsItem regDetailsItem = new RegistrationDetailsItem(regItemButtonGroup, regItemButtons, footer);
293
        registrationItemMap.put(registrationEntityUuid, regDetailsItem);
294

    
295
        Stack<EditorActionContext> context = new Stack<EditorActionContext>();
296
        context.push(new EditorActionContext(
297
                    new TypedEntityReference<>(Registration.class, registrationEntityUuid),
298
                    this)
299
                    );
300

    
301
        if(regItemButtonGroup.getNameButton() != null){
302
            regItemButtonGroup.getNameButton().getButton().addClickListener(e -> {
303
                UUID nameuUuid = regItemButtonGroup.getNameButton().getUuid();
304
                getViewEventBus().publish(this, new TaxonNameEditorAction(
305
                    EditorActionType.EDIT,
306
                    nameuUuid,
307
                    e.getButton(),
308
                    this,
309
                    context
310
                    )
311
                );
312
            });
313
        }
314

    
315
        for(TypeDesignationWorkingSetButton workingsetButton : regItemButtonGroup.getTypeDesignationButtons()){
316
            workingsetButton.getButton().addClickListener(e -> {
317
                TypedEntityReference baseEntityRef = workingsetButton.getBaseEntity();
318
                EntityReference typifiedNameRef = typifiedNamesMap.get(registrationEntityUuid);
319
                TypeDesignationWorkingSetType workingsetType = workingsetButton.getType();
320
                getViewEventBus().publish(this, new TypeDesignationWorkingsetEditorAction(
321
                        EditorActionType.EDIT,
322
                        baseEntityRef,
323
                        workingsetType,
324
                        registrationEntityUuid,
325
                        typifiedNameRef.getUuid(),
326
                        e.getButton(),
327
                        this,
328
                        context
329
                        )
330
                    );
331
            });
332
        }
333

    
334
        regItemButtonGroup.getAddTypeDesignationButton().addClickListener(
335
                e -> chooseNewTypeRegistrationWorkingset(dto.getUuid())
336
                );
337

    
338

    
339
        Button blockingRegistrationButton = regItemButtons.getBlockingRegistrationButton();
340
        blockingRegistrationButton.setStyleName(ValoTheme.BUTTON_TINY);
341
        blockingRegistrationButton.setDescription("No blocking registrations");
342
        if(dto.isBlocked()){
343
            blockingRegistrationButton.setEnabled(true);
344
            blockingRegistrationButton.setDescription("This registration is currently blocked by other registrations");
345
            blockingRegistrationButton.addStyleName(EditValoTheme.BUTTON_HIGHLITE);
346
            blockingRegistrationButton.addClickListener(e -> getViewEventBus().publish(
347
                    this,
348
                    new ShowDetailsEvent<RegistrationDTO, UUID>(
349
                            e,
350
                            RegistrationDTO.class,
351
                            dto.getUuid(),
352
                            RegistrationItem.BLOCKED_BY
353
                            )
354
                    ));
355
        }
356

    
357
        Button validationProblemsButton = regItemButtons.getValidationProblemsButton();
358
        validationProblemsButton.setStyleName(ValoTheme.BUTTON_TINY); //  + " " + RegistrationStyles.STYLE_FRIENDLY_FOREGROUND);
359

    
360
        if(!dto.getValidationProblems().isEmpty()){
361
            validationProblemsButton.setEnabled(true);
362
            validationProblemsButton.addClickListener(e -> getViewEventBus().publish(this,
363
                    new ShowDetailsEvent<RegistrationDTO, UUID>(
364
                        e,
365
                        RegistrationDTO.class,
366
                        dto.getUuid(),
367
                        RegistrationItem.VALIDATION_PROBLEMS
368
                        )
369
                    )
370
                );
371
        }
372
        validationProblemsButton.setCaption("<span class=\"" + RegistrationStyles.BUTTON_BADGE +"\"> " + dto.getValidationProblems().size() + "</span>");
373
        validationProblemsButton.setCaptionAsHtml(true);
374

    
375
        Button messageButton = regItemButtons.getMessagesButton();
376
        messageButton.addClickListener(e -> getViewEventBus().publish(this,
377
                    new ShowDetailsEvent<RegistrationDTO, UUID>(
378
                        e,
379
                        RegistrationDTO.class,
380
                        dto.getUuid(),
381
                        RegistrationItem.MESSAGES
382
                        )
383
                    )
384
                );
385
        messageButton.setStyleName(ValoTheme.BUTTON_TINY);
386

    
387
        RegistrationStateLabel stateLabel = new RegistrationStateLabel().update(dto.getStatus());
388
        Label submitterLabel = new Label(dto.getSubmitterUserName());
389
        submitterLabel.setStyleName(LABEL_NOWRAP + " submitter");
390
        submitterLabel.setIcon(FontAwesome.USER);
391
        submitterLabel.setContentMode(ContentMode.HTML);
392
        CssLayout stateAndSubmitter = new CssLayout(stateLabel, submitterLabel);
393

    
394

    
395
        if(UserHelper.fromSession().userIsRegistrationCurator() || UserHelper.fromSession().userIsAdmin()) {
396
            Button editRegistrationButton = new Button(FontAwesome.COG);
397
            editRegistrationButton.setStyleName(ValoTheme.BUTTON_TINY);
398
            editRegistrationButton.setDescription("Edit registration");
399
            editRegistrationButton.addClickListener(e -> getViewEventBus().publish(this, new RegistrationEditorAction(
400
                EditorActionType.EDIT,
401
                dto.getUuid(),
402
                null,
403
                this
404
                )));
405
            regItemButtons.addComponent(editRegistrationButton);
406
        }
407

    
408
        PermissionDebugUtils.addGainPerEntityPermissionButton(regItemButtons, Registration.class, dto.getUuid(),
409
                EnumSet.of(CRUD.UPDATE), RegistrationStatus.PREPARATION.name());
410

    
411
        row++;
412
        registrationsGrid.addComponent(stateAndSubmitter, COL_INDEX_STATE_LABEL, row);
413
        // registrationsGrid.setComponentAlignment(stateLabel, Alignment.TOP_LEFT);
414
        registrationsGrid.addComponent(regItemButtonGroup, COL_INDEX_REG_ITEM, row);
415
        registrationsGrid.addComponent(regItemButtons, COL_INDEX_BUTTON_GROUP, row);
416
        registrationsGrid.setComponentAlignment(regItemButtons, Alignment.TOP_LEFT);
417

    
418
        row++;
419
        registrationsGrid.addComponent(footer, 0, row, COL_INDEX_BUTTON_GROUP, row);
420

    
421
        return row;
422
    }
423

    
424
    /**
425
     * @param button
426
     * @param registrationEntityId
427
     *
428
     */
429
    @Override
430
    public void chooseNewTypeRegistrationWorkingset(UUID registrationEntityUuid){
431
        Window typeDesignationTypeCooser = new Window();
432
        typeDesignationTypeCooser.setModal(true);
433
        typeDesignationTypeCooser.setResizable(false);
434
        typeDesignationTypeCooser.setCaption("Add new type designation");
435
        Label label = new Label("Please select kind of type designation to be created.");
436
        Button newSpecimenTypeDesignationButton = new Button("Specimen type designation",
437
                e -> addNewTypeDesignationWorkingset(TypeDesignationWorkingSetType.SPECIMEN_TYPE_DESIGNATION_WORKINGSET, registrationEntityUuid, typeDesignationTypeCooser));
438
        Button newNameTypeDesignationButton = new Button("Name type designation",
439
                e -> addNewTypeDesignationWorkingset(TypeDesignationWorkingSetType.NAME_TYPE_DESIGNATION_WORKINGSET, registrationEntityUuid, typeDesignationTypeCooser));
440

    
441
        VerticalLayout layout = new VerticalLayout(label, newSpecimenTypeDesignationButton, newNameTypeDesignationButton);
442
        layout.setMargin(true);
443
        layout.setSpacing(true);
444
        layout.setComponentAlignment(newSpecimenTypeDesignationButton, Alignment.MIDDLE_CENTER);
445
        layout.setComponentAlignment(newNameTypeDesignationButton, Alignment.MIDDLE_CENTER);
446
        typeDesignationTypeCooser.setContent(layout);
447
        UI.getCurrent().addWindow(typeDesignationTypeCooser);
448
    }
449

    
450
    /**
451
     * @param button
452
     *
453
     */
454
    protected void addNewTypeDesignationWorkingset(TypeDesignationWorkingSetType newWorkingsetType, UUID registrationEntityUuid, Window typeDesignationTypeCooser) {
455
        UI.getCurrent().removeWindow(typeDesignationTypeCooser);
456
        EntityReference typifiedNameRef = typifiedNamesMap.get(registrationEntityUuid);
457
        getViewEventBus().publish(this, new TypeDesignationWorkingsetEditorAction(
458
                EditorActionType.ADD,
459
                newWorkingsetType,
460
                registrationEntityUuid,
461
                typifiedNameRef.getUuid(),
462
                null,
463
                this
464
                ));
465
    }
466

    
467
    /**
468
     * {@inheritDoc}
469
     */
470
    @Override
471
    public void openReferenceEditor(UUID referenceUuid) {
472
        // TODO Auto-generated method stub
473

    
474
    }
475

    
476
    /**
477
     * {@inheritDoc}
478
     */
479
    @Override
480
    public void openNameEditor(UUID nameUuid) {
481
        // TODO Auto-generated method stub
482

    
483
    }
484

    
485
    /**
486
     * {@inheritDoc}
487
     */
488
    @Override
489
    protected String getHeaderText() {
490
        return headerText;
491
    }
492

    
493
    /**
494
     * {@inheritDoc}
495
     */
496
    @Override
497
    public void setHeaderText(String text) {
498
        this.headerText = text;
499
        updateHeader();
500

    
501
    }
502

    
503
    /**
504
     * @return the subheaderText
505
     */
506
    public String getSubheaderText() {
507
        return subheaderText;
508
    }
509

    
510
    /**
511
     * {@inheritDoc}
512
     */
513
    @Override
514
    public void setSubheaderText(String text) {
515
        subheaderText = text;
516
        updateHeader();
517
    }
518

    
519
    /**
520
     * {@inheritDoc}
521
     */
522
    @Override
523
    protected String getSubHeaderText() {
524
        return subheaderText;
525
    }
526

    
527
    /**
528
     * {@inheritDoc}
529
     */
530
    @Override
531
    public void openDetailsPopup(String caption, List<String> messages) {
532
        StringBuffer sb = new StringBuffer();
533
        sb.append("<div class=\"details-popup-content\">");
534
        messages.forEach(s -> sb.append(s).append("</br>"));
535
        sb.append("</div>");
536
        new Notification(caption, sb.toString(), Notification.Type.HUMANIZED_MESSAGE, true).show(Page.getCurrent());
537
    }
538

    
539
    /**
540
     * {@inheritDoc}
541
     */
542
    @Override
543
    public boolean allowAnonymousAccess() {
544
        return false;
545
    }
546

    
547
    /**
548
     * {@inheritDoc}
549
     */
550
    @Override
551
    public Collection<Collection<GrantedAuthority>> allowedGrantedAuthorities() {
552
        return null;
553
    }
554

    
555
    /**
556
     * @return the addNewNameRegistrationButton
557
     */
558
    @Override
559
    public Button getAddNewNameRegistrationButton() {
560
        return addNewNameRegistrationButton;
561
    }
562

    
563
    @Override
564
    public Button getAddExistingNameRegistrationButton() {
565
        return addExistingNameButton;
566
    }
567

    
568
    @Override
569
    public LazyComboBox<TaxonName> getAddExistingNameCombobox() {
570
        return existingNameCombobox;
571
    }
572

    
573
    /**
574
     * @return the citationID
575
     */
576
    @Override
577
    public UUID getCitationUuid() {
578
        return citationUuid;
579
    }
580

    
581
    @Override
582
    public Map<UUID, RegistrationDetailsItem> getRegistrationItemMap(){
583
        return Collections.unmodifiableMap(registrationItemMap);
584
    }
585

    
586

    
587
}
(15-15/20)