diff --git a/app/femr/ui/controllers/SettingsController.java b/app/femr/ui/controllers/SettingsController.java index ac29c395d..1873aa260 100644 --- a/app/femr/ui/controllers/SettingsController.java +++ b/app/femr/ui/controllers/SettingsController.java @@ -3,23 +3,37 @@ import com.google.inject.Inject; import controllers.AssetsFinder; import femr.business.services.core.ISessionService; +import femr.business.services.core.IUserService; import femr.common.dtos.CurrentUser; -import femr.ui.views.html.home.index; +import femr.common.dtos.ServiceResponse; +import femr.common.models.UserItem; +import femr.ui.models.settings.EditViewModel; +import femr.ui.views.html.settings.index; + import femr.ui.views.html.sessions.create; +import femr.util.stringhelpers.StringUtils; +import play.data.FormFactory; import play.mvc.Controller; import play.mvc.Result; +import play.data.Form; import java.util.ArrayList; public class SettingsController extends Controller { private final AssetsFinder assetsFinder; - private ISessionService sessionService; + private final FormFactory formFactory; + private final ISessionService sessionService; + private final IUserService userService; @Inject - public SettingsController(AssetsFinder assetsFinder, ISessionService sessionService) { - + public SettingsController(AssetsFinder assetsFinder, + FormFactory formFactory, + ISessionService sessionService, + IUserService userService) { this.assetsFinder = assetsFinder; + this.formFactory = formFactory; this.sessionService = sessionService; + this.userService = userService; } public Result index() { @@ -31,4 +45,59 @@ public Result index() { return ok(create.render(null, 0, null, assetsFinder, new ArrayList())); } + + public Result update() { + CurrentUser currentUser = sessionService.retrieveCurrentUserSession(); + if (currentUser == null) { + return unauthorized(); + } + + Form editViewModelForm = formFactory.form(EditViewModel.class).bindFromRequest(); + if (editViewModelForm.hasErrors()) { + return badRequest(index.render(currentUser, assetsFinder)); + } + + EditViewModel viewModel = editViewModelForm.get(); + ServiceResponse userServiceResponse = userService.retrieveUser(currentUser.getId()); + if (userServiceResponse.hasErrors()) { + return internalServerError(); + } + + UserItem userItem = userServiceResponse.getResponseObject(); + userItem.setFirstName(viewModel.getFirstName()); + userItem.setLastName(viewModel.getLastName()); + userItem.setLanguageCode(viewModel.getLanguageCode()); + + if (StringUtils.isNotNullOrWhiteSpace(viewModel.getPasswordReset()) && viewModel.getPasswordReset().equals("on")) { + userItem.setPasswordReset(true); + } else { + userItem.setPasswordReset(false); + } + + String newPassword = null; + if (StringUtils.isNotNullOrWhiteSpace(viewModel.getNewPassword()) && viewModel.getNewPassword().equals(viewModel.getNewPasswordVerify())) { + newPassword = viewModel.getNewPassword(); + } + + ServiceResponse updateResponse = userService.updateUser(userItem, newPassword); + if (updateResponse.hasErrors()) { + return internalServerError(); + } + + return redirect("/settings"); + } + + + + private EditViewModel convertToEditViewModel(UserItem userItem) { + EditViewModel editViewModel = new EditViewModel(); + editViewModel.setUserId(userItem.getId()); + editViewModel.setFirstName(userItem.getFirstName()); + editViewModel.setLastName(userItem.getLastName()); + editViewModel.setEmail(userItem.getEmail()); + editViewModel.setPasswordReset(Boolean.toString(userItem.isPasswordReset())); + editViewModel.setLanguageCode(userItem.getLanguageCode()); + return editViewModel; + } + } diff --git a/conf/routes b/conf/routes index 3af252653..03a14ed7e 100644 --- a/conf/routes +++ b/conf/routes @@ -72,7 +72,10 @@ GET /search/typeahead/medicationsWithID @femr.ui.controll GET /search/typeahead/medicationAdministrations @femr.ui.controllers.SearchController.typeaheadMedicationAdministrationsJSONGet() #Settings -#GET /settings @femr.ui.controllers.SettingsController.index() +#GET /settings @femr.ui.controllers.SettingsController.index() +POST /settings/update @femr.ui.controllers.SettingsController.update() + + #Photo GET /photo/patient/:id @femr.ui.controllers.PhotoController.GetPatientPhoto(id: Integer, showDefault: Boolean ?= false) GET /photo/encounter/:id @femr.ui.controllers.PhotoController.GetPhoto(id: Int)