Project

General

Profile

Download (13.1 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.model.name.RegistrationStatus;
34
import eu.etaxonomy.cdm.model.reference.Reference;
35
import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
36
import eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction.Action;
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.UserHelper;
41
import eu.etaxonomy.cdm.vaadin.util.formatter.DateTimeFormat;
42
import eu.etaxonomy.cdm.vaadin.util.formatter.TimePeriodFormatter;
43
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationDTO;
44
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationTypeConverter;
45
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationWorksetViewBean;
46
import eu.etaxonomy.vaadin.mvp.AbstractView;
47
import eu.etaxonomy.vaadin.ui.navigation.NavigationEvent;
48

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

    
56

    
57
    private static final String LABEL_CAPTION_CREATED = "Created";
58

    
59
    private static final String LABEL_CAPTION_PUBLISHED = "Published";
60

    
61
    private static final String LABEL_CAPTION_RELEASED = "Released";
62

    
63
    private static final int GRID_ROWS = 5;
64

    
65
    private static final int GRID_COLS = 3;
66

    
67
    private static final long serialVersionUID = -211003770452173644L;
68

    
69
    private RegistrationTypeConverter regTypeConverter = new RegistrationTypeConverter();
70

    
71
    private AbstractView<?> parentView;
72

    
73
    private TimePeriodFormatter timePeriodFormatter = new TimePeriodFormatter(DateTimeFormat.ISO8601_DATE);
74

    
75
    // --------------------------------------------------
76

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

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

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

    
106
    public void init() {
107

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

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

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

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

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

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

    
136
        openButton.setStyleName(ValoTheme.BUTTON_TINY);
137
        openButton.addStyleName(ValoTheme.BUTTON_PRIMARY);
138
        openButton.setVisible(false);
139

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

    
144
        citationSummaryLabel.setContentMode(ContentMode.HTML);
145
        addComponent(citationSummaryLabel, 0, 1, 1, 3);
146

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

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

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

    
167
    }
168

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

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

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

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

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

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

    
216
        StringBuffer labelMarkup = new StringBuffer();
217
        DateTime registrationDate = null;
218

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

    
244
        labelMarkup.append(citationString);
245

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

    
254
        if(openButtonIcon != null){
255
            getOpenButton().setIcon(openButtonIcon);
256
        }
257

    
258
        if(regDto != null){
259
            labelMarkup.append("</br>").append(regDto.getSummary());
260

    
261
            stateLabel.setVisible(true);
262
            stateLabel.update(regDto.getStatus());
263
            getIdentifierLink().setResource(new ExternalResource(regDto.getIdentifier()));
264
            getIdentifierLink().setCaption(regDto.getIdentifier());
265
            //TODO make responsive and use specificIdentifier in case the space gets too narrow
266
            getIdentifierLink().setVisible(true);
267
            getIdentifierLink().setEnabled(regDto.getStatus() == RegistrationStatus.PUBLISHED);
268

    
269
            registrationDate = regDto.getRegistrationDate();
270
        }
271

    
272
        getCitationSummaryLabel().setValue(labelMarkup.toString());
273
        getSubmitterLabel().setValue(submitterUserName);
274
        updateDateLabels(created, datePublished, registrationDate);
275
    }
276

    
277

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

    
292

    
293
    private void publishEvent(Object event) {
294
        parentView.getEventBus().publishEvent(event);
295
    }
296

    
297
    /* ====== RegistrationItemDesign Getters ====== */
298
    /**
299
     * @return the typeStateLabel
300
     */
301
    public Label getTypeStateLabel() {
302
        return stateLabel;
303
    }
304

    
305
    /**
306
     * @return the identifierLink
307
     */
308
    public Link getIdentifierLink() {
309
        return identifierLink;
310
    }
311

    
312
    /**
313
     * @return the citationSummaryLabel
314
     */
315
    public Label getCitationSummaryLabel() {
316
        return citationSummaryLabel;
317
    }
318

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

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

    
333
    /**
334
     * @return the openButton
335
     */
336
    public Button getOpenButton() {
337
        return openButton;
338
    }
339

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

    
347
    /**
348
     * @return the publishedLabel
349
     */
350
    public Label getPublishedLabel() {
351
        return publishedLabel;
352
    }
353

    
354

    
355
    /**
356
     * @return
357
     */
358
    public Label getReleasedLabel() {
359
        return releasedLabel;
360
    }
361

    
362
    /**
363
     * @return the submitterLabel
364
     */
365
    public Label getSubmitterLabel() {
366
        return submitterLabel;
367
    }
368

    
369

    
370
   /* --------------------------------------- */
371

    
372
}
(2-2/8)