/**
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.module.webservices.rest.web.v1_0.search.openmrs1_8;
import org.openmrs.User;
import org.openmrs.api.UserService;
import org.openmrs.module.webservices.rest.web.RequestContext;
import org.openmrs.module.webservices.rest.web.RestConstants;
import org.openmrs.module.webservices.rest.web.resource.api.PageableResult;
import org.openmrs.module.webservices.rest.web.resource.api.SearchConfig;
import org.openmrs.module.webservices.rest.web.resource.api.SearchHandler;
import org.openmrs.module.webservices.rest.web.resource.api.SearchQuery;
import org.openmrs.module.webservices.rest.web.resource.impl.EmptySearchResult;
import org.openmrs.module.webservices.rest.web.resource.impl.NeedsPaging;
import org.openmrs.module.webservices.rest.web.response.ResponseException;
import org.openmrs.module.webservices.rest.web.v1_0.wrapper.openmrs1_8.UserAndPassword1_8;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Allows for finding users by username
*/
@Component
public class UserSearchHandler1_8 implements SearchHandler {
@Autowired
@Qualifier("userService")
UserService userService;
private final SearchConfig searchConfig = new SearchConfig("default", RestConstants.VERSION_1 + "/user", Arrays.asList(
"1.8.*", "1.9.*", "1.10.*", "1.11.*", "1.12.*", "2.0.*", "2.1.*"), new SearchQuery.Builder(
"Allows you to find users by username").withRequiredParameters("username").build());
/**
* @see org.openmrs.module.webservices.rest.web.resource.api.SearchHandler#getSearchConfig()
*/
@Override
public SearchConfig getSearchConfig() {
return searchConfig;
}
/**
* @see org.openmrs.module.webservices.rest.web.resource.api.SearchHandler#search(org.openmrs.module.webservices.rest.web.RequestContext)
*/
@Override
public PageableResult search(RequestContext context) throws ResponseException {
String username = context.getParameter("username");
User user = userService.getUserByUsername(username);
if (user == null) {
return new EmptySearchResult();
}
List<UserAndPassword1_8> users = new ArrayList<UserAndPassword1_8>();
users.add(new UserAndPassword1_8(user));
return new NeedsPaging<UserAndPassword1_8>(users, context);
}
}