Project

General

Profile

Revision 16f5f3d4

ID16f5f3d4006269507cc28f9238908bbd63e530c5
Parent e4d66102
Child 705f268c

Added by Andreas Müller about 3 years ago

ref #6612 implemented list (ref ...) for RegistrationDaoHibernateImpl

View differences:

cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/name/Registration.java
181 181
    public Set<TypeDesignationBase> getTypeDesignations() {return typeDesignations;}
182 182
    public void setTypeDesignations(Set<TypeDesignationBase> typeDesignations) {this.typeDesignations = typeDesignations;}
183 183

  
184
    public void addTypeDesignation(TypeDesignationBase desig) {
185
        this.typeDesignations.add(desig);
186
    }
187
    public void removeTypeDesignation(TypeDesignationBase desig) {
188
        this.typeDesignations.remove(desig);
189
    }
190

  
184 191

  
185 192
}
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/dao/hibernate/name/RegistrationDaoHibernateImpl.java
8 8
*/
9 9
package eu.etaxonomy.cdm.persistence.dao.hibernate.name;
10 10

  
11
import java.util.Collections;
11 12
import java.util.List;
12 13

  
13 14
import org.apache.log4j.Logger;
......
18 19
import eu.etaxonomy.cdm.model.reference.IReference;
19 20
import eu.etaxonomy.cdm.persistence.dao.hibernate.common.AnnotatableDaoImpl;
20 21
import eu.etaxonomy.cdm.persistence.dao.name.IRegistrationDao;
21
import eu.etaxonomy.cdm.persistence.query.OrderHint;
22 22

  
23 23
/**
24 24
 * @author a.kohlbecker
......
42 42
     * {@inheritDoc}
43 43
     */
44 44
    @Override
45
    public List<Registration> list(Integer limit, Integer start, IReference reference, List<OrderHint> orderHints,
45
    public List<Registration> list(IReference reference, Integer limit, Integer start,
46 46
            List<String> propertyPaths) {
47 47

  
48
        Query query = getSession().createQuery("from Registration order by uuid");
48
        if (reference != null && reference.getId() == 0){
49
            return Collections.emptyList();
50
        }
51

  
52
        String hql = "SELECT DISTINCT r "
53
                + " FROM Registration r LEFT JOIN r.typeDesignations desig "
54
                + "     LEFT JOIN r.name n "
55
                + " WHERE (n IS NOT NULL AND n.nomenclaturalReference =:ref)"
56
                + "     OR desig.citation =:ref "
57
                + " ORDER BY r.id ";
58
        if (reference == null){
59
            hql = "SELECT DISTINCT r "
60
                    + " FROM Registration r LEFT JOIN r.typeDesignations desig "
61
                    + "    LEFT JOIN r.name n"
62
                    + " WHERE (r.name IS NULL AND size(r.typeDesignations) = 0 ) "
63
                    + "     OR (n IS NOT NULL AND r.name.nomenclaturalReference IS NULL ) "
64
                    + "     OR (size(r.typeDesignations) > 0 AND desig.citation IS NULL )  "
65
                    + " ORDER BY r.id ";
66
        }
67

  
68
        Query query = getSession().createQuery(hql);
69
        if (reference != null){
70
            query.setParameter("ref", reference);
71
        }
72

  
49 73
        // TODO complete ....
50
        query.setFirstResult(start);
51
        query.setMaxResults(limit);
74
        if(limit != null /*&&  !doCount*/) {
75
            query.setMaxResults(limit);
76
            if(start != null) {
77
                query.setFirstResult(start);
78
            }
79
        }
52 80

  
53
        return null;
81
        //TODO order hints do not work with queries
82

  
83
        List<Registration> results = query.list();
84
        defaultBeanInitializer.initializeAll(results, propertyPaths);
85

  
86
        return results;
54 87
    }
55 88

  
56 89
    /**
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/dao/name/IRegistrationDao.java
17 17
import eu.etaxonomy.cdm.model.reference.IReference;
18 18
import eu.etaxonomy.cdm.persistence.dao.common.IAnnotatableDao;
19 19
import eu.etaxonomy.cdm.persistence.dao.initializer.IBeanInitializer;
20
import eu.etaxonomy.cdm.persistence.query.OrderHint;
21 20

  
22 21
/**
23 22
 * @author a.kohlbecker
......
37 36
     * <code>propertyPaths</code> <b>please refer to:</b>
38 37
     * {@link IBeanInitializer#initialize(Object, List)}
39 38
     *
39
     * @param reference
40
     *            filters the Registration by the reference of the nomenclatural act
41
     *            for which the Registration has been created.
42
     *            The name and all type designations associated with the Registration
43
     *            are sharing the same  reference.
44
     *            If <code>null</code> all registrations with a name or type designation
45
     *            that has no reference are returned. Also those registrations
46
     *            having no name and type designation at all.
40 47
     * @param limit
41
     *            the maximum number of entities returned (can be null to return
42
     *            all entities)
48
     *            the maximum number of entities returned (can be <code>null</code>
49
     *            to return all entities)
43 50
     * @param start
44
     * @param reference
45
     *            filters the Registration by the reference of the nomenclatural act for which the Registration as been created.
46
     *            The name and all type designations associated with the Registration are sharing the same  citation and citation detail.
47
     * @param orderHints
48
     *            Supports path like <code>orderHints.propertyNames</code> which
49
     *            include *-to-one properties like createdBy.username or
50
     *            authorTeam.persistentTitleCache
51 51
     * @param propertyPaths
52 52
     * @return
53 53
     * @throws DataAccessException
54 54
     */
55
    public List<Registration> list(Integer limit, Integer start, IReference reference, List<OrderHint> orderHints, List<String> propertyPaths);
55
    public List<Registration> list(IReference reference, Integer limit, Integer start, List<String> propertyPaths);
56 56

  
57 57
    /**
58 58
     * Counts the Registration instances stored in the database.
cdmlib-persistence/src/test/java/eu/etaxonomy/cdm/persistence/dao/hibernate/name/RegistrationDaoHibernateImplTest.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.persistence.dao.hibernate.name;
10

  
11
import static org.junit.Assert.assertEquals;
12
import static org.junit.Assert.assertFalse;
13
import static org.junit.Assert.assertTrue;
14

  
15
import java.io.FileNotFoundException;
16
import java.util.List;
17

  
18
import org.junit.Before;
19
import org.junit.Test;
20
import org.unitils.spring.annotation.SpringBeanByType;
21

  
22
import eu.etaxonomy.cdm.model.name.Registration;
23
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
24
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
25
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
26
import eu.etaxonomy.cdm.model.reference.Reference;
27
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
28
import eu.etaxonomy.cdm.persistence.dao.name.IRegistrationDao;
29
import eu.etaxonomy.cdm.persistence.dao.reference.IReferenceDao;
30
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
31

  
32
/**
33
 * @author a.mueller
34
 * @date 05.05.2017
35
 *
36
 */
37
public class RegistrationDaoHibernateImplTest  extends CdmTransactionalIntegrationTest {
38

  
39
    @SpringBeanByType
40
    IRegistrationDao registrationDao;
41

  
42
    @SpringBeanByType
43
    IReferenceDao referenceDao;
44

  
45

  
46
    @Before
47
    public void setUp() {
48

  
49
    }
50

  
51
    @Test
52
    public void testListByReference() {
53
        List<Registration> registrationList;
54

  
55
        //test name with ref
56
        TaxonNameBase<?,?> name = TaxonNameFactory.NewBotanicalInstance(null);
57
        Reference nomRef = ReferenceFactory.newBook();
58
        name.setNomenclaturalReference(nomRef);
59
        Registration registration = Registration.NewInstance();
60
        registration.setName(name);
61
        registrationDao.save(registration);
62
        registrationList = registrationDao.list( nomRef, null, null,null);
63
        assertFalse("List should not be empty", registrationList.isEmpty());
64

  
65
        //test type designation with ref
66
        Registration regWithType = Registration.NewInstance();
67
        Reference desigRef = ReferenceFactory.newArticle();
68
        SpecimenTypeDesignation desig = SpecimenTypeDesignation.NewInstance();
69
        desig.setCitation(desigRef);
70
        regWithType.addTypeDesignation(desig);
71
        registrationDao.save(regWithType);
72
        registrationList = registrationDao.list(desigRef, null, null, null);
73
        assertEquals("List should have 1 entry", 1, registrationList.size());
74

  
75
        //test unused ref
76
        Reference newRef = ReferenceFactory.newArticle();
77
        registrationList = registrationDao.list(newRef, null, null, null);
78
        assertTrue("List should be empty", registrationList.isEmpty());
79
        //... and saved
80
        referenceDao.save(newRef);
81
        registrationList = registrationDao.list(newRef, null, null, null);
82
        assertTrue("List should be empty", registrationList.isEmpty());
83

  
84

  
85
        //test null
86
        Reference nullRef = null;
87
        registrationList = registrationDao.list(nullRef, null, null, null);
88
        assertEquals("List should be empty", 0, registrationList.size());
89

  
90
        //... name without ref
91
        Registration regWithUnreferenceName = Registration.NewInstance();
92
        TaxonNameBase<?,?> nameWithoutRef = TaxonNameFactory.NewBotanicalInstance(null);
93
        regWithUnreferenceName.setName(nameWithoutRef);
94
        registrationDao.save(regWithUnreferenceName);
95
        registrationList = registrationDao.list(nullRef, null, null, null);
96
        assertEquals("List should have 1 entry", 1, registrationList.size());
97

  
98
        //... registration without name and type designation
99
        Registration emptyRegistration = Registration.NewInstance();
100
        registrationDao.save(emptyRegistration);
101
        registrationList = registrationDao.list(nullRef, null, null, null);
102
        assertEquals("List should have 2 entries now", 2, registrationList.size());
103

  
104

  
105
        //test name and type designation with same ref
106
        TaxonNameBase<?,?> additionalName = TaxonNameFactory.NewBotanicalInstance(null);
107
        additionalName.setNomenclaturalReference(desigRef);
108
        regWithType.setName(additionalName);
109
        registrationList = registrationDao.list( desigRef, null, null,null);
110
        assertEquals("List should still have 1 entry", 1, registrationList.size());
111
        assertEquals("",regWithType, registrationList.get(0));
112

  
113
        //test dirty data (it is sufficient that 1 reference matches, not necessarily all)
114
        SpecimenTypeDesignation desig2 = SpecimenTypeDesignation.NewInstance();
115
        Reference otherRef = ReferenceFactory.newGeneric();
116
        desig2.setCitation(otherRef);
117
        regWithType.addTypeDesignation(desig2);
118
        registrationDao.saveOrUpdate(regWithType);
119
        registrationList = registrationDao.list( desigRef, null, null,null);
120
        assertEquals("List should still have 1 entry", 1, registrationList.size());
121
        assertEquals("",regWithType, registrationList.get(0));
122
        registrationList = registrationDao.list( otherRef, null, null,null);
123
        assertEquals("Also for otherRef the registration should be found", 1, registrationList.size());
124
        assertEquals("",regWithType, registrationList.get(0));
125

  
126
        //> 1
127
        Registration registration2 = Registration.NewInstance();
128
        TaxonNameBase<?,?> name2 = TaxonNameFactory.NewBotanicalInstance(null);
129
        name2.setNomenclaturalReference(desigRef);
130
        registration2.setName(name2);
131
        registrationDao.save(registration2);
132

  
133
        Registration registration3 = Registration.NewInstance();
134
        TaxonNameBase<?,?> name3 = TaxonNameFactory.NewBotanicalInstance(null);
135
        name3.setNomenclaturalReference(desigRef);
136
        registration3.setName(name3);
137
        registrationDao.save(registration3);
138

  
139
        Registration registration4 = Registration.NewInstance();
140
        SpecimenTypeDesignation desig4 = SpecimenTypeDesignation.NewInstance();
141
        desig4.setCitation(desigRef);
142
        registration4.addTypeDesignation(desig4);
143
        registrationDao.save(registration4);
144

  
145
        registrationList = registrationDao.list( desigRef, null, null,null);
146
        assertEquals("List should have 4 entries", 4, registrationList.size());
147
        registrationList = registrationDao.list( desigRef, 3, null,null);
148
        assertEquals("List should have 3 entries", 3, registrationList.size());
149
        registrationList = registrationDao.list( desigRef, 3, 2,null);
150
        assertEquals("List should have 2 entries", 2, registrationList.size());
151

  
152
    }
153

  
154
    /**
155
     * {@inheritDoc}
156
     */
157
    @Override
158
    public void createTestDataSet() throws FileNotFoundException {}
159
}

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)