Project

General

Profile

Revision a089c5d4

IDa089c5d4b6cc6572d8d5d868c9609b3344d85c3c
Parent 78614c1a
Child 1fc33b39

Added by Andreas Müller almost 2 years ago

ref #6682 add types and deduplicateMisapplications parameter to web service

View differences:

cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/dao/common/ICdmEntityDao.java
15 15
import java.util.Set;
16 16
import java.util.UUID;
17 17

  
18
import org.hibernate.Criteria;
19 18
import org.hibernate.LockOptions;
20 19
import org.hibernate.Session;
21 20
import org.hibernate.criterion.Criterion;
22
import org.hibernate.criterion.Projections;
23
import org.hibernate.criterion.Restrictions;
24 21
import org.springframework.dao.DataAccessException;
25 22

  
26 23
import eu.etaxonomy.cdm.model.common.CdmBase;
......
358 355
     */
359 356
    public List<T> list(Collection<UUID> uuids, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) throws DataAccessException;
360 357

  
358

  
359
    /**
360
     * @param clazz
361
     * @param uuids
362
     * @param pageSize
363
     * @param pageNumber
364
     * @param orderHints
365
     * @param propertyPaths
366
     * @return
367
     * @throws DataAccessException
368
     */
369
    public <S extends T> List<S> list(Class<S> clazz, Collection<UUID> uuids, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) throws DataAccessException;
370

  
371

  
372

  
361 373
    /**
362 374
     * Finds the cdm entity specified by the <code>uuid</code> parameter and
363 375
     * initializes all its *ToOne relations.
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/dao/hibernate/common/CdmEntityDaoBase.java
469 469
            return new ArrayList<T>(0);
470 470
        }
471 471

  
472
        Criteria criteria = prepareList(ids, null, null, null, "id");
472
        Criteria criteria = prepareList(null, ids, null, null, null, "id");
473 473

  
474 474
        if (logger.isDebugEnabled()) {
475 475
            logger.debug(criteria.toString());
......
489 489
            return new ArrayList<>();
490 490
        }
491 491

  
492
        Criteria criteria = prepareList(uuids, pageSize, pageNumber, orderHints, "uuid");
492
        Criteria criteria = prepareList(null, uuids, pageSize, pageNumber, orderHints, "uuid");
493 493
        @SuppressWarnings("unchecked")
494 494
        List<T> result = criteria.list();
495 495
        defaultBeanInitializer.initializeAll(result, propertyPaths);
496 496
        return result;
497 497
    }
498 498

  
499
    @Override
500
    public <S extends T> List<S> list(Class<S> clazz, Collection<UUID> uuids, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints,
501
            List<String> propertyPaths) throws DataAccessException {
502

  
503
        if (uuids == null || uuids.isEmpty()) {
504
            return new ArrayList<>();
505
        }
506

  
507
        Criteria criteria = prepareList(clazz, uuids, pageSize, pageNumber, orderHints, "uuid");
508
        @SuppressWarnings("unchecked")
509
        List<S> result = criteria.list();
510
        defaultBeanInitializer.initializeAll(result, propertyPaths);
511
        return result;
512
    }
513

  
499 514
    /**
500 515
     * {@inheritDoc}
501 516
     */
......
681 696
     * @param propertyName
682 697
     * @return
683 698
     */
684
    private Criteria prepareList(Collection<?> uuids, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints,
699
    private Criteria prepareList(Class<? extends T> clazz, Collection<?> uuids, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints,
685 700
            String propertyName) {
686
        Criteria criteria = getSession().createCriteria(type);
701
        if (clazz == null){
702
            clazz = type;
703
        }
704
        Criteria criteria = getSession().createCriteria(clazz);
687 705
        criteria.add(Restrictions.in(propertyName, uuids));
688 706

  
689 707
        if (pageSize != null) {
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/dao/hibernate/taxon/TaxonDaoHibernateImpl.java
942 942
        return result;
943 943
    }
944 944

  
945

  
946 945
    @Override
947 946
    public long countTaxonRelationships(Taxon taxon, TaxonRelationshipType type,
948 947
            boolean includeUnpublished, Direction direction) {
948
        Set<TaxonRelationshipType> types = null;
949
        if (type != null){
950
            types = new HashSet<>();
951
            types.add(type);
952
        }
953
        return countTaxonRelationships(taxon, types, includeUnpublished, direction);
954
    }
955

  
956
    @Override
957
    public long countTaxonRelationships(Taxon taxon, Set<TaxonRelationshipType> types,
958
            boolean includeUnpublished, Direction direction) {
949 959
        AuditEvent auditEvent = getAuditEventFromContext();
950 960
        if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
951 961

  
952
            String queryString = prepareTaxonRelationshipQuery(type, includeUnpublished, direction, true);
962
            String queryString = prepareTaxonRelationshipQuery(types, includeUnpublished, direction, true);
953 963
            Query query = getSession().createQuery(queryString);
954 964
            query.setParameter("relatedTaxon", taxon);
955 965
            if(type != null) {
......
977 987
            query.add(AuditEntity.relatedId(direction.toString()).eq(taxon.getId()));
978 988
            query.addProjection(AuditEntity.id().count());
979 989

  
980
            if(type != null) {
981
                query.add(AuditEntity.relatedId("type").eq(type.getId()));
990
            if(types != null) {
991
                //TODO adapt to new Set semantic, was single type before
992
//                query.add(AuditEntity.relatedId("type").eq(type.getId()));
982 993
            }
983 994

  
984 995
            return (Long)query.getSingleResult();
......
993 1004
     * @param b
994 1005
     * @return
995 1006
     */
996
    private String prepareTaxonRelationshipQuery(TaxonRelationshipType type, boolean includeUnpublished,
1007
    private String prepareTaxonRelationshipQuery(Set<TaxonRelationshipType> types, boolean includeUnpublished,
997 1008
            Direction direction, boolean isCount) {
998 1009
        String selectStr = isCount? " count(rel) as n ":" rel ";
999 1010
        String result = "SELECT " + selectStr +
1000 1011
             " FROM TaxonRelationship rel " +
1001 1012
             " WHERE rel."+direction+" = :relatedTaxon";
1002
        if (type != null){
1003
            result += " AND rel.type = :type ";
1013
        if (types != null){
1014
            result += " AND rel.type IN (:types) ";
1004 1015
        }
1005 1016
        if(! includeUnpublished) {
1006 1017
            result += " AND rel."+direction.invers()+".publish = :publish";
......
1012 1023
    public List<TaxonRelationship> getTaxonRelationships(Taxon taxon, TaxonRelationshipType type,
1013 1024
            boolean includeUnpublished, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints,
1014 1025
            List<String> propertyPaths, Direction direction) {
1026
        Set<TaxonRelationshipType> types = null;
1027
        if (type != null){
1028
            types = new HashSet<>();
1029
            types.add(type);
1030
        }
1031
        return getTaxonRelationships(taxon, types, includeUnpublished, pageSize, pageNumber, orderHints, propertyPaths, direction);
1032
    }
1033

  
1034
    @Override
1035
    public List<TaxonRelationship> getTaxonRelationships(Taxon taxon, Set<TaxonRelationshipType> types,
1036
            boolean includeUnpublished, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints,
1037
            List<String> propertyPaths, Direction direction) {
1015 1038

  
1016 1039
        AuditEvent auditEvent = getAuditEventFromContext();
1017 1040
        if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
1018 1041

  
1019
            String queryString = prepareTaxonRelationshipQuery(type, includeUnpublished, direction, false);
1042
            String queryString = prepareTaxonRelationshipQuery(types, includeUnpublished, direction, false);
1020 1043

  
1021 1044
            queryString += orderByClause("rel", orderHints);
1022 1045

  
1023 1046
            Query query = getSession().createQuery(queryString);
1024 1047
            query.setParameter("relatedTaxon", taxon);
1025 1048
            if(type != null) {
1026
                query.setParameter("type",type);
1049
                query.setParameterList("types",types);
1027 1050
            }
1028 1051
            if(! includeUnpublished) {
1029 1052
                query.setBoolean("publish",Boolean.TRUE);
......
1066 1089
            AuditQuery query = getAuditReader().createQuery().forEntitiesAtRevision(TaxonRelationship.class,auditEvent.getRevisionNumber());
1067 1090
            query.add(AuditEntity.relatedId("relatedTo").eq(taxon.getId()));
1068 1091

  
1069
            if(type != null) {
1070
                query.add(AuditEntity.relatedId("type").eq(type.getId()));
1092
            if(types != null) {
1093
                //FIXME adapt to Set (was single type before)
1094
//                query.add(AuditEntity.relatedId("type").eq(types.getId()));
1071 1095
            }
1072 1096

  
1073 1097
            if(pageSize != null) {
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/dao/taxon/ITaxonDao.java
281 281
     */
282 282
    public long countTaxonRelationships(Taxon taxon, TaxonRelationshipType type,
283 283
            boolean includeUnpublished, Direction direction);
284
    public long countTaxonRelationships(Taxon taxon, Set<TaxonRelationshipType> types,
285
            boolean includeUnpublished, Direction direction);
284 286

  
285 287
    /**
286 288
     * Returns the TaxonRelationships (of where relationship.type == type, if
......
314 316
            List<OrderHint> orderHints, List<String> propertyPaths,
315 317
            Direction direction);
316 318

  
319
    public List<TaxonRelationship> getTaxonRelationships(Taxon taxon,
320
            Set<TaxonRelationshipType> type, boolean includeUnpublished,
321
            Integer pageSize, Integer pageNumber,
322
            List<OrderHint> orderHints, List<String> propertyPaths,
323
            Direction direction);
324

  
317 325
    /**
318 326
     * Returns a count of the Synonyms (where relationship.type == type,
319 327
     *  if this argument is supplied) where the supplied taxon is relatedTo.
cdmlib-remote/src/main/java/eu/etaxonomy/cdm/remote/controller/TaxonController.java
44 44
import eu.etaxonomy.cdm.api.service.dto.TaxonRelationshipsDTO;
45 45
import eu.etaxonomy.cdm.api.service.pager.Pager;
46 46
import eu.etaxonomy.cdm.exception.UnpublishedException;
47
import eu.etaxonomy.cdm.model.common.CdmBase;
48
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
49 47
import eu.etaxonomy.cdm.model.common.MarkerType;
50 48
import eu.etaxonomy.cdm.model.common.RelationshipBase.Direction;
51 49
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
......
62 60
import eu.etaxonomy.cdm.remote.controller.util.PagerParameters;
63 61
import eu.etaxonomy.cdm.remote.dto.common.StringResultDTO;
64 62
import eu.etaxonomy.cdm.remote.editor.TermBasePropertyEditor;
63
import eu.etaxonomy.cdm.remote.editor.UuidList;
65 64
import io.swagger.annotations.Api;
66 65

  
67 66
/**
......
468 467
    public TaxonRelationshipsDTO doGetTaxonRelationshipsDTO(
469 468
            @PathVariable("uuid") UUID taxonUuid,
470 469
            //TODO should be set
471
            @RequestParam(value = "type", required = false) UUID typeUuid,
470
            @RequestParam(value = "types", required = false) UuidList typeUuids,
472 471
            @RequestParam(value = "direction", required = false) Direction direction,
472
            @RequestParam(value="deduplicateMisapplications", required=false, defaultValue="true") final boolean deduplicateMisapplications,
473 473
            HttpServletRequest request,
474 474
            HttpServletResponse response) throws IOException {
475 475

  
......
477 477

  
478 478
        logger.info("doGetTaxonRelationshipDTOs(): " + request.getRequestURI());
479 479
        TaxonBase<?> taxonBase = service.load(taxonUuid);
480
        Taxon taxon = checkExistsAccessType(taxonBase, includeUnpublished, Taxon.class, response);
480
        checkExistsAccessType(taxonBase, includeUnpublished, Taxon.class, response);
481 481

  
482 482
        Set<TaxonRelationshipType> types = null;
483
        if (typeUuid != null){
483

  
484
        if (typeUuids != null && !typeUuids.isEmpty()){
484 485
            types = new HashSet<>();
485
            DefinedTermBase<?> type = termService.find(typeUuid);
486
            if (type.isInstanceOf(TaxonRelationshipType.class)){
487
                types.add(CdmBase.deproxy(type, TaxonRelationshipType.class));
488
            }else{
489
                HttpStatusMessage.UUID_REFERENCES_WRONG_TYPE.send(response);
490
            }
486
            List<TaxonRelationshipType> typeList = termService.find(TaxonRelationshipType.class, new HashSet<>(typeUuids));
487
            types.addAll(typeList);
488
            //TODO should we handle missing uuids as error response
489
//            HttpStatusMessage.UUID_REFERENCES_WRONG_TYPE.send(response);
491 490
        }
492
        boolean deduplicateMisapplications = true;
491

  
492
//        boolean deduplicateMisapplications = true;
493 493
        Integer pageSize = null;
494 494
        Integer pageNumber = null;
495 495
        return service.listTaxonRelationships(taxonUuid, types, direction, deduplicateMisapplications,
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/IService.java
19 19
import org.hibernate.LockOptions;
20 20
import org.hibernate.Session;
21 21
import org.hibernate.criterion.Criterion;
22
import org.hibernate.event.spi.MergeEvent;
22 23

  
23 24
import eu.etaxonomy.cdm.api.service.pager.Pager;
24 25
import eu.etaxonomy.cdm.model.common.ICdmBase;
25 26
import eu.etaxonomy.cdm.persistence.dao.common.Restriction;
27
import eu.etaxonomy.cdm.persistence.dao.initializer.IBeanInitializer;
26 28
import eu.etaxonomy.cdm.persistence.dto.MergeResult;
29
import eu.etaxonomy.cdm.persistence.hibernate.PostMergeEntityListener;
27 30
import eu.etaxonomy.cdm.persistence.query.Grouping;
28 31
import eu.etaxonomy.cdm.persistence.query.MatchMode;
29 32
import eu.etaxonomy.cdm.persistence.query.OrderHint;
......
109 112
    public List<T> find(Set<UUID> uuidSet);
110 113

  
111 114
    /**
115
     * Return a list of persisted entities that match the unique identifier
116
     * set supplied as an argument and that do match the supplied class.
117
     *
118
     * @param uuidSet the set of unique identifiers of the entities required
119
     * @return a list of entities of type <T>
120
     */
121
    public <S extends T> List<S> find(Class<S> clazz, Set<UUID> uuidSet);
122

  
123
    /**
112 124
     * Return a persisted entity that matches the unique identifier
113 125
     * supplied as an argument, or null if the entity does not exist
114 126
     *
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/ServiceBase.java
123 123
        return dao.list(uuidSet, null, null, null, null);
124 124
    }
125 125

  
126

  
127
    @Override
128
    @Transactional(readOnly = true)
129
    public <S extends T> List<S> find(Class<S> clazz, Set<UUID> uuidSet) {
130
        return dao.list(clazz, uuidSet, null, null, null, null);
131
    }
132

  
126 133
    @Override
127 134
    @Transactional(readOnly = true)
128 135
    public List<T> findById(Set<Integer> idSet) {  //can't be called find(Set<Integer>) as this conflicts with find(Set<UUID)
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/TaxonServiceImpl.java
569 569
        if(logger.isDebugEnabled()){
570 570
            logger.debug("collecting related taxa for " + taxon + " with maxDepth=" + maxDepth);
571 571
        }
572
        List<TaxonRelationship> taxonRelationships = dao.getTaxonRelationships(taxon, null, includeUnpublished, null, null, null, null, null);
572
        List<TaxonRelationship> taxonRelationships = dao.getTaxonRelationships(taxon,
573
                (Set<TaxonRelationshipType>)null, includeUnpublished, null, null, null, null, null);
573 574
        for (TaxonRelationship taxRel : taxonRelationships) {
574 575

  
575 576
            // skip invalid data

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)