Project

General

Profile

Download (11.6 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
import java.util.regex.Pattern;
16

    
17
import org.apache.log4j.Logger;
18

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

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

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

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

    
51

    
52
    private static final String DEFAULT_BUTTON_STYLES = "";
53

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

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

    
58
    private IdButton<TaxonName> nameIdButton = null;
59

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

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

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

    
66
    private Button addTypeDesignationButton;
67

    
68
    private Label nameLabel = null;
69

    
70
    private Link identifierLink;
71

    
72
    private boolean isRegistrationLocked;
73

    
74
    private boolean isLockOverride;
75

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

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

    
82
        setWidth(100, Unit.PERCENTAGE);
83

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

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

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

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

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

    
140
                    String citationString = regDto.getCitation().getCitation();
141
                    labelText = labelText.replaceFirst(Pattern.quote(citationString), "");
142
                }
143
                Label label = new Label(labelText, ContentMode.HTML);
144

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

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

    
162
        iterator().forEachRemaining(c -> addStyledComponent(c));
163
        updateEditorButtonReadonlyStates();
164
        addDefaultStyles();
165

    
166
    }
167

    
168

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

    
178
    }
179

    
180
    public IdButton<TaxonName> getNameButton() {
181
        return nameIdButton;
182
    }
183

    
184
    public List<TypeDesignationWorkingSetButton> getTypeDesignationButtons() {
185
        return typeDesignationButtons;
186
    }
187

    
188
    public Button getAddTypeDesignationButton() {
189
        return addTypeDesignationButton;
190
    }
191

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

    
208
    public class TypeDesignationWorkingSetButton {
209

    
210
        private TypedEntityReference baseEntityRef;
211
        private TypeDesignationWorkingSetType type;
212
        private Button button;
213

    
214
        public TypeDesignationWorkingSetButton(TypeDesignationWorkingSetType type, TypedEntityReference baseEntityRef, Button button){
215
            this.type = type;
216
            this.baseEntityRef = baseEntityRef;
217
            this.button = button;
218
        }
219

    
220
        /**
221
         * @return the id
222
         */
223
        public TypedEntityReference getBaseEntity() {
224
            return baseEntityRef;
225
        }
226

    
227
        /**
228
         * @return the button
229
         */
230
        public Button getButton() {
231
            return button;
232
        }
233

    
234
        /**
235
         * @return the type
236
         */
237
        public TypeDesignationWorkingSetType getType() {
238
            return type;
239
        }
240

    
241
    }
242

    
243
    public class IdButton<T> {
244
        private UUID uuid;
245
        private Class<T> entityType;
246
        private Button button;
247

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

    
254
        /**
255
         * @return the id
256
         */
257
        public UUID getUuid() {
258
            return uuid;
259
        }
260

    
261
        /**
262
         * @return the button
263
         */
264
        public Button getButton() {
265
            return button;
266
        }
267

    
268
        /**
269
         * @return the type
270
         */
271
        public Class<T> getType() {
272
            return entityType;
273
        }
274

    
275
    }
276

    
277
    public class ButtonWithUserEditPermission {
278

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

    
291

    
292

    
293
    }
294
    public boolean isRegistrationLocked() {
295
        return isRegistrationLocked;
296
    }
297

    
298

    
299
    /**
300
     * @return the isLockOverride
301
     */
302
    public boolean isLockOverride() {
303
        return isLockOverride;
304
    }
305

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

    
316
}
(4-4/11)