Project

General

Profile

Download (15.3 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
    public static final String VALIDATION_PROBLEMS = "validationProblems";
64

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

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

    
69
    /**
70
     *
71
     */
72
    public static final String STYLE_NAME_BLOCKED = "blocked";
73

    
74
    private static final String LABEL_CAPTION_CREATED = "Created";
75

    
76
    private static final String LABEL_CAPTION_PUBLISHED = "Published";
77

    
78
    private static final String LABEL_CAPTION_RELEASED = "Released";
79

    
80
    private static final int GRID_ROWS = 5;
81

    
82
    private static final int GRID_COLS = 3;
83

    
84
    private static final long serialVersionUID = -211003770452173644L;
85

    
86
    private RegistrationTypeConverter regTypeConverter = new RegistrationTypeConverter();
87

    
88
    private AbstractView<?> parentView;
89

    
90
    private RegistrationDTO regDto;
91

    
92
    private TimePeriodFormatter timePeriodFormatter = new TimePeriodFormatter(DateTimeFormat.ISO8601_DATE);
93

    
94
    // --------------------------------------------------
95

    
96
    private RegistrationStateLabel stateLabel = new RegistrationStateLabel();
97
    private Link identifierLink = new Link();
98
    private Label citationSummaryLabel = new Label();
99
    private Button blockedByButton = new Button(FontAwesome.WARNING);
100
    private Button messageButton;
101
    private Button openButton = new Button(FontAwesome.COGS);
102
    private Label submitterLabel = new Label();
103
    private Label createdLabel = new Label();
104
    private Label publishedLabel = new Label();
105
    private Label releasedLabel = new Label();
106

    
107
    private Panel blockingRelationsPanel;
108

    
109
    /**
110
     *
111
     */
112
    public RegistrationItem(RegistrationDTO item, AbstractView<?> parentView) {
113
        super(GRID_COLS, GRID_ROWS);
114
        init();
115
        setItem(item, parentView);
116
    }
117

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

    
128
    public void init() {
129

    
130
        setWidth(100, Unit.PERCENTAGE);
131
        addStyleName("registration-list-item");
132

    
133
        CssLayout stateUserContainer = new CssLayout();
134
        stateLabel.setStyleName(LABEL_NOWRAP + " registration-state");
135
        stateLabel.setVisible(false);
136

    
137
        submitterLabel.setStyleName(LABEL_NOWRAP + " submitter");
138
        submitterLabel.setIcon(FontAwesome.USER);
139
        submitterLabel.setContentMode(ContentMode.HTML);
140
        submitterLabel.setVisible(false);
141

    
142
        stateUserContainer.addComponents(stateLabel, submitterLabel);
143
        addComponent(stateUserContainer, 0, 0);
144
        setComponentAlignment(stateUserContainer, Alignment.TOP_LEFT);
145

    
146
        identifierLink.setVisible(false);
147
        addComponent(identifierLink, 1, 0);
148
        setComponentAlignment(identifierLink, Alignment.TOP_CENTER);
149
        setColumnExpandRatio(1, 1.0f);
150

    
151
        messageButton = new Button(FontAwesome.COMMENT);
152
        CssLayout buttonGroup = new CssLayout(blockedByButton, messageButton, openButton);
153
        blockedByButton.setStyleName(ValoTheme.BUTTON_TINY);
154
        blockedByButton.setEnabled(false);
155
        messageButton.setStyleName(ValoTheme.BUTTON_TINY);
156
        messageButton.setEnabled(false);
157

    
158
        openButton.setStyleName(ValoTheme.BUTTON_TINY);
159
        openButton.addStyleName(ValoTheme.BUTTON_PRIMARY);
160
        openButton.setVisible(false);
161

    
162
        buttonGroup.setStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
163
        addComponent(buttonGroup, 2, 0);
164
        setComponentAlignment(buttonGroup, Alignment.TOP_RIGHT);
165

    
166
        citationSummaryLabel.setContentMode(ContentMode.HTML);
167
        addComponent(citationSummaryLabel, 0, 1, 1, 3);
168

    
169
        createdLabel.setStyleName(LABEL_NOWRAP);
170
        createdLabel.setContentMode(ContentMode.HTML);
171
        createdLabel.setWidthUndefined();
172
        addComponent(createdLabel, 2, 1);
173
        setComponentAlignment(createdLabel, Alignment.BOTTOM_RIGHT);
174

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

    
182
        releasedLabel.setStyleName(LABEL_NOWRAP);
183
        releasedLabel.setContentMode(ContentMode.HTML);
184
        releasedLabel.setWidthUndefined();
185
        releasedLabel.setVisible(false);
186
        addComponent(releasedLabel, 2, 3);
187
        setComponentAlignment(releasedLabel, Alignment.BOTTOM_RIGHT);
188

    
189
    }
190

    
191
    public void setItem(RegistrationDTO regDto, AbstractView<?> parentView){
192

    
193
        this.parentView = parentView;
194

    
195
        this.regDto = regDto;
196

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

    
207
        updateUI(regDto.getBibliographicCitationString(), regDto.getCreated(), regDto.getDatePublished(), regDto.getValidationProblems().size(),
208
                navigationEvent, null, regDto, regDto.getSubmitterUserName());
209
    }
210

    
211
    public void setWorkingSet(RegistrationWorkingSet workingSet, AbstractView<?> parentView){
212
        this.parentView = parentView;
213

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

    
235

    
236
    /**
237
     * @param submitterUserName TODO
238
     *
239
     */
240
    private void updateUI(String citationString,  DateTime created, TimePeriod datePublished,  int messagesCount,
241
            Object openButtonEvent, Resource openButtonIcon, RegistrationDTO regDto, String submitterUserName) {
242

    
243
        StringBuffer labelMarkup = new StringBuffer();
244
        DateTime registrationDate = null;
245

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

    
271
        if(regDto != null && regDto.isBlocked()){
272
            getBlockedByButton().setEnabled(true);
273
            getBlockedByButton().addStyleName(STYLE_NAME_BLOCKED);
274
        }
275

    
276
        labelMarkup.append(citationString);
277

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

    
286
        if(openButtonIcon != null){
287
            getOpenButton().setIcon(openButtonIcon);
288
        }
289

    
290
        if(regDto != null){
291
            labelMarkup.append("</br>").append(regDto.getSummary());
292

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

    
303
            registrationDate = regDto.getRegistrationDate();
304
        }
305

    
306
        getCitationSummaryLabel().setValue(labelMarkup.toString());
307
        getSubmitterLabel().setValue(submitterUserName);
308
        updateDateLabels(created, datePublished, registrationDate);
309
    }
310

    
311

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

    
326

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

    
335
    public int getRegistrationId(){
336
        return regDto.getId();
337
    }
338

    
339
    /**
340
     * @param showBlockingRelations the showBlockingRelations to set
341
     */
342
    public void showBlockingRegistrations(Set<RegistrationDTO> blockingRegDTOs) {
343

    
344
        if(blockingRelationsPanel == null) {
345

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

    
353
            blockingRelationsPanel = new RegistrationItemsPanel(parentView, "blocked by", blockingRegDTOs);
354
            addComponent(blockingRelationsPanel, 0, 4, GRID_COLS - 1, 4);
355
        }
356

    
357
    }
358

    
359
    /* ====== RegistrationItemDesign Getters ====== */
360
    /**
361
     * @return the typeStateLabel
362
     */
363
    public Label getTypeStateLabel() {
364
        return stateLabel;
365
    }
366

    
367
    /**
368
     * @return the identifierLink
369
     */
370
    public Link getIdentifierLink() {
371
        return identifierLink;
372
    }
373

    
374
    /**
375
     * @return the citationSummaryLabel
376
     */
377
    public Label getCitationSummaryLabel() {
378
        return citationSummaryLabel;
379
    }
380

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

    
388
    /**
389
     * @return the validationProblemsButton
390
     */
391
    public Button getMessageButton() {
392
        return messageButton;
393
    }
394

    
395
    /**
396
     * @return the openButton
397
     */
398
    public Button getOpenButton() {
399
        return openButton;
400
    }
401

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

    
409
    /**
410
     * @return the publishedLabel
411
     */
412
    public Label getPublishedLabel() {
413
        return publishedLabel;
414
    }
415

    
416

    
417
    /**
418
     * @return
419
     */
420
    public Label getReleasedLabel() {
421
        return releasedLabel;
422
    }
423

    
424
    /**
425
     * @return the submitterLabel
426
     */
427
    public Label getSubmitterLabel() {
428
        return submitterLabel;
429
    }
430

    
431
    /**
432
     * @return the showBlockingRelations
433
     */
434
    public boolean isShowBlockingRelations() {
435
        return blockingRelationsPanel != null;
436
    }
437

    
438

    
439
   /* --------------------------------------- */
440

    
441
}
(2-2/10)