Project

General

Profile

Download (9.31 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.dto.TypedEntityReference;
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.model.name.Registration;
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.vaadin.component.ButtonFactory;
35
import eu.etaxonomy.cdm.vaadin.permission.PermissionDebugUtils;
36
import eu.etaxonomy.cdm.vaadin.permission.UserHelper;
37
import eu.etaxonomy.vaadin.component.CompositeStyledComponent;
38

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

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

    
48

    
49
    private static final String DEFAULT_BUTTON_STYLES = "";
50

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

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

    
55
    private IdButton<TaxonName> nameIdButton = null;
56

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

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

    
61
    private Button addTypeDesignationButton;
62

    
63
    private Label nameLabel = null;
64

    
65
    private Link identifierLink;
66

    
67
    public RegistrationItemNameAndTypeButtons(RegistrationDTO regDto) {
68

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

    
73
        setWidth(100, Unit.PERCENTAGE);
74

    
75
        if(regDto.getNameRef() != null){
76
            Button nameButton = new Button("Name:");
77
            nameButton.setDescription("Edit the Name");
78
            nameIdButton = new IdButton<TaxonName>(TaxonName.class, regDto.getNameRef().getUuid(), nameButton);
79
            Label nameLabel = new Label(regDto.getNameRef().getLabel());
80
            nameLabel.setWidthUndefined();
81
            boolean userHasPermission = UserHelper.fromSession().userHasPermission(regDto.registration().getName(), CRUD.UPDATE);
82
            nameButton.setReadOnly(isRegistrationLocked || ! userHasPermission);
83

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

    
107
                PermissionDebugUtils.addGainPerEntityPermissionButton(this, SpecimenOrObservationBase.class,
108
                        baseEntityRef.getUuid(), EnumSet.of(CRUD.UPDATE, CRUD.DELETE), RegistrationStatus.PREPARATION.name());
109

    
110
                typeDesignationButtons.add(new TypeDesignationWorkingSetButton(
111
                        typeDesignationWorkingSet.getWorkingsetType(),
112
                        typeDesignationWorkingSet.getBaseEntityReference(),
113
                        tdButton)
114
                        );
115
                String labelText = typeDesignationWorkingSet.getRepresentation();
116
                labelText = labelText.replaceAll("^[^:]+:", ""); // remove "Type:", "NameType:" from the beginning
117
                if(typeDesignationWorkingSet.getWorkingsetType().equals(TypeDesignationWorkingSetType.NAME_TYPE_DESIGNATION_WORKINGSET)){
118
                    // remove the citation from the label which looks very redundant in the registration working set editor
119
                    // TODO when use in other contexts. it might be required to make this configurable.
120

    
121
                    String citationString = regDto.getCitation().getCitation();
122
                    labelText = labelText.replaceFirst(citationString, "");
123
                }
124
                Label label = new Label(labelText);
125

    
126
                label.setWidthUndefined();
127
                addComponent(label);
128
                labels.add(label);
129
            }
130
        }
131
        addTypeDesignationButton = ButtonFactory.ADD_ITEM.createButton();
132
        addTypeDesignationButton.setDescription("Add a new type designation workingset.");
133
        addTypeDesignationButton.setVisible(!isRegistrationLocked && userHasAddPermission);
134
        addComponent(addTypeDesignationButton);
135

    
136
        //TODO make responsive and use specificIdentifier in case the space gets too narrow
137
        identifierLink = new Link(regDto.getIdentifier(), new ExternalResource(regDto.getIdentifier()));
138
        identifierLink.setEnabled(regDto.getStatus() == RegistrationStatus.PUBLISHED);
139

    
140
        addComponents(identifierLink);
141

    
142
        iterator().forEachRemaining(c -> addStyledComponent(c));
143
        addDefaultStyles();
144

    
145
    }
146

    
147
    public IdButton<TaxonName> getNameButton() {
148
        return nameIdButton;
149
    }
150

    
151
    public List<TypeDesignationWorkingSetButton> getTypeDesignationButtons() {
152
        return typeDesignationButtons;
153
    }
154

    
155
    public Button getAddTypeDesignationButton() {
156
        return addTypeDesignationButton;
157
    }
158

    
159
    /**
160
     * {@inheritDoc}
161
     */
162
    @Override
163
    protected void addDefaultStyles() {
164
        addStyleName(STYLE_NAMES);
165
        if(nameIdButton != null){
166
            nameIdButton.getButton().addStyleName(DEFAULT_BUTTON_STYLES);
167
        }
168
        if(nameLabel != null){
169
            nameLabel.addStyleName("v-disabled");
170
        }
171
        typeDesignationButtons.forEach(idb -> idb.getButton().addStyleName(DEFAULT_BUTTON_STYLES));
172
        addTypeDesignationButton.addStyleName(DEFAULT_BUTTON_STYLES);
173
    }
174

    
175
    public class TypeDesignationWorkingSetButton {
176

    
177
        private TypedEntityReference baseEntityRef;
178
        private TypeDesignationWorkingSetType type;
179
        private Button button;
180

    
181
        public TypeDesignationWorkingSetButton(TypeDesignationWorkingSetType type, TypedEntityReference baseEntityRef, Button button){
182
            this.type = type;
183
            this.baseEntityRef = baseEntityRef;
184
            this.button = button;
185
        }
186

    
187
        /**
188
         * @return the id
189
         */
190
        public TypedEntityReference getBaseEntity() {
191
            return baseEntityRef;
192
        }
193

    
194
        /**
195
         * @return the button
196
         */
197
        public Button getButton() {
198
            return button;
199
        }
200

    
201
        /**
202
         * @return the type
203
         */
204
        public TypeDesignationWorkingSetType getType() {
205
            return type;
206
        }
207

    
208
    }
209

    
210
    public class IdButton<T> {
211
        private UUID uuid;
212
        private Class<T> entityType;
213
        private Button button;
214

    
215
        public IdButton(Class<T> type, UUID uuid, Button button){
216
            this.entityType = type;
217
            this.uuid = uuid;
218
            this.button = button;
219
        }
220

    
221
        /**
222
         * @return the id
223
         */
224
        public UUID getUuid() {
225
            return uuid;
226
        }
227

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

    
235
        /**
236
         * @return the type
237
         */
238
        public Class<T> getType() {
239
            return entityType;
240
        }
241

    
242
    }
243

    
244
}
(4-4/11)