Project

General

Profile

Download (15.4 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.theme.EditValoTheme;
46
import eu.etaxonomy.cdm.vaadin.util.formatter.DateTimeFormat;
47
import eu.etaxonomy.cdm.vaadin.util.formatter.TimePeriodFormatter;
48
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationDTO;
49
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationTypeConverter;
50
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationWorksetViewBean;
51
import eu.etaxonomy.vaadin.event.EditorActionType;
52
import eu.etaxonomy.vaadin.mvp.AbstractView;
53
import eu.etaxonomy.vaadin.ui.navigation.NavigationEvent;
54

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

    
62

    
63

    
64
    public static final String VALIDATION_PROBLEMS = "validationProblems";
65

    
66
    public static final String MESSAGES = "messages";
67

    
68
    public static final String BLOCKED_BY = "blockedBy";
69

    
70
    private static final String LABEL_CAPTION_CREATED = "Created";
71

    
72
    private static final String LABEL_CAPTION_PUBLISHED = "Published";
73

    
74
    private static final String LABEL_CAPTION_RELEASED = "Released";
75

    
76
    private static final int GRID_ROWS = 5;
77

    
78
    private static final int GRID_COLS = 3;
79

    
80
    private static final long serialVersionUID = -211003770452173644L;
81

    
82
    private RegistrationTypeConverter regTypeConverter = new RegistrationTypeConverter();
83

    
84
    private AbstractView<?> parentView;
85

    
86
    private RegistrationDTO regDto;
87

    
88
    private TimePeriodFormatter timePeriodFormatter = new TimePeriodFormatter(DateTimeFormat.ISO8601_DATE);
89

    
90
    // --------------------------------------------------
91

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

    
103
    private Panel blockingRelationsPanel;
104

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

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

    
124
    public void init() {
125

    
126
        setWidth(100, Unit.PERCENTAGE);
127
        addStyleName("registration-list-item");
128

    
129
        CssLayout stateUserContainer = new CssLayout();
130
        stateLabel.setStyleName(LABEL_NOWRAP + " registration-state");
131
        stateLabel.setVisible(false);
132

    
133
        submitterLabel.setStyleName(LABEL_NOWRAP + " submitter");
134
        submitterLabel.setIcon(FontAwesome.USER);
135
        submitterLabel.setContentMode(ContentMode.HTML);
136
        submitterLabel.setVisible(false);
137

    
138
        stateUserContainer.addComponents(stateLabel, submitterLabel);
139
        addComponent(stateUserContainer, 0, 0);
140
        setComponentAlignment(stateUserContainer, Alignment.TOP_LEFT);
141

    
142
        identifierLink.setVisible(false);
143
        addComponent(identifierLink, 1, 0);
144
        setComponentAlignment(identifierLink, Alignment.TOP_CENTER);
145
        setColumnExpandRatio(1, 1.0f);
146

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

    
154
        openButton.setStyleName(ValoTheme.BUTTON_TINY);
155
        openButton.addStyleName(ValoTheme.BUTTON_PRIMARY);
156
        openButton.setVisible(false);
157

    
158
        buttonGroup.setStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
159
        addComponent(buttonGroup, 2, 0);
160
        setComponentAlignment(buttonGroup, Alignment.TOP_RIGHT);
161

    
162
        citationSummaryLabel.setContentMode(ContentMode.HTML);
163
        addComponent(citationSummaryLabel, 0, 1, 1, 3);
164

    
165
        createdLabel.setStyleName(LABEL_NOWRAP);
166
        createdLabel.setContentMode(ContentMode.HTML);
167
        createdLabel.setWidthUndefined();
168
        addComponent(createdLabel, 2, 1);
169
        setComponentAlignment(createdLabel, Alignment.BOTTOM_RIGHT);
170

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

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

    
185
    }
186

    
187
    public void setItem(RegistrationDTO regDto, AbstractView<?> parentView){
188

    
189
        this.parentView = parentView;
190

    
191
        this.regDto = regDto;
192

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

    
203
        updateUI(regDto.getBibliographicCitationString(), regDto.getCreated(), regDto.getDatePublished(), regDto.getValidationProblems().size(),
204
                navigationEvent, null, regDto, regDto.getSubmitterUserName());
205
    }
206

    
207
    public void setWorkingSet(RegistrationWorkingSet workingSet, AbstractView<?> parentView){
208
        this.parentView = parentView;
209

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

    
231

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

    
239
        StringBuffer labelMarkup = new StringBuffer();
240
        DateTime registrationDate = null;
241

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

    
267
        if(regDto != null && regDto.isBlocked()){
268
            getBlockedByButton().setEnabled(true);
269
            getBlockedByButton().addStyleName(EditValoTheme.BUTTON_HIGHLITE);
270
        }
271

    
272
        labelMarkup.append(citationString);
273

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

    
282
        if(openButtonIcon != null){
283
            getOpenButton().setIcon(openButtonIcon);
284
        }
285

    
286
        if(regDto != null){
287
            labelMarkup.append("</br>").append(regDto.getSummary());
288

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

    
299
            registrationDate = regDto.getRegistrationDate();
300
        }
301

    
302
        getCitationSummaryLabel().setValue(labelMarkup.toString());
303
        getSubmitterLabel().setValue(submitterUserName);
304
        getSubmitterLabel().setVisible(submitterUserName != null);
305
        updateDateLabels(created, datePublished, registrationDate);
306
    }
307

    
308

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

    
323

    
324
    private void publishEvent(Object event) {
325
        if(event instanceof NavigationEvent){
326
            parentView.getViewEventBus().publish(EventScope.UI, this, event);
327
        } else {
328
            parentView.getViewEventBus().publish(this, event);
329
        }
330
    }
331

    
332
    public int getRegistrationId(){
333
        return regDto.getId();
334
    }
335

    
336
    /**
337
     * @param showBlockingRelations the showBlockingRelations to set
338
     */
339
    public void showBlockingRegistrations(Set<RegistrationDTO> blockingRegDTOs) {
340

    
341
        if(blockingRelationsPanel == null) {
342

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

    
350
            blockingRelationsPanel = new RegistrationItemsPanel(parentView, "blocked by", blockingRegDTOs);
351
            addComponent(blockingRelationsPanel, 0, 4, GRID_COLS - 1, 4);
352
        }
353

    
354
    }
355

    
356
    /* ====== RegistrationItemDesign Getters ====== */
357
    /**
358
     * @return the typeStateLabel
359
     */
360
    public Label getTypeStateLabel() {
361
        return stateLabel;
362
    }
363

    
364
    /**
365
     * @return the identifierLink
366
     */
367
    public Link getIdentifierLink() {
368
        return identifierLink;
369
    }
370

    
371
    /**
372
     * @return the citationSummaryLabel
373
     */
374
    public Label getCitationSummaryLabel() {
375
        return citationSummaryLabel;
376
    }
377

    
378
    /**
379
     * @return the blockedByButton
380
     */
381
    public Button getBlockedByButton() {
382
        return blockedByButton;
383
    }
384

    
385
    /**
386
     * @return the validationProblemsButton
387
     */
388
    public Button getMessageButton() {
389
        return messageButton;
390
    }
391

    
392
    /**
393
     * @return the openButton
394
     */
395
    public Button getOpenButton() {
396
        return openButton;
397
    }
398

    
399
    /**
400
     * @return the createdLabel
401
     */
402
    public Label getCreatedLabel() {
403
        return createdLabel;
404
    }
405

    
406
    /**
407
     * @return the publishedLabel
408
     */
409
    public Label getPublishedLabel() {
410
        return publishedLabel;
411
    }
412

    
413

    
414
    /**
415
     * @return
416
     */
417
    public Label getReleasedLabel() {
418
        return releasedLabel;
419
    }
420

    
421
    /**
422
     * @return the submitterLabel
423
     */
424
    public Label getSubmitterLabel() {
425
        return submitterLabel;
426
    }
427

    
428
    /**
429
     * @return the showBlockingRelations
430
     */
431
    public boolean isShowBlockingRelations() {
432
        return blockingRelationsPanel != null;
433
    }
434

    
435

    
436
   /* --------------------------------------- */
437

    
438
}
(2-2/10)