Project

General

Profile

Download (9.74 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

    
10

    
11
package eu.etaxonomy.cdm.remote.controller;
12

    
13
import java.io.IOException;
14
import java.util.ArrayList;
15
import java.util.List;
16
import java.util.UUID;
17

    
18
import javax.servlet.http.HttpServletRequest;
19
import javax.servlet.http.HttpServletResponse;
20

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

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

    
40

    
41
/**
42
 * @author a.kohlbecker
43
 * @since 22.07.2009
44
 *
45
 * @param <T>
46
 * @param <SERVICE>
47
 */
48
public abstract class BaseListController <T extends CdmBase, SERVICE extends IService<T>> extends AbstractListController<T, SERVICE> {
49

    
50
    public static final Logger logger = Logger.getLogger(BaseListController.class);
51

    
52
    @InitBinder
53
    public void initBinder(WebDataBinder binder) {
54
        binder.registerCustomEditor(UUID.class, new UUIDPropertyEditor());
55
        binder.registerCustomEditor(Class.class, new CdmTypePropertyEditor());
56
    }
57

    
58

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

    
87
        logger.info("doPage() " + requestPathAndQuery(request));
88
        PagerParameters pagerParameters = new PagerParameters(pageSize, pageIndex).normalizeAndValidate(response);
89

    
90
        if(type != null) {
91
            orderBy = orderBy.checkSuitableFor(type);
92
            // TODO how can we check in case type == null?
93
        }
94
        return service.page(type, pagerParameters.getPageSize(), pagerParameters.getPageIndex(), orderBy.orderHints(), getInitializationStrategy());
95
    }
96

    
97
    @SuppressWarnings("unchecked")
98
    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, params={"restriction"})
99
    public Pager<T> doPageByRestrictions(
100
            @RequestParam(value = "pageNumber", required = false) Integer pageIndex,
101
            @RequestParam(value = "pageSize", required = false) Integer pageSize,
102
            @RequestParam(value = "class", required = false) Class type,
103
            @RequestParam(value = "restriction", required = true) List<Restriction<?>> restrictions,
104
            @RequestParam(value = "initStrategy", required = true) List<String> initStrategy,
105
            @RequestParam(name="orderBy", defaultValue="BY_TITLE_CACHE_ASC", required=true) OrderHintPreset orderBy,
106
            HttpServletRequest request,
107
            HttpServletResponse response) throws IOException
108
            {
109

    
110
        // NOTE: for testing with httpi and jq:
111
        // 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'
112
        logger.info("doPageByRestrictions() " + requestPathAndQuery(request));
113
        PagerParameters pagerParameters = new PagerParameters(pageSize, pageIndex).normalizeAndValidate(response);
114

    
115
        if(type != null) {
116
            orderBy = orderBy.checkSuitableFor(type);
117
        }
118

    
119
        return pageByRestrictions(type, initStrategy, orderBy, pagerParameters, new ArrayList<>(restrictions));
120
    }
121

    
122

    
123
    /**
124
     * This method can be overwritten by subclasses, for example to apply additional filtering like for the publish flag.
125
     *
126
     * @param type
127
     * @param initStrategy
128
     * @param orderBy
129
     * @param pagerParameters
130
     * @param restrictions
131
     * @return
132
     */
133
    protected Pager<T> pageByRestrictions(Class<T> type, List<String> initStrategy, OrderHintPreset orderBy,
134
            PagerParameters pagerParameters, ArrayList<Restriction<?>> restrictions) {
135
        return service.page(type, restrictions, pagerParameters.getPageSize(), pagerParameters.getPageIndex(), orderBy.orderHints(), initStrategy);
136
    }
137

    
138

    
139
//    /**
140
//     * Parameter less method to be used as default when request without parameter are made. Otherwise
141
//     * the nameless methods {@link #doPage(Integer, Integer, Class)} and {@link #doList(Integer, Integer, Class)}
142
//     * are ambigous.
143
//     * @return
144
//     * @throws IOException
145
//     */
146
//    @RequestMapping(method = RequestMethod.GET)
147
//    public Pager<T> doPage(HttpServletRequest request, HttpServletResponse response) throws IOException{
148
//        return doPage(null, null, null, request, response);
149
//    }
150

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

    
171
        if (request != null)
172
        {
173
            logger.info("doList() " + requestPathAndQuery(request));
174
        }
175

    
176
        //if(start == null){ start = 0;}
177
        if(limit == null){ limit = PagerParameters.DEFAULT_PAGESIZE;}
178
        if(limit < 1){ limit = null;}
179
        return service.list(type, limit, start, null, getInitializationStrategy());
180
    }
181

    
182
    // this is a copy from BaseController, should be unified
183
    protected TaxonNode getSubtreeOrError(UUID subtreeUuid, ITaxonNodeService taxonNodeService, HttpServletResponse response) throws IOException {
184
        TaxonNode subtree = null;
185
        if (subtreeUuid != null){
186
            subtree = taxonNodeService.find(subtreeUuid);
187
            if(subtree == null) {
188
                response.sendError(404 , "Taxon node for subtree not found: " + subtreeUuid );
189
                //will not happen
190
                return null;
191
            }
192
        }
193
        return subtree;
194
    }
195

    
196
    // this is a copy from BaseController, should be unified
197
    protected Classification getClassificationOrError(UUID classificationUuid,
198
            IClassificationService classificationService, HttpServletResponse response) throws IOException {
199
        Classification classification = null;
200
        if (classificationUuid != null){
201
            classification = classificationService.find(classificationUuid);
202
            if(classification == null) {
203
                response.sendError(404 , "Classification not found: " + classificationUuid );
204
                //will not happen
205
                return null;
206
            }
207
        }
208
        return classification;
209
    }
210

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