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
import java.util.UUID;
17

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

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

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

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

    
63

    
64

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

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

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

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

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

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

    
77
    private static final int GRID_ROWS = 5;
78

    
79
    private static final int GRID_COLS = 3;
80

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

    
83
    private RegistrationTypeConverter regTypeConverter = new RegistrationTypeConverter();
84

    
85
    private AbstractView<?> parentView;
86

    
87
    private RegistrationDTO regDto;
88

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

    
91
    // --------------------------------------------------
92

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

    
104
    private Panel blockingRelationsPanel;
105

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

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

    
125
    public void init() {
126

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

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

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

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

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

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

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

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

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

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

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

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

    
186
    }
187

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

    
190
        this.parentView = parentView;
191

    
192
        this.regDto = regDto;
193

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

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

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

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

    
232

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

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

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

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

    
273
        labelMarkup.append(citationString);
274

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

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

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

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

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

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

    
309

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

    
324

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

    
333
    public UUID getRegistrationUuid(){
334
        return regDto.getUuid();
335
    }
336

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

    
342
        if(blockingRelationsPanel == null) {
343

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

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

    
355
    }
356

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

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

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

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

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

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

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

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

    
414

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

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

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

    
436

    
437
   /* --------------------------------------- */
438

    
439
}
(2-2/10)