Project

General

Profile

Download (15.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

    
16
import org.joda.time.DateTime;
17
import org.joda.time.format.ISODateTimeFormat;
18
import org.vaadin.spring.events.EventScope;
19

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

    
36
import eu.etaxonomy.cdm.model.common.TimePeriod;
37
import eu.etaxonomy.cdm.model.name.RegistrationStatus;
38
import eu.etaxonomy.cdm.model.reference.Reference;
39
import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
40
import eu.etaxonomy.cdm.vaadin.event.ReferenceEditorAction;
41
import eu.etaxonomy.cdm.vaadin.event.ShowDetailsEvent;
42
import eu.etaxonomy.cdm.vaadin.model.registration.RegistrationWorkingSet;
43
import eu.etaxonomy.cdm.vaadin.security.PermissionDebugUtils;
44
import eu.etaxonomy.cdm.vaadin.security.UserHelper;
45
import eu.etaxonomy.cdm.vaadin.util.formatter.DateTimeFormat;
46
import eu.etaxonomy.cdm.vaadin.util.formatter.TimePeriodFormatter;
47
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationDTO;
48
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationTypeConverter;
49
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationWorksetViewBean;
50
import eu.etaxonomy.vaadin.event.EditorActionType;
51
import eu.etaxonomy.vaadin.mvp.AbstractView;
52
import eu.etaxonomy.vaadin.ui.navigation.NavigationEvent;
53

    
54
/**
55
 * @author a.kohlbecker
56
 * @since Mar 17, 2017
57
 *
58
 */
59
public class RegistrationItem extends GridLayout {
60

    
61

    
62
    private static final String LABEL_CAPTION_CREATED = "Created";
63

    
64
    private static final String LABEL_CAPTION_PUBLISHED = "Published";
65

    
66
    private static final String LABEL_CAPTION_RELEASED = "Released";
67

    
68
    private static final int GRID_ROWS = 5;
69

    
70
    private static final int GRID_COLS = 3;
71

    
72
    private static final long serialVersionUID = -211003770452173644L;
73

    
74
    private RegistrationTypeConverter regTypeConverter = new RegistrationTypeConverter();
75

    
76
    private AbstractView<?> parentView;
77

    
78
    private RegistrationDTO regDto;
79

    
80
    private boolean showBlockingRelations = false;
81

    
82
    private TimePeriodFormatter timePeriodFormatter = new TimePeriodFormatter(DateTimeFormat.ISO8601_DATE);
83

    
84
    // --------------------------------------------------
85

    
86
    private RegistrationStateLabel stateLabel = new RegistrationStateLabel();
87
    private Link identifierLink = new Link();
88
    private Label citationSummaryLabel = new Label();
89
    private Button blockedByButton = new Button(FontAwesome.WARNING);
90
    private Button messageButton;
91
    private Button openButton = new Button(FontAwesome.COGS);
92
    private Label submitterLabel = new Label();
93
    private Label createdLabel = new Label();
94
    private Label publishedLabel = new Label();
95
    private Label releasedLabel = new Label();
96

    
97
    private Panel blockingRelationsPanel;
98

    
99
    /**
100
     *
101
     */
102
    public RegistrationItem(RegistrationDTO item, AbstractView<?> parentView) {
103
        super(GRID_COLS, GRID_ROWS);
104
        init();
105
        setItem(item, parentView);
106
    }
107

    
108
    /**
109
    *
110
    */
111
   public RegistrationItem(RegistrationWorkingSet workingSet, AbstractView<?> parentView) {
112
       super(GRID_COLS, GRID_ROWS);
113
       init();
114
       setWorkingSet(workingSet, parentView);
115
   }
116

    
117
    public void init() {
118

    
119
        setWidth(100, Unit.PERCENTAGE);
120
        addStyleName("registration-list-item");
121

    
122
        CssLayout stateUserContainer = new CssLayout();
123
        stateLabel.setStyleName(LABEL_NOWRAP + " registration-state");
124
        stateLabel.setVisible(false);
125

    
126
        submitterLabel.setStyleName(LABEL_NOWRAP + " submitter");
127
        submitterLabel.setIcon(FontAwesome.USER);
128
        submitterLabel.setContentMode(ContentMode.HTML);
129
        submitterLabel.setVisible(false);
130

    
131
        stateUserContainer.addComponents(stateLabel, submitterLabel);
132
        addComponent(stateUserContainer, 0, 0);
133
        setComponentAlignment(stateUserContainer, Alignment.TOP_LEFT);
134

    
135
        identifierLink.setVisible(false);
136
        addComponent(identifierLink, 1, 0);
137
        setComponentAlignment(identifierLink, Alignment.TOP_CENTER);
138
        setColumnExpandRatio(1, 1.0f);
139

    
140
        messageButton = new Button(FontAwesome.COMMENT);
141
        CssLayout buttonGroup = new CssLayout(blockedByButton, messageButton, openButton);
142
        blockedByButton.setStyleName(ValoTheme.BUTTON_TINY);
143
        blockedByButton.setEnabled(false);
144
        messageButton.setStyleName(ValoTheme.BUTTON_TINY);
145
        messageButton.setEnabled(false);
146

    
147
        openButton.setStyleName(ValoTheme.BUTTON_TINY);
148
        openButton.addStyleName(ValoTheme.BUTTON_PRIMARY);
149
        openButton.setVisible(false);
150

    
151
        buttonGroup.setStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
152
        addComponent(buttonGroup, 2, 0);
153
        setComponentAlignment(buttonGroup, Alignment.TOP_RIGHT);
154

    
155
        citationSummaryLabel.setContentMode(ContentMode.HTML);
156
        addComponent(citationSummaryLabel, 0, 1, 1, 3);
157

    
158
        createdLabel.setStyleName(LABEL_NOWRAP);
159
        createdLabel.setContentMode(ContentMode.HTML);
160
        createdLabel.setWidthUndefined();
161
        addComponent(createdLabel, 2, 1);
162
        setComponentAlignment(createdLabel, Alignment.BOTTOM_RIGHT);
163

    
164
        publishedLabel.setStyleName(LABEL_NOWRAP);
165
        publishedLabel.setContentMode(ContentMode.HTML);
166
        publishedLabel.setWidthUndefined();
167
        publishedLabel.setVisible(false);
168
        addComponent(publishedLabel, 2, 2);
169
        setComponentAlignment(publishedLabel, Alignment.BOTTOM_RIGHT);
170

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

    
178
    }
179

    
180
    public void setItem(RegistrationDTO regDto, AbstractView<?> parentView){
181

    
182
        this.parentView = parentView;
183

    
184
        this.regDto = regDto;
185

    
186
        NavigationEvent navigationEvent = null;
187
        if(regDto.getCitationID() != null) {
188
            navigationEvent = new NavigationEvent(
189
                    RegistrationWorksetViewBean.NAME,
190
                    Integer.toString(regDto.getCitationID())
191
                    );
192
        } else {
193
            setComponentError(new UserError("Citation is missing"));
194
        }
195

    
196
        if(showBlockingRelations){
197
            createBlockingRelationsContent();
198
        }
199

    
200
        updateUI(regDto.getBibliographicCitationString(), regDto.getCreated(), regDto.getDatePublished(), regDto.getMessages().size(),
201
                navigationEvent, null, regDto, regDto.getSubmitterUserName());
202
    }
203

    
204
    public void setWorkingSet(RegistrationWorkingSet workingSet, AbstractView<?> parentView){
205
        this.parentView = parentView;
206

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

    
228
    private void createBlockingRelationsContent(){
229

    
230
        if(blockingRelationsPanel == null && !regDto.getBlockedBy().isEmpty()){
231
            blockingRelationsPanel = new Panel("blocked by");
232
            Layout blockingRelations = new CssLayout();
233
            blockingRelations.setWidth(100, Unit.PERCENTAGE);
234
            blockingRelationsPanel.setContent(blockingRelations);
235
            for(RegistrationDTO blockRegDto : regDto.getBlockedBy()){
236
                RegistrationItem blockRegItem = new RegistrationItem(blockRegDto, parentView);
237
                blockingRelations.addComponent(blockRegItem);
238
            }
239
            blockingRelationsPanel.setWidth(100, Unit.PERCENTAGE);
240
            addComponent(blockingRelationsPanel, 0, 4, GRID_COLS - 1, 4);
241
        }
242

    
243
    }
244

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

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

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

    
280
        if(regDto != null && !regDto.getBlockedBy().isEmpty()){
281
            getBlockedByButton().setEnabled(true);
282
            getBlockedByButton().addStyleName("blocked");
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
            labelMarkup.append("</br>").append(regDto.getSummary());
301

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

    
312
            registrationDate = regDto.getRegistrationDate();
313
        }
314

    
315
        getCitationSummaryLabel().setValue(labelMarkup.toString());
316
        getSubmitterLabel().setValue(submitterUserName);
317
        updateDateLabels(created, datePublished, registrationDate);
318
    }
319

    
320

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

    
335

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

    
344
    /* ====== RegistrationItemDesign Getters ====== */
345
    /**
346
     * @return the typeStateLabel
347
     */
348
    public Label getTypeStateLabel() {
349
        return stateLabel;
350
    }
351

    
352
    /**
353
     * @return the identifierLink
354
     */
355
    public Link getIdentifierLink() {
356
        return identifierLink;
357
    }
358

    
359
    /**
360
     * @return the citationSummaryLabel
361
     */
362
    public Label getCitationSummaryLabel() {
363
        return citationSummaryLabel;
364
    }
365

    
366
    /**
367
     * @return the blockedByButton
368
     */
369
    public Button getBlockedByButton() {
370
        return blockedByButton;
371
    }
372

    
373
    /**
374
     * @return the messageButton
375
     */
376
    public Button getMessageButton() {
377
        return messageButton;
378
    }
379

    
380
    /**
381
     * @return the openButton
382
     */
383
    public Button getOpenButton() {
384
        return openButton;
385
    }
386

    
387
    /**
388
     * @return the createdLabel
389
     */
390
    public Label getCreatedLabel() {
391
        return createdLabel;
392
    }
393

    
394
    /**
395
     * @return the publishedLabel
396
     */
397
    public Label getPublishedLabel() {
398
        return publishedLabel;
399
    }
400

    
401

    
402
    /**
403
     * @return
404
     */
405
    public Label getReleasedLabel() {
406
        return releasedLabel;
407
    }
408

    
409
    /**
410
     * @return the submitterLabel
411
     */
412
    public Label getSubmitterLabel() {
413
        return submitterLabel;
414
    }
415

    
416
    /**
417
     * @return the showBlockingRelations
418
     */
419
    public boolean isShowBlockingRelations() {
420
        return showBlockingRelations;
421
    }
422

    
423
    /**
424
     * @param showBlockingRelations the showBlockingRelations to set
425
     */
426
    public void setShowBlockingRelations(boolean showBlockingRelations) {
427
        if(showBlockingRelations && !this.showBlockingRelations){
428
            createBlockingRelationsContent();
429
        }
430
        this.showBlockingRelations = showBlockingRelations;
431
    }
432

    
433

    
434

    
435
   /* --------------------------------------- */
436

    
437
}
(2-2/8)