Project

General

Profile

Download (6.04 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
 * Copyright (C) 2007 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

    
11

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

    
14
import java.io.IOException;
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.IService;
29
import eu.etaxonomy.cdm.api.service.pager.Pager;
30
import eu.etaxonomy.cdm.model.common.CdmBase;
31
import eu.etaxonomy.cdm.remote.controller.util.PagerParameters;
32
import eu.etaxonomy.cdm.remote.editor.CdmTypePropertyEditor;
33
import eu.etaxonomy.cdm.remote.editor.UUIDPropertyEditor;
34

    
35

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

    
46
    public static final Logger logger = Logger.getLogger(BaseListController.class);
47

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

    
54

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

    
83
        logger.info("doPage() " + requestPathAndQuery(request));
84
        PagerParameters pagerParameters = new PagerParameters(pageSize, pageIndex);
85
        pagerParameters.normalizeAndValidate(response);
86

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

    
94
//    /**
95
//     * Parameter less method to be used as default when request without parameter are made. Otherwise
96
//     * the nameless methods {@link #doPage(Integer, Integer, Class)} and {@link #doList(Integer, Integer, Class)}
97
//     * are ambigous.
98
//     * @return
99
//     * @throws IOException
100
//     */
101
//    @RequestMapping(method = RequestMethod.GET)
102
//    public Pager<T> doPage(HttpServletRequest request, HttpServletResponse response) throws IOException{
103
//        return doPage(null, null, null, request, response);
104
//    }
105

    
106
    /**
107
     * @param start
108
     *            The offset index from the start of the list. The first entity
109
     *            has the index = 0 - <i>required parameter</i>
110
     * @param limit
111
     *            The maximum number of entities returned. - <i>optional parameter</i>
112
     *            If limit is set to a value < 1 all entities will be returned
113
     * @param type
114
     *            Further restricts the type of entities to be returned.
115
     *            If null the base type <code>&lt;T&gt;</code> is being used. - <i>optional parameter</i>
116
     * @return a List of entities
117
     */
118
    @RequestMapping(method = RequestMethod.GET, params = "start")
119
    public List<T> doList(
120
            @RequestParam(value = "start", required = true) Integer start,
121
            @RequestParam(value = "limit", required = false) Integer limit,
122
            @RequestParam(value = "class", required = false) Class<T> type,
123
            HttpServletRequest request,
124
            HttpServletResponse response) {
125

    
126
        if (request != null)
127
        {
128
            logger.info("doList() " + requestPathAndQuery(request));
129
        }
130

    
131
        //if(start == null){ start = 0;}
132
        if(limit == null){ limit = PagerParameters.DEFAULT_PAGESIZE;}
133
        if(limit < 1){ limit = null;}
134
        return service.list(type, limit, start, null, getInitializationStrategy());
135
    }
136

    
137
  /* TODO
138
   @RequestMapping(method = RequestMethod.POST)
139
  public T doPost(@ModelAttribute("object") T object, BindingResult result) {
140
        validator.validate(object, result);
141
        if (result.hasErrors()) {
142
                // set http status code depending upon what happened, possibly return
143
            // the put object and errors so that they can be rendered into a suitable error response
144
        } else {
145
          // should set the status to 201 created  and "Location" header to "/resource/uuid"
146
          service.save(object);
147
        }
148
  }
149
  */
150
}
(8-8/63)