Project

General

Profile

Download (12.5 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

    
15
import org.joda.time.DateTime;
16
import org.joda.time.format.ISODateTimeFormat;
17

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

    
32
import eu.etaxonomy.cdm.model.common.TimePeriod;
33
import eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction.Action;
34
import eu.etaxonomy.cdm.vaadin.event.ReferenceEditorAction;
35
import eu.etaxonomy.cdm.vaadin.event.ShowDetailsEvent;
36
import eu.etaxonomy.cdm.vaadin.model.registration.RegistrationWorkingSet;
37
import eu.etaxonomy.cdm.vaadin.util.formatter.DateTimeFormat;
38
import eu.etaxonomy.cdm.vaadin.util.formatter.TimePeriodFormatter;
39
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationDTO;
40
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationTypeConverter;
41
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationWorkflowViewBean;
42
import eu.etaxonomy.vaadin.mvp.AbstractView;
43
import eu.etaxonomy.vaadin.ui.navigation.NavigationEvent;
44

    
45
/**
46
 * @author a.kohlbecker
47
 * @since Mar 17, 2017
48
 *
49
 */
50
public class RegistrationItem extends GridLayout {
51

    
52

    
53
    private static final String LABEL_CAPTION_CREATED = "Created";
54

    
55
    private static final String LABEL_CAPTION_PUBLISHED = "Published";
56

    
57
    private static final String LABEL_CAPTION_RELEASED = "Released";
58

    
59
    private static final int GRID_ROWS = 5;
60

    
61
    private static final int GRID_COLS = 3;
62

    
63
    private static final long serialVersionUID = -211003770452173644L;
64

    
65
    private RegistrationTypeConverter regTypeConverter = new RegistrationTypeConverter();
66

    
67
    private AbstractView<?> parentView;
68

    
69
    private TimePeriodFormatter timePeriodFormatter = new TimePeriodFormatter(DateTimeFormat.ISO8601_DATE);
70

    
71
    // --------------------------------------------------
72

    
73
    private RegistrationStateLabel stateLabel = new RegistrationStateLabel();
74
    private Link identifierLink = new Link();
75
    private Label citationSummaryLabel = new Label();
76
    private Button blockedByButton = new Button(FontAwesome.WARNING);
77
    private Button messageButton;
78
    private Button openButton = new Button(FontAwesome.COGS);
79
    private Label submitterLabel = new Label();
80
    private Label createdLabel = new Label();
81
    private Label publishedLabel = new Label();
82
    private Label releasedLabel = new Label();
83

    
84
    /**
85
     *
86
     */
87
    public RegistrationItem(RegistrationDTO item, AbstractView<?> parentView) {
88
        super(GRID_COLS, GRID_ROWS);
89
        init();
90
        setItem(item, parentView);
91
    }
92

    
93
    /**
94
    *
95
    */
96
   public RegistrationItem(RegistrationWorkingSet workingSet, AbstractView<?> parentView) {
97
       super(GRID_COLS, GRID_ROWS);
98
       init();
99
       setWorkingSet(workingSet, parentView);
100
   }
101

    
102
    public void init() {
103

    
104
        setWidth(100, Unit.PERCENTAGE);
105
        addStyleName("registration-list-item");
106

    
107
        CssLayout stateUserContainer = new CssLayout();
108
        stateLabel.setStyleName(LABEL_NOWRAP + " registration-state");
109
        stateLabel.setVisible(false);
110

    
111
        submitterLabel.setStyleName(LABEL_NOWRAP + " submitter");
112
        submitterLabel.setIcon(FontAwesome.USER);
113
        submitterLabel.setContentMode(ContentMode.HTML);
114
        submitterLabel.setVisible(false);
115

    
116
        stateUserContainer.addComponents(stateLabel, submitterLabel);
117
        addComponent(stateUserContainer, 0, 0);
118
        setComponentAlignment(stateUserContainer, Alignment.TOP_LEFT);
119

    
120
        identifierLink.setVisible(false);
121
        addComponent(identifierLink, 1, 0);
122
        setComponentAlignment(identifierLink, Alignment.TOP_CENTER);
123
        setColumnExpandRatio(1, 1.0f);
124

    
125
        messageButton = new Button(FontAwesome.COMMENT);
126
        CssLayout buttonGroup = new CssLayout(blockedByButton, messageButton, openButton);
127
        blockedByButton.setStyleName(ValoTheme.BUTTON_TINY);
128
        blockedByButton.setEnabled(false);
129
        messageButton.setStyleName(ValoTheme.BUTTON_TINY);
130
        messageButton.setEnabled(false);
131

    
132
        openButton.setStyleName(ValoTheme.BUTTON_TINY);
133
        openButton.addStyleName(ValoTheme.BUTTON_PRIMARY);
134
        openButton.setVisible(false);
135

    
136
        buttonGroup.setStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
137
        addComponent(buttonGroup, 2, 0);
138
        setComponentAlignment(buttonGroup, Alignment.TOP_RIGHT);
139

    
140
        citationSummaryLabel.setContentMode(ContentMode.HTML);
141
        addComponent(citationSummaryLabel, 0, 1, 1, 3);
142

    
143
        createdLabel.setStyleName(LABEL_NOWRAP);
144
        createdLabel.setContentMode(ContentMode.HTML);
145
        createdLabel.setWidthUndefined();
146
        addComponent(createdLabel, 2, 1);
147
        setComponentAlignment(createdLabel, Alignment.BOTTOM_RIGHT);
148

    
149
        publishedLabel.setStyleName(LABEL_NOWRAP);
150
        publishedLabel.setContentMode(ContentMode.HTML);
151
        publishedLabel.setWidthUndefined();
152
        publishedLabel.setVisible(false);
153
        addComponent(publishedLabel, 2, 2);
154
        setComponentAlignment(publishedLabel, Alignment.BOTTOM_RIGHT);
155

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

    
163
    }
164

    
165
    public void setItem(RegistrationDTO regDto, AbstractView<?> parentView){
166
        this.parentView = parentView;
167

    
168
        NavigationEvent navigationEvent = null;
169
        if(regDto.getCitationID() != null) {
170
            navigationEvent = new NavigationEvent(
171
                    RegistrationWorkflowViewBean.NAME,
172
                    Integer.toString(regDto.getCitationID())
173
                    );
174
        } else {
175
            setComponentError(new UserError("Citation is missing"));
176
        }
177

    
178
        updateUI(regDto.getBibliographicCitationString(), regDto.getCreated(), regDto.getDatePublished(),
179
                regDto.getMessages().size(),
180
                navigationEvent, null, regDto, regDto.getSubmitterUserName());
181
    }
182

    
183
    public void setWorkingSet(RegistrationWorkingSet workingSet, AbstractView<?> parentView){
184
        this.parentView = parentView;
185

    
186
        ReferenceEditorAction referenceEditorAction;
187
        if(workingSet.getCitationId() != null){
188
            referenceEditorAction = new ReferenceEditorAction(Action.EDIT, workingSet.getCitationId());
189
        } else {
190
            referenceEditorAction = new ReferenceEditorAction(Action.ADD);
191
        }
192
        TimePeriod datePublished = null;
193
        String submitterName = null;
194
        if(workingSet.getRegistrationDTOs().size() > 0){
195
            datePublished = workingSet.getRegistrationDTOs().get(0).getDatePublished();
196
            submitterName = workingSet.getRegistrationDTOs().get(0).getSubmitterUserName();
197
        }
198
        updateUI(workingSet.getCitation(), workingSet.getCreated(), datePublished, workingSet.messagesCount(),
199
                referenceEditorAction, FontAwesome.EDIT, null, submitterName);
200
    }
201

    
202
    /**
203
     * @param submitterUserName TODO
204
     *
205
     */
206
    private void updateUI(String citationString,  DateTime created, TimePeriod datePublished,  int messagesCount,
207
            Object openButtonEvent, Resource openButtonIcon, RegistrationDTO regDto, String submitterUserName) {
208

    
209
        StringBuffer labelMarkup = new StringBuffer();
210
        DateTime registrationDate = null;
211

    
212
        if(messagesCount > 0){
213
            getMessageButton().setEnabled(true);
214
            // getMessageButton().addStyleName(RegistrationStyles.STYLE_FRIENDLY_FOREGROUND);
215
            getMessageButton().addClickListener(e -> {
216
                ShowDetailsEvent detailsEvent;
217
                if(regDto != null){
218
                    detailsEvent = new ShowDetailsEvent<RegistrationDTO, Integer>(
219
                            e,
220
                            RegistrationDTO.class,
221
                            regDto.getId(),
222
                            "messages");
223
                } else {
224
                    detailsEvent = new ShowDetailsEvent<RegistrationWorkingSet, Integer>(
225
                            e,
226
                            RegistrationWorkingSet.class,
227
                            null,
228
                            "messages");
229
                }
230
                publishEvent(detailsEvent);
231
                }
232
            );
233
            getMessageButton().setCaption("<span class=\"" + RegistrationStyles.BUTTON_BADGE +"\"> " + messagesCount + "</span>");
234
            getMessageButton().setCaptionAsHtml(true);
235
        }
236

    
237
        labelMarkup.append(citationString);
238

    
239
        if(openButtonEvent != null){
240
            // Buttons
241
            getOpenButton().setVisible(true);
242
            Collection<?> removeCandidates = getOpenButton().getListeners(ClickListener.class);
243
            removeCandidates.forEach(l -> getOpenButton().removeClickListener((ClickListener)l));
244
            getOpenButton().addClickListener(e -> publishEvent(openButtonEvent));
245
        }
246

    
247
        if(openButtonIcon != null){
248
            getOpenButton().setIcon(openButtonIcon);
249
        }
250

    
251
        if(regDto != null){
252
            labelMarkup.append("</br>").append(regDto.getSummary());
253

    
254
            stateLabel.setVisible(true);
255
            stateLabel.update(regDto.getStatus());
256
            getIdentifierLink().setResource(new ExternalResource(regDto.getIdentifier()));
257
            //TODO make responsive and use specificIdentifier in case the space gets too narrow
258
            getIdentifierLink().setVisible(true);
259
            getIdentifierLink().setCaption(regDto.getIdentifier());
260

    
261
            registrationDate = regDto.getRegistrationDate();
262
        }
263

    
264
        getCitationSummaryLabel().setValue(labelMarkup.toString());
265
        getSubmitterLabel().setValue(submitterUserName);
266
        updateDateLabels(created, datePublished, registrationDate);
267
    }
268

    
269

    
270
    private void updateDateLabels(DateTime created, TimePeriod datePublished, DateTime released) {
271
        if(created != null){
272
            getCreatedLabel().setValue("<span class=\"caption\">" + LABEL_CAPTION_CREATED + "</span>&nbsp;" + created.toString(ISODateTimeFormat.yearMonthDay()));
273
        }
274
        if(datePublished != null){
275
            getPublishedLabel().setVisible(true);
276
            getPublishedLabel().setValue("<span class=\"caption\">" + LABEL_CAPTION_PUBLISHED + "</span>&nbsp;" + timePeriodFormatter.print(datePublished));
277
        }
278
        if(released != null){
279
            getReleasedLabel().setVisible(true);
280
            getReleasedLabel().setValue("<span class=\"caption\">" + LABEL_CAPTION_RELEASED + "</span>&nbsp;" + released.toString(ISODateTimeFormat.yearMonthDay()));
281
        }
282
    }
283

    
284

    
285
    private void publishEvent(Object event) {
286
        parentView.getEventBus().publishEvent(event);
287
    }
288

    
289
    /* ====== RegistrationItemDesign Getters ====== */
290
    /**
291
     * @return the typeStateLabel
292
     */
293
    public Label getTypeStateLabel() {
294
        return stateLabel;
295
    }
296

    
297
    /**
298
     * @return the identifierLink
299
     */
300
    public Link getIdentifierLink() {
301
        return identifierLink;
302
    }
303

    
304
    /**
305
     * @return the citationSummaryLabel
306
     */
307
    public Label getCitationSummaryLabel() {
308
        return citationSummaryLabel;
309
    }
310

    
311
    /**
312
     * @return the blockedByButton
313
     */
314
    public Button getBlockedByButton() {
315
        return blockedByButton;
316
    }
317

    
318
    /**
319
     * @return the messageButton
320
     */
321
    public Button getMessageButton() {
322
        return messageButton;
323
    }
324

    
325
    /**
326
     * @return the openButton
327
     */
328
    public Button getOpenButton() {
329
        return openButton;
330
    }
331

    
332
    /**
333
     * @return the createdLabel
334
     */
335
    public Label getCreatedLabel() {
336
        return createdLabel;
337
    }
338

    
339
    /**
340
     * @return the publishedLabel
341
     */
342
    public Label getPublishedLabel() {
343
        return publishedLabel;
344
    }
345

    
346

    
347
    /**
348
     * @return
349
     */
350
    public Label getReleasedLabel() {
351
        return releasedLabel;
352
    }
353

    
354
    /**
355
     * @return the submitterLabel
356
     */
357
    public Label getSubmitterLabel() {
358
        return submitterLabel;
359
    }
360

    
361

    
362
   /* --------------------------------------- */
363

    
364
}
(2-2/8)