Project

General

Profile

Revision bd1bf93c

IDbd1bf93c1adc6e6730e5560df44d9cdf565b3563
Parent 4e195c54
Child bb750b55

Added by Andreas Müller over 3 years ago

ref #6459 added IntextReferenceHelper class and added some more tests (for name and Annotation)

View differences:

cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/common/Annotation.java
159 159
	}
160 160

  
161 161
	@Override
162
    public IntextReference addIntextReference(IIntextReferenceTarget target, String start, String inner, String end){
163
        return IntextReferenceHelper.addIntextReference(target, this, start, inner, end);
164
    }
165
    @Override
166
    public IntextReference addIntextReference(IIntextReferenceTarget target, int start, int end){
167
        return IntextReferenceHelper.addIntextReference(target, this, start, end);
168
    }
169

  
170
	@Override
162 171
    public void addIntextReference(IntextReference intextReference){
163 172
		if (intextReference != null){
164 173
			intextReference.setReferencedEntity(this);
cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/common/IIntextReferencable.java
24 24
     * @param text the new referenced text
25 25
     */
26 26
    public void setText(String text);
27

  
28
    /**
29
     * Adds an {@link IntextReference} and
30
     * sets the text of the referenced entity.<BR>
31
     *
32
     * NOTE: this will override any existing
33
     * text.
34
     *
35
     * @param target
36
     * @param start
37
     * @param inner
38
     * @param end
39
     * @return
40
     */
41
    public IntextReference addIntextReference(IIntextReferenceTarget target, String start, String inner, String end);
42

  
43
    /**
44
     * Adds an
45
     * @param target
46
     * @param start
47
     * @param end
48
     * @return
49
     */
50
    public IntextReference addIntextReference(IIntextReferenceTarget target, int start, int end);
27 51
}
cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/common/IntextReferenceHelper.java
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.model.common;
10

  
11
/**
12
 * Utility class to handle {@link IntextReference}s
13
 *
14
 * @author a.mueller
15
 * @date 10.03.2017
16
 *
17
 */
18
public final class IntextReferenceHelper {
19

  
20
    public static IntextReference addIntextReference(IIntextReferenceTarget target, IIntextReferencable referencedEntity, String start, String inner, String end){
21
        IntextReference intextReference = IntextReference.NewInstance(target, referencedEntity, 0, 0);
22
        referencedEntity.setText(start + intextReference.toInlineString(inner) + end);
23
        referencedEntity.getIntextReferences().add(intextReference);
24
        return intextReference;
25
    }
26
    public static IntextReference addIntextReference(IIntextReferenceTarget target, IIntextReferencable referencedEntity, int start, int end){
27
        String text = referencedEntity.getText();
28
        if (start < 0 || end < 0 || start > end || end > text.length()){
29
            throw new IndexOutOfBoundsException("Start and end must be within bounds");
30
        }
31
        IntextReference intextReference = IntextReference.NewInstance(target, referencedEntity, 0, 0);
32
        referencedEntity.setText(text.substring(0, start) + intextReference.toInlineString(text.substring(start,end))
33
            + text.substring(end));
34
        referencedEntity.getIntextReferences().add(intextReference);
35
        return intextReference;
36
    }
37
}
cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/common/LanguageString.java
66 66

  
67 67

  
68 68
    public IntextReference addIntextReference(IIntextReferenceTarget target, String start, String inner, String end){
69
        IntextReference intextReference = IntextReference.NewInstance(target, this, 0, 0);
70
        setText(start + intextReference.toInlineString(inner) + end);
71
        getIntextReferences().add(intextReference);
72
        return intextReference;
69
        return IntextReferenceHelper.addIntextReference(target, this, start, inner, end);
73 70
    }
74 71
    public IntextReference addIntextReference(IIntextReferenceTarget target, int start, int end){
75
        if (start < 0 || end < 0 || start > end || end > text.length()){
76
            throw new IndexOutOfBoundsException("Start and end must be within bounds");
77
        }
78
        IntextReference intextReference = IntextReference.NewInstance(target, this, 0, 0);
79
        String text = this.getText();
80
        setText(text.substring(0, start) + intextReference.toInlineString(text.substring(start,end))
81
            + text.substring(end));
82
        getIntextReferences().add(intextReference);
83
        return intextReference;
72
        return IntextReferenceHelper.addIntextReference(target, this, start, end);
84 73
    }
85 74

  
86 75
	@Override
cdmlib-persistence/src/test/java/eu/etaxonomy/cdm/persistence/dao/hibernate/common/CdmGenericDaoImplTest.java
825 825
    }
826 826

  
827 827
    @Test
828
    public void testReallocateIntextReference() throws MergeException {
828
    public void testReallocateIntextReferenceForReferenceAndLanguageString() throws MergeException {
829 829
        UUID uuidRef1 = UUID.fromString("41743cec-b893-4e8b-b06c-91f9b9ba8fee");
830 830
        UUID uuidRef2 = UUID.fromString("8fd56b43-7cca-4c3b-bb90-7576da81c072");
831 831

  
832
        // CREATE DATA
833

  
832 834
        Reference ref1 = ReferenceFactory.newGeneric();
833 835
        ref1.setTitle("Reference1");
834 836
        ref1.setUuid(uuidRef1);
......
836 838
        ref2.setTitle("Reference2");
837 839
        ref2.setUuid(uuidRef2);
838 840
        referenceDao.save(ref2);
841
        Taxon taxon = Taxon.NewInstance(null, null);
839 842

  
840
        TaxonNameBase<?,?> name1 = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
841
        name1.setTitleCache("BotanicalName1", true);
842
        Taxon taxon = Taxon.NewInstance(name1, null);
843 843
        TaxonDescription desc = TaxonDescription.NewInstance(taxon);
844 844
        Language language = Language.DEFAULT();
845 845
        TextData textData = TextData.NewInstance(Feature.DESCRIPTION(), "And here is a citation" , language, null);
846 846
        LanguageString languageString = textData.getLanguageText(language);
847
        IntextReference intextRef = languageString.addIntextReference(ref1, 4, 8);
848
        String uuidIntextRef = intextRef.getUuid().toString();
847
        IntextReference intextRefRef = languageString.addIntextReference(ref1, 4, 8);
848
        String uuidIntextRefRef = intextRefRef.getUuid().toString();
849 849
        desc.addElement(textData);
850
        Assert.assertEquals("And <cdm:reference cdmId='"+ref1.getUuid()+"' intextId='"+uuidIntextRef+"'>here</cdm:reference> is a citation",
850
        Assert.assertEquals("And <cdm:reference cdmId='"+uuidRef1+"' intextId='"+uuidIntextRefRef+"'>here</cdm:reference> is a citation",
851 851
                    languageString.getText());
852
        taxonDao.save(taxon);
853 852

  
853
        //SAVE AND COMMIT
854
        taxonDao.save(taxon);
854 855
        commitAndStartNewTransaction(null);
855
        DefaultMergeStrategy strategy = DefaultMergeStrategy.NewInstance(Reference.class);
856 856

  
857
        ref1 = referenceDao.findByUuid(ref1.getUuid());
858
        ref2 = referenceDao.findByUuid(ref2.getUuid());
857
        //MERGE
858
        DefaultMergeStrategy strategy = DefaultMergeStrategy.NewInstance(Reference.class);
859
        ref1 = referenceDao.findByUuid(uuidRef1);
860
        ref2 = referenceDao.findByUuid(uuidRef2);
859 861
        cdmGenericDao.merge(ref2, ref1, strategy);
860 862

  
861 863
        taxon = (Taxon)taxonDao.findByUuid(taxon.getUuid());
862 864
        textData = (TextData)taxon.getDescriptions().iterator().next().getElements().iterator().next();
863 865
        languageString = textData.getLanguageText(language);
864
        Assert.assertEquals("And <cdm:reference cdmId='"+ref2.getUuid()+"' intextId='"+uuidIntextRef+"'>here</cdm:reference> is a citation",
866
        Assert.assertEquals("And <cdm:reference cdmId='"+uuidRef2+"' intextId='"+uuidIntextRefRef+"'>here</cdm:reference> is a citation",
865 867
                languageString.getText());
866 868

  
867 869
    }
868 870

  
871
    @Test
872
    public void testReallocateIntextReferenceForNameAndAnnotation() throws MergeException {
873
        UUID uuidPinusAlba = UUID.fromString("52743cec-b893-4e8b-b06c-91f9b9ba8fee");
874
        UUID uuidAbiesAlba = UUID.fromString("6ed56b43-7cca-4c3b-bb90-7576da81c072");
875

  
876
        // CREATE DATA
877
        TaxonNameBase<?,?> pinusAlba = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
878
        pinusAlba.setTitleCache("BotanicalName1", true);
879
        pinusAlba.setUuid(uuidPinusAlba);
880

  
881
        TaxonNameBase<?,?> abiesAlba = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
882
        abiesAlba.setTitleCache("Abies alba", true);
883
        abiesAlba.setUuid(uuidAbiesAlba);
884

  
885
        Taxon taxon = Taxon.NewInstance(null, null);
886

  
887
        Annotation annotation = Annotation.NewDefaultLanguageInstance("My annotation on Abies alba and its habit.");
888
        taxon.addAnnotation(annotation);
889
        IntextReference intextRefName = annotation.addIntextReference(abiesAlba, "My annotation on ", "Abies alba", " and its habit.");
890
        String uuidIntextRefName = intextRefName.getUuid().toString();
891
        Assert.assertEquals("My annotation on <cdm:name cdmId='"+uuidAbiesAlba+"' intextId='"+uuidIntextRefName+"'>Abies alba</cdm:name> and its habit.",
892
                annotation.getText());
893

  
894
        //SAVE AND COMMIT
895
        taxonDao.save(taxon);
896
        nameDao.save(abiesAlba);
897
        nameDao.save(pinusAlba);
898
        commitAndStartNewTransaction(null);
899

  
900
        //MERGE
901
        DefaultMergeStrategy strategy = DefaultMergeStrategy.NewInstance(TaxonNameBase.class);
902
        abiesAlba = nameDao.findByUuid(uuidAbiesAlba);
903
        pinusAlba = nameDao.findByUuid(uuidPinusAlba);
904
        cdmGenericDao.merge(pinusAlba, abiesAlba, strategy);
905

  
906
        taxon = (Taxon)taxonDao.findByUuid(taxon.getUuid());
907
        annotation = taxon.getAnnotations().iterator().next();
908

  
909
        Assert.assertEquals("My annotation on <cdm:name cdmId='"+uuidPinusAlba+"' intextId='"+uuidIntextRefName+"'>Abies alba</cdm:name> and its habit.",
910
                annotation.getText());
911
    }
912

  
869 913
	/**
870 914
	 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmGenericDaoImpl#merge(CdmBase, CdmBase)}.
871 915
	 * @throws MergeException

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)