Project

General

Profile

Download (11.8 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 java.util.ArrayList;
12
import java.util.EnumSet;
13
import java.util.LinkedHashMap;
14
import java.util.List;
15
import java.util.UUID;
16
import java.util.regex.Pattern;
17

    
18
import org.apache.log4j.Logger;
19

    
20
import com.vaadin.server.ExternalResource;
21
import com.vaadin.server.FontAwesome;
22
import com.vaadin.shared.ui.label.ContentMode;
23
import com.vaadin.ui.Button;
24
import com.vaadin.ui.Label;
25
import com.vaadin.ui.Link;
26
import com.vaadin.ui.themes.ValoTheme;
27

    
28
import eu.etaxonomy.cdm.api.service.dto.RegistrationDTO;
29
import eu.etaxonomy.cdm.api.service.name.TypeDesignationWorkingSet;
30
import eu.etaxonomy.cdm.api.service.name.TypeDesignationWorkingSet.TypeDesignationWorkingSetType;
31
import eu.etaxonomy.cdm.api.utility.UserHelper;
32
import eu.etaxonomy.cdm.model.ICdmEntityUuidCacher;
33
import eu.etaxonomy.cdm.model.common.VersionableEntity;
34
import eu.etaxonomy.cdm.model.name.RegistrationStatus;
35
import eu.etaxonomy.cdm.model.name.TaxonName;
36
import eu.etaxonomy.cdm.model.name.TypeDesignationStatusBase;
37
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
38
import eu.etaxonomy.cdm.model.permission.CRUD;
39
import eu.etaxonomy.cdm.ref.TypedEntityReference;
40
import eu.etaxonomy.cdm.service.UserHelperAccess;
41
import eu.etaxonomy.cdm.vaadin.component.ButtonFactory;
42
import eu.etaxonomy.cdm.vaadin.permission.PermissionDebugUtils;
43
import eu.etaxonomy.vaadin.component.CompositeStyledComponent;
44

    
45
/**
46
 * @author a.kohlbecker
47
 * @since May 19, 2017
48
 */
49
public class RegistrationItemNameAndTypeButtons extends CompositeStyledComponent {
50

    
51
    private final static Logger logger = Logger.getLogger(RegistrationItemNameAndTypeButtons.class);
52

    
53

    
54
    private static final String DEFAULT_BUTTON_STYLES = "";
55

    
56
    private static final long serialVersionUID = -5059163772392864050L;
57

    
58
    public static final String STYLE_NAMES = "edit-button-group  " + ValoTheme.LAYOUT_COMPONENT_GROUP;
59

    
60
    private IdButton<TaxonName> nameIdButton = null;
61

    
62
    private List<TypeDesignationWorkingSetButton> typeDesignationButtons = new ArrayList<>();
63

    
64
    private List<Label> labels = new ArrayList<>();
65

    
66
    private List<ButtonWithUserEditPermission> editButtons = new ArrayList<>();
67

    
68
    private Button addTypeDesignationButton;
69

    
70
    private Label nameLabel = null;
71

    
72
    private Link identifierLink;
73

    
74
    private boolean isRegistrationLocked;
75

    
76
    private boolean isLockOverride;
77

    
78
    public RegistrationItemNameAndTypeButtons(RegistrationDTO regDto, ICdmEntityUuidCacher entitiyCacher) {
79

    
80
        isRegistrationLocked = EnumSet.of(
81
                RegistrationStatus.PUBLISHED, RegistrationStatus.REJECTED)
82
                .contains(regDto.getStatus());
83

    
84
        setWidth(100, Unit.PERCENTAGE);
85

    
86
        UserHelper userHelper;
87
        if(entitiyCacher != null){
88
            userHelper = UserHelperAccess.userHelper().withCache(entitiyCacher);
89
        } else {
90
            userHelper = UserHelperAccess.userHelper();
91
        }
92

    
93
        if(regDto.getNameRef() != null){
94
            Button nameButton = new Button("Name:");
95
            nameButton.setDescription("Edit the Name");
96
            nameIdButton = new IdButton<TaxonName>(TaxonName.class, regDto.getNameRef().getUuid(), nameButton);
97
            Label nameLabel = new Label(regDto.getNameRef().getLabel());
98
            nameLabel.setWidthUndefined();
99
            boolean userHasPermission = userHelper.userHasPermission(regDto.registration().getName(), CRUD.UPDATE);
100
            editButtons.add(new ButtonWithUserEditPermission(nameButton, userHasPermission));
101

    
102
            addComponent(nameIdButton.getButton());
103
            PermissionDebugUtils.addGainPerEntityPermissionButton(this, TaxonName.class, regDto.getNameRef().getUuid(),
104
                    EnumSet.of(CRUD.UPDATE, CRUD.DELETE), null);
105
            addComponent(nameLabel);
106
        } else {
107
            // no name in the registration! we only show the typified name as label
108
            if(regDto.getTypifiedNameRef() != null){
109
                nameLabel = new Label(regDto.getTypifiedNameRef().getLabel());
110
                addComponent(nameLabel);
111
            }
112
        }
113
        boolean userHasAddPermission = !regDto.isPersisted() || userHelper.userHasPermission(regDto.registration(), CRUD.UPDATE);
114
        LinkedHashMap<TypedEntityReference<? extends VersionableEntity>, TypeDesignationWorkingSet> workingSets = regDto.getOrderedTypeDesignationWorkingSets();
115
        if(workingSets != null){
116
            for(TypedEntityReference<? extends VersionableEntity> baseEntityRef : workingSets.keySet()) {
117
                TypeDesignationWorkingSet typeDesignationWorkingSet = workingSets.get(baseEntityRef);
118
                logger.debug("WorkingSet:" + typeDesignationWorkingSet.getWorkingsetType() + ">" + typeDesignationWorkingSet.getBaseEntityReference());
119
                String buttonLabel = SpecimenOrObservationBase.class.isAssignableFrom(baseEntityRef.getType()) ? "Type": "NameType";
120
                Button tdButton = new Button(buttonLabel + ":");
121
                tdButton.setDescription("Edit the type designation working set");
122
                boolean userHasUpdatePermission = userHelper.userHasPermission(baseEntityRef.getType(), baseEntityRef.getUuid(), CRUD.UPDATE, CRUD.DELETE);
123
                editButtons.add(new ButtonWithUserEditPermission(tdButton, userHasUpdatePermission));
124
                addComponent(tdButton);
125

    
126
                PermissionDebugUtils.addGainPerEntityPermissionButton(this, SpecimenOrObservationBase.class,
127
                        baseEntityRef.getUuid(), EnumSet.of(CRUD.UPDATE, CRUD.DELETE), RegistrationStatus.PREPARATION.name());
128

    
129
                typeDesignationButtons.add(new TypeDesignationWorkingSetButton(
130
                        typeDesignationWorkingSet.getWorkingsetType(),
131
                        typeDesignationWorkingSet.getBaseEntityReference(),
132
                        tdButton)
133
                        );
134
                String labelText = typeDesignationWorkingSet.getLabel();
135
                labelText = labelText.replaceAll("^[^:]+:", ""); // remove "Type:", "NameType:" from the beginning
136
                for(TypeDesignationStatusBase<?> typeStatus : typeDesignationWorkingSet.keySet()){
137
                    labelText = labelText.replace(typeStatus.getLabel(), "<strong>" + typeStatus.getLabel() + "</strong>");
138
                }
139
                if(typeDesignationWorkingSet.getWorkingsetType().equals(TypeDesignationWorkingSetType.NAME_TYPE_DESIGNATION_WORKINGSET)){
140
                    // remove the citation from the label which looks very redundant in the registration working set editor
141
                    // TODO when use in other contexts. it might be required to make this configurable.
142

    
143
                    String citationString = regDto.getCitation().getCitation();
144
                    labelText = labelText.replaceFirst(Pattern.quote(citationString), "");
145
                }
146
                Label label = new Label(labelText, ContentMode.HTML);
147

    
148
                label.setWidthUndefined();
149
                addComponent(label);
150
                labels.add(label);
151
            }
152
        }
153
        addTypeDesignationButton = ButtonFactory.ADD_ITEM.createButton();
154
        addTypeDesignationButton.setDescription("Add a new type designation workingset.");
155
        addTypeDesignationButton.setVisible(!isRegistrationLocked && userHasAddPermission);
156
        addComponent(addTypeDesignationButton);
157

    
158
        //TODO make responsive and use specificIdentifier in case the space gets too narrow
159
        if(regDto.isPersisted()){
160
            identifierLink = new Link(regDto.getIdentifier(), new ExternalResource(regDto.getIdentifier()));
161
            identifierLink.setEnabled(regDto.getStatus() == RegistrationStatus.PUBLISHED);
162
            addComponents(identifierLink);
163
        }
164

    
165
        iterator().forEachRemaining(c -> addStyledComponent(c));
166
        updateEditorButtonReadonlyStates();
167
        addDefaultStyles();
168

    
169
    }
170

    
171

    
172
    private void updateEditorButtonReadonlyStates() {
173
        for(ButtonWithUserEditPermission b : editButtons){
174
            boolean impossibleToUnlock = !b.userCanEdit && isLockOverride && isRegistrationLocked;
175
            b.button.setReadOnly((isRegistrationLocked && !isLockOverride) || !b.userCanEdit);
176
            b.button.setEnabled(!impossibleToUnlock);
177
            b.button.setDescription(impossibleToUnlock ? "Unlock failed due to missing permissions!" : "");
178
            b.button.setIcon(isLockOverride ? FontAwesome.UNLOCK_ALT : null);
179
        }
180

    
181
    }
182

    
183
    public IdButton<TaxonName> getNameButton() {
184
        return nameIdButton;
185
    }
186

    
187
    public List<TypeDesignationWorkingSetButton> getTypeDesignationButtons() {
188
        return typeDesignationButtons;
189
    }
190

    
191
    public Button getAddTypeDesignationButton() {
192
        return addTypeDesignationButton;
193
    }
194

    
195
    /**
196
     * {@inheritDoc}
197
     */
198
    @Override
199
    protected void addDefaultStyles() {
200
        addStyleName(STYLE_NAMES);
201
        if(nameIdButton != null){
202
            nameIdButton.getButton().addStyleName(DEFAULT_BUTTON_STYLES);
203
        }
204
        if(nameLabel != null){
205
            nameLabel.addStyleName("v-disabled");
206
        }
207
        typeDesignationButtons.forEach(idb -> idb.getButton().addStyleName(DEFAULT_BUTTON_STYLES));
208
        addTypeDesignationButton.addStyleName(DEFAULT_BUTTON_STYLES);
209
    }
210

    
211
    public class TypeDesignationWorkingSetButton {
212

    
213
        private TypedEntityReference baseEntityRef;
214
        private TypeDesignationWorkingSetType type;
215
        private Button button;
216

    
217
        public TypeDesignationWorkingSetButton(TypeDesignationWorkingSetType type, TypedEntityReference baseEntityRef, Button button){
218
            this.type = type;
219
            this.baseEntityRef = baseEntityRef;
220
            this.button = button;
221
        }
222

    
223
        /**
224
         * @return the id
225
         */
226
        public TypedEntityReference getBaseEntity() {
227
            return baseEntityRef;
228
        }
229

    
230
        /**
231
         * @return the button
232
         */
233
        public Button getButton() {
234
            return button;
235
        }
236

    
237
        /**
238
         * @return the type
239
         */
240
        public TypeDesignationWorkingSetType getType() {
241
            return type;
242
        }
243

    
244
    }
245

    
246
    public class IdButton<T> {
247
        private UUID uuid;
248
        private Class<T> entityType;
249
        private Button button;
250

    
251
        public IdButton(Class<T> type, UUID uuid, Button button){
252
            this.entityType = type;
253
            this.uuid = uuid;
254
            this.button = button;
255
        }
256

    
257
        /**
258
         * @return the id
259
         */
260
        public UUID getUuid() {
261
            return uuid;
262
        }
263

    
264
        /**
265
         * @return the button
266
         */
267
        public Button getButton() {
268
            return button;
269
        }
270

    
271
        /**
272
         * @return the type
273
         */
274
        public Class<T> getType() {
275
            return entityType;
276
        }
277

    
278
    }
279

    
280
    public class ButtonWithUserEditPermission {
281

    
282
        Button button;
283
        boolean userCanEdit;
284
        /**
285
         * @param button
286
         * @param userCanEdit
287
         */
288
        public ButtonWithUserEditPermission(Button button, boolean userCanEdit) {
289
            super();
290
            this.button = button;
291
            this.userCanEdit = userCanEdit;
292
        }
293

    
294

    
295

    
296
    }
297
    public boolean isRegistrationLocked() {
298
        return isRegistrationLocked;
299
    }
300

    
301

    
302
    /**
303
     * @return the isLockOverride
304
     */
305
    public boolean isLockOverride() {
306
        return isLockOverride;
307
    }
308

    
309
    /**
310
     * @param isLockOverride the isLockOverride to set
311
     */
312
    public void setLockOverride(boolean isLockOverride) {
313
        if(this.isLockOverride != isLockOverride){
314
            this.isLockOverride = isLockOverride;
315
            updateEditorButtonReadonlyStates();
316
        }
317
    }
318

    
319
}
(4-4/11)