Project

General

Profile

Download (6.04 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.List;
15
import java.util.UUID;
16

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

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

    
27
import eu.etaxonomy.cdm.api.service.IService;
28
import eu.etaxonomy.cdm.api.service.pager.Pager;
29
import eu.etaxonomy.cdm.model.common.CdmBase;
30
import eu.etaxonomy.cdm.remote.controller.util.PagerParameters;
31
import eu.etaxonomy.cdm.remote.editor.CdmTypePropertyEditor;
32
import eu.etaxonomy.cdm.remote.editor.UUIDPropertyEditor;
33

    
34

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

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

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

    
53

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

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

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

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

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

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

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

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