Project

General

Profile

Revision 2d4cb2bf

ID2d4cb2bf735ed4759b08006ca4f131dc8f3cadf9
Parent 74bdd593
Child 0ebf52c1

Added by Patrick Plitzner over 1 year ago

ref #7875 Implement term service based on TermDto

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.UuidAndTitleCache;
30
import eu.etaxonomy.cdm.persistence.dto.TermDto;
31 31
import eu.etaxonomy.cdm.persistence.query.OrderHint;
32 32

  
33 33

  
......
212 212
     * @param childTerm the child term
213 213
     * @return the parent term
214 214
     */
215
    public UuidAndTitleCache<DefinedTermBase> getParentUuidAndTitleCache(UuidAndTitleCache<DefinedTermBase> childTerm);
215
    public TermDto getParentUuidAndTitleCache(TermDto childTerm);
216 216

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

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

  
231 231
}
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/dao/common/ITermVocabularyDao.java
19 19
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
20 20
import eu.etaxonomy.cdm.model.common.TermType;
21 21
import eu.etaxonomy.cdm.model.common.TermVocabulary;
22
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
22
import eu.etaxonomy.cdm.persistence.dto.TermDto;
23 23
import eu.etaxonomy.cdm.persistence.query.OrderHint;
24 24

  
25 25

  
......
109 109
     * @param vocabularyId the id of the vocabulary
110 110
     * @return a collection of top level terms
111 111
     */
112
    public Collection<UuidAndTitleCache<DefinedTermBase>> getTopLevelTerms(int vocabularyId);
112
    public Collection<TermDto> getTopLevelTerms(int vocabularyId);
113 113

  
114 114
}
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/dao/hibernate/common/DefinedTermDaoImpl.java
13 13
import java.util.ArrayList;
14 14
import java.util.Collection;
15 15
import java.util.Enumeration;
16
import java.util.HashMap;
17
import java.util.HashSet;
16 18
import java.util.Iterator;
17 19
import java.util.List;
18 20
import java.util.Locale;
21
import java.util.Map;
19 22
import java.util.Set;
20 23
import java.util.UUID;
21 24

  
......
38 41
import eu.etaxonomy.cdm.model.common.ExtensionType;
39 42
import eu.etaxonomy.cdm.model.common.Language;
40 43
import eu.etaxonomy.cdm.model.common.MarkerType;
44
import eu.etaxonomy.cdm.model.common.Representation;
41 45
import eu.etaxonomy.cdm.model.common.TermType;
42 46
import eu.etaxonomy.cdm.model.common.TermVocabulary;
43 47
import eu.etaxonomy.cdm.model.description.MeasurementUnit;
......
63 67
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
64 68
import eu.etaxonomy.cdm.model.view.AuditEvent;
65 69
import eu.etaxonomy.cdm.persistence.dao.common.IDefinedTermDao;
66
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
70
import eu.etaxonomy.cdm.persistence.dto.TermDto;
67 71
import eu.etaxonomy.cdm.persistence.query.MatchMode;
68 72
import eu.etaxonomy.cdm.persistence.query.OrderHint;
69 73

  
......
667 671
    }
668 672

  
669 673
    @Override
670
    public UuidAndTitleCache<DefinedTermBase> getParentUuidAndTitleCache(UuidAndTitleCache<DefinedTermBase> childTerm) {
671
        String parentQueryString = ""
672
                + "select distinct t1.uuid, t1.id, t1.titleCache "
673
                + "from DefinedTermBase t1, DefinedTermBase t2 "
674
                + "where t1.id = t2.partOf.id "
675
                + "and t2.id = :childTerm";
676
        Query query =  getSession().createQuery(parentQueryString);
677
        query.setParameter("childTerm", childTerm.getId());
678
        List<UuidAndTitleCache<DefinedTermBase>> list = new ArrayList<>();
674
    public TermDto getParentUuidAndTitleCache(TermDto childTerm) {
675

  
676
        String queryString = "select p.uuid, r, p.partOf.uuid, v.uuid, p.orderIndex "
677
                + "from DefinedTermBase as a LEFT JOIN a.vocabulary as v LEFT JOIN a.partOf as p LEFT JOIN p.representations AS r "
678
                + "where a.uuid = :childUuid";
679

  
680
        Query query =  getSession().createQuery(queryString);
681
        query.setParameter("childUuid", childTerm.getUuid());
679 682

  
680 683
        @SuppressWarnings("unchecked")
681 684
        List<Object[]> result = query.list();
682 685

  
683
        if(result.size()==1){
684
            Object[] object = result.get(0);
685
            UUID uuid = (UUID) object[0];
686
            Integer id = (Integer) object[1];
687
            String titleCache = (String) object[2];
688
            return new UuidAndTitleCache<>(uuid,id, titleCache);
686
        List<TermDto> list = termDtoListFrom(result);
687
        if(list.size()==1){
688
            return list.get(0);
689 689
        }
690 690
        return null;
691 691
    }
692 692

  
693 693
    @Override
694
    public List<UuidAndTitleCache<DefinedTermBase>> getIncludesAsUuidAndTitleCache(
695
            UuidAndTitleCache<DefinedTermBase> parentTerm) {
696
        String queryString = ""
697
                + "select distinct t2.uuid, t2.id, t2.titleCache "
698
                + "from DefinedTermBase t1, DefinedTermBase t2 "
699
                + "where t2.partOf.id = :parentId";
694
    public Collection<TermDto> getIncludesAsUuidAndTitleCache(
695
            TermDto parentTerm) {
696
        String queryString = "select a.uuid, r, p.uuid, v.uuid, a.orderIndex "
697
                + "from DefinedTermBase as a LEFT JOIN a.partOf as p LEFT JOIN a.representations AS r LEFT JOIN a.vocabulary as v "
698
                + "where a.partOf.uuid = :parentUuid";
700 699
        Query query =  getSession().createQuery(queryString);
701
        query.setParameter("parentId", parentTerm.getId());
700
        query.setParameter("parentUuid", parentTerm.getUuid());
702 701

  
703 702
        @SuppressWarnings("unchecked")
704 703
        List<Object[]> result = query.list();
705 704

  
706
        List<UuidAndTitleCache<DefinedTermBase>> list = generateUuidAndTitleCache(result);
705
        List<TermDto> list = termDtoListFrom(result);
707 706
        return list;
708 707
    }
709 708

  
710 709
    @Override
711
    public List<UuidAndTitleCache<DefinedTermBase>> getKindOfsAsUuidAndTitleCache(
712
            UuidAndTitleCache<DefinedTermBase> parentTerm) {
713
        String queryString = ""
714
                + "select distinct t2.uuid, t2.id, t2.titleCache "
715
                + "from DefinedTermBase t1, DefinedTermBase t2 "
716
                + "where t2.kindOf.id = :parentId";
710
    public Collection<TermDto> getKindOfsAsUuidAndTitleCache(
711
            TermDto parentTerm) {
712
        String queryString = "select t.uuid, r, p.uuid, v.uuid, t.orderIndex "
713
                + "from DefinedTermBase as t LEFT JOIN t.partOf as p LEFT JOIN t.representations AS r LEFT JOIN t.vocabulary as v "
714
                + "where t.kindOf.uuid = :parentUuid";
717 715
        Query query =  getSession().createQuery(queryString);
718
        query.setParameter("parentId", parentTerm.getId());
716
        query.setParameter("parentUuid", parentTerm.getUuid());
719 717

  
720 718
        @SuppressWarnings("unchecked")
721 719
        List<Object[]> result = query.list();
722 720

  
723
        List<UuidAndTitleCache<DefinedTermBase>> list = generateUuidAndTitleCache(result);
721
        List<TermDto> list = termDtoListFrom(result);
724 722
        return list;
725 723
    }
726 724

  
727
    private List<UuidAndTitleCache<DefinedTermBase>> generateUuidAndTitleCache(List<Object[]> result){
728
        List<UuidAndTitleCache<DefinedTermBase>> list = new ArrayList<>();
729
        for(Object[] object : result){
730
            UUID uuid = (UUID) object[0];
731
            Integer id = (Integer) object[1];
732
            String titleCache = (String) object[2];
733
            if(titleCache!=null){
734
                list.add(new UuidAndTitleCache<>(uuid,id, titleCache));
725
    private List<TermDto> termDtoListFrom(List<Object[]> results) {
726
        Map<UUID, TermDto> dtoMap = new HashMap<>(results.size());
727
        for (Object[] elements : results) {
728
            UUID uuid = (UUID)elements[0];
729
            if(dtoMap.containsKey(uuid)){
730
                dtoMap.get(uuid).addRepresentation((Representation)elements[1]);
731
            } else {
732
                Set<Representation> representations;
733
                if(elements[1] instanceof Representation) {
734
                    representations = new HashSet<Representation>(1);
735
                    representations.add((Representation)elements[1]);
736
                } else {
737
                    representations = (Set<Representation>)elements[1];
738
                }
739
                dtoMap.put(uuid, new TermDto(uuid, representations, (UUID)elements[2], (UUID)elements[3], (Integer)elements[4]));
735 740
            }
736 741
        }
737
        return list;
742
        return new ArrayList<>(dtoMap.values());
738 743
    }
739 744

  
740

  
741 745
}
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;
33
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
35
import eu.etaxonomy.cdm.persistence.dto.TermDto;
34 36
import eu.etaxonomy.cdm.persistence.query.OrderHint;
35 37

  
36 38
/**
......
244 246
	}
245 247

  
246 248
    @Override
247
    public Collection<UuidAndTitleCache<DefinedTermBase>> getTopLevelTerms(int vocabularyId) {
249
    public Collection<TermDto> getTopLevelTerms(int vocabularyId) {
248 250
        String queryString = ""
249
                + "select t.uuid, t.id, t.titleCache, t.partOf.id, t.kindOf.id "
250
                + "from TermVocabulary v, DefinedTermBase t "
251
                + "select a.uuid, r, p.uuid, v.uuid, a.orderIndex "
252
                + "from DefinedTermBase as a LEFT JOIN a.partOf as p LEFT JOIN a.representations AS r LEFT JOIN a.vocabulary as v "
251 253
                + "where v.id = :vocabularyId "
252
                + "and t.vocabulary.id = v.id "
253
                + "and t.partOf is null "
254
                + "and t.kindOf is null";
254
                + "and a.partOf is null "
255
                + "and a.kindOf is null";
255 256
        Query query =  getSession().createQuery(queryString);
256 257
        query.setParameter("vocabularyId", vocabularyId);
257
        List<UuidAndTitleCache<DefinedTermBase>> list = new ArrayList<>();
258 258

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

  
262
        for(Object[] object : result){
263
            UUID uuid = (UUID) object[0];
264
            Integer id = (Integer) object[1];
265
            String taxonTitleCache = (String) object[2];
266
            String classificationTitleCache = (String) object[3];
267
            if(taxonTitleCache!=null){
268
                list.add(new UuidAndTitleCache<>(uuid,id, taxonTitleCache));
269
            }
270
            else{
271
                list.add(new UuidAndTitleCache<>(uuid,id, classificationTitleCache));
262
        Map<UUID, TermDto> dtoMap = new HashMap<>(result.size());
263
        for (Object[] elements : result) {
264
            UUID uuid = (UUID)elements[0];
265
            if(dtoMap.containsKey(uuid)){
266
                dtoMap.get(uuid).addRepresentation((Representation)elements[1]);
267
            } else {
268
                Set<Representation> representations;
269
                if(elements[1] instanceof Representation) {
270
                    representations = new HashSet<Representation>(1);
271
                    representations.add((Representation)elements[1]);
272
                } else {
273
                    representations = (Set<Representation>)elements[1];
274
                }
275
                dtoMap.put(uuid, new TermDto(uuid, representations, (UUID)elements[2], (UUID)elements[3], (Integer)elements[4]));
272 276
            }
273 277
        }
274
        return list;
278
        return new ArrayList<>(dtoMap.values());
275 279
    }
276 280

  
277 281
}
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.io.Serializable;
11 12
import java.util.Set;
12 13
import java.util.UUID;
13 14

  
......
19 20
 * @since Mar 25, 2015
20 21
 *
21 22
 */
22
public class TermDto {
23

  
23
public class TermDto implements Serializable{
24 24

  
25
    private static final long serialVersionUID = 5627308906985438034L;
25 26
    private final UUID uuid;
26 27
    private UUID partOfUuid = null;
27 28
    private UUID vocabularyUuid = null;
......
143 144
        this.orderIndex = orderIndex;
144 145
    }
145 146

  
147
    @Override
148
    public int hashCode() {
149
        final int prime = 31;
150
        int result = 1;
151
        result = prime * result + ((uuid == null) ? 0 : uuid.hashCode());
152
        return result;
153
    }
146 154

  
155
    @Override
156
    public boolean equals(Object obj) {
157
        if (this == obj) {
158
            return true;
159
        }
160
        if (obj == null) {
161
            return false;
162
        }
163
        if (getClass() != obj.getClass()) {
164
            return false;
165
        }
166
        TermDto other = (TermDto) obj;
167
        if (uuid == null) {
168
            if (other.uuid != null) {
169
                return false;
170
            }
171
        } else if (!uuid.equals(other.uuid)) {
172
            return false;
173
        }
174
        return true;
175
    }
147 176

  
148 177
}
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/ITermService.java
32 32
import eu.etaxonomy.cdm.model.location.NamedAreaType;
33 33
import eu.etaxonomy.cdm.model.media.Media;
34 34
import eu.etaxonomy.cdm.persistence.dao.initializer.IBeanInitializer;
35
import eu.etaxonomy.cdm.persistence.dto.TermDto;
35 36
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
36 37
import eu.etaxonomy.cdm.persistence.query.OrderHint;
37 38

  
......
198 199
     * @param childTerm the child term
199 200
     * @return the parent term
200 201
     */
201
    public UuidAndTitleCache<DefinedTermBase> getParentUuidAndTitleCache(UuidAndTitleCache<DefinedTermBase> childTerm);
202
    public TermDto getParentUuidAndTitleCache(TermDto childTerm);
202 203

  
203 204
    /**
204 205
     * Returns all terms that are included in the given parent term resp. a part of the given term.
205 206
     * @param parentTerm the parent term
206 207
     * @return a collection of included terms
207 208
     */
208
    public Collection<UuidAndTitleCache<DefinedTermBase>> getIncludesAsUuidAndTitleCache(UuidAndTitleCache<DefinedTermBase> parentTerm);
209
    public Collection<TermDto> getIncludesAsUuidAndTitleCache(TermDto parentTerm);
209 210

  
210 211
    /**
211 212
     * Returns all terms that the given term is a generalization of resp. that are a kind of the given term
212 213
     * @param parentTerm the parent term
213 214
     * @return a collection of included terms
214 215
     */
215
    public Collection<UuidAndTitleCache<DefinedTermBase>> getKindOfsAsUuidAndTitleCache(UuidAndTitleCache<DefinedTermBase> parentTerm);
216
    public Collection<TermDto> getKindOfsAsUuidAndTitleCache(TermDto parentTerm);
216 217

  
217 218
}
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/IVocabularyService.java
17 17
import eu.etaxonomy.cdm.model.common.Language;
18 18
import eu.etaxonomy.cdm.model.common.TermType;
19 19
import eu.etaxonomy.cdm.model.common.TermVocabulary;
20
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
20
import eu.etaxonomy.cdm.persistence.dto.TermDto;
21 21
import eu.etaxonomy.cdm.persistence.query.OrderHint;
22 22

  
23 23
public interface IVocabularyService extends IIdentifiableEntityService<TermVocabulary> {
......
75 75
	 * @param vocabularyId the id of the vocabulary
76 76
	 * @return a collection of top level terms
77 77
	 */
78
	public Collection<UuidAndTitleCache<DefinedTermBase>> getTopLevelTerms(int vocabularyId);
78
	public Collection<TermDto> getTopLevelTerms(int vocabularyId);
79 79

  
80 80
}
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/TermServiceImpl.java
53 53
import eu.etaxonomy.cdm.persistence.dao.common.ILanguageStringBaseDao;
54 54
import eu.etaxonomy.cdm.persistence.dao.common.ILanguageStringDao;
55 55
import eu.etaxonomy.cdm.persistence.dao.common.IRepresentationDao;
56
import eu.etaxonomy.cdm.persistence.dto.TermDto;
56 57
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
57 58
import eu.etaxonomy.cdm.persistence.query.OrderHint;
58 59
import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy;
......
461 462
    }
462 463

  
463 464
    @Override
464
    public UuidAndTitleCache<DefinedTermBase> getParentUuidAndTitleCache(UuidAndTitleCache<DefinedTermBase> childTerm) {
465
    public TermDto getParentUuidAndTitleCache(TermDto childTerm) {
465 466
        return dao.getParentUuidAndTitleCache(childTerm);
466 467
    }
467 468

  
468 469
    @Override
469
    public Collection<UuidAndTitleCache<DefinedTermBase>> getIncludesAsUuidAndTitleCache(
470
            UuidAndTitleCache<DefinedTermBase> parentTerm) {
470
    public Collection<TermDto> getIncludesAsUuidAndTitleCache(
471
            TermDto parentTerm) {
471 472
        return dao.getIncludesAsUuidAndTitleCache(parentTerm);
472 473
    }
473 474

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

  
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/VocabularyServiceImpl.java
26 26
import eu.etaxonomy.cdm.model.common.TermType;
27 27
import eu.etaxonomy.cdm.model.common.TermVocabulary;
28 28
import eu.etaxonomy.cdm.persistence.dao.common.ITermVocabularyDao;
29
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
29
import eu.etaxonomy.cdm.persistence.dto.TermDto;
30 30
import eu.etaxonomy.cdm.persistence.query.OrderHint;
31 31
import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy;
32 32

  
......
89 89

  
90 90

  
91 91
    @Override
92
    public Collection<UuidAndTitleCache<DefinedTermBase>> getTopLevelTerms(int vocabularyId) {
92
    public Collection<TermDto> getTopLevelTerms(int vocabularyId) {
93 93
        return dao.getTopLevelTerms(vocabularyId);
94 94
    }
95 95

  

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)