Project

General

Profile

Revision a1ab6a74

IDa1ab6a7468fefe10f4d00800ae000401114c5974
Parent ebc80cbb
Child 88710aed

Added by Patrick Plitzner almost 2 years ago

ref #7887 Implement term service methods based on DTOs

View differences:

cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/dao/common/IDefinedTermDao.java
27 27
import eu.etaxonomy.cdm.model.location.NamedAreaType;
28 28
import eu.etaxonomy.cdm.model.media.Media;
29 29
import eu.etaxonomy.cdm.persistence.dao.initializer.IBeanInitializer;
30
import eu.etaxonomy.cdm.persistence.dto.TermDto;
30 31
import eu.etaxonomy.cdm.persistence.query.OrderHint;
31 32

  
32 33

  
......
205 206
     */
206 207
    public List<NamedArea> getUuidAndTitleCache(List<TermVocabulary> vocs, Integer limit,
207 208
            String pattern);
209

  
210
    /**
211
     * Returns the parent term of the given term
212
     * @param childTerm the child term
213
     * @return the parent term
214
     */
215
    public TermDto getParentAsDto(TermDto childTerm);
216

  
217
    /**
218
     * Returns all terms that are included in the given parent term resp. a part of the given term.
219
     * @param parentTerm the parent term
220
     * @return a collection of included terms
221
     */
222
    public Collection<TermDto> getIncludesAsDto(TermDto parentTerm);
223

  
224
    /**
225
     * Returns all terms that the given term is a generalization of resp. that are a kind of the given term
226
     * @param parentTerm the parent term
227
     * @return a collection of included terms
228
     */
229
    public Collection<TermDto> getKindOfsAsDto(TermDto parentTerm);
230

  
208 231
}
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/dao/common/ITermVocabularyDao.java
10 10
package eu.etaxonomy.cdm.persistence.dao.common;
11 11

  
12 12

  
13
import java.util.Collection;
13 14
import java.util.List;
14 15
import java.util.Map;
15 16
import java.util.Set;
......
18 19
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
19 20
import eu.etaxonomy.cdm.model.common.TermType;
20 21
import eu.etaxonomy.cdm.model.common.TermVocabulary;
22
import eu.etaxonomy.cdm.persistence.dto.TermDto;
23
import eu.etaxonomy.cdm.persistence.dto.TermVocabularyDto;
21 24
import eu.etaxonomy.cdm.persistence.query.OrderHint;
22 25

  
23 26

  
......
102 105
			Map<UUID, Set<UUID>> uuidsRepsonse,
103 106
			Map<UUID, TermVocabulary<?>> vocabularyResponse);
104 107

  
108
    /**
109
     * Loads all top level terms, i.e. terms that have no parent terms, for the given vocabulary
110
     * @param vocabularyUuid the id of the vocabulary
111
     * @return a collection of top level terms
112
     */
113
    public Collection<TermDto> getTopLevelTerms(UUID vocabularyUuid);
114

  
115
    /**
116
     * Returns term vocabularies that contain terms of a certain {@link TermType} e.g. Feature, Modifier, State.
117
     *
118
     * @param termType the {@link TermType} of the terms in the vocabulary and of the vocabulary
119
     * @return a list of term vocabularies
120
     */
121
    public List<TermVocabularyDto> findVocabularyDtoByTermType(TermType termType);
122

  
105 123
}
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/dao/hibernate/common/DefinedTermDaoImpl.java
63 63
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
64 64
import eu.etaxonomy.cdm.model.view.AuditEvent;
65 65
import eu.etaxonomy.cdm.persistence.dao.common.IDefinedTermDao;
66
import eu.etaxonomy.cdm.persistence.dto.TermDto;
66 67
import eu.etaxonomy.cdm.persistence.query.MatchMode;
67 68
import eu.etaxonomy.cdm.persistence.query.OrderHint;
68 69

  
......
665 666
        return result;
666 667
    }
667 668

  
669
    @Override
670
    public TermDto getParentAsDto(TermDto childTerm) {
671
        String queryString = TermDto.getTermDtoSelect()
672
                + "where a.uuid = :childUuid";
673

  
674
        Query query =  getSession().createQuery(queryString);
675
        query.setParameter("childUuid", childTerm.getUuid());
676

  
677
        @SuppressWarnings("unchecked")
678
        List<Object[]> result = query.list();
679

  
680
        List<TermDto> list = TermDto.termDtoListFrom(result);
681
        if(list.size()==1){
682
            return list.get(0);
683
        }
684
        return null;
685
    }
686

  
687
    @Override
688
    public Collection<TermDto> getIncludesAsDto(
689
            TermDto parentTerm) {
690
        String queryString = TermDto.getTermDtoSelect()
691
                + "where a.partOf.uuid = :parentUuid";
692
        Query query =  getSession().createQuery(queryString);
693
        query.setParameter("parentUuid", parentTerm.getUuid());
694

  
695
        @SuppressWarnings("unchecked")
696
        List<Object[]> result = query.list();
697

  
698
        List<TermDto> list = TermDto.termDtoListFrom(result);
699
        return list;
700
    }
701

  
702
    @Override
703
    public Collection<TermDto> getKindOfsAsDto(
704
            TermDto parentTerm) {
705
        String queryString = TermDto.getTermDtoSelect()
706
                + "where a.kindOf.uuid = :parentUuid";
707
        Query query =  getSession().createQuery(queryString);
708
        query.setParameter("parentUuid", parentTerm.getUuid());
709

  
710
        @SuppressWarnings("unchecked")
711
        List<Object[]> result = query.list();
712

  
713
        List<TermDto> list = TermDto.termDtoListFrom(result);
714
        return list;
715
    }
668 716

  
669 717
}
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/dao/hibernate/common/TermVocabularyDaoImpl.java
11 11

  
12 12
import java.util.ArrayList;
13 13
import java.util.Collection;
14
import java.util.HashMap;
14 15
import java.util.HashSet;
15 16
import java.util.List;
16 17
import java.util.Map;
......
26 27

  
27 28
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
28 29
import eu.etaxonomy.cdm.model.common.OrderedTermVocabulary;
30
import eu.etaxonomy.cdm.model.common.Representation;
29 31
import eu.etaxonomy.cdm.model.common.TermType;
30 32
import eu.etaxonomy.cdm.model.common.TermVocabulary;
31 33
import eu.etaxonomy.cdm.model.view.AuditEvent;
32 34
import eu.etaxonomy.cdm.persistence.dao.common.ITermVocabularyDao;
35
import eu.etaxonomy.cdm.persistence.dto.TermDto;
36
import eu.etaxonomy.cdm.persistence.dto.TermVocabularyDto;
33 37
import eu.etaxonomy.cdm.persistence.query.OrderHint;
34 38

  
35 39
/**
......
106 110
	    	return result;
107 111
		} else {
108 112
			@SuppressWarnings("unchecked")
109
            AuditQuery query = makeAuditQuery((Class)clazz, auditEvent);
113
            AuditQuery query = makeAuditQuery(clazz, auditEvent);
110 114
			query.add(AuditEntity.property("termSourceUri").eq(termSourceUri));
111 115

  
112 116
			@SuppressWarnings("unchecked")
......
242 246
		return;
243 247
	}
244 248

  
249
    @Override
250
    public Collection<TermDto> getTopLevelTerms(UUID vocabularyUuid) {
251
        String queryString = TermDto.getTermDtoSelect()
252
                + "where v.uuid = :vocabularyUuid "
253
                + "and a.partOf is null "
254
                + "and a.kindOf is null";
255
        Query query =  getSession().createQuery(queryString);
256
        query.setParameter("vocabularyUuid", vocabularyUuid);
257

  
258
        @SuppressWarnings("unchecked")
259
        List<Object[]> result = query.list();
260

  
261
        List<TermDto> list = TermDto.termDtoListFrom(result);
262
        return list;
263
    }
264

  
265
    @Override
266
    public List<TermVocabularyDto> findVocabularyDtoByTermType(TermType termType) {
267
        String queryString = ""
268
                + "select v.uuid, r "
269
                + "from TermVocabulary as v LEFT JOIN v.representations AS r "
270
                + "where v.termType = :termType "
271
                ;
272
        Query query =  getSession().createQuery(queryString);
273
        query.setParameter("termType", termType);
274

  
275
        @SuppressWarnings("unchecked")
276
        List<Object[]> result = query.list();
277

  
278
        Map<UUID, TermVocabularyDto> dtoMap = new HashMap<>(result.size());
279
        for (Object[] elements : result) {
280
            UUID uuid = (UUID)elements[0];
281
            if(dtoMap.containsKey(uuid)){
282
                dtoMap.get(uuid).addRepresentation((Representation)elements[1]);
283
            } else {
284
                Set<Representation> representations = new HashSet<>();
285
                if(elements[1] instanceof Representation) {
286
                    representations = new HashSet<Representation>(1);
287
                    representations.add((Representation)elements[1]);
288
                } else {
289
                    representations = (Set<Representation>)elements[1];
290
                }
291
                dtoMap.put(uuid, new TermVocabularyDto(uuid, representations));
292
            }
293
        }
294
        return new ArrayList<>(dtoMap.values());
295
    }
296

  
245 297
}
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/dto/AbstractTermDto.java
1
// $Id$
2
/**
3
* Copyright (C) 2018 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10
package eu.etaxonomy.cdm.persistence.dto;
11

  
12
import java.io.Serializable;
13
import java.net.URI;
14
import java.util.Set;
15
import java.util.UUID;
16

  
17
import eu.etaxonomy.cdm.model.common.Representation;
18
import eu.etaxonomy.cdm.model.common.TermType;
19

  
20
/**
21
 * @author pplitzner
22
 * @date 05.11.2018
23
 *
24
 */
25
public class AbstractTermDto implements Serializable {
26

  
27
    private static final long serialVersionUID = -7160319884811828125L;
28

  
29
    private final UUID uuid;
30
    private URI uri;
31
    private TermType termType;
32
    private final Set<Representation> representations;
33
    private String representation_L10n = null;
34
    private String representation_L10n_abbreviatedLabel = null;
35

  
36
    public AbstractTermDto(UUID uuid, Set<Representation> representations) {
37
        this.representations = representations;
38
        this.uuid = uuid;
39
    }
40

  
41
    /**
42
     *
43
     * @param representation_L10n a blank instance of ITermRepresentation_L10n
44
     *   created by the  default constructor
45
     */
46
    public void localize(ITermRepresentation_L10n representation_L10n) {
47

  
48
        representation_L10n.localize(representations);
49
        if (representation_L10n.getLabel() != null) {
50
            setRepresentation_L10n(representation_L10n.getLabel());
51
        }
52
        if (representation_L10n.getAbbreviatedLabel() != null) {
53
            setRepresentation_L10n_abbreviatedLabel(representation_L10n.getAbbreviatedLabel());
54
        }
55
    }
56

  
57
    public URI getUri() {
58
        return uri;
59
    }
60

  
61
    public void setUri(URI uri) {
62
        this.uri = uri;
63
    }
64

  
65
    public TermType getTermType() {
66
        return termType;
67
    }
68

  
69
    public void setTermType(TermType termType) {
70
        this.termType = termType;
71
    }
72

  
73
    public String getRepresentation_L10n() {
74
        return representation_L10n;
75
    }
76

  
77
    public void setRepresentation_L10n(String representation_L10n) {
78
        this.representation_L10n = representation_L10n;
79
    }
80

  
81
    public void addRepresentation(Representation representation) {
82
        representations.add(representation);
83

  
84
    }
85

  
86
    public String getRepresentation_L10n_abbreviatedLabel() {
87
        return representation_L10n_abbreviatedLabel;
88
    }
89

  
90
    public void setRepresentation_L10n_abbreviatedLabel(String representation_L10n_abbreviatedLabel) {
91
        this.representation_L10n_abbreviatedLabel = representation_L10n_abbreviatedLabel;
92
    }
93

  
94
    public UUID getUuid() {
95
        return uuid;
96
    }
97

  
98
    public Set<Representation> getRepresentations() {
99
        return representations;
100
    }
101

  
102
    @Override
103
    public int hashCode() {
104
        final int prime = 31;
105
        int result = 1;
106
        result = prime * result + ((uuid == null) ? 0 : uuid.hashCode());
107
        return result;
108
    }
109

  
110
    @Override
111
    public boolean equals(Object obj) {
112
        if (this == obj) {
113
            return true;
114
        }
115
        if (obj == null) {
116
            return false;
117
        }
118
        if (getClass() != obj.getClass()) {
119
            return false;
120
        }
121
        AbstractTermDto other = (AbstractTermDto) obj;
122
        if (uuid == null) {
123
            if (other.uuid != null) {
124
                return false;
125
            }
126
        } else if (!uuid.equals(other.uuid)) {
127
            return false;
128
        }
129
        return true;
130
    }
131

  
132
}
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/dto/TermDto.java
8 8
*/
9 9
package eu.etaxonomy.cdm.persistence.dto;
10 10

  
11
import java.util.ArrayList;
12
import java.util.Collection;
13
import java.util.HashMap;
14
import java.util.HashSet;
15
import java.util.List;
16
import java.util.Map;
11 17
import java.util.Set;
12 18
import java.util.UUID;
13 19

  
......
19 25
 * @since Mar 25, 2015
20 26
 *
21 27
 */
22
public class TermDto {
28
public class TermDto extends AbstractTermDto{
23 29

  
30
    private static final long serialVersionUID = 5627308906985438034L;
24 31

  
25
    private final UUID uuid;
32
    private UUID kindOfUuid = null;
26 33
    private UUID partOfUuid = null;
27 34
    private UUID vocabularyUuid = null;
28 35
    private Integer orderIndex = null;
29
    private final Set<Representation> representations;
30
    private String representation_L10n = null;
31
    private String representation_L10n_abbreviatedLabel = null;
36
    private String idInVocabulary = null;
37
    private Collection<TermDto> includes;
38
    private Collection<TermDto> generalizationOf;
32 39

  
33 40
    public TermDto(UUID uuid, Set<Representation> representations, Integer orderIndex) {
34
        this.representations = representations;
35
        this.uuid = uuid;
41
        super(uuid, representations);
36 42
        this.setOrderIndex(orderIndex);
37 43
    }
38 44

  
39 45
    public TermDto(UUID uuid, Set<Representation> representations, UUID partOfUuid, UUID vocabularyUuid, Integer orderIndex) {
40
        this.representations = representations;
41
        this.uuid = uuid;
46
        this(uuid, representations, partOfUuid, null, vocabularyUuid, orderIndex, null);
47
    }
48

  
49
    public TermDto(UUID uuid, Set<Representation> representations, UUID partOfUuid, UUID kindOfUuid, UUID vocabularyUuid, Integer orderIndex, String idInVocabulary) {
50
        super(uuid, representations);
42 51
        this.partOfUuid = partOfUuid;
52
        this.kindOfUuid = kindOfUuid;
43 53
        this.vocabularyUuid = vocabularyUuid;
44 54
        this.orderIndex = orderIndex;
55
        this.idInVocabulary = idInVocabulary;
45 56
    }
46 57

  
47 58
    static public TermDto fromNamedArea(NamedArea namedArea) {
......
49 60
        return dto;
50 61
    }
51 62

  
52
    /**
53
     *
54
     * @param representation_L10n a blank instance of ITermRepresentation_L10n
55
     *   created by the  default constructor
56
     */
57
    public void localize(ITermRepresentation_L10n representation_L10n) {
58

  
59
        representation_L10n.localize(representations);
60
        if (representation_L10n.getLabel() != null) {
61
            setRepresentation_L10n(representation_L10n.getLabel());
62
        }
63
        if (representation_L10n.getAbbreviatedLabel() != null) {
64
            setRepresentation_L10n_abbreviatedLabel(representation_L10n.getAbbreviatedLabel());
65
        }
63
    public UUID getVocabularyUuid() {
64
        return vocabularyUuid;
66 65
    }
67 66

  
68
    /**
69
     * @return the uuid
70
     */
71
    public UUID getUuid() {
72
        return uuid;
67
    public void setVocabularyUuid(UUID vocabularyUuid) {
68
        this.vocabularyUuid = vocabularyUuid;
73 69
    }
74 70

  
75
    /**
76
     * @return the representation_L10n
77
     */
78
    public String getRepresentation_L10n() {
79
        return representation_L10n;
71
    public UUID getPartOfUuid() {
72
        return partOfUuid;
80 73
    }
81 74

  
82
    /**
83
     * @param representation_L10n the representation_L10n to set
84
     */
85
    public void setRepresentation_L10n(String representation_L10n) {
86
        this.representation_L10n = representation_L10n;
75
    public UUID getKindOfUuid() {
76
        return kindOfUuid;
87 77
    }
88 78

  
89
    /**
90
     * @param representation_L10n_abbreviatedLabel the representation_L10n_abbreviatedLabel to set
91
     */
92
    public void setRepresentation_L10n_abbreviatedLabel(String representation_L10n_abbreviatedLabel) {
93
        this.representation_L10n_abbreviatedLabel = representation_L10n_abbreviatedLabel;
79
    public Integer getOrderIndex() {
80
        return orderIndex;
94 81
    }
95 82

  
96
    /**
97
     * @return the representation_L10n_abbreviatedLabel
98
     */
99
    public String getRepresentation_L10n_abbreviatedLabel() {
100
        return representation_L10n_abbreviatedLabel;
83
    public void setOrderIndex(Integer orderIndex) {
84
        this.orderIndex = orderIndex;
101 85
    }
102 86

  
103
    /**
104
     * @return the vocabularyUuid
105
     */
106
    public UUID getVocabularyUuid() {
107
        return vocabularyUuid;
87
    public String getIdInVocabulary() {
88
        return idInVocabulary;
108 89
    }
109 90

  
110
    /**
111
     * @param vocabularyUuid the vocabularyUuid to set
112
     */
113
    public void setVocabularyUuid(UUID vocabularyUuid) {
114
        this.vocabularyUuid = vocabularyUuid;
91
    public void setIdInVocabulary(String idInVocabulary) {
92
        this.idInVocabulary = idInVocabulary;
115 93
    }
116 94

  
117
    /**
118
     * @return the partOfUuid
119
     */
120
    public UUID getPartOfUuid() {
121
        return partOfUuid;
95
    public Collection<TermDto> getIncludes() {
96
        return includes;
122 97
    }
123 98

  
124
    /**
125
     * @param representation
126
     */
127
    public void addRepresentation(Representation representation) {
128
        representations.add(representation);
129

  
99
    public void setIncludes(Collection<TermDto> includes) {
100
        this.includes = includes;
130 101
    }
131 102

  
132
    /**
133
     * @return the orderIndex
134
     */
135
    public Integer getOrderIndex() {
136
        return orderIndex;
103
    public Collection<TermDto> getGeneralizationOf() {
104
        return generalizationOf;
137 105
    }
138 106

  
139
    /**
140
     * @param orderIndex the orderIndex to set
141
     */
142
    public void setOrderIndex(Integer orderIndex) {
143
        this.orderIndex = orderIndex;
107
    public void setGeneralizationOf(Collection<TermDto> generalizationOf) {
108
        this.generalizationOf = generalizationOf;
144 109
    }
145 110

  
111
    public static String getTermDtoSelect(){
112
        return ""
113
                + "select a.uuid, r, p.uuid, k.uuid, v.uuid, a.orderIndex, a.idInVocabulary "
114
                + "from DefinedTermBase as a "
115
                + "LEFT JOIN a.partOf as p "
116
                + "LEFT JOIN a.kindOf as k "
117
                + "LEFT JOIN a.representations AS r "
118
                + "LEFT JOIN a.vocabulary as v ";
119
    }
146 120

  
121
    public static List<TermDto> termDtoListFrom(List<Object[]> results) {
122
        Map<UUID, TermDto> dtoMap = new HashMap<>(results.size());
123
        for (Object[] elements : results) {
124
            UUID uuid = (UUID)elements[0];
125
            if(dtoMap.containsKey(uuid)){
126
                dtoMap.get(uuid).addRepresentation((Representation)elements[1]);
127
            } else {
128
                Set<Representation> representations = new HashSet<>();
129
                if(elements[1] instanceof Representation) {
130
                    representations = new HashSet<Representation>(1);
131
                    representations.add((Representation)elements[1]);
132
                } else {
133
                    representations = (Set<Representation>)elements[1];
134
                }
135
                dtoMap.put(uuid, new TermDto(uuid, representations, (UUID)elements[2], (UUID)elements[3], (UUID)elements[4], (Integer)elements[5], (String)elements[6]));
136
            }
137
        }
138
        return new ArrayList<>(dtoMap.values());
139
    }
147 140

  
148 141
}
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/dto/TermVocabularyDto.java
1
// $Id$
2
/**
3
* Copyright (C) 2018 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10
package eu.etaxonomy.cdm.persistence.dto;
11

  
12
import java.util.HashSet;
13
import java.util.Set;
14
import java.util.UUID;
15

  
16
import eu.etaxonomy.cdm.model.common.Representation;
17

  
18
/**
19
 * @author pplitzner
20
 * @date 05.11.2018
21
 *
22
 */
23
public class TermVocabularyDto extends AbstractTermDto {
24

  
25
    private static final long serialVersionUID = 6053392236860675874L;
26

  
27
    private Set<TermDto> terms;
28

  
29
    public TermVocabularyDto(UUID uuid, Set<Representation> representations) {
30
        super(uuid, representations);
31
        terms = new HashSet<>();
32
    }
33

  
34
    public Set<TermDto> getTerms() {
35
        return terms;
36
    }
37

  
38
    public void addTerm(TermDto term){
39
        terms.add(term);
40
    }
41

  
42
}
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/ITermService.java
18 18
import java.util.Set;
19 19
import java.util.UUID;
20 20

  
21
import eu.etaxonomy.cdm.api.service.TermServiceImpl.TermMovePosition;
21 22
import eu.etaxonomy.cdm.api.service.config.TermDeletionConfigurator;
22 23
import eu.etaxonomy.cdm.api.service.pager.Pager;
23 24
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
24 25
import eu.etaxonomy.cdm.model.common.Language;
25 26
import eu.etaxonomy.cdm.model.common.LanguageString;
26 27
import eu.etaxonomy.cdm.model.common.LanguageStringBase;
28
import eu.etaxonomy.cdm.model.common.OrderedTermBase;
29
import eu.etaxonomy.cdm.model.common.OrderedTermVocabulary;
27 30
import eu.etaxonomy.cdm.model.common.Representation;
28 31
import eu.etaxonomy.cdm.model.common.TermType;
29 32
import eu.etaxonomy.cdm.model.common.TermVocabulary;
......
32 35
import eu.etaxonomy.cdm.model.location.NamedAreaType;
33 36
import eu.etaxonomy.cdm.model.media.Media;
34 37
import eu.etaxonomy.cdm.persistence.dao.initializer.IBeanInitializer;
38
import eu.etaxonomy.cdm.persistence.dto.TermDto;
35 39
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
36 40
import eu.etaxonomy.cdm.persistence.query.OrderHint;
37 41

  
......
192 196
     */
193 197
    List<UuidAndTitleCache<NamedArea>> getUuidAndTitleCache(List<TermVocabulary> vocs, Integer limit, String pattern,
194 198
            Language lang);
199

  
200
    /**
201
     * Returns the parent term of the given term
202
     * @param childTerm the child term
203
     * @return the parent term
204
     */
205
    public TermDto getParentAsDto(TermDto childTerm);
206

  
207
    /**
208
     * Returns all terms that are included in the given parent term resp. a part of the given term.
209
     * @param parentTerm the parent term
210
     * @return a collection of included terms
211
     */
212
    public Collection<TermDto> getIncludesAsDto(TermDto parentTerm);
213

  
214
    /**
215
     * Returns all terms that the given term is a generalization of resp. that are a kind of the given term
216
     * @param parentTerm the parent term
217
     * @return a collection of included terms
218
     */
219
    public Collection<TermDto> getKindOfsAsDto(TermDto parentTerm);
220

  
221
    /**
222
     * Move the given term to the given parent
223
     * @param termDto the {@link TermDto} of the term to move
224
     * @param parentUuid the {@link UUID} of the new parent term
225
     * @param termMovePosition enum to specify the position for {@link OrderedTermBase}s in an {@link OrderedTermVocabulary}
226
     */
227
    public void moveTerm(TermDto termDto, UUID parentUuid, TermMovePosition termMovePosition);
228

  
229
    /**
230
     * Move the given term to the given parent
231
     * @param termDto the {@link TermDto} of the term to move
232
     * @param parentUuid the {@link UUID} of the new parent term
233
     */
234
    public void moveTerm(TermDto termDto, UUID parentUuid);
235

  
236
    /**
237
     * Creates a new term as a child of the given parent.
238
     * @param termType the {@link TermType} of the term to create
239
     * @param parentUuid the {@link UUID} of the parent term
240
     * @param isKindOf if <code>true</code> the term will be added via a
241
     * kindOf relation. Otherwise it will added via a partOf relation
242
     */
243
    public void addNewTerm(TermType termType, UUID parentUuid, boolean isKindOf);
244

  
195 245
}
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/IVocabularyService.java
9 9

  
10 10
package eu.etaxonomy.cdm.api.service;
11 11

  
12
import java.util.Collection;
12 13
import java.util.List;
14
import java.util.UUID;
13 15

  
14 16
import eu.etaxonomy.cdm.api.service.pager.Pager;
15 17
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
16 18
import eu.etaxonomy.cdm.model.common.Language;
17 19
import eu.etaxonomy.cdm.model.common.TermType;
18 20
import eu.etaxonomy.cdm.model.common.TermVocabulary;
21
import eu.etaxonomy.cdm.persistence.dto.TermDto;
22
import eu.etaxonomy.cdm.persistence.dto.TermVocabularyDto;
19 23
import eu.etaxonomy.cdm.persistence.query.OrderHint;
20 24

  
21 25
public interface IVocabularyService extends IIdentifiableEntityService<TermVocabulary> {
......
68 72
	 */
69 73
	public <T extends DefinedTermBase> List<TermVocabulary<T>> findByTermType(TermType termType, List<String> propertyPaths);
70 74

  
75
	/**
76
	 * Loads all top level terms, i.e. terms that have no parent terms, for the given vocabulary
77
	 * @param vocabularyUuid the uuid of the vocabulary
78
	 * @return a collection of top level terms
79
	 */
80
	public Collection<TermDto> getTopLevelTerms(UUID vocabularyUuid);
81

  
82
	/**
83
	 * Initializes the complete term hierarchy consisting of {@link TermDto}s
84
	 * for the given vocabulary
85
	 * @param vocabularyUuid the UUID of the {@link TermVocabulary}
86
	 * @return a the top level elements for this vocabulary
87
	 */
88

  
89
	public Collection<TermDto> getCompleteTermHierarchy(UUID vocabularyUuid);
90
    /**
91
     * Returns term vocabularies that contain terms of a certain {@link TermType} e.g. Feature, Modifier, State.
92
     *
93
     * @param termType the {@link TermType} of the terms in the vocabulary and of the vocabulary
94
     * @return a list of term vocabularies
95
     */
96
    public List<TermVocabularyDto> findVocabularyDtoByTermType(TermType termType);
97

  
98
    /**
99
     * Creates a new term as a direct child of the given vocabulary.
100
     * @param termType the {@link TermType} of the term to create
101
     * @param vocabularyUUID the {@link UUID} of the vocabulary
102
     * kindOf relation. Otherwise it will added via a partOf relation
103
     */
104
    public void addNewTerm(TermType termType, UUID vocabularyUUID);
105

  
71 106
}
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/TermServiceImpl.java
36 36
import eu.etaxonomy.cdm.api.service.pager.Pager;
37 37
import eu.etaxonomy.cdm.api.service.pager.impl.DefaultPagerImpl;
38 38
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
39
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
39 40
import eu.etaxonomy.cdm.model.common.CdmBase;
40 41
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
41 42
import eu.etaxonomy.cdm.model.common.Language;
42 43
import eu.etaxonomy.cdm.model.common.LanguageString;
43 44
import eu.etaxonomy.cdm.model.common.LanguageStringBase;
45
import eu.etaxonomy.cdm.model.common.OrderedTermBase;
46
import eu.etaxonomy.cdm.model.common.OrderedTermVocabulary;
44 47
import eu.etaxonomy.cdm.model.common.Representation;
45 48
import eu.etaxonomy.cdm.model.common.TermBase;
46 49
import eu.etaxonomy.cdm.model.common.TermType;
......
53 56
import eu.etaxonomy.cdm.persistence.dao.common.ILanguageStringBaseDao;
54 57
import eu.etaxonomy.cdm.persistence.dao.common.ILanguageStringDao;
55 58
import eu.etaxonomy.cdm.persistence.dao.common.IRepresentationDao;
59
import eu.etaxonomy.cdm.persistence.dto.TermDto;
56 60
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
57 61
import eu.etaxonomy.cdm.persistence.query.OrderHint;
58 62
import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy;
......
66 70
	private ILanguageStringDao languageStringDao;
67 71

  
68 72
	@Autowired
73
	private IVocabularyService vocabularyService;
74

  
75
	@Autowired
69 76
	@Qualifier("langStrBaseDao")
70 77
	private ILanguageStringBaseDao languageStringBaseDao;
71 78
	private IRepresentationDao representationDao;
......
460 467
        return result;
461 468
    }
462 469

  
470
    @Override
471
    public TermDto getParentAsDto(TermDto childTerm) {
472
        return dao.getParentAsDto(childTerm);
473
    }
474

  
475
    @Override
476
    public Collection<TermDto> getIncludesAsDto(
477
            TermDto parentTerm) {
478
        return dao.getIncludesAsDto(parentTerm);
479
    }
480

  
481
    @Override
482
    public Collection<TermDto> getKindOfsAsDto(
483
            TermDto parentTerm) {
484
        return dao.getKindOfsAsDto(parentTerm);
485
    }
486

  
487
    @Transactional(readOnly = false)
488
    @Override
489
    public void moveTerm(TermDto termDto, UUID parentUUID) {
490
        moveTerm(termDto, parentUUID, null);
491
    }
492

  
493
    @SuppressWarnings({ "rawtypes", "unchecked" })
494
    @Transactional(readOnly = false)
495
    @Override
496
    public void moveTerm(TermDto termDto, UUID parentUuid, TermMovePosition termMovePosition) {
497
        boolean isKindOf = termDto.getKindOfUuid()!=null && termDto.getKindOfUuid().equals(parentUuid);
498
        TermVocabulary vocabulary = HibernateProxyHelper.deproxy(vocabularyService.load(termDto.getVocabularyUuid()));
499
        DefinedTermBase parent = HibernateProxyHelper.deproxy(dao.load(parentUuid));
500
        if(parent==null){
501
            //new parent is a vocabulary
502
            TermVocabulary parentVocabulary = HibernateProxyHelper.deproxy(vocabularyService.load(parentUuid));
503
            DefinedTermBase term = HibernateProxyHelper.deproxy(dao.load(termDto.getUuid()));
504
            if(parentVocabulary!=null){
505
                term.setKindOf(null);
506
                term.setPartOf(null);
507

  
508
                vocabulary.removeTerm(term);
509
                parentVocabulary.addTerm(term);
510
            }
511
            vocabularyService.saveOrUpdate(parentVocabulary);
512
        }
513
        else {
514
            DefinedTermBase term = HibernateProxyHelper.deproxy(dao.load(termDto.getUuid()));
515
            //new parent is a term
516
            if(parent.isInstanceOf(OrderedTermBase.class)
517
                    && term.isInstanceOf(OrderedTermBase.class)
518
                    && termMovePosition!=null
519
                    && HibernateProxyHelper.deproxy(parent, OrderedTermBase.class).getVocabulary().isInstanceOf(OrderedTermVocabulary.class)) {
520
                //new parent is an ordered term
521
                OrderedTermBase orderedTerm = HibernateProxyHelper.deproxy(term, OrderedTermBase.class);
522
                OrderedTermBase targetOrderedDefinedTerm = HibernateProxyHelper.deproxy(parent, OrderedTermBase.class);
523
                OrderedTermVocabulary otVoc = HibernateProxyHelper.deproxy(targetOrderedDefinedTerm.getVocabulary(), OrderedTermVocabulary.class);
524
                if(termMovePosition.equals(TermMovePosition.BEFORE)) {
525
                    orderedTerm.getVocabulary().removeTerm(orderedTerm);
526
                    otVoc.addTermAbove(orderedTerm, targetOrderedDefinedTerm);
527
                    if (targetOrderedDefinedTerm.getPartOf() != null){
528
                        targetOrderedDefinedTerm.getPartOf().addIncludes(orderedTerm);
529
                    }
530
                }
531
                else if(termMovePosition.equals(TermMovePosition.AFTER)) {
532
                    orderedTerm.getVocabulary().removeTerm(orderedTerm);
533
                    otVoc.addTermBelow(orderedTerm, targetOrderedDefinedTerm);
534
                    if (targetOrderedDefinedTerm.getPartOf() != null){
535
                        targetOrderedDefinedTerm.getPartOf().addIncludes(orderedTerm);
536
                    }
537
                }
538
                else if(termMovePosition.equals(TermMovePosition.ON)) {
539
                    orderedTerm.getVocabulary().removeTerm(orderedTerm);
540
                    targetOrderedDefinedTerm.addIncludes(orderedTerm);
541
                    targetOrderedDefinedTerm.getVocabulary().addTerm(orderedTerm);
542
                }
543
            }
544
            else{
545
                vocabulary.removeTerm(term);
546
                if(isKindOf){
547
                    parent.addGeneralizationOf(term);
548
                }
549
                else{
550
                    parent.addIncludes(term);
551
                }
552
                parent.getVocabulary().addTerm(term);
553
            }
554
            vocabularyService.saveOrUpdate(parent.getVocabulary());
555
        }
556
    }
557

  
558
    @SuppressWarnings({ "rawtypes", "unchecked" })
559
    @Transactional(readOnly = false)
560
    @Override
561
    public void addNewTerm(TermType termType, UUID parentUUID, boolean isKindOf) {
562
        DefinedTermBase term = termType.getEmptyDefinedTermBase();
563
        dao.save(term);
564
        DefinedTermBase parent = dao.load(parentUUID);
565
        if(isKindOf){
566
            parent.addGeneralizationOf(term);
567
        }
568
        else{
569
            parent.addIncludes(term);
570
        }
571
        parent.getVocabulary().addTerm(term);
572
        dao.saveOrUpdate(parent);
573
    }
574

  
575
    public enum TermMovePosition{
576
        BEFORE,
577
        AFTER,
578
        ON
579
    }
463 580

  
464 581
}
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/VocabularyServiceImpl.java
10 10
package eu.etaxonomy.cdm.api.service;
11 11

  
12 12
import java.util.ArrayList;
13
import java.util.Collection;
13 14
import java.util.List;
14 15
import java.util.UUID;
15 16

  
......
25 26
import eu.etaxonomy.cdm.model.common.TermType;
26 27
import eu.etaxonomy.cdm.model.common.TermVocabulary;
27 28
import eu.etaxonomy.cdm.persistence.dao.common.ITermVocabularyDao;
29
import eu.etaxonomy.cdm.persistence.dto.TermDto;
30
import eu.etaxonomy.cdm.persistence.dto.TermVocabularyDto;
28 31
import eu.etaxonomy.cdm.persistence.query.OrderHint;
29 32
import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy;
30 33

  
......
32 35
@Transactional(readOnly = true)
33 36
public class VocabularyServiceImpl extends IdentifiableServiceBase<TermVocabulary,ITermVocabularyDao>  implements IVocabularyService {
34 37

  
38
    @Autowired
39
    private ITermService termService;
40

  
35 41
	@Override
36 42
    @Autowired
37 43
	protected void setDao(ITermVocabularyDao dao) {
......
85 91
		return new DefaultPagerImpl<>(pageNumber, numberOfResults, pageSize, results);
86 92
	}
87 93

  
94

  
95
    @Override
96
    public Collection<TermDto> getTopLevelTerms(UUID vocabularyUuid) {
97
        return dao.getTopLevelTerms(vocabularyUuid);
98
    }
99

  
100
    @Override
101
    public Collection<TermDto> getCompleteTermHierarchy(UUID vocabularyUuid) {
102
        Collection<TermDto> topLevelTerms = dao.getTopLevelTerms(vocabularyUuid);
103
        for (TermDto termDto : topLevelTerms) {
104
            initializeIncludes(termDto);
105
            initializeGeneralizationOf(termDto);
106
        }
107
        return topLevelTerms;
108
    }
109

  
110
    private void initializeGeneralizationOf(TermDto parentTerm){
111
        Collection<TermDto> generalizationOf = termService.getKindOfsAsDto(parentTerm);
112
        parentTerm.setGeneralizationOf(generalizationOf);
113
        generalizationOf.forEach(include->initializeGeneralizationOf(include));
114
    }
115

  
116
    private void initializeIncludes(TermDto parentTerm){
117
        Collection<TermDto> includes = termService.getIncludesAsDto(parentTerm);
118
        parentTerm.setIncludes(includes);
119
        includes.forEach(include->initializeIncludes(include));
120
    }
121

  
122
    @Override
123
    public List<TermVocabularyDto> findVocabularyDtoByTermType(TermType termType) {
124
        return dao.findVocabularyDtoByTermType(termType);
125
    }
126

  
127
    @Transactional(readOnly = false)
128
    @Override
129
    public void addNewTerm(TermType termType, UUID vocabularyUUID) {
130
        DefinedTermBase term = termType.getEmptyDefinedTermBase();
131
        termService.save(term);
132
        TermVocabulary vocabulary = dao.load(vocabularyUUID);
133
        vocabulary.addTerm(term);
134
        dao.saveOrUpdate(vocabulary);
135
    }
88 136
}

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)