Project

General

Profile

Download (12.6 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
                    RegistrationWorkflowViewBean.ACTION_EDIT,
173
                    Integer.toString(regDto.getCitationID())
174
                    );
175
        } else {
176
            setComponentError(new UserError("Citation is missing"));
177
        }
178

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

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

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

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

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

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

    
238
        labelMarkup.append(citationString);
239

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

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

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

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

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

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

    
270

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

    
285

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

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

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

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

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

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

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

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

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

    
347

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

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

    
362

    
363
   /* --------------------------------------- */
364

    
365
}
(2-2/8)