Project

General

Profile

« Previous | Next » 

Revision cb994835

Added by Andreas Müller over 7 years ago

ref #6065 improved Pager handling and pager exception handling

View differences:

cdmlib-remote/src/main/java/eu/etaxonomy/cdm/remote/controller/ClassificationController.java
14 14
import java.util.List;
15 15
import java.util.UUID;
16 16

  
17
import javax.persistence.EntityNotFoundException;
17 18
import javax.servlet.http.HttpServletRequest;
18 19
import javax.servlet.http.HttpServletResponse;
19 20

  
......
163 164
//       service.startTransaction();
164 165
       boolean recursive = false;
165 166
       UUID taxonNodeUuid = service.getTaxonNodeUuidByTaxonUuid(classificationUuid, taxonUuid) ;
167
       if (taxonNodeUuid == null){
168
           HttpStatusMessage.UUID_NOT_FOUND.send(response);
169
           return null;
170
       }
166 171
       Pager<TaxonNodeDto> pager = taxonNodeService.pageChildNodesDTOs(taxonNodeUuid, recursive, doSynonyms, sortMode, pageSize, pageIndex);
167 172
//       service.commitTransaction()
168 173

  
......
235 240
           HttpServletResponse response
236 241
           ) throws IOException {
237 242

  
238
       TaxonInContextDTO taxonInContextDTO = service.getTaxonInContext(classificationUuid, taxonUuid, doChildren, doSynonyms, ancestorMarkers, sortMode) ;
239

  
240
       return taxonInContextDTO;
243
       try {
244
           TaxonInContextDTO taxonInContextDTO = service.getTaxonInContext(classificationUuid, taxonUuid, doChildren, doSynonyms, ancestorMarkers, sortMode);
245
           return taxonInContextDTO;
246
       } catch (EntityNotFoundException e) {
247
           HttpStatusMessage.UUID_NOT_FOUND.send(response);
248
           return null;
249
       }
241 250
   }
242 251

  
243 252

  
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/TaxonNodeServiceImpl.java
121 121

  
122 122
        List<TaxonNodeDto> dtos = new ArrayList<>(pageSize==null?25:pageSize);
123 123
        Integer start = PagerUtils.startFor(pageSize, pageIndex);
124
        start = start == null ? 0 : start;
125 124
        Integer limit = PagerUtils.limitFor(pageSize);
126 125
        limit = limit == null ? Integer.MAX_VALUE : limit;
127 126
        Long totalCount = Long.valueOf(allRecords.size());
128 127

  
129
        if(PagerUtils.hasResultsInRange(totalCount, pageIndex, pageSize)) {
130 128
            TaxonNameBase<?,?> parentName = null;
131 129

  
132
            for(int i = start; i < Math.min(totalCount, start + limit); i++) {
133
                CdmBase record = allRecords.get(i);
134

  
130
//            for(int i = start; i < Math.min(totalCount, start + limit); i++) {
131
            for(CdmBase record : PagerUtils.pageList(allRecords, pageIndex, pageSize)) {
135 132
                if (record.isInstanceOf(TaxonNode.class)){
136 133
                    dtos.add(new TaxonNodeDto(CdmBase.deproxy(record, TaxonNode.class)));
137 134
                }else if (record.isInstanceOf(Synonym.class)){
......
141 138
                    dtos.add(new TaxonNodeDto(synonym, isHomotypic));
142 139
                }
143 140
            }
144
        }
141

  
142
//        }
145 143

  
146 144
        return new DefaultPagerImpl<TaxonNodeDto>(pageIndex, totalCount, pageSize , dtos);
147 145
    }
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/pager/PagerRangeException.java
1
// $Id$
2
/**
3
* Copyright (C) 2016 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.api.service.pager;
11

  
12
/**
13
 * @author a.mueller
14
 * @date 22.09.2016
15
 *
16
 */
17
public class PagerRangeException extends RuntimeException {
18

  
19
    private static final long serialVersionUID = 1626299671702704921L;
20

  
21
    /**
22
     * @param message
23
     */
24
    public PagerRangeException(String message) {
25
        super(message);
26
    }
27

  
28

  
29

  
30

  
31
}
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/pager/PagerUtils.java
9 9
*/
10 10
package eu.etaxonomy.cdm.api.service.pager;
11 11

  
12
import java.util.List;
13

  
12 14
import eu.etaxonomy.cdm.api.service.pager.impl.AbstractPagerImpl;
13 15

  
14 16
/**
......
20 22
 */
21 23
public class PagerUtils {
22 24

  
23
    public static Integer startFor(Integer pageSize, Integer pageIndex) {
24
        if(pageSize == null){
25
            return null;
26
        }
25
    /**
26
     * Returns the start index for the given page size and page index values
27
     * by multiplying both values (startFor = pageSize * pageIndex).<BR>
28
     * If page size is <code>null</code>, either <code>null</code> is returned
29
     * or an exception is thrown if page index is > 0.
30
     * If page index is null it is assumed as 0.
31
     *
32
     * @param pageSize the page size
33
     * @param pageIndex the page index
34
     * @return the start index
35
     * @throws PagerRangeException if pageSize == null and pageIndex > 0
36
     */
37
    public static Integer startFor(Integer pageSize, Integer pageIndex) throws PagerRangeException {
27 38
        if(pageIndex == null) {
28 39
            pageIndex = 0;
29 40
        }
41
        if(pageSize == null){
42
            if (pageIndex > 0 ){
43
                throw new PagerRangeException("Page index must be undefined or 0 for undefined page size but was " + pageIndex);
44
            }else{
45
                return null;
46
            }
47
        }
30 48
        return pageSize *  pageIndex;
31

  
32 49
    }
33 50

  
51
    /**
52
     * Returns the limit value as used in SQL/HQL statements for the given
53
     * page size.<BR>
54
     * Earlier, for <code>null</code> the value 0 was returned but as
55
     * it was decided that pageSize = <code>null</code> should not set any limitation
56
     * it was changed to return value <code>null</code>. This way the current implementation
57
     * is the identity function and has no effect except for documentation.
58
     * <code>null</code> should be handled in dao-s or specialized code such as
59
     * {@link #pageList(List, Integer, Integer)}.
60
     *
61
     * @param pageSize
62
     * @return
63
     */
34 64
    public static Integer limitFor(Integer pageSize) {
35 65
        if(pageSize == null){
36 66
            return null;
67
        }else{
68
            return pageSize;
37 69
        }
38
        return pageSize;
39 70
    }
40 71

  
41 72
    /**
......
52 83
        return AbstractPagerImpl.hasResultsInRange(numberOfResults, pageIndex, pageSize);
53 84
    }
54 85

  
86
    /**
87
     * Returns a synchronized (sub)list of fullList which contains only the items
88
     * for the given page
89
     * @param fullList
90
     * @param pageIndex page index
91
     * @param pageSize page size
92
     * @return a synchronized list holding the page items
93
     */
94
    public  static  <T extends Object> List<T> pageList(List<T> fullList, Integer pageIndex, Integer pageSize ){
95
        Integer start = startFor(pageSize, pageIndex);
96
        if (start == null){
97
            return fullList;
98
        }
99
        Integer limit = limitFor(pageSize); //no effect and never null at this point due to startFor semantics
100
        limit = Math.min(Integer.MAX_VALUE - start, limit);
101

  
102
        if (fullList.size() > start) {
103
            Integer toIndex = Math.min(fullList.size(), start + limit);
104
            return fullList.subList(start, toIndex);
105
        }else{
106
            return fullList.subList(fullList.size(), fullList.size()); //empty list
107
        }
108
    }
109

  
55 110
}

Also available in: Unified diff