Project

General

Profile

Download (9.72 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * Copyright (C) 2007 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.remote.controller;
10

    
11
import java.io.IOException;
12
import java.util.ArrayList;
13
import java.util.List;
14
import java.util.UUID;
15

    
16
import javax.servlet.http.HttpServletRequest;
17
import javax.servlet.http.HttpServletResponse;
18

    
19
import org.apache.log4j.Logger;
20
import org.springframework.web.bind.WebDataBinder;
21
import org.springframework.web.bind.annotation.InitBinder;
22
import org.springframework.web.bind.annotation.RequestMapping;
23
import org.springframework.web.bind.annotation.RequestMethod;
24
import org.springframework.web.bind.annotation.RequestParam;
25

    
26
import eu.etaxonomy.cdm.api.service.IClassificationService;
27
import eu.etaxonomy.cdm.api.service.IService;
28
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
29
import eu.etaxonomy.cdm.api.service.pager.Pager;
30
import eu.etaxonomy.cdm.model.common.CdmBase;
31
import eu.etaxonomy.cdm.model.taxon.Classification;
32
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
33
import eu.etaxonomy.cdm.persistence.dao.common.Restriction;
34
import eu.etaxonomy.cdm.remote.controller.util.PagerParameters;
35
import eu.etaxonomy.cdm.remote.editor.CdmTypePropertyEditor;
36
import eu.etaxonomy.cdm.remote.editor.UUIDPropertyEditor;
37

    
38
/**
39
 * @author a.kohlbecker
40
 * @since 22.07.2009
41
 */
42
public abstract class BaseListController <T extends CdmBase, SERVICE extends IService<T>> extends AbstractListController<T, SERVICE> {
43

    
44
    public static final Logger logger = Logger.getLogger(BaseListController.class);
45

    
46
    @InitBinder
47
    public void initBinder(WebDataBinder binder) {
48
        binder.registerCustomEditor(UUID.class, new UUIDPropertyEditor());
49
        binder.registerCustomEditor(Class.class, new CdmTypePropertyEditor());
50
    }
51

    
52
    /**
53
     * NOTE: The indices for pages are 0-based see {@link Pager}
54
     *
55
     * @param pageIndex
56
     *            the index of the page to be returned, the first page has the
57
     *            pageIndex = 0 - <i>optional parameter</i>. Defaults to 0 if
58
     *            set to <code>NULL</code>.
59
     * @param pageSize
60
     *            the maximum number of entities returned per page.
61
     *            The {@link #DEFAULT_PAGE_SIZE} will be used if pageSize is set to
62
     *            <code>null</code> - <i>optional parameter</i>
63
     * @param type
64
     *            Further restricts the type of entities to be returned.
65
     *            If null the base type <code>&lt;T&gt;</code> is being used. - <i>optional parameter</i>
66
     * @return
67
     * @throws IOException
68
     */
69
    @SuppressWarnings("unchecked")
70
    @RequestMapping(method = RequestMethod.GET)
71
    public Pager<T> doPage(
72
            @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
73
            @RequestParam(value = "pageSize", required = false) Integer pageSize,
74
            @RequestParam(value = "class", required = false) Class type,
75
            @RequestParam(name="orderBy", defaultValue="BY_TITLE_CACHE_ASC", required=true) OrderHintPreset orderBy,
76
            HttpServletRequest request,
77
            HttpServletResponse response) throws IOException
78
            {
79

    
80
        logger.info("doPage() " + requestPathAndQuery(request));
81
        PagerParameters pagerParameters = new PagerParameters(pageSize, pageIndex).normalizeAndValidate(response);
82

    
83
        if(type != null) {
84
            orderBy = orderBy.checkSuitableFor(type);
85
            // TODO how can we check in case type == null?
86
        }
87
        return service.page(type, pagerParameters.getPageSize(), pagerParameters.getPageIndex(), orderBy.orderHints(), getInitializationStrategy());
88
    }
89

    
90
    @SuppressWarnings("unchecked")
91
    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, params={"restriction"})
92
    public Pager<T> doPageByRestrictions(
93
            @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
94
            @RequestParam(value = "pageSize", required = false) Integer pageSize,
95
            @RequestParam(value = "class", required = false) Class type,
96
            @RequestParam(value = "restriction", required = true) List<Restriction<?>> restrictions,
97
            @RequestParam(value = "initStrategy", required = true) List<String> initStrategy,
98
            @RequestParam(name="orderBy", defaultValue="BY_TITLE_CACHE_ASC", required=true) OrderHintPreset orderBy,
99
            HttpServletRequest request,
100
            HttpServletResponse response) throws IOException
101
            {
102

    
103
        // NOTE: for testing with httpi and jq:
104
        // http GET :8080/portal/taxon.json restriction=='{"propertyName":"name.titleCache","matchMode":"EXACT","values":["Eunotia krammeri Metzeltin & Lange-Bert."]}' initStrategy=name.titleCache | jq '.records[].name.titleCache'
105
        logger.info("doPageByRestrictions() " + requestPathAndQuery(request));
106
        PagerParameters pagerParameters = new PagerParameters(pageSize, pageIndex).normalizeAndValidate(response);
107

    
108
        if(type != null) {
109
            orderBy = orderBy.checkSuitableFor(type);
110
        }
111

    
112
        return pageByRestrictions(type, initStrategy, orderBy, pagerParameters, new ArrayList<>(restrictions));
113
    }
114

    
115
    /**
116
     * This method can be overwritten by subclasses, for example to apply additional filtering like for the publish flag.
117
     *
118
     * @param type
119
     * @param initStrategy
120
     * @param orderBy
121
     * @param pagerParameters
122
     * @param restrictions
123
     * @return
124
     */
125
    protected Pager<T> pageByRestrictions(Class<T> type, List<String> initStrategy, OrderHintPreset orderBy,
126
            PagerParameters pagerParameters, ArrayList<Restriction<?>> restrictions) {
127
        return service.page(type, restrictions, pagerParameters.getPageSize(), pagerParameters.getPageIndex(), orderBy.orderHints(), initStrategy);
128
    }
129

    
130
//    /**
131
//     * Parameter less method to be used as default when request without parameter are made. Otherwise
132
//     * the nameless methods {@link #doPage(Integer, Integer, Class)} and {@link #doList(Integer, Integer, Class)}
133
//     * are ambigous.
134
//     * @return
135
//     * @throws IOException
136
//     */
137
//    @RequestMapping(method = RequestMethod.GET)
138
//    public Pager<T> doPage(HttpServletRequest request, HttpServletResponse response) throws IOException{
139
//        return doPage(null, null, null, request, response);
140
//    }
141

    
142
    /**
143
     * @param start
144
     *            The offset index from the start of the list. The first entity
145
     *            has the index = 0 - <i>required parameter</i>
146
     * @param limit
147
     *            The maximum number of entities returned. - <i>optional parameter</i>
148
     *            If limit is set to a value < 1 all entities will be returned
149
     * @param type
150
     *            Further restricts the type of entities to be returned.
151
     *            If null the base type <code>&lt;T&gt;</code> is being used. - <i>optional parameter</i>
152
     * @return a List of entities
153
     */
154
    @RequestMapping(method = RequestMethod.GET, params = "start")
155
    public List<T> doList(
156
            @RequestParam(value = "start", required = true) Integer start,
157
            @RequestParam(value = "limit", required = false) Integer limit,
158
            @RequestParam(value = "class", required = false) Class<T> type,
159
            HttpServletRequest request,
160
            @SuppressWarnings("unused") HttpServletResponse response) {
161

    
162
        if (request != null){
163
            logger.info("doList() " + requestPathAndQuery(request));
164
        }
165

    
166
        //if(start == null){ start = 0;}
167
        if(limit == null){
168
            limit = PagerParameters.DEFAULT_PAGESIZE;
169
        }else if(limit < 1){
170
            limit = null;
171
        }
172
        return service.list(type, limit, start, null, getInitializationStrategy());
173
    }
174

    
175
    // this is a copy from BaseController, should be unified
176
    protected TaxonNode getSubtreeOrError(UUID subtreeUuid, ITaxonNodeService taxonNodeService, HttpServletResponse response) throws IOException {
177
        TaxonNode subtree = null;
178
        if (subtreeUuid != null){
179
            subtree = taxonNodeService.find(subtreeUuid);
180
            if(subtree == null) {
181
                response.sendError(404 , "Taxon node for subtree not found: " + subtreeUuid );
182
                //will not happen
183
                return null;
184
            }
185
        }
186
        return subtree;
187
    }
188

    
189
    // this is a copy from BaseController, should be unified
190
    protected Classification getClassificationOrError(UUID classificationUuid,
191
            IClassificationService classificationService, HttpServletResponse response) throws IOException {
192
        Classification classification = null;
193
        if (classificationUuid != null){
194
            classification = classificationService.find(classificationUuid);
195
            if(classification == null) {
196
                response.sendError(404 , "Classification not found: " + classificationUuid );
197
                //will not happen
198
                return null;
199
            }
200
        }
201
        return classification;
202
    }
203

    
204
  /* TODO
205
   @RequestMapping(method = RequestMethod.POST)
206
  public T doPost(@ModelAttribute("object") T object, BindingResult result) {
207
        validator.validate(object, result);
208
        if (result.hasErrors()) {
209
                // set http status code depending upon what happened, possibly return
210
            // the put object and errors so that they can be rendered into a suitable error response
211
        } else {
212
          // should set the status to 201 created  and "Location" header to "/resource/uuid"
213
          service.save(object);
214
        }
215
  }
216
  */
217
}
(11-11/76)