Project

General

Profile

Download (16.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 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.RegistrationEditorAction;
49
import eu.etaxonomy.cdm.vaadin.event.ShowDetailsEvent;
50
import eu.etaxonomy.cdm.vaadin.event.TaxonNameEditorAction;
51
import eu.etaxonomy.cdm.vaadin.event.TypeDesignationWorkingsetEditorAction;
52
import eu.etaxonomy.cdm.vaadin.event.registration.RegistrationWorkingsetAction;
53
import eu.etaxonomy.cdm.vaadin.model.TypedEntityReference;
54
import eu.etaxonomy.cdm.vaadin.model.registration.RegistrationWorkingSet;
55
import eu.etaxonomy.cdm.vaadin.security.AccessRestrictedView;
56
import eu.etaxonomy.cdm.vaadin.security.PermissionDebugUtils;
57
import eu.etaxonomy.cdm.vaadin.security.UserHelper;
58
import eu.etaxonomy.cdm.vaadin.util.converter.TypeDesignationSetManager.TypeDesignationWorkingSetType;
59
import eu.etaxonomy.cdm.vaadin.view.AbstractPageView;
60
import eu.etaxonomy.vaadin.event.EditorActionType;
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(EditorActionType.ADD, null, addNewNameRegistrationButton, this))
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
                EditorActionType.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
                    EditorActionType.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
                TypedEntityReference baseEntityRef = workingsetButton.getBaseEntity();
285
                TypeDesignationWorkingSetType workingsetType = workingsetButton.getType();
286
                Integer registrationEntityID = dto.getId();
287
                getEventBus().publishEvent(new TypeDesignationWorkingsetEditorAction(
288
                        EditorActionType.EDIT,
289
                        baseEntityRef,
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

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

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

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

    
361
    }
362

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

    
370
    }
371

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

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

    
388
    }
389

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

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

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

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

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

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

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

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

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

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

    
468

    
469
}
(14-14/19)