Project

General

Profile

Download (15.2 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.api.service.dto.RegistrationDTO;
38
import eu.etaxonomy.cdm.api.service.dto.RegistrationWorkingSet;
39
import eu.etaxonomy.cdm.model.common.TimePeriod;
40
import eu.etaxonomy.cdm.model.name.RegistrationStatus;
41
import eu.etaxonomy.cdm.model.reference.Reference;
42
import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
43
import eu.etaxonomy.cdm.vaadin.event.ReferenceEditorAction;
44
import eu.etaxonomy.cdm.vaadin.event.ShowDetailsEvent;
45
import eu.etaxonomy.cdm.vaadin.permission.PermissionDebugUtils;
46
import eu.etaxonomy.cdm.vaadin.permission.VaadinUserHelper;
47
import eu.etaxonomy.cdm.vaadin.theme.EditValoTheme;
48
import eu.etaxonomy.cdm.vaadin.util.formatter.DateTimeFormat;
49
import eu.etaxonomy.cdm.vaadin.util.formatter.TimePeriodFormatter;
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 AbstractView<?> parentView;
83

    
84
    private RegistrationDTO regDto;
85

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

    
88
    // --------------------------------------------------
89

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

    
101
    private Panel blockingRelationsPanel;
102

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

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

    
122
    public void init() {
123

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

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

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

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

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

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

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

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

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

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

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

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

    
183
    }
184

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

    
187
        this.parentView = parentView;
188

    
189
        this.regDto = regDto;
190

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

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

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

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

    
229

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

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

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

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

    
270
        labelMarkup.append(citationString);
271

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

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

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

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

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

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

    
306

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

    
321

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

    
330
    public UUID getRegistrationUuid(){
331
        return regDto.getUuid();
332
    }
333

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

    
339
        if(blockingRelationsPanel == null) {
340

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

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

    
352
    }
353

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

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

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

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

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

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

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

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

    
411

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

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

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

    
433

    
434
   /* --------------------------------------- */
435

    
436
}
(2-2/11)