Project

General

Profile

Download (16.2 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.UUID;
16

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

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

    
39
import eu.etaxonomy.cdm.model.name.Registration;
40
import eu.etaxonomy.cdm.model.name.RegistrationStatus;
41
import eu.etaxonomy.cdm.model.name.TaxonName;
42
import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
43
import eu.etaxonomy.cdm.vaadin.component.registration.RegistrationItem;
44
import eu.etaxonomy.cdm.vaadin.component.registration.RegistrationItemEditButtonGroup;
45
import eu.etaxonomy.cdm.vaadin.component.registration.RegistrationItemEditButtonGroup.TypeDesignationWorkingSetButton;
46
import eu.etaxonomy.cdm.vaadin.component.registration.RegistrationStateLabel;
47
import eu.etaxonomy.cdm.vaadin.component.registration.RegistrationStyles;
48
import eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction;
49
import eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction.Action;
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.registration.RegistrationWorkingSet;
56
import eu.etaxonomy.cdm.vaadin.security.AccessRestrictedView;
57
import eu.etaxonomy.cdm.vaadin.security.PermissionDebugUtils;
58
import eu.etaxonomy.cdm.vaadin.security.UserHelper;
59
import eu.etaxonomy.cdm.vaadin.util.converter.TypeDesignationSetManager.TypeDesignationWorkingSetType;
60
import eu.etaxonomy.cdm.vaadin.view.AbstractPageView;
61

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

    
71
    public static final String DOM_ID_WORKINGSET = "workingset";
72

    
73
    private static final long serialVersionUID = -213040114015958970L;
74

    
75
    public static final String NAME = "workingset";
76

    
77
    public RegistrationType regType = null;
78

    
79
    private List<CssLayout> registrations = new ArrayList<>();
80

    
81
    private String headerText = "Registration Workingset Editor";
82
    private String subheaderText = "";
83

    
84
    private Integer citationID;
85

    
86
    private Button addNewNameRegistrationButton;
87

    
88
    private LazyComboBox<TaxonName> existingNameCombobox;
89

    
90
    private GridLayout registrationsGrid;
91

    
92
    private Button addExistingNameButton;
93

    
94
    private RegistrationItem workingsetHeader;
95

    
96
    private Panel registrationListPanel;
97

    
98
    public RegistrationWorksetViewBean() {
99
        super();
100
    }
101

    
102

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

    
113

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

    
122
            getPresenter().handleViewEntered();
123
        }
124
    }
125

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

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

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

    
148
    }
149

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

    
160
    }
161

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

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

    
173
        //registrationsGrid.setColumnExpandRatio(0, 0.1f);
174
        registrationsGrid.setColumnExpandRatio(1, 1f);
175

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

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

    
184
        addNewNameRegistrationButton = new Button("new name");
185
        addNewNameRegistrationButton.setDescription("A name which is newly published in this publication.");
186
        addNewNameRegistrationButton.addClickListener(
187
                e -> eventBus.publishEvent(new TaxonNameEditorAction(Action.ADD, addNewNameRegistrationButton))
188
                );
189

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

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

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

    
210
        Panel namesTypesPanel = new Panel(registrationsGrid);
211
        return namesTypesPanel;
212
    }
213

    
214

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

    
222
        grid.setRows(grid.getRows() + 1);
223

    
224
        CssLayout buttonGroup = new CssLayout();
225
        buttonGroup.setStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
226

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

    
246
        RegistrationStateLabel stateLabel = new RegistrationStateLabel().update(dto.getStatus());
247

    
248

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

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

    
265
        Component regItem;
266

    
267
        RegistrationItemEditButtonGroup editButtonGroup = new RegistrationItemEditButtonGroup(dto);
268

    
269
        if(editButtonGroup.getNameButton() != null){
270
            editButtonGroup.getNameButton().getButton().addClickListener(e -> {
271
                Integer nameId = editButtonGroup.getNameButton().getId();
272
                getEventBus().publishEvent(new TaxonNameEditorAction(
273
                    AbstractEditorAction.Action.EDIT,
274
                    nameId,
275
                    e.getButton(),
276
                    this
277
                    )
278
                );
279
            });
280
        }
281

    
282
        for(TypeDesignationWorkingSetButton workingsetButton : editButtonGroup.getTypeDesignationButtons()){
283
            workingsetButton.getButton().addClickListener(e -> {
284
                Integer typeDesignationWorkingsetId = workingsetButton.getId();
285
                TypeDesignationWorkingSetType workingsetType = workingsetButton.getType();
286
                Integer registrationEntityID = dto.getId();
287
                getEventBus().publishEvent(new TypeDesignationWorkingsetEditorAction(
288
                        AbstractEditorAction.Action.EDIT,
289
                        typeDesignationWorkingsetId,
290
                        workingsetType,
291
                        registrationEntityID,
292
                        e.getButton(),
293
                        this
294
                        )
295
                    );
296
            });
297
        }
298

    
299
        editButtonGroup.getAddTypeDesignationButton().addClickListener(
300
                e -> chooseNewTypeRegistrationWorkingset(dto.getId())
301
                );
302
        regItem = editButtonGroup;
303

    
304

    
305
        grid.addComponent(stateLabel, 0, row);
306
        grid.setComponentAlignment(stateLabel, Alignment.TOP_LEFT);
307
        grid.addComponent(regItem, 1, row);
308
        grid.addComponent(buttonGroup, 2, row);
309
        grid.setComponentAlignment(buttonGroup, Alignment.TOP_LEFT);
310
    }
311

    
312
    /**
313
     * @param button
314
     * @param registrationEntityId
315
     *
316
     */
317
    @Override
318
    public void chooseNewTypeRegistrationWorkingset(Integer registrationEntityId) {
319

    
320
        Window typeDesignationTypeCooser = new Window();
321
        typeDesignationTypeCooser.setModal(true);
322
        typeDesignationTypeCooser.setResizable(false);
323
        typeDesignationTypeCooser.setCaption("Add new type designation");
324
        Label label = new Label("Please select kind of type designation to be created.");
325
        Button newSpecimenTypeDesignationButton = new Button("Specimen type designation",
326
                e -> addNewTypeDesignationWorkingset(TypeDesignationWorkingSetType.SPECIMEN_TYPE_DESIGNATION_WORKINGSET, registrationEntityId, typeDesignationTypeCooser));
327
        Button newNameTypeDesignationButton = new Button("Name type designation",
328
                e -> addNewTypeDesignationWorkingset(TypeDesignationWorkingSetType.NAME_TYPE_DESIGNATION_WORKINGSET, registrationEntityId, typeDesignationTypeCooser));
329
        newNameTypeDesignationButton.setEnabled(false);
330

    
331
        VerticalLayout layout = new VerticalLayout(label, newSpecimenTypeDesignationButton, newNameTypeDesignationButton);
332
        layout.setMargin(true);
333
        layout.setSpacing(true);
334
        layout.setComponentAlignment(newSpecimenTypeDesignationButton, Alignment.MIDDLE_CENTER);
335
        layout.setComponentAlignment(newNameTypeDesignationButton, Alignment.MIDDLE_CENTER);
336
        typeDesignationTypeCooser.setContent(layout);
337
        UI.getCurrent().addWindow(typeDesignationTypeCooser);
338
    }
339

    
340
    /**
341
     * @param button
342
     *
343
     */
344
    protected void addNewTypeDesignationWorkingset(TypeDesignationWorkingSetType newWorkingsetType, Integer registrationEntityId, Window typeDesignationTypeCooser) {
345
        UI.getCurrent().removeWindow(typeDesignationTypeCooser);
346
        getEventBus().publishEvent(new TypeDesignationWorkingsetEditorAction(
347
                AbstractEditorAction.Action.ADD,
348
                newWorkingsetType,
349
                registrationEntityId,
350
                null,
351
                this
352
                ));
353
    }
354

    
355
    /**
356
     * {@inheritDoc}
357
     */
358
    @Override
359
    public void openReferenceEditor(UUID referenceUuid) {
360
        // TODO Auto-generated method stub
361

    
362
    }
363

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

    
371
    }
372

    
373
    /**
374
     * {@inheritDoc}
375
     */
376
    @Override
377
    protected String getHeaderText() {
378
        return headerText;
379
    }
380

    
381
    /**
382
     * {@inheritDoc}
383
     */
384
    @Override
385
    public void setHeaderText(String text) {
386
        this.headerText = text;
387
        updateHeader();
388

    
389
    }
390

    
391
    /**
392
     * @return the subheaderText
393
     */
394
    public String getSubheaderText() {
395
        return subheaderText;
396
    }
397

    
398
    /**
399
     * {@inheritDoc}
400
     */
401
    @Override
402
    public void setSubheaderText(String text) {
403
        subheaderText = text;
404
        updateHeader();
405
    }
406

    
407
    /**
408
     * {@inheritDoc}
409
     */
410
    @Override
411
    protected String getSubHeaderText() {
412
        return subheaderText;
413
    }
414

    
415
    /**
416
     * {@inheritDoc}
417
     */
418
    @Override
419
    public void openDetailsPopup(String caption, List<String> messages) {
420
        StringBuffer sb = new StringBuffer();
421
        sb.append("<div class=\"details-popup-content\">");
422
        messages.forEach(s -> sb.append(s).append("</br>"));
423
        sb.append("</div>");
424
        new Notification(caption, sb.toString(), Notification.Type.HUMANIZED_MESSAGE, true).show(Page.getCurrent());
425
    }
426

    
427
    /**
428
     * {@inheritDoc}
429
     */
430
    @Override
431
    public boolean allowAnonymousAccess() {
432
        return false;
433
    }
434

    
435
    /**
436
     * {@inheritDoc}
437
     */
438
    @Override
439
    public Collection<Collection<GrantedAuthority>> allowedGrantedAuthorities() {
440
        return null;
441
    }
442

    
443
    /**
444
     * @return the addNewNameRegistrationButton
445
     */
446
    @Override
447
    public Button getAddNewNameRegistrationButton() {
448
        return addNewNameRegistrationButton;
449
    }
450

    
451
    @Override
452
    public Button getAddExistingNameRegistrationButton() {
453
        return addExistingNameButton;
454
    }
455

    
456
    @Override
457
    public LazyComboBox<TaxonName> getAddExistingNameCombobox() {
458
        return existingNameCombobox;
459
    }
460

    
461
    /**
462
     * @return the citationID
463
     */
464
    @Override
465
    public Integer getCitationID() {
466
        return citationID;
467
    }
468

    
469

    
470
}
(14-14/19)