Project

General

Profile

Revision 580d5157

ID580d5157a12f54778106d6f32bfe17694a58469a
Parent 595150ac
Child ee53ccf7

Added by Andreas Kohlbecker 4 months 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
490 490
                    if(event.isCreateOrModifiedType()){
491 491
                        getCache().load(otherName);
492 492
                        if(event.isCreatedType()){
493
                            // TODO use reloadWith((TaxonName) event.getEntity()); also in this case?
493 494
                            otherNameField.setValue(otherName);
494 495
                        } else {
495
                            otherNameField.reload();
496
                            otherNameField.reloadWith(otherName);
496 497
                        }
497 498

  
498 499
                    } else
......
505 506
                    if(event.isCreateOrModifiedType()){
506 507
                        getCache().load(event.getEntity());
507 508
                        if(event.isCreatedType()){
509
                            // TODO use reloadWith((TaxonName) event.getEntity()); also in this case?
508 510
                            basionymSourceField .setValue((TaxonName) event.getEntity());
509 511
                        } else {
510
                            basionymSourceField.reload();
512
                            basionymSourceField.reloadWith((TaxonName) event.getEntity());
511 513
                        }
512 514
                        getView().getBasionymAuthorshipField().discard(); //refresh from the datasource
513 515
                        getView().getExBasionymAuthorshipField().discard(); //refresh from the datasource
......
523 525
                    if(event.isCreateOrModifiedType()){
524 526
                        getCache().load(event.getEntity());
525 527
                        if(event.isCreatedType()){
528
                            // TODO use reloadWith((TaxonName) event.getEntity()); also in this case?
526 529
                            replacedSynonyms .setValue((TaxonName) event.getEntity());
527 530
                        } else {
528
                            replacedSynonyms.reload();
531
                            replacedSynonyms.reloadWith((TaxonName) event.getEntity());
529 532
                        }
530 533
                        getView().getExCombinationAuthorshipField().discard(); //refresh from the datasource
531 534
                        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
271 271
        triggerNestedButtonStateUpdaters();
272 272
    }
273 273

  
274

  
275

  
274 276
    /**
275 277
     *
276 278
     */
src/main/java/eu/etaxonomy/vaadin/component/NameRelationField.java
86 86

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

  
90
        setValidationVisible(false);
89 91
    }
90 92

  
91 93
    /**
src/main/java/eu/etaxonomy/vaadin/component/ReloadableLazyComboBox.java
54 54
        discard(); // reload from property data source
55 55
    }
56 56

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

  
57 74

  
58 75
    /**
59 76
     * This method allows updating the value even if the equals check done
src/main/java/eu/etaxonomy/vaadin/component/ToManyRelatedEntitiesListSelect.java
19 19
import org.apache.log4j.Logger;
20 20

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

  
89 90
    private boolean creatingFields;
90 91

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

  
91 94
    public  ToManyRelatedEntitiesListSelect(Class<V> itemType, Class<F> fieldType, String caption){
92 95
        this.fieldType = fieldType;
93 96
        this.itemType = itemType;
......
110 113
    }
111 114

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

  
128
    /**
113 129
     * @param field
114 130
     * @return
115 131
     */
......
317 333
    protected int addNewRow(int row, V val) {
318 334
        try {
319 335
            F field = newFieldInstance(val);
336
            for(Validator validator : fieldValidators) {
337
                field.addValidator(validator);
338
            }
320 339
            ButtonGroup buttonGroup = new ButtonGroup(field);
321 340
            updateEditOrCreateButton(buttonGroup, val);
322 341
            field.addValueChangeListener(e -> {
......
530 549
    }
531 550

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

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

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

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

  

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)