Project

General

Profile

Download (16.7 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.component.registration;
10

    
11
import static eu.etaxonomy.cdm.vaadin.component.registration.RegistrationStyles.LABEL_NOWRAP;
12

    
13
import java.util.Collection;
14
import java.util.EnumSet;
15
import java.util.Set;
16
import java.util.UUID;
17

    
18
import org.joda.time.DateTime;
19
import org.joda.time.format.ISODateTimeFormat;
20
import org.vaadin.spring.events.EventScope;
21

    
22
import com.vaadin.server.ExternalResource;
23
import com.vaadin.server.FontAwesome;
24
import com.vaadin.server.Resource;
25
import com.vaadin.server.UserError;
26
import com.vaadin.shared.ui.label.ContentMode;
27
import com.vaadin.ui.Alignment;
28
import com.vaadin.ui.Button;
29
import com.vaadin.ui.Button.ClickListener;
30
import com.vaadin.ui.CssLayout;
31
import com.vaadin.ui.GridLayout;
32
import com.vaadin.ui.Label;
33
import com.vaadin.ui.Link;
34
import com.vaadin.ui.Panel;
35
import com.vaadin.ui.themes.ValoTheme;
36

    
37
import eu.etaxonomy.cdm.api.service.dto.RegistrationDTO;
38
import eu.etaxonomy.cdm.api.service.dto.RegistrationWorkingSet;
39
import eu.etaxonomy.cdm.api.service.name.TypeDesignationWorkingSet;
40
import eu.etaxonomy.cdm.api.utility.UserHelper;
41
import eu.etaxonomy.cdm.model.ICdmEntityUuidCacher;
42
import eu.etaxonomy.cdm.model.common.TimePeriod;
43
import eu.etaxonomy.cdm.model.name.RegistrationStatus;
44
import eu.etaxonomy.cdm.model.name.TypeDesignationStatusBase;
45
import eu.etaxonomy.cdm.model.permission.CRUD;
46
import eu.etaxonomy.cdm.model.reference.Reference;
47
import eu.etaxonomy.cdm.service.UserHelperAccess;
48
import eu.etaxonomy.cdm.vaadin.component.BadgeButton;
49
import eu.etaxonomy.cdm.vaadin.event.ReferenceEditorAction;
50
import eu.etaxonomy.cdm.vaadin.event.ShowDetailsEvent;
51
import eu.etaxonomy.cdm.vaadin.permission.PermissionDebugUtils;
52
import eu.etaxonomy.cdm.vaadin.theme.EditValoTheme;
53
import eu.etaxonomy.cdm.vaadin.util.formatter.DateTimeFormat;
54
import eu.etaxonomy.cdm.vaadin.util.formatter.TimePeriodFormatter;
55
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationWorksetViewBean;
56
import eu.etaxonomy.vaadin.event.EditorActionType;
57
import eu.etaxonomy.vaadin.mvp.AbstractView;
58
import eu.etaxonomy.vaadin.ui.navigation.NavigationEvent;
59

    
60
/**
61
 * @author a.kohlbecker
62
 * @since Mar 17, 2017
63
 *
64
 */
65
public class RegistrationItem extends GridLayout {
66

    
67
    public static final String VALIDATION_PROBLEMS = "validationProblems";
68

    
69
    public static final String BLOCKED_BY = "blockedBy";
70

    
71
    private static final String LABEL_CAPTION_CREATED = "Created";
72

    
73
    private static final String LABEL_CAPTION_PUBLISHED = "Published";
74

    
75
    private static final String LABEL_CAPTION_RELEASED = "Released";
76

    
77
    private static final int GRID_ROWS = 5;
78

    
79
    private static final int GRID_COLS = 3;
80

    
81
    private static final long serialVersionUID = -211003770452173644L;
82

    
83
    private AbstractView<?> parentView;
84

    
85
    private RegistrationDTO regDto;
86

    
87
    private TimePeriodFormatter timePeriodFormatter = new TimePeriodFormatter(DateTimeFormat.ISO8601_DATE);
88

    
89
    // --------------------------------------------------
90

    
91
    private RegistrationStatusLabel stateLabel = new RegistrationStatusLabel();
92
    private Link identifierLink = new Link();
93
    private Label citationSummaryLabel = new Label();
94
    private Button blockedByButton = new Button(FontAwesome.BAN);
95
    private BadgeButton validationProblemsButton;
96
    private Button openButton = new Button(FontAwesome.COGS);
97
    private Label submitterLabel = new Label();
98
    private Label createdLabel = new Label();
99
    private Label publishedLabel = new Label();
100
    private Label releasedLabel = new Label();
101

    
102
    private Panel blockingRelationsPanel;
103

    
104
    private ICdmEntityUuidCacher cache;
105

    
106
    /**
107
     *
108
     */
109
    public RegistrationItem(RegistrationDTO item, AbstractView<?> parentView, ICdmEntityUuidCacher cache) {
110
        super(GRID_COLS, GRID_ROWS);
111
        this.cache = cache;
112
        init();
113
        setItem(item, parentView);
114
    }
115

    
116
    /**
117
    *
118
    */
119
   public RegistrationItem(RegistrationWorkingSet workingSet, AbstractView<?> parentView, ICdmEntityUuidCacher cache) {
120
       super(GRID_COLS, GRID_ROWS);
121
       this.cache = cache;
122
       init();
123
       blockedByButton.setVisible(false);
124
       setWorkingSet(workingSet, parentView);
125
   }
126

    
127
    public void init() {
128

    
129
        setWidth(100, Unit.PERCENTAGE);
130
        addStyleName("registration-list-item");
131

    
132
        CssLayout stateUserContainer = new CssLayout();
133
        stateLabel.setStyleName(LABEL_NOWRAP + " registration-state");
134
        stateLabel.setVisible(false);
135

    
136
        submitterLabel.setStyleName(LABEL_NOWRAP + " submitter");
137
        submitterLabel.setIcon(FontAwesome.USER);
138
        submitterLabel.setContentMode(ContentMode.HTML);
139
        submitterLabel.setVisible(false);
140

    
141
        stateUserContainer.addComponents(stateLabel, submitterLabel);
142
        addComponent(stateUserContainer, 0, 0);
143
        setComponentAlignment(stateUserContainer, Alignment.TOP_LEFT);
144

    
145
        identifierLink.setVisible(false);
146
        addComponent(identifierLink, 1, 0);
147
        setComponentAlignment(identifierLink, Alignment.TOP_CENTER);
148
        setColumnExpandRatio(1, 1.0f);
149

    
150
        validationProblemsButton = new BadgeButton(FontAwesome.WARNING);
151
        CssLayout buttonGroup = new CssLayout(blockedByButton, validationProblemsButton, openButton);
152
        blockedByButton.setStyleName(ValoTheme.BUTTON_TINY);
153
        blockedByButton.setEnabled(false);
154
        validationProblemsButton.setStyleName(ValoTheme.BUTTON_TINY);
155
        validationProblemsButton.setEnabled(false);
156

    
157
        openButton.setStyleName(ValoTheme.BUTTON_TINY);
158
        openButton.addStyleName(ValoTheme.BUTTON_PRIMARY);
159
        openButton.setVisible(false);
160

    
161
        buttonGroup.setStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
162
        addComponent(buttonGroup, 2, 0);
163
        setComponentAlignment(buttonGroup, Alignment.TOP_RIGHT);
164

    
165
        citationSummaryLabel.setContentMode(ContentMode.HTML);
166
        addComponent(citationSummaryLabel, 0, 1, 1, 3);
167

    
168
        createdLabel.setStyleName(LABEL_NOWRAP);
169
        createdLabel.setContentMode(ContentMode.HTML);
170
        createdLabel.setWidthUndefined();
171
        addComponent(createdLabel, 2, 1);
172
        setComponentAlignment(createdLabel, Alignment.BOTTOM_RIGHT);
173

    
174
        publishedLabel.setStyleName(LABEL_NOWRAP);
175
        publishedLabel.setContentMode(ContentMode.HTML);
176
        publishedLabel.setWidthUndefined();
177
        publishedLabel.setVisible(false);
178
        addComponent(publishedLabel, 2, 2);
179
        setComponentAlignment(publishedLabel, Alignment.BOTTOM_RIGHT);
180

    
181
        releasedLabel.setStyleName(LABEL_NOWRAP);
182
        releasedLabel.setContentMode(ContentMode.HTML);
183
        releasedLabel.setWidthUndefined();
184
        releasedLabel.setVisible(false);
185
        addComponent(releasedLabel, 2, 3);
186
        setComponentAlignment(releasedLabel, Alignment.BOTTOM_RIGHT);
187

    
188
    }
189

    
190
    public void setItem(RegistrationDTO regDto, AbstractView<?> parentView){
191

    
192
        this.parentView = parentView;
193

    
194
        this.regDto = regDto;
195

    
196
        NavigationEvent navigationEvent = null;
197
        if(regDto.getCitationUuid() != null) {
198
            navigationEvent = new NavigationEvent(
199
                    RegistrationWorksetViewBean.NAME,
200
                    regDto.getCitationUuid().toString()
201
                    );
202
        } else {
203
            setComponentError(new UserError("Citation is missing"));
204
        }
205

    
206
        updateUI(regDto.getBibliographicInRefCitationString(), regDto.getCreated(), regDto.getDatePublished(), regDto.getValidationProblems().size(),
207
                navigationEvent, null, regDto, regDto.getSubmitterUserName());
208
    }
209

    
210
    public void setWorkingSet(RegistrationWorkingSet workingSet, AbstractView<?> parentView){
211
        this.parentView = parentView;
212

    
213
        ReferenceEditorAction referenceEditorAction = null;
214
        if(workingSet.getCitationUuid() != null){
215
            if(cdmUserHelper().userHasPermission(Reference.class, workingSet.getCitationUuid(), CRUD.UPDATE)){
216
                referenceEditorAction = new ReferenceEditorAction(EditorActionType.EDIT, workingSet.getCitationUuid(), null, null, parentView);
217
            }
218
            PermissionDebugUtils.addGainPerEntityPermissionButton(this, Reference.class, workingSet.getCitationUuid(), EnumSet.of(CRUD.UPDATE, CRUD.DELETE), null);
219
        } else {
220
            if(UserHelperAccess.userHelper().userHasPermission(Reference.class, CRUD.CREATE, null, null, parentView)){
221
                referenceEditorAction = new ReferenceEditorAction(EditorActionType.ADD);
222
            }
223
        }
224
        TimePeriod datePublished = null;
225
        String submitterName = null;
226
        if(workingSet.getRegistrationDTOs().size() > 0){
227
            datePublished = workingSet.getRegistrationDTOs().get(0).getDatePublished();
228
            // submitterName = workingSet.getRegistrationDTOs().get(0).getSubmitterUserName();
229
        }
230
        updateUI(workingSet.getCitation(), workingSet.getCreated(), datePublished, workingSet.validationProblemsCount(),
231
                referenceEditorAction, FontAwesome.EDIT, null, submitterName);
232
    }
233

    
234
    /**
235
     * @return
236
     */
237
    private UserHelper cdmUserHelper() {
238
        if(cache != null){
239
            return UserHelperAccess.userHelper().withCache(cache);
240
        } else {
241
            return UserHelperAccess.userHelper();
242
        }
243
    }
244

    
245

    
246
    /**
247
     * @param submitterUserName TODO
248
     *
249
     */
250
    private void updateUI(String citationString,  DateTime created, TimePeriod datePublished,  int validationProblemsCount,
251
            Object openButtonEvent, Resource openButtonIcon, RegistrationDTO regDto, String submitterUserName) {
252

    
253
        StringBuffer labelMarkup = new StringBuffer();
254
        DateTime registrationDate = null;
255

    
256
        if(validationProblemsCount > 0){
257
            getValidationProblemsButton().setEnabled(true);
258
            // getMessageButton().addStyleName(RegistrationStyles.STYLE_FRIENDLY_FOREGROUND);
259
            getValidationProblemsButton().addClickListener(e -> {
260
                ShowDetailsEvent detailsEvent;
261
                if(regDto != null){
262
                    detailsEvent = new ShowDetailsEvent<RegistrationDTO, UUID>(
263
                            e,
264
                            RegistrationDTO.class,
265
                            regDto.getUuid(),
266
                            VALIDATION_PROBLEMS);
267
                } else {
268
                    detailsEvent = new ShowDetailsEvent<RegistrationWorkingSet, UUID>(
269
                            e,
270
                            RegistrationWorkingSet.class,
271
                            null,
272
                            VALIDATION_PROBLEMS);
273
                }
274
                publishEvent(detailsEvent);
275
                }
276
            );
277
            getValidationProblemsButton().setCaption(Integer.toString(validationProblemsCount));
278
        }
279

    
280
        if(regDto != null && regDto.isBlocked()){
281
            getBlockedByButton().setEnabled(true);
282
            getBlockedByButton().addStyleName(EditValoTheme.BUTTON_HIGHLITE);
283
        }
284

    
285
        labelMarkup.append(citationString);
286

    
287
        if(openButtonEvent != null){
288
            // Buttons
289
            getOpenButton().setVisible(true);
290
            Collection<?> removeCandidates = getOpenButton().getListeners(ClickListener.class);
291
            removeCandidates.forEach(l -> getOpenButton().removeClickListener((ClickListener)l));
292
            getOpenButton().addClickListener(e -> publishEvent(openButtonEvent));
293
        }
294

    
295
        if(openButtonIcon != null){
296
            getOpenButton().setIcon(openButtonIcon);
297
        }
298

    
299
        if(regDto != null){
300
            String summary = regDto.getSummary();
301
            if(regDto.getOrderedTypeDesignationWorkingSets() != null) {
302
                for( TypeDesignationWorkingSet workingSet : regDto.getOrderedTypeDesignationWorkingSets().values()) {
303
                    for(TypeDesignationStatusBase<?> typeStatus : workingSet.keySet()) {
304
                        if(summary.contains(typeStatus.getLabel() + "s")){
305
                            // replace plural form
306
                            summary = summary.replace(typeStatus.getLabel() + "s", "<strong>" + typeStatus.getLabel() + "s</strong>");
307
                        } else {
308
                            // replace singular form
309
                            summary = summary.replace(typeStatus.getLabel(), "<strong>" + typeStatus.getLabel() + "</strong>");
310
                        }
311
                    }
312
                }
313
            }
314
            labelMarkup.append("</br>").append(summary);
315

    
316
            stateLabel.setVisible(true);
317
            stateLabel.update(regDto.getStatus());
318
            if(regDto.getIdentifier() != null){
319
                getIdentifierLink().setResource(new ExternalResource(regDto.getIdentifier()));
320
            }
321
            getIdentifierLink().setCaption(regDto.getIdentifier());
322
            //TODO make responsive and use specificIdentifier in case the space gets too narrow
323
            getIdentifierLink().setVisible(true);
324
            getIdentifierLink().setEnabled(regDto.getStatus() == RegistrationStatus.PUBLISHED);
325

    
326
            registrationDate = regDto.getRegistrationDate();
327
        }
328

    
329
        getCitationSummaryLabel().setValue(labelMarkup.toString());
330
        getSubmitterLabel().setValue(submitterUserName);
331
        getSubmitterLabel().setVisible(submitterUserName != null);
332
        updateDateLabels(created, datePublished, registrationDate);
333
    }
334

    
335

    
336
    private void updateDateLabels(DateTime created, TimePeriod datePublished, DateTime released) {
337
        if(created != null){
338
            getCreatedLabel().setValue("<span class=\"caption\">" + LABEL_CAPTION_CREATED + "</span>&nbsp;" + created.toString(ISODateTimeFormat.yearMonthDay()));
339
        }
340
        if(datePublished != null){
341
            getPublishedLabel().setVisible(true);
342
            getPublishedLabel().setValue("<span class=\"caption\">" + LABEL_CAPTION_PUBLISHED + "</span>&nbsp;" + timePeriodFormatter.print(datePublished));
343
        }
344
        if(released != null){
345
            getReleasedLabel().setVisible(true);
346
            getReleasedLabel().setValue("<span class=\"caption\">" + LABEL_CAPTION_RELEASED + "</span>&nbsp;" + released.toString(ISODateTimeFormat.yearMonthDay()));
347
        }
348
    }
349

    
350

    
351
    private void publishEvent(Object event) {
352
        if(event instanceof NavigationEvent){
353
            parentView.getViewEventBus().publish(EventScope.UI, this, event);
354
        } else {
355
            parentView.getViewEventBus().publish(this, event);
356
        }
357
    }
358

    
359
    public UUID getRegistrationUuid(){
360
        return regDto.getUuid();
361
    }
362

    
363
    /**
364
     * @param showBlockingRelations the showBlockingRelations to set
365
     */
366
    public void showBlockingRegistrations(Set<RegistrationDTO> blockingRegDTOs) {
367

    
368
        if(blockingRelationsPanel == null) {
369

    
370
            if(regDto.isBlocked() && blockingRegDTOs.isEmpty()){
371
                throw new RuntimeException("Registration is blocked but tet of blocking registrations is empty");
372
            }
373
            if(!regDto.isBlocked() && !blockingRegDTOs.isEmpty()){
374
                throw new RuntimeException("No point showing blocking registrations for an unblocked registration");
375
            }
376

    
377
            blockingRelationsPanel = new RegistrationItemsPanel(parentView, "blocked by", blockingRegDTOs, cache);
378
            addComponent(blockingRelationsPanel, 0, 4, GRID_COLS - 1, 4);
379
        }
380

    
381
    }
382

    
383
    /* ====== RegistrationItemDesign Getters ====== */
384
    /**
385
     * @return the typeStateLabel
386
     */
387
    public Label getTypeStateLabel() {
388
        return stateLabel;
389
    }
390

    
391
    /**
392
     * @return the identifierLink
393
     */
394
    public Link getIdentifierLink() {
395
        return identifierLink;
396
    }
397

    
398
    /**
399
     * @return the citationSummaryLabel
400
     */
401
    public Label getCitationSummaryLabel() {
402
        return citationSummaryLabel;
403
    }
404

    
405
    /**
406
     * @return the blockedByButton
407
     */
408
    public Button getBlockedByButton() {
409
        return blockedByButton;
410
    }
411

    
412
    /**
413
     * @return the validationProblemsButton
414
     */
415
    public BadgeButton getValidationProblemsButton() {
416
        return validationProblemsButton;
417
    }
418

    
419
    /**
420
     * @return the openButton
421
     */
422
    public Button getOpenButton() {
423
        return openButton;
424
    }
425

    
426
    /**
427
     * @return the createdLabel
428
     */
429
    public Label getCreatedLabel() {
430
        return createdLabel;
431
    }
432

    
433
    /**
434
     * @return the publishedLabel
435
     */
436
    public Label getPublishedLabel() {
437
        return publishedLabel;
438
    }
439

    
440

    
441
    /**
442
     * @return
443
     */
444
    public Label getReleasedLabel() {
445
        return releasedLabel;
446
    }
447

    
448
    /**
449
     * @return the submitterLabel
450
     */
451
    public Label getSubmitterLabel() {
452
        return submitterLabel;
453
    }
454

    
455
    /**
456
     * @return the showBlockingRelations
457
     */
458
    public boolean isShowBlockingRelations() {
459
        return blockingRelationsPanel != null;
460
    }
461

    
462

    
463
   /* --------------------------------------- */
464

    
465
}
(2-2/11)