Project

General

Profile

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

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

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

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

    
58

    
59
    private static final String LABEL_CAPTION_CREATED = "Created";
60

    
61
    private static final String LABEL_CAPTION_PUBLISHED = "Published";
62

    
63
    private static final String LABEL_CAPTION_RELEASED = "Released";
64

    
65
    private static final int GRID_ROWS = 5;
66

    
67
    private static final int GRID_COLS = 3;
68

    
69
    private static final long serialVersionUID = -211003770452173644L;
70

    
71
    private RegistrationTypeConverter regTypeConverter = new RegistrationTypeConverter();
72

    
73
    private AbstractView<?> parentView;
74

    
75
    private TimePeriodFormatter timePeriodFormatter = new TimePeriodFormatter(DateTimeFormat.ISO8601_DATE);
76

    
77
    // --------------------------------------------------
78

    
79
    private RegistrationStateLabel stateLabel = new RegistrationStateLabel();
80
    private Link identifierLink = new Link();
81
    private Label citationSummaryLabel = new Label();
82
    private Button blockedByButton = new Button(FontAwesome.WARNING);
83
    private Button messageButton;
84
    private Button openButton = new Button(FontAwesome.COGS);
85
    private Label submitterLabel = new Label();
86
    private Label createdLabel = new Label();
87
    private Label publishedLabel = new Label();
88
    private Label releasedLabel = new Label();
89

    
90
    /**
91
     *
92
     */
93
    public RegistrationItem(RegistrationDTO item, AbstractView<?> parentView) {
94
        super(GRID_COLS, GRID_ROWS);
95
        init();
96
        setItem(item, parentView);
97
    }
98

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

    
108
    public void init() {
109

    
110
        setWidth(100, Unit.PERCENTAGE);
111
        addStyleName("registration-list-item");
112

    
113
        CssLayout stateUserContainer = new CssLayout();
114
        stateLabel.setStyleName(LABEL_NOWRAP + " registration-state");
115
        stateLabel.setVisible(false);
116

    
117
        submitterLabel.setStyleName(LABEL_NOWRAP + " submitter");
118
        submitterLabel.setIcon(FontAwesome.USER);
119
        submitterLabel.setContentMode(ContentMode.HTML);
120
        submitterLabel.setVisible(false);
121

    
122
        stateUserContainer.addComponents(stateLabel, submitterLabel);
123
        addComponent(stateUserContainer, 0, 0);
124
        setComponentAlignment(stateUserContainer, Alignment.TOP_LEFT);
125

    
126
        identifierLink.setVisible(false);
127
        addComponent(identifierLink, 1, 0);
128
        setComponentAlignment(identifierLink, Alignment.TOP_CENTER);
129
        setColumnExpandRatio(1, 1.0f);
130

    
131
        messageButton = new Button(FontAwesome.COMMENT);
132
        CssLayout buttonGroup = new CssLayout(blockedByButton, messageButton, openButton);
133
        blockedByButton.setStyleName(ValoTheme.BUTTON_TINY);
134
        blockedByButton.setEnabled(false);
135
        messageButton.setStyleName(ValoTheme.BUTTON_TINY);
136
        messageButton.setEnabled(false);
137

    
138
        openButton.setStyleName(ValoTheme.BUTTON_TINY);
139
        openButton.addStyleName(ValoTheme.BUTTON_PRIMARY);
140
        openButton.setVisible(false);
141

    
142
        buttonGroup.setStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
143
        addComponent(buttonGroup, 2, 0);
144
        setComponentAlignment(buttonGroup, Alignment.TOP_RIGHT);
145

    
146
        citationSummaryLabel.setContentMode(ContentMode.HTML);
147
        addComponent(citationSummaryLabel, 0, 1, 1, 3);
148

    
149
        createdLabel.setStyleName(LABEL_NOWRAP);
150
        createdLabel.setContentMode(ContentMode.HTML);
151
        createdLabel.setWidthUndefined();
152
        addComponent(createdLabel, 2, 1);
153
        setComponentAlignment(createdLabel, Alignment.BOTTOM_RIGHT);
154

    
155
        publishedLabel.setStyleName(LABEL_NOWRAP);
156
        publishedLabel.setContentMode(ContentMode.HTML);
157
        publishedLabel.setWidthUndefined();
158
        publishedLabel.setVisible(false);
159
        addComponent(publishedLabel, 2, 2);
160
        setComponentAlignment(publishedLabel, Alignment.BOTTOM_RIGHT);
161

    
162
        releasedLabel.setStyleName(LABEL_NOWRAP);
163
        releasedLabel.setContentMode(ContentMode.HTML);
164
        releasedLabel.setWidthUndefined();
165
        releasedLabel.setVisible(false);
166
        addComponent(releasedLabel, 2, 3);
167
        setComponentAlignment(releasedLabel, Alignment.BOTTOM_RIGHT);
168

    
169
    }
170

    
171
    public void setItem(RegistrationDTO regDto, AbstractView<?> parentView){
172
        this.parentView = parentView;
173

    
174
        NavigationEvent navigationEvent = null;
175
        if(regDto.getCitationID() != null) {
176
            navigationEvent = new NavigationEvent(
177
                    RegistrationWorksetViewBean.NAME,
178
                    Integer.toString(regDto.getCitationID())
179
                    );
180
        } else {
181
            setComponentError(new UserError("Citation is missing"));
182
        }
183

    
184
        updateUI(regDto.getBibliographicCitationString(), regDto.getCreated(), regDto.getDatePublished(), regDto.getMessages().size(),
185
                navigationEvent, null, regDto, regDto.getSubmitterUserName());
186
    }
187

    
188
    public void setWorkingSet(RegistrationWorkingSet workingSet, AbstractView<?> parentView){
189
        this.parentView = parentView;
190

    
191
        ReferenceEditorAction referenceEditorAction = null;
192
        if(workingSet.getCitationId() != null){
193
            if(UserHelper.fromSession().userHasPermission(Reference.class, workingSet.getCitationId(), CRUD.UPDATE)){
194
                referenceEditorAction = new ReferenceEditorAction(EditorActionType.EDIT, workingSet.getCitationId());
195
            }
196
            PermissionDebugUtils.addGainPerEntityPermissionButton(this, Reference.class, workingSet.getCitationId(), EnumSet.of(CRUD.UPDATE, CRUD.DELETE), null);
197
        } else {
198
            if(UserHelper.fromSession().userHasPermission(Reference.class, CRUD.CREATE)){
199
                referenceEditorAction = new ReferenceEditorAction(EditorActionType.ADD);
200
            }
201
        }
202
        TimePeriod datePublished = null;
203
        String submitterName = null;
204
        if(workingSet.getRegistrationDTOs().size() > 0){
205
            datePublished = workingSet.getRegistrationDTOs().get(0).getDatePublished();
206
            submitterName = workingSet.getRegistrationDTOs().get(0).getSubmitterUserName();
207
        }
208
        updateUI(workingSet.getCitation(), workingSet.getCreated(), datePublished, workingSet.messagesCount(),
209
                referenceEditorAction, FontAwesome.EDIT, null, submitterName);
210
    }
211

    
212
    /**
213
     * @param submitterUserName TODO
214
     *
215
     */
216
    private void updateUI(String citationString,  DateTime created, TimePeriod datePublished,  int messagesCount,
217
            Object openButtonEvent, Resource openButtonIcon, RegistrationDTO regDto, String submitterUserName) {
218

    
219
        StringBuffer labelMarkup = new StringBuffer();
220
        DateTime registrationDate = null;
221

    
222
        if(messagesCount > 0){
223
            getMessageButton().setEnabled(true);
224
            // getMessageButton().addStyleName(RegistrationStyles.STYLE_FRIENDLY_FOREGROUND);
225
            getMessageButton().addClickListener(e -> {
226
                ShowDetailsEvent detailsEvent;
227
                if(regDto != null){
228
                    detailsEvent = new ShowDetailsEvent<RegistrationDTO, Integer>(
229
                            e,
230
                            RegistrationDTO.class,
231
                            regDto.getId(),
232
                            "messages");
233
                } else {
234
                    detailsEvent = new ShowDetailsEvent<RegistrationWorkingSet, Integer>(
235
                            e,
236
                            RegistrationWorkingSet.class,
237
                            null,
238
                            "messages");
239
                }
240
                publishEvent(detailsEvent);
241
                }
242
            );
243
            getMessageButton().setCaption("<span class=\"" + RegistrationStyles.BUTTON_BADGE +"\"> " + messagesCount + "</span>");
244
            getMessageButton().setCaptionAsHtml(true);
245
        }
246

    
247
        labelMarkup.append(citationString);
248

    
249
        if(openButtonEvent != null){
250
            // Buttons
251
            getOpenButton().setVisible(true);
252
            Collection<?> removeCandidates = getOpenButton().getListeners(ClickListener.class);
253
            removeCandidates.forEach(l -> getOpenButton().removeClickListener((ClickListener)l));
254
            getOpenButton().addClickListener(e -> publishEvent(openButtonEvent));
255
        }
256

    
257
        if(openButtonIcon != null){
258
            getOpenButton().setIcon(openButtonIcon);
259
        }
260

    
261
        if(regDto != null){
262
            labelMarkup.append("</br>").append(regDto.getSummary());
263

    
264
            stateLabel.setVisible(true);
265
            stateLabel.update(regDto.getStatus());
266
            if(regDto.getIdentifier() != null){
267
                getIdentifierLink().setResource(new ExternalResource(regDto.getIdentifier()));
268
            }
269
            getIdentifierLink().setCaption(regDto.getIdentifier());
270
            //TODO make responsive and use specificIdentifier in case the space gets too narrow
271
            getIdentifierLink().setVisible(true);
272
            getIdentifierLink().setEnabled(regDto.getStatus() == RegistrationStatus.PUBLISHED);
273

    
274
            registrationDate = regDto.getRegistrationDate();
275
        }
276

    
277
        getCitationSummaryLabel().setValue(labelMarkup.toString());
278
        getSubmitterLabel().setValue(submitterUserName);
279
        updateDateLabels(created, datePublished, registrationDate);
280
    }
281

    
282

    
283
    private void updateDateLabels(DateTime created, TimePeriod datePublished, DateTime released) {
284
        if(created != null){
285
            getCreatedLabel().setValue("<span class=\"caption\">" + LABEL_CAPTION_CREATED + "</span>&nbsp;" + created.toString(ISODateTimeFormat.yearMonthDay()));
286
        }
287
        if(datePublished != null){
288
            getPublishedLabel().setVisible(true);
289
            getPublishedLabel().setValue("<span class=\"caption\">" + LABEL_CAPTION_PUBLISHED + "</span>&nbsp;" + timePeriodFormatter.print(datePublished));
290
        }
291
        if(released != null){
292
            getReleasedLabel().setVisible(true);
293
            getReleasedLabel().setValue("<span class=\"caption\">" + LABEL_CAPTION_RELEASED + "</span>&nbsp;" + released.toString(ISODateTimeFormat.yearMonthDay()));
294
        }
295
    }
296

    
297

    
298
    private void publishEvent(Object event) {
299
        parentView.getEventBus().publishEvent(event);
300
    }
301

    
302
    /* ====== RegistrationItemDesign Getters ====== */
303
    /**
304
     * @return the typeStateLabel
305
     */
306
    public Label getTypeStateLabel() {
307
        return stateLabel;
308
    }
309

    
310
    /**
311
     * @return the identifierLink
312
     */
313
    public Link getIdentifierLink() {
314
        return identifierLink;
315
    }
316

    
317
    /**
318
     * @return the citationSummaryLabel
319
     */
320
    public Label getCitationSummaryLabel() {
321
        return citationSummaryLabel;
322
    }
323

    
324
    /**
325
     * @return the blockedByButton
326
     */
327
    public Button getBlockedByButton() {
328
        return blockedByButton;
329
    }
330

    
331
    /**
332
     * @return the messageButton
333
     */
334
    public Button getMessageButton() {
335
        return messageButton;
336
    }
337

    
338
    /**
339
     * @return the openButton
340
     */
341
    public Button getOpenButton() {
342
        return openButton;
343
    }
344

    
345
    /**
346
     * @return the createdLabel
347
     */
348
    public Label getCreatedLabel() {
349
        return createdLabel;
350
    }
351

    
352
    /**
353
     * @return the publishedLabel
354
     */
355
    public Label getPublishedLabel() {
356
        return publishedLabel;
357
    }
358

    
359

    
360
    /**
361
     * @return
362
     */
363
    public Label getReleasedLabel() {
364
        return releasedLabel;
365
    }
366

    
367
    /**
368
     * @return the submitterLabel
369
     */
370
    public Label getSubmitterLabel() {
371
        return submitterLabel;
372
    }
373

    
374

    
375
   /* --------------------------------------- */
376

    
377
}
(2-2/8)