Skip to content

Commit 999306c

Browse files
RESTWS-925: Session endpoint user.person.display should respect the name template.
1 parent c083c03 commit 999306c

File tree

17 files changed

+400
-660
lines changed

17 files changed

+400
-660
lines changed

omod-1.10/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_10/PersonResource1_10Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public void validateFullRepresentation() throws Exception {
3535

3636
@Override
3737
public String getDisplayProperty() {
38-
return "Mr. Horatio Test Hornblower Esq.";
38+
return "Horatio Test Hornblower";
3939
}
4040

4141
@Override

omod-1.11/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_11/PersonResource1_11Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void validateFullRepresentation() throws Exception {
3737

3838
@Override
3939
public String getDisplayProperty() {
40-
return "Mr. Horatio Test Hornblower";
40+
return "Horatio Test Hornblower";
4141
}
4242

4343
@Override

omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PersonResource1_8.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
package org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_8;
1111

12+
import java.lang.reflect.Method;
1213
import java.util.Arrays;
1314
import java.util.Date;
1415
import java.util.List;
@@ -28,6 +29,8 @@
2829
import org.openmrs.PersonAttribute;
2930
import org.openmrs.PersonName;
3031
import org.openmrs.api.context.Context;
32+
import org.openmrs.layout.web.name.NameSupport;
33+
import org.openmrs.layout.web.name.NameTemplate;
3134
import org.openmrs.module.webservices.rest.SimpleObject;
3235
import org.openmrs.module.webservices.rest.web.ConversionUtil;
3336
import org.openmrs.module.webservices.rest.web.RequestContext;
@@ -465,8 +468,22 @@ public String getDisplayString(Person person) {
465468
// TODO copy what is done in PatientResource to use configured name layout
466469
if (person.getPersonName() == null)
467470
return "";
468-
469-
return person.getPersonName().getFullName();
471+
472+
PersonName personName = person.getPersonName();
473+
try {
474+
NameTemplate nameTemplate = NameSupport.getInstance().getDefaultLayoutTemplate();
475+
476+
if (nameTemplate != null) {
477+
// need to use reflection since the format method was not added until later versions of openmrs
478+
Method format = NameTemplate.class.getDeclaredMethod("format", PersonName.class);
479+
return (String) format.invoke(nameTemplate, personName);
480+
}
481+
}
482+
catch (Exception e) {
483+
// fall through to just returning full name if no format method found or format fails
484+
}
485+
486+
return personName.getFullName();
470487
}
471488

472489
private static void copyNameFields(PersonName existingName, PersonName personName) {

omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PatientResource1_8Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public void validateFullRepresentation() throws Exception {
6161

6262
@Override
6363
public String getDisplayProperty() {
64-
return "Mr. Horatio Test Hornblower Esq.";
64+
return "Horatio Test Hornblower";
6565
}
6666

6767
@Override

omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PersonResource1_8Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public void validateFullRepresentation() throws Exception {
7070

7171
@Override
7272
public String getDisplayProperty() {
73-
return "Mr. Horatio Test Hornblower Esq.";
73+
return "Horatio Test Hornblower";
7474
}
7575

7676
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
/**
2+
* This Source Code Form is subject to the terms of the Mozilla Public License,
3+
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
4+
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
5+
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
6+
*
7+
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
8+
* graphic logo is a trademark of OpenMRS Inc.
9+
*/
10+
package org.openmrs.module.webservices.rest.web.v1_0.controller.openmrs1_9;
11+
12+
import org.apache.commons.lang3.LocaleUtils;
13+
import org.openmrs.Location;
14+
import org.openmrs.Provider;
15+
import org.openmrs.User;
16+
import org.openmrs.api.APIException;
17+
import org.openmrs.api.context.Context;
18+
import org.openmrs.module.webservices.rest.SimpleObject;
19+
import org.openmrs.module.webservices.rest.web.ConversionUtil;
20+
import org.openmrs.module.webservices.rest.web.RestConstants;
21+
import org.openmrs.module.webservices.rest.web.api.RestService;
22+
import org.openmrs.module.webservices.rest.web.representation.CustomRepresentation;
23+
import org.openmrs.module.webservices.rest.web.representation.Representation;
24+
import org.openmrs.module.webservices.rest.web.v1_0.controller.BaseRestController;
25+
import org.openmrs.util.PrivilegeConstants;
26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
28+
import org.springframework.beans.factory.annotation.Autowired;
29+
import org.springframework.http.HttpStatus;
30+
import org.springframework.stereotype.Controller;
31+
import org.springframework.web.bind.annotation.RequestBody;
32+
import org.springframework.web.bind.annotation.RequestMapping;
33+
import org.springframework.web.bind.annotation.RequestMethod;
34+
import org.springframework.web.bind.annotation.ResponseBody;
35+
import org.springframework.web.bind.annotation.ResponseStatus;
36+
37+
import javax.servlet.http.HttpServletRequest;
38+
import javax.servlet.http.HttpSession;
39+
import java.util.Collection;
40+
import java.util.HashSet;
41+
import java.util.Locale;
42+
import java.util.Map;
43+
import java.util.Set;
44+
45+
/**
46+
* Controller that lets a client check the status of their session, and log out. (Authenticating is
47+
* handled through a filter, and may happen through this or any other resource.
48+
*/
49+
@Controller
50+
@RequestMapping(value = "/rest/" + RestConstants.VERSION_1 + "/session")
51+
public class SessionController1_9 extends BaseRestController {
52+
53+
private static final Logger log = LoggerFactory.getLogger(SessionController1_9.class);
54+
55+
public static final String USER_CUSTOM_REP = "(uuid,display,username,systemId,userProperties,person:(uuid,display),privileges:(uuid,display,name),roles:(uuid,display,name),links)";
56+
57+
@Autowired
58+
RestService restService;
59+
60+
/**
61+
* Tells the user whether they are authenticated and provides details on the logged-in user
62+
*/
63+
@RequestMapping(method = RequestMethod.GET)
64+
@ResponseBody
65+
public Object get() {
66+
boolean authenticated = Context.isAuthenticated();
67+
SimpleObject session = new SimpleObject();
68+
session.add("authenticated", authenticated);
69+
if (authenticated) {
70+
session.add("user", ConversionUtil.convertToRepresentation(Context.getAuthenticatedUser(),
71+
new CustomRepresentation(USER_CUSTOM_REP)));
72+
session.add("locale", Context.getLocale());
73+
session.add("allowedLocales", Context.getAdministrationService().getAllowedLocales());
74+
session.add("sessionLocation", ConversionUtil.convertToRepresentation(Context.getUserContext().getLocation(), Representation.REF));
75+
session.add("currentProvider", ConversionUtil.convertToRepresentation(getCurrentProvider(), Representation.REF));
76+
}
77+
return session;
78+
}
79+
80+
@RequestMapping(method = RequestMethod.POST)
81+
@ResponseBody
82+
@ResponseStatus(value = HttpStatus.OK)
83+
public Object post(HttpServletRequest request, @RequestBody Map<String, String> body) {
84+
String localeStr = body.get("locale");
85+
if (localeStr != null) {
86+
Locale locale = null;
87+
try {
88+
locale = LocaleUtils.toLocale(localeStr);
89+
}
90+
catch (IllegalArgumentException e) {
91+
throw new APIException(" '" + localeStr + "' does not represent a valid locale.");
92+
}
93+
Set<Locale> allowedLocales = new HashSet<Locale>(Context.getAdministrationService().getAllowedLocales());
94+
if (allowedLocales.contains(locale)) {
95+
Context.setLocale(locale);
96+
} else {
97+
throw new APIException(" '" + localeStr + "' is not in the list of allowed locales.");
98+
}
99+
}
100+
String locationUuid = body.get("sessionLocation");
101+
if (locationUuid != null) {
102+
Location location = Context.getLocationService().getLocationByUuid(locationUuid);
103+
if (location == null) {
104+
throw new APIException(" '" + locationUuid + "' is not the UUID of any location.");
105+
}
106+
Context.getUserContext().setLocation(location);
107+
{ // for compatability with AppUi session location
108+
request.getSession().setAttribute("emrContext.sessionLocationId", location.getId());
109+
}
110+
}
111+
return get();
112+
}
113+
114+
/**
115+
* Logs the client out
116+
*
117+
* <strong>Should</strong> log the client out
118+
*/
119+
@RequestMapping(method = RequestMethod.DELETE)
120+
@ResponseBody
121+
@ResponseStatus(value = HttpStatus.NO_CONTENT)
122+
public void delete(HttpServletRequest request) {
123+
Context.logout();
124+
HttpSession session = request.getSession(false);
125+
if (session != null && request.isRequestedSessionIdValid()) {
126+
session.invalidate();
127+
}
128+
}
129+
130+
/**
131+
* Get current provider
132+
*
133+
* @return Provider if the user is authenticated
134+
*/
135+
private Provider getCurrentProvider() {
136+
Provider currentProvider = null;
137+
User currentUser = Context.getAuthenticatedUser();
138+
if (currentUser != null) {
139+
Collection<Provider> providers = new HashSet<Provider>();
140+
try {
141+
Context.addProxyPrivilege(PrivilegeConstants.VIEW_PROVIDERS);
142+
if (currentUser.getPerson() != null) {
143+
providers = Context.getProviderService().getProvidersByPerson(currentUser.getPerson(), false);
144+
}
145+
}
146+
finally {
147+
Context.removeProxyPrivilege(PrivilegeConstants.VIEW_PROVIDERS);
148+
}
149+
if (providers.size() > 1) {
150+
log.warn("Can't handle users with multiple provider accounts");
151+
} else if (providers.size() == 1) {
152+
currentProvider = providers.iterator().next();
153+
}
154+
}
155+
return currentProvider;
156+
}
157+
}

0 commit comments

Comments
 (0)