Project

General

Profile

« Previous | Next » 

Revision 6062e4a0

Added by Andreas Kohlbecker about 5 years ago

ref #8050 validing related name nomenclatural reference to require the user to add missing references when reusing an exising name

View differences:

src/main/java/eu/etaxonomy/cdm/vaadin/data/validator/NomenclaturalReferenceExistsValidator.java
1
/**
2
* Copyright (C) 2019 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.data.validator;
10

  
11
import com.vaadin.data.Validator;
12

  
13
import eu.etaxonomy.cdm.model.name.TaxonName;
14
import eu.etaxonomy.cdm.vaadin.model.name.NameRelationshipDTO;
15

  
16
/**
17
 * Validates a {@link TaxonName} for existence of the nomenclatural reference.
18
 *
19
 * Compatible with fields operating on {@link TaxonName}, {@link NameRelationshipDTO},
20
 *
21
 * @author a.kohlbecker
22
 * @since Mar 27, 2019
23
 *
24
 */
25
public class NomenclaturalReferenceExistsValidator implements Validator {
26

  
27

  
28
    private static final long serialVersionUID = -7750232876262922982L;
29

  
30
    private String userHint = "";
31

  
32
    public NomenclaturalReferenceExistsValidator(String userHint){
33
        this.userHint = userHint;
34
    }
35

  
36
    /**
37
     * {@inheritDoc}
38
     */
39
    @Override
40
    public void validate(Object value) throws InvalidValueException {
41
        if(value != null){
42
            if(value instanceof TaxonName){
43
                TaxonName name = (TaxonName)value;
44
                validateName(name);
45
            }
46
            if(value instanceof NameRelationshipDTO){
47
                NameRelationshipDTO nameRelDto = (NameRelationshipDTO)value;
48
                validateName(nameRelDto.getOtherName());
49
            }
50
        }
51

  
52
    }
53

  
54
    /**
55
     * @param name
56
     */
57
    public void validateName(TaxonName name) {
58
        if(name.getNomenclaturalReference() == null) {
59
            throw new InvalidValueException("The taxon name must have a nomenclatural reference. " + userHint);
60
        }
61
    }
62

  
63
}
src/main/java/eu/etaxonomy/cdm/vaadin/view/name/TaxonNameEditorPresenter.java
495 495
                    if(event.isCreateOrModifiedType()){
496 496
                        getCache().load(otherName);
497 497
                        if(event.isCreatedType()){
498
                            // TODO use reloadWith((TaxonName) event.getEntity()); also in this case?
498 499
                            otherNameField.setValue(otherName);
499 500
                        } else {
500
                            otherNameField.reload();
501
                            otherNameField.reloadWith(otherName);
501 502
                        }
502 503

  
503 504
                    } else
......
510 511
                    if(event.isCreateOrModifiedType()){
511 512
                        getCache().load(event.getEntity());
512 513
                        if(event.isCreatedType()){
514
                            // TODO use reloadWith((TaxonName) event.getEntity()); also in this case?
513 515
                            basionymSourceField .setValue((TaxonName) event.getEntity());
514 516
                        } else {
515
                            basionymSourceField.reload();
517
                            basionymSourceField.reloadWith((TaxonName) event.getEntity());
516 518
                        }
517 519
                        getView().getBasionymAuthorshipField().discard(); //refresh from the datasource
518 520
                        getView().getExBasionymAuthorshipField().discard(); //refresh from the datasource
......
528 530
                    if(event.isCreateOrModifiedType()){
529 531
                        getCache().load(event.getEntity());
530 532
                        if(event.isCreatedType()){
533
                            // TODO use reloadWith((TaxonName) event.getEntity()); also in this case?
531 534
                            replacedSynonyms .setValue((TaxonName) event.getEntity());
532 535
                        } else {
533
                            replacedSynonyms.reload();
536
                            replacedSynonyms.reloadWith((TaxonName) event.getEntity());
534 537
                        }
535 538
                        getView().getExCombinationAuthorshipField().discard(); //refresh from the datasource
536 539
                        getView().updateAuthorshipFields();
src/main/java/eu/etaxonomy/cdm/vaadin/view/name/TaxonNamePopupEditor.java
39 39
import eu.etaxonomy.cdm.vaadin.component.TextFieldNFix;
40 40
import eu.etaxonomy.cdm.vaadin.component.common.FilterableAnnotationsField;
41 41
import eu.etaxonomy.cdm.vaadin.component.common.TeamOrPersonField;
42
import eu.etaxonomy.cdm.vaadin.data.validator.NomenclaturalReferenceExistsValidator;
42 43
import eu.etaxonomy.cdm.vaadin.event.ReferenceEditorAction;
43 44
import eu.etaxonomy.cdm.vaadin.event.TaxonNameEditorAction;
44 45
import eu.etaxonomy.cdm.vaadin.event.TaxonNameEditorActionStrRep;
......
611 612
            nomReferenceCombobox.setDescription("Selection limited to nomenclatural reference and parts of it.");
612 613
        }
613 614
        if(isModeEnabled(TaxonNamePopupEditorMode.REQUIRE_NOMENCLATURALREFERENCE)) {
614
            if(combinationAuthorshipField.getValue() == null){
615
                nomReferenceCombobox.setRequired(true);
616
                nomReferenceCombobox.setImmediate(true);
617
            } else {
618
                combinationAuthorshipField.addValueChangeListener(e -> {
619
                    if(e.getProperty().getValue() == null){
620
                        nomReferenceCombobox.setRequired(true);
621
                        nomReferenceCombobox.setImmediate(true);
622
                    }
623
                });
624
            }
615
            nomReferenceCombobox.setRequired(true);
616
            nomReferenceCombobox.setImmediate(true);
617

  
618
            String userHint = "Please use the 'Edit' function to fix the problem in the related name.";
619
            validationField.getRelatedNameComboBox().getSelect().addValidator(new NomenclaturalReferenceExistsValidator(userHint));
620
            orthographicVariantField.getRelatedNameComboBox().getSelect().addValidator(new NomenclaturalReferenceExistsValidator(userHint));
621
            basionymsComboboxSelect.addFieldValidator(new NomenclaturalReferenceExistsValidator(userHint));
622
            replacedSynonymsComboboxSelect.addFieldValidator(new NomenclaturalReferenceExistsValidator(userHint));
625 623
        }
626 624
    }
627 625

  
src/main/java/eu/etaxonomy/cdm/vaadin/view/name/TaxonNamePopupEditorMode.java
32 32
    NOMENCLATURALREFERENCE_SECTION_EDITING_ONLY,
33 33

  
34 34
    /**
35
     * setting the nomenclatural reference is required with the exception
36
     * that existing data is considered complete if the combination
37
     * authors are set.
35
     * setting the nomenclatural reference is required. This also accounts for names realted to the name
36
     * being edites in the TaxonNamePopupEditor (basionyms, validation, replaced synonmys, ...).
38 37
     */
39 38
    REQUIRE_NOMENCLATURALREFERENCE,
40 39

  
src/main/java/eu/etaxonomy/vaadin/component/CompositeCustomField.java
267 267
        triggerNestedButtonStateUpdaters();
268 268
    }
269 269

  
270

  
271

  
270 272
    /**
271 273
     *
272 274
     */
src/main/java/eu/etaxonomy/vaadin/component/NameRelationField.java
84 84

  
85 85
        relatedNameComboBox = new ToOneRelatedEntityCombobox<TaxonName>(this.nameFieldCaption, TaxonName.class);
86 86
        citatonComboBox = new ToOneRelatedEntityCombobox<Reference>("Reference", Reference.class);
87

  
88
        setValidationVisible(false);
87 89
    }
88 90

  
89 91
    /**
src/main/java/eu/etaxonomy/vaadin/component/ReloadableLazyComboBox.java
60 60
        discard(); // reload from property data source
61 61
    }
62 62

  
63
    /**
64
     * Calls {@link #reload()} but checks is the <code>bean</code> passed as
65
     * parameter has a different caption than the bean which is currently selected.
66
     *
67
     * Differences in the captions can occur for the same entity when the entity
68
     * has been modified through a popup edior.
69
     *
70
     * @param bean
71
     */
72
    public void reloadWith(T bean){
73
        boolean reapplyBean = getCaption(bean).equals(getCaption(getValue()));
74
        reload();
75
        if(reapplyBean){
76
            setValue(bean);
77
        }
78
    }
79

  
63 80

  
64 81
    /**
65 82
     * This method allows updating the value even if the equals check done
src/main/java/eu/etaxonomy/vaadin/component/ToManyRelatedEntitiesListSelect.java
18 18
import org.apache.log4j.Logger;
19 19

  
20 20
import com.vaadin.data.Property;
21
import com.vaadin.data.Validator;
21 22
import com.vaadin.data.Validator.InvalidValueException;
22 23
import com.vaadin.data.fieldgroup.FieldGroup;
23 24
import com.vaadin.server.FontAwesome;
......
87 88

  
88 89
    private boolean creatingFields;
89 90

  
91
    private List<Validator> fieldValidators = new ArrayList<>();
92

  
90 93
    public  ToManyRelatedEntitiesListSelect(Class<V> itemType, Class<F> fieldType, String caption){
91 94
        this.fieldType = fieldType;
92 95
        this.itemType = itemType;
......
108 111
        return row;
109 112
    }
110 113

  
114
    /**
115
     *
116
     * @return an unmodifiable List of the data Fields
117
     */
118
    protected List<F> fields() {
119
        Integer row = null;
120
        List<F> fields = new ArrayList<>();
121
        for(int r = 0; r < grid.getRows(); r++){
122
            fields.add((F) grid.getComponent(GRID_X_FIELD, r));
123
        }
124
        return fields;
125
    }
126

  
111 127
    /**
112 128
     * @param field
113 129
     * @return
......
316 332
    protected int addNewRow(int row, V val) {
317 333
        try {
318 334
            F field = newFieldInstance(val);
335
            for(Validator validator : fieldValidators) {
336
                field.addValidator(validator);
337
            }
319 338
            ButtonGroup buttonGroup = new ButtonGroup(field);
320 339
            updateEditOrCreateButton(buttonGroup, val);
321 340
            field.addValueChangeListener(e -> {
......
528 547
        return field;
529 548
    }
530 549

  
550
    /**
551
     * Adds the validator to the list of validators which
552
     * are applied to new fields and adds the validator to
553
     * existing fields
554
     *
555
     * @param validator
556
     */
557
    public void addFieldValidator(Validator validator){
558
        fieldValidators.add(validator);
559
        for(F field : fields()) {
560
            field.addValidator(validator);
561
        }
562
    }
563

  
564
    /**
565
     * removes the validator from the list of validators which
566
     * are applied to new fields and removes the validator from
567
     * existing fields
568
     *
569
     * @param validator
570
     */
571
    public void removeFieldValidator(Validator validator){
572
        fieldValidators.remove(validator);
573
        for(F field : fields()) {
574
            field.removeValidator(validator);
575
        }
576
    }
577

  
578
    /**
579
     * @return a unmodifialble List of the fieldValidators
580
     */
581
    public List<Validator> getFieldValidators() {
582
        return Collections.unmodifiableList(fieldValidators);
583
    }
584

  
531 585
    /**
532 586
     * Handle the data binding of the sub fields. Sub-fields can either be composite editor fields
533 587
     * or 'simple' fields, usually select fields.
src/main/java/eu/etaxonomy/vaadin/component/ToOneRelatedEntityCombobox.java
55 55
        this.type = type;
56 56
        setCaption(caption);
57 57
        lazySelect = new ReloadableLazyComboBox<V>(type);
58
        lazySelect.setValidationVisible(false); // validation is to be shown for the ToOneRelatedEntityCombobox
59 58
        lazySelect.setRequiredError("Must be given");
60 59
        setRequiredError("Must be given");
61 60
        addStyledComponents(lazySelect, addButton, editButton);
......
195 194
            lazySelect.commit();
196 195
        } catch (InvalidValueException ex){
197 196
            UserError componentError = new UserError(ex.getHtmlMessage(), ContentMode.HTML, ErrorLevel.ERROR);
198
            setComponentError(componentError);
197
            lazySelect.setComponentError(componentError);
199 198
        }
200 199
    }
201 200

  

Also available in: Unified diff