Project

General

Profile

Download (16.4 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.TypeDesignationSetManager.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.getOrderdTypeDesignationWorkingSets() != null) {
302
                for( TypeDesignationWorkingSet workingSet : regDto.getOrderdTypeDesignationWorkingSets().values()) {
303
                    for(TypeDesignationStatusBase<?> typeStatus : workingSet.keySet()) {
304
                        summary = summary.replace(typeStatus.getLabel(), "<strong>" + typeStatus.getLabel() + "</strong>");
305
                    }
306
                }
307
            }
308
            labelMarkup.append("</br>").append(summary);
309

    
310
            stateLabel.setVisible(true);
311
            stateLabel.update(regDto.getStatus());
312
            if(regDto.getIdentifier() != null){
313
                getIdentifierLink().setResource(new ExternalResource(regDto.getIdentifier()));
314
            }
315
            getIdentifierLink().setCaption(regDto.getIdentifier());
316
            //TODO make responsive and use specificIdentifier in case the space gets too narrow
317
            getIdentifierLink().setVisible(true);
318
            getIdentifierLink().setEnabled(regDto.getStatus() == RegistrationStatus.PUBLISHED);
319

    
320
            registrationDate = regDto.getRegistrationDate();
321
        }
322

    
323
        getCitationSummaryLabel().setValue(labelMarkup.toString());
324
        getSubmitterLabel().setValue(submitterUserName);
325
        getSubmitterLabel().setVisible(submitterUserName != null);
326
        updateDateLabels(created, datePublished, registrationDate);
327
    }
328

    
329

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

    
344

    
345
    private void publishEvent(Object event) {
346
        if(event instanceof NavigationEvent){
347
            parentView.getViewEventBus().publish(EventScope.UI, this, event);
348
        } else {
349
            parentView.getViewEventBus().publish(this, event);
350
        }
351
    }
352

    
353
    public UUID getRegistrationUuid(){
354
        return regDto.getUuid();
355
    }
356

    
357
    /**
358
     * @param showBlockingRelations the showBlockingRelations to set
359
     */
360
    public void showBlockingRegistrations(Set<RegistrationDTO> blockingRegDTOs) {
361

    
362
        if(blockingRelationsPanel == null) {
363

    
364
            if(regDto.isBlocked() && blockingRegDTOs.isEmpty()){
365
                throw new RuntimeException("Registration is blocked but tet of blocking registrations is empty");
366
            }
367
            if(!regDto.isBlocked() && !blockingRegDTOs.isEmpty()){
368
                throw new RuntimeException("No point showing blocking registrations for an unblocked registration");
369
            }
370

    
371
            blockingRelationsPanel = new RegistrationItemsPanel(parentView, "blocked by", blockingRegDTOs, cache);
372
            addComponent(blockingRelationsPanel, 0, 4, GRID_COLS - 1, 4);
373
        }
374

    
375
    }
376

    
377
    /* ====== RegistrationItemDesign Getters ====== */
378
    /**
379
     * @return the typeStateLabel
380
     */
381
    public Label getTypeStateLabel() {
382
        return stateLabel;
383
    }
384

    
385
    /**
386
     * @return the identifierLink
387
     */
388
    public Link getIdentifierLink() {
389
        return identifierLink;
390
    }
391

    
392
    /**
393
     * @return the citationSummaryLabel
394
     */
395
    public Label getCitationSummaryLabel() {
396
        return citationSummaryLabel;
397
    }
398

    
399
    /**
400
     * @return the blockedByButton
401
     */
402
    public Button getBlockedByButton() {
403
        return blockedByButton;
404
    }
405

    
406
    /**
407
     * @return the validationProblemsButton
408
     */
409
    public BadgeButton getValidationProblemsButton() {
410
        return validationProblemsButton;
411
    }
412

    
413
    /**
414
     * @return the openButton
415
     */
416
    public Button getOpenButton() {
417
        return openButton;
418
    }
419

    
420
    /**
421
     * @return the createdLabel
422
     */
423
    public Label getCreatedLabel() {
424
        return createdLabel;
425
    }
426

    
427
    /**
428
     * @return the publishedLabel
429
     */
430
    public Label getPublishedLabel() {
431
        return publishedLabel;
432
    }
433

    
434

    
435
    /**
436
     * @return
437
     */
438
    public Label getReleasedLabel() {
439
        return releasedLabel;
440
    }
441

    
442
    /**
443
     * @return the submitterLabel
444
     */
445
    public Label getSubmitterLabel() {
446
        return submitterLabel;
447
    }
448

    
449
    /**
450
     * @return the showBlockingRelations
451
     */
452
    public boolean isShowBlockingRelations() {
453
        return blockingRelationsPanel != null;
454
    }
455

    
456

    
457
   /* --------------------------------------- */
458

    
459
}
(2-2/11)