Project

General

Profile

Download (20 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.Collections;
14
import java.util.EnumSet;
15
import java.util.HashMap;
16
import java.util.Iterator;
17
import java.util.List;
18
import java.util.Map;
19
import java.util.Set;
20
import java.util.Stack;
21
import java.util.UUID;
22

    
23
import org.springframework.security.core.GrantedAuthority;
24
import org.vaadin.viritin.fields.LazyComboBox;
25

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

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

    
71
/**
72
 * @author a.kohlbecker
73
 * @since Mar 2, 2017
74
 *
75
 */
76
@SpringView(name=RegistrationWorksetViewBean.NAME)
77
public class RegistrationWorksetViewBean extends AbstractPageView<RegistrationWorkingsetPresenter>
78
    implements RegistrationWorkingsetView, View, AccessRestrictedView {
79

    
80
    /**
81
     *
82
     */
83
    private static final int COL_INDEX_STATE_LABEL = 0;
84

    
85
    /**
86
     *
87
     */
88
    private static final int COL_INDEX_REG_ITEM = 1;
89

    
90
    /**
91
     *
92
     */
93
    private static final int COL_INDEX_BUTTON_GROUP = 2;
94

    
95
    public static final String DOM_ID_WORKINGSET = "workingset";
96

    
97
    private static final long serialVersionUID = -213040114015958970L;
98

    
99
    public static final String NAME = "workingset";
100

    
101
    public RegistrationType regType = null;
102

    
103
    private List<CssLayout> registrations = new ArrayList<>();
104

    
105
    private String headerText = "Registration Workingset Editor";
106
    private String subheaderText = "";
107

    
108
    private Integer citationID;
109

    
110
    private Button addNewNameRegistrationButton;
111

    
112
    private LazyComboBox<TaxonName> existingNameCombobox;
113

    
114
    private GridLayout registrationsGrid;
115

    
116
    private Button addExistingNameButton;
117

    
118
    private RegistrationItem workingsetHeader;
119

    
120
    private Panel registrationListPanel;
121

    
122
    /**
123
     * uses the registrationId as key
124
     */
125
    private Map<Integer, RegistrationDetailsItem> registrationItemMap = new HashMap<>();
126

    
127
    public RegistrationWorksetViewBean() {
128
        super();
129
    }
130

    
131

    
132
    /**
133
     * {@inheritDoc}
134
     */
135
    @Override
136
    protected void initContent() {
137
        getLayout().setId(NAME);
138
        updateHeader();
139
        // all content is added in createRegistrationsList()
140
    }
141

    
142

    
143
    /**
144
     * {@inheritDoc}
145
     */
146
    @Override
147
    public void enter(ViewChangeEvent event) {
148
        if(event.getParameters() != null){
149
            this.citationID = Integer.valueOf(event.getParameters());
150

    
151
            getPresenter().handleViewEntered();
152
        }
153
    }
154

    
155
    /**
156
     * {@inheritDoc}
157
     */
158
    @Override
159
    public void setWorkingset(RegistrationWorkingSet workingset) {
160

    
161
        if(workingsetHeader != null){
162
            getLayout().removeComponent(workingsetHeader);
163
            getLayout().removeComponent(registrationListPanel);
164
        }
165
        workingsetHeader = new RegistrationItem(workingset, this);
166
        if(UserHelper.fromSession().userIsRegistrationCurator() || UserHelper.fromSession().userIsAdmin()){
167
            workingsetHeader.getSubmitterLabel().setVisible(true);
168
        }
169
        addContentComponent(workingsetHeader, null);
170

    
171
        registrationListPanel = createRegistrationsList(workingset);
172
        registrationListPanel.setHeight("100%");
173
        registrationListPanel.setStyleName("registration-list");
174
        registrationListPanel.setCaption("Registrations");
175
        addContentComponent(registrationListPanel, 1.0f);
176

    
177
    }
178

    
179
    @Override
180
    public void setBlockingRegistrations(int registrationId, Set<RegistrationDTO> blockingRegDTOs) {
181

    
182
        RegistrationDetailsItem regItem = registrationItemMap.get(registrationId);
183

    
184
        boolean blockingRegAdded = false;
185
        for(Iterator it = regItem.itemFooter.iterator(); it.hasNext(); ){
186
            if(it.next() instanceof RegistrationItemsPanel){
187
                blockingRegAdded = true;
188
                break;
189
            }
190
        }
191
        if(!blockingRegAdded){
192
            regItem.itemFooter.addComponent(new RegistrationItemsPanel(this, "Blocked by", blockingRegDTOs));
193
        }
194
    }
195

    
196
    /**
197
     * {@inheritDoc}
198
     */
199
    @Override
200
    @Deprecated // no longer needed
201
    public void addBlockingRegistration(RegistrationDTO blocking) {
202
        if(registrations == null) {
203
            throw new RuntimeException("A Workingset must be present prior adding blocking registrations.");
204
        }
205
        // add the blocking registration
206

    
207
    }
208

    
209
    /**
210
     * @param workingset
211
     * @return
212
     */
213
    public Panel createRegistrationsList(RegistrationWorkingSet workingset) {
214

    
215
        registrationsGrid = new GridLayout(3, 1);
216
        registrationsGrid.setWidth("100%");
217
        // allow vertical scrolling:
218
        registrationsGrid.setHeightUndefined();
219

    
220
        //registrationsGrid.setColumnExpandRatio(0, 0.1f);
221
        registrationsGrid.setColumnExpandRatio(1, 1f);
222

    
223
        registrationItemMap.clear();
224
        registrationsGrid.setRows(workingset.getRegistrationDTOs().size() * 2  + 2);
225
        int row = 0;
226
        for(RegistrationDTO dto : workingset.getRegistrationDTOs()) {
227
            row = putRegistrationListComponent(row, dto);
228
        }
229

    
230
        Label addRegistrationLabel_1 = new Label("Add a new registration for a");
231
        Label addRegistrationLabel_2 = new Label("or an");
232

    
233
        addNewNameRegistrationButton = new Button("new name");
234
        addNewNameRegistrationButton.setDescription("A name which is newly published in this publication.");
235
        addNewNameRegistrationButton.addClickListener(
236
                e -> getViewEventBus().publish(this, new TaxonNameEditorAction(EditorActionType.ADD, null, addNewNameRegistrationButton, this)));
237

    
238
        addExistingNameButton = new Button("existing name:");
239
        addExistingNameButton.setDescription("A name which was previously published in a earlier publication.");
240
        addExistingNameButton.setEnabled(false);
241
        addExistingNameButton.addClickListener(
242
                e -> getViewEventBus().publish(this, new RegistrationWorkingsetAction(citationID, RegistrationWorkingsetAction.Action.start))
243
                );
244

    
245
        existingNameCombobox = new LazyComboBox<TaxonName>(TaxonName.class);
246
        existingNameCombobox.addValueChangeListener(
247
                e -> addExistingNameButton.setEnabled(e.getProperty().getValue() != null)
248
                );
249

    
250
        HorizontalLayout buttonContainer = new HorizontalLayout(addRegistrationLabel_1, addNewNameRegistrationButton, addRegistrationLabel_2, addExistingNameButton, existingNameCombobox);
251
        buttonContainer.setSpacing(true);
252
//        buttonContainer.setWidth(100, Unit.PERCENTAGE);
253
        buttonContainer.setComponentAlignment(addRegistrationLabel_1, Alignment.MIDDLE_LEFT);
254
        buttonContainer.setComponentAlignment(addRegistrationLabel_2, Alignment.MIDDLE_LEFT);
255

    
256
        row++;
257
        registrationsGrid.addComponent(buttonContainer, 0, row, COL_INDEX_BUTTON_GROUP, row);
258
        registrationsGrid.setComponentAlignment(buttonContainer, Alignment.MIDDLE_RIGHT);
259

    
260
        Panel namesTypesPanel = new Panel(registrationsGrid);
261
        namesTypesPanel.setStyleName(EditValoTheme.PANEL_CONTENT_PADDING_LEFT);
262
        return namesTypesPanel;
263
    }
264

    
265

    
266

    
267
    protected int putRegistrationListComponent(int row, RegistrationDTO dto) {
268

    
269
        RegistrationItemNameAndTypeButtons regItemButtonGroup = new RegistrationItemNameAndTypeButtons(dto);
270
        Integer registrationEntityID = dto.getId();
271

    
272
        RegistrationItemButtons regItemButtons = new RegistrationItemButtons();
273

    
274
        CssLayout footer = new CssLayout();
275
        footer.setWidth(100, Unit.PERCENTAGE);
276
        footer.setStyleName("item-footer");
277

    
278
        RegistrationDetailsItem regDetailsItem = new RegistrationDetailsItem(regItemButtonGroup, regItemButtons, footer);
279
        registrationItemMap.put(registrationEntityID, regDetailsItem);
280

    
281
        Stack<EditorActionContext> context = new Stack<EditorActionContext>();
282
        context.push(new EditorActionContext(
283
                    new TypedEntityReference<>(Registration.class, registrationEntityID),
284
                    this)
285
                    );
286

    
287
        if(regItemButtonGroup.getNameButton() != null){
288
            regItemButtonGroup.getNameButton().getButton().addClickListener(e -> {
289
                Integer nameId = regItemButtonGroup.getNameButton().getId();
290
                getViewEventBus().publish(this, new TaxonNameEditorAction(
291
                    EditorActionType.EDIT,
292
                    nameId,
293
                    e.getButton(),
294
                    this,
295
                    context
296
                    )
297
                );
298
            });
299
        }
300

    
301
        for(TypeDesignationWorkingSetButton workingsetButton : regItemButtonGroup.getTypeDesignationButtons()){
302
            workingsetButton.getButton().addClickListener(e -> {
303
                TypedEntityReference baseEntityRef = workingsetButton.getBaseEntity();
304
                TypeDesignationWorkingSetType workingsetType = workingsetButton.getType();
305
                getViewEventBus().publish(this, new TypeDesignationWorkingsetEditorAction(
306
                        EditorActionType.EDIT,
307
                        baseEntityRef,
308
                        workingsetType,
309
                        registrationEntityID,
310
                        e.getButton(),
311
                        this,
312
                        context
313
                        )
314
                    );
315
            });
316
        }
317

    
318
        regItemButtonGroup.getAddTypeDesignationButton().addClickListener(
319
                e -> chooseNewTypeRegistrationWorkingset(dto.getId())
320
                );
321

    
322

    
323
        Button blockingRegistrationButton = regItemButtons.getBlockingRegistrationButton();
324
        blockingRegistrationButton.setStyleName(ValoTheme.BUTTON_TINY);
325
        blockingRegistrationButton.setDescription("No blocking registrations");
326
        if(dto.isBlocked()){
327
            blockingRegistrationButton.setEnabled(true);
328
            blockingRegistrationButton.setDescription("This registration is currently blocked by other registrations");
329
            blockingRegistrationButton.addStyleName(EditValoTheme.BUTTON_HIGHLITE);
330
            blockingRegistrationButton.addClickListener(e -> getViewEventBus().publish(
331
                    this,
332
                    new ShowDetailsEvent<RegistrationDTO, Integer>(
333
                            e,
334
                            RegistrationDTO.class,
335
                            dto.getId(),
336
                            RegistrationItem.BLOCKED_BY
337
                            )
338
                    ));
339
        }
340

    
341
        Button validationProblemsButton = regItemButtons.getValidationProblemsButton();
342
        validationProblemsButton.setStyleName(ValoTheme.BUTTON_TINY); //  + " " + RegistrationStyles.STYLE_FRIENDLY_FOREGROUND);
343

    
344
        if(!dto.getValidationProblems().isEmpty()){
345
            validationProblemsButton.setEnabled(true);
346
            validationProblemsButton.addClickListener(e -> getViewEventBus().publish(this,
347
                    new ShowDetailsEvent<RegistrationDTO, Integer>(
348
                        e,
349
                        RegistrationDTO.class,
350
                        dto.getId(),
351
                        RegistrationItem.VALIDATION_PROBLEMS
352
                        )
353
                    )
354
                );
355
        }
356
        validationProblemsButton.setCaption("<span class=\"" + RegistrationStyles.BUTTON_BADGE +"\"> " + dto.getValidationProblems().size() + "</span>");
357
        validationProblemsButton.setCaptionAsHtml(true);
358

    
359
        Button messageButton = regItemButtons.getMessagesButton();
360
        messageButton.addClickListener(e -> getViewEventBus().publish(this,
361
                    new ShowDetailsEvent<RegistrationDTO, Integer>(
362
                        e,
363
                        RegistrationDTO.class,
364
                        dto.getId(),
365
                        RegistrationItem.MESSAGES
366
                        )
367
                    )
368
                );
369
        messageButton.setStyleName(ValoTheme.BUTTON_TINY);
370

    
371
        RegistrationStateLabel stateLabel = new RegistrationStateLabel().update(dto.getStatus());
372

    
373

    
374
        if(UserHelper.fromSession().userIsRegistrationCurator() || UserHelper.fromSession().userIsAdmin()) {
375
            Button editRegistrationButton = new Button(FontAwesome.COG);
376
            editRegistrationButton.setStyleName(ValoTheme.BUTTON_TINY);
377
            editRegistrationButton.setDescription("Edit registration");
378
            editRegistrationButton.addClickListener(e -> getViewEventBus().publish(this, new RegistrationEditorAction(
379
                EditorActionType.EDIT,
380
                dto.getId(),
381
                null,
382
                this
383
                )));
384
            regItemButtons.addComponent(editRegistrationButton);
385
        }
386

    
387
        PermissionDebugUtils.addGainPerEntityPermissionButton(regItemButtons, Registration.class, dto.getId(),
388
                EnumSet.of(CRUD.UPDATE), RegistrationStatus.PREPARATION.name());
389

    
390
        row++;
391
        registrationsGrid.addComponent(stateLabel, COL_INDEX_STATE_LABEL, row);
392
        registrationsGrid.setComponentAlignment(stateLabel, Alignment.TOP_LEFT);
393
        registrationsGrid.addComponent(regItemButtonGroup, COL_INDEX_REG_ITEM, row);
394
        registrationsGrid.addComponent(regItemButtons, COL_INDEX_BUTTON_GROUP, row);
395
        registrationsGrid.setComponentAlignment(regItemButtons, Alignment.TOP_LEFT);
396

    
397
        row++;
398
        registrationsGrid.addComponent(footer, 0, row, COL_INDEX_BUTTON_GROUP, row);
399

    
400
        return row;
401
    }
402

    
403
    /**
404
     * @param button
405
     * @param registrationEntityId
406
     *
407
     */
408
    @Override
409
    public void chooseNewTypeRegistrationWorkingset(Integer registrationEntityId) {
410

    
411
        Window typeDesignationTypeCooser = new Window();
412
        typeDesignationTypeCooser.setModal(true);
413
        typeDesignationTypeCooser.setResizable(false);
414
        typeDesignationTypeCooser.setCaption("Add new type designation");
415
        Label label = new Label("Please select kind of type designation to be created.");
416
        Button newSpecimenTypeDesignationButton = new Button("Specimen type designation",
417
                e -> addNewTypeDesignationWorkingset(TypeDesignationWorkingSetType.SPECIMEN_TYPE_DESIGNATION_WORKINGSET, registrationEntityId, typeDesignationTypeCooser));
418
        Button newNameTypeDesignationButton = new Button("Name type designation",
419
                e -> addNewTypeDesignationWorkingset(TypeDesignationWorkingSetType.NAME_TYPE_DESIGNATION_WORKINGSET, registrationEntityId, typeDesignationTypeCooser));
420

    
421
        VerticalLayout layout = new VerticalLayout(label, newSpecimenTypeDesignationButton, newNameTypeDesignationButton);
422
        layout.setMargin(true);
423
        layout.setSpacing(true);
424
        layout.setComponentAlignment(newSpecimenTypeDesignationButton, Alignment.MIDDLE_CENTER);
425
        layout.setComponentAlignment(newNameTypeDesignationButton, Alignment.MIDDLE_CENTER);
426
        typeDesignationTypeCooser.setContent(layout);
427
        UI.getCurrent().addWindow(typeDesignationTypeCooser);
428
    }
429

    
430
    /**
431
     * @param button
432
     *
433
     */
434
    protected void addNewTypeDesignationWorkingset(TypeDesignationWorkingSetType newWorkingsetType, Integer registrationEntityId, Window typeDesignationTypeCooser) {
435
        UI.getCurrent().removeWindow(typeDesignationTypeCooser);
436
        getViewEventBus().publish(this, new TypeDesignationWorkingsetEditorAction(
437
                EditorActionType.ADD,
438
                newWorkingsetType,
439
                registrationEntityId,
440
                null,
441
                this
442
                ));
443
    }
444

    
445
    /**
446
     * {@inheritDoc}
447
     */
448
    @Override
449
    public void openReferenceEditor(UUID referenceUuid) {
450
        // TODO Auto-generated method stub
451

    
452
    }
453

    
454
    /**
455
     * {@inheritDoc}
456
     */
457
    @Override
458
    public void openNameEditor(UUID nameUuid) {
459
        // TODO Auto-generated method stub
460

    
461
    }
462

    
463
    /**
464
     * {@inheritDoc}
465
     */
466
    @Override
467
    protected String getHeaderText() {
468
        return headerText;
469
    }
470

    
471
    /**
472
     * {@inheritDoc}
473
     */
474
    @Override
475
    public void setHeaderText(String text) {
476
        this.headerText = text;
477
        updateHeader();
478

    
479
    }
480

    
481
    /**
482
     * @return the subheaderText
483
     */
484
    public String getSubheaderText() {
485
        return subheaderText;
486
    }
487

    
488
    /**
489
     * {@inheritDoc}
490
     */
491
    @Override
492
    public void setSubheaderText(String text) {
493
        subheaderText = text;
494
        updateHeader();
495
    }
496

    
497
    /**
498
     * {@inheritDoc}
499
     */
500
    @Override
501
    protected String getSubHeaderText() {
502
        return subheaderText;
503
    }
504

    
505
    /**
506
     * {@inheritDoc}
507
     */
508
    @Override
509
    public void openDetailsPopup(String caption, List<String> messages) {
510
        StringBuffer sb = new StringBuffer();
511
        sb.append("<div class=\"details-popup-content\">");
512
        messages.forEach(s -> sb.append(s).append("</br>"));
513
        sb.append("</div>");
514
        new Notification(caption, sb.toString(), Notification.Type.HUMANIZED_MESSAGE, true).show(Page.getCurrent());
515
    }
516

    
517
    /**
518
     * {@inheritDoc}
519
     */
520
    @Override
521
    public boolean allowAnonymousAccess() {
522
        return false;
523
    }
524

    
525
    /**
526
     * {@inheritDoc}
527
     */
528
    @Override
529
    public Collection<Collection<GrantedAuthority>> allowedGrantedAuthorities() {
530
        return null;
531
    }
532

    
533
    /**
534
     * @return the addNewNameRegistrationButton
535
     */
536
    @Override
537
    public Button getAddNewNameRegistrationButton() {
538
        return addNewNameRegistrationButton;
539
    }
540

    
541
    @Override
542
    public Button getAddExistingNameRegistrationButton() {
543
        return addExistingNameButton;
544
    }
545

    
546
    @Override
547
    public LazyComboBox<TaxonName> getAddExistingNameCombobox() {
548
        return existingNameCombobox;
549
    }
550

    
551
    /**
552
     * @return the citationID
553
     */
554
    @Override
555
    public Integer getCitationID() {
556
        return citationID;
557
    }
558

    
559
    @Override
560
    public Map<Integer, RegistrationDetailsItem> getRegistrationItemMap(){
561
        return Collections.unmodifiableMap(registrationItemMap);
562
    }
563

    
564

    
565
}
(18-18/23)