Project

General

Profile

Download (8.89 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2009 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.List;
13
import java.util.UUID;
14

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

    
18
import org.apache.commons.lang.StringUtils;
19
import org.springframework.beans.factory.annotation.Autowired;
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.IIdentifiableEntityService;
27
import eu.etaxonomy.cdm.api.service.ITermService;
28
import eu.etaxonomy.cdm.api.service.dto.IdentifiedEntityDTO;
29
import eu.etaxonomy.cdm.api.service.dto.MarkedEntityDTO;
30
import eu.etaxonomy.cdm.api.service.pager.Pager;
31
import eu.etaxonomy.cdm.model.common.CdmBase;
32
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
33
import eu.etaxonomy.cdm.model.common.MarkerType;
34
import eu.etaxonomy.cdm.model.term.DefinedTerm;
35
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
36
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
37
import eu.etaxonomy.cdm.persistence.query.MatchMode;
38
import eu.etaxonomy.cdm.remote.controller.util.PagerParameters;
39
import eu.etaxonomy.cdm.remote.editor.MatchModePropertyEditor;
40

    
41
/**
42
 * @author l.morris
43
 * @since 27 Mar 2012
44
 */
45
public abstract class AbstractIdentifiableListController <T extends IdentifiableEntity, SERVICE extends IIdentifiableEntityService<T>>
46
            extends BaseListController<T,SERVICE>  {
47

    
48
    @InitBinder
49
    @Override
50
    public void initBinder(WebDataBinder binder) {
51
        super.initBinder(binder);
52
        binder.registerCustomEditor(MatchMode.class, new MatchModePropertyEditor());
53
    }
54

    
55
	@Autowired
56
	private ITermService termService;
57

    
58
    /**
59
     * Find IdentifiableEntity objects by name
60
     * <p>
61
     *
62
     * @param query
63
     *            the string to query for. Since the wild-card character '*'
64
     *            internally always is appended to the query string, a search
65
     *            always compares the query string with the beginning of a name.
66
     *            - <i>required parameter</i>
67
     * @param pageIndex
68
     *            the number of the page to be returned, the first page has the
69
     *            pageNumber = 1 - <i>optional parameter</i>
70
     * @param pageSize
71
     *            the maximum number of entities returned per page (can be -1
72
     *            to return all entities in a single page) - <i>optional parameter</i>
73
     * @param matchMode
74
     *           valid values are "EXACT", "BEGINNING", "ANYWHERE", "END" (case sensitive !!!)
75
     * @return a Pager on a list of {@link IdentifiableEntity}s
76
     * @throws IOException
77
     */
78
    @RequestMapping(method = RequestMethod.GET, value={"findByTitle"})
79
    public Pager<T> doFindByTitle(
80
            @RequestParam(value = "query", required = true) String query,
81
            @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
82
            @RequestParam(value = "pageSize", required = false) Integer pageSize,
83
            @RequestParam(value = "matchMode", required = false) MatchMode matchMode,
84
            HttpServletRequest request,
85
            HttpServletResponse response
86
            )
87
             throws IOException {
88

    
89
        logger.info("doFind() : " + requestPathAndQuery(request) );
90

    
91
        PagerParameters pagerParams = new PagerParameters(pageSize, pageIndex);
92
        pagerParams.normalizeAndValidate(response);
93

    
94
        matchMode = matchMode != null ? matchMode : MatchMode.BEGINNING;
95

    
96
        return service.findByTitleWithRestrictions(null, query, matchMode, null, pagerParams.getPageSize(), pagerParams.getPageIndex(), null, initializationStrategy);
97
    }
98

    
99
    /**
100
     * List IdentifiableEntity objects by identifiers
101
     *
102
     * @param type
103
     * @param identifierType
104
     * @param identifier
105
     * @param pageIndex
106
     * @param pageSize
107
     * @param matchMode
108
     * @param request
109
     * @param response
110
     * @param includeEntity
111
     * @return
112
     * @throws IOException
113
     */
114
    @RequestMapping(method = RequestMethod.GET, value={"findByIdentifier"})
115
    public  Pager<IdentifiedEntityDTO<T>> doFindByIdentifier(
116
    		@RequestParam(value = "class", required = false) Class<T> type,
117
    		@RequestParam(value = "identifierType", required = false) String identifierType,
118
            @RequestParam(value = "identifier", required = false) String identifier,
119
            @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
120
            @RequestParam(value = "pageSize", required = false) Integer pageSize,
121
            @RequestParam(value = "matchMode", required = false) MatchMode matchMode,
122
            @RequestParam(value = "includeEntity", required = false) Boolean includeEntity,
123
            HttpServletRequest request,
124
            HttpServletResponse response
125
            )
126
             throws IOException {
127

    
128
    	DefinedTerm definedTerm = null;
129
    	if(StringUtils.isNotBlank(identifierType)){
130
    		identifierType = StringUtils.trim(identifierType);
131
    		UUID identifierTypeUUID = UUID.fromString(identifierType);
132
    		definedTerm = CdmBase.deproxy(termService.find(identifierTypeUUID), DefinedTerm.class);
133
    	}
134

    
135
        logger.info("doFindByIdentifier() : " + requestPathAndQuery(request) );
136

    
137
        PagerParameters pagerParams = new PagerParameters(pageSize, pageIndex).normalizeAndValidate(response);
138

    
139
        matchMode = matchMode != null ? matchMode : MatchMode.EXACT;
140
        boolean includeCdmEntity = includeEntity == null ||  includeEntity == true ? true : false;
141
        return service.findByIdentifier(type, identifier, definedTerm , matchMode, includeCdmEntity, pagerParams.getPageSize(), pagerParams.getPageIndex(), initializationStrategy);
142
    }
143

    
144
    /**
145
     * List identifiable entities by markers
146
     *
147
     * @param type
148
     * @param markerType
149
     * @param value
150
     * @param pageIndex
151
     * @param pageSize
152
     * @param request
153
     * @param response
154
     * @return
155
     * @see AbstractIdentifiableListController#doFindByIdentifier(Class, String, String, Integer, Integer, MatchMode, Boolean, HttpServletRequest, HttpServletResponse)
156
     * @throws IOException
157
     */
158
    @RequestMapping(method = RequestMethod.GET, value={"findByMarker"})
159
    public Pager<MarkedEntityDTO<T>> doFindByMarker(
160
            @RequestParam(value = "class", required = false) Class<T> type,
161
            @RequestParam(value = "markerType", required = true) UUID markerTypeUuid,
162
            @RequestParam(value = "value", required = false) Boolean value,
163
            @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
164
            @RequestParam(value = "pageSize", required = false) Integer pageSize,
165
            @RequestParam(value = "includeEntity", required = false, defaultValue="false") Boolean includeEntity,
166
            HttpServletRequest request,
167
            HttpServletResponse response
168
            )
169
            throws IOException {
170

    
171
        MarkerType markerType = null;
172
        if(markerTypeUuid != null){
173
            DefinedTermBase<?> term = CdmBase.deproxy(termService.find(markerTypeUuid), MarkerType.class);
174
            if (term != null && term.isInstanceOf(MarkerType.class)){
175
                markerType = CdmBase.deproxy(term, MarkerType.class);
176
            }
177
        }
178

    
179
        logger.info("doFindByMarker() " + requestPathAndQuery(request));
180

    
181
        PagerParameters pagerParams = new PagerParameters(pageSize, pageIndex).normalizeAndValidate(response);
182

    
183
        Pager<MarkedEntityDTO<T>> result = service.findByMarker(type, markerType, value, includeEntity, pagerParams.getPageSize(), pagerParams.getPageIndex(), initializationStrategy);
184
        return result;
185
    }
186

    
187
    /**
188
     * List identifiable entities by markers
189
     *
190
     * @param type
191
     * @param request
192
     * @param response
193
     * @return
194
     * @see AbstractIdentifiableListController#doFindByIdentifier(Class, String, String, Integer, Integer, MatchMode, Boolean, HttpServletRequest, HttpServletResponse)
195
     * @throws IOException
196
     */
197
    @RequestMapping(method = RequestMethod.GET, value={"uuidAndTitleCache"})
198
    public List<UuidAndTitleCache<T>> doGetUuidAndTitleCache(
199
            @RequestParam(value = "class", required = false) Class<T> type,
200
            @RequestParam(value = "limit", required = false) Integer limit,
201
            @RequestParam(value = "pattern", required = false) String pattern,
202
            HttpServletRequest request,
203
            HttpServletResponse response
204
            )
205
            throws IOException {
206

    
207
        logger.info("doGetUuidAndTitleCache() " + requestPathAndQuery(request));
208

    
209
        return service.getUuidAndTitleCache(type, limit, pattern);
210
    }
211

    
212
}
(3-3/76)