Project

General

Profile

Download (18 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.AbstractLayout;
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.component.registration.WorkflowSteps;
50
import eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction;
51
import eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction.Action;
52
import eu.etaxonomy.cdm.vaadin.event.ReferenceEditorAction;
53
import eu.etaxonomy.cdm.vaadin.event.RegistrationEditorAction;
54
import eu.etaxonomy.cdm.vaadin.event.ShowDetailsEvent;
55
import eu.etaxonomy.cdm.vaadin.event.TaxonNameEditorAction;
56
import eu.etaxonomy.cdm.vaadin.event.TypeDesignationWorkingsetEditorAction;
57
import eu.etaxonomy.cdm.vaadin.event.registration.RegistrationWorkflowEvent;
58
import eu.etaxonomy.cdm.vaadin.model.registration.RegistrationWorkingSet;
59
import eu.etaxonomy.cdm.vaadin.security.AccessRestrictedView;
60
import eu.etaxonomy.cdm.vaadin.security.PermissionDebugUtils;
61
import eu.etaxonomy.cdm.vaadin.security.UserHelper;
62
import eu.etaxonomy.cdm.vaadin.util.converter.TypeDesignationSetManager.TypeDesignationWorkingSetType;
63
import eu.etaxonomy.cdm.vaadin.view.AbstractPageView;
64

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

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

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

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

    
80
    public RegistrationType regType = null;
81

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

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

    
87
    private Integer citationID;
88

    
89
    private Button addNewNameRegistrationButton;
90

    
91
    private LazyComboBox<TaxonName> existingNameCombobox;
92

    
93
    private GridLayout registrationsGrid;
94

    
95
    private Button addExistingNameButton;
96

    
97
    private RegistrationItem workingsetHeader;
98

    
99
    private Panel registrationListPanel;
100

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

    
105

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

    
116

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

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

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

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

    
145
        registrationListPanel = createRegistrationsList(workingset);
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 -> eventBus.publishEvent(new TaxonNameEditorAction(Action.ADD, addNewNameRegistrationButton))
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 -> eventBus.publishEvent(new RegistrationWorkflowEvent(citationID, RegistrationWorkflowEvent.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
        namesTypesPanel.setSizeFull();
214
        return namesTypesPanel;
215
    }
216

    
217

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

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

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

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

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

    
251

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

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

    
268
        Component regItem;
269

    
270
        RegistrationItemEditButtonGroup editButtonGroup = new RegistrationItemEditButtonGroup(dto);
271

    
272
        if(editButtonGroup.getNameButton() != null){
273
            editButtonGroup.getNameButton().getButton().addClickListener(e -> {
274
                Integer nameId = editButtonGroup.getNameButton().getId();
275
                getEventBus().publishEvent(new TaxonNameEditorAction(
276
                    AbstractEditorAction.Action.EDIT,
277
                    nameId,
278
                    null, //e.getButton(), the listener method expects this to be null
279
                    this
280
                    )
281
                );
282
            });
283
            editButtonGroup.getNameButton().getButton().addFocusListener(e -> System.out.println("###"));
284
        }
285

    
286
        for(TypeDesignationWorkingSetButton workingsetButton : editButtonGroup.getTypeDesignationButtons()){
287
            workingsetButton.getButton().addClickListener(e -> {
288
                Integer typeDesignationWorkingsetId = workingsetButton.getId();
289
                TypeDesignationWorkingSetType workingsetType = workingsetButton.getType();
290
                Integer registrationEntityID = dto.getId();
291
                getEventBus().publishEvent(new TypeDesignationWorkingsetEditorAction(
292
                        AbstractEditorAction.Action.EDIT,
293
                        typeDesignationWorkingsetId,
294
                        workingsetType,
295
                        registrationEntityID,
296
                        null, //e.getButton(), the listener method expects this to be null
297
                        this
298
                        )
299
                    );
300
            });
301
        }
302

    
303
        editButtonGroup.getAddTypeDesignationButton().addClickListener(
304
                e -> chooseNewTypeRegistrationWorkingset(dto.getId())
305
                );
306
        regItem = editButtonGroup;
307

    
308

    
309
        grid.addComponent(stateLabel, 0, row);
310
        grid.setComponentAlignment(stateLabel, Alignment.TOP_LEFT);
311
        grid.addComponent(regItem, 1, row);
312
        grid.addComponent(buttonGroup, 2, row);
313
        grid.setComponentAlignment(buttonGroup, Alignment.TOP_LEFT);
314
    }
315

    
316
    /**
317
     * @param button
318
     * @param registrationEntityId
319
     *
320
     */
321
    @Override
322
    public void chooseNewTypeRegistrationWorkingset(Integer registrationEntityId) {
323

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

    
335
        VerticalLayout layout = new VerticalLayout(label, newSpecimenTypeDesignationButton, newNameTypeDesignationButton);
336
        layout.setMargin(true);
337
        layout.setSpacing(true);
338
        layout.setComponentAlignment(newSpecimenTypeDesignationButton, Alignment.MIDDLE_CENTER);
339
        layout.setComponentAlignment(newNameTypeDesignationButton, Alignment.MIDDLE_CENTER);
340
        typeDesignationTypeCooser.setContent(layout);
341
        UI.getCurrent().addWindow(typeDesignationTypeCooser);
342
    }
343

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

    
359

    
360
    /**
361
    *
362
    */
363
   private void addBulletWorkflowName() {
364
       WorkflowSteps steps = new WorkflowSteps();
365
       steps.appendNewWorkflowItem(1, "Publication details including the publisher.",
366
               e -> eventBus.publishEvent(new ReferenceEditorAction(Action.EDIT)));
367
       steps.appendNewWorkflowItem(2, "One or multiple published scientific new names.",
368
               e -> eventBus.publishEvent(new TaxonNameEditorAction(Action.EDIT)));
369
       steps.appendNewWorkflowItem(3, "Request for data curation and await approval.", null);
370
       steps.appendNewWorkflowItem(4, "Awaiting publication", null);
371
       getWorkflow().addComponent(steps);
372
   }
373

    
374
   /**
375
   *
376
   */
377
  private void addBulletWorkflowTypification() {
378
      WorkflowSteps steps = new WorkflowSteps();
379
      steps.appendNewWorkflowItem(1, "Publication details including the publisher.",
380
              e -> eventBus.publishEvent(new ReferenceEditorAction(Action.EDIT)));
381
      steps.appendNewWorkflowItem(2, "One or multiple published typifications.",
382
              e -> eventBus.publishEvent(new TaxonNameEditorAction(Action.EDIT)));
383
      steps.appendNewWorkflowItem(3, "Request for data curation and await approval.", null);
384
      steps.appendNewWorkflowItem(4, "Awaiting publication", null);
385
      getWorkflow().addComponent(steps);
386
  }
387

    
388
    /**
389
     * {@inheritDoc}
390
     */
391
    @Override
392
    public void openReferenceEditor(UUID referenceUuid) {
393
        // TODO Auto-generated method stub
394

    
395
    }
396

    
397
    /**
398
     * {@inheritDoc}
399
     */
400
    @Override
401
    public void openNameEditor(UUID nameUuid) {
402
        // TODO Auto-generated method stub
403

    
404
    }
405

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

    
414
    /**
415
     * {@inheritDoc}
416
     */
417
    @Override
418
    public void setHeaderText(String text) {
419
        this.headerText = text;
420
        updateHeader();
421

    
422
    }
423

    
424
    /**
425
     * @return the subheaderText
426
     */
427
    public String getSubheaderText() {
428
        return subheaderText;
429
    }
430

    
431
    /**
432
     * {@inheritDoc}
433
     */
434
    @Override
435
    public void setSubheaderText(String text) {
436
        subheaderText = text;
437
        updateHeader();
438
    }
439

    
440
    /**
441
     * {@inheritDoc}
442
     */
443
    @Override
444
    protected String getSubHeaderText() {
445
        return subheaderText;
446
    }
447

    
448
    /**
449
     * {@inheritDoc}
450
     */
451
    @Override
452
    public AbstractLayout getWorkflow() {
453
        return getLayout();
454
    }
455

    
456
    /**
457
     * {@inheritDoc}
458
     */
459
    @Override
460
    public void openDetailsPopup(String caption, List<String> messages) {
461
        StringBuffer sb = new StringBuffer();
462
        sb.append("<div class=\"details-popup-content\">");
463
        messages.forEach(s -> sb.append(s).append("</br>"));
464
        sb.append("</div>");
465
        new Notification(caption, sb.toString(), Notification.Type.HUMANIZED_MESSAGE, true).show(Page.getCurrent());
466
    }
467

    
468
    /**
469
     * {@inheritDoc}
470
     */
471
    @Override
472
    public boolean allowAnonymousAccess() {
473
        return false;
474
    }
475

    
476
    /**
477
     * {@inheritDoc}
478
     */
479
    @Override
480
    public Collection<Collection<GrantedAuthority>> allowedGrantedAuthorities() {
481
        return null;
482
    }
483

    
484
    /**
485
     * @return the addNewNameRegistrationButton
486
     */
487
    @Override
488
    public Button getAddNewNameRegistrationButton() {
489
        return addNewNameRegistrationButton;
490
    }
491

    
492
    @Override
493
    public Button getAddExistingNameRegistrationButton() {
494
        return addExistingNameButton;
495
    }
496

    
497
    @Override
498
    public LazyComboBox<TaxonName> getAddExistingNameCombobox() {
499
        return existingNameCombobox;
500
    }
501

    
502
    /**
503
     * @return the citationID
504
     */
505
    @Override
506
    public Integer getCitationID() {
507
        return citationID;
508
    }
509

    
510

    
511
}
(14-14/19)