Project

General

Profile

Download (15.8 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.utility.UserHelper;
40
import eu.etaxonomy.cdm.model.ICdmEntityUuidCacher;
41
import eu.etaxonomy.cdm.model.common.TimePeriod;
42
import eu.etaxonomy.cdm.model.name.RegistrationStatus;
43
import eu.etaxonomy.cdm.model.reference.Reference;
44
import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
45
import eu.etaxonomy.cdm.service.UserHelperAccess;
46
import eu.etaxonomy.cdm.vaadin.event.ReferenceEditorAction;
47
import eu.etaxonomy.cdm.vaadin.event.ShowDetailsEvent;
48
import eu.etaxonomy.cdm.vaadin.permission.PermissionDebugUtils;
49
import eu.etaxonomy.cdm.vaadin.theme.EditValoTheme;
50
import eu.etaxonomy.cdm.vaadin.util.formatter.DateTimeFormat;
51
import eu.etaxonomy.cdm.vaadin.util.formatter.TimePeriodFormatter;
52
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationWorksetViewBean;
53
import eu.etaxonomy.vaadin.event.EditorActionType;
54
import eu.etaxonomy.vaadin.mvp.AbstractView;
55
import eu.etaxonomy.vaadin.ui.navigation.NavigationEvent;
56

    
57
/**
58
 * @author a.kohlbecker
59
 * @since Mar 17, 2017
60
 *
61
 */
62
public class RegistrationItem extends GridLayout {
63

    
64
    public static final String VALIDATION_PROBLEMS = "validationProblems";
65

    
66
    public static final String BLOCKED_BY = "blockedBy";
67

    
68
    private static final String LABEL_CAPTION_CREATED = "Created";
69

    
70
    private static final String LABEL_CAPTION_PUBLISHED = "Published";
71

    
72
    private static final String LABEL_CAPTION_RELEASED = "Released";
73

    
74
    private static final int GRID_ROWS = 5;
75

    
76
    private static final int GRID_COLS = 3;
77

    
78
    private static final long serialVersionUID = -211003770452173644L;
79

    
80
    private AbstractView<?> parentView;
81

    
82
    private RegistrationDTO regDto;
83

    
84
    private TimePeriodFormatter timePeriodFormatter = new TimePeriodFormatter(DateTimeFormat.ISO8601_DATE);
85

    
86
    // --------------------------------------------------
87

    
88
    private RegistrationStatusLabel stateLabel = new RegistrationStatusLabel();
89
    private Link identifierLink = new Link();
90
    private Label citationSummaryLabel = new Label();
91
    private Button blockedByButton = new Button(FontAwesome.WARNING);
92
    private Button validationProblemsButton;
93
    private Button openButton = new Button(FontAwesome.COGS);
94
    private Label submitterLabel = new Label();
95
    private Label createdLabel = new Label();
96
    private Label publishedLabel = new Label();
97
    private Label releasedLabel = new Label();
98

    
99
    private Panel blockingRelationsPanel;
100

    
101
    private ICdmEntityUuidCacher cache;
102

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

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

    
124
    public void init() {
125

    
126
        setWidth(100, Unit.PERCENTAGE);
127
        addStyleName("registration-list-item");
128

    
129
        CssLayout stateUserContainer = new CssLayout();
130
        stateLabel.setStyleName(LABEL_NOWRAP + " registration-state");
131
        stateLabel.setVisible(false);
132

    
133
        submitterLabel.setStyleName(LABEL_NOWRAP + " submitter");
134
        submitterLabel.setIcon(FontAwesome.USER);
135
        submitterLabel.setContentMode(ContentMode.HTML);
136
        submitterLabel.setVisible(false);
137

    
138
        stateUserContainer.addComponents(stateLabel, submitterLabel);
139
        addComponent(stateUserContainer, 0, 0);
140
        setComponentAlignment(stateUserContainer, Alignment.TOP_LEFT);
141

    
142
        identifierLink.setVisible(false);
143
        addComponent(identifierLink, 1, 0);
144
        setComponentAlignment(identifierLink, Alignment.TOP_CENTER);
145
        setColumnExpandRatio(1, 1.0f);
146

    
147
        validationProblemsButton = new Button(FontAwesome.COMMENT);
148
        CssLayout buttonGroup = new CssLayout(blockedByButton, validationProblemsButton, openButton);
149
        blockedByButton.setStyleName(ValoTheme.BUTTON_TINY);
150
        blockedByButton.setEnabled(false);
151
        validationProblemsButton.setStyleName(ValoTheme.BUTTON_TINY);
152
        validationProblemsButton.setEnabled(false);
153

    
154
        openButton.setStyleName(ValoTheme.BUTTON_TINY);
155
        openButton.addStyleName(ValoTheme.BUTTON_PRIMARY);
156
        openButton.setVisible(false);
157

    
158
        buttonGroup.setStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
159
        addComponent(buttonGroup, 2, 0);
160
        setComponentAlignment(buttonGroup, Alignment.TOP_RIGHT);
161

    
162
        citationSummaryLabel.setContentMode(ContentMode.HTML);
163
        addComponent(citationSummaryLabel, 0, 1, 1, 3);
164

    
165
        createdLabel.setStyleName(LABEL_NOWRAP);
166
        createdLabel.setContentMode(ContentMode.HTML);
167
        createdLabel.setWidthUndefined();
168
        addComponent(createdLabel, 2, 1);
169
        setComponentAlignment(createdLabel, Alignment.BOTTOM_RIGHT);
170

    
171
        publishedLabel.setStyleName(LABEL_NOWRAP);
172
        publishedLabel.setContentMode(ContentMode.HTML);
173
        publishedLabel.setWidthUndefined();
174
        publishedLabel.setVisible(false);
175
        addComponent(publishedLabel, 2, 2);
176
        setComponentAlignment(publishedLabel, Alignment.BOTTOM_RIGHT);
177

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

    
185
    }
186

    
187
    public void setItem(RegistrationDTO regDto, AbstractView<?> parentView){
188

    
189
        this.parentView = parentView;
190

    
191
        this.regDto = regDto;
192

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

    
203
        updateUI(regDto.getBibliographicInRefCitationString(), regDto.getCreated(), regDto.getDatePublished(), regDto.getValidationProblems().size(),
204
                navigationEvent, null, regDto, regDto.getSubmitterUserName());
205
    }
206

    
207
    public void setWorkingSet(RegistrationWorkingSet workingSet, AbstractView<?> parentView){
208
        this.parentView = parentView;
209

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

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

    
242

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

    
250
        StringBuffer labelMarkup = new StringBuffer();
251
        DateTime registrationDate = null;
252

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

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

    
283
        labelMarkup.append(citationString);
284

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

    
293
        if(openButtonIcon != null){
294
            getOpenButton().setIcon(openButtonIcon);
295
        }
296

    
297
        if(regDto != null){
298
            labelMarkup.append("</br>").append(regDto.getSummary());
299

    
300
            stateLabel.setVisible(true);
301
            stateLabel.update(regDto.getStatus());
302
            if(regDto.getIdentifier() != null){
303
                getIdentifierLink().setResource(new ExternalResource(regDto.getIdentifier()));
304
            }
305
            getIdentifierLink().setCaption(regDto.getIdentifier());
306
            //TODO make responsive and use specificIdentifier in case the space gets too narrow
307
            getIdentifierLink().setVisible(true);
308
            getIdentifierLink().setEnabled(regDto.getStatus() == RegistrationStatus.PUBLISHED);
309

    
310
            registrationDate = regDto.getRegistrationDate();
311
        }
312

    
313
        getCitationSummaryLabel().setValue(labelMarkup.toString());
314
        getSubmitterLabel().setValue(submitterUserName);
315
        getSubmitterLabel().setVisible(submitterUserName != null);
316
        updateDateLabels(created, datePublished, registrationDate);
317
    }
318

    
319

    
320
    private void updateDateLabels(DateTime created, TimePeriod datePublished, DateTime released) {
321
        if(created != null){
322
            getCreatedLabel().setValue("<span class=\"caption\">" + LABEL_CAPTION_CREATED + "</span>&nbsp;" + created.toString(ISODateTimeFormat.yearMonthDay()));
323
        }
324
        if(datePublished != null){
325
            getPublishedLabel().setVisible(true);
326
            getPublishedLabel().setValue("<span class=\"caption\">" + LABEL_CAPTION_PUBLISHED + "</span>&nbsp;" + timePeriodFormatter.print(datePublished));
327
        }
328
        if(released != null){
329
            getReleasedLabel().setVisible(true);
330
            getReleasedLabel().setValue("<span class=\"caption\">" + LABEL_CAPTION_RELEASED + "</span>&nbsp;" + released.toString(ISODateTimeFormat.yearMonthDay()));
331
        }
332
    }
333

    
334

    
335
    private void publishEvent(Object event) {
336
        if(event instanceof NavigationEvent){
337
            parentView.getViewEventBus().publish(EventScope.UI, this, event);
338
        } else {
339
            parentView.getViewEventBus().publish(this, event);
340
        }
341
    }
342

    
343
    public UUID getRegistrationUuid(){
344
        return regDto.getUuid();
345
    }
346

    
347
    /**
348
     * @param showBlockingRelations the showBlockingRelations to set
349
     */
350
    public void showBlockingRegistrations(Set<RegistrationDTO> blockingRegDTOs) {
351

    
352
        if(blockingRelationsPanel == null) {
353

    
354
            if(regDto.isBlocked() && blockingRegDTOs.isEmpty()){
355
                throw new RuntimeException("Registration is blocked but tet of blocking registrations is empty");
356
            }
357
            if(!regDto.isBlocked() && !blockingRegDTOs.isEmpty()){
358
                throw new RuntimeException("No point showing blocking registrations for an unblocked registration");
359
            }
360

    
361
            blockingRelationsPanel = new RegistrationItemsPanel(parentView, "blocked by", blockingRegDTOs, cache);
362
            addComponent(blockingRelationsPanel, 0, 4, GRID_COLS - 1, 4);
363
        }
364

    
365
    }
366

    
367
    /* ====== RegistrationItemDesign Getters ====== */
368
    /**
369
     * @return the typeStateLabel
370
     */
371
    public Label getTypeStateLabel() {
372
        return stateLabel;
373
    }
374

    
375
    /**
376
     * @return the identifierLink
377
     */
378
    public Link getIdentifierLink() {
379
        return identifierLink;
380
    }
381

    
382
    /**
383
     * @return the citationSummaryLabel
384
     */
385
    public Label getCitationSummaryLabel() {
386
        return citationSummaryLabel;
387
    }
388

    
389
    /**
390
     * @return the blockedByButton
391
     */
392
    public Button getBlockedByButton() {
393
        return blockedByButton;
394
    }
395

    
396
    /**
397
     * @return the validationProblemsButton
398
     */
399
    public Button getValidationProblemsButton() {
400
        return validationProblemsButton;
401
    }
402

    
403
    /**
404
     * @return the openButton
405
     */
406
    public Button getOpenButton() {
407
        return openButton;
408
    }
409

    
410
    /**
411
     * @return the createdLabel
412
     */
413
    public Label getCreatedLabel() {
414
        return createdLabel;
415
    }
416

    
417
    /**
418
     * @return the publishedLabel
419
     */
420
    public Label getPublishedLabel() {
421
        return publishedLabel;
422
    }
423

    
424

    
425
    /**
426
     * @return
427
     */
428
    public Label getReleasedLabel() {
429
        return releasedLabel;
430
    }
431

    
432
    /**
433
     * @return the submitterLabel
434
     */
435
    public Label getSubmitterLabel() {
436
        return submitterLabel;
437
    }
438

    
439
    /**
440
     * @return the showBlockingRelations
441
     */
442
    public boolean isShowBlockingRelations() {
443
        return blockingRelationsPanel != null;
444
    }
445

    
446

    
447
   /* --------------------------------------- */
448

    
449
}
(2-2/11)