Project

General

Profile

Download (11.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 java.util.ArrayList;
12
import java.util.EnumSet;
13
import java.util.List;
14
import java.util.UUID;
15

    
16
import org.apache.log4j.Logger;
17

    
18
import com.vaadin.server.ExternalResource;
19
import com.vaadin.server.FontAwesome;
20
import com.vaadin.ui.Button;
21
import com.vaadin.ui.Label;
22
import com.vaadin.ui.Link;
23
import com.vaadin.ui.themes.ValoTheme;
24

    
25
import eu.etaxonomy.cdm.api.service.dto.RegistrationDTO;
26
import eu.etaxonomy.cdm.api.service.name.TypeDesignationSetManager.TypeDesignationWorkingSet;
27
import eu.etaxonomy.cdm.api.service.name.TypeDesignationSetManager.TypeDesignationWorkingSetType;
28
import eu.etaxonomy.cdm.api.utility.UserHelper;
29
import eu.etaxonomy.cdm.model.ICdmEntityUuidCacher;
30
import eu.etaxonomy.cdm.model.name.RegistrationStatus;
31
import eu.etaxonomy.cdm.model.name.TaxonName;
32
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
33
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
34
import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
35
import eu.etaxonomy.cdm.ref.TypedEntityReference;
36
import eu.etaxonomy.cdm.service.UserHelperAccess;
37
import eu.etaxonomy.cdm.vaadin.component.ButtonFactory;
38
import eu.etaxonomy.cdm.vaadin.permission.PermissionDebugUtils;
39
import eu.etaxonomy.vaadin.component.CompositeStyledComponent;
40

    
41
/**
42
 * @author a.kohlbecker
43
 * @since May 19, 2017
44
 *
45
 */
46
public class RegistrationItemNameAndTypeButtons extends CompositeStyledComponent {
47

    
48
    private final static Logger logger = Logger.getLogger(RegistrationItemNameAndTypeButtons.class);
49

    
50

    
51
    private static final String DEFAULT_BUTTON_STYLES = "";
52

    
53
    private static final long serialVersionUID = -5059163772392864050L;
54

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

    
57
    private IdButton<TaxonName> nameIdButton = null;
58

    
59
    private List<TypeDesignationWorkingSetButton> typeDesignationButtons = new ArrayList<>();
60

    
61
    private List<Label> labels = new ArrayList<>();
62

    
63
    private List<ButtonWithUserEditPermission> editButtons = new ArrayList<>();
64

    
65
    private Button addTypeDesignationButton;
66

    
67
    private Label nameLabel = null;
68

    
69
    private Link identifierLink;
70

    
71
    private boolean isRegistrationLocked;
72

    
73
    private boolean isLockOverride;
74

    
75
    public RegistrationItemNameAndTypeButtons(RegistrationDTO regDto, ICdmEntityUuidCacher entitiyCacher) {
76

    
77
        isRegistrationLocked = EnumSet.of(
78
                RegistrationStatus.PUBLISHED, RegistrationStatus.REJECTED)
79
                .contains(regDto.getStatus());
80

    
81
        setWidth(100, Unit.PERCENTAGE);
82

    
83
        UserHelper userHelper;
84
        if(entitiyCacher != null){
85
            userHelper = UserHelperAccess.userHelper().withCache(entitiyCacher);
86
        } else {
87
            userHelper = UserHelperAccess.userHelper();
88
        }
89

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

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

    
122
                PermissionDebugUtils.addGainPerEntityPermissionButton(this, SpecimenOrObservationBase.class,
123
                        baseEntityRef.getUuid(), EnumSet.of(CRUD.UPDATE, CRUD.DELETE), RegistrationStatus.PREPARATION.name());
124

    
125
                typeDesignationButtons.add(new TypeDesignationWorkingSetButton(
126
                        typeDesignationWorkingSet.getWorkingsetType(),
127
                        typeDesignationWorkingSet.getBaseEntityReference(),
128
                        tdButton)
129
                        );
130
                String labelText = typeDesignationWorkingSet.getRepresentation();
131
                labelText = labelText.replaceAll("^[^:]+:", ""); // remove "Type:", "NameType:" from the beginning
132
                if(typeDesignationWorkingSet.getWorkingsetType().equals(TypeDesignationWorkingSetType.NAME_TYPE_DESIGNATION_WORKINGSET)){
133
                    // remove the citation from the label which looks very redundant in the registration working set editor
134
                    // TODO when use in other contexts. it might be required to make this configurable.
135

    
136
                    String citationString = regDto.getCitation().getCitation();
137
                    labelText = labelText.replaceFirst(citationString, "");
138
                }
139
                Label label = new Label(labelText);
140

    
141
                label.setWidthUndefined();
142
                addComponent(label);
143
                labels.add(label);
144
            }
145
        }
146
        addTypeDesignationButton = ButtonFactory.ADD_ITEM.createButton();
147
        addTypeDesignationButton.setDescription("Add a new type designation workingset.");
148
        addTypeDesignationButton.setVisible(!isRegistrationLocked && userHasAddPermission);
149
        addComponent(addTypeDesignationButton);
150

    
151
        //TODO make responsive and use specificIdentifier in case the space gets too narrow
152
        if(regDto.isPersisted()){
153
            identifierLink = new Link(regDto.getIdentifier(), new ExternalResource(regDto.getIdentifier()));
154
            identifierLink.setEnabled(regDto.getStatus() == RegistrationStatus.PUBLISHED);
155
            addComponents(identifierLink);
156
        }
157

    
158
        iterator().forEachRemaining(c -> addStyledComponent(c));
159
        updateEditorButtonReadonlyStates();
160
        addDefaultStyles();
161

    
162
    }
163

    
164

    
165
    private void updateEditorButtonReadonlyStates() {
166
        for(ButtonWithUserEditPermission b : editButtons){
167
            boolean impossibleToUnlock = !b.userCanEdit && isLockOverride && isRegistrationLocked;
168
            b.button.setReadOnly((isRegistrationLocked && !isLockOverride) || !b.userCanEdit);
169
            b.button.setEnabled(!impossibleToUnlock);
170
            b.button.setDescription(impossibleToUnlock ? "Unlock failed due to missing permissions!" : "");
171
            b.button.setIcon(isLockOverride ? FontAwesome.UNLOCK_ALT : null);
172
        }
173

    
174
    }
175

    
176
    public IdButton<TaxonName> getNameButton() {
177
        return nameIdButton;
178
    }
179

    
180
    public List<TypeDesignationWorkingSetButton> getTypeDesignationButtons() {
181
        return typeDesignationButtons;
182
    }
183

    
184
    public Button getAddTypeDesignationButton() {
185
        return addTypeDesignationButton;
186
    }
187

    
188
    /**
189
     * {@inheritDoc}
190
     */
191
    @Override
192
    protected void addDefaultStyles() {
193
        addStyleName(STYLE_NAMES);
194
        if(nameIdButton != null){
195
            nameIdButton.getButton().addStyleName(DEFAULT_BUTTON_STYLES);
196
        }
197
        if(nameLabel != null){
198
            nameLabel.addStyleName("v-disabled");
199
        }
200
        typeDesignationButtons.forEach(idb -> idb.getButton().addStyleName(DEFAULT_BUTTON_STYLES));
201
        addTypeDesignationButton.addStyleName(DEFAULT_BUTTON_STYLES);
202
    }
203

    
204
    public class TypeDesignationWorkingSetButton {
205

    
206
        private TypedEntityReference baseEntityRef;
207
        private TypeDesignationWorkingSetType type;
208
        private Button button;
209

    
210
        public TypeDesignationWorkingSetButton(TypeDesignationWorkingSetType type, TypedEntityReference baseEntityRef, Button button){
211
            this.type = type;
212
            this.baseEntityRef = baseEntityRef;
213
            this.button = button;
214
        }
215

    
216
        /**
217
         * @return the id
218
         */
219
        public TypedEntityReference getBaseEntity() {
220
            return baseEntityRef;
221
        }
222

    
223
        /**
224
         * @return the button
225
         */
226
        public Button getButton() {
227
            return button;
228
        }
229

    
230
        /**
231
         * @return the type
232
         */
233
        public TypeDesignationWorkingSetType getType() {
234
            return type;
235
        }
236

    
237
    }
238

    
239
    public class IdButton<T> {
240
        private UUID uuid;
241
        private Class<T> entityType;
242
        private Button button;
243

    
244
        public IdButton(Class<T> type, UUID uuid, Button button){
245
            this.entityType = type;
246
            this.uuid = uuid;
247
            this.button = button;
248
        }
249

    
250
        /**
251
         * @return the id
252
         */
253
        public UUID getUuid() {
254
            return uuid;
255
        }
256

    
257
        /**
258
         * @return the button
259
         */
260
        public Button getButton() {
261
            return button;
262
        }
263

    
264
        /**
265
         * @return the type
266
         */
267
        public Class<T> getType() {
268
            return entityType;
269
        }
270

    
271
    }
272

    
273
    public class ButtonWithUserEditPermission {
274

    
275
        Button button;
276
        boolean userCanEdit;
277
        /**
278
         * @param button
279
         * @param userCanEdit
280
         */
281
        public ButtonWithUserEditPermission(Button button, boolean userCanEdit) {
282
            super();
283
            this.button = button;
284
            this.userCanEdit = userCanEdit;
285
        }
286

    
287

    
288

    
289
    }
290
    public boolean isRegistrationLocked() {
291
        return isRegistrationLocked;
292
    }
293

    
294

    
295
    /**
296
     * @return the isLockOverride
297
     */
298
    public boolean isLockOverride() {
299
        return isLockOverride;
300
    }
301

    
302
    /**
303
     * @param isLockOverride the isLockOverride to set
304
     */
305
    public void setLockOverride(boolean isLockOverride) {
306
        if(this.isLockOverride != isLockOverride){
307
            this.isLockOverride = isLockOverride;
308
            updateEditorButtonReadonlyStates();
309
        }
310
    }
311

    
312
}
(4-4/11)