Project

General

Profile

Download (15.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
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
    /**
63
     *
64
     */
65
    public static final String STYLE_NAME_BLOCKED = "blocked";
66

    
67
    private static final String LABEL_CAPTION_CREATED = "Created";
68

    
69
    private static final String LABEL_CAPTION_PUBLISHED = "Published";
70

    
71
    private static final String LABEL_CAPTION_RELEASED = "Released";
72

    
73
    private static final int GRID_ROWS = 5;
74

    
75
    private static final int GRID_COLS = 3;
76

    
77
    private static final long serialVersionUID = -211003770452173644L;
78

    
79
    private RegistrationTypeConverter regTypeConverter = new RegistrationTypeConverter();
80

    
81
    private AbstractView<?> parentView;
82

    
83
    private RegistrationDTO regDto;
84

    
85
    private TimePeriodFormatter timePeriodFormatter = new TimePeriodFormatter(DateTimeFormat.ISO8601_DATE);
86

    
87
    // --------------------------------------------------
88

    
89
    private RegistrationStateLabel stateLabel = new RegistrationStateLabel();
90
    private Link identifierLink = new Link();
91
    private Label citationSummaryLabel = new Label();
92
    private Button blockedByButton = new Button(FontAwesome.WARNING);
93
    private Button messageButton;
94
    private Button openButton = new Button(FontAwesome.COGS);
95
    private Label submitterLabel = new Label();
96
    private Label createdLabel = new Label();
97
    private Label publishedLabel = new Label();
98
    private Label releasedLabel = new Label();
99

    
100
    private Panel blockingRelationsPanel;
101

    
102
    /**
103
     *
104
     */
105
    public RegistrationItem(RegistrationDTO item, AbstractView<?> parentView) {
106
        super(GRID_COLS, GRID_ROWS);
107
        init();
108
        setItem(item, parentView);
109
    }
110

    
111
    /**
112
    *
113
    */
114
   public RegistrationItem(RegistrationWorkingSet workingSet, AbstractView<?> parentView) {
115
       super(GRID_COLS, GRID_ROWS);
116
       init();
117
       blockedByButton.setVisible(false);
118
       setWorkingSet(workingSet, parentView);
119
   }
120

    
121
    public void init() {
122

    
123
        setWidth(100, Unit.PERCENTAGE);
124
        addStyleName("registration-list-item");
125

    
126
        CssLayout stateUserContainer = new CssLayout();
127
        stateLabel.setStyleName(LABEL_NOWRAP + " registration-state");
128
        stateLabel.setVisible(false);
129

    
130
        submitterLabel.setStyleName(LABEL_NOWRAP + " submitter");
131
        submitterLabel.setIcon(FontAwesome.USER);
132
        submitterLabel.setContentMode(ContentMode.HTML);
133
        submitterLabel.setVisible(false);
134

    
135
        stateUserContainer.addComponents(stateLabel, submitterLabel);
136
        addComponent(stateUserContainer, 0, 0);
137
        setComponentAlignment(stateUserContainer, Alignment.TOP_LEFT);
138

    
139
        identifierLink.setVisible(false);
140
        addComponent(identifierLink, 1, 0);
141
        setComponentAlignment(identifierLink, Alignment.TOP_CENTER);
142
        setColumnExpandRatio(1, 1.0f);
143

    
144
        messageButton = new Button(FontAwesome.COMMENT);
145
        CssLayout buttonGroup = new CssLayout(blockedByButton, messageButton, openButton);
146
        blockedByButton.setStyleName(ValoTheme.BUTTON_TINY);
147
        blockedByButton.setEnabled(false);
148
        messageButton.setStyleName(ValoTheme.BUTTON_TINY);
149
        messageButton.setEnabled(false);
150

    
151
        openButton.setStyleName(ValoTheme.BUTTON_TINY);
152
        openButton.addStyleName(ValoTheme.BUTTON_PRIMARY);
153
        openButton.setVisible(false);
154

    
155
        buttonGroup.setStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
156
        addComponent(buttonGroup, 2, 0);
157
        setComponentAlignment(buttonGroup, Alignment.TOP_RIGHT);
158

    
159
        citationSummaryLabel.setContentMode(ContentMode.HTML);
160
        addComponent(citationSummaryLabel, 0, 1, 1, 3);
161

    
162
        createdLabel.setStyleName(LABEL_NOWRAP);
163
        createdLabel.setContentMode(ContentMode.HTML);
164
        createdLabel.setWidthUndefined();
165
        addComponent(createdLabel, 2, 1);
166
        setComponentAlignment(createdLabel, Alignment.BOTTOM_RIGHT);
167

    
168
        publishedLabel.setStyleName(LABEL_NOWRAP);
169
        publishedLabel.setContentMode(ContentMode.HTML);
170
        publishedLabel.setWidthUndefined();
171
        publishedLabel.setVisible(false);
172
        addComponent(publishedLabel, 2, 2);
173
        setComponentAlignment(publishedLabel, Alignment.BOTTOM_RIGHT);
174

    
175
        releasedLabel.setStyleName(LABEL_NOWRAP);
176
        releasedLabel.setContentMode(ContentMode.HTML);
177
        releasedLabel.setWidthUndefined();
178
        releasedLabel.setVisible(false);
179
        addComponent(releasedLabel, 2, 3);
180
        setComponentAlignment(releasedLabel, Alignment.BOTTOM_RIGHT);
181

    
182
    }
183

    
184
    public void setItem(RegistrationDTO regDto, AbstractView<?> parentView){
185

    
186
        this.parentView = parentView;
187

    
188
        this.regDto = regDto;
189

    
190
        NavigationEvent navigationEvent = null;
191
        if(regDto.getCitationID() != null) {
192
            navigationEvent = new NavigationEvent(
193
                    RegistrationWorksetViewBean.NAME,
194
                    Integer.toString(regDto.getCitationID())
195
                    );
196
        } else {
197
            setComponentError(new UserError("Citation is missing"));
198
        }
199

    
200
        updateUI(regDto.getBibliographicCitationString(), regDto.getCreated(), regDto.getDatePublished(), regDto.getValidationProblems().size(),
201
                navigationEvent, null, regDto, regDto.getSubmitterUserName());
202
    }
203

    
204
    public void setWorkingSet(RegistrationWorkingSet workingSet, AbstractView<?> parentView){
205
        this.parentView = parentView;
206

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

    
228

    
229
    /**
230
     * @param submitterUserName TODO
231
     *
232
     */
233
    private void updateUI(String citationString,  DateTime created, TimePeriod datePublished,  int messagesCount,
234
            Object openButtonEvent, Resource openButtonIcon, RegistrationDTO regDto, String submitterUserName) {
235

    
236
        StringBuffer labelMarkup = new StringBuffer();
237
        DateTime registrationDate = null;
238

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

    
264
        if(regDto != null && regDto.isBlocked()){
265
            getBlockedByButton().setEnabled(true);
266
            getBlockedByButton().addStyleName(STYLE_NAME_BLOCKED);
267
        }
268

    
269
        labelMarkup.append(citationString);
270

    
271
        if(openButtonEvent != null){
272
            // Buttons
273
            getOpenButton().setVisible(true);
274
            Collection<?> removeCandidates = getOpenButton().getListeners(ClickListener.class);
275
            removeCandidates.forEach(l -> getOpenButton().removeClickListener((ClickListener)l));
276
            getOpenButton().addClickListener(e -> publishEvent(openButtonEvent));
277
        }
278

    
279
        if(openButtonIcon != null){
280
            getOpenButton().setIcon(openButtonIcon);
281
        }
282

    
283
        if(regDto != null){
284
            labelMarkup.append("</br>").append(regDto.getSummary());
285

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

    
296
            registrationDate = regDto.getRegistrationDate();
297
        }
298

    
299
        getCitationSummaryLabel().setValue(labelMarkup.toString());
300
        getSubmitterLabel().setValue(submitterUserName);
301
        updateDateLabels(created, datePublished, registrationDate);
302
    }
303

    
304

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

    
319

    
320
    private void publishEvent(Object event) {
321
        if(event instanceof NavigationEvent){
322
            parentView.getViewEventBus().publish(EventScope.UI, this, event);
323
        } else {
324
            parentView.getViewEventBus().publish(this, event);
325
        }
326
    }
327

    
328
    public int getRegistrationId(){
329
        return regDto.getId();
330
    }
331

    
332
    /**
333
     * @param showBlockingRelations the showBlockingRelations to set
334
     */
335
    public void showBlockingRegistrations(Set<RegistrationDTO> blockingRegDTOs) {
336

    
337
        if(blockingRelationsPanel == null) {
338

    
339
            if(regDto.isBlocked() && blockingRegDTOs.isEmpty()){
340
                throw new RuntimeException("Registration is blocked but tet of blocking registrations is empty");
341
            }
342
            if(!regDto.isBlocked() && !blockingRegDTOs.isEmpty()){
343
                throw new RuntimeException("No point showing blocking registrations for an unblocked registration");
344
            }
345

    
346
            blockingRelationsPanel = new RegistrationItemsPanel(parentView, "blocked by", blockingRegDTOs);
347
            addComponent(blockingRelationsPanel, 0, 4, GRID_COLS - 1, 4);
348
        }
349

    
350
    }
351

    
352
    /* ====== RegistrationItemDesign Getters ====== */
353
    /**
354
     * @return the typeStateLabel
355
     */
356
    public Label getTypeStateLabel() {
357
        return stateLabel;
358
    }
359

    
360
    /**
361
     * @return the identifierLink
362
     */
363
    public Link getIdentifierLink() {
364
        return identifierLink;
365
    }
366

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

    
374
    /**
375
     * @return the blockedByButton
376
     */
377
    public Button getBlockedByButton() {
378
        return blockedByButton;
379
    }
380

    
381
    /**
382
     * @return the messageButton
383
     */
384
    public Button getMessageButton() {
385
        return messageButton;
386
    }
387

    
388
    /**
389
     * @return the openButton
390
     */
391
    public Button getOpenButton() {
392
        return openButton;
393
    }
394

    
395
    /**
396
     * @return the createdLabel
397
     */
398
    public Label getCreatedLabel() {
399
        return createdLabel;
400
    }
401

    
402
    /**
403
     * @return the publishedLabel
404
     */
405
    public Label getPublishedLabel() {
406
        return publishedLabel;
407
    }
408

    
409

    
410
    /**
411
     * @return
412
     */
413
    public Label getReleasedLabel() {
414
        return releasedLabel;
415
    }
416

    
417
    /**
418
     * @return the submitterLabel
419
     */
420
    public Label getSubmitterLabel() {
421
        return submitterLabel;
422
    }
423

    
424
    /**
425
     * @return the showBlockingRelations
426
     */
427
    public boolean isShowBlockingRelations() {
428
        return blockingRelationsPanel != null;
429
    }
430

    
431

    
432
   /* --------------------------------------- */
433

    
434
}
(2-2/9)