Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RESTWS-755: Modify user resource to include email property which would enable a user email to be edited from reference application #404

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Modify user resource to include email property which would enable a u…
…ser email to be edited from reference application, fixed failing tests.
  • Loading branch information
tendongeryanawo-nasako committed Aug 24, 2019
commit d73bc2d0cceb1b97f15d2f32cd76e01e62200245
Original file line number Diff line number Diff line change
@@ -247,7 +247,7 @@ public void purge(UserAndPassword1_8 user, RequestContext context) throws Respon
*/
@Override
protected NeedsPaging<UserAndPassword1_8> doSearch(RequestContext context) {
// determine roles
// determine roles
List<Role> foundRoles = null;
final String requestedRolesParameter = context.getParameter(PARAMETER_ROLES);
if (requestedRolesParameter != null) {
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@
* {@link Resource} for User, supporting standard CRUD operations
*/
@Resource(name = RestConstants.VERSION_1 + "/user", supportedClass = UserAndPassword1_8.class, supportedOpenmrsVersions = {
"2.0.*", "2.1.*", "2.2.*", "2.3.*" })
"2.0.*", "2.1.*" })
public class UserResource2_0 extends UserResource1_8 {

/**
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**
* 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.resource.openmrs2_2;

import io.swagger.models.Model;
import io.swagger.models.ModelImpl;
import io.swagger.models.properties.StringProperty;
import org.openmrs.User;
import org.openmrs.api.context.Context;
import org.openmrs.module.webservices.rest.web.RestConstants;
import org.openmrs.module.webservices.rest.web.annotation.Resource;
import org.openmrs.module.webservices.rest.web.representation.DefaultRepresentation;
import org.openmrs.module.webservices.rest.web.representation.FullRepresentation;
import org.openmrs.module.webservices.rest.web.representation.Representation;
import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceDescription;
import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_8.UserResource1_8;
import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs2_0.UserResource2_0;
import org.openmrs.module.webservices.rest.web.v1_0.wrapper.openmrs1_8.UserAndPassword1_8;

/**
* {@link Resource} for User, supporting standard CRUD operations
*/
@Resource(name = RestConstants.VERSION_1 + "/user", supportedClass = UserAndPassword1_8.class, supportedOpenmrsVersions = {
"2.2.*", "2.3.*" })
public class UserResource2_2 extends UserResource1_8 {

/**
* @see org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource#getRepresentationDescription(org.openmrs.module.webservices.rest.web.representation.Representation)
*/
@Override
public DelegatingResourceDescription getRepresentationDescription(Representation rep) {
DelegatingResourceDescription description = super.getRepresentationDescription(rep);
if (rep instanceof DefaultRepresentation) {
description.addProperty("email");
}
else if (rep instanceof FullRepresentation) {
description.addProperty("email");
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whats the purpose of adding these if else conditions ? Both conditions are doing the same thing.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first condition checks for defaultRepresentation, second condition checks for Full representation

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah but after checking its doing the same thing description.addProperty("email");

return description;
}

@Override
public DelegatingResourceDescription getCreatableProperties() {
DelegatingResourceDescription description = super.getCreatableProperties();
description.addRequiredProperty("email");
return description;
}

@Override
public Model getGETModel(Representation rep) {
ModelImpl model = (ModelImpl) super.getGETModel(rep);
if (rep instanceof DefaultRepresentation || rep instanceof FullRepresentation) {
model.property("email", new StringProperty());
}
return model;
}

@Override
public Model getCREATEModel(Representation rep) {
return ((ModelImpl) super.getCREATEModel(rep))
.property("email", new StringProperty());
}

/**
* @see org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource#save(java.lang.Object)
*/
@Override
public UserAndPassword1_8 save(UserAndPassword1_8 user) {
User openmrsUser = new User();
if (user.getUser().getUserId() == null) {
openmrsUser = Context.getUserService().createUser(user.getUser(), user.getPassword());
} else {
openmrsUser = Context.getUserService().saveUser(user.getUser());
Context.refreshAuthenticatedUser();
}

return new UserAndPassword1_8(openmrsUser);

}
}