Project

General

Profile

Download (14.9 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.Link;
32
import com.vaadin.ui.Panel;
33
import com.vaadin.ui.themes.ValoTheme;
34

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

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

    
60

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

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

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

    
67
    private static final int GRID_ROWS = 5;
68

    
69
    private static final int GRID_COLS = 3;
70

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

    
73
    private RegistrationTypeConverter regTypeConverter = new RegistrationTypeConverter();
74

    
75
    private AbstractView<?> parentView;
76

    
77
    private RegistrationDTO regDto;
78

    
79
    private boolean showBlockingRelations = false;
80

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

    
83
    // --------------------------------------------------
84

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

    
96
    private Panel blockingRelationsPanel;
97

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

    
107
    /**
108
    *
109
    */
110
   public RegistrationItem(RegistrationWorkingSet workingSet, AbstractView<?> parentView) {
111
       super(GRID_COLS, GRID_ROWS);
112
       init();
113
       blockedByButton.setVisible(false);
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 RegistrationItemsPanel(parentView, "blocked by", regDto.getBlockedBy());
232
            addComponent(blockingRelationsPanel, 0, 4, GRID_COLS - 1, 4);
233
        }
234

    
235
    }
236

    
237
    /**
238
     * @param submitterUserName TODO
239
     *
240
     */
241
    private void updateUI(String citationString,  DateTime created, TimePeriod datePublished,  int messagesCount,
242
            Object openButtonEvent, Resource openButtonIcon, RegistrationDTO regDto, String submitterUserName) {
243

    
244
        StringBuffer labelMarkup = new StringBuffer();
245
        DateTime registrationDate = null;
246

    
247
        if(messagesCount > 0){
248
            getMessageButton().setEnabled(true);
249
            // getMessageButton().addStyleName(RegistrationStyles.STYLE_FRIENDLY_FOREGROUND);
250
            getMessageButton().addClickListener(e -> {
251
                ShowDetailsEvent detailsEvent;
252
                if(regDto != null){
253
                    detailsEvent = new ShowDetailsEvent<RegistrationDTO, Integer>(
254
                            e,
255
                            RegistrationDTO.class,
256
                            regDto.getId(),
257
                            "messages");
258
                } else {
259
                    detailsEvent = new ShowDetailsEvent<RegistrationWorkingSet, Integer>(
260
                            e,
261
                            RegistrationWorkingSet.class,
262
                            null,
263
                            "messages");
264
                }
265
                publishEvent(detailsEvent);
266
                }
267
            );
268
            getMessageButton().setCaption("<span class=\"" + RegistrationStyles.BUTTON_BADGE +"\"> " + messagesCount + "</span>");
269
            getMessageButton().setCaptionAsHtml(true);
270
        }
271

    
272
        if(regDto != null && !regDto.isBlocked()){
273
            getBlockedByButton().setEnabled(true);
274
            getBlockedByButton().addStyleName("blocked");
275
        }
276

    
277
        labelMarkup.append(citationString);
278

    
279
        if(openButtonEvent != null){
280
            // Buttons
281
            getOpenButton().setVisible(true);
282
            Collection<?> removeCandidates = getOpenButton().getListeners(ClickListener.class);
283
            removeCandidates.forEach(l -> getOpenButton().removeClickListener((ClickListener)l));
284
            getOpenButton().addClickListener(e -> publishEvent(openButtonEvent));
285
        }
286

    
287
        if(openButtonIcon != null){
288
            getOpenButton().setIcon(openButtonIcon);
289
        }
290

    
291
        if(regDto != null){
292
            labelMarkup.append("</br>").append(regDto.getSummary());
293

    
294
            stateLabel.setVisible(true);
295
            stateLabel.update(regDto.getStatus());
296
            if(regDto.getIdentifier() != null){
297
                getIdentifierLink().setResource(new ExternalResource(regDto.getIdentifier()));
298
            }
299
            getIdentifierLink().setCaption(regDto.getIdentifier());
300
            //TODO make responsive and use specificIdentifier in case the space gets too narrow
301
            getIdentifierLink().setVisible(true);
302
            getIdentifierLink().setEnabled(regDto.getStatus() == RegistrationStatus.PUBLISHED);
303

    
304
            registrationDate = regDto.getRegistrationDate();
305
        }
306

    
307
        getCitationSummaryLabel().setValue(labelMarkup.toString());
308
        getSubmitterLabel().setValue(submitterUserName);
309
        updateDateLabels(created, datePublished, registrationDate);
310
    }
311

    
312

    
313
    private void updateDateLabels(DateTime created, TimePeriod datePublished, DateTime released) {
314
        if(created != null){
315
            getCreatedLabel().setValue("<span class=\"caption\">" + LABEL_CAPTION_CREATED + "</span>&nbsp;" + created.toString(ISODateTimeFormat.yearMonthDay()));
316
        }
317
        if(datePublished != null){
318
            getPublishedLabel().setVisible(true);
319
            getPublishedLabel().setValue("<span class=\"caption\">" + LABEL_CAPTION_PUBLISHED + "</span>&nbsp;" + timePeriodFormatter.print(datePublished));
320
        }
321
        if(released != null){
322
            getReleasedLabel().setVisible(true);
323
            getReleasedLabel().setValue("<span class=\"caption\">" + LABEL_CAPTION_RELEASED + "</span>&nbsp;" + released.toString(ISODateTimeFormat.yearMonthDay()));
324
        }
325
    }
326

    
327

    
328
    private void publishEvent(Object event) {
329
        if(event instanceof NavigationEvent){
330
            parentView.getViewEventBus().publish(EventScope.UI, this, event);
331
        } else {
332
            parentView.getViewEventBus().publish(this, event);
333
        }
334
    }
335

    
336
    /* ====== RegistrationItemDesign Getters ====== */
337
    /**
338
     * @return the typeStateLabel
339
     */
340
    public Label getTypeStateLabel() {
341
        return stateLabel;
342
    }
343

    
344
    /**
345
     * @return the identifierLink
346
     */
347
    public Link getIdentifierLink() {
348
        return identifierLink;
349
    }
350

    
351
    /**
352
     * @return the citationSummaryLabel
353
     */
354
    public Label getCitationSummaryLabel() {
355
        return citationSummaryLabel;
356
    }
357

    
358
    /**
359
     * @return the blockedByButton
360
     */
361
    public Button getBlockedByButton() {
362
        return blockedByButton;
363
    }
364

    
365
    /**
366
     * @return the messageButton
367
     */
368
    public Button getMessageButton() {
369
        return messageButton;
370
    }
371

    
372
    /**
373
     * @return the openButton
374
     */
375
    public Button getOpenButton() {
376
        return openButton;
377
    }
378

    
379
    /**
380
     * @return the createdLabel
381
     */
382
    public Label getCreatedLabel() {
383
        return createdLabel;
384
    }
385

    
386
    /**
387
     * @return the publishedLabel
388
     */
389
    public Label getPublishedLabel() {
390
        return publishedLabel;
391
    }
392

    
393

    
394
    /**
395
     * @return
396
     */
397
    public Label getReleasedLabel() {
398
        return releasedLabel;
399
    }
400

    
401
    /**
402
     * @return the submitterLabel
403
     */
404
    public Label getSubmitterLabel() {
405
        return submitterLabel;
406
    }
407

    
408
    /**
409
     * @return the showBlockingRelations
410
     */
411
    public boolean isShowBlockingRelations() {
412
        return showBlockingRelations;
413
    }
414

    
415
    /**
416
     * @param showBlockingRelations the showBlockingRelations to set
417
     */
418
    public void setShowBlockingRelations(boolean showBlockingRelations) {
419
        if(showBlockingRelations && !this.showBlockingRelations){
420
            createBlockingRelationsContent();
421
        }
422
        this.showBlockingRelations = showBlockingRelations;
423
    }
424

    
425

    
426

    
427
   /* --------------------------------------- */
428

    
429
}
(2-2/9)