Project

General

Profile

Download (15 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
import java.util.Set;
16

    
17
import org.joda.time.DateTime;
18
import org.joda.time.format.ISODateTimeFormat;
19
import org.vaadin.spring.events.EventScope;
20

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

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

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

    
61

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

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

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

    
68
    private static final int GRID_ROWS = 5;
69

    
70
    private static final int GRID_COLS = 3;
71

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

    
74
    private RegistrationTypeConverter regTypeConverter = new RegistrationTypeConverter();
75

    
76
    private AbstractView<?> parentView;
77

    
78
    private RegistrationDTO regDto;
79

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

    
82
    // --------------------------------------------------
83

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

    
95
    private Panel blockingRelationsPanel;
96

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

    
106
    /**
107
    *
108
    */
109
   public RegistrationItem(RegistrationWorkingSet workingSet, AbstractView<?> parentView) {
110
       super(GRID_COLS, GRID_ROWS);
111
       init();
112
       blockedByButton.setVisible(false);
113
       setWorkingSet(workingSet, parentView);
114
   }
115

    
116
    public void init() {
117

    
118
        setWidth(100, Unit.PERCENTAGE);
119
        addStyleName("registration-list-item");
120

    
121
        CssLayout stateUserContainer = new CssLayout();
122
        stateLabel.setStyleName(LABEL_NOWRAP + " registration-state");
123
        stateLabel.setVisible(false);
124

    
125
        submitterLabel.setStyleName(LABEL_NOWRAP + " submitter");
126
        submitterLabel.setIcon(FontAwesome.USER);
127
        submitterLabel.setContentMode(ContentMode.HTML);
128
        submitterLabel.setVisible(false);
129

    
130
        stateUserContainer.addComponents(stateLabel, submitterLabel);
131
        addComponent(stateUserContainer, 0, 0);
132
        setComponentAlignment(stateUserContainer, Alignment.TOP_LEFT);
133

    
134
        identifierLink.setVisible(false);
135
        addComponent(identifierLink, 1, 0);
136
        setComponentAlignment(identifierLink, Alignment.TOP_CENTER);
137
        setColumnExpandRatio(1, 1.0f);
138

    
139
        messageButton = new Button(FontAwesome.COMMENT);
140
        CssLayout buttonGroup = new CssLayout(blockedByButton, messageButton, openButton);
141
        blockedByButton.setStyleName(ValoTheme.BUTTON_TINY);
142
        blockedByButton.setEnabled(false);
143
        messageButton.setStyleName(ValoTheme.BUTTON_TINY);
144
        messageButton.setEnabled(false);
145

    
146
        openButton.setStyleName(ValoTheme.BUTTON_TINY);
147
        openButton.addStyleName(ValoTheme.BUTTON_PRIMARY);
148
        openButton.setVisible(false);
149

    
150
        buttonGroup.setStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
151
        addComponent(buttonGroup, 2, 0);
152
        setComponentAlignment(buttonGroup, Alignment.TOP_RIGHT);
153

    
154
        citationSummaryLabel.setContentMode(ContentMode.HTML);
155
        addComponent(citationSummaryLabel, 0, 1, 1, 3);
156

    
157
        createdLabel.setStyleName(LABEL_NOWRAP);
158
        createdLabel.setContentMode(ContentMode.HTML);
159
        createdLabel.setWidthUndefined();
160
        addComponent(createdLabel, 2, 1);
161
        setComponentAlignment(createdLabel, Alignment.BOTTOM_RIGHT);
162

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

    
170
        releasedLabel.setStyleName(LABEL_NOWRAP);
171
        releasedLabel.setContentMode(ContentMode.HTML);
172
        releasedLabel.setWidthUndefined();
173
        releasedLabel.setVisible(false);
174
        addComponent(releasedLabel, 2, 3);
175
        setComponentAlignment(releasedLabel, Alignment.BOTTOM_RIGHT);
176

    
177
    }
178

    
179
    public void setItem(RegistrationDTO regDto, AbstractView<?> parentView){
180

    
181
        this.parentView = parentView;
182

    
183
        this.regDto = regDto;
184

    
185
        NavigationEvent navigationEvent = null;
186
        if(regDto.getCitationID() != null) {
187
            navigationEvent = new NavigationEvent(
188
                    RegistrationWorksetViewBean.NAME,
189
                    Integer.toString(regDto.getCitationID())
190
                    );
191
        } else {
192
            setComponentError(new UserError("Citation is missing"));
193
        }
194

    
195
        updateUI(regDto.getBibliographicCitationString(), regDto.getCreated(), regDto.getDatePublished(), regDto.getMessages().size(),
196
                navigationEvent, null, regDto, regDto.getSubmitterUserName());
197
    }
198

    
199
    public void setWorkingSet(RegistrationWorkingSet workingSet, AbstractView<?> parentView){
200
        this.parentView = parentView;
201

    
202
        ReferenceEditorAction referenceEditorAction = null;
203
        if(workingSet.getCitationId() != null){
204
            if(UserHelper.fromSession().userHasPermission(Reference.class, workingSet.getCitationId(), CRUD.UPDATE)){
205
                referenceEditorAction = new ReferenceEditorAction(EditorActionType.EDIT, workingSet.getCitationId(), null, parentView);
206
            }
207
            PermissionDebugUtils.addGainPerEntityPermissionButton(this, Reference.class, workingSet.getCitationId(), EnumSet.of(CRUD.UPDATE, CRUD.DELETE), null);
208
        } else {
209
            if(UserHelper.fromSession().userHasPermission(Reference.class, CRUD.CREATE, null, null, parentView)){
210
                referenceEditorAction = new ReferenceEditorAction(EditorActionType.ADD);
211
            }
212
        }
213
        TimePeriod datePublished = null;
214
        String submitterName = null;
215
        if(workingSet.getRegistrationDTOs().size() > 0){
216
            datePublished = workingSet.getRegistrationDTOs().get(0).getDatePublished();
217
            submitterName = workingSet.getRegistrationDTOs().get(0).getSubmitterUserName();
218
        }
219
        updateUI(workingSet.getCitation(), workingSet.getCreated(), datePublished, workingSet.messagesCount(),
220
                referenceEditorAction, FontAwesome.EDIT, null, submitterName);
221
    }
222

    
223

    
224
    /**
225
     * @param submitterUserName TODO
226
     *
227
     */
228
    private void updateUI(String citationString,  DateTime created, TimePeriod datePublished,  int messagesCount,
229
            Object openButtonEvent, Resource openButtonIcon, RegistrationDTO regDto, String submitterUserName) {
230

    
231
        StringBuffer labelMarkup = new StringBuffer();
232
        DateTime registrationDate = null;
233

    
234
        if(messagesCount > 0){
235
            getMessageButton().setEnabled(true);
236
            // getMessageButton().addStyleName(RegistrationStyles.STYLE_FRIENDLY_FOREGROUND);
237
            getMessageButton().addClickListener(e -> {
238
                ShowDetailsEvent detailsEvent;
239
                if(regDto != null){
240
                    detailsEvent = new ShowDetailsEvent<RegistrationDTO, Integer>(
241
                            e,
242
                            RegistrationDTO.class,
243
                            regDto.getId(),
244
                            "messages");
245
                } else {
246
                    detailsEvent = new ShowDetailsEvent<RegistrationWorkingSet, Integer>(
247
                            e,
248
                            RegistrationWorkingSet.class,
249
                            null,
250
                            "messages");
251
                }
252
                publishEvent(detailsEvent);
253
                }
254
            );
255
            getMessageButton().setCaption("<span class=\"" + RegistrationStyles.BUTTON_BADGE +"\"> " + messagesCount + "</span>");
256
            getMessageButton().setCaptionAsHtml(true);
257
        }
258

    
259
        if(regDto != null && regDto.isBlocked()){
260
            getBlockedByButton().setEnabled(true);
261
            getBlockedByButton().addStyleName("blocked");
262
        }
263

    
264
        labelMarkup.append(citationString);
265

    
266
        if(openButtonEvent != null){
267
            // Buttons
268
            getOpenButton().setVisible(true);
269
            Collection<?> removeCandidates = getOpenButton().getListeners(ClickListener.class);
270
            removeCandidates.forEach(l -> getOpenButton().removeClickListener((ClickListener)l));
271
            getOpenButton().addClickListener(e -> publishEvent(openButtonEvent));
272
        }
273

    
274
        if(openButtonIcon != null){
275
            getOpenButton().setIcon(openButtonIcon);
276
        }
277

    
278
        if(regDto != null){
279
            labelMarkup.append("</br>").append(regDto.getSummary());
280

    
281
            stateLabel.setVisible(true);
282
            stateLabel.update(regDto.getStatus());
283
            if(regDto.getIdentifier() != null){
284
                getIdentifierLink().setResource(new ExternalResource(regDto.getIdentifier()));
285
            }
286
            getIdentifierLink().setCaption(regDto.getIdentifier());
287
            //TODO make responsive and use specificIdentifier in case the space gets too narrow
288
            getIdentifierLink().setVisible(true);
289
            getIdentifierLink().setEnabled(regDto.getStatus() == RegistrationStatus.PUBLISHED);
290

    
291
            registrationDate = regDto.getRegistrationDate();
292
        }
293

    
294
        getCitationSummaryLabel().setValue(labelMarkup.toString());
295
        getSubmitterLabel().setValue(submitterUserName);
296
        updateDateLabels(created, datePublished, registrationDate);
297
    }
298

    
299

    
300
    private void updateDateLabels(DateTime created, TimePeriod datePublished, DateTime released) {
301
        if(created != null){
302
            getCreatedLabel().setValue("<span class=\"caption\">" + LABEL_CAPTION_CREATED + "</span>&nbsp;" + created.toString(ISODateTimeFormat.yearMonthDay()));
303
        }
304
        if(datePublished != null){
305
            getPublishedLabel().setVisible(true);
306
            getPublishedLabel().setValue("<span class=\"caption\">" + LABEL_CAPTION_PUBLISHED + "</span>&nbsp;" + timePeriodFormatter.print(datePublished));
307
        }
308
        if(released != null){
309
            getReleasedLabel().setVisible(true);
310
            getReleasedLabel().setValue("<span class=\"caption\">" + LABEL_CAPTION_RELEASED + "</span>&nbsp;" + released.toString(ISODateTimeFormat.yearMonthDay()));
311
        }
312
    }
313

    
314

    
315
    private void publishEvent(Object event) {
316
        if(event instanceof NavigationEvent){
317
            parentView.getViewEventBus().publish(EventScope.UI, this, event);
318
        } else {
319
            parentView.getViewEventBus().publish(this, event);
320
        }
321
    }
322

    
323
    public int getRegistrationId(){
324
        return regDto.getId();
325
    }
326

    
327
    /**
328
     * @param showBlockingRelations the showBlockingRelations to set
329
     */
330
    public void showBlockingRegistrations(Set<RegistrationDTO> blockingRegDTOs) {
331

    
332
        if(blockingRelationsPanel == null) {
333

    
334
            if(regDto.isBlocked() && blockingRegDTOs.isEmpty()){
335
                throw new RuntimeException("Registration is blocked but tet of blocking registrations is empty");
336
            }
337
            if(!regDto.isBlocked() && !blockingRegDTOs.isEmpty()){
338
                throw new RuntimeException("No point showing blocking registrations for an unblocked registration");
339
            }
340

    
341
            blockingRelationsPanel = new RegistrationItemsPanel(parentView, "blocked by", blockingRegDTOs);
342
            addComponent(blockingRelationsPanel, 0, 4, GRID_COLS - 1, 4);
343
        }
344

    
345
    }
346

    
347
    /* ====== RegistrationItemDesign Getters ====== */
348
    /**
349
     * @return the typeStateLabel
350
     */
351
    public Label getTypeStateLabel() {
352
        return stateLabel;
353
    }
354

    
355
    /**
356
     * @return the identifierLink
357
     */
358
    public Link getIdentifierLink() {
359
        return identifierLink;
360
    }
361

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

    
369
    /**
370
     * @return the blockedByButton
371
     */
372
    public Button getBlockedByButton() {
373
        return blockedByButton;
374
    }
375

    
376
    /**
377
     * @return the messageButton
378
     */
379
    public Button getMessageButton() {
380
        return messageButton;
381
    }
382

    
383
    /**
384
     * @return the openButton
385
     */
386
    public Button getOpenButton() {
387
        return openButton;
388
    }
389

    
390
    /**
391
     * @return the createdLabel
392
     */
393
    public Label getCreatedLabel() {
394
        return createdLabel;
395
    }
396

    
397
    /**
398
     * @return the publishedLabel
399
     */
400
    public Label getPublishedLabel() {
401
        return publishedLabel;
402
    }
403

    
404

    
405
    /**
406
     * @return
407
     */
408
    public Label getReleasedLabel() {
409
        return releasedLabel;
410
    }
411

    
412
    /**
413
     * @return the submitterLabel
414
     */
415
    public Label getSubmitterLabel() {
416
        return submitterLabel;
417
    }
418

    
419
    /**
420
     * @return the showBlockingRelations
421
     */
422
    public boolean isShowBlockingRelations() {
423
        return blockingRelationsPanel != null;
424
    }
425

    
426

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

    
429
}
(2-2/9)