Project

General

Profile

Download (9.62 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.ui.Button;
20
import com.vaadin.ui.Label;
21
import com.vaadin.ui.Link;
22
import com.vaadin.ui.themes.ValoTheme;
23

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

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

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

    
49

    
50
    private static final String DEFAULT_BUTTON_STYLES = "";
51

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

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

    
56
    private IdButton<TaxonName> nameIdButton = null;
57

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

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

    
62
    private Button addTypeDesignationButton;
63

    
64
    private Label nameLabel = null;
65

    
66
    private Link identifierLink;
67

    
68
    public RegistrationItemNameAndTypeButtons(RegistrationDTO regDto, ICdmEntityUuidCacher entitiyCacher) {
69

    
70
        boolean isRegistrationLocked = EnumSet.of(
71
                RegistrationStatus.PUBLISHED, RegistrationStatus.REJECTED)
72
                .contains(regDto.getStatus());
73

    
74
        setWidth(100, Unit.PERCENTAGE);
75

    
76
        UserHelper userHelper;
77
        if(entitiyCacher != null){
78
            userHelper = UserHelperAccess.userHelper().withCache(entitiyCacher);
79
        } else {
80
            userHelper = UserHelperAccess.userHelper();
81
        }
82

    
83
        if(regDto.getNameRef() != null){
84
            Button nameButton = new Button("Name:");
85
            nameButton.setDescription("Edit the Name");
86
            nameIdButton = new IdButton<TaxonName>(TaxonName.class, regDto.getNameRef().getUuid(), nameButton);
87
            Label nameLabel = new Label(regDto.getNameRef().getLabel());
88
            nameLabel.setWidthUndefined();
89
            boolean userHasPermission = userHelper.userHasPermission(regDto.registration().getName(), CRUD.UPDATE);
90
            nameButton.setReadOnly(isRegistrationLocked || ! userHasPermission);
91

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

    
115
                PermissionDebugUtils.addGainPerEntityPermissionButton(this, SpecimenOrObservationBase.class,
116
                        baseEntityRef.getUuid(), EnumSet.of(CRUD.UPDATE, CRUD.DELETE), RegistrationStatus.PREPARATION.name());
117

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

    
129
                    String citationString = regDto.getCitation().getCitation();
130
                    labelText = labelText.replaceFirst(citationString, "");
131
                }
132
                Label label = new Label(labelText);
133

    
134
                label.setWidthUndefined();
135
                addComponent(label);
136
                labels.add(label);
137
            }
138
        }
139
        addTypeDesignationButton = ButtonFactory.ADD_ITEM.createButton();
140
        addTypeDesignationButton.setDescription("Add a new type designation workingset.");
141
        addTypeDesignationButton.setVisible(!isRegistrationLocked && userHasAddPermission);
142
        addComponent(addTypeDesignationButton);
143

    
144
        //TODO make responsive and use specificIdentifier in case the space gets too narrow
145
        if(regDto.isPersisted()){
146
            identifierLink = new Link(regDto.getIdentifier(), new ExternalResource(regDto.getIdentifier()));
147
            identifierLink.setEnabled(regDto.getStatus() == RegistrationStatus.PUBLISHED);
148
            addComponents(identifierLink);
149
        }
150

    
151
        iterator().forEachRemaining(c -> addStyledComponent(c));
152
        addDefaultStyles();
153

    
154
    }
155

    
156
    public IdButton<TaxonName> getNameButton() {
157
        return nameIdButton;
158
    }
159

    
160
    public List<TypeDesignationWorkingSetButton> getTypeDesignationButtons() {
161
        return typeDesignationButtons;
162
    }
163

    
164
    public Button getAddTypeDesignationButton() {
165
        return addTypeDesignationButton;
166
    }
167

    
168
    /**
169
     * {@inheritDoc}
170
     */
171
    @Override
172
    protected void addDefaultStyles() {
173
        addStyleName(STYLE_NAMES);
174
        if(nameIdButton != null){
175
            nameIdButton.getButton().addStyleName(DEFAULT_BUTTON_STYLES);
176
        }
177
        if(nameLabel != null){
178
            nameLabel.addStyleName("v-disabled");
179
        }
180
        typeDesignationButtons.forEach(idb -> idb.getButton().addStyleName(DEFAULT_BUTTON_STYLES));
181
        addTypeDesignationButton.addStyleName(DEFAULT_BUTTON_STYLES);
182
    }
183

    
184
    public class TypeDesignationWorkingSetButton {
185

    
186
        private TypedEntityReference baseEntityRef;
187
        private TypeDesignationWorkingSetType type;
188
        private Button button;
189

    
190
        public TypeDesignationWorkingSetButton(TypeDesignationWorkingSetType type, TypedEntityReference baseEntityRef, Button button){
191
            this.type = type;
192
            this.baseEntityRef = baseEntityRef;
193
            this.button = button;
194
        }
195

    
196
        /**
197
         * @return the id
198
         */
199
        public TypedEntityReference getBaseEntity() {
200
            return baseEntityRef;
201
        }
202

    
203
        /**
204
         * @return the button
205
         */
206
        public Button getButton() {
207
            return button;
208
        }
209

    
210
        /**
211
         * @return the type
212
         */
213
        public TypeDesignationWorkingSetType getType() {
214
            return type;
215
        }
216

    
217
    }
218

    
219
    public class IdButton<T> {
220
        private UUID uuid;
221
        private Class<T> entityType;
222
        private Button button;
223

    
224
        public IdButton(Class<T> type, UUID uuid, Button button){
225
            this.entityType = type;
226
            this.uuid = uuid;
227
            this.button = button;
228
        }
229

    
230
        /**
231
         * @return the id
232
         */
233
        public UUID getUuid() {
234
            return uuid;
235
        }
236

    
237
        /**
238
         * @return the button
239
         */
240
        public Button getButton() {
241
            return button;
242
        }
243

    
244
        /**
245
         * @return the type
246
         */
247
        public Class<T> getType() {
248
            return entityType;
249
        }
250

    
251
    }
252

    
253
}
(4-4/11)